diff --git a/coordinator/changefeed/backoff.go b/coordinator/changefeed/backoff.go index 42fed64c68..4ad2245a57 100644 --- a/coordinator/changefeed/backoff.go +++ b/coordinator/changefeed/backoff.go @@ -17,12 +17,11 @@ import ( "time" "github.com/cenkalti/backoff/v4" - "github.com/pingcap/errors" "github.com/pingcap/log" "github.com/pingcap/ticdc/heartbeatpb" "github.com/pingcap/ticdc/pkg/common" "github.com/pingcap/ticdc/pkg/config" - cerrors "github.com/pingcap/ticdc/pkg/errors" + "github.com/pingcap/ticdc/pkg/errors" "go.uber.org/atomic" "go.uber.org/zap" ) @@ -111,6 +110,29 @@ func (m *Backoff) resetErrRetry() { m.retrying.Store(false) } +func (m *Backoff) checkFailedStatus(status *heartbeatpb.MaintainerStatus) (bool, config.FeedState, *heartbeatpb.RunningError) { + if m.failed.Load() { + return false, config.StateFailed, nil + } + + if err := findFastFailError(status.Err); err != nil { + if m.checkpointTs < status.CheckpointTs { + m.checkpointTs = status.CheckpointTs + } + log.Error("changefeed maintainer report a fast fail error", + zap.Stringer("changefeed", m.id), + zap.Uint64("checkpointTs", m.checkpointTs), + zap.String("node", err.Node), + zap.String("code", err.Code), + zap.String("state", string(config.StateFailed)), + zap.Any("error", err)) + m.failed.Store(true) + return true, config.StateFailed, err + } + + return false, config.StateNormal, nil +} + // CheckStatus checks the current status of a changefeed and determines its state transition. // It takes a MaintainerStatus which contains the current checkpoint timestamp and any errors. // Returns: @@ -120,11 +142,12 @@ func (m *Backoff) resetErrRetry() { // // The method handles several cases: // 1. If the changefeed is marked as failed, returns Failed state -// 2. If checkpoint has advanced (making progress), resets any retry attempts and returns Normal state -// 3. If there are errors and checkpoint hasn't advanced, initiates the retry mechanism +// 2. If a fast-fail error is reported, returns Failed state +// 3. If checkpoint has advanced (making progress), resets any retry attempts and returns Normal state +// 4. If there are errors and checkpoint hasn't advanced, initiates the retry mechanism func (m *Backoff) CheckStatus(status *heartbeatpb.MaintainerStatus) (bool, config.FeedState, *heartbeatpb.RunningError) { - if m.failed.Load() { - return false, config.StateFailed, nil + if changed, state, err := m.checkFailedStatus(status); state == config.StateFailed { + return changed, state, err } if m.checkpointTs < status.CheckpointTs { @@ -182,16 +205,15 @@ func (m *Backoff) StartFinished() { // ShouldFailChangefeed return true if a running error contains a changefeed not retry error. func ShouldFailChangefeed(e *heartbeatpb.RunningError) bool { - return cerrors.ShouldFailChangefeed(errors.New(e.Message + e.Code)) + if e == nil { + return false + } + return errors.ShouldFailChangefeed(errors.New(e.Message + e.Code)) } func (m *Backoff) HandleError(errs []*heartbeatpb.RunningError) (bool, *heartbeatpb.RunningError) { - // if there are a fastFail error in errs, we can just fastFail the changefeed - for _, err := range errs { - if cerrors.IsChangefeedGCFastFailErrorCode(errors.RFCErrorCode(err.Code)) || - ShouldFailChangefeed(err) { - return true, err - } + if err := findFastFailError(errs); err != nil { + return true, err } lastError := errs[len(errs)-1] @@ -225,3 +247,16 @@ func (m *Backoff) HandleError(errs []*heartbeatpb.RunningError) (bool, *heartbea // patch the last error to changefeed info return false, lastError } + +func findFastFailError(errs []*heartbeatpb.RunningError) *heartbeatpb.RunningError { + for _, err := range errs { + if err == nil { + continue + } + if errors.IsChangefeedGCFastFailErrorCode(errors.RFCErrorCode(err.Code)) || + ShouldFailChangefeed(err) { + return err + } + } + return nil +} diff --git a/coordinator/changefeed/backoff_test.go b/coordinator/changefeed/backoff_test.go index c0af7d7f0c..7fe230478a 100644 --- a/coordinator/changefeed/backoff_test.go +++ b/coordinator/changefeed/backoff_test.go @@ -147,6 +147,11 @@ func TestTableRoutingErrorsFastFail(t *testing.T) { code: string(errors.ErrTableRoutingFailed.RFCCode()), message: "table routing failed", }, + { + name: "table route conflict", + code: string(errors.ErrTableRouteConflict.RFCCode()), + message: "table route conflict", + }, } for _, tc := range tests { @@ -173,6 +178,36 @@ func TestTableRoutingErrorsFastFail(t *testing.T) { } } +func TestFastFailErrorWinsOverCheckpointProgress(t *testing.T) { + backoff := NewBackoff(common.NewChangeFeedIDWithName("test", common.DefaultKeyspaceName), time.Minute*30, 1) + require.True(t, backoff.ShouldRun()) + + changed, state, err := backoff.CheckStatus(&heartbeatpb.MaintainerStatus{ + CheckpointTs: 2, + Err: []*heartbeatpb.RunningError{ + { + Code: string(errors.ErrTableRouteConflict.RFCCode()), + Message: "table route conflict", + }, + }, + }) + + require.True(t, changed) + require.Equal(t, config.StateFailed, state) + require.NotNil(t, err) + require.Equal(t, uint64(2), backoff.checkpointTs) + require.False(t, backoff.ShouldRun()) + require.False(t, backoff.retrying.Load()) + + changed, state, err = backoff.CheckStatus(&heartbeatpb.MaintainerStatus{ + CheckpointTs: 3, + }) + require.False(t, changed) + require.Equal(t, config.StateFailed, state) + require.Nil(t, err) + require.Equal(t, uint64(2), backoff.checkpointTs) +} + func TestFailedWhenRetry(t *testing.T) { backoff := NewBackoff(common.NewChangeFeedIDWithName("test", common.DefaultKeyspaceName), time.Second*30, 1) require.True(t, backoff.ShouldRun()) diff --git a/coordinator/changefeed/changefeed.go b/coordinator/changefeed/changefeed.go index a594e45b8c..54d32e7f52 100644 --- a/coordinator/changefeed/changefeed.go +++ b/coordinator/changefeed/changefeed.go @@ -173,6 +173,12 @@ func (c *Changefeed) UpdateStatus(newStatus *heartbeatpb.MaintainerStatus) (bool if newStatus != nil && newStatus.CheckpointTs >= old.CheckpointTs { c.status.Store(newStatus) + + changed, state, err := c.backoff.checkFailedStatus(newStatus) + if state == config.StateFailed { + return changed, state, err + } + if old.BootstrapDone != newStatus.BootstrapDone { log.Info("Received changefeed status with bootstrapDone", zap.Stringer("changefeed", c.ID), diff --git a/coordinator/changefeed/changefeed_test.go b/coordinator/changefeed/changefeed_test.go index 92fe7cb0aa..3fdbb1148b 100644 --- a/coordinator/changefeed/changefeed_test.go +++ b/coordinator/changefeed/changefeed_test.go @@ -19,6 +19,7 @@ import ( "github.com/pingcap/ticdc/heartbeatpb" "github.com/pingcap/ticdc/pkg/common" "github.com/pingcap/ticdc/pkg/config" + "github.com/pingcap/ticdc/pkg/errors" "github.com/pingcap/ticdc/pkg/messaging" "github.com/pingcap/ticdc/pkg/node" "github.com/stretchr/testify/require" @@ -90,6 +91,80 @@ func TestChangefeed_UpdateStatus(t *testing.T) { require.Equal(t, newStatus, cf.GetStatus()) } +func TestChangefeed_UpdateStatusFastFailWhenBootstrapDoneChanges(t *testing.T) { + cfID := common.NewChangeFeedIDWithName("test", common.DefaultKeyspaceName) + info := &config.ChangeFeedInfo{ + SinkURI: "kafka://127.0.0.1:9092", + State: config.StateNormal, + Config: config.GetDefaultReplicaConfig(), + } + cf := NewChangefeed(cfID, info, 100, true) + + fastFailErr := &heartbeatpb.RunningError{ + Node: "node-1", + Code: string(errors.ErrTableRouteConflict.RFCCode()), + Message: "table route conflict", + } + newStatus := &heartbeatpb.MaintainerStatus{ + CheckpointTs: 200, + BootstrapDone: true, + Err: []*heartbeatpb.RunningError{fastFailErr}, + } + updated, state, err := cf.UpdateStatus(newStatus) + + require.True(t, updated) + require.Equal(t, config.StateFailed, state) + require.Same(t, fastFailErr, err) + require.Equal(t, newStatus, cf.GetStatus()) + require.False(t, cf.ShouldRun()) +} + +func TestChangefeed_UpdateStatusRetryableErrorWhenBootstrapDoneChanges(t *testing.T) { + cfID := common.NewChangeFeedIDWithName("test", common.DefaultKeyspaceName) + info := &config.ChangeFeedInfo{ + SinkURI: "kafka://127.0.0.1:9092", + State: config.StateNormal, + Config: config.GetDefaultReplicaConfig(), + } + cf := NewChangefeed(cfID, info, 100, true) + + retryableErr := &heartbeatpb.RunningError{ + Node: "node-1", + Code: "CDC:ErrChangefeedRetryable", + Message: "retryable error", + } + newStatus := &heartbeatpb.MaintainerStatus{ + CheckpointTs: 100, + BootstrapDone: true, + Err: []*heartbeatpb.RunningError{retryableErr}, + } + updated, state, err := cf.UpdateStatus(newStatus) + + require.True(t, updated) + require.Equal(t, config.StateNormal, state) + require.Nil(t, err) + require.Equal(t, newStatus, cf.GetStatus()) + require.True(t, cf.ShouldRun()) + require.False(t, cf.backoff.retrying.Load()) + require.False(t, cf.backoff.isRestarting.Load()) + require.True(t, cf.backoff.nextRetryTime.Load().IsZero()) + + nextStatus := &heartbeatpb.MaintainerStatus{ + CheckpointTs: 100, + BootstrapDone: true, + Err: []*heartbeatpb.RunningError{retryableErr}, + } + updated, state, err = cf.UpdateStatus(nextStatus) + + require.True(t, updated) + require.Equal(t, config.StateWarning, state) + require.Same(t, retryableErr, err) + require.Equal(t, nextStatus, cf.GetStatus()) + require.False(t, cf.ShouldRun()) + require.True(t, cf.backoff.retrying.Load()) + require.True(t, cf.backoff.isRestarting.Load()) +} + func TestChangefeed_IsMQSink(t *testing.T) { cfID := common.NewChangeFeedIDWithName("test", common.DefaultKeyspaceName) info := &config.ChangeFeedInfo{