This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
625 lines (520 loc) · 15.8 KB
/
main.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"math/rand"
"os"
"runtime"
"strings"
"sync"
"time"
log "github.com/sirupsen/logrus"
"github.com/twmb/franz-go/pkg/kerr"
"github.com/twmb/franz-go/pkg/kgo"
"github.com/twmb/franz-go/pkg/kmsg"
"github.com/twmb/franz-go/pkg/sasl/scram"
"github.com/vectorizedio/redpanda/src/go/rpk/pkg/kafka"
"golang.org/x/sync/semaphore"
)
func Die(msg string, args ...interface{}) {
formatted := fmt.Sprintf(msg, args...)
log.Error(formatted)
os.Exit(1)
}
func Chk(err error, msg string, args ...interface{}) {
if err != nil {
Die(msg, args...)
}
}
var (
debug = flag.Bool("debug", false, "Enable verbose logging")
trace = flag.Bool("trace", false, "Enable super-verbose (franz-go internals)")
brokers = flag.String("brokers", "localhost:9092", "comma delimited list of brokers")
topic = flag.String("topic", "", "topic to produce to or consume from")
username = flag.String("username", "", "SASL username")
password = flag.String("password", "", "SASL password")
mSize = flag.Int("msg_size", 16384, "Size of messages to produce")
pCount = flag.Int("produce_msgs", 1000, "Number of messages to produce")
cCount = flag.Int("rand_read_msgs", 10, "Number of validation reads to do")
seqRead = flag.Bool("seq_read", true, "Whether to do sequential read validation")
parallelRead = flag.Int("parallel", 1, "How many readers to run in parallel")
)
type OffsetRange struct {
Lower int64 // Inclusive
Upper int64 // Exclusive
}
type OffsetRanges struct {
Ranges []OffsetRange
}
func (ors *OffsetRanges) Insert(o int64) {
// Normal case: this is the next offset after the current range in flight
if len(ors.Ranges) == 0 {
ors.Ranges = append(ors.Ranges, OffsetRange{Lower: o, Upper: o + 1})
return
}
last := &ors.Ranges[len(ors.Ranges)-1]
if o >= last.Lower && o == last.Upper {
last.Upper += 1
return
} else {
if o < last.Upper {
// TODO: more flexible structure for out of order inserts, at the moment
// we rely on franz-go callbacks being invoked in order.
Die("Out of order offset %d", o)
} else {
ors.Ranges = append(ors.Ranges, OffsetRange{Lower: o, Upper: o + 1})
}
}
}
func (ors *OffsetRanges) Contains(o int64) bool {
for _, r := range ors.Ranges {
if o >= r.Lower && o < r.Upper {
return true
}
}
return false
}
type TopicOffsetRanges struct {
PartitionRanges []OffsetRanges
}
func (tors *TopicOffsetRanges) Insert(p int32, o int64) {
tors.PartitionRanges[p].Insert(o)
}
func (tors *TopicOffsetRanges) Contains(p int32, o int64) bool {
return tors.PartitionRanges[p].Contains(o)
}
func topicOffsetRangeFile() string {
return fmt.Sprintf("valid_offsets_%s.json", *topic)
}
func (tors *TopicOffsetRanges) Store() error {
log.Infof("TopicOffsetRanges::Storing %s...", topicOffsetRangeFile())
data, err := json.Marshal(tors)
if err != nil {
return err
}
err = ioutil.WriteFile(topicOffsetRangeFile(), data, 0644)
if err != nil {
return err
}
for p, or := range tors.PartitionRanges {
log.Debugf("TopicOffsetRanges::Store: %d %d", p, len(or.Ranges))
}
return nil
}
func NewTopicOffsetRanges(nPartitions int32) TopicOffsetRanges {
prs := make([]OffsetRanges, nPartitions)
for _, or := range prs {
or.Ranges = make([]OffsetRange, 0)
}
return TopicOffsetRanges{
PartitionRanges: prs,
}
}
func LoadTopicOffsetRanges(nPartitions int32) TopicOffsetRanges {
data, err := ioutil.ReadFile(topicOffsetRangeFile())
if err != nil {
// Pass, assume it's not existing yet
return NewTopicOffsetRanges(nPartitions)
} else {
var tors TopicOffsetRanges
if len(data) > 0 {
err = json.Unmarshal(data, &tors)
Chk(err, "Bad JSON %v", err)
}
if int32(len(tors.PartitionRanges)) > nPartitions {
Die("More partitions in valid_offsets file than in topic!")
} else if len(tors.PartitionRanges) < int(nPartitions) {
// Creating new partitions is allowed
blanks := make([]OffsetRanges, nPartitions-int32(len(tors.PartitionRanges)))
tors.PartitionRanges = append(tors.PartitionRanges, blanks...)
}
return tors
}
}
func sequentialRead(nPartitions int32) {
client := newClient(nil)
hwm := getOffsets(client, nPartitions, -1)
lwm := make([]int64, nPartitions)
for {
var err error
lwm, err = sequentialReadInner(nPartitions, lwm, hwm)
if err != nil {
log.Warnf("Restarting reader for error %v", err)
// Loop around
} else {
return
}
}
}
func sequentialReadInner(nPartitions int32, startAt []int64, upTo []int64) ([]int64, error) {
log.Infof("Sequential read...")
offsets := make(map[string]map[int32]kgo.Offset)
partOffsets := make(map[int32]kgo.Offset, nPartitions)
complete := make([]bool, nPartitions)
for i, o := range startAt {
partOffsets[int32(i)] = kgo.NewOffset().At(o)
log.Infof("Sequential start offset %s/%d %d...", *topic, i, partOffsets[int32(i)])
if o == upTo[i] {
complete[i] = true
}
}
offsets[*topic] = partOffsets
validRanges := LoadTopicOffsetRanges(nPartitions)
opts := []kgo.Opt{
kgo.ConsumePartitions(offsets),
}
client := newClient(opts)
last_read := make([]int64, nPartitions)
for {
fetches := client.PollFetches(context.Background())
var r_err error
fetches.EachError(func(t string, p int32, err error) {
log.Debugf("Sequential fetch %s/%d e=%v...", t, p, err)
r_err = err
})
if r_err != nil {
return last_read, r_err
}
fetches.EachRecord(func(r *kgo.Record) {
log.Debugf("Sequential read %s/%d o=%d...", *topic, r.Partition, r.Offset)
if r.Offset > last_read[r.Partition] {
last_read[r.Partition] = r.Offset
}
if r.Offset >= upTo[r.Partition]-1 {
complete[r.Partition] = true
}
validateRecord(r, &validRanges)
})
any_incomplete := false
for _, c := range complete {
if !c {
any_incomplete = true
}
}
if !any_incomplete {
break
}
}
return last_read, nil
}
func validateRecord(r *kgo.Record, validRanges *TopicOffsetRanges) {
expect_key := fmt.Sprintf("%06d.%018d", 0, r.Offset)
log.Debugf("Consumed %s on p=%d at o=%d", r.Key, r.Partition, r.Offset)
if expect_key != string(r.Key) {
shouldBeValid := validRanges.Contains(r.Partition, r.Offset)
if shouldBeValid {
Die("Bad read at offset %d on partition %s/%d. Expect '%s', found '%s'", r.Offset, *topic, r.Partition, expect_key, r.Key)
} else {
log.Infof("Ignoring read validation at offset outside valid range %s/%d %d", *topic, r.Partition, r.Offset)
}
} else {
log.Debugf("Read OK (%s) on p=%d at o=%d", r.Key, r.Partition, r.Offset)
}
}
func randomRead(tag string, nPartitions int32) {
// Basic client to read offsets
client := newClient(make([]kgo.Opt, 0))
endOffsets := getOffsets(client, nPartitions, -1)
client.Close()
client = newClient(make([]kgo.Opt, 0))
startOffsets := getOffsets(client, nPartitions, -2)
client.Close()
runtime.GC()
validRanges := LoadTopicOffsetRanges(nPartitions)
ctxLog := log.WithFields(log.Fields{"tag": tag})
// Select a partition and location
ctxLog.Infof("Reading %d random offsets", *cCount)
for i := 0; i < *cCount; i++ {
p := rand.Int31n(nPartitions)
pStart := startOffsets[p]
pEnd := endOffsets[p]
if pEnd-pStart < 2 {
ctxLog.Warnf("Partition %d is empty, skipping read", p)
continue
}
o := rand.Int63n(pEnd-pStart-1) + pStart
offset := kgo.NewOffset().At(o)
// Construct a map of topic->partition->offset to seek our new client to the right place
offsets := make(map[string]map[int32]kgo.Offset)
partOffsets := make(map[int32]kgo.Offset, 1)
partOffsets[p] = offset
offsets[*topic] = partOffsets
// Fully-baked client for actual consume
opts := []kgo.Opt{
kgo.ConsumePartitions(offsets),
}
client = newClient(opts)
// Read one record
ctxLog.Debugf("Reading partition %d (%d-%d) at offset %d", p, pStart, pEnd, offset)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
fetches := client.PollRecords(ctx, 1)
ctxLog.Debugf("Read done for partition %d (%d-%d) at offset %d", p, pStart, pEnd, offset)
fetches.EachError(func(topic string, partition int32, e error) {
// In random read mode, we tolerate read errors: if the server is unavailable
// we will just proceed to read the next random offset.
ctxLog.Errorf("Error reading from partition %s:%d: %v", topic, partition, e)
})
fetches.EachRecord(func(r *kgo.Record) {
if r.Partition != p {
Die("Wrong partition %d in read at offset %d on partition %s/%d", r.Partition, r.Offset, *topic, p)
}
validateRecord(r, &validRanges)
})
if len(fetches.Records()) == 0 {
ctxLog.Errorf("Empty response reading from partition %d at %d", p, offset)
}
fetches = nil
client.Flush(context.Background())
client.Close()
}
}
func newRecord(producerId int, sequence int64) *kgo.Record {
var key bytes.Buffer
fmt.Fprintf(&key, "%06d.%018d", producerId, sequence)
payload := make([]byte, *mSize)
var r *kgo.Record
r = kgo.KeySliceRecord(key.Bytes(), payload)
return r
}
// Try to get offsets, with a retry loop in case any partitions are not
// in a position to respond. This is useful to avoid terminating if e.g.
// the cluster is subject to failure injection while workload runs.
func getOffsets(client *kgo.Client, nPartitions int32, t int64) []int64 {
wait_t := 2 * time.Second
for {
result, err := getOffsetsInner(client, nPartitions, t)
if err != nil {
log.Warnf("Retrying getOffsets in %v", wait_t)
time.Sleep(wait_t)
} else {
return result
}
}
}
func getOffsetsInner(client *kgo.Client, nPartitions int32, t int64) ([]int64, error) {
log.Infof("Loading offsets for topic %s t=%d...", *topic, t)
pOffsets := make([]int64, nPartitions)
req := kmsg.NewPtrListOffsetsRequest()
req.ReplicaID = -1
reqTopic := kmsg.NewListOffsetsRequestTopic()
reqTopic.Topic = *topic
for i := 0; i < int(nPartitions); i++ {
part := kmsg.NewListOffsetsRequestTopicPartition()
part.Partition = int32(i)
part.Timestamp = t
reqTopic.Partitions = append(reqTopic.Partitions, part)
}
req.Topics = append(req.Topics, reqTopic)
seenPartitions := int32(0)
shards := client.RequestSharded(context.Background(), req)
var r_err error
allFailed := kafka.EachShard(req, shards, func(shard kgo.ResponseShard) {
if shard.Err != nil {
r_err = shard.Err
return
}
resp := shard.Resp.(*kmsg.ListOffsetsResponse)
for _, partition := range resp.Topics[0].Partitions {
if partition.ErrorCode != 0 {
log.Warnf("error fetching %s/%d metadata: %v", *topic, partition.Partition, kerr.ErrorForCode(partition.ErrorCode))
r_err = kerr.ErrorForCode(partition.ErrorCode)
}
pOffsets[partition.Partition] = partition.Offset
seenPartitions += 1
log.Debugf("Partition %d offset %d", partition.Partition, pOffsets[partition.Partition])
}
})
if allFailed {
return nil, errors.New("All offset requests failed")
}
if seenPartitions < nPartitions {
// The results may be partial, simply omitting some partitions while not
// raising any error. We transform this into an error to avoid wrongly
// returning a 0 offset for any missing partitions
return nil, errors.New("Didn't get data for all partitions")
}
return pOffsets, r_err
}
func produce(nPartitions int32) {
n := int64(*pCount)
for {
n_produced, bad_offsets := produceInner(n, nPartitions)
n = n - n_produced
if len(bad_offsets) > 0 {
log.Infof("Produce stopped early, %d still to do", n)
}
if n <= 0 {
return
}
}
}
type BadOffset struct {
P int32
O int64
}
func produceInner(n int64, nPartitions int32) (int64, []BadOffset) {
opts := []kgo.Opt{
kgo.DefaultProduceTopic(*topic),
kgo.MaxBufferedRecords(1024),
kgo.ProducerBatchMaxBytes(1024 * 1024),
kgo.ProducerBatchCompression(kgo.NoCompression()),
kgo.RequiredAcks(kgo.AllISRAcks()),
kgo.RecordPartitioner(kgo.ManualPartitioner()),
}
client := newClient(opts)
validOffsets := LoadTopicOffsetRanges(nPartitions)
nextOffset := getOffsets(client, nPartitions, -1)
for i, o := range nextOffset {
log.Infof("Produce start offset %s/%d %d...", *topic, i, o)
}
var wg sync.WaitGroup
errored := false
produced := int64(0)
// Channel must be >= concurrency
bad_offsets := make(chan BadOffset, 16384)
concurrent := semaphore.NewWeighted(4096)
log.Infof("Producing %d messages (%d bytes)", n, *mSize)
storeEveryN := 10000
for i := int64(0); i < n && len(bad_offsets) == 0; i = i + 1 {
concurrent.Acquire(context.Background(), 1)
produced += 1
var p = rand.Int31n(nPartitions)
expect_offset := nextOffset[p]
nextOffset[p] += 1
r := newRecord(0, expect_offset)
r.Partition = p
wg.Add(1)
log.Debugf("Writing partition %d at %d", r.Partition, nextOffset[p])
handler := func(r *kgo.Record, err error) {
concurrent.Release(1)
Chk(err, "Produce failed!")
if expect_offset != r.Offset {
log.Warnf("Produced at unexpected offset %d (expected %d) on partition %d", r.Offset, expect_offset, r.Partition)
bad_offsets <- BadOffset{r.Partition, r.Offset}
errored = true
log.Debugf("errored = %b", errored)
} else {
validOffsets.Insert(r.Partition, r.Offset)
log.Debugf("Wrote partition %d at %d", r.Partition, r.Offset)
}
wg.Done()
}
client.Produce(context.Background(), r, handler)
// Not strictly necessary, but useful if a long running producer gets killed
// before finishing
if i%int64(storeEveryN) == 0 && i != 0 {
err := validOffsets.Store()
Chk(err, "Error writing interim results: %v", err)
}
}
log.Info("Waiting...")
wg.Wait()
log.Info("Waited.")
wg.Wait()
close(bad_offsets)
err := validOffsets.Store()
Chk(err, "Error writing interim results: %v", err)
if errored {
log.Warnf("%d bad offsets", len(bad_offsets))
var r []BadOffset
for o := range bad_offsets {
r = append(r, o)
}
if len(r) == 0 {
Die("No bad offsets but errored?")
}
successful_produced := produced - int64(len(r))
return successful_produced, r
} else {
wg.Wait()
return produced, nil
}
}
func newClient(opts []kgo.Opt) *kgo.Client {
// Disable auth if username not given
if len(*username) > 0 {
auth_mech := scram.Auth{
User: *username,
Pass: *password,
}
auth := auth_mech.AsSha256Mechanism()
opts = append(opts,
kgo.SASL(auth))
}
opts = append(opts,
kgo.SeedBrokers(strings.Split(*brokers, ",")...))
if *trace {
opts = append(opts, kgo.WithLogger(kgo.BasicLogger(os.Stderr, kgo.LogLevelDebug, nil)))
}
client, err := kgo.NewClient(opts...)
Chk(err, "Error creating kafka client")
return client
}
func main() {
flag.Parse()
if *debug || *trace {
log.SetLevel(log.DebugLevel)
} else {
log.SetLevel(log.InfoLevel)
}
log.Info("Getting topic metadata...")
client := newClient(make([]kgo.Opt, 0))
var t kmsg.MetadataResponseTopic
{
req := kmsg.NewPtrMetadataRequest()
reqTopic := kmsg.NewMetadataRequestTopic()
reqTopic.Topic = kmsg.StringPtr(*topic)
req.Topics = append(req.Topics, reqTopic)
resp, err := req.RequestWith(context.Background(), client)
Chk(err, "unable to request topic metadata: %v", err)
if len(resp.Topics) != 1 {
Die("metadata response returned %d topics when we asked for 1", len(resp.Topics))
}
t = resp.Topics[0]
if t.ErrorCode != 0 {
Die("Error %s getting topic metadata", kerr.ErrorForCode(t.ErrorCode))
}
}
nPartitions := int32(len(t.Partitions))
log.Debugf("Targeting topic %s with %d partitions", *topic, nPartitions)
if *pCount > 0 {
produce(nPartitions)
}
if *parallelRead <= 1 {
if *seqRead {
sequentialRead(nPartitions)
}
if *cCount > 0 {
randomRead("", nPartitions)
}
} else {
var wg sync.WaitGroup
if *seqRead {
wg.Add(1)
go func() {
sequentialRead(nPartitions)
wg.Done()
}()
}
parallelRandoms := *parallelRead
if *seqRead {
parallelRandoms -= 1
}
if *cCount > 0 {
for i := 0; i < parallelRandoms; i++ {
wg.Add(1)
go func(tag string) {
randomRead(tag, nPartitions)
wg.Done()
}(fmt.Sprintf("%03d", i))
}
}
wg.Wait()
}
}