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

(sink/cdc): fix some bugs introduced by #8949 #9010

Merged
merged 7 commits into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
43 changes: 30 additions & 13 deletions cdc/processor/sinkmanager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package sinkmanager

import (
"context"
"math"
"sync"
"time"

Expand Down Expand Up @@ -108,8 +109,9 @@ type SinkManager struct {
redoMemQuota *memquota.MemQuota

// To control lifetime of all sub-goroutines.
managerCtx context.Context
ready chan struct{}
managerCtx context.Context
managerCancel context.CancelFunc
ready chan struct{}

// To control lifetime of sink and redo tasks.
sinkEg *errgroup.Group
Expand Down Expand Up @@ -170,10 +172,11 @@ func New(

// Run implements util.Runnable.
func (m *SinkManager) Run(ctx context.Context, warnings ...chan<- error) (err error) {
var managerCancel context.CancelFunc
m.managerCtx, managerCancel = context.WithCancel(ctx)
m.managerCtx, m.managerCancel = context.WithCancel(ctx)
m.wg.Add(1) // So `SinkManager.Close` will also wait the function.
defer func() {
managerCancel()
m.managerCancel()
m.wg.Done()
m.wg.Wait()
log.Info("Sink manager exists",
zap.String("namespace", m.changefeedID.Namespace),
Expand All @@ -193,7 +196,7 @@ func (m *SinkManager) Run(ctx context.Context, warnings ...chan<- error) (err er
if m.sinkEg == nil {
var sinkCtx context.Context
m.sinkEg, sinkCtx = errgroup.WithContext(m.managerCtx)
m.startSinkWorkers(sinkCtx, splitTxn, enableOldValue)
m.startSinkWorkers(sinkCtx, m.sinkEg, splitTxn, enableOldValue)
m.sinkEg.Go(func() error { return m.generateSinkTasks(sinkCtx) })
m.wg.Add(1)
go func() {
Expand All @@ -213,7 +216,7 @@ func (m *SinkManager) Run(ctx context.Context, warnings ...chan<- error) (err er
if m.redoDMLMgr != nil && m.redoEg == nil {
var redoCtx context.Context
m.redoEg, redoCtx = errgroup.WithContext(m.managerCtx)
m.startRedoWorkers(redoCtx, enableOldValue)
m.startRedoWorkers(redoCtx, m.redoEg, enableOldValue)
m.redoEg.Go(func() error { return m.generateRedoTasks(redoCtx) })
m.wg.Add(1)
go func() {
Expand Down Expand Up @@ -248,7 +251,7 @@ func (m *SinkManager) Run(ctx context.Context, warnings ...chan<- error) (err er

select {
case <-m.managerCtx.Done():
return errors.Trace(ctx.Err())
return errors.Trace(m.managerCtx.Err())
case err = <-gcErrors:
return errors.Trace(err)
case err = <-sinkErrors:
Expand Down Expand Up @@ -310,13 +313,26 @@ func (m *SinkManager) clearSinkFactory() {
m.sinkFactoryMu.Lock()
defer m.sinkFactoryMu.Unlock()
if m.sinkFactory != nil {
log.Info("Sink manager closing sink factory",
zap.String("namespace", m.changefeedID.Namespace),
zap.String("changefeed", m.changefeedID.ID))

// Firstly clear all table sinks so dmlsink.EventSink.WriteEvents won't be called any more.
// Then dmlsink.EventSink.Close can be closed safety.
m.tableSinks.Range(func(_ tablepb.Span, value interface{}) bool {
value.(*tableSinkWrapper).clearTableSink()
return true
})
m.sinkFactory.Close()
m.sinkFactory = nil

log.Info("Sink manager has closed sink factory",
zap.String("namespace", m.changefeedID.Namespace),
zap.String("changefeed", m.changefeedID.ID))
}
}

func (m *SinkManager) startSinkWorkers(ctx context.Context, splitTxn bool, enableOldValue bool) {
eg, ctx := errgroup.WithContext(ctx)
func (m *SinkManager) startSinkWorkers(ctx context.Context, eg *errgroup.Group, splitTxn bool, enableOldValue bool) {
for i := 0; i < sinkWorkerNum; i++ {
w := newSinkWorker(m.changefeedID, m.sourceManager,
m.sinkMemQuota, m.redoMemQuota,
Expand All @@ -326,8 +342,7 @@ func (m *SinkManager) startSinkWorkers(ctx context.Context, splitTxn bool, enabl
}
}

func (m *SinkManager) startRedoWorkers(ctx context.Context, enableOldValue bool) {
eg, ctx := errgroup.WithContext(ctx)
func (m *SinkManager) startRedoWorkers(ctx context.Context, eg *errgroup.Group, enableOldValue bool) {
for i := 0; i < redoWorkerNum; i++ {
w := newRedoWorker(m.changefeedID, m.sourceManager, m.redoMemQuota,
m.redoDMLMgr, m.eventCache, enableOldValue)
Expand Down Expand Up @@ -407,7 +422,8 @@ func (m *SinkManager) generateSinkTasks(ctx context.Context) error {
tableSinkUpperBoundTs model.Ts,
) engine.Position {
schemaTs := m.schemaStorage.ResolvedTs()
if tableSinkUpperBoundTs > schemaTs+1 {
if schemaTs != math.MaxUint64 && tableSinkUpperBoundTs > schemaTs+1 {
// schemaTs == math.MaxUint64 means it's in tests.
tableSinkUpperBoundTs = schemaTs + 1
}
return engine.Position{StartTs: tableSinkUpperBoundTs - 1, CommitTs: tableSinkUpperBoundTs}
Expand Down Expand Up @@ -954,6 +970,7 @@ func (m *SinkManager) Close() {
zap.String("changefeed", m.changefeedID.ID))

start := time.Now()
m.managerCancel()
m.wg.Wait()
m.sinkMemQuota.Close()
m.redoMemQuota.Close()
Expand Down
42 changes: 42 additions & 0 deletions cdc/processor/sinkmanager/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@ package sinkmanager

import (
"context"
"math"
"testing"
"time"

"github.com/pingcap/failpoint"
"github.com/pingcap/log"
"github.com/pingcap/tiflow/cdc/model"
"github.com/pingcap/tiflow/cdc/processor/sourcemanager/engine"
"github.com/pingcap/tiflow/cdc/processor/tablepb"
Expand Down Expand Up @@ -314,3 +317,42 @@ func TestUpdateReceivedSorterResolvedTsOfNonExistTable(t *testing.T) {

manager.UpdateReceivedSorterResolvedTs(spanz.TableIDToComparableSpan(1), 1)
}

// Sink worker errors should cancel the sink manager correctly.
func TestSinkManagerRunWithErrors(t *testing.T) {
t.Parallel()

ctx, cancel := context.WithCancel(context.Background())
errCh := make(chan error, 16)
changefeedInfo := getChangefeedInfo()
manager, source, _ := CreateManagerWithMemEngine(t, ctx, model.DefaultChangeFeedID("1"), changefeedInfo, errCh)
defer func() {
cancel()
manager.Close()
}()

_ = failpoint.Enable("github.com/pingcap/tiflow/cdc/processor/sinkmanager/SinkWorkerTaskError", "return")
defer func() {
_ = failpoint.Disable("github.com/pingcap/tiflow/cdc/processor/sinkmanager/SinkWorkerTaskError")
}()

span := spanz.TableIDToComparableSpan(1)

source.AddTable(span, "test", 100)
manager.AddTable(span, 100, math.MaxUint64)
manager.StartTable(span, 100)
source.Add(span, model.NewResolvedPolymorphicEvent(0, 101))
manager.UpdateReceivedSorterResolvedTs(span, 101)
manager.UpdateBarrierTs(101, nil)

timer := time.NewTimer(5 * time.Second)
select {
case <-errCh:
if !timer.Stop() {
<-timer.C
}
return
case <-timer.C:
log.Panic("must get an error instead of a timeout")
}
}
9 changes: 3 additions & 6 deletions cdc/processor/sinkmanager/redo_log_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ func (w *redoWorker) handleTask(ctx context.Context, task *redoTask) (finalErr e
zap.Any("lowerBound", lowerBound),
zap.Any("upperBound", upperBound),
zap.Any("lastPos", advancer.lastPos),
zap.Float64("lag", time.Since(
oracle.GetTimeFromTS(advancer.lastPos.CommitTs)).Seconds()))
zap.Float64("lag", time.Since(oracle.GetTimeFromTS(advancer.lastPos.CommitTs)).Seconds()),
zap.Error(finalErr))

if finalErr == nil {
// Otherwise we can't ensure all events before `lastPos` are emitted.
Expand Down Expand Up @@ -181,8 +181,5 @@ func (w *redoWorker) handleTask(ctx context.Context, task *redoTask) (finalErr e

// Even if task is canceled we still call this again, to avoid something
// are left and leak forever.
return advancer.advance(
ctx,
cachedSize,
)
return advancer.advance(ctx, cachedSize)
}
7 changes: 7 additions & 0 deletions cdc/processor/sinkmanager/table_sink_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ package sinkmanager
import (
"context"
"sync/atomic"
"time"

"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/log"
"github.com/pingcap/tiflow/cdc/model"
"github.com/pingcap/tiflow/cdc/processor/memquota"
"github.com/pingcap/tiflow/cdc/processor/sourcemanager"
"github.com/pingcap/tiflow/cdc/processor/sourcemanager/engine"
"github.com/pingcap/tiflow/cdc/sink/tablesink"
"github.com/prometheus/client_golang/prometheus"
"github.com/tikv/client-go/v2/oracle"
"go.uber.org/zap"
)

Expand Down Expand Up @@ -104,6 +107,9 @@ func (w *sinkWorker) handleTasks(ctx context.Context, taskChan <-chan *sinkTask)
return ctx.Err()
case task := <-taskChan:
err := w.handleTask(ctx, task)
failpoint.Inject("SinkWorkerTaskError", func() {
err = errors.New("SinkWorkerTaskError")
})
if err != nil {
return err
}
Expand Down Expand Up @@ -169,6 +175,7 @@ func (w *sinkWorker) handleTask(ctx context.Context, task *sinkTask) (finalErr e
zap.Any("upperBound", upperBound),
zap.Bool("splitTxn", w.splitTxn),
zap.Any("lastPos", advancer.lastPos),
zap.Float64("lag", time.Since(oracle.GetTimeFromTS(advancer.lastPos.CommitTs)).Seconds()),
zap.Error(finalErr))

// Otherwise we can't ensure all events before `lastPos` are emitted.
Expand Down
2 changes: 1 addition & 1 deletion cdc/sink/dmlsink/event_sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type EventSink[E TableEvent] interface {
// WriteEvents writes events to the sink.
// This is an asynchronously and thread-safe method.
WriteEvents(events ...*CallbackableEvent[E]) error
// Close closes the sink.
// Close closes the sink. Should never be called with `WriteEvents` concurrently.
Close()
// The EventSink meets internal errors and has been dead already.
Dead() <-chan struct{}
Expand Down