{"record":{"id":"5d0fdd549962f7c0","repo":"vitessio/vitess","slug":"could-not-find-primary-tablet-v","errorCode":null,"errorMessage":"could not find primary tablet %v","messagePattern":"could not find primary tablet (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/vtorc/logic/topology_recovery.go","lineNumber":562,"sourceCode":"\t// Get all tablets in the shard\n\ttablets, err := getShardTablets(ctx, analysisEntry.AnalyzedKeyspace, analysisEntry.AnalyzedShard)\n\tif err != nil {\n\t\tlogger.Error(fmt.Sprintf(\"Error fetching tablets for keyspace/shard %v/%v: %v\", analysisEntry.AnalyzedKeyspace, analysisEntry.AnalyzedShard, err))\n\t\treturn false, topologyRecovery, err\n\t}\n\n\t// Find the primary tablet for semi-sync policy determination\n\tvar primaryTablet *topodatapb.Tablet\n\tfor _, tabletInfo := range tablets {\n\t\tif topoproto.TabletAliasEqual(tabletInfo.Alias, analysisEntry.AnalyzedInstanceAlias) {\n\t\t\tprimaryTablet = tabletInfo.Tablet\n\t\t\tbreak\n\t\t}\n\t}\n\n\tif primaryTablet == nil {\n\t\tlogger.Error(\"Could not find primary tablet \" + topoproto.TabletAliasString(analysisEntry.AnalyzedInstanceAlias))\n\t\treturn false, topologyRecovery, fmt.Errorf(\"could not find primary tablet %v\", topoproto.TabletAliasString(analysisEntry.AnalyzedInstanceAlias))\n\t}\n\n\teg, _ := errgroup.WithContext(ctx)\n\tvar restartExpected int\n\tvar restartPerformed atomic.Int64\n\t// Iterate through all tablets and find direct replicas of the primary.\n\t// We intentionally shuffle tablet order. When maxReplicas is non-zero, we want to\n\t// randomly pick which replicas to restart, to avoid biasing towards replicas.\n\tfor i, tabletIndex := range rand.Perm(len(tablets)) {\n\t\tif maxReplicas > 0 && i >= maxReplicas {\n\t\t\tbreak\n\t\t}\n\t\ttabletInfo := tablets[tabletIndex]\n\t\ttablet := tabletInfo.Tablet\n\t\ttabletAliasString := topoproto.TabletAliasString(tablet.Alias)\n\n\t\t// Skip the primary itself\n\t\tif topoproto.TabletAliasEqual(tablet.Alias, analysisEntry.AnalyzedInstanceAlias) {","sourceCodeStart":544,"sourceCodeEnd":580,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vtorc/logic/topology_recovery.go#L544-L580","documentation":"restartDirectReplicas must locate the current primary tablet record in VTOrc's DB to enumerate its direct replicas before restarting replication on them. When no matching primary tablet exists for the analyzed instance alias, recovery cannot proceed and this error is returned (after also logging it via logger.Error).","triggerScenarios":"A recovery (restartArbitraryDirectReplica / restartAllDirectReplicas) runs for an analysis entry whose AnalyzedInstanceAlias has no corresponding primary Tablet record in the vtorc DB — e.g. the tablet record was pruned, the tablet was replaced/re-aliased, or replication mid-level topology changed so no primary candidate is found.","commonSituations":"Tablet was deleted from topology while recovery was in flight; emergency repair after keyspace resharding left stale aliases; VTOrc's DB hasn't finished refreshing tablets from topo; mistake in the analyzed instance alias.","solutions":["Run a tablet refresh (vtorc refresh or wait for the discovery loop) so the tablet map includes the current primary","Verify the tablet exists with `vtctldclient GetTablets` and that its alias matches what the recovery analysis references","Re-prime VTOrc's DB: delete stale tablet rows and let Rediscovery repopulate, then re-trigger the recovery","If the primary is genuinely gone, use a different recovery action (e.g. EmergencyReparentShard) rather than replica-restart"],"exampleFix":null,"handlingStrategy":"validation","validationCode":"tablet, err := inst.ReadTablet(alias)\nif err != nil || tablet == nil {\n    return fmt.Errorf(\"primary tablet %s not present; refresh discovery before recovery\", aliasStr)\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Let the discovery loop refresh tablet data before triggering manual recoveries","Avoid removing tablets from topo while recoveries are pending","Use vtctldclient GetTablets to confirm aliases before scripted failovers"],"tags":["vtorc","replication","topology-recovery"],"backgroundTag":"primary-tablet-not-found","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T15:18:49.778Z"}