Skip to content

Commit

Permalink
sinkv2(cdc): fix some races of source manager (#7996)
Browse files Browse the repository at this point in the history
Signed-off-by: qupeng <[email protected]>
  • Loading branch information
hicqu authored Jan 3, 2023
1 parent f8711bd commit aa27dd5
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
6 changes: 2 additions & 4 deletions cdc/processor/sinkmanager/redo_log_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,10 @@ func (w *redoWorker) handleTask(ctx context.Context, task *redoTask) (finalErr e
if err != nil {
return errors.Trace(err)
}
// Should always re-allocate space because EmitRowChangedEvents is asynchronous.
rows = make([]*model.RowChangedEvent, 0, 1024)
rowsSize = 0
cachedSize = 0
rows = rows[:0]
if cap(rows) > 1024 {
rows = make([]*model.RowChangedEvent, 0, 1024)
}
}
if lastTxnCommitTs > emitedCommitTs {
if err := w.redoManager.UpdateResolvedTs(
Expand Down
35 changes: 24 additions & 11 deletions cdc/processor/sourcemanager/engine/factory/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ const (
metricsCollectInterval = 15 * time.Second
)

var (
// Use singleton to be compatible with test cases.
factoryMu sync.Mutex
factory *SortEngineFactory = nil
)

// SortEngineFactory is a manager to create or drop SortEngine.
type SortEngineFactory struct {
// Read-only fields.
Expand Down Expand Up @@ -105,6 +111,10 @@ func (f *SortEngineFactory) Drop(ID model.ChangeFeedID) error {

// Close will close all created engines and release all resources.
func (f *SortEngineFactory) Close() (err error) {
factoryMu.Lock()
defer factoryMu.Unlock()
factory = nil

f.mu.Lock()
defer f.mu.Unlock()

Expand All @@ -122,18 +132,21 @@ func (f *SortEngineFactory) Close() (err error) {

// NewForPebble will create a SortEngineFactory for the pebble implementation.
func NewForPebble(dir string, memQuotaInBytes uint64, cfg *config.DBConfig) *SortEngineFactory {
manager := &SortEngineFactory{
engineType: pebbleEngine,
dir: dir,
memQuotaInBytes: memQuotaInBytes,
engines: make(map[model.ChangeFeedID]engine.SortEngine),
closed: make(chan struct{}),
pebbleConfig: cfg,
dbInitialized: atomic.NewBool(false),
factoryMu.Lock()
defer factoryMu.Unlock()
if factory == nil {
factory = &SortEngineFactory{
engineType: pebbleEngine,
dir: dir,
memQuotaInBytes: memQuotaInBytes,
engines: make(map[model.ChangeFeedID]engine.SortEngine),
closed: make(chan struct{}),
pebbleConfig: cfg,
dbInitialized: atomic.NewBool(false),
}
factory.startMetricsCollector()
}

manager.startMetricsCollector()
return manager
return factory
}

func (f *SortEngineFactory) startMetricsCollector() {
Expand Down

0 comments on commit aa27dd5

Please sign in to comment.