{"record":{"id":"4c0d968fc2da3bf9","repo":"vitessio/vitess","slug":"shard-v-v-has-a-different-keyrange-v-v","errorCode":null,"errorMessage":"shard %v/%v has a different KeyRange: %v != %v","messagePattern":"shard (.+?)/(.+?) has a different KeyRange: (.+?) != (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"go/vt/topo/tablet.go","lineNumber":619,"sourceCode":"\t// get the shard, possibly creating it\n\tvar si *ShardInfo\n\n\tif createShardAndKeyspace {\n\t\t// create the parent keyspace and shard if needed\n\t\tsi, err = ts.GetOrCreateShard(ctx, tablet.Keyspace, tablet.Shard)\n\t} else {\n\t\tsi, err = ts.GetShard(ctx, tablet.Keyspace, tablet.Shard)\n\t\tif IsErrType(err, NoNode) {\n\t\t\treturn errors.New(\"missing parent shard, use -parent option to create it, or CreateKeyspace / CreateShard\")\n\t\t}\n\t}\n\n\t// get the shard, checks a couple things\n\tif err != nil {\n\t\treturn fmt.Errorf(\"cannot get (or create) shard %v/%v: %v\", tablet.Keyspace, tablet.Shard, err)\n\t}\n\tif !key.KeyRangeEqual(si.KeyRange, tablet.KeyRange) {\n\t\treturn fmt.Errorf(\"shard %v/%v has a different KeyRange: %v != %v\", tablet.Keyspace, tablet.Shard, si.KeyRange, tablet.KeyRange)\n\t}\n\tif tablet.Type == topodatapb.TabletType_PRIMARY && si.HasPrimary() && !topoproto.TabletAliasEqual(si.PrimaryAlias, tablet.Alias) && !allowPrimaryOverride {\n\t\t// InitTablet is deprecated, so the flag has not been renamed\n\t\treturn fmt.Errorf(\"creating this tablet would override old primary %v in shard %v/%v, use allow_master_override flag\", topoproto.TabletAliasString(si.PrimaryAlias), tablet.Keyspace, tablet.Shard)\n\t}\n\n\tif tablet.Type == topodatapb.TabletType_PRIMARY {\n\t\t// we update primary_term_start_time even if the primary hasn't changed\n\t\t// because that means a new primary term with the same primary\n\t\ttablet.PrimaryTermStartTime = protoutil.TimeToProto(time.Now())\n\t}\n\n\terr = ts.CreateTablet(ctx, tablet)\n\tif IsErrType(err, NodeExists) && allowUpdate {\n\t\t// Try to update then\n\t\toldTablet, err := ts.GetTablet(ctx, tablet.Alias)\n\t\tif err != nil {\n\t\t\treturn fmt.Errorf(\"failed reading existing tablet %v: %v\", topoproto.TabletAliasString(tablet.Alias), err)","sourceCodeStart":601,"sourceCodeEnd":637,"githubUrl":"https://github.com/vitessio/vitess/blob/01a25a7d176f94613b8d59d799f438380a8760e4/go/vt/topo/tablet.go#L601-L637","documentation":"InitTablet verifies that the shard record's KeyRange matches the KeyRange the tablet claims. This error is thrown when si.KeyRange (stored on the shard) differs from tablet.KeyRange, meaning the tablet thinks it belongs to a range that the topo's shard record does not cover. A tablet with the wrong keyrange would receive incorrect keyrouting, so registration is refused.","triggerScenarios":"Calling InitTablet (or vttablet boot / addTablet in tests) with -keyrange that does not equal the KeyRange stored on the shard record for keyspace/shard — e.g. tablet says 80- but shard record was created with -80.","commonSituations":"Copy-pasted vttablet startup scripts where -shard and -keyrange drifted; resharding moved a tablet to a new shard but the old flags remained; manual shard creation with wrong boundaries; restoring a tablet from backup into a different shard.","solutions":["Correct the tablet's -keyrange/-shard flags to match the shard record shown in the error (`vtctldclient GetShard <keyspace>/<shard>`).","If the shard record itself is wrong, fix it (delete/recreate shard with the right KeyRange) then RebuildKeyspaceGraph, and re-run InitTablet.","After a reshard, restart the tablet with the flags of its new shard rather than reusing the old startup script.","Regenerate tablet startup configs from a template keyed on shard name to keep -shard and -keyrange in sync."],"exampleFix":"// before\nvttablet -keyspace commerce -shard \"80-\" -keyrange \"-80\" ...  // mismatch\n// after\nvttablet -keyspace commerce -shard \"80-\" -keyrange \"80-\" ...","handlingStrategy":"validation","validationCode":"// Verify the tablet flags match the shard record before InitTablet\nsi, _ := ts.GetShard(ctx, keyspace, shard)\nif !key.KeyRangeEqual(si.KeyRange, tablet.KeyRange) {\n    return fmt.Errorf(\"fix -keyrange: shard has %v, tablet claims %v\",\n        si.KeyRange, tablet.KeyRange)\n}","typeGuard":"func keyRangesMatch(a, b *topodatapb.KeyRange) bool {\n    return key.KeyRangeEqual(a, b)\n}","tryCatchPattern":"if err := topotools.InitTablet(ctx, ts, false, tablet, cp); err != nil {\n    if strings.Contains(err.Error(), \"has a different KeyRange\") {\n        return fmt.Errorf(\"tablet startup flags disagree with shard record; re-sync from shard plan: %w\", err)\n    }\n    return err\n}","preventionTips":["Generate -keyrange and -shard flags from the same source (shard plan/template).","Update tablet startup scripts after every reshard.","Verify with `vtctldclient GetTablet <alias>` after restarts.","Never copy startup scripts between shards without editing the keyrange."],"tags":["topo","tablet","keyrange","shard"],"backgroundTag":"shard-keyrange-mismatch","analyzedSha":"01a25a7d176f94613b8d59d799f438380a8760e4","analyzedAt":"2026-09-01T17:28:30.605Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}