-
Notifications
You must be signed in to change notification settings - Fork 4
/
kafka_test.go
492 lines (462 loc) · 12.6 KB
/
kafka_test.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
package kafka
import (
"context"
"encoding/json"
"errors"
"fmt"
"github.com/celerway/metamorphosis/bridge/observability"
"github.com/celerway/metamorphosis/log"
is2 "github.com/matryer/is"
"github.com/segmentio/kafka-go"
"os"
"sync"
"sync/atomic"
"testing"
"time"
)
type mockWriter struct {
mu sync.Mutex
storage []kafka.Message
failed bool
msgs uint64
writes uint64
deadlock bool
batchDelay time.Duration
msgDelay time.Duration
}
func (m *mockWriter) WriteMessages(ctx context.Context, msgs ...kafka.Message) error {
m.mu.Lock()
defer m.mu.Unlock()
// if deadlock, block until context is cancelled
if m.deadlock {
log.Warn("writer is deadlocked")
<-ctx.Done()
}
time.Sleep(m.batchDelay + m.msgDelay*time.Duration(len(msgs)))
if m.failed {
return errors.New("storage is in a failed state")
}
if m.storage == nil {
m.storage = make([]kafka.Message, 0)
}
l := uint64(len(msgs))
log.Debugf("Writing %d messages to pretend kafka", l)
m.storage = append(m.storage, msgs...)
atomic.AddUint64(&m.msgs, l)
atomic.AddUint64(&m.writes, 1)
return nil
}
func (m *mockWriter) setDelay(batchDelay, msgDelay time.Duration) {
log.Infof("Setting storage delay to %v for batch / %v for msg", batchDelay, msgDelay)
m.mu.Lock()
defer m.mu.Unlock()
m.batchDelay = batchDelay
m.msgDelay = msgDelay
}
func (m *mockWriter) setState(failed bool) {
log.Info("Setting storage failed state to ", failed)
m.mu.Lock()
defer m.mu.Unlock()
m.failed = failed
}
func (m *mockWriter) setDeadlock(deadlock bool) {
log.Info("Setting storage deadlock to ", deadlock)
m.mu.Lock()
defer m.mu.Unlock()
m.deadlock = deadlock
}
func (m *mockWriter) getMessage(id int) (Message, error) {
m.mu.Lock()
defer m.mu.Unlock()
if id >= len(m.storage) {
return Message{}, errors.New("message not found")
}
var Msg Message
err := json.Unmarshal(m.storage[id].Value, &Msg)
if err != nil {
return Message{}, err
}
return Msg, nil
}
func (m *mockWriter) getDecodedMessage(id int) (Message, error) {
m.mu.Lock()
defer m.mu.Unlock()
if id >= len(m.storage) {
return Message{}, errors.New("message not found")
}
mess := m.storage[id]
var Msg Message
err := json.Unmarshal(mess.Value, &Msg)
if err != nil {
return Message{}, err
}
return Msg, nil
}
func waitForAtomic(a *uint64, v uint64, timeout, sleeptime time.Duration) error {
start := time.Now()
for time.Since(start) < timeout {
if atomic.LoadUint64(a) >= v {
return nil
}
time.Sleep(sleeptime)
}
return fmt.Errorf("waitForAtomic (waiting for %d, is %d) timed out after %v", v, atomic.LoadUint64(a), timeout)
}
func TestMain(m *testing.M) {
log.SetLevel(log.InfoLevel)
log.Debug("Running test suite")
ret := m.Run()
log.Debug("Test suite complete")
os.Exit(ret)
}
// Test that we can start and stop a buffer.
func TestBuffer_Run(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
storage := &mockWriter{}
buffer := makeTestBuffer(storage, defaultLogger())
defer close(buffer.obsChannel)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
err := buffer.Run(ctx)
if err != nil {
log.Errorf("Error %s", err)
}
log.Info("buffer run complete")
}()
time.Sleep(100 * time.Millisecond)
cancel()
log.Debug("Cancel issued. Waiting.")
wg.Wait()
log.Debug("Done")
}
func defaultLogger() *log.Logger {
logger := log.NewLogger(os.Stdout, os.Stderr)
logger.SetLevel(log.DebugLevel)
return logger
}
func makeTestBuffer(writer *mockWriter, logger *log.Logger) buffer {
obsChannel := make(observability.Channel)
go func() { // service the obs channel.
for range obsChannel {
}
}()
return buffer{
interval: 2 * time.Millisecond,
failureRetryInterval: 200 * time.Millisecond,
buffer: make([]kafka.Message, 0, 10),
topic: "unittest",
writer: writer,
C: make(chan Message),
batchSize: 5,
maxBatchSize: 20,
kafkaTimeout: 25 * time.Millisecond,
obsChannel: obsChannel,
testMessageTopic: "test",
logger: logger,
}
}
// Simple test. Send 10 messages and check that they are all received.
func TestBuffer_Process_ok(t *testing.T) {
is := is2.New(t)
storage := &mockWriter{}
ctx, cancel := context.WithCancel(context.Background())
buffer := makeTestBuffer(storage, defaultLogger())
defer close(buffer.obsChannel)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
err := buffer.Run(ctx)
if err != nil {
log.Errorf("Error %s", err)
}
log.Info("buffer run complete")
}()
for i := 0; i < 10; i++ {
buffer.C <- makeMessage("test", i)
}
cancel()
wg.Wait()
for i := 1; i <= 10; i++ {
m, err := storage.getDecodedMessage(i)
is.NoErr(err)
is.Equal([]byte(fmt.Sprintf("%d", i-1)), m.Content)
}
log.Debug("Done")
}
// Somewhat more advanced. Induce a failure and check that the buffer recovers.
func TestBuffer_Process_fail(t *testing.T) {
storage := &mockWriter{}
ctx, cancel := context.WithCancel(context.Background())
buffer := makeTestBuffer(storage, defaultLogger())
defer close(buffer.obsChannel)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
err := buffer.Run(ctx)
if err != nil {
log.Errorf("Error %s", err)
}
log.Info("buffer run complete")
}()
log.Info("Sending msgs 0 -> 5 ")
for i := 0; i < 5; i++ {
buffer.C <- makeMessage("test", i)
}
storage.setState(true)
log.Info("Sending msgs 5 -> 10")
for i := 5; i < 10; i++ {
buffer.C <- makeMessage("test", i)
}
log.Info("Done with msgs")
time.Sleep(100 * time.Millisecond)
storage.setState(false)
time.Sleep(1 * time.Second)
cancel()
wg.Wait()
for i := 0; i < 10; i++ {
m, err := storage.getMessage(i)
if err != nil {
t.Errorf("Error getting message %d: %s", i, err)
}
fmt.Printf("Message: %s\n", string(m.Content))
}
log.Debug("Done")
}
// Test with the buffer in a failed state at startup.
func TestBuffer_Process_initial_fail(t *testing.T) {
is := is2.New(t)
storage := &mockWriter{}
storage.setState(true)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
buffer := makeTestBuffer(storage, defaultLogger())
defer close(buffer.obsChannel)
err := buffer.Run(ctx)
is.True(err != nil) // should be error
fmt.Println("Expected error: ", err)
}
// Test with the buffer deadlocking
func TestBuffer_deadlock(t *testing.T) {
is := is2.New(t)
is.True(true)
storage := &mockWriter{}
storage.setDeadlock(true)
buffer := makeTestBuffer(storage, defaultLogger())
defer close(buffer.obsChannel)
wg := sync.WaitGroup{}
wg.Add(1)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
defer wg.Done()
err := buffer.Run(ctx)
if err != nil {
log.Errorf("Error %s", err)
}
log.Info("buffer run complete")
}()
for i := 0; i < 50; i++ {
buffer.C <- makeMessage("test", i)
}
time.Sleep(time.Millisecond * 100)
cancel() // release the deadlock.
time.Sleep(time.Millisecond * 100)
for i := 0; i < 50; i++ {
_, err := storage.getMessage(i)
if err != nil {
t.Errorf("Error getting message %d: %s", i, err)
}
/*
topic := m.Topic
body := m.Content
fmt.Printf("Topic: %s Message: %s\n", topic, body)
*/
}
}
// TestBuffer_Process_slow - Induces slowness into the writer.
// It guards against re-ordering of the messages
func TestBuffer_Process_slow(t *testing.T) {
const noOfMessages = 500
is := is2.New(t)
storage := &mockWriter{}
storage.setDelay(2*time.Millisecond, time.Microsecond*20)
ctx, cancel := context.WithCancel(context.Background())
buffer := makeTestBuffer(storage, defaultLogger())
defer close(buffer.obsChannel)
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
err := buffer.Run(ctx)
if err != nil {
log.Errorf("Error %s", err)
}
log.Info("buffer run complete")
}()
for i := 0; i < noOfMessages; i++ {
buffer.C <- makeMessage("test", i)
time.Sleep(time.Microsecond * 10)
}
log.Info("Messages are sent")
err := waitForAtomic(&storage.msgs, noOfMessages+1, time.Millisecond*5000, time.Millisecond)
if err != nil {
t.Errorf("Error %s", err)
}
cancel()
wg.Wait()
for i := 1; i < noOfMessages; i++ {
m, err := storage.getMessage(i)
is.NoErr(err)
is.Equal(fmt.Sprintf("%d", i-1), string(m.Content))
is.Equal("test", m.Topic)
}
is.Equal(storage.msgs, uint64(noOfMessages+1))
fmt.Println("==== Done ==== ")
fmt.Printf("Writes %d ", storage.writes)
fmt.Printf("Messages %d", storage.msgs)
fmt.Println("\n =========== ")
}
func TestBuffer_Batching(t *testing.T) {
const batchSize = 100
const totalMsgs = 10000
storage := &mockWriter{}
buffer := makeTestBuffer(storage, defaultLogger())
defer close(buffer.obsChannel)
buffer.batchSize = batchSize
buffer.maxBatchSize = 1000
ctx, cancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
err := buffer.Run(ctx)
if err != nil {
log.Errorf("Error %s", err)
}
log.Info("buffer run complete")
}()
storage.setState(false)
for i := 0; i < totalMsgs; i++ {
buffer.C <- makeMessage("test", i)
}
storage.setState(false)
start := time.Now()
err := waitForAtomic(&storage.msgs, totalMsgs+1, time.Millisecond*500, time.Millisecond)
dur := time.Since(start)
log.Info("Duration: ", dur)
if err != nil {
t.Errorf("waitForAtomic Error %s", err)
}
if atomic.LoadUint64(&storage.writes) != batchSize+1 {
t.Errorf("Wrong number of batched writes: %d", atomic.LoadUint64(&storage.writes))
}
if atomic.LoadUint64(&storage.msgs) != totalMsgs+1 {
t.Errorf("Wrong number of messages: %d", atomic.LoadUint64(&storage.msgs))
}
cancel()
wg.Wait()
log.Debug("Done")
}
// Get the buffer up and running. Fails it and then proceeed to rewrite
// 10000 messages to it. See if it recovers and clears all the messages.
func TestBuffer_Batching_Recovery(t *testing.T) {
is := is2.New(t)
storage := &mockWriter{}
buffer := makeTestBuffer(storage, defaultLogger())
defer close(buffer.obsChannel)
buffer.batchSize = 100
buffer.maxBatchSize = 1000
ctx, cancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
err := buffer.Run(ctx)
if err != nil {
log.Errorf("Error %s", err)
}
log.Info("buffer run complete")
}()
storage.setState(false)
buffer.C <- makeMessage("test", 0)
storage.setState(true)
for i := 0; i < 10000; i++ {
buffer.C <- makeMessage("test", i)
}
storage.setState(false)
err := waitForAtomic(&storage.msgs, 10002, time.Millisecond*2000, time.Millisecond)
log.Infof("Writes: %d", atomic.LoadUint64(&storage.writes))
log.Infof("Msgs: %d", atomic.LoadUint64(&storage.msgs))
log.Infof("Failures: %d", buffer.failures)
is.NoErr(err)
is.Equal(atomic.LoadUint64(&storage.msgs), uint64(10002))
is.Equal(atomic.LoadUint64(&storage.writes), uint64(12))
is.Equal(buffer.failures, 1) // We expect one failure here.
cancel()
wg.Wait()
log.Debug("Done")
}
// Pump a 1000 messages into the buffer when storage is failed.
// Have the storage recover.
// Interrrupt the recovery by failing the storage in the middle of the recovery.
// Then have the storage recover again
// Finally check that all messages have been written to the storage correctly in the right order.
func TestBuffer_Batching_RecoveryInterrupted(t *testing.T) {
const count = 1000
is := is2.New(t)
storage := &mockWriter{}
buffer := makeTestBuffer(storage, defaultLogger())
defer close(buffer.obsChannel)
buffer.batchSize = 10
buffer.maxBatchSize = 100
storage.batchDelay = time.Millisecond
ctx, cancel := context.WithCancel(context.Background())
wg := sync.WaitGroup{}
wg.Add(1)
go func() {
defer wg.Done()
err := buffer.Run(ctx)
if err != nil {
log.Errorf("Error %s", err)
}
log.Info("buffer run complete")
}()
time.Sleep(10 * time.Millisecond)
storage.setState(true)
for i := 1; i <= count; i++ {
buffer.C <- makeMessage("test", i)
}
log.Info("Pumped 1000 messages into buffer")
storage.setState(false)
err := waitForAtomic(&storage.msgs, 500, time.Second*3, time.Nanosecond*100)
is.NoErr(err)
storage.setState(true)
time.Sleep(100 * time.Millisecond)
storage.setState(false)
err = waitForAtomic(&storage.msgs, 1000, time.Second*3, time.Nanosecond*100)
is.NoErr(err)
log.Infof("Writes: %d", atomic.LoadUint64(&storage.writes))
log.Infof("Msgs: %d", atomic.LoadUint64(&storage.msgs))
log.Infof("Failures: %d", buffer.failures)
cancel()
wg.Wait()
for i := 1; i <= count; i++ {
msg := storage.storage[i]
val := msg.Value
jmsg := Message{}
err := json.Unmarshal(val, &jmsg)
is.NoErr(err)
is.Equal(fmt.Sprintf("%d", i), string(jmsg.Content))
}
log.Debug("Done")
}
func makeMessage(topic string, id int) Message {
return Message{
Topic: topic,
Content: []byte(fmt.Sprintf("%d", id)),
}
}