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

Augment VTOrc to also store the shard records and use it to better judge Primary recoveries #13587

Merged
merged 15 commits into from
Jul 27, 2023
Merged
Show file tree
Hide file tree
Changes from 9 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
66 changes: 65 additions & 1 deletion go/test/endtoend/vtorc/primaryfailure/primary_failure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,70 @@ func TestDownPrimaryBeforeVTOrc(t *testing.T) {
utils.WaitForSuccessfulRecoveryCount(t, vtOrcProcess, logic.RecoverDeadPrimaryRecoveryName, 1)
}

// delete the primary record and let vtorc repair.
func TestDeletedPrimaryTablet(t *testing.T) {
defer utils.PrintVTOrcLogsOnFailure(t, clusterInfo.ClusterInstance)
defer cluster.PanicHandler(t)
utils.SetupVttabletsAndVTOrcs(t, clusterInfo, 2, 1, []string{"--remote_operation_timeout=10s"}, cluster.VTOrcConfiguration{}, 1, "none")
keyspace := &clusterInfo.ClusterInstance.Keyspaces[0]
shard0 := &keyspace.Shards[0]
// find primary from topo
curPrimary := utils.ShardPrimaryTablet(t, clusterInfo, keyspace, shard0)
assert.NotNil(t, curPrimary, "should have elected a primary")
vtOrcProcess := clusterInfo.ClusterInstance.VTOrcProcesses[0]
utils.WaitForSuccessfulRecoveryCount(t, vtOrcProcess, logic.ElectNewPrimaryRecoveryName, 1)

// find the replica and rdonly tablets
var replica, rdonly *cluster.Vttablet
for _, tablet := range shard0.Vttablets {
// we know we have only two replcia tablets, so the one not the primary must be the other replica
if tablet.Alias != curPrimary.Alias && tablet.Type == "replica" {
replica = tablet
}
if tablet.Type == "rdonly" {
rdonly = tablet
}
}
assert.NotNil(t, replica, "could not find replica tablet")
assert.NotNil(t, rdonly, "could not find rdonly tablet")

// check that the replication is setup correctly before we failover
utils.CheckReplication(t, clusterInfo, curPrimary, []*cluster.Vttablet{replica, rdonly}, 10*time.Second)

// Disable VTOrc recoveries
vtOrcProcess.DisableGlobalRecoveries(t)
// use vtctlclient to stop replication on the replica
_, err := clusterInfo.ClusterInstance.VtctldClientProcess.ExecuteCommandWithOutput("StopReplication", replica.Alias)
require.NoError(t, err)
// insert a write that is not available on the replica.
utils.VerifyWritesSucceed(t, clusterInfo, curPrimary, []*cluster.Vttablet{rdonly}, 10*time.Second)

// Make the current primary vttablet unavailable and delete its tablet record.
_ = curPrimary.VttabletProcess.TearDown()
err = curPrimary.MysqlctlProcess.Stop()
require.NoError(t, err)
// use vtctlclient to start replication on the replica back
_, err = clusterInfo.ClusterInstance.VtctldClientProcess.ExecuteCommandWithOutput("StartReplication", replica.Alias)
require.NoError(t, err)
err = clusterInfo.ClusterInstance.VtctldClientProcess.ExecuteCommand("DeleteTablets", "--allow-primary", curPrimary.Alias)
require.NoError(t, err)
// Enable VTOrc recoveries now
vtOrcProcess.EnableGlobalRecoveries(t)

defer func() {
// we remove the tablet from our global list
utils.PermanentlyRemoveVttablet(clusterInfo, curPrimary)
}()

// check that the replica gets promoted. Also verify that it has all the writes.
utils.CheckPrimaryTablet(t, clusterInfo, replica, true)
utils.CheckTabletUptoDate(t, clusterInfo, replica)

// also check that the replication is working correctly after failover
utils.VerifyWritesSucceed(t, clusterInfo, replica, []*cluster.Vttablet{rdonly}, 10*time.Second)
utils.WaitForSuccessfulRecoveryCount(t, vtOrcProcess, logic.RecoverPrimaryTabletDeletedRecoveryName, 1)
}

// TestDeadPrimaryRecoversImmediately test Vtorc ability to recover immediately if primary is dead.
// Reason is, unlike other recoveries, in DeadPrimary we don't call DiscoverInstance since we know
// that primary is unreachable. This help us save few seconds depending on value of `RemoteOperationTimeout` flag.
Expand Down Expand Up @@ -217,7 +281,7 @@ func TestDeadPrimaryRecoversImmediately(t *testing.T) {
// log prefix printed at the end of analysis where we conclude we have DeadPrimary
t1 := extractTimeFromLog(t, logFile, "Proceeding with DeadPrimary recovery")
// log prefix printed at the end of recovery
t2 := extractTimeFromLog(t, logFile, "auditType:recover-dead-primary")
t2 := extractTimeFromLog(t, logFile, "auditType:RecoverDeadPrimary")
curr := time.Now().Format("2006-01-02")
timeLayout := "2006-01-02 15:04:05.000000"
timeStr1 := fmt.Sprintf("%s %s", curr, t1)
Expand Down
23 changes: 17 additions & 6 deletions go/test/endtoend/vtorc/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,10 +203,8 @@ func shutdownVttablets(clusterInfo *VTOrcClusterInfo) error {
}
// Remove the tablet record for this tablet
}
err = clusterInfo.ClusterInstance.VtctlclientProcess.ExecuteCommand("DeleteTablet", vttablet.Alias)
if err != nil {
return err
}
// Ignoring error here because some tests delete tablets themselves.
_ = clusterInfo.ClusterInstance.VtctlclientProcess.ExecuteCommand("DeleteTablet", vttablet.Alias)
}
clusterInfo.ClusterInstance.Keyspaces[0].Shards[0].Vttablets = nil
return nil
Expand Down Expand Up @@ -304,6 +302,13 @@ func SetupVttabletsAndVTOrcs(t *testing.T, clusterInfo *VTOrcClusterInfo, numRep
}
out, err := clusterInfo.ClusterInstance.VtctldClientProcess.ExecuteCommandWithOutput("SetKeyspaceDurabilityPolicy", keyspaceName, fmt.Sprintf("--durability-policy=%s", durability))
require.NoError(t, err, out)
// VTOrc now uses shard record too, so we need to clear that as well for correct testing.
_, err = clusterInfo.Ts.UpdateShardFields(context.Background(), keyspaceName, shardName, func(info *topo.ShardInfo) error {
info.PrimaryTermStartTime = nil
info.PrimaryAlias = nil
return nil
})
require.NoError(t, err)

// start vtorc
StartVTOrcs(t, clusterInfo, orcExtraArgs, config, vtorcCount)
Expand Down Expand Up @@ -430,8 +435,8 @@ func CheckReplication(t *testing.T, clusterInfo *VTOrcClusterInfo, primary *clus
time.Sleep(100 * time.Millisecond)
break
}
confirmReplication(t, primary, replicas, time.Until(endTime), clusterInfo.lastUsedValue)
clusterInfo.lastUsedValue++
confirmReplication(t, primary, replicas, time.Until(endTime), clusterInfo.lastUsedValue)
validateTopology(t, clusterInfo, true, time.Until(endTime))
return
}
Expand All @@ -442,8 +447,8 @@ func CheckReplication(t *testing.T, clusterInfo *VTOrcClusterInfo, primary *clus
// Call this function only after CheckReplication has been executed once, since that function creates the table that this function uses.
func VerifyWritesSucceed(t *testing.T, clusterInfo *VTOrcClusterInfo, primary *cluster.Vttablet, replicas []*cluster.Vttablet, timeToWait time.Duration) {
t.Helper()
confirmReplication(t, primary, replicas, timeToWait, clusterInfo.lastUsedValue)
clusterInfo.lastUsedValue++
confirmReplication(t, primary, replicas, timeToWait, clusterInfo.lastUsedValue)
}

func confirmReplication(t *testing.T, primary *cluster.Vttablet, replicas []*cluster.Vttablet, timeToWait time.Duration, valueToInsert int) {
Expand Down Expand Up @@ -478,6 +483,12 @@ func confirmReplication(t *testing.T, primary *cluster.Vttablet, replicas []*clu
}
}

// CheckTabletUptoDate verifies that the tablet has all the writes so far
func CheckTabletUptoDate(t *testing.T, clusterInfo *VTOrcClusterInfo, tablet *cluster.Vttablet) {
err := checkInsertedValues(t, tablet, clusterInfo.lastUsedValue)
require.NoError(t, err)
}

func checkInsertedValues(t *testing.T, tablet *cluster.Vttablet, index int) error {
selectSQL := fmt.Sprintf("select msg from vt_ks.vt_insert_test where id=%d", index)
qr, err := RunSQL(t, selectSQL, tablet, "")
Expand Down
9 changes: 6 additions & 3 deletions go/vt/vtctl/reparentutil/emergency_reparenter.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ type EmergencyReparenter struct {
// EmergencyReparentShard operations. Options are passed by value, so it is safe
// for callers to mutate and reuse options structs for multiple calls.
type EmergencyReparentOptions struct {
NewPrimaryAlias *topodatapb.TabletAlias
IgnoreReplicas sets.Set[string]
NewPrimaryAlias *topodatapb.TabletAlias
IgnoreReplicas sets.Set[string]
// WaitAllTablets is used to specify whether ERS should wait for all the tablets to return and not proceed
// further after n-1 tablets have returned.
WaitAllTablets bool
deepthi marked this conversation as resolved.
Show resolved Hide resolved
WaitReplicasTimeout time.Duration
PreventCrossCellPromotion bool

Expand Down Expand Up @@ -191,7 +194,7 @@ func (erp *EmergencyReparenter) reparentShardLocked(ctx context.Context, ev *eve
}

// Stop replication on all the tablets and build their status map
stoppedReplicationSnapshot, err = stopReplicationAndBuildStatusMaps(ctx, erp.tmc, ev, tabletMap, topo.RemoteOperationTimeout, opts.IgnoreReplicas, opts.NewPrimaryAlias, opts.durability, erp.logger)
stoppedReplicationSnapshot, err = stopReplicationAndBuildStatusMaps(ctx, erp.tmc, ev, tabletMap, topo.RemoteOperationTimeout, opts.IgnoreReplicas, opts.NewPrimaryAlias, opts.durability, opts.WaitAllTablets, erp.logger)
if err != nil {
return vterrors.Wrapf(err, "failed to stop replication and build status maps: %v", err)
}
Expand Down
14 changes: 12 additions & 2 deletions go/vt/vtctl/reparentutil/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ func stopReplicationAndBuildStatusMaps(
ignoredTablets sets.Set[string],
tabletToWaitFor *topodatapb.TabletAlias,
durability Durabler,
waitForAllTablets bool,
logger logutil.Logger,
) (*replicationSnapshot, error) {
event.DispatchUpdate(ev, "stop replication on all replicas")
Expand Down Expand Up @@ -307,9 +308,18 @@ func stopReplicationAndBuildStatusMaps(
}
}

numGoRoutines := len(tabletMap) - ignoredTablets.Len()
GuptaManan100 marked this conversation as resolved.
Show resolved Hide resolved
// In general we want to wait for n-1 tablets to respond, since we know the primary tablet is down.
requiredSuccesses := numGoRoutines - 1
if waitForAllTablets {
// In the special case, where we are explicitly told to wait for all the tablets to return,
// we set the required success to all the go-routines.
requiredSuccesses = numGoRoutines
}

errgroup := concurrency.ErrorGroup{
NumGoroutines: len(tabletMap) - ignoredTablets.Len(),
NumRequiredSuccesses: len(tabletMap) - ignoredTablets.Len() - 1,
NumGoroutines: numGoRoutines,
NumRequiredSuccesses: requiredSuccesses,
NumAllowedErrors: len(tabletMap), // We set the number of allowed errors to a very high value, because we don't want to exit early
// even in case of multiple failures. We rely on the revoke function below to determine if we have more failures than we can tolerate
NumErrorsToWaitFor: numErrorsToWaitFor,
Expand Down
72 changes: 71 additions & 1 deletion go/vt/vtctl/reparentutil/replication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ func Test_stopReplicationAndBuildStatusMaps(t *testing.T) {
stopReplicasTimeout time.Duration
ignoredTablets sets.Set[string]
tabletToWaitFor *topodatapb.TabletAlias
waitForAllTablets bool
expectedStatusMap map[string]*replicationdatapb.StopReplicationStatus
expectedPrimaryStatusMap map[string]*replicationdatapb.PrimaryStatus
expectedTabletsReachable []*topodatapb.Tablet
Expand Down Expand Up @@ -372,6 +373,75 @@ func Test_stopReplicationAndBuildStatusMaps(t *testing.T) {
},
}},
shouldErr: false,
}, {
name: "success with wait for all tablets",
durability: "none",
tmc: &stopReplicationAndBuildStatusMapsTestTMClient{
stopReplicationAndGetStatusResults: map[string]*struct {
StopStatus *replicationdatapb.StopReplicationStatus
Err error
}{
"zone1-0000000100": {
StopStatus: &replicationdatapb.StopReplicationStatus{
Before: &replicationdatapb.Status{Position: "MySQL56/3E11FA47-71CA-11E1-9E33-C80AA9429100:1-5", IoState: int32(mysql.ReplicationStateRunning), SqlState: int32(mysql.ReplicationStateRunning)},
After: &replicationdatapb.Status{Position: "MySQL56/3E11FA47-71CA-11E1-9E33-C80AA9429100:1-9"},
},
},
"zone1-0000000101": {
StopStatus: &replicationdatapb.StopReplicationStatus{
Before: &replicationdatapb.Status{Position: "MySQL56/3E11FA47-71CA-11E1-9E33-C80AA9429101:1-5", IoState: int32(mysql.ReplicationStateRunning), SqlState: int32(mysql.ReplicationStateRunning)},
After: &replicationdatapb.Status{Position: "MySQL56/3E11FA47-71CA-11E1-9E33-C80AA9429101:1-9"},
},
},
},
},
tabletMap: map[string]*topo.TabletInfo{
"zone1-0000000100": {
Tablet: &topodatapb.Tablet{
Type: topodatapb.TabletType_REPLICA,
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 100,
},
},
},
"zone1-0000000101": {
Tablet: &topodatapb.Tablet{
Type: topodatapb.TabletType_REPLICA,
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 101,
},
},
},
},
ignoredTablets: sets.New[string](),
expectedStatusMap: map[string]*replicationdatapb.StopReplicationStatus{
"zone1-0000000100": {
Before: &replicationdatapb.Status{Position: "MySQL56/3E11FA47-71CA-11E1-9E33-C80AA9429100:1-5", IoState: int32(mysql.ReplicationStateRunning), SqlState: int32(mysql.ReplicationStateRunning)},
After: &replicationdatapb.Status{Position: "MySQL56/3E11FA47-71CA-11E1-9E33-C80AA9429100:1-9"},
},
"zone1-0000000101": {
Before: &replicationdatapb.Status{Position: "MySQL56/3E11FA47-71CA-11E1-9E33-C80AA9429101:1-5", IoState: int32(mysql.ReplicationStateRunning), SqlState: int32(mysql.ReplicationStateRunning)},
After: &replicationdatapb.Status{Position: "MySQL56/3E11FA47-71CA-11E1-9E33-C80AA9429101:1-9"},
},
},
expectedPrimaryStatusMap: map[string]*replicationdatapb.PrimaryStatus{},
expectedTabletsReachable: []*topodatapb.Tablet{{
Type: topodatapb.TabletType_REPLICA,
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 100,
},
}, {
Type: topodatapb.TabletType_REPLICA,
Alias: &topodatapb.TabletAlias{
Cell: "zone1",
Uid: 101,
},
}},
waitForAllTablets: true,
shouldErr: false,
},
{
name: "success - 2 rdonly failures",
Expand Down Expand Up @@ -1128,7 +1198,7 @@ func Test_stopReplicationAndBuildStatusMaps(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
durability, err := GetDurabilityPolicy(tt.durability)
require.NoError(t, err)
res, err := stopReplicationAndBuildStatusMaps(ctx, tt.tmc, &events.Reparent{}, tt.tabletMap, tt.stopReplicasTimeout, tt.ignoredTablets, tt.tabletToWaitFor, durability, logger)
res, err := stopReplicationAndBuildStatusMaps(ctx, tt.tmc, &events.Reparent{}, tt.tabletMap, tt.stopReplicasTimeout, tt.ignoredTablets, tt.tabletToWaitFor, durability, tt.waitForAllTablets, logger)
if tt.shouldErr {
assert.Error(t, err)
return
Expand Down
11 changes: 11 additions & 0 deletions go/vt/vtorc/db/generate_base.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,17 @@ CREATE TABLE vitess_keyspace (
PRIMARY KEY (keyspace)
)`,
`
DROP TABLE IF EXISTS vitess_shard
`,
`
CREATE TABLE vitess_shard (
keyspace varchar(128) NOT NULL,
shard varchar(128) NOT NULL,
primary_alias varchar(512) NOT NULL,
primary_timestamp varchar(512) NOT NULL,
PRIMARY KEY (keyspace, shard)
)`,
`
CREATE INDEX source_host_port_idx_database_instance_database_instance on database_instance (source_host, source_port)
`,
`
Expand Down
25 changes: 14 additions & 11 deletions go/vt/vtorc/inst/analysis.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type StructureAnalysisCode string
const (
NoProblem AnalysisCode = "NoProblem"
ClusterHasNoPrimary AnalysisCode = "ClusterHasNoPrimary"
PrimaryTabletDeleted AnalysisCode = "PrimaryTabletDeleted"
InvalidPrimary AnalysisCode = "InvalidPrimary"
InvalidReplica AnalysisCode = "InvalidReplica"
DeadPrimaryWithoutReplicas AnalysisCode = "DeadPrimaryWithoutReplicas"
Expand Down Expand Up @@ -90,17 +91,19 @@ const (

// ReplicationAnalysis notes analysis on replication chain status, per instance
type ReplicationAnalysis struct {
AnalyzedInstanceHostname string
AnalyzedInstancePort int
AnalyzedInstanceAlias string
AnalyzedInstancePrimaryAlias string
TabletType topodatapb.TabletType
PrimaryTimeStamp time.Time
ClusterDetails ClusterInfo
AnalyzedInstanceDataCenter string
AnalyzedInstanceRegion string
AnalyzedKeyspace string
AnalyzedShard string
AnalyzedInstanceHostname string
AnalyzedInstancePort int
AnalyzedInstanceAlias string
AnalyzedInstancePrimaryAlias string
TabletType topodatapb.TabletType
PrimaryTimeStamp time.Time
ClusterDetails ClusterInfo
AnalyzedInstanceDataCenter string
AnalyzedInstanceRegion string
AnalyzedKeyspace string
AnalyzedShard string
// ShardPrimaryTimestamp is the primary term start time stored in the shard record.
ShardPrimaryTimestamp string
GuptaManan100 marked this conversation as resolved.
Show resolved Hide resolved
AnalyzedInstancePhysicalEnvironment string
AnalyzedInstanceBinlogCoordinates BinlogCoordinates
IsPrimary bool
Expand Down
15 changes: 14 additions & 1 deletion go/vt/vtorc/inst/analysis_dao.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ func GetReplicationAnalysis(keyspace string, shard string, hints *ReplicationAna
vitess_keyspace.keyspace AS keyspace,
vitess_keyspace.keyspace_type AS keyspace_type,
vitess_keyspace.durability_policy AS durability_policy,
vitess_shard.primary_timestamp AS shard_primary_timestamp,
primary_instance.read_only AS read_only,
MIN(primary_instance.alias) IS NULL AS is_invalid,
MIN(primary_instance.data_center) AS data_center,
Expand Down Expand Up @@ -267,6 +268,10 @@ func GetReplicationAnalysis(keyspace string, shard string, hints *ReplicationAna
JOIN vitess_keyspace ON (
vitess_tablet.keyspace = vitess_keyspace.keyspace
)
JOIN vitess_shard ON (
vitess_tablet.keyspace = vitess_shard.keyspace
AND vitess_tablet.shard = vitess_shard.shard
)
GuptaManan100 marked this conversation as resolved.
Show resolved Hide resolved
LEFT JOIN database_instance primary_instance ON (
vitess_tablet.alias = primary_instance.alias
AND vitess_tablet.hostname = primary_instance.hostname
Expand Down Expand Up @@ -326,6 +331,7 @@ func GetReplicationAnalysis(keyspace string, shard string, hints *ReplicationAna
return nil
}

a.ShardPrimaryTimestamp = m.GetString("shard_primary_timestamp")
a.IsPrimary = m.GetBool("is_primary")
countCoPrimaryReplicas := m.GetUint("count_co_primary_replicas")
a.IsCoPrimary = m.GetBool("is_co_primary") || (countCoPrimaryReplicas > 0)
Expand Down Expand Up @@ -469,10 +475,17 @@ func GetReplicationAnalysis(keyspace string, shard string, hints *ReplicationAna
a.Analysis = PrimarySemiSyncMustNotBeSet
a.Description = "Primary semi-sync must not be set"
//
} else if topo.IsReplicaType(a.TabletType) && ca.primaryAlias == "" {
} else if topo.IsReplicaType(a.TabletType) && ca.primaryAlias == "" && a.ShardPrimaryTimestamp == "" {
// ClusterHasNoPrimary should only be detected when the shard record doesn't have any primary term start time specified either.
a.Analysis = ClusterHasNoPrimary
a.Description = "Cluster has no primary"
ca.hasClusterwideAction = true
} else if topo.IsReplicaType(a.TabletType) && ca.primaryAlias == "" && a.ShardPrimaryTimestamp != "" {
// If there are no primary tablets, but the shard primary start time isn't empty, then we know
// the primary tablet was deleted.
a.Analysis = PrimaryTabletDeleted
a.Description = "Primary tablet has been deleted"
ca.hasClusterwideAction = true
} else if topo.IsReplicaType(a.TabletType) && !a.IsReadOnly {
a.Analysis = ReplicaIsWritable
a.Description = "Replica is writable"
Expand Down
Loading