Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: failTimeoutMessages cannot delete outdated messages #1247

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 15 additions & 21 deletions pulsar/producer_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,7 @@ func (p *partitionProducer) grabCnx(assignedBrokerURL string) error {
continue
}
pi := item.(*pendingItem)
// when resending pending batches, we update the sendAt timestamp and put to the back of queue
// to avoid pending item been removed by failTimeoutMessages and cause race condition
// when resending pending batches, we update the sendAt timestamp to record the metric.
pi.Lock()
pi.sentAt = time.Now()
pi.Unlock()
Expand Down Expand Up @@ -814,19 +813,14 @@ func (p *partitionProducer) internalSingleSend(
return
}

p.pendingQueue.Put(&pendingItem{
sentAt: time.Now(),
buffer: buffer,
sequenceID: sid,
sendRequests: []interface{}{sr},
})
p._getConn().WriteData(buffer)
p.writeData(buffer, sid, []interface{}{sr})
}

type pendingItem struct {
sync.Mutex
buffer internal.Buffer
sequenceID uint64
createdAt time.Time
sentAt time.Time
sendRequests []interface{}
isDone bool
Expand Down Expand Up @@ -868,13 +862,19 @@ func (p *partitionProducer) internalFlushCurrentBatch() {
return
}

p.writeData(batchData, sequenceID, callbacks)
}

func (p *partitionProducer) writeData(buffer internal.Buffer, sequenceID uint64, callbacks []interface{}) {
now := time.Now()
p.pendingQueue.Put(&pendingItem{
sentAt: time.Now(),
buffer: batchData,
createdAt: now,
sentAt: now,
buffer: buffer,
sequenceID: sequenceID,
sendRequests: callbacks,
})
p._getConn().WriteData(batchData)
p._getConn().WriteData(buffer)
}

func (p *partitionProducer) failTimeoutMessages() {
Expand All @@ -898,7 +898,7 @@ func (p *partitionProducer) failTimeoutMessages() {
continue
}
oldestItem := item.(*pendingItem)
if nextWaiting := diff(oldestItem.sentAt); nextWaiting > 0 {
if nextWaiting := diff(oldestItem.createdAt); nextWaiting > 0 {
// none of these pending messages have timed out, wait and retry
t.Reset(nextWaiting)
continue
Expand Down Expand Up @@ -930,7 +930,7 @@ func (p *partitionProducer) failTimeoutMessages() {
pi := m.(*pendingItem)
pi.Lock()
defer pi.Unlock()
if nextWaiting := diff(pi.sentAt); nextWaiting > 0 {
if nextWaiting := diff(pi.createdAt); nextWaiting > 0 {
// current and subsequent items not timeout yet, stop iterating
tickerNeedWaiting = nextWaiting
return false
Expand Down Expand Up @@ -995,13 +995,7 @@ func (p *partitionProducer) internalFlushCurrentBatches() {
if b.BatchData == nil {
continue
}
p.pendingQueue.Put(&pendingItem{
sentAt: time.Now(),
buffer: b.BatchData,
sequenceID: b.SequenceID,
sendRequests: b.Callbacks,
})
p._getConn().WriteData(b.BatchData)
p.writeData(b.BatchData, b.SequenceID, b.Callbacks)
}

}
Expand Down
Loading