Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
49 changes: 38 additions & 11 deletions coordinator/changefeed/backoff.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -120,13 +119,29 @@ 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 err := findFastFailError(status.Err); err != nil {
Comment thread
3AceShowHand marked this conversation as resolved.
Resolved
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
}

if m.checkpointTs < status.CheckpointTs {
m.checkpointTs = status.CheckpointTs
if m.retrying.Load() {
Expand Down Expand Up @@ -182,16 +197,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]
Comment thread
3AceShowHand marked this conversation as resolved.
Resolved
Expand Down Expand Up @@ -225,3 +239,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
}
35 changes: 35 additions & 0 deletions coordinator/changefeed/backoff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())
Expand Down
8 changes: 7 additions & 1 deletion coordinator/changefeed/changefeed.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.CheckStatus(newStatus)
if changed || err != nil || state == config.StateFailed {
return changed, state, err
}
Comment thread
3AceShowHand marked this conversation as resolved.
Resolved

if old.BootstrapDone != newStatus.BootstrapDone {
log.Info("Received changefeed status with bootstrapDone",
zap.Stringer("changefeed", c.ID),
Expand All @@ -186,7 +192,7 @@ func (c *Changefeed) UpdateStatus(newStatus *heartbeatpb.MaintainerStatus) (bool
return true, config.StateFinished, nil
}

return c.backoff.CheckStatus(newStatus)
return changed, state, err
}

return false, config.StateNormal, nil
Expand Down
29 changes: 29 additions & 0 deletions coordinator/changefeed/changefeed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -90,6 +91,34 @@ 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_IsMQSink(t *testing.T) {
cfID := common.NewChangeFeedIDWithName("test", common.DefaultKeyspaceName)
info := &config.ChangeFeedInfo{
Expand Down
Loading