-
Notifications
You must be signed in to change notification settings - Fork 132
/
ingestion_pipeline.go
279 lines (252 loc) · 9.3 KB
/
ingestion_pipeline.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package ingest
import (
"sync"
"time"
"github.com/apache/pulsar-client-go/pulsar"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"golang.org/x/net/context"
"github.com/armadaproject/armada/internal/armada/configuration"
"github.com/armadaproject/armada/internal/common"
"github.com/armadaproject/armada/internal/common/eventutil"
commonmetrics "github.com/armadaproject/armada/internal/common/ingest/metrics"
"github.com/armadaproject/armada/internal/common/pulsarutils"
"github.com/armadaproject/armada/pkg/armadaevents"
)
// HasPulsarMessageIds should be implemented by structs that can store a batch of pulsar message ids
// This is needed so we can pass message Ids down the pipeline and ack them at the end
type HasPulsarMessageIds interface {
GetMessageIDs() []pulsar.MessageID
}
// InstructionConverter should be implemented by structs that can convert a batch of event sequences into an object
// suitable for passing to the sink
type InstructionConverter[T HasPulsarMessageIds] interface {
Convert(ctx context.Context, msg *EventSequencesWithIds) T
}
// Sink should be implemented by the struct responsible for putting the data in its final resting place, e.g. a
// database.
type Sink[T HasPulsarMessageIds] interface {
// Store should persist the sink. The store is responsible for retrying failed attempts and should only return an error
// When it is satisfied that operation cannot be retries.
Store(ctx context.Context, msg T) error
}
// EventSequencesWithIds consists of a batch of Event Sequences along with the corresponding Pulsar Message Ids
type EventSequencesWithIds struct {
EventSequences []*armadaevents.EventSequence
MessageIds []pulsar.MessageID
}
// IngestionPipeline is a pipeline that reads message from pulsar and inserts them into a sink. The pipeline will
// handle the following automatically:
// - Receiving messages from pulsar
// - Combining messages into batches for efficient processing
// - Unmarshalling into event sequences
// - Acking processed messages
//
// Callers must supply two structs, an InstructionConverter for converting event sequences into something that can be
// exhausted and a Sink capable of exhausting these objects
type IngestionPipeline[T HasPulsarMessageIds] struct {
pulsarConfig configuration.PulsarConfig
metricsConfig configuration.MetricsConfig
metrics *commonmetrics.Metrics
pulsarSubscriptionName string
pulsarBatchSize int
pulsarBatchDuration time.Duration
pulsarSubscriptionType pulsar.SubscriptionType
msgFilter func(msg pulsar.Message) bool
converter InstructionConverter[T]
sink Sink[T]
consumer pulsar.Consumer // for test purposes only
}
// NewIngestionPipeline creates an IngestionPipeline that processes all pulsar messages
func NewIngestionPipeline[T HasPulsarMessageIds](
pulsarConfig configuration.PulsarConfig,
pulsarSubscriptionName string,
pulsarBatchSize int,
pulsarBatchDuration time.Duration,
pulsarSubscriptionType pulsar.SubscriptionType,
converter InstructionConverter[T],
sink Sink[T],
metricsConfig configuration.MetricsConfig,
metrics *commonmetrics.Metrics,
) *IngestionPipeline[T] {
return NewFilteredMsgIngestionPipeline[T](
pulsarConfig,
pulsarSubscriptionName,
pulsarBatchSize,
pulsarBatchDuration,
pulsarSubscriptionType,
func(_ pulsar.Message) bool { return true },
converter,
sink,
metricsConfig,
metrics,
)
}
// NewFilteredMsgIngestionPipeline creates an IngestionPipeline that processes only messages corresponding to the
// supplied message filter
func NewFilteredMsgIngestionPipeline[T HasPulsarMessageIds](
pulsarConfig configuration.PulsarConfig,
pulsarSubscriptionName string,
pulsarBatchSize int,
pulsarBatchDuration time.Duration,
pulsarSubscriptionType pulsar.SubscriptionType,
msgFilter func(msg pulsar.Message) bool,
converter InstructionConverter[T],
sink Sink[T],
metricsConfig configuration.MetricsConfig,
metrics *commonmetrics.Metrics,
) *IngestionPipeline[T] {
return &IngestionPipeline[T]{
pulsarConfig: pulsarConfig,
metricsConfig: metricsConfig,
metrics: metrics,
pulsarSubscriptionName: pulsarSubscriptionName,
pulsarBatchSize: pulsarBatchSize,
pulsarBatchDuration: pulsarBatchDuration,
pulsarSubscriptionType: pulsarSubscriptionType,
msgFilter: msgFilter,
converter: converter,
sink: sink,
}
}
// Run will run the ingestion pipeline until the supplied context is shut down
func (ingester *IngestionPipeline[T]) Run(ctx context.Context) error {
shutdownMetricServer := common.ServeMetrics(ingester.metricsConfig.Port)
defer shutdownMetricServer()
// Waitgroup that wil fire when the pipeline has been torn down
wg := &sync.WaitGroup{}
wg.Add(1)
if ingester.consumer == nil {
consumer, closePulsar, err := ingester.subscribe()
if err != nil {
return err
}
ingester.consumer = consumer
defer closePulsar()
}
pulsarMsgs := pulsarutils.Receive(
ctx,
ingester.consumer,
ingester.pulsarConfig.ReceiveTimeout,
ingester.pulsarConfig.BackoffTime,
ingester.metrics)
// Set up a context that n seconds after ctx
// This gives the rest of the pipeline a chance to flush pending messages
pipelineShutdownContext, cancel := context.WithCancel(context.Background())
go func() {
for {
select {
case <-ctx.Done():
time.Sleep(2 * ingester.pulsarBatchDuration)
log.Infof("Waited for %v: forcing cancel", 2*ingester.pulsarBatchDuration)
cancel()
}
}
}()
// Batch up messages
batchedMsgs := make(chan []pulsar.Message)
batcher := NewBatcher[pulsar.Message](pulsarMsgs, ingester.pulsarBatchSize, ingester.pulsarBatchDuration, func(b []pulsar.Message) { batchedMsgs <- b })
go func() {
batcher.Run(pipelineShutdownContext)
close(batchedMsgs)
}()
// Convert to event sequences
eventSequences := make(chan *EventSequencesWithIds)
go func() {
for msg := range batchedMsgs {
converted := unmarshalEventSequences(msg, ingester.msgFilter, ingester.metrics)
eventSequences <- converted
}
close(eventSequences)
}()
// Convert to instructions
instructions := make(chan T)
go func() {
for msg := range eventSequences {
converted := ingester.converter.Convert(pipelineShutdownContext, msg)
instructions <- converted
}
close(instructions)
}()
// Publish messages to sink then ACK on pulsar
go func() {
for msg := range instructions {
// The sink is responsible for retrying any messages so if we get a message here we know we can give up
// and just ACK the ids
start := time.Now()
err := ingester.sink.Store(pipelineShutdownContext, msg)
taken := time.Now().Sub(start)
if err != nil {
log.WithError(err).Warn("Error inserting messages")
} else {
log.Infof("Inserted %d pulsar messages in %dms", len(msg.GetMessageIDs()), taken.Milliseconds())
}
if errors.Is(err, context.DeadlineExceeded) {
// This occurs when we're shutting down- it's a signal to stop processing immediately
break
} else {
for _, msgId := range msg.GetMessageIDs() {
ingester.consumer.AckID(msgId)
}
}
}
wg.Done()
}()
log.Info("Ingestion pipeline set up. Running until shutdown event received")
// wait for a shutdown event
wg.Wait()
log.Info("Shutdown event received - closing")
return nil
}
func (ingester *IngestionPipeline[T]) subscribe() (pulsar.Consumer, func(), error) {
// Subscribe to Pulsar and receive messages
pulsarClient, err := pulsarutils.NewPulsarClient(&ingester.pulsarConfig)
if err != nil {
return nil, nil, errors.WithMessage(err, "Error creating pulsar client")
}
consumer, err := pulsarClient.Subscribe(pulsar.ConsumerOptions{
Topic: ingester.pulsarConfig.JobsetEventsTopic,
SubscriptionName: ingester.pulsarSubscriptionName,
Type: ingester.pulsarSubscriptionType,
SubscriptionInitialPosition: pulsar.SubscriptionPositionEarliest,
})
if err != nil {
return nil, nil, errors.WithMessage(err, "Error creating pulsar consumer")
}
return consumer, func() {
consumer.Close()
pulsarClient.Close()
}, nil
}
func unmarshalEventSequences(batch []pulsar.Message, msgFilter func(msg pulsar.Message) bool, metrics *commonmetrics.Metrics) *EventSequencesWithIds {
sequences := make([]*armadaevents.EventSequence, 0, len(batch))
messageIds := make([]pulsar.MessageID, len(batch))
for i, msg := range batch {
// Record the messageId- we need to record all message Ids, even if the event they contain is invalid
// As they must be acked at the end
messageIds[i] = msg.ID()
// If we're not interested in this then continue
if !msgFilter(msg) {
continue
}
// Try and unmarshall the proto
es, err := eventutil.UnmarshalEventSequence(context.Background(), msg.Payload())
if err != nil {
metrics.RecordPulsarMessageError(commonmetrics.PulsarMessageErrorDeserialization)
log.WithError(err).Warnf("Could not unmarshal proto for msg %s", msg.ID())
continue
}
// Fill in time if it is not set
// TODO - once created is set everywhere we can remove this
for _, event := range es.Events {
if event.GetCreated() == nil {
publishTime := msg.PublishTime()
event.Created = &publishTime
}
}
sequences = append(sequences, es)
}
return &EventSequencesWithIds{
EventSequences: sequences, MessageIds: messageIds,
}
}