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

[Issue 1258][producer] Avoid a data race when flushing with load #1261

Merged
merged 1 commit into from
Jul 30, 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
33 changes: 20 additions & 13 deletions pulsar/producer_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,6 @@ func (p *partitionProducer) grabCnx(assignedBrokerURL string) error {
p.schemaCache.Put(p.schemaInfo, schemaVersion)
}

if err != nil {
return err
}

if !p.options.DisableBatching && p.batchBuilder == nil {
provider, err := GetBatcherBuilderProvider(p.options.BatcherBuilderType)
if err != nil {
Expand Down Expand Up @@ -1022,15 +1018,7 @@ func (p *partitionProducer) internalFlushCurrentBatches() {
}

func (p *partitionProducer) internalFlush(fr *flushRequest) {
// clear all the messages which have sent to dataChan before flush
if len(p.dataChan) != 0 {
oldDataChan := p.dataChan
p.dataChan = make(chan *sendRequest, p.options.MaxPendingMessages)
for len(oldDataChan) != 0 {
pendingData := <-oldDataChan
p.internalSend(pendingData)
}
}
p.clearPendingSendRequests()

if !p.options.DisableBatching {
p.internalFlushCurrentBatch()
Expand Down Expand Up @@ -1061,6 +1049,25 @@ func (p *partitionProducer) internalFlush(fr *flushRequest) {
}
}

// clearPendingSendRequests makes sure to push forward previous sending requests
// by emptying the data channel.
func (p *partitionProducer) clearPendingSendRequests() {
sizeBeforeFlushing := len(p.dataChan)

// Bound the for loop to the current length of the channel to ensure that it
// will eventually stop as we only want to ensure that existing messages are
// flushed.
for i := 0; i < sizeBeforeFlushing; i++ {
select {
case pendingData := <-p.dataChan:
p.internalSend(pendingData)

default:
return
}
}
}

func (p *partitionProducer) Send(ctx context.Context, msg *ProducerMessage) (MessageID, error) {
var err error
var msgID MessageID
Expand Down
Loading