Skip to content
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

Merged
merged 6 commits into from
Feb 16, 2021
68 changes: 55 additions & 13 deletions go/vt/vtctl/reparentutil/emergency_reparenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package reparentutil
import (
"context"
"fmt"
"sync"
"time"

"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -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.
Copy link
Member

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 by WaitReplicasTimeout which guarantees that allReplicasDoneCancel is eventually called.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Precisely!

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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rephrase this sentence? I had difficulty parsing it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, let me take a shot at editing that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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!

Copy link
Member

Choose a reason for hiding this comment

The 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")
Expand All @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 handleReplica is ever updated to record multiple errors per replica.

Copy link
Contributor

Choose a reason for hiding this comment

The 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)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess

if len(rec.Errors) == numReplicas

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This code path implies this is a valid state of existence.

The code directly states that this is an error state and not a valid state.

If I change this to len(rec.Errors) == numReplicas then we can return success in places where we definitely shouldn't. Open to suggestions on how to make the comments more clear, though!

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This lgtm.

Copy link
Contributor

Choose a reason for hiding this comment

The 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 {
Expand Down
147 changes: 139 additions & 8 deletions go/vt/vtctl/reparentutil/emergency_reparenter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,10 @@ func TestEmergencyReparenter_reparentShardLocked(t *testing.T) {
Error: nil,
},
},
SetMasterResults: map[string]error{
"zone1-0000000100": nil,
"zone1-0000000101": nil,
},
StopReplicationAndGetStatusResults: map[string]struct {
Status *replicationdatapb.Status
StopStatus *replicationdatapb.StopReplicationStatus
Expand Down Expand Up @@ -240,6 +244,10 @@ func TestEmergencyReparenter_reparentShardLocked(t *testing.T) {
Error: nil,
},
},
SetMasterResults: map[string]error{
"zone1-0000000100": nil,
"zone1-0000000102": nil,
},
StopReplicationAndGetStatusResults: map[string]struct {
Status *replicationdatapb.Status
StopStatus *replicationdatapb.StopReplicationStatus
Expand Down Expand Up @@ -1003,7 +1011,7 @@ func TestEmergencyReparenter_promoteNewPrimary(t *testing.T) {
},
},
},
"zone1-00000000102": {
"zone1-0000000102": {
Tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Expand All @@ -1012,7 +1020,7 @@ func TestEmergencyReparenter_promoteNewPrimary(t *testing.T) {
Hostname: "requires force start",
},
},
"zone1-00000000404": {
"zone1-0000000404": {
Tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Expand Down Expand Up @@ -1052,6 +1060,7 @@ func TestEmergencyReparenter_promoteNewPrimary(t *testing.T) {
"zone1-0000000100": {},
"zone1-0000000101": {},
},
statusMap: map[string]*replicationdatapb.StopReplicationStatus{},
opts: EmergencyReparentOptions{},
shouldErr: true,
},
Expand Down Expand Up @@ -1174,7 +1183,7 @@ func TestEmergencyReparenter_promoteNewPrimary(t *testing.T) {
shouldErr: true,
},
{
name: "replicas failing to SetMaster does not fail the promotion",
name: "all replicas failing to SetMaster does fail the promotion",
ts: memorytopo.NewServer("zone1"),
tmc: &emergencyReparenterTestTMClient{
PopulateReparentJournalResults: map[string]error{
Expand All @@ -1189,7 +1198,7 @@ func TestEmergencyReparenter_promoteNewPrimary(t *testing.T) {
},
},
SetMasterResults: map[string]error{
// everyone fails, who cares?!
// everyone fails, we all fail
"zone1-0000000101": assert.AnError,
"zone1-0000000102": assert.AnError,
},
Expand Down Expand Up @@ -1225,6 +1234,118 @@ func TestEmergencyReparenter_promoteNewPrimary(t *testing.T) {
},
statusMap: map[string]*replicationdatapb.StopReplicationStatus{},
opts: EmergencyReparentOptions{},
shouldErr: true,
},
{
name: "all replicas slow to SetMaster does fail the promotion",
ts: memorytopo.NewServer("zone1"),
tmc: &emergencyReparenterTestTMClient{
PopulateReparentJournalResults: map[string]error{
"zone1-0000000100": nil,
},
PromoteReplicaResults: map[string]struct {
Result string
Error error
}{
"zone1-0000000100": {
Error: nil,
},
},
SetMasterDelays: map[string]time.Duration{
// nothing is failing, we're just slow
"zone1-0000000101": time.Millisecond * 100,
"zone1-0000000102": time.Millisecond * 75,
},
SetMasterResults: map[string]error{
"zone1-0000000101": nil,
"zone1-0000000102": nil,
},
},
keyspace: "testkeyspace",
shard: "-",
newPrimaryTabletAlias: "zone1-0000000100",
tabletMap: map[string]*topo.TabletInfo{
"zone1-0000000100": {
Tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 100,
},
},
},
"zone1-0000000101": {
Tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 101,
},
},
},
"zone1-0000000102": {
Tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 102,
},
},
},
},
statusMap: map[string]*replicationdatapb.StopReplicationStatus{},
opts: EmergencyReparentOptions{
WaitReplicasTimeout: time.Millisecond * 10,
},
shouldErr: true,
},
{
name: "one replica failing to SetMaster does not fail the promotion",
ts: memorytopo.NewServer("zone1"),
tmc: &emergencyReparenterTestTMClient{
PopulateReparentJournalResults: map[string]error{
"zone1-0000000100": nil,
},
PromoteReplicaResults: map[string]struct {
Result string
Error error
}{
"zone1-0000000100": {
Error: nil,
},
},
SetMasterResults: map[string]error{
"zone1-0000000101": nil, // this one succeeds, so we're good
"zone1-0000000102": assert.AnError,
},
},
keyspace: "testkeyspace",
shard: "-",
newPrimaryTabletAlias: "zone1-0000000100",
tabletMap: map[string]*topo.TabletInfo{
"zone1-0000000100": {
Tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 100,
},
},
},
"zone1-0000000101": {
Tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 101,
},
},
},
"zone1-0000000102": {
Tablet: &topodatapb.Tablet{
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 102,
},
},
},
},
statusMap: map[string]*replicationdatapb.StopReplicationStatus{},
shouldErr: false,
},
}
Expand All @@ -1235,10 +1356,6 @@ func TestEmergencyReparenter_promoteNewPrimary(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()

if tt.ts == nil {
t.Skip("toposerver is nil, assuming we're working on other test cases")
}

ctx := context.Background()
logger := logutil.NewMemoryLogger()
ev := &events.Reparent{}
Expand Down Expand Up @@ -1535,6 +1652,8 @@ type emergencyReparenterTestTMClient struct {
Error error
}
// keyed by tablet alias.
SetMasterDelays map[string]time.Duration
// keyed by tablet alias.
SetMasterResults map[string]error
// keyed by tablet alias.
StopReplicationAndGetStatusDelays map[string]time.Duration
Expand Down Expand Up @@ -1583,6 +1702,18 @@ func (fake *emergencyReparenterTestTMClient) SetMaster(ctx context.Context, tabl
}

key := topoproto.TabletAliasString(tablet.Alias)

if fake.SetMasterDelays != nil {
if delay, ok := fake.SetMasterDelays[key]; ok {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(delay):
// proceed to results
}
}
}

if result, ok := fake.SetMasterResults[key]; ok {
return result
}
Expand Down
4 changes: 4 additions & 0 deletions go/vt/vtctl/reparentutil/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 Before at all, IMO, should be a panic case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The 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 recover defers and figure out how to recover from ... any point where the process can fail, or risk leaving the cluster in a worse state than when we started.

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
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better error than panic :) This lgtm.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks better

return false
}

return stopStatus.Before.IoThreadRunning || stopStatus.Before.SqlThreadRunning
}

Expand Down
7 changes: 7 additions & 0 deletions go/vt/vtctl/reparentutil/replication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,13 @@ func TestReplicaWasRunning(t *testing.T) {
},
expected: false,
},
{
name: "status.Before is nil means we were not running",
in: &replicationdatapb.StopReplicationStatus{
Before: nil,
},
expected: false,
},
}

for _, tt := range tests {
Expand Down