{"record":{"id":"9548d6a81918dde1","repo":"vitessio/vitess","slug":"no-primary-tablet-found","errorCode":null,"errorMessage":"no primary tablet found","messagePattern":"no primary tablet found","errorType":"error_code","errorClass":"ErrNoPrimaryTablet","httpStatus":null,"severity":"error","filePath":"go/vt/vtorc/logic/tablet_discovery.go","lineNumber":65,"sourceCode":"\nvar (\n\tts               *topo.Server\n\ttmc              tmclient.TabletManagerClient\n\tclustersToWatch  []string\n\tcellsNoRecovery  []string\n\tshutdownWaitTime = 30 * time.Second\n\t// cellsNoRecoveryValidated is true once validateCellsNoRecovery has confirmed\n\t// every entry in cellsNoRecovery exists in the topology. It starts false when\n\t// the flag is set but the topo was unreachable at startup; recovery is blocked\n\t// until a subsequent refresh cycle can complete validation.\n\tcellsNoRecoveryValidated atomic.Bool\n\t// shardsToWatch is a map storing the shards for a given keyspace that need to be watched.\n\t// We store the key range for all the shards that we want to watch.\n\t// This is populated by parsing `--clusters_to_watch` flag.\n\tshardsToWatch map[string][]*topodatapb.KeyRange\n\n\t// ErrNoPrimaryTablet is a fixed error message.\n\tErrNoPrimaryTablet = errors.New(\"no primary tablet found\")\n)\n\nfunc init() {\n\tstats.NewGaugesFuncWithMultiLabels(\n\t\t\"TabletsWatchedByCell\",\n\t\t\"Number of tablets watched by cell\",\n\t\t[]string{\"Cell\"},\n\t\tgetTabletsWatchedByCellStats,\n\t)\n\tstats.NewGaugesFuncWithMultiLabels(\n\t\t\"TabletsWatchedByShard\",\n\t\t\"Number of tablets watched by keyspace/shard\",\n\t\t[]string{\"Keyspace\", \"Shard\"},\n\t\tgetTabletsWatchedByShardStats,\n\t)\n\tstats.NewGaugesFuncWithMultiLabels(\n\t\t\"EmergencyReparentShardDisabled\",\n\t\t\"Shards with EmergencyReparentShard disabled by keyspace/shard (1 = disabled)\",","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vtorc/logic/tablet_discovery.go#L47-L83","documentation":"ErrNoPrimaryTablet is the sentinel returned by shardPrimary when a shard has no elected primary tablet. It signals the shard's primary alias is empty in the topo record, so primary-dependent operations cannot proceed.","triggerScenarios":"shardPrimary lookup for a keyspace/shard whose record has no primary — during initial cluster setup, after a failed PrimaryElect, or while reparenting is in flight.","commonSituations":"Freshly created shard before InitShardPrimary; primary tablet down mid-reparenting leaving the record unset; vtorc scanning a shard concurrently with vtctl reparent operations.","solutions":["Elect a primary: vtctldclient PlannedReparentShard or EmergencyReparentShard for the keyspace/shard.","Check tablet health and replication status if the primary election failed.","Wait and retry — during an in-flight reparent the primary record is temporarily empty; vtorc retries on subsequent ticks."],"exampleFix":"// before\nprim, err := shardPrimary(ctx, keyspace, shard)\nif err != nil { return err }\n// after\nprim, err := shardPrimary(ctx, keyspace, shard)\nif errors.Is(err, ErrNoPrimaryTablet) {\n    return fmt.Errorf(\"shard %s/%s has no primary; run PlannedReparentShard first\", keyspace, shard)\n} else if err != nil { return err }","handlingStrategy":"retry","validationCode":"// before primary-dependent work\nshardInfo, err := ts.GetShard(ctx, keyspace, shard)\nif err != nil { return err }\nif shardInfo.GetPrimaryAlias() == nil {\n    return ErrNoPrimaryTablet\n}","typeGuard":"func shardHasPrimary(si *topo.ShardInfo) bool {\n    return si != nil && si.GetPrimaryAlias() != nil\n}","tryCatchPattern":"prim, err := shardPrimary(ctx, keyspace, shard)\nif errors.Is(err, ErrNoPrimaryTablet) {\n    // wait for reparent / election to finish, retry next tick\n    return nil\n} else if err != nil { return err }","preventionTips":["Ensure every shard has an elected primary (InitShardPrimary after creation).","Monitor reparent operations and retry logic around them.","Check ShardInfo primary alias before primary-dependent operations."],"tags":["vtorc","primary","shard","replication","topology"],"backgroundTag":"no-primary-elected","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}