-
Notifications
You must be signed in to change notification settings - Fork 41
redo: improve redo writer #4353
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
486f86a
init
wk989898 31592de
update
wk989898 e29004d
update
wk989898 71216c4
chore
wk989898 e43775b
chore
wk989898 1d40b99
fix
wk989898 8571a24
update test
wk989898 a634bcd
Merge branch 'master' into redo-0304
wk989898 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,9 @@ import ( | |
| commonEvent "github.com/pingcap/ticdc/pkg/common/event" | ||
| "github.com/pingcap/ticdc/pkg/config" | ||
| "github.com/pingcap/ticdc/pkg/redo" | ||
| "github.com/pingcap/ticdc/pkg/redo/writer" | ||
| "github.com/pingcap/ticdc/pkg/util" | ||
| "github.com/pingcap/ticdc/utils/chann" | ||
| "github.com/stretchr/testify/require" | ||
| "golang.org/x/sync/errgroup" | ||
| ) | ||
|
|
@@ -329,3 +331,62 @@ func runBenchTest(b *testing.B, storage string, useFileBackend bool) { | |
|
|
||
| require.ErrorIs(b, eg.Wait(), context.Canceled) | ||
| } | ||
|
|
||
| type mockBatchWriter struct { | ||
|
||
| mu sync.Mutex | ||
| batchLens []int | ||
| } | ||
|
|
||
| func (m *mockBatchWriter) WriteEvents(_ context.Context, events ...writer.RedoEvent) error { | ||
| m.mu.Lock() | ||
| defer m.mu.Unlock() | ||
| m.batchLens = append(m.batchLens, len(events)) | ||
| for _, event := range events { | ||
| event.PostFlush() | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (m *mockBatchWriter) Run(ctx context.Context) error { | ||
| <-ctx.Done() | ||
| return ctx.Err() | ||
| } | ||
|
|
||
| func (m *mockBatchWriter) Close() error { | ||
| return nil | ||
| } | ||
|
|
||
| func (m *mockBatchWriter) SetTableSchemaStore(_ *commonEvent.TableSchemaStore) {} | ||
|
|
||
| func TestRedoSinkSendMessagesInBatch(t *testing.T) { | ||
| t.Parallel() | ||
|
|
||
| ctx, cancel := context.WithCancel(context.Background()) | ||
| defer cancel() | ||
|
|
||
| mockWriter := &mockBatchWriter{} | ||
| s := &Sink{ | ||
| dmlWriter: mockWriter, | ||
| logBuffer: chann.NewUnlimitedChannelDefault[writer.RedoEvent](), | ||
| } | ||
|
|
||
| doneCh := make(chan error, 1) | ||
| go func() { | ||
| doneCh <- s.sendMessages(ctx) | ||
| }() | ||
|
|
||
| totalEvents := redo.DefaultFlushBatchSize*2 + 17 | ||
| events := make([]writer.RedoEvent, 0, totalEvents) | ||
| for i := 0; i < totalEvents; i++ { | ||
| events = append(events, &commonEvent.RedoRowEvent{}) | ||
| } | ||
| s.logBuffer.Push(events...) | ||
| s.logBuffer.Close() | ||
|
|
||
| err := <-doneCh | ||
| require.NoError(t, err) | ||
|
|
||
| mockWriter.mu.Lock() | ||
| defer mockWriter.mu.Unlock() | ||
| require.Equal(t, []int{redo.DefaultFlushBatchSize, redo.DefaultFlushBatchSize, 17}, mockWriter.batchLens) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,184 @@ | ||
| // Copyright 2026 PingCAP, Inc. | ||
| // | ||
| // Licensed 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, | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package memory | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/binary" | ||
|
|
||
| "github.com/pingcap/log" | ||
| "github.com/pingcap/ticdc/pkg/common" | ||
| commonEvent "github.com/pingcap/ticdc/pkg/common/event" | ||
| "github.com/pingcap/ticdc/pkg/errors" | ||
| "github.com/pingcap/ticdc/pkg/redo" | ||
| "github.com/pingcap/ticdc/pkg/redo/codec" | ||
| "github.com/pingcap/ticdc/pkg/redo/writer" | ||
| "github.com/pingcap/ticdc/pkg/util" | ||
| "go.uber.org/atomic" | ||
| "go.uber.org/zap" | ||
| "golang.org/x/sync/errgroup" | ||
| ) | ||
|
|
||
| // polymorphicRedoEvent wraps RedoLog and callback for file worker. | ||
| type polymorphicRedoEvent struct { | ||
| commitTs common.Ts | ||
| data []byte | ||
| callback func() | ||
| } | ||
|
|
||
| func (e *polymorphicRedoEvent) PostFlush() { | ||
| if e.callback != nil { | ||
| e.callback() | ||
| } | ||
| } | ||
|
|
||
| func toPolymorphicRedoEvent( | ||
| event writer.RedoEvent, | ||
| tableSchemaStore *commonEvent.TableSchemaStore, | ||
| ) (*polymorphicRedoEvent, error) { | ||
| rl := event.ToRedoLog() | ||
| if rl == nil { | ||
| return nil, errors.ErrUnexpected.FastGenByArgs("redo event to log conversion failed") | ||
| } | ||
| if rl.Type == commonEvent.RedoLogTypeDDL { | ||
| rl.RedoDDL.SetTableSchemaStore(tableSchemaStore) | ||
| } | ||
|
|
||
| rawData, err := codec.MarshalRedoLog(rl, nil) | ||
| if err != nil { | ||
| return nil, errors.WrapError(errors.ErrMarshalFailed, err) | ||
| } | ||
| lenField, padBytes := writer.EncodeFrameSize(len(rawData)) | ||
| data := make([]byte, 8+len(rawData)+padBytes) | ||
| binary.LittleEndian.PutUint64(data[:8], lenField) | ||
| copy(data[8:], rawData) | ||
| return &polymorphicRedoEvent{ | ||
| commitTs: rl.GetCommitTs(), | ||
| callback: event.PostFlush, | ||
| data: data, | ||
| }, nil | ||
| } | ||
|
|
||
| type encodingWorkerGroup struct { | ||
| changefeed common.ChangeFeedID | ||
|
|
||
| outputCh chan *polymorphicRedoEvent | ||
| inputChs []chan writer.RedoEvent | ||
| workerNum int | ||
|
|
||
| nextWorker atomic.Uint64 | ||
| closed chan error | ||
|
|
||
| tableSchemaStore *commonEvent.TableSchemaStore | ||
| } | ||
|
|
||
| func newEncodingWorkerGroup(cfg *writer.LogWriterConfig) *encodingWorkerGroup { | ||
| workerNum := util.GetOrZero(cfg.EncodingWorkerNum) | ||
| if workerNum <= 0 { | ||
| workerNum = redo.DefaultEncodingWorkerNum | ||
| } | ||
| inputChs := make([]chan writer.RedoEvent, workerNum) | ||
| for i := 0; i < workerNum; i++ { | ||
| inputChs[i] = make(chan writer.RedoEvent, redo.DefaultEncodingInputChanSize) | ||
| } | ||
| return &encodingWorkerGroup{ | ||
| changefeed: cfg.ChangeFeedID, | ||
| inputChs: inputChs, | ||
| outputCh: make(chan *polymorphicRedoEvent, redo.DefaultEncodingOutputChanSize), | ||
| workerNum: workerNum, | ||
| closed: make(chan error, 1), | ||
| } | ||
| } | ||
|
|
||
| func (e *encodingWorkerGroup) Run(ctx context.Context) (err error) { | ||
| defer func() { | ||
| log.Warn("redo encoding workers closed", | ||
| zap.String("keyspace", e.changefeed.Keyspace()), | ||
| zap.String("changefeed", e.changefeed.Name()), | ||
| zap.Error(err)) | ||
| if err != nil { | ||
| select { | ||
| case e.closed <- err: | ||
| default: | ||
| } | ||
| } | ||
| close(e.closed) | ||
| }() | ||
| g, egCtx := errgroup.WithContext(ctx) | ||
| for i := 0; i < e.workerNum; i++ { | ||
| idx := i | ||
| g.Go(func() error { | ||
| return e.runWorker(egCtx, idx) | ||
| }) | ||
| } | ||
| log.Info("redo log encoding workers started", | ||
| zap.String("keyspace", e.changefeed.Keyspace()), | ||
| zap.String("changefeed", e.changefeed.Name()), | ||
| zap.Int("workerNum", e.workerNum)) | ||
| return g.Wait() | ||
| } | ||
|
|
||
| func (e *encodingWorkerGroup) AddEvent(ctx context.Context, event writer.RedoEvent) error { | ||
| idx := int((e.nextWorker.Inc() - 1) % uint64(e.workerNum)) | ||
| return e.input(ctx, idx, event) | ||
| } | ||
|
|
||
| func (e *encodingWorkerGroup) runWorker(egCtx context.Context, idx int) error { | ||
| for { | ||
| select { | ||
| case <-egCtx.Done(): | ||
| return errors.Trace(egCtx.Err()) | ||
| case event := <-e.inputChs[idx]: | ||
| if event == nil { | ||
| log.Warn("received nil event in redo encoding worker", | ||
| zap.String("keyspace", e.changefeed.Keyspace()), | ||
| zap.String("changefeed", e.changefeed.Name())) | ||
| continue | ||
| } | ||
| redoLogEvent, err := toPolymorphicRedoEvent(event, e.tableSchemaStore) | ||
| if err != nil { | ||
| return errors.Trace(err) | ||
| } | ||
| if err := e.output(egCtx, redoLogEvent); err != nil { | ||
| return errors.Trace(err) | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (e *encodingWorkerGroup) input( | ||
| ctx context.Context, idx int, event writer.RedoEvent, | ||
| ) error { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
wk989898 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| case err := <-e.closed: | ||
| return errors.ErrRedoWriterStopped.FastGenByArgs(err) | ||
| case e.inputChs[idx] <- event: | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| func (e *encodingWorkerGroup) output( | ||
| ctx context.Context, event *polymorphicRedoEvent, | ||
| ) error { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return ctx.Err() | ||
| case err := <-e.closed: | ||
| return errors.ErrRedoWriterStopped.FastGenByArgs(err) | ||
| case e.outputCh <- event: | ||
| return nil | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.