Skip to content

Commit 4ac2873

Browse files
committed
fix(panic): partition offset manager
Signed-off-by: napallday <[email protected]>
1 parent bf40615 commit 4ac2873

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

balance_strategy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
// StickyBalanceStrategyName identifies strategies that use the sticky-partition assignment strategy
2020
StickyBalanceStrategyName = "sticky"
2121

22+
// CooperativeStickyBalanceStrategyName identifies strategies that use the cooperative sticky-partition assignment strategy
2223
CooperativeStickyBalanceStrategyName = "cooperative-sticky"
2324

2425
defaultGeneration = -1

offset_manager.go

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,13 @@ func (om *offsetManager) ManagePartition(topic string, partition int32) (Partiti
140140
}
141141

142142
func (om *offsetManager) RemovePartitions(topicPartitions map[string][]int32) error {
143+
// flush one last time
144+
if om.conf.Consumer.Offsets.AutoCommit.Enable {
145+
for attempt := 0; attempt <= om.conf.Consumer.Offsets.Retry.Max; attempt++ {
146+
om.flushToBroker()
147+
}
148+
}
149+
143150
var errs ConsumerErrors
144151
var errsLock sync.Mutex
145152

@@ -150,10 +157,15 @@ func (om *offsetManager) RemovePartitions(topicPartitions map[string][]int32) er
150157
go func(topic string, partition int32) {
151158
defer wg.Done()
152159

153-
om.pomsLock.RLock()
160+
om.pomsLock.Lock()
154161
pom := om.poms[topic][partition]
155-
om.pomsLock.RUnlock()
156162
err := pom.Close()
163+
delete(om.poms[topic], partition)
164+
if len(om.poms[topic]) == 0 {
165+
delete(om.poms, topic)
166+
}
167+
om.pomsLock.Unlock()
168+
157169
if err != nil {
158170
errsLock.Lock()
159171
var consumerErrs ConsumerErrors
@@ -167,24 +179,6 @@ func (om *offsetManager) RemovePartitions(topicPartitions map[string][]int32) er
167179
}
168180
wg.Wait()
169181

170-
// flush one last time
171-
if om.conf.Consumer.Offsets.AutoCommit.Enable {
172-
for attempt := 0; attempt <= om.conf.Consumer.Offsets.Retry.Max; attempt++ {
173-
om.flushToBroker()
174-
}
175-
}
176-
177-
om.pomsLock.Lock()
178-
for topic, partitions := range topicPartitions {
179-
for _, partition := range partitions {
180-
delete(om.poms[topic], partition)
181-
if len(om.poms[topic]) == 0 {
182-
delete(om.poms, topic)
183-
}
184-
}
185-
}
186-
187-
om.pomsLock.Unlock()
188182
if len(errs) > 0 {
189183
return errs
190184
}

0 commit comments

Comments
 (0)