-
Notifications
You must be signed in to change notification settings - Fork 2k
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
[reparentutil / ERS] confirm at least one replica succeeded to SetMaster
, or fail
#7486
Changes from 3 commits
53cdbfe
1ee9045
6d70d98
b9fe9a4
cdffd63
8f2e1fc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ package reparentutil | |
import ( | ||
"context" | ||
"fmt" | ||
"sync" | ||
"time" | ||
|
||
"k8s.io/apimachinery/pkg/util/sets" | ||
|
@@ -150,47 +151,71 @@ func (erp *EmergencyReparenter) promoteNewPrimary( | |
|
||
event.DispatchUpdate(ev, "reparenting all tablets") | ||
|
||
// (@ajm188) - A question while migrating: Is this by design? By my read, | ||
// there's nothing consuming that error channel, meaning any replica that | ||
// fails to SetMaster will actually block trying to send to the errCh. In | ||
// addition, the only way an operator will ever notice these errors will be | ||
// in the logs on the tablet, and not from any error propagation in | ||
// vtctl/wrangler, so a shard will continue to attempt to serve (probably?) | ||
// after a partially-failed ERS. | ||
// Create a context and cancel function to watch for the first successful | ||
// SetMaster call on a replica. We use a background context so that this | ||
// context is only ever Done when its cancel is called by the background | ||
// goroutine we're about to spin up. | ||
// | ||
// Similarly, create a context and cancel for the replica waiter goroutine | ||
// to signal when all replica goroutines have finished. In the case where at | ||
// least one replica succeeds, replSuccessCtx will be canceled first, while | ||
// allReplicasDoneCtx is guaranteed to be canceled within | ||
// opts.WaitReplicasTimeout plus some jitter. | ||
replSuccessCtx, replSuccessCancel := context.WithCancel(context.Background()) | ||
allReplicasDoneCtx, allReplicasDoneCancel := context.WithCancel(context.Background()) | ||
|
||
now := time.Now().UnixNano() | ||
errCh := make(chan error) | ||
replWg := sync.WaitGroup{} | ||
rec := concurrency.AllErrorRecorder{} | ||
|
||
handlePrimary := func(alias string, ti *topo.TabletInfo) error { | ||
erp.logger.Infof("populating reparent journal on new master %v", alias) | ||
return erp.tmc.PopulateReparentJournal(replCtx, ti.Tablet, now, opts.lockAction, newPrimaryTabletInfo.Alias, rp) | ||
} | ||
|
||
handleReplica := func(alias string, ti *topo.TabletInfo) { | ||
defer replWg.Done() | ||
erp.logger.Infof("setting new master on replica %v", alias) | ||
|
||
var err error | ||
defer func() { errCh <- err }() | ||
|
||
forceStart := false | ||
if status, ok := statusMap[alias]; ok { | ||
forceStart = ReplicaWasRunning(status) | ||
} | ||
|
||
err = erp.tmc.SetMaster(replCtx, ti.Tablet, newPrimaryTabletInfo.Alias, now, "", forceStart) | ||
err := erp.tmc.SetMaster(replCtx, ti.Tablet, newPrimaryTabletInfo.Alias, now, "", forceStart) | ||
if err != nil { | ||
err = vterrors.Wrapf(err, "tablet %v SetMaster failed: %v", alias, err) | ||
rec.RecordError(err) | ||
|
||
return | ||
} | ||
|
||
// Signal that at least one goroutine succeeded to SetMaster. | ||
replSuccessCancel() | ||
} | ||
|
||
numReplicas := 0 | ||
|
||
for alias, ti := range tabletMap { | ||
switch { | ||
case alias == newPrimaryTabletAlias: | ||
continue | ||
case !opts.IgnoreReplicas.Has(alias): | ||
replWg.Add(1) | ||
numReplicas++ | ||
go handleReplica(alias, ti) | ||
} | ||
} | ||
|
||
// Spin up a background goroutine to wait until all replica goroutines | ||
// finished. If one replica is slow, but another finishes quickly, the main | ||
// thread of execution in this function while this goroutine will run until | ||
// the parent context times out, without slowing down the flow of ERS. | ||
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. Can you rephrase this sentence? I had difficulty parsing it. 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. Yeah, let me take a shot at editing that. 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. Let me know if the newest version makes more sense, or if you have other suggestions! 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. Much better. |
||
go func() { | ||
replWg.Wait() | ||
allReplicasDoneCancel() | ||
}() | ||
|
||
primaryErr := handlePrimary(newPrimaryTabletAlias, newPrimaryTabletInfo) | ||
if primaryErr != nil { | ||
erp.logger.Warningf("master failed to PopulateReparentJournal") | ||
|
@@ -199,7 +224,24 @@ func (erp *EmergencyReparenter) promoteNewPrimary( | |
return vterrors.Wrapf(primaryErr, "failed to PopulateReparentJournal on master: %v", primaryErr) | ||
} | ||
|
||
return nil | ||
select { | ||
case <-replSuccessCtx.Done(): | ||
// At least one replica was able to SetMaster successfully | ||
return nil | ||
case <-allReplicasDoneCtx.Done(): | ||
if len(rec.Errors) >= numReplicas { | ||
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. Why would summed up errors exceed the number of replicas? I think if it should never be greater than this is an odd thing to write in code. 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. My thinking was that being more permissive here means the code still works if 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 think if that's the case it should get updated when that happens. This code path implies this is a valid state of existence. (I'm required as a reader to read into the comment to understand that this is simply defensive programming, and for a reality we don't live in yet) 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 guess
feels weirdly specific, but I agree with @PrismaPhonic that if we ever record more than one error from each goroutine, this will need to change anyway. So it is probably best to check for the specific condition. 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.
The code directly states that this is an error state and not a valid state. If I change this to 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. How about: switch {
case len(rec.Errors) == numReplicas:
// original error case in question
case len(rec.Errors) > numReplicas:
return vterrors.Wrapf(rec.Error(), "received more errors (= %d) than replicas (= %d), which should be impossible", len(rec.Errors), numReplicas)
default:
return nil
} 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. This lgtm. 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 still am not seeing how we would return success in cases when we shouldn't if we truly think that errors will never exceeed count of num replicas. This does seem overly defensive to me, but I do think this newer version is better. |
||
// There are certain timing issues between replSuccessCtx.Done | ||
// firing and allReplicasDoneCtx.Done firing, so we check again if | ||
// truly all replicas failed (where `numReplicas` goroutines | ||
// recorded an error) or one or more actually managed to succeed. | ||
// | ||
// Technically, rec.Errors should never be greater than numReplicas, | ||
// but it's better to err on the side of caution here. | ||
return vterrors.Wrapf(rec.Error(), "%d replica(s) failed: %v", len(rec.Errors), rec.Error()) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func (erp *EmergencyReparenter) reparentShardLocked(ctx context.Context, ev *events.Reparent, keyspace string, shard string, opts EmergencyReparentOptions) error { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -142,6 +142,10 @@ func FindValidEmergencyReparentCandidates( | |
// ReplicaWasRunning returns true if a StopReplicationStatus indicates that the | ||
// replica had running replication threads before being stopped. | ||
func ReplicaWasRunning(stopStatus *replicationdatapb.StopReplicationStatus) bool { | ||
if stopStatus.Before == nil { | ||
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'm not sure false is the right thing to return here. If before is nil we can't know if it was running or not. Not getting 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 think we shouldn't have code in the critical path of an ERS that can panic, because we'd either need to add How about: func ReplicaWasRunning(stopStatus *replicationdatapb.StopReplicationStatus) (bool, error) {
if stopStatus.Before == nil {
return false, errors.New("...")
}
return stopStatus.Before.IoThreadRunning || stopStatus.Before.SqlThreadRunning, nil
} 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. Better error than panic :) This lgtm. 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. This looks better |
||
return false | ||
} | ||
|
||
return stopStatus.Before.IoThreadRunning || stopStatus.Before.SqlThreadRunning | ||
} | ||
|
||
|
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.
Very nice. IIUC, the calls to
SetMaster
are bounded byWaitReplicasTimeout
which guarantees thatallReplicasDoneCancel
is eventually called.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.
Precisely!