-
Notifications
You must be signed in to change notification settings - Fork 5.9k
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
disttask: refine manager retry #51071
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
|
@@ -24,10 +24,13 @@ import ( | |
"github.com/pingcap/errors" | ||
"github.com/pingcap/log" | ||
"github.com/pingcap/tidb/pkg/config" | ||
"github.com/pingcap/tidb/pkg/disttask/framework/handle" | ||
"github.com/pingcap/tidb/pkg/disttask/framework/proto" | ||
"github.com/pingcap/tidb/pkg/disttask/framework/scheduler" | ||
"github.com/pingcap/tidb/pkg/disttask/framework/storage" | ||
"github.com/pingcap/tidb/pkg/metrics" | ||
tidbutil "github.com/pingcap/tidb/pkg/util" | ||
"github.com/pingcap/tidb/pkg/util/backoff" | ||
"github.com/pingcap/tidb/pkg/util/cpu" | ||
"github.com/pingcap/tidb/pkg/util/intest" | ||
"github.com/pingcap/tidb/pkg/util/memory" | ||
|
@@ -44,8 +47,6 @@ var ( | |
MaxCheckInterval = 2 * time.Second | ||
maxChecksWhenNoSubtask = 7 | ||
recoverMetaInterval = 90 * time.Second | ||
retrySQLTimes = 30 | ||
retrySQLInterval = 500 * time.Millisecond | ||
unfinishedSubtaskStates = []proto.SubtaskState{ | ||
proto.SubtaskStatePending, | ||
proto.SubtaskStateRunning, | ||
|
@@ -108,44 +109,16 @@ func NewManager(ctx context.Context, id string, taskTable TaskTable) (*Manager, | |
// InitMeta initializes the meta of the Manager. | ||
// not a must-success step before start manager, | ||
// manager will try to recover meta periodically. | ||
func (m *Manager) InitMeta() (err error) { | ||
for i := 0; i < retrySQLTimes; i++ { | ||
err = m.taskTable.InitMeta(m.ctx, m.id, config.GetGlobalConfig().Instance.TiDBServiceScope) | ||
if err == nil { | ||
break | ||
} | ||
if err1 := m.ctx.Err(); err1 != nil { | ||
return err1 | ||
} | ||
if i%10 == 0 { | ||
m.logger.Warn("start manager failed", | ||
zap.String("scope", config.GetGlobalConfig().Instance.TiDBServiceScope), | ||
zap.Int("retry times", i), | ||
zap.Error(err)) | ||
} | ||
time.Sleep(retrySQLInterval) | ||
} | ||
return err | ||
func (m *Manager) InitMeta() error { | ||
return m.runWithRetry(func() error { | ||
return m.taskTable.InitMeta(m.ctx, m.id, config.GetGlobalConfig().Instance.TiDBServiceScope) | ||
}, "init meta failed") | ||
} | ||
|
||
func (m *Manager) recoverMeta() (err error) { | ||
for i := 0; i < retrySQLTimes; i++ { | ||
err = m.taskTable.RecoverMeta(m.ctx, m.id, config.GetGlobalConfig().Instance.TiDBServiceScope) | ||
if err == nil { | ||
break | ||
} | ||
if err1 := m.ctx.Err(); err1 != nil { | ||
return err1 | ||
} | ||
if i%10 == 0 { | ||
m.logger.Warn("recover meta failed", | ||
zap.String("scope", config.GetGlobalConfig().Instance.TiDBServiceScope), | ||
zap.Int("retry times", i), | ||
zap.Error(err)) | ||
} | ||
time.Sleep(retrySQLInterval) | ||
} | ||
return err | ||
func (m *Manager) recoverMeta() error { | ||
return m.runWithRetry(func() error { | ||
return m.taskTable.RecoverMeta(m.ctx, m.id, config.GetGlobalConfig().Instance.TiDBServiceScope) | ||
}, "recover meta failed") | ||
} | ||
|
||
// Start starts the Manager. | ||
|
@@ -311,13 +284,13 @@ func (m *Manager) startTaskExecutor(task *proto.Task) { | |
factory := GetTaskExecutorFactory(task.Type) | ||
if factory == nil { | ||
err := errors.Errorf("task type %s not found", task.Type) | ||
m.logErrAndPersist(err, task.ID, nil) | ||
m.failSubtask(err, task.ID, nil) | ||
return | ||
} | ||
executor := factory(m.ctx, m.id, task, m.taskTable) | ||
err := executor.Init(m.ctx) | ||
if err != nil { | ||
m.logErrAndPersist(err, task.ID, executor) | ||
m.failSubtask(err, task.ID, executor) | ||
return | ||
} | ||
m.addTaskExecutor(executor) | ||
|
@@ -367,17 +340,31 @@ func (m *Manager) logErr(err error) { | |
m.logger.Error("task manager met error", zap.Error(err), zap.Stack("stack")) | ||
} | ||
|
||
func (m *Manager) logErrAndPersist(err error, taskID int64, taskExecutor TaskExecutor) { | ||
func (m *Manager) failSubtask(err error, taskID int64, taskExecutor TaskExecutor) { | ||
m.logErr(err) | ||
// TODO we want to define err of taskexecutor.Init as fatal, but add-index have | ||
// some code in Init that need retry, remove it after it's decoupled. | ||
if taskExecutor != nil && taskExecutor.IsRetryableError(err) { | ||
m.logger.Error("met retryable err", zap.Error(err), zap.Stack("stack")) | ||
return | ||
} | ||
err1 := m.taskTable.FailSubtask(m.ctx, m.id, taskID, err) | ||
err1 := m.runWithRetry(func() error { | ||
return m.taskTable.FailSubtask(m.ctx, m.id, taskID, err) | ||
}, "update to subtask failed") | ||
if err1 == nil { | ||
m.logger.Error("update error to subtask success", zap.Int64("task-id", taskID), zap.Error(err1), zap.Stack("stack")) | ||
} | ||
} | ||
|
||
func (m *Manager) runWithRetry(fn func() error, msg string) error { | ||
backoffer := backoff.NewExponential(scheduler.RetrySQLInterval, 2, scheduler.RetrySQLMaxInterval) | ||
err1 := handle.RunWithRetry(m.ctx, scheduler.RetrySQLTimes, backoffer, m.logger, | ||
func(_ context.Context) (bool, error) { | ||
return true, fn() | ||
}, | ||
) | ||
if err1 != nil { | ||
m.logger.Error("update to subtask failed", zap.Error(err1), zap.Stack("stack")) | ||
m.logger.Warn(msg, zap.Error(err1)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems that the error log is printed in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I prefer to add some log to indicate where it happens |
||
} | ||
m.logger.Error("update error to subtask", zap.Int64("task-id", taskID), zap.Error(err1), zap.Stack("stack")) | ||
return err1 | ||
} |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we not put this to
RunWithRetry
? Then we can use it directly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe too many params?