-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconsumer.go
More file actions
1530 lines (1309 loc) · 45.3 KB
/
consumer.go
File metadata and controls
1530 lines (1309 loc) · 45.3 KB
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
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package comet
import (
"context"
"errors"
"fmt"
"math/rand"
"os"
"path/filepath"
"slices"
"strings"
"sync"
"sync/atomic"
"time"
)
// ErrStopProcessing is a sentinel error that can be returned from ProcessFunc
// to gracefully stop processing while still ACKing the current batch
var ErrStopProcessing = errors.New("stop processing")
// MessageID represents a structured message ID
// Fields ordered for optimal memory alignment: int64 first, then uint32
type MessageID struct {
EntryNumber int64 `json:"entry_number"`
ShardID uint32 `json:"shard_id"`
}
// String returns the string representation of the ID (ShardID-EntryNumber format)
func (id MessageID) String() string {
return fmt.Sprintf("%d-%d", id.ShardID, id.EntryNumber)
}
// ParseMessageID parses a string ID back to MessageID
func ParseMessageID(str string) (MessageID, error) {
var shardID uint32
var entryNumber int64
if _, err := fmt.Sscanf(str, "%d-%d", &shardID, &entryNumber); err != nil {
return MessageID{}, fmt.Errorf("invalid message ID format: %w", err)
}
return MessageID{EntryNumber: entryNumber, ShardID: shardID}, nil
}
// StreamMessage represents a message read from a stream
type StreamMessage struct {
Stream string // Stream name/identifier
ID MessageID // Unique message ID
Data []byte // Raw message data
}
// Consumer reads from comet stream shards
// Fields ordered for optimal memory alignment
type Consumer struct {
// Pointers first (8 bytes on 64-bit)
client *Client
// Composite types
readers sync.Map // Cached readers per shard (optimized for read-heavy workload)
shards sync.Map // Track shards that have been accessed (for Close persistence)
// Track highest read entry per shard for AckRange validation
highestReadMu sync.RWMutex
highestRead map[uint32]int64 // shardID -> highest entry read
// In-memory consumer offsets for immediate read-after-ACK consistency
memOffsetsMu sync.RWMutex
memOffsets map[uint32]int64 // shardID -> consumer offset (in-memory view)
// Idempotent processing: track processed messages to prevent duplicates
processedMsgsMu sync.RWMutex
processedMsgs map[MessageID]bool // messageID -> processed flag
// Deterministic assignment
consumerID int
consumerCount int
// Background ACK flushing
flushMu sync.Mutex
flushDone chan struct{}
flushWg sync.WaitGroup
// Strings last
group string
}
// ConsumerOptions configures a consumer
type ConsumerOptions struct {
Group string
ConsumerID int // 0, 1, 2, etc. for deterministic shard assignment
ConsumerCount int // Total consumers in this group for deterministic shard assignment
}
// isShardAssigned checks if this shard is deterministically assigned to this consumer
func (c *Consumer) isShardAssigned(shardID uint32) bool {
// If no consumer coordination is configured, default to claiming all shards
if c.consumerCount <= 0 {
return true
}
// Deterministic assignment: shard goes to consumer (shardID % consumerCount)
return shardID%uint32(c.consumerCount) == uint32(c.consumerID)
}
// getAssignedShards returns shards deterministically assigned to this consumer
func (c *Consumer) getAssignedShards(candidateShards []uint32) []uint32 {
var assigned []uint32
for _, shardID := range candidateShards {
if c.isShardAssigned(shardID) {
assigned = append(assigned, shardID)
}
}
return assigned
}
// getExistingShard retrieves an existing shard without creating it
// Returns an error if the shard doesn't exist
func (c *Consumer) getExistingShard(shardID uint32) (*Shard, error) {
shard := c.client.getShard(shardID)
if shard == nil {
// Try to load it from disk if it exists
var err error
shard, err = c.client.loadExistingShard(shardID)
if err != nil {
return nil, fmt.Errorf("shard %d does not exist: %w", shardID, err)
}
}
return shard, nil
}
// findExistingShards scans filesystem for existing shard directories
func (c *Consumer) findExistingShards() ([]uint32, error) {
var shards []uint32
shardDirs, err := filepath.Glob(filepath.Join(c.client.dataDir, "shard-*"))
if err != nil {
return nil, fmt.Errorf("failed to scan for shard directories: %w", err)
}
for _, shardDir := range shardDirs {
// Extract shard ID from directory name (e.g., "shard-0166" -> 166)
dirName := filepath.Base(shardDir)
if !strings.HasPrefix(dirName, "shard-") {
continue
}
shardIDStr := strings.TrimPrefix(dirName, "shard-")
var shardID uint64
if _, parseErr := fmt.Sscanf(shardIDStr, "%04d", &shardID); parseErr == nil {
shards = append(shards, uint32(shardID))
}
}
return shards, nil
}
// ProcessFunc handles a batch of messages, returning error to trigger retry
type ProcessFunc func(ctx context.Context, messages []StreamMessage) error
// ProcessOption configures the Process method
type ProcessOption func(*processConfig)
// processConfig holds internal configuration built from options
type processConfig struct {
// Core processing
handler ProcessFunc
autoAck bool
// Callbacks
onError func(err error, retryCount int)
onBatch func(size int, duration time.Duration)
// Behavior
batchSize int
maxRetries int
pollInterval time.Duration
retryDelay time.Duration
shardDiscoveryInterval time.Duration
// Sharding
stream string
shards []uint32
consumerID int
consumerCount int
}
// WithStream specifies a stream pattern for shard discovery
func WithStream(pattern string) ProcessOption {
return func(cfg *processConfig) {
cfg.stream = pattern
}
}
// WithShards specifies explicit shards to process
func WithShards(shards ...uint32) ProcessOption {
return func(cfg *processConfig) {
cfg.shards = shards
}
}
// WithBatchSize sets the number of messages to read at once
func WithBatchSize(size int) ProcessOption {
return func(cfg *processConfig) {
cfg.batchSize = size
}
}
// WithMaxRetries sets the number of retry attempts for failed batches
func WithMaxRetries(retries int) ProcessOption {
return func(cfg *processConfig) {
cfg.maxRetries = retries
}
}
// WithPollInterval sets how long to wait when no messages are available
func WithPollInterval(interval time.Duration) ProcessOption {
return func(cfg *processConfig) {
cfg.pollInterval = interval
}
}
// WithRetryDelay sets the base delay between retries
func WithRetryDelay(delay time.Duration) ProcessOption {
return func(cfg *processConfig) {
cfg.retryDelay = delay
}
}
// WithAutoAck controls automatic acknowledgment (default: true)
func WithAutoAck(enabled bool) ProcessOption {
return func(cfg *processConfig) {
cfg.autoAck = enabled
}
}
// WithErrorHandler sets a callback for processing errors
func WithErrorHandler(handler func(err error, retryCount int)) ProcessOption {
return func(cfg *processConfig) {
cfg.onError = handler
}
}
// WithBatchCallback sets a callback after each batch completes
func WithBatchCallback(callback func(size int, duration time.Duration)) ProcessOption {
return func(cfg *processConfig) {
cfg.onBatch = callback
}
}
// WithConsumerAssignment configures distributed processing
func WithConsumerAssignment(id, total int) ProcessOption {
return func(cfg *processConfig) {
cfg.consumerID = id
cfg.consumerCount = total
}
}
// WithShardDiscoveryInterval sets how often to check for new shards
// Default is 5 seconds. Set to 0 to disable periodic rediscovery.
func WithShardDiscoveryInterval(interval time.Duration) ProcessOption {
return func(cfg *processConfig) {
cfg.shardDiscoveryInterval = interval
}
}
// buildProcessConfig applies options and defaults
func buildProcessConfig(handler ProcessFunc, opts []ProcessOption) *processConfig {
cfg := &processConfig{
handler: handler,
autoAck: true,
batchSize: 100,
maxRetries: 3,
pollInterval: 100 * time.Millisecond,
retryDelay: time.Second,
shardDiscoveryInterval: 5 * time.Second,
consumerCount: 1,
consumerID: 0,
}
for _, opt := range opts {
opt(cfg)
}
// If no stream or shards specified, discover all shards
if cfg.stream == "" && len(cfg.shards) == 0 {
cfg.stream = "*:*:*:*" // Match any stream pattern
}
return cfg
}
// NewConsumer creates a new consumer for comet streams
func NewConsumer(client *Client, opts ConsumerOptions) *Consumer {
group := opts.Group
// If no group specified, use a consistent default to avoid confusion
if group == "" {
group = "default"
}
// Since processes own their shards exclusively, they own all consumer groups
// No need to register/track active groups
// client.registerConsumerGroup(group)
consumer := &Consumer{
client: client,
group: group,
consumerID: opts.ConsumerID,
consumerCount: opts.ConsumerCount,
highestRead: make(map[uint32]int64),
memOffsets: make(map[uint32]int64),
processedMsgs: make(map[MessageID]bool),
flushDone: make(chan struct{}),
// readers sync.Map is zero-initialized and ready to use
}
// Start background ACK flusher for durability
consumer.flushWg.Add(1)
go consumer.backgroundFlush()
return consumer
}
// backgroundFlush periodically persists consumer offsets for durability
func (c *Consumer) backgroundFlush() {
defer c.flushWg.Done()
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
// Check if we should exit
c.flushMu.Lock()
done := c.flushDone
c.flushMu.Unlock()
if done == nil {
return
}
select {
case <-ticker.C:
// Flush any pending ACKs
ctx := context.Background()
if err := c.FlushACKs(ctx); err != nil && c.client.logger != nil {
// Only log if it's not a "directory not found" error (common in tests)
if !os.IsNotExist(err) && !strings.Contains(err.Error(), "no such file or directory") {
c.client.logger.Warn("Background ACK flush failed", "error", err)
}
}
case <-done:
return
}
}
}
// Close closes the consumer and releases all resources
func (c *Consumer) Close() error {
// Stop background flusher first to prevent races
c.flushMu.Lock()
if c.flushDone != nil {
// Only close channel if it hasn't been closed already
select {
case <-c.flushDone:
// Already closed
default:
close(c.flushDone)
c.flushDone = nil // Prevent double close
}
}
c.flushMu.Unlock()
// Wait for background goroutine to exit
c.flushWg.Wait()
// Close all cached readers
c.readers.Range(func(key, value any) bool {
if reader, ok := value.(*Reader); ok {
reader.Close()
// Decrement active readers count
shardID := key.(uint32)
if shard, err := c.getExistingShard(shardID); err == nil {
if state := shard.state; state != nil {
atomic.AddUint64(&state.ActiveReaders, ^uint64(0)) // Decrement by 1
}
}
}
return true
})
// Persist any pending consumer offsets before closing
// This is critical to prevent ACK loss when consumer restarts
// Note: We don't wait for shard background operations (like periodic flush)
// since those are managed by the client, not the consumer
// Now persist any remaining changes synchronously
c.shards.Range(func(key, value any) bool {
shardID := key.(uint32)
if shard, err := c.getExistingShard(shardID); err == nil {
// Clone index while holding lock
shard.mu.Lock()
// Always persist on close, regardless of writesSinceCheckpoint
// This ensures any recent ACKs are saved
if IsDebug() && c.client.logger != nil {
c.client.logger.Debug("Consumer close: persisting index",
"shardID", shardID,
"group", c.group,
"offset", shard.index.ConsumerOffsets[c.group],
"writesSinceCheckpoint", shard.writesSinceCheckpoint)
}
// Clone the index to avoid holding lock during persist
indexCopy := shard.cloneIndex()
shard.mu.Unlock()
// Return index to pool after use
defer returnIndexToPool(indexCopy)
// TODO: Consumer should not overwrite the writer's index file!
// This is a temporary fix - we need to separate consumer offsets from the shard index
// For now, skip persisting to avoid overwriting writer's data
if false {
// Persist without holding the lock
shard.indexMu.Lock()
err := shard.saveBinaryIndex(indexCopy)
shard.indexMu.Unlock()
if err != nil && c.client.logger != nil {
c.client.logger.Warn("Failed to persist consumer offsets on close",
"error", err, "shard", shardID, "group", c.group)
}
}
}
return true
})
// Since processes own their shards exclusively, they own all consumer groups
// No need to deregister
// c.client.deregisterConsumerGroup(c.group)
return nil
}
// isMessageProcessed checks if a message has already been processed by this consumer group
func (c *Consumer) isMessageProcessed(messageID MessageID) bool {
c.processedMsgsMu.RLock()
defer c.processedMsgsMu.RUnlock()
return c.processedMsgs[messageID]
}
// markMessageProcessed marks a message as processed by this consumer group
func (c *Consumer) markMessageProcessed(messageID MessageID) {
c.processedMsgsMu.Lock()
defer c.processedMsgsMu.Unlock()
c.processedMsgs[messageID] = true
// Cleanup old entries periodically to prevent unbounded growth
// Clean up every 1000 messages to amortize the cost
if len(c.processedMsgs) > 1000 && len(c.processedMsgs)%1000 == 0 {
c.cleanupProcessedMessages()
}
}
// cleanupProcessedMessages removes entries for messages that are below the current consumer offset
// Must be called with processedMsgsMu held
func (c *Consumer) cleanupProcessedMessages() {
// Get current consumer offsets for all shards
shardOffsets := make(map[uint32]int64)
c.client.mu.RLock()
for shardID, shard := range c.client.shards {
shard.mu.RLock()
if shard.offsetMmap != nil {
if offset, exists := shard.offsetMmap.Get(c.group); exists {
shardOffsets[shardID] = offset
}
}
shard.mu.RUnlock()
}
c.client.mu.RUnlock()
// Remove messages that are below the consumer offset
for msgID := range c.processedMsgs {
if offset, exists := shardOffsets[msgID.ShardID]; exists {
// Remove if this message is below the consumer offset
// We keep a buffer of 100 to handle edge cases with concurrent processing
if msgID.EntryNumber < offset-100 {
delete(c.processedMsgs, msgID)
}
}
}
}
// FilterDuplicates removes already-processed messages from a batch, returning only new messages
func (c *Consumer) FilterDuplicates(messages []StreamMessage) []StreamMessage {
if len(messages) == 0 {
return messages
}
var filteredMessages []StreamMessage
for _, msg := range messages {
if !c.isMessageProcessed(msg.ID) {
filteredMessages = append(filteredMessages, msg)
}
}
return filteredMessages
}
// MarkBatchProcessed marks all messages in a batch as processed
func (c *Consumer) MarkBatchProcessed(messages []StreamMessage) {
for _, msg := range messages {
c.markMessageProcessed(msg.ID)
}
}
// Read reads up to count entries from the specified shards starting from the consumer group's current offset.
// This is a low-level method for manual message processing - you probably want Process() instead.
//
// Key differences from Process():
// - Read() is one-shot, Process() is continuous
// - Read() requires manual ACKing, Process() can auto-ACK
// - Read() uses explicit shard IDs, Process() can use wildcards
// - Read() has no retry logic, Process() has configurable retries
func (c *Consumer) Read(ctx context.Context, shards []uint32, count int) ([]StreamMessage, error) {
var messages []StreamMessage
remaining := count
// Pick random shards without replacement to ensure fair reading
for i := 0; i < len(shards) && remaining > 0; i++ {
// Pick a random shard from the remaining ones
j := rand.Intn(len(shards) - i)
shardID := shards[j]
// Swap the used shard to the end so we don't pick it again
shards[j], shards[len(shards)-1-i] = shards[len(shards)-1-i], shards[j]
// Get existing shard for reading
shard, err := c.getExistingShard(shardID)
if err != nil {
return nil, fmt.Errorf("failed to get shard %d: %w", shardID, err)
}
shardMessages, err := c.readFromShard(ctx, shard, remaining)
if err != nil {
return nil, fmt.Errorf("failed to read from shard %d: %w", shardID, err)
}
messages = append(messages, shardMessages...)
remaining -= len(shardMessages)
}
return messages, nil
}
// Process continuously reads and processes messages from shards.
// This is the high-level API for stream processing - handles discovery, retries, and ACKing automatically.
//
// The simplest usage processes all discoverable shards:
//
// err := consumer.Process(ctx, handleMessages)
//
// Recommended usage with explicit configuration:
//
// err := consumer.Process(ctx, handleMessages,
// comet.WithStream("events:v1:shard:*"), // Wildcard pattern for shard discovery
// comet.WithBatchSize(1000), // Process up to 1000 messages at once
// comet.WithErrorHandler(logError), // Handle processing errors
// comet.WithAutoAck(true), // Automatically ACK successful batches
// )
//
// For distributed processing across multiple consumer instances:
//
// err := consumer.Process(ctx, handleMessages,
// comet.WithStream("events:v1:shard:*"),
// comet.WithConsumerAssignment(workerID, totalWorkers), // Distribute shards across workers
// )
//
// Stream patterns must end with ":*" for wildcard matching, e.g.:
// - "events:v1:shard:*" matches events:v1:shard:0000, events:v1:shard:0001, etc.
// - "logs:*:*:*" matches any 4-part stream name starting with "logs"
func (c *Consumer) Process(ctx context.Context, handler ProcessFunc, opts ...ProcessOption) error {
// Build config from options
cfg := buildProcessConfig(handler, opts)
// Validate required fields
if cfg.handler == nil {
return fmt.Errorf("handler is required")
}
// Determine candidate shards to process
var candidateShards []uint32
var lastShardDiscovery time.Time
if len(cfg.shards) > 0 {
candidateShards = cfg.shards
// No discovery needed for explicit shards
} else if cfg.stream != "" {
// Auto-discover shards from stream pattern using systematic polling
discoveredShards, err := c.discoverShards(cfg.stream, cfg.consumerID, cfg.consumerCount)
if err != nil {
return fmt.Errorf("failed to discover shards: %w", err)
}
candidateShards = discoveredShards
lastShardDiscovery = time.Now()
} else {
return fmt.Errorf("either Shards or Stream must be specified")
}
// Main processing loop
for {
select {
case <-ctx.Done():
return ctx.Err()
default:
}
// Check if it's time to rediscover shards (only if using stream pattern)
if cfg.stream != "" && cfg.shardDiscoveryInterval > 0 && time.Since(lastShardDiscovery) >= cfg.shardDiscoveryInterval {
newShards, err := c.discoverShards(cfg.stream, cfg.consumerID, cfg.consumerCount)
if err == nil {
if len(newShards) != len(candidateShards) {
oldCount := len(candidateShards)
candidateShards = newShards
lastShardDiscovery = time.Now()
if IsDebug() && c.client.logger != nil {
c.client.logger.Debug("Periodic shard rediscovery found changes",
"old_count", oldCount,
"new_count", len(candidateShards),
"interval", cfg.shardDiscoveryInterval)
}
// Re-run shard assignment with new candidates
continue
}
lastShardDiscovery = time.Now()
}
}
// Consumer group coordination: get deterministically assigned shards
shards := c.getAssignedShards(candidateShards)
// Re-discover shards when we have no work - with systematic polling, catches new shards immediately
if cfg.stream != "" && len(shards) == 0 {
newShards, err := c.discoverShards(cfg.stream, cfg.consumerID, cfg.consumerCount)
if err == nil && len(newShards) > len(candidateShards) {
oldCount := len(candidateShards)
candidateShards = newShards
lastShardDiscovery = time.Now()
if IsDebug() && c.client.logger != nil {
c.client.logger.Debug("Systematic rediscovery found new shards",
"old_count", oldCount,
"new_count", len(candidateShards),
"shards", candidateShards)
}
// Re-run shard assignment with new candidates
continue
}
}
if len(shards) == 0 {
// No shards available for this consumer group - wait and retry
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(cfg.pollInterval):
continue
}
}
// Proactively refresh indexes for all candidate shards to ensure real-time visibility
// This catches updates from other processes even when we're not actively reading from all shards
c.refreshShardIndexes(candidateShards)
start := time.Now()
messages, err := c.Read(ctx, shards, cfg.batchSize)
if IsDebug() && c.client.logger != nil {
c.client.logger.Debug("Consumer read attempt",
"messages_count", len(messages),
"batch_size", cfg.batchSize,
"error", err)
}
if err != nil {
if cfg.onError != nil {
cfg.onError(err, 0)
}
// For read errors, sleep and retry
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(cfg.pollInterval):
continue
}
}
if len(messages) == 0 {
// No messages, wait before polling again
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(cfg.pollInterval):
continue
}
}
// Process batch with retries
var processErr error
for retry := 0; retry <= cfg.maxRetries; retry++ {
processErr = cfg.handler(ctx, messages)
if processErr == nil {
break
}
if cfg.onError != nil {
cfg.onError(processErr, retry)
}
if retry < cfg.maxRetries {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(cfg.retryDelay * time.Duration(retry+1)):
// Exponential backoff
}
}
}
// Auto-ack if enabled and processing succeeded
if cfg.autoAck && processErr == nil {
for _, msg := range messages {
if err := c.Ack(ctx, msg.ID); err != nil {
if cfg.onError != nil {
cfg.onError(fmt.Errorf("ack failed for message %s: %w", msg.ID, err), 0)
}
// Also log for debugging
if IsDebug() && c.client.logger != nil {
c.client.logger.Debug("AutoAck failed",
"messageID", msg.ID,
"error", err)
}
}
}
}
// Call batch callback if provided
if cfg.onBatch != nil {
cfg.onBatch(len(messages), time.Since(start))
}
}
}
// getOrCreateReader gets or creates a reader for a shard
func (c *Consumer) getOrCreateReader(shard *Shard) (*Reader, error) {
// Fast path: check if reader exists using sync.Map
if value, ok := c.readers.Load(shard.shardID); ok {
reader := value.(*Reader)
// Check if the cached reader is stale by comparing LastIndexUpdate
if shard.state != nil {
currentIndexUpdate := shard.state.GetLastIndexUpdate()
if currentIndexUpdate > reader.lastKnownIndexUpdate {
// Reader is stale, remove it and create a new one
c.readers.Delete(shard.shardID)
// Fall through to create new reader
} else {
// Reader is still fresh
atomic.AddUint64(&shard.state.ReaderCacheHits, 1)
return reader, nil
}
} else {
// No state to check, assume reader is still valid
return reader, nil
}
}
// Create new reader with current index - reader will refresh itself when stale
shard.mu.RLock()
indexSnapshot := shard.cloneIndex()
shard.mu.RUnlock()
newReader, err := NewReader(shard.shardID, indexSnapshot, c.client.config.Reader)
if err != nil {
return nil, err
}
// Set the state and client reference for metrics tracking and index refreshing
if state := shard.state; state != nil {
newReader.SetState(state)
}
newReader.SetClient(c.client)
// Use LoadOrStore to handle race where multiple goroutines try to create
if actual, loaded := c.readers.LoadOrStore(shard.shardID, newReader); loaded {
// Another goroutine created a reader, close ours and use theirs
newReader.Close()
return actual.(*Reader), nil
}
// Track new reader creation
if state := shard.state; state != nil {
atomic.AddUint64(&state.TotalReaders, 1)
atomic.AddUint64(&state.ActiveReaders, 1)
}
return newReader, nil
}
// readFromShard reads entries from a single shard using entry-based positioning
func (c *Consumer) readFromShard(ctx context.Context, shard *Shard, maxCount int) ([]StreamMessage, error) {
if IsDebug() && c.client.logger != nil {
c.client.logger.Debug("readFromShard called", "shardID", shard.shardID, "maxCount", maxCount)
}
// Track this shard for persistence on close
c.shards.Store(shard.shardID, true)
// Lock-free reader tracking
atomic.AddInt64(&shard.readerCount, 1)
defer atomic.AddInt64(&shard.readerCount, -1)
// Check if index has been updated by another process using memory-mapped atomic state
// This is much more efficient than time-based reloading since we only reload when actually needed
var needsReload bool
var lastKnownUpdate int64
var currentIndexUpdate int64
if shard.state != nil {
currentIndexUpdate = shard.state.GetLastIndexUpdate()
shard.mu.RLock()
lastKnownUpdate = shard.lastIndexReload
shard.mu.RUnlock()
needsReload = currentIndexUpdate > lastKnownUpdate
if IsDebug() && c.client.logger != nil {
c.client.logger.Debug("Index reload check",
"shardID", shard.shardID,
"currentIndexUpdate", currentIndexUpdate,
"lastKnownUpdate", lastKnownUpdate,
"needsReload", needsReload)
}
if needsReload && IsDebug() && c.client.logger != nil {
c.client.logger.Debug("Index update detected via memory-mapped state",
"shard", shard.shardID,
"current_update", currentIndexUpdate,
"last_known", lastKnownUpdate)
}
} else {
if c.client.logger != nil && IsDebug() {
c.client.logger.Debug("No shard state available for index update detection",
"shard", shard.shardID)
}
}
if needsReload {
shard.mu.Lock()
oldEntries := shard.index.CurrentEntryNumber
if err := shard.loadIndex(); err != nil {
// Log but don't fail - continue with current index
if c.client.logger != nil {
c.client.logger.Warn("Failed to reload index, using cached state",
"shard", shard.shardID,
"error", err)
}
} else {
newEntries := shard.index.CurrentEntryNumber
// Only update lastIndexReload if we actually loaded a non-empty index
// This prevents marking an empty index as "up to date" when it's not
if newEntries > 0 || len(shard.index.Files) > 0 {
shard.lastIndexReload = currentIndexUpdate
if c.client.logger != nil && IsDebug() {
c.client.logger.Debug("Reloaded index after state change",
"shard", shard.shardID,
"old_entries", oldEntries,
"new_entries", newEntries,
"entries_added", newEntries-oldEntries,
"files", len(shard.index.Files),
"updated_lastReload", true)
}
} else {
if c.client.logger != nil && IsDebug() {
c.client.logger.Debug("Reloaded empty index, NOT updating lastIndexReload",
"shard", shard.shardID,
"currentIndexUpdate", currentIndexUpdate,
"keeping_lastReload", shard.lastIndexReload)
}
}
}
shard.mu.Unlock()
}
// First check in-memory offset for immediate consistency
c.memOffsetsMu.RLock()
memOffset, hasMemOffset := c.memOffsets[shard.shardID]
c.memOffsetsMu.RUnlock()
// Get consumer entry offset from mmap store
var persistedOffset int64
var exists bool
if shard.offsetMmap != nil {
persistedOffset, exists = shard.offsetMmap.Get(c.group)
}
if !exists {
persistedOffset = 0
}
shard.mu.RLock()
// Use the higher of memory or persisted offset for consistency
startEntryNum := persistedOffset
if hasMemOffset && memOffset > startEntryNum {
startEntryNum = memOffset
}
// After retention, the requested start entry might no longer exist
// Adjust to the earliest available entry if needed
if len(shard.index.Files) > 0 {
earliestEntry := shard.index.Files[0].StartEntry
if startEntryNum < earliestEntry {
startEntryNum = earliestEntry
}
}
endEntryNum := shard.index.CurrentEntryNumber // Only read durable data
fileCount := len(shard.index.Files)
shard.mu.RUnlock()
// If we just reloaded the index, re-capture the updated endEntryNum
if needsReload {
shard.mu.RLock()
endEntryNum = shard.index.CurrentEntryNumber
shard.mu.RUnlock()
if c.client.logger != nil && IsDebug() {
c.client.logger.Debug("Updated endEntryNum after index reload",
"shard", shard.shardID,
"end_entry_num", endEntryNum)
}
}
// The reader cache invalidation in getOrCreateReader now handles stale data detection
// No need for the hacky workaround anymore
if startEntryNum >= endEntryNum {
return nil, nil // No new data
}
// Safety check: if we have entries but no files, something is wrong
if endEntryNum > 0 && fileCount == 0 {
return nil, fmt.Errorf("shard %d has %d entries but no data files", shard.shardID, endEntryNum)
}
// Preallocate slice capacity based on available entries and maxCount
availableEntries := endEntryNum - startEntryNum
expectedCount := availableEntries
if maxCount > 0 && expectedCount > int64(maxCount) {
expectedCount = int64(maxCount)
}
messages := make([]StreamMessage, 0, expectedCount)
// Read entries by entry number, looking up positions from index
for entryNum := startEntryNum; entryNum < endEntryNum && len(messages) < maxCount; entryNum++ {
// Check context
if ctx.Err() != nil {
return messages, ctx.Err()
}
// Get reader for this shard
reader, err := c.getOrCreateReader(shard)
if err != nil {
return nil, fmt.Errorf("failed to get reader for shard %d: %w", shard.shardID, err)
}
// Use the new ReadEntryByNumber method which handles position finding internally
data, err := reader.ReadEntryByNumber(entryNum)
if err != nil {
// Check if this is a transient error that can occur during concurrent operations
errStr := err.Error()
if strings.Contains(errStr, "mmap coherence issue") ||
strings.Contains(errStr, "not found in any file") ||
strings.Contains(errStr, "file too short") {
// These errors can happen when:
// 1. Reading very recently written data (mmap coherence)
// 2. The reader's index is stale and doesn't know about new files yet
// 3. File rotation happened but data wasn't fully flushed (file too short)
// Force the reader to refresh its index from the live shard
if refreshErr := reader.refreshFromLiveIndex(); refreshErr == nil {
// Try reading again with refreshed index
data, err = reader.ReadEntryByNumber(entryNum)
if err == nil {
// Success after refresh
goto gotData
}
}
// If still failing, give the system a moment to catch up
time.Sleep(10 * time.Millisecond)
// Try once more
data, err = reader.ReadEntryByNumber(entryNum)
if err != nil && (strings.Contains(err.Error(), "mmap coherence issue") ||