forked from obsidiandynamics/goharvest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
harvest.go
549 lines (478 loc) · 15.4 KB
/
harvest.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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
package goharvest
import (
"fmt"
"runtime/debug"
"sync"
"sync/atomic"
"time"
"github.com/google/uuid"
"github.com/obsidiandynamics/goharvest/metric"
"github.com/obsidiandynamics/goneli"
"github.com/obsidiandynamics/libstdgo/concurrent"
"github.com/obsidiandynamics/libstdgo/diags"
"github.com/obsidiandynamics/libstdgo/scribe"
"gopkg.in/confluentinc/confluent-kafka-go.v1/kafka"
_ "gopkg.in/confluentinc/confluent-kafka-go.v1/kafka/librdkafka"
)
var noLeader uuid.UUID
// State of the Harvest instance.
type State int
const (
// Created — initialised (configured) but not started.
Created State = iota
// Running — currently running.
Running
// Stopping — in the process of being stopped. I.e. Stop() has been invoked, but workers are still running.
Stopping
// Stopped — has been completely disposed of.
Stopped
)
type tracedPanic struct {
cause interface{}
stack string
}
func (e tracedPanic) Error() string {
return fmt.Sprintf("%v\n%s", e.cause, e.stack)
}
// Harvest performs background harvesting of a transactional outbox table.
type Harvest interface {
Start() error
Stop()
Await() error
State() State
IsLeader() bool
LeaderID() *uuid.UUID
InFlightRecords() int
InFlightRecordKeys() []string
SetEventHandler(eventHandler EventHandler)
}
const watcherTimeout = 60 * time.Second
type harvest struct {
config Config
producerConfigs KafkaConfigMap
scribe scribe.Scribe
state concurrent.AtomicReference
shouldBeRunningFlag concurrent.AtomicCounter
neli goneli.Neli
leaderID atomic.Value
db DatabaseBinding
queuedRecords concurrent.AtomicCounter
inFlightRecords concurrent.AtomicCounter
inFlightKeys concurrent.Scoreboard
throughput *metric.Meter
throughputLock sync.Mutex
panicCause atomic.Value
eventHandler EventHandler
forceRemarkFlag concurrent.AtomicCounter
sendBattery battery
}
// New creates a new Harvest instance from the supplied config.
func New(config Config) (Harvest, error) {
config.SetDefaults()
if err := config.Validate(); err != nil {
return nil, err
}
h := &harvest{
config: config,
scribe: config.Scribe,
state: concurrent.NewAtomicReference(Created),
shouldBeRunningFlag: concurrent.NewAtomicCounter(1),
queuedRecords: concurrent.NewAtomicCounter(),
inFlightRecords: concurrent.NewAtomicCounter(),
inFlightKeys: concurrent.NewScoreboard(*config.Limits.SendConcurrency),
forceRemarkFlag: concurrent.NewAtomicCounter(),
eventHandler: func(e Event) {},
}
h.leaderID.Store(noLeader)
h.producerConfigs = copyKafkaConfig(h.config.BaseKafkaConfig)
putAllKafkaConfig(h.config.ProducerKafkaConfig, h.producerConfigs)
err := setKafkaConfigs(h.producerConfigs, KafkaConfigMap{
"enable.idempotence": true,
})
if err != nil {
return nil, err
}
return h, nil
}
// State obtains the present state of this Harvest instance.
func (h *harvest) State() State {
return h.state.Get().(State)
}
func (h *harvest) logger() scribe.StdLogAPI {
return h.scribe.Capture(h.scene())
}
func (h *harvest) scene() scribe.Scene {
return scribe.Scene{Fields: scribe.Fields{
"name": h.config.Name,
"lib": "goharvest",
}}
}
func (h *harvest) cleanupFailedStart() {
if h.State() != Created {
return
}
if h.db != nil {
h.db.Dispose()
}
}
// Start the harvester.
func (h *harvest) Start() error {
ensureState(h.State() == Created, "Cannot start at this time")
defer h.cleanupFailedStart()
db, err := h.config.DatabaseBindingProvider(h.config.DataSource, h.config.OutboxTable)
if err != nil {
return err
}
h.db = db
neliConfig := goneli.Config{
KafkaConfig: configToNeli(h.config.BaseKafkaConfig),
LeaderTopic: h.config.LeaderTopic,
LeaderGroupID: h.config.LeaderGroupID,
KafkaConsumerProvider: convertKafkaConsumerProvider(h.config.KafkaConsumerProvider),
KafkaProducerProvider: convertKafkaProducerProvider(h.config.KafkaProducerProvider),
Scribe: h.config.Scribe,
Name: h.config.Name,
PollDuration: h.config.Limits.PollDuration,
MinPollInterval: h.config.Limits.MinPollInterval,
HeartbeatTimeout: h.config.Limits.HeartbeatTimeout,
}
h.logger().T()("Creating NELI with config %v", neliConfig)
n, err := h.config.NeliProvider(neliConfig, func(e goneli.Event) {
switch e.(type) {
case goneli.LeaderAcquired:
h.onAcquired()
case goneli.LeaderRevoked:
h.onRevoked()
case goneli.LeaderFenced:
h.onFenced()
}
})
if err != nil {
return err
}
h.neli = n
h.throughput = metric.NewMeter("throughput", *h.config.Limits.MinMetricsInterval)
h.state.Set(Running)
go backgroundPoller(h)
return nil
}
// IsLeader returns true if the current Harvest is the leader among competing instances.
func (h *harvest) IsLeader() bool {
return h.LeaderID() != nil
}
// LeaderID returns the leader UUID of the current instance, if it is a leader at the time of this call.
// Otherwise, a nil is returned.
func (h *harvest) LeaderID() *uuid.UUID {
if stored := h.leaderID.Load().(uuid.UUID); stored != noLeader {
return &stored
}
return nil
}
// InFlightRecords returns the number of in-flight records; i.e. records that have been published on Kafka for which an
// acknowledgement is still pending.
func (h *harvest) InFlightRecords() int {
return h.inFlightRecords.GetInt()
}
// InFlightRecordKeys returns the keys of records that are still in-flight. For any given key, there will be at most one
// record pending acknowledgement.
func (h *harvest) InFlightRecordKeys() []string {
view := h.inFlightKeys.View()
keys := make([]string, len(view))
i := 0
for k := range view {
keys[i] = k
i++
}
return keys
}
// SetEventHandler assigns an optional event handler callback to be notified of changes in leader state as well as other
// events of interest.
//
// This method must be invoked prior to Start().
func (h *harvest) SetEventHandler(eventHandler EventHandler) {
ensureState(h.State() == Created, "Cannot set event handler at this time")
h.eventHandler = eventHandler
}
func (h *harvest) shouldBeRunning() bool {
return h.shouldBeRunningFlag.Get() == 1
}
func (h *harvest) reportPanic(goroutineName string) {
if r := recover(); r != nil {
h.logger().E()("Caught panic in %s: %v", goroutineName, r)
h.panicCause.Store(tracedPanic{r, string(debug.Stack())})
h.logger().E()(string(debug.Stack()))
h.Stop()
}
}
func ensureState(expected bool, format string, args ...interface{}) {
if !expected {
panic(fmt.Errorf("state assertion failed: "+format, args...))
}
}
func backgroundPoller(h *harvest) {
h.logger().I()("Starting background poller")
defer h.logger().I()("Stopped")
defer h.state.Set(Stopped)
defer h.reportPanic("background poller")
defer h.db.Dispose()
defer h.neli.Close()
defer h.shutdownSendBattery()
defer h.state.Set(Stopping)
defer h.logger().I()("Stopping")
for h.shouldBeRunning() {
isLeader, err := h.neli.Pulse(1 * time.Millisecond)
if err != nil {
panic(err)
}
if isLeader {
if h.forceRemarkFlag.Get() == 1 {
h.logger().D()("Remark requested")
h.shutdownSendBattery()
h.refreshLeader()
}
if h.sendBattery == nil {
inFlightRecordsValue := h.inFlightRecords.Get()
ensureState(inFlightRecordsValue == 0, "inFlightRecords=%d", inFlightRecordsValue)
inFlightKeysView := h.inFlightKeys.View()
ensureState(len(inFlightKeysView) == 0, "inFlightKeys=%d", inFlightKeysView)
h.spawnSendBattery()
}
onLeaderPoll(h)
}
}
}
func (h *harvest) spawnSendBattery() {
ensureState(h.sendBattery == nil, "send battery not nil before spawn")
h.logger().D()("Spawning send battery")
h.sendBattery = newConcurrentBattery(*h.config.Limits.SendConcurrency, *h.config.Limits.SendBuffer, func(records chan OutboxRecord) {
defer h.reportPanic("send cell")
h.logger().T()("Creating Kafka producer with config %v", h.producerConfigs)
prod, err := h.config.KafkaProducerProvider(&h.producerConfigs)
if err != nil {
panic(err)
}
deliveryHandlerDone := make(chan int)
go backgroundDeliveryHandler(h, prod, deliveryHandlerDone)
defer func() {
<-deliveryHandlerDone
}()
defer func() {
go func() {
// A bug in confluent-kafka-go (#463) occasionally causes an indefinite syscall hang in Close(), after it closes
// the Events channel. So we delegate this to a separate goroutine — better an orphaned goroutine than a
// frozen harvester. (The rest of the battery will still unwind normally.)
closeWatcher := h.watch("close producer")
prod.Close()
closeWatcher.End()
}()
}()
var lastID *int64
for rec := range records {
ensureState(lastID == nil || rec.ID >= *lastID, "discontinuity for key %s: ID %s, lastID: %v", rec.KafkaKey, rec.ID, lastID)
lastID = &rec.ID
m := &kafka.Message{
TopicPartition: kafka.TopicPartition{Topic: &rec.KafkaTopic, Partition: kafka.PartitionAny},
Key: []byte(rec.KafkaKey),
Value: stringPointerToByteArray(rec.KafkaValue),
Opaque: rec,
Headers: toNativeKafkaHeaders(rec.KafkaHeaders),
}
h.inFlightRecords.Drain(int64(*h.config.Limits.MaxInFlightRecords-1), concurrent.Indefinitely)
startTime := time.Now()
for {
if h.deadlineExceeded("poll", h.neli.Deadline().Elapsed(), *h.config.Limits.MaxPollInterval) {
break
}
if h.deadlineExceeded("message queueing", time.Now().Sub(startTime), *h.config.Limits.QueueTimeout) {
break
}
if remaining := h.inFlightKeys.Drain(rec.KafkaKey, 0, *h.config.Limits.DrainInterval); remaining <= 0 {
ensureState(remaining == 0, "drain failed: %d remaining in-flight records for key %s", remaining, rec.KafkaKey)
break
}
h.logger().D()("Drain stalled for record %d (key %s)", rec.ID, rec.KafkaKey)
}
if h.forceRemarkFlag.Get() == 1 {
h.queuedRecords.Dec()
continue
}
h.inFlightRecords.Inc()
h.queuedRecords.Dec()
h.inFlightKeys.Inc(rec.KafkaKey)
err := prod.Produce(m, nil)
if err != nil {
h.logger().W()("Error publishing record %v: %v", rec, err)
h.inFlightKeys.Dec(rec.KafkaKey)
h.inFlightRecords.Dec()
h.forceRemarkFlag.Set(1)
}
}
})
}
func stringPointerToByteArray(str *string) []byte {
if str != nil {
return []byte(*str)
}
return nil
}
func (h *harvest) shutdownSendBattery() {
if h.sendBattery != nil {
shutdownWatcher := h.watch("shutdown send battery")
h.logger().D()("Shutting down send battery")
// Expedite shutdown by raising the remark flag, forcing any queued records to be skipped.
h.forceRemarkFlag.Set(1)
// Take the battery down, waiting for all goroutines to complete.
h.sendBattery.shutdown()
h.sendBattery = nil
// Reset flags and counters for next time.
h.forceRemarkFlag.Set(0)
h.inFlightRecords.Set(0)
h.inFlightKeys.Clear()
h.logger().D()("Send battery terminated")
shutdownWatcher.End()
}
}
func onLeaderPoll(h *harvest) {
markBegin := time.Now()
records, err := h.db.Mark(*h.LeaderID(), *h.config.Limits.MarkQueryRecords)
if err != nil {
h.logger().W()("Error executing mark query: %v", err)
// When an error occurs during marking, we cannot just backoff and retry, as the error could have
// occurred on the return leg (i.e. DB operation succeeded on the server, but timed out on the client).
h.forceRemarkFlag.Set(1)
time.Sleep(*h.config.Limits.IOErrorBackoff)
return
}
if len(records) > 0 {
sendBegin := time.Now()
h.logger().T()("Leader poll: marked %d in the range %d-%d, took %v",
len(records), records[0].ID, records[len(records)-1].ID, sendBegin.Sub(markBegin))
enqueueWatcher := h.watch("enqueue marked records")
for _, rec := range records {
h.queuedRecords.Inc()
h.sendBattery.enqueue(rec)
}
enqueueWatcher.End()
h.logger().T()("Send took %v", time.Now().Sub(sendBegin))
} else {
time.Sleep(*h.config.Limits.MarkBackoff)
}
}
func (h *harvest) watch(operation string) *diags.Watcher {
return diags.Watch(operation, watcherTimeout, diags.Print(h.logger().W()))
}
func (h *harvest) refreshLeader() {
newLeaderID, _ := uuid.NewRandom()
h.leaderID.Store(newLeaderID)
h.logger().W()("Refreshed leader ID: %v", newLeaderID)
h.eventHandler(LeaderRefreshed{newLeaderID})
}
func (h *harvest) deadlineExceeded(deadline string, elapsed time.Duration, threshold time.Duration) bool {
if excess := elapsed - threshold; excess > 0 {
if h.forceRemarkFlag.CompareAndSwap(0, 1) {
h.logger().W()("Exceeded %s deadline by %v", deadline, excess)
}
return true
}
return false
}
func backgroundDeliveryHandler(h *harvest, prod KafkaProducer, done chan int) {
h.logger().I()("Starting background delivery handler")
defer h.reportPanic("background delivery handler")
defer close(done)
for e := range prod.Events() {
switch ev := e.(type) {
case *kafka.Message:
rec := ev.Opaque.(OutboxRecord)
if ev.TopicPartition.Error != nil {
onFailedDelivery(h, rec, ev.TopicPartition.Error)
} else {
onSuccessfulDelivery(h, rec)
h.updateStats()
}
default:
h.logger().I()("Observed event: %v (%T)", e, e)
}
}
}
func (h *harvest) updateStats() {
h.throughputLock.Lock()
defer h.throughputLock.Unlock()
h.throughput.MaybeStatsCall(func(stats metric.MeterStats) {
h.logger().D()("%v", stats)
h.eventHandler(MeterRead{stats})
})
h.throughput.Add(1)
}
func onSuccessfulDelivery(h *harvest, rec OutboxRecord) {
for {
done, err := h.db.Purge(rec.ID)
if err == nil {
if !done {
h.logger().W()("Did not purge record %v", rec)
}
break
}
h.logger().W()("Error executing purge query for record %v: %v", rec, err)
time.Sleep(*h.config.Limits.IOErrorBackoff)
}
h.inFlightKeys.Dec(rec.KafkaKey)
h.inFlightRecords.Dec()
}
func onFailedDelivery(h *harvest, rec OutboxRecord, err error) {
h.logger().W()("Delivery failed for %v, err: %v", rec, err)
for {
done, err := h.db.Reset(rec.ID)
if err == nil {
if !done {
h.logger().W()("Did not reset record %v", rec)
} else {
h.forceRemarkFlag.Set(1)
}
break
}
h.logger().W()("Error executing reset query for record %v: %v", rec, err)
time.Sleep(*h.config.Limits.IOErrorBackoff)
}
h.inFlightKeys.Dec(rec.KafkaKey)
h.inFlightRecords.Dec()
}
func (h *harvest) onAcquired() {
newLeaderID, _ := uuid.NewRandom()
h.leaderID.Store(newLeaderID)
h.logger().I()("Elected as leader, ID: %v", newLeaderID)
h.eventHandler(LeaderAcquired{newLeaderID})
}
func (h *harvest) onRevoked() {
h.logger().I()("Lost leader status")
h.cleanupLeaderState()
h.eventHandler(LeaderRevoked{})
}
func (h *harvest) onFenced() {
h.logger().W()("Leader fenced")
h.cleanupLeaderState()
h.eventHandler(LeaderFenced{})
}
func (h *harvest) cleanupLeaderState() {
h.shutdownSendBattery()
h.leaderID.Store(noLeader)
}
// Stop the harvester, returning immediately.
//
// This method does not wait until the underlying Goroutines have been terminated
// and all resources have been disposed off properly. This is accomplished by calling Await()
func (h *harvest) Stop() {
h.shouldBeRunningFlag.Set(0)
}
// Await the termination of this Harvest instance.
//
// This method blocks indefinitely, returning only when this instance has completed an orderly shutdown. I.e.
// when all Goroutines have returned and all resources have been disposed of.
func (h *harvest) Await() error {
h.state.Await(concurrent.RefEqual(Stopped), concurrent.Indefinitely)
panicCause := h.panicCause.Load()
if panicCause != nil {
return panicCause.(tracedPanic)
}
return nil
}