-
Notifications
You must be signed in to change notification settings - Fork 346
/
Copy pathnegative_acks_tracker.go
155 lines (127 loc) · 3.58 KB
/
negative_acks_tracker.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
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package pulsar
import (
"sync"
"time"
log "github.com/apache/pulsar-client-go/pulsar/log"
)
type redeliveryConsumer interface {
Redeliver(msgIDs []messageID)
}
type negativeAcksTracker struct {
sync.Mutex
doneCh chan interface{}
doneOnce sync.Once
negativeAcks map[messageID]time.Time
rc redeliveryConsumer
nackBackoff NackBackoffPolicy
tick *time.Ticker
delay time.Duration
log log.Logger
}
func newNegativeAcksTracker(rc redeliveryConsumer, delay time.Duration,
nackBackoffPolicy NackBackoffPolicy, logger log.Logger) *negativeAcksTracker {
t := &negativeAcksTracker{
doneCh: make(chan interface{}),
negativeAcks: make(map[messageID]time.Time),
rc: rc,
nackBackoff: nackBackoffPolicy,
log: logger,
}
if nackBackoffPolicy != nil {
firstDelayForNackBackoff := nackBackoffPolicy.Next(1)
t.delay = firstDelayForNackBackoff
} else {
t.delay = delay
}
t.tick = time.NewTicker(t.delay / 3)
go t.track()
return t
}
func (t *negativeAcksTracker) Add(msgID *messageID) {
// Always clear up the batch index since we want to track the nack
// for the entire batch
batchMsgID := messageID{
ledgerID: msgID.ledgerID,
entryID: msgID.entryID,
batchIdx: 0,
}
t.Lock()
defer t.Unlock()
_, present := t.negativeAcks[batchMsgID]
if present {
// The batch is already being tracked
return
}
targetTime := time.Now().Add(t.delay)
t.negativeAcks[batchMsgID] = targetTime
}
func (t *negativeAcksTracker) AddMessage(msg Message) {
nackBackoffDelay := t.nackBackoff.Next(msg.RedeliveryCount())
msgID := msg.ID()
// Always clear up the batch index since we want to track the nack
// for the entire batch
batchMsgID := messageID{
ledgerID: msgID.LedgerID(),
entryID: msgID.EntryID(),
batchIdx: 0,
}
t.Lock()
defer t.Unlock()
_, present := t.negativeAcks[batchMsgID]
if present {
// The batch is already being tracked
return
}
targetTime := time.Now().Add(nackBackoffDelay)
t.negativeAcks[batchMsgID] = targetTime
}
func (t *negativeAcksTracker) track() {
for {
select {
case <-t.doneCh:
t.log.Debug("Closing nack tracker")
return
case <-t.tick.C:
{
now := time.Now()
msgIDs := make([]messageID, 0)
t.Lock()
for msgID, targetTime := range t.negativeAcks {
t.log.Debugf("MsgId: %v -- targetTime: %v -- now: %v", msgID, targetTime, now)
if targetTime.Before(now) {
t.log.Debugf("Adding MsgId: %v", msgID)
msgIDs = append(msgIDs, msgID)
delete(t.negativeAcks, msgID)
}
}
t.Unlock()
if len(msgIDs) > 0 {
t.rc.Redeliver(msgIDs)
}
}
}
}
}
func (t *negativeAcksTracker) Close() {
// allow Close() to be invoked multiple times by consumer_partition to avoid panic
t.doneOnce.Do(func() {
t.tick.Stop()
t.doneCh <- nil
})
}