-
-
Notifications
You must be signed in to change notification settings - Fork 121
Expand file tree
/
Copy pathmap_broker_memory.go
More file actions
1465 lines (1322 loc) · 43.9 KB
/
Copy pathmap_broker_memory.go
File metadata and controls
1465 lines (1322 loc) · 43.9 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 centrifuge
import (
"container/heap"
"context"
"errors"
"sort"
"strconv"
"strings"
"sync"
"time"
"github.com/centrifugal/centrifuge/internal/memstream"
"github.com/centrifugal/centrifuge/internal/priority"
)
// MemoryMapBroker is builtin default MapBroker which allows running Centrifuge-based
// server without any external storage. All data managed inside process memory.
//
// With this MapBroker you can only run single Centrifuge node. If you need to scale
// you should consider using another MapBroker implementation instead – for example
// RedisMapBroker.
type MemoryMapBroker struct {
node *Node
eventHandler BrokerEventHandler
mapHub *mapHub
closeOnce sync.Once
closeCh chan struct{}
pubLocks map[int]*sync.Mutex
resultCache map[string]map[string]resultCacheEntry // ch -> idempotencyKey -> entry
resultCacheMu sync.RWMutex
nextExpireCheck int64
resultExpireQueue priority.Queue
}
type resultCacheEntry struct {
Position StreamPosition
ExpireAt int64 // UnixMilli
}
var _ MapBroker = (*MemoryMapBroker)(nil)
// MemoryMapBrokerConfig is a memory map broker config.
type MemoryMapBrokerConfig struct{}
// NewMemoryMapBroker initializes MemoryMapBroker.
func NewMemoryMapBroker(n *Node, _ MemoryMapBrokerConfig) (*MemoryMapBroker, error) {
pubLocks := make(map[int]*sync.Mutex, numPubLocks)
for i := 0; i < numPubLocks; i++ {
pubLocks[i] = &sync.Mutex{}
}
closeCh := make(chan struct{})
mapHub := newMapHub(n, pubLocks, closeCh)
mapHub.setChannelOptionsResolver(n.config.Map.GetMapChannelOptions)
e := &MemoryMapBroker{
node: n,
mapHub: mapHub,
pubLocks: pubLocks,
closeCh: closeCh,
resultCache: make(map[string]map[string]resultCacheEntry),
}
return e, nil
}
// RegisterEventHandler registers event handler and runs memory map broker.
func (e *MemoryMapBroker) RegisterEventHandler(h BrokerEventHandler) error {
e.eventHandler = h
e.mapHub.setEventHandler(h)
go e.expireResultCache()
e.mapHub.runCleanups()
return nil
}
// Close shuts down the broker.
func (e *MemoryMapBroker) Close(_ context.Context) error {
e.closeOnce.Do(func() {
close(e.closeCh)
})
return nil
}
func (e *MemoryMapBroker) pubLock(ch string) *sync.Mutex {
return e.pubLocks[index(ch, numPubLocks)]
}
func (e *MemoryMapBroker) Clear(_ context.Context, ch string, _ MapClearOptions) error {
mu := e.pubLock(ch)
mu.Lock()
defer mu.Unlock()
e.mapHub.clear(ch)
e.clearResultCache(ch)
return nil
}
// Subscribe is noop here.
func (e *MemoryMapBroker) Subscribe(_ ...string) error {
return nil
}
// Unsubscribe is noop here.
func (e *MemoryMapBroker) Unsubscribe(_ ...string) error {
return nil
}
// Publish publishes data to channel with optional key for keyed state.
func (e *MemoryMapBroker) Publish(ctx context.Context, ch string, key string, opts MapPublishOptions) (MapUpdateResult, error) {
mu := e.pubLock(ch)
mu.Lock()
defer mu.Unlock()
// Resolve and validate channel options.
chOpts, err := ResolveAndValidateMapChannelOptions(e.node.config.Map.GetMapChannelOptions, ch)
if err != nil {
return MapUpdateResult{}, err
}
// Reject CAS and Version in ephemeral mode.
if chOpts.Mode.IsEphemeral() {
if opts.ExpectedPosition != nil {
return MapUpdateResult{}, errors.New("CAS (ExpectedPosition) requires recoverable or persistent mode")
}
if opts.Version > 0 {
return MapUpdateResult{}, errors.New("version-based dedup requires recoverable or persistent mode")
}
}
if opts.IdempotencyKey != "" {
if res, ok := e.getResultFromCache(ch, opts.IdempotencyKey); ok {
return MapUpdateResult{Position: res, Suppressed: true, SuppressReason: SuppressReasonIdempotency}, nil
}
}
now := time.Now().UnixMilli()
// state publication stores full state (Data).
statePub := &Publication{
Data: opts.Data,
Info: opts.ClientInfo,
Tags: opts.Tags,
Time: now,
Key: key,
Score: opts.score,
}
streamPub := statePub
var prevPub *Publication
streamTop, prevPub, suppressReason, err := e.mapHub.add(ch, key, statePub, streamPub, chOpts, opts)
if err != nil {
return MapUpdateResult{}, err
}
if suppressReason != "" {
result := MapUpdateResult{Position: streamTop, Suppressed: true, SuppressReason: suppressReason}
// For CAS mismatch, include current key state for immediate retry.
// Client uses: CurrentEntry.Offset + Position.Epoch for the next CAS attempt.
if suppressReason == SuppressReasonPositionMismatch && prevPub != nil {
result.CurrentEntry = &MapCurrentEntry{Offset: prevPub.Offset, Data: prevPub.Data}
}
return result, nil
}
if opts.IdempotencyKey != "" {
resultExpireMs := int64(defaultIdempotentResultExpireSeconds) * 1000
if opts.IdempotentResultTTL != 0 {
resultExpireMs = opts.IdempotentResultTTL.Milliseconds()
}
e.saveResultToCache(ch, opts.IdempotencyKey, streamTop, resultExpireMs)
}
if e.eventHandler != nil {
// Publish streamPub to subscribers.
return MapUpdateResult{Position: streamTop}, e.eventHandler.HandlePublication(ch, streamPub, streamTop, opts.UseDelta, prevPub)
}
return MapUpdateResult{Position: streamTop}, nil
}
// Remove removes a key from keyed state.
func (e *MemoryMapBroker) Remove(ctx context.Context, ch string, key string, opts MapRemoveOptions) (MapUpdateResult, error) {
mu := e.pubLock(ch)
mu.Lock()
defer mu.Unlock()
// Resolve and validate channel options.
chOpts, err := ResolveAndValidateMapChannelOptions(e.node.config.Map.GetMapChannelOptions, ch)
if err != nil {
return MapUpdateResult{}, err
}
// Reject CAS in ephemeral mode.
if chOpts.Mode.IsEphemeral() {
if opts.ExpectedPosition != nil {
return MapUpdateResult{}, errors.New("CAS (ExpectedPosition) requires recoverable or persistent mode")
}
}
if opts.IdempotencyKey != "" {
if res, ok := e.getResultFromCache(ch, opts.IdempotencyKey); ok {
return MapUpdateResult{Position: res, Suppressed: true, SuppressReason: SuppressReasonIdempotency}, nil
}
}
streamTop, removePub, suppressReason, err := e.mapHub.remove(ch, key, chOpts, opts)
if err != nil {
return MapUpdateResult{}, err
}
if suppressReason != "" {
result := MapUpdateResult{Position: streamTop, Suppressed: true, SuppressReason: suppressReason}
if suppressReason == SuppressReasonPositionMismatch && removePub != nil {
result.CurrentEntry = &MapCurrentEntry{Offset: removePub.Offset, Data: removePub.Data}
}
return result, nil
}
if opts.IdempotencyKey != "" {
resultExpireMs := int64(defaultIdempotentResultExpireSeconds) * 1000
if opts.IdempotentResultTTL != 0 {
resultExpireMs = opts.IdempotentResultTTL.Milliseconds()
}
e.saveResultToCache(ch, opts.IdempotencyKey, streamTop, resultExpireMs)
}
if e.eventHandler != nil {
return MapUpdateResult{Position: streamTop}, e.eventHandler.HandlePublication(ch, removePub, streamTop, false, nil)
}
return MapUpdateResult{Position: streamTop}, nil
}
// ReadStream retrieves publications from stream.
func (e *MemoryMapBroker) ReadStream(ctx context.Context, ch string, opts MapReadStreamOptions) (MapStreamResult, error) {
return e.mapHub.getStream(ch, opts)
}
// ReadState retrieves keyed state with revisions.
func (e *MemoryMapBroker) ReadState(ctx context.Context, ch string, opts MapReadStateOptions) (MapStateResult, error) {
_, err := ResolveAndValidateMapChannelOptions(e.node.config.Map.GetMapChannelOptions, ch)
if err != nil {
return MapStateResult{}, err
}
return e.mapHub.getState(ch, opts)
}
// Stats returns state statistics.
func (e *MemoryMapBroker) Stats(ctx context.Context, ch string) (MapStats, error) {
return e.mapHub.getStats(ch)
}
func (e *MemoryMapBroker) getResultFromCache(ch string, key string) (StreamPosition, bool) {
e.resultCacheMu.RLock()
defer e.resultCacheMu.RUnlock()
chCache, ok := e.resultCache[ch]
if !ok {
return StreamPosition{}, false
}
entry, ok := chCache[key]
if !ok || entry.ExpireAt <= time.Now().UnixMilli() {
return StreamPosition{}, false
}
return entry.Position, true
}
func (e *MemoryMapBroker) saveResultToCache(ch string, key string, sp StreamPosition, resultExpireMs int64) {
e.resultCacheMu.Lock()
defer e.resultCacheMu.Unlock()
chCache, ok := e.resultCache[ch]
if !ok {
chCache = make(map[string]resultCacheEntry)
e.resultCache[ch] = chCache
}
expireAt := time.Now().UnixMilli() + resultExpireMs
chCache[key] = resultCacheEntry{Position: sp, ExpireAt: expireAt}
cacheKey := ch + "\x00" + key
heap.Push(&e.resultExpireQueue, &priority.Item{Value: cacheKey, Priority: expireAt})
if e.nextExpireCheck == 0 || e.nextExpireCheck > expireAt {
e.nextExpireCheck = expireAt
}
}
func (e *MemoryMapBroker) clearResultCache(ch string) {
e.resultCacheMu.Lock()
defer e.resultCacheMu.Unlock()
delete(e.resultCache, ch)
}
func (e *MemoryMapBroker) expireResultCache() {
var nextExpireCheck int64
timer := time.NewTimer(time.Second)
defer timer.Stop()
for {
select {
case <-timer.C:
case <-e.closeCh:
return
}
e.resultCacheMu.Lock()
now := time.Now().UnixMilli()
if e.nextExpireCheck == 0 || e.nextExpireCheck > now {
e.resultCacheMu.Unlock()
timer.Reset(time.Second)
continue
}
nextExpireCheck = 0
for e.resultExpireQueue.Len() > 0 {
item := heap.Pop(&e.resultExpireQueue).(*priority.Item)
expireAt := item.Priority
if expireAt > now {
heap.Push(&e.resultExpireQueue, item)
nextExpireCheck = expireAt
break
}
combined := item.Value
if idx := strings.IndexByte(combined, '\x00'); idx >= 0 {
ch := combined[:idx]
key := combined[idx+1:]
if chCache, ok := e.resultCache[ch]; ok {
if entry, keyOk := chCache[key]; keyOk && entry.ExpireAt <= now {
delete(chCache, key)
if len(chCache) == 0 {
delete(e.resultCache, ch)
}
}
}
}
}
// Compact heap when stale entries accumulate excessively.
var totalCacheEntries int
for _, chCache := range e.resultCache {
totalCacheEntries += len(chCache)
}
if e.resultExpireQueue.Len() > 2*totalCacheEntries+100 {
e.resultExpireQueue = priority.MakeQueue()
for ch, chCache := range e.resultCache {
for key, entry := range chCache {
cacheKey := ch + "\x00" + key
heap.Push(&e.resultExpireQueue, &priority.Item{Value: cacheKey, Priority: entry.ExpireAt})
}
}
if e.resultExpireQueue.Len() > 0 {
nextExpireCheck = e.resultExpireQueue[0].Priority
}
}
e.nextExpireCheck = nextExpireCheck
e.resultCacheMu.Unlock()
timer.Reset(time.Second)
}
}
// mapHub manages keyed state for all channels.
//
// Lock ordering (acquire in this order to prevent deadlock):
//
// pubLock(ch) → mapHub.Lock/RLock → mapChannel.mu
//
// pubLock: serializes Publish/Remove per channel (including stream.Add and HandlePublication).
// mapHub.Lock: protects channels map, expiration queues, and channel creation/deletion.
// mapHub.RLock: concurrent reads of channel state.
// mapChannel.mu: protects sortedKeys rebuild during getState (held under mapHub.RLock).
//
// expireKeysIteration uses two phases to respect this ordering:
//
// Phase 1: mapHub.Lock — collect expired keys, remove from state.
// Phase 2: pubLock → mapHub.Lock — add to stream, deliver events.
type mapHub struct {
sync.RWMutex
node *Node
channels map[string]*mapChannel
nextExpireCheck int64
expireQueue priority.Queue
expires map[string]int64
nextRemoveCheck int64
removeQueue priority.Queue
removes map[string]int64
closeCh chan struct{}
// Key TTL tracking
nextKeyExpireCheck int64
keyExpireQueue priority.Queue // priority queue of {ch:key, expireAt}
keyExpires map[string]int64 // "ch:key" -> expireAt
eventHandler BrokerEventHandler // for publishing removal events
channelOptionsResolver func(channel string) MapChannelOptions // for key expiration events
pubLocks map[int]*sync.Mutex // for ordering HandlePublication calls
}
// mapChannel represents keyed state for a single channel.
type mapChannel struct {
mu sync.Mutex // protects sortedKeys rebuild in getState
stream *memstream.Stream
state map[string]*stateEntry // key -> entry
ordered bool
scores map[string]int64 // key -> score (for ordered state)
sortedKeys []string // cached sorted keys
sortedKeysDirty bool // true if sortedKeys needs rebuilding
lastSortOrdered bool // tracks whether last sort used ordered or unordered
lastSortAsc bool // tracks last sort direction for ordered state
}
type stateEntry struct {
Key string
Revision StreamPosition
Publication *Publication
Score int64 // For ordered state
ExpireAt int64 // Millisecond timestamp (UnixMilli) for key TTL expiration (0 = no expiration)
Version uint64 // Per-key version for ordering (0 = disabled)
VersionEpoch string // Per-key version epoch
}
func newMapHub(node *Node, pubLocks map[int]*sync.Mutex, closeCh chan struct{}) *mapHub {
return &mapHub{
node: node,
channels: make(map[string]*mapChannel),
expireQueue: priority.MakeQueue(),
expires: make(map[string]int64),
removeQueue: priority.MakeQueue(),
removes: make(map[string]int64),
closeCh: closeCh,
keyExpireQueue: priority.MakeQueue(),
keyExpires: make(map[string]int64),
pubLocks: pubLocks,
}
}
func (h *mapHub) setChannelOptionsResolver(r func(channel string) MapChannelOptions) {
h.Lock()
defer h.Unlock()
h.channelOptionsResolver = r
}
func (h *mapHub) setEventHandler(handler BrokerEventHandler) {
h.Lock()
defer h.Unlock()
h.eventHandler = handler
}
func (h *mapHub) runCleanups() {
go h.expireStreams()
go h.removeChannels()
go h.expireKeys()
}
func (h *mapHub) expireStreams() {
var nextExpireCheck int64
timer := time.NewTimer(time.Second)
defer timer.Stop()
for {
select {
case <-timer.C:
case <-h.closeCh:
return
}
h.Lock()
if h.nextExpireCheck == 0 || h.nextExpireCheck > time.Now().UnixMilli() {
h.Unlock()
timer.Reset(time.Second)
continue
}
nextExpireCheck = 0
for h.expireQueue.Len() > 0 {
item := heap.Pop(&h.expireQueue).(*priority.Item)
expireAt := item.Priority
if expireAt > time.Now().UnixMilli() {
heap.Push(&h.expireQueue, item)
nextExpireCheck = expireAt
break
}
ch := item.Value
exp, ok := h.expires[ch]
if !ok {
continue
}
if exp <= expireAt {
delete(h.expires, ch)
if channel, ok := h.channels[ch]; ok && channel.stream != nil {
channel.stream.Clear()
}
} else {
heap.Push(&h.expireQueue, &priority.Item{Value: ch, Priority: exp})
}
}
h.nextExpireCheck = nextExpireCheck
h.Unlock()
timer.Reset(time.Second)
}
}
func (h *mapHub) removeChannels() {
var nextRemoveCheck int64
timer := time.NewTimer(time.Second)
defer timer.Stop()
for {
select {
case <-timer.C:
case <-h.closeCh:
return
}
h.Lock()
if h.nextRemoveCheck == 0 || h.nextRemoveCheck > time.Now().UnixMilli() {
h.Unlock()
timer.Reset(time.Second)
continue
}
nextRemoveCheck = 0
for h.removeQueue.Len() > 0 {
item := heap.Pop(&h.removeQueue).(*priority.Item)
expireAt := item.Priority
if expireAt > time.Now().UnixMilli() {
heap.Push(&h.removeQueue, item)
nextRemoveCheck = expireAt
break
}
ch := item.Value
exp, ok := h.removes[ch]
if !ok {
continue
}
if exp <= expireAt {
delete(h.removes, ch)
delete(h.channels, ch)
} else {
heap.Push(&h.removeQueue, &priority.Item{Value: ch, Priority: exp})
}
}
h.nextRemoveCheck = nextRemoveCheck
h.Unlock()
timer.Reset(time.Second)
}
}
// expiredKeyEvent holds a Phase 1 snapshot of an expired key candidate. Phase 2
// re-validates the entry under pubLock(ch) → hub lock before deleting state and
// appending the removal to the stream atomically.
type expiredKeyEvent struct {
channel string
key string
expireAt int64
tags map[string]string
streamSize int
}
// expireKeys handles TTL-based expiration of individual state keys.
// When a key expires, it removes it from the state, updates aggregation counts,
// and publishes a removal event.
func (h *mapHub) expireKeys() {
var nextKeyExpireCheck int64
timer := time.NewTimer(time.Second)
defer timer.Stop()
for {
select {
case <-timer.C:
case <-h.closeCh:
return
}
h.expireKeysIteration(&nextKeyExpireCheck)
timer.Reset(time.Second)
}
}
func (h *mapHub) expireKeysIteration(nextKeyExpireCheck *int64) {
// Phase 1: Under hub lock — collect expired key candidates only. State is NOT
// mutated here. Mutating state in Phase 1 without holding pubLock(ch) would
// expose subscribers to an inconsistent ReadState→ReadStream window where the
// key is gone from state but the corresponding removal event is not yet on the
// stream — they would later receive a removal for a key they never saw. Phase 2
// acquires pubLock(ch) → hub lock per channel and atomically deletes state +
// appends the removal to the stream + invokes the handler, matching the lock
// ordering and atomicity of Remove() and Publish().
var expiredEvents []expiredKeyEvent
var eventHandler BrokerEventHandler
var oldestExpireAt int64 // Track oldest expired timestamp for lag metric.
h.Lock()
if h.nextKeyExpireCheck == 0 || h.nextKeyExpireCheck > time.Now().UnixMilli() {
h.Unlock()
return
}
*nextKeyExpireCheck = 0
now := time.Now().UnixMilli()
eventHandler = h.eventHandler
for h.keyExpireQueue.Len() > 0 {
item := heap.Pop(&h.keyExpireQueue).(*priority.Item)
expireAt := item.Priority
if expireAt > now {
heap.Push(&h.keyExpireQueue, item)
*nextKeyExpireCheck = expireAt
break
}
chKey := item.Value // format: "channel\x00key"
storedExpireAt, ok := h.keyExpires[chKey]
if !ok {
continue
}
// Check if expiration time was updated (key was refreshed)
if storedExpireAt > expireAt {
// Re-queue with updated expiration
heap.Push(&h.keyExpireQueue, &priority.Item{Value: chKey, Priority: storedExpireAt})
continue
}
// Parse channel and key from combined string
ch, key := h.parseChKey(chKey)
if ch == "" || key == "" {
delete(h.keyExpires, chKey)
continue
}
channel, ok := h.channels[ch]
if !ok {
delete(h.keyExpires, chKey)
continue
}
entry, ok := channel.state[key]
if !ok {
delete(h.keyExpires, chKey)
continue
}
// Verify entry's expiration matches (wasn't refreshed)
if entry.ExpireAt != expireAt {
if entry.ExpireAt > now { // now is UnixMilli
// Entry was refreshed, re-queue
h.keyExpires[chKey] = entry.ExpireAt
heap.Push(&h.keyExpireQueue, &priority.Item{Value: chKey, Priority: entry.ExpireAt})
}
continue
}
// Track the oldest expired timestamp for the lag metric.
if oldestExpireAt == 0 || expireAt < oldestExpireAt {
oldestExpireAt = expireAt
}
var streamSize int
if h.channelOptionsResolver != nil {
chOpts, err := ResolveAndValidateMapChannelOptions(h.channelOptionsResolver, ch)
if err == nil && chOpts.Mode.HasStream() {
streamSize = chOpts.StreamSize
}
}
expiredEvents = append(expiredEvents, expiredKeyEvent{
channel: ch,
key: key,
expireAt: expireAt,
tags: entry.Publication.Tags,
streamSize: streamSize,
})
}
// Compact heap when stale entries exceed 2x live entries.
// Stale entries accumulate from TTL refreshes that push new items without
// removing old ones. Periodic compaction rebuilds the queue from h.keyExpires,
// the source of truth for per-key deadlines. This is correct because every
// key insertion/refresh updates h.keyExpires with the latest deadline, and
// every key removal deletes from h.keyExpires. The queue may contain outdated
// entries (old deadlines for refreshed keys), but they are harmlessly skipped
// at pop time when the deadline doesn't match h.keyExpires.
if h.keyExpireQueue.Len() > 2*len(h.keyExpires)+100 {
h.keyExpireQueue = priority.MakeQueue()
for chKey, exp := range h.keyExpires {
heap.Push(&h.keyExpireQueue, &priority.Item{Value: chKey, Priority: exp})
}
if h.keyExpireQueue.Len() > 0 {
*nextKeyExpireCheck = h.keyExpireQueue[0].Priority
}
}
h.nextKeyExpireCheck = *nextKeyExpireCheck
h.Unlock()
// Report cleanup lag metric outside the lock.
if h.node != nil && h.node.metrics != nil {
if oldestExpireAt > 0 {
lagSeconds := float64(now-oldestExpireAt) / 1000.0
if lagSeconds < 0 {
lagSeconds = 0
}
h.node.metrics.setMapBrokerCleanupLag("", lagSeconds)
} else {
h.node.metrics.setMapBrokerCleanupLag("", 0)
}
}
// Phase 2: Under pubLock(ch) → hub lock — delete state, append removal stream
// entry, and dispatch the event atomically per channel. Re-validate the entry
// to handle refreshes or removals that landed after the Phase 1 snapshot.
var keysRemoved int64
for _, event := range expiredEvents {
var mu *sync.Mutex
if h.pubLocks != nil {
mu = h.pubLocks[index(event.channel, numPubLocks)]
mu.Lock()
}
removePub := &Publication{
Key: event.key,
Removed: true,
Time: time.Now().UnixMilli(),
Tags: event.tags,
}
var streamPos StreamPosition
var dispatch bool
h.Lock()
channel, ok := h.channels[event.channel]
if ok {
entry, exists := channel.state[event.key]
chKey := h.makeChKey(event.channel, event.key)
if exists && entry.ExpireAt == event.expireAt {
// Still expired with the same deadline — delete state and stream-append atomically.
delete(channel.state, event.key)
delete(h.keyExpires, chKey)
keysRemoved++
channel.sortedKeysDirty = true
if channel.ordered {
delete(channel.scores, event.key)
}
if channel.stream != nil {
if event.streamSize > 0 {
offset, _ := channel.stream.Add(removePub, event.streamSize, 0, "")
removePub.Offset = offset
streamPos = StreamPosition{Offset: offset, Epoch: channel.stream.Epoch()}
} else {
streamPos = StreamPosition{Offset: channel.stream.Top(), Epoch: channel.stream.Epoch()}
}
}
dispatch = eventHandler != nil
} else if exists && entry.ExpireAt > now {
// Entry was refreshed between Phase 1 and Phase 2 — re-queue.
h.keyExpires[chKey] = entry.ExpireAt
heap.Push(&h.keyExpireQueue, &priority.Item{Value: chKey, Priority: entry.ExpireAt})
if h.nextKeyExpireCheck == 0 || entry.ExpireAt < h.nextKeyExpireCheck {
h.nextKeyExpireCheck = entry.ExpireAt
}
}
}
h.Unlock()
if dispatch {
err := eventHandler.HandlePublication(event.channel, removePub, streamPos, false, nil)
if err != nil && h.node != nil {
h.node.logger.log(newErrorLogEntry(err, "error handling expired key publication", map[string]any{"channel": event.channel, "key": event.key}))
if h.node.metrics != nil {
h.node.metrics.incMapBrokerCleanupErrors("")
}
}
}
if mu != nil {
mu.Unlock()
}
}
if h.node != nil && h.node.metrics != nil && keysRemoved > 0 {
h.node.metrics.addMapBrokerCleanupRemoved("", keysRemoved)
}
}
// makeChKey creates a combined channel:key string for the expiration map.
func (h *mapHub) makeChKey(ch, key string) string {
return ch + "\x00" + key
}
// parseChKey splits a combined channel:key string.
func (h *mapHub) parseChKey(chKey string) (string, string) {
for i := 0; i < len(chKey); i++ {
if chKey[i] == '\x00' {
return chKey[:i], chKey[i+1:]
}
}
return "", ""
}
func (h *mapHub) add(ch string, key string, statePub *Publication, streamPub *Publication, chOpts MapChannelOptions, opts MapPublishOptions) (StreamPosition, *Publication, SuppressReason, error) {
h.Lock()
defer h.Unlock()
var prevPub *Publication
if opts.UseDelta && key != "" {
// Get previous publication for delta (key-based: same key's previous state).
if channel, ok := h.channels[ch]; ok {
if entry, ok := channel.state[key]; ok {
prevPub = entry.Publication
}
}
}
channel, ok := h.channels[ch]
if !ok {
channel = &mapChannel{
stream: memstream.New(),
state: make(map[string]*stateEntry),
ordered: chOpts.ordered,
scores: make(map[string]int64),
}
h.channels[ch] = channel
} else if chOpts.ordered && !channel.ordered {
channel.ordered = true
channel.sortedKeysDirty = true
}
// Canonical check order across brokers: Version → KeyMode → CAS.
// Dedup checks (version) drop duplicates first; constraint checks
// (KeyMode, CAS) report meaningful intent failures last.
// Version is gated by HasStream — streamless channels skip dedup.
if chOpts.Mode.HasStream() && key != "" && opts.Version > 0 {
if existing, ok := channel.state[key]; ok {
if (opts.VersionEpoch == "" || opts.VersionEpoch == existing.VersionEpoch) &&
opts.Version <= existing.Version {
var pos StreamPosition
if channel.stream != nil {
pos = StreamPosition{Offset: channel.stream.Top(), Epoch: channel.stream.Epoch()}
}
return pos, nil, SuppressReasonVersion, nil
}
}
}
// Check KeyMode condition before proceeding
if key != "" && opts.KeyMode != KeyModeReplace {
existingEntry, keyExists := channel.state[key]
if opts.KeyMode == KeyModeIfNew && keyExists {
// KeyModeIfNew but key already exists - suppress publish
// But optionally refresh TTL if RefreshTTLOnSuppress is set
if opts.RefreshTTLOnSuppress && chOpts.KeyTTL > 0 {
expireAt := time.Now().UnixMilli() + chOpts.KeyTTL.Milliseconds()
existingEntry.ExpireAt = expireAt
// Update TTL tracking
chKey := h.makeChKey(ch, key)
heap.Push(&h.keyExpireQueue, &priority.Item{Value: chKey, Priority: expireAt})
h.keyExpires[chKey] = expireAt
if h.nextKeyExpireCheck == 0 || h.nextKeyExpireCheck > expireAt {
h.nextKeyExpireCheck = expireAt
}
// Keepalive must extend MetaTTL too — without this the channel
// can be garbage-collected by removeChannels even while keys are
// being refreshed, forcing an epoch reset on the next publish.
if chOpts.MetaTTL > 0 {
removeAt := time.Now().UnixMilli() + chOpts.MetaTTL.Milliseconds()
if _, ok := h.removes[ch]; !ok {
heap.Push(&h.removeQueue, &priority.Item{Value: ch, Priority: removeAt})
}
h.removes[ch] = removeAt
if h.nextRemoveCheck == 0 || h.nextRemoveCheck > removeAt {
h.nextRemoveCheck = removeAt
}
}
}
var pos StreamPosition
if channel.stream != nil {
pos = StreamPosition{Offset: channel.stream.Top(), Epoch: channel.stream.Epoch()}
}
return pos, nil, SuppressReasonKeyExists, nil
}
if opts.KeyMode == KeyModeIfExists && !keyExists {
// KeyModeIfExists but key doesn't exist - skip
var pos StreamPosition
if channel.stream != nil {
pos = StreamPosition{Offset: channel.stream.Top(), Epoch: channel.stream.Epoch()}
}
return pos, nil, SuppressReasonKeyNotFound, nil
}
}
// CAS check: verify expected position (offset + epoch)
if key != "" && opts.ExpectedPosition != nil {
existing, exists := channel.state[key]
var pos StreamPosition
if channel.stream != nil {
pos = StreamPosition{Offset: channel.stream.Top(), Epoch: channel.stream.Epoch()}
}
if !exists {
// Key doesn't exist - position mismatch
return pos, nil, SuppressReasonPositionMismatch, nil
}
// Check both offset AND epoch
if existing.Publication.Offset != opts.ExpectedPosition.Offset ||
pos.Epoch != opts.ExpectedPosition.Epoch {
// Return current publication for immediate retry.
// Client uses: CurrentEntry.Offset + Position.Epoch for the next CAS attempt.
return pos, existing.Publication, SuppressReasonPositionMismatch, nil
}
}
var streamPosition StreamPosition
// Handle stream
if chOpts.Mode.HasStream() {
expireAt := time.Now().UnixMilli() + chOpts.StreamTTL.Milliseconds()
if _, ok := h.expires[ch]; !ok {
heap.Push(&h.expireQueue, &priority.Item{Value: ch, Priority: expireAt})
}
h.expires[ch] = expireAt
if h.nextExpireCheck == 0 || h.nextExpireCheck > expireAt {
h.nextExpireCheck = expireAt
}
if chOpts.MetaTTL > 0 {
removeAt := time.Now().UnixMilli() + chOpts.MetaTTL.Milliseconds()
if _, ok := h.removes[ch]; !ok {
heap.Push(&h.removeQueue, &priority.Item{Value: ch, Priority: removeAt})
}
h.removes[ch] = removeAt
if h.nextRemoveCheck == 0 || h.nextRemoveCheck > removeAt {
h.nextRemoveCheck = removeAt
}
}
offset, _ := channel.stream.Add(streamPub, chOpts.StreamSize, 0, "")
streamPub.Offset = offset // Set offset on publication for delivery
streamPosition = StreamPosition{
Offset: offset,
Epoch: channel.stream.Epoch(),
}
} else {
// No stream, just use current position
if channel.stream != nil {
streamPosition = StreamPosition{
Offset: channel.stream.Top(),
Epoch: channel.stream.Epoch(),
}
}
}
// Handle keyed state.
if key != "" {
// Calculate expiration time (milliseconds for sub-second TTL precision).
var expireAt int64
if chOpts.KeyTTL > 0 {
expireAt = time.Now().UnixMilli() + chOpts.KeyTTL.Milliseconds()
}
// Store statePub in state (contains full state Data).
// Preserve stored version when caller publishes without one (matches Redis).
// Overwriting with 0 would erase dedup protection against late-arriving
// older versions from a concurrent producer.
version := opts.Version
versionEpoch := opts.VersionEpoch
if version == 0 {
if existing, ok := channel.state[key]; ok {
version = existing.Version
versionEpoch = existing.VersionEpoch
}
}
statePub.Offset = streamPosition.Offset
entry := &stateEntry{
Key: key,
Revision: streamPosition,
Publication: statePub,
Score: opts.score,
ExpireAt: expireAt,
Version: version,
VersionEpoch: versionEpoch,
}
channel.state[key] = entry
// Mark sorted keys as dirty for any state change
channel.sortedKeysDirty = true
if chOpts.ordered {
channel.scores[key] = opts.score
}
// Handle key TTL expiration tracking
if chOpts.KeyTTL > 0 {
chKey := h.makeChKey(ch, key)
// Always push new heap entry. When a key is refreshed, the old heap entry
// becomes stale and will be discarded in expireKeysIteration (which checks
// storedExpireAt > poppedExpireAt). Pushing unconditionally ensures the heap
// has an entry with the correct (latest) expiration time.
heap.Push(&h.keyExpireQueue, &priority.Item{Value: chKey, Priority: expireAt})
h.keyExpires[chKey] = expireAt
if h.nextKeyExpireCheck == 0 || h.nextKeyExpireCheck > expireAt {
h.nextKeyExpireCheck = expireAt
}
}
}
return streamPosition, prevPub, SuppressReasonNone, nil
}
func (h *mapHub) remove(ch string, key string, chOpts MapChannelOptions, opts MapRemoveOptions) (StreamPosition, *Publication, SuppressReason, error) {
h.Lock()
defer h.Unlock()
channel, ok := h.channels[ch]
if !ok {
// Channel doesn't exist. When CAS is requested, the caller's
// ExpectedPosition cannot match (no meta yet) — mirror the Redis
// Lua which auto-creates meta with a fresh epoch and then returns
// position_mismatch against the caller's stale epoch. Without
// ExpectedPosition, surface KeyNotFound as before.
if opts.ExpectedPosition != nil {
return StreamPosition{}, nil, SuppressReasonPositionMismatch, nil
}
return StreamPosition{}, nil, SuppressReasonKeyNotFound, nil
}
var removeTags map[string]string
entry, keyExists := channel.state[key]