-
Notifications
You must be signed in to change notification settings - Fork 8
/
outbox.go
172 lines (145 loc) · 4.16 KB
/
outbox.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package workflow
import (
"context"
"strconv"
"time"
"google.golang.org/protobuf/proto"
"k8s.io/utils/clock"
"github.com/luno/workflow/internal/metrics"
"github.com/luno/workflow/internal/outboxpb"
)
func outboxConsumer[Type any, Status StatusType](w *Workflow[Type, Status], config outboxConfig, shard, totalShards int) {
role := makeRole(
w.Name,
"outbox",
"consumer",
strconv.FormatInt(int64(shard), 10),
"of",
strconv.FormatInt(int64(totalShards), 10),
)
// processName can change in value if the string value of the status enum is changed. It should not be used for
// storing in the record store, event streamer, timeoutstore, or offset store.
processName := makeRole(
"outbox",
"consumer",
strconv.FormatInt(int64(shard), 10),
"of",
strconv.FormatInt(int64(totalShards), 10),
)
errBackOff := w.outboxConfig.errBackOff
if config.errBackOff > 0 {
errBackOff = config.errBackOff
}
pollingFrequency := w.outboxConfig.pollingFrequency
if config.pollingFrequency > 0 {
pollingFrequency = config.pollingFrequency
}
lagAlert := w.outboxConfig.lagAlert
if config.lagAlert > 0 {
lagAlert = config.lagAlert
}
w.run(role, processName, func(ctx context.Context) error {
return purgeOutbox[Type, Status](ctx, w.Name, processName, w.recordStore, w.eventStreamer, w.clock, pollingFrequency, lagAlert, config.limit, shard, totalShards)
}, errBackOff)
}
func defaultOutboxConfig() outboxConfig {
return outboxConfig{
parallelCount: 1,
errBackOff: defaultOutboxErrBackOff,
pollingFrequency: defaultOutboxPollingFrequency,
lagAlert: defaultOutboxLagAlert,
limit: 1000,
}
}
type outboxConfig struct {
parallelCount int
errBackOff time.Duration
pollingFrequency time.Duration
lagAlert time.Duration
limit int64
}
func WithOutboxParallelCount(count int) BuildOption {
return func(bo *buildOptions) {
bo.outboxConfig.parallelCount = count
}
}
func WithOutboxPollingFrequency(d time.Duration) BuildOption {
return func(bo *buildOptions) {
bo.outboxConfig.pollingFrequency = d
}
}
func WithOutboxErrBackoff(d time.Duration) BuildOption {
return func(bo *buildOptions) {
bo.outboxConfig.errBackOff = d
}
}
func WithOutboxLookupLimit(limit int64) BuildOption {
return func(bo *buildOptions) {
bo.outboxConfig.limit = limit
}
}
func WithOutboxLagAlert(d time.Duration) BuildOption {
return func(bo *buildOptions) {
bo.outboxConfig.lagAlert = d
}
}
func purgeOutbox[Type any, Status StatusType](
ctx context.Context,
workflowName string,
processName string,
recordStore RecordStore,
streamer EventStreamer,
clock clock.Clock,
pollingFrequency time.Duration,
lagAlert time.Duration,
lookupLimit int64,
shard int,
totalShards int,
) error {
events, err := recordStore.ListOutboxEvents(ctx, workflowName, lookupLimit)
if err != nil {
return err
}
// Send the events to the EventStreamer.
for _, e := range events {
var outboxRecord outboxpb.OutboxRecord
err := proto.Unmarshal(e.Data, &outboxRecord)
if err != nil {
return err
}
headers := make(map[Header]string)
for k, v := range outboxRecord.Headers {
headers[Header(k)] = v
}
event := &Event{
ForeignID: outboxRecord.RunId,
Type: int(outboxRecord.Type),
Headers: headers,
CreatedAt: e.CreatedAt,
}
// Exclude events that should not be consumed by this shard instance
shouldExclude := shardFilter(shard, totalShards)(event)
if shouldExclude {
continue
}
// Push metrics and alerting around the age of the event being processed.
pushLagMetricAndAlerting(workflowName, processName, event.CreatedAt, lagAlert, clock)
t0 := clock.Now()
topic := headers[HeaderTopic]
producer, err := streamer.NewProducer(ctx, topic)
if err != nil {
return err
}
err = producer.Send(ctx, event.ForeignID, event.Type, event.Headers)
if err != nil {
return err
}
err = recordStore.DeleteOutboxEvent(ctx, e.ID)
if err != nil {
return err
}
// Push the time it took to create the producer, send the event, and delete the outbox entry.
metrics.ProcessLatency.WithLabelValues(workflowName, processName).Observe(clock.Since(t0).Seconds())
}
return wait(ctx, pollingFrequency)
}