{"record":{"id":"f247fc5a4a89660b","repo":"vitessio/vitess","slug":"cannot-lookup-primary-tablet-v-for-shard-v-v-f247fc","errorCode":null,"errorMessage":"cannot lookup primary tablet %v for shard %v/%v: %w","messagePattern":"cannot lookup primary tablet (.+?) for shard (.+?)/(.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/vtctl/grpcvtctldserver/server.go","lineNumber":702,"sourceCode":"\n\tdurabilityName, err := s.ts.GetKeyspaceDurability(ctx, tablet.Keyspace)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\tlog.Info(fmt.Sprintf(\"Getting a new durability policy for %v\", durabilityName))\n\tdurability, err := policy.GetDurabilityPolicy(durabilityName)\n\tif err != nil {\n\t\treturn nil, err\n\t}\n\n\tif !shard.HasPrimary() {\n\t\terr = vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, \"no primary tablet for shard %v/%v\", tablet.Keyspace, tablet.Shard)\n\t\treturn nil, err\n\t}\n\n\tshardPrimary, err := s.ts.GetTablet(ctx, shard.PrimaryAlias)\n\tif err != nil {\n\t\terr = fmt.Errorf(\"cannot lookup primary tablet %v for shard %v/%v: %w\", topoproto.TabletAliasString(shard.PrimaryAlias), tablet.Keyspace, tablet.Shard, err)\n\t\treturn nil, err\n\t}\n\n\tif shardPrimary.Type != topodatapb.TabletType_PRIMARY {\n\t\terr = vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, \"TopologyServer has incosistent state for shard primary %v\", topoproto.TabletAliasString(shard.PrimaryAlias))\n\t\treturn nil, err\n\t}\n\n\tif shardPrimary.Keyspace != tablet.Keyspace || shardPrimary.Shard != tablet.Shard {\n\t\terr = vterrors.Errorf(vtrpcpb.Code_FAILED_PRECONDITION, \"primary %v and potential replica %v not in same keypace shard (%v/%v)\", topoproto.TabletAliasString(shard.PrimaryAlias), req.TabletAlias, tablet.Keyspace, tablet.Shard)\n\t\treturn nil, err\n\t}\n\n\t// We should clone the tablet and change its type to the expected type before checking the durability rules\n\t// Since we want to check the durability rules for the desired state and not before we make that change\n\texpectedTablet := tablet.CloneVT()\n\texpectedTablet.Type = req.DbType\n\terr = s.tmc.ChangeType(ctx, tablet.Tablet, req.DbType, policy.IsReplicaSemiSync(durability, shardPrimary.Tablet, expectedTablet))","sourceCodeStart":684,"sourceCodeEnd":720,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/vtctl/grpcvtctldserver/server.go#L684-L720","documentation":"After finding the shard record, ChangeTabletType fetches the shard's primary tablet record from the topology to validate state. This error wraps the underlying topo error when the primary alias listed in the shard record cannot be fetched, meaning the shard record points at a primary that is missing or unreachable in the topology.","triggerScenarios":"Calling ChangeTabletType on a tablet in a shard whose shard record's PrimaryAlias references a tablet that no longer exists in the topo server (deleted tablet record) or the topo read fails (etcd/ZK connectivity issue).","commonSituations":"Stale topology after a crashed reparent; manual deletion of tablet records; etcd/ZooKeeper connectivity problems; shard record left pointing at a decommissioned tablet.","solutions":["Inspect the shard record (vtctldclient GetShard) and verify the primary alias exists via GetTablet","If the primary record is gone, repair the shard record (RemoveShardPrimary or run a reparent) before retrying","Verify topo server connectivity (etcd/zk flags and health) for the vtctld process","Fix the underlying wrapped error reported in the %w cause first"],"exampleFix":null,"handlingStrategy":"retry","validationCode":"shard, err := ts.GetShard(ctx, keyspace, shardName)\nif shard.PrimaryAlias != nil {\n    if _, err := ts.GetTablet(ctx, shard.PrimaryAlias); err != nil {\n        return fmt.Errorf(\"shard primary record missing, repair topology first\")\n    }\n}","typeGuard":null,"tryCatchPattern":"_, err := client.ChangeTabletType(ctx, alias, tt)\nif err != nil && strings.Contains(err.Error(), \"cannot lookup primary tablet\") {\n    // inspect GetShard primary alias; run reparent if stale\n}","preventionTips":["Verify shard primary alias exists before changing tablet types","Run reparents through supported workflows to keep shard records consistent","Monitor topo (etcd/ZK) health and connectivity","Clean up decommissioned tablets so shard records don't dangle"],"tags":["vitess","topology","shard","primary"],"backgroundTag":"stale-shard-primary","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}