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

ticdc: Fix CPU surge problem in sorter #10739

Merged
merged 7 commits into from
Mar 11, 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
56 changes: 36 additions & 20 deletions cdc/processor/sourcemanager/sorter/pebble/event_sorter.go
Original file line number Diff line number Diff line change
Expand Up @@ -392,14 +392,16 @@
case batchEvent := <-batchCh:
// do batch commit
batch := batchEvent.batch
writeBytes.Observe(float64(len(batch.Repr())))
start := time.Now()
if err := batch.Commit(&pebbleWriteOptions); err != nil {
log.Panic("failed to commit pebble batch", zap.Error(err),
zap.String("namespace", s.changefeedID.Namespace),
zap.String("changefeed", s.changefeedID.ID))
if !batch.Empty() {
writeBytes.Observe(float64(len(batch.Repr())))
start := time.Now()
if err := batch.Commit(&pebbleWriteOptions); err != nil {
log.Panic("failed to commit pebble batch", zap.Error(err),
zap.String("namespace", s.changefeedID.Namespace),
zap.String("changefeed", s.changefeedID.ID))
}

Check warning on line 402 in cdc/processor/sourcemanager/sorter/pebble/event_sorter.go

View check run for this annotation

Codecov / codecov/patch

cdc/processor/sourcemanager/sorter/pebble/event_sorter.go#L399-L402

Added lines #L399 - L402 were not covered by tests
writeDuration.Observe(time.Since(start).Seconds())
}
writeDuration.Observe(time.Since(start).Seconds())

// update resolved ts after commit successfully
batchResolved := batchEvent.batchResolved
Expand Down Expand Up @@ -440,11 +442,10 @@
s.wg.Add(1)
go s.batchCommitAndUpdateResolvedTs(batchCh, id)

batch := db.NewBatch()
newResolved := spanz.NewHashMap[model.Ts]()
startToCollectBatch := time.Now()
ticker := time.NewTicker(batchCommitInterval / 2)
defer ticker.Stop()

handleItem := func(item eventWithTableID) {
encodeItemAndBatch := func(batch *pebble.Batch, newResolved *spanz.HashMap[model.Ts], item eventWithTableID) {
if item.event.IsResolved() {
newResolved.ReplaceOrInsert(item.span, item.event.CRTs)
return
Expand All @@ -463,21 +464,36 @@
}
}

for {
for len(batch.Repr()) < batchCommitSize && time.Since(startToCollectBatch) < batchCommitInterval {
// Batch item and commit until batch size is larger than batchCommitSize,
// or the time since the last commit is larger than batchCommitInterval.
// Only return false when the sorter is closed.
doBatching := func() (*DBBatchEvent, bool) {
batch := db.NewBatch()
newResolved := spanz.NewHashMap[model.Ts]()
startToBatch := time.Now()
for {
select {
case item := <-inputCh:
handleItem(item)
encodeItemAndBatch(batch, newResolved, item)
if len(batch.Repr()) >= batchCommitSize {
return &DBBatchEvent{batch, newResolved}, true
}

Check warning on line 480 in cdc/processor/sourcemanager/sorter/pebble/event_sorter.go

View check run for this annotation

Codecov / codecov/patch

cdc/processor/sourcemanager/sorter/pebble/event_sorter.go#L479-L480

Added lines #L479 - L480 were not covered by tests
case <-s.closed:
return
default:
return nil, false
case <-ticker.C:
if time.Since(startToBatch) >= batchCommitInterval {
return &DBBatchEvent{batch, newResolved}, true
}
}
}
batchCh <- &DBBatchEvent{batch, newResolved}
}

batch = db.NewBatch()
newResolved = spanz.NewHashMap[model.Ts]()
startToCollectBatch = time.Now()
for {
batchEvent, ok := doBatching()
if !ok {
return
}
batchCh <- batchEvent
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should an empty batchEvent be ignored?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I check whether batchEvent is empty in L395, because whether the batchEvent is empty, we have to update resolved ts.

}
}

Expand Down
Loading