This repository has been archived by the owner on Nov 24, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 188
poor: resume task if sync unit exits with invalid connection
error
#66
Merged
Merged
Changes from all commits
Commits
Show all changes
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
package worker | ||
|
||
import ( | ||
"strings" | ||
"sync" | ||
"time" | ||
|
||
|
@@ -154,7 +155,7 @@ func (st *SubTask) Run() { | |
st.ctx, st.cancel = context.WithCancel(context.Background()) | ||
pr := make(chan pb.ProcessResult, 1) | ||
st.wg.Add(1) | ||
go st.fetchResult(st.ctx, st.cancel, pr) | ||
go st.fetchResult(pr) | ||
go cu.Process(st.ctx, pr) | ||
|
||
st.wg.Add(1) | ||
|
@@ -163,34 +164,50 @@ func (st *SubTask) Run() { | |
|
||
// fetchResult fetches units process result | ||
// when dm-unit report an error, we need to re-Process the sub task | ||
func (st *SubTask) fetchResult(ctx context.Context, cancel context.CancelFunc, pr chan pb.ProcessResult) { | ||
func (st *SubTask) fetchResult(pr chan pb.ProcessResult) { | ||
defer st.wg.Done() | ||
|
||
retry: | ||
select { | ||
case <-ctx.Done(): | ||
case <-st.ctx.Done(): | ||
return | ||
case result := <-pr: | ||
st.setResult(&result) // save result | ||
cancel() // dm-unit finished, canceled or error occurred, always cancel processing | ||
st.cancel() // dm-unit finished, canceled or error occurred, always cancel processing | ||
|
||
if len(result.Errors) == 0 && st.Stage() == pb.Stage_Paused { | ||
return // paused by external request | ||
} | ||
|
||
var stage pb.Stage | ||
var ( | ||
cu = st.CurrUnit() | ||
stage pb.Stage | ||
) | ||
if len(result.Errors) == 0 { | ||
if result.IsCanceled { | ||
stage = pb.Stage_Stopped // canceled by user | ||
} else { | ||
stage = pb.Stage_Finished // process finished with no error | ||
} | ||
} else { | ||
/* TODO | ||
it's a poor and very rough retry feature, the main reason is that | ||
the concurrency control of the sub task module is very confusing and needs to be optimized. | ||
After improving its state transition and concurrency control, | ||
I will optimize the implementation of retry feature. | ||
*/ | ||
if st.retryErrors(result.Errors, cu) { | ||
log.Warnf("[subtask] %s (%s) retry on error %v, waiting 10 seconds!", st.cfg.Name, cu.Type(), result.Errors) | ||
st.ctx, st.cancel = context.WithCancel(context.Background()) | ||
time.Sleep(10 * time.Second) | ||
go cu.Resume(st.ctx, pr) | ||
goto retry | ||
} | ||
|
||
stage = pb.Stage_Paused // error occurred, paused | ||
} | ||
st.setStage(stage) | ||
|
||
cu := st.CurrUnit() | ||
|
||
log.Infof("[subtask] %s dm-unit %s process returned with stage %s, status %s", st.cfg.Name, cu.Type(), stage.String(), st.StatusJSON()) | ||
|
||
switch stage { | ||
|
@@ -391,7 +408,7 @@ func (st *SubTask) Resume() error { | |
st.ctx, st.cancel = context.WithCancel(context.Background()) | ||
pr := make(chan pb.ProcessResult, 1) | ||
st.wg.Add(1) | ||
go st.fetchResult(st.ctx, st.cancel, pr) | ||
go st.fetchResult(pr) | ||
go cu.Resume(st.ctx, pr) | ||
|
||
st.wg.Add(1) | ||
|
@@ -621,3 +638,20 @@ func (st *SubTask) unitTransWaitCondition() error { | |
} | ||
return nil | ||
} | ||
|
||
func (st *SubTask) retryErrors(errors []*pb.ProcessError, current unit.Unit) bool { | ||
retry := true | ||
switch current.Type() { | ||
case pb.UnitType_Sync: | ||
for _, err := range errors { | ||
if strings.Contains(err.Msg, "invalid connection") { | ||
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. should we need to close all DB connections and re-open them in sync unit? |
||
continue | ||
} | ||
retry = false | ||
} | ||
default: | ||
retry = false | ||
} | ||
|
||
return retry | ||
} |
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.
Is the
concurrency control optimization
including the following scenario?all of DM-worker's connections to downstream TiDB suddenly reset by downstream(caused by TiDB restart, network cutting etc.), then we must recover each connection in syncer and if the worker-count config of syncer is N, we will wait (N + 1) * 10 seconds at most.
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.
first of all it is concurrent, I don't understand why we need to wait (N + 1) * 10 seconds at most.
For the refactoring, more is the reconstruction of the the structure, making the moudle and funcion more reasone, of course we can increase the backoff property
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.
I mean for each subtask,
(N + 1) * 10 seconds
may be not continuous, but each DB connection must be recovered. So the worst case is(N + 1) * 10 seconds
sums up.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.
but it's real concurrent, right?
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.
it's the logic of unit, we can refine while refactoring sync unit
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.
we can refactor units one by one and then the outer state transition later.