Skip to content

Commit 9220355

Browse files
committed
initial impl adding tablet type lookup
1 parent f0738d1 commit 9220355

File tree

1 file changed

+36
-7
lines changed

1 file changed

+36
-7
lines changed

go/vt/vttablet/tabletmanager/tm_init.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,14 @@ const (
8989

9090
var (
9191
// The following flags initialize the tablet record.
92-
tabletHostname string
93-
initKeyspace string
94-
initShard string
95-
initTabletType string
96-
initDbNameOverride string
97-
skipBuildInfoTags = "/.*/"
98-
initTags flagutil.StringMapValue
92+
tabletHostname string
93+
initKeyspace string
94+
initShard string
95+
initTabletType string
96+
initTabletTypeLookup bool
97+
initDbNameOverride string
98+
skipBuildInfoTags = "/.*/"
99+
initTags flagutil.StringMapValue
99100

100101
initTimeout = 1 * time.Minute
101102
mysqlShutdownTimeout = mysqlctl.DefaultShutdownTimeout
@@ -106,6 +107,7 @@ func registerInitFlags(fs *pflag.FlagSet) {
106107
utils.SetFlagStringVar(fs, &initKeyspace, "init-keyspace", initKeyspace, "(init parameter) keyspace to use for this tablet")
107108
utils.SetFlagStringVar(fs, &initShard, "init-shard", initShard, "(init parameter) shard to use for this tablet")
108109
utils.SetFlagStringVar(fs, &initTabletType, "init-tablet-type", initTabletType, "(init parameter) tablet type to use for this tablet. Valid values are: PRIMARY, REPLICA, SPARE, and RDONLY. The default is REPLICA.")
110+
fs.BoolVar(&initTabletTypeLookup, "init-tablet-type-lookup", initTabletTypeLookup, "(optional, init parameter) if enabled, look up the tablet type from the existing topology record on restart and use that instead of init-tablet-type. This allows tablets to maintain their changed roles (e.g., RDONLY/DRAINED) across restarts. If disabled or if no topology record exists, init-tablet-type will be used.")
109111
utils.SetFlagStringVar(fs, &initDbNameOverride, "init-db-name-override", initDbNameOverride, "(init parameter) override the name of the db used by vttablet. Without this flag, the db name defaults to vt_<keyspacename>")
110112
utils.SetFlagStringVar(fs, &skipBuildInfoTags, "vttablet-skip-buildinfo-tags", skipBuildInfoTags, "comma-separated list of buildinfo tags to skip from merging with --init-tags. each tag is either an exact match or a regular expression of the form '/regexp/'.")
111113
utils.SetFlagVar(fs, &initTags, "init-tags", "(init parameter) comma separated list of key:value pairs used to tag the tablet")
@@ -373,6 +375,33 @@ func (tm *TabletManager) Start(tablet *topodatapb.Tablet, config *tabletenv.Tabl
373375
tm.DBConfigs.DBName = topoproto.TabletDbName(tablet)
374376
tm.tabletAlias = tablet.Alias
375377
tm.tmc = tmclient.NewTabletManagerClient()
378+
379+
// Check if there's an existing tablet record in topology and use it if flag is enabled
380+
if initTabletTypeLookup {
381+
ctx, cancel := context.WithTimeout(tm.BatchCtx, initTimeout)
382+
defer cancel()
383+
existingTablet, err := tm.TopoServer.GetTablet(ctx, tablet.Alias)
384+
if err != nil && !topo.IsErrType(err, topo.NoNode) {
385+
// Error other than "node doesn't exist" - return it
386+
return vterrors.Wrap(err, "failed to check for existing tablet record")
387+
}
388+
389+
// If we found an existing tablet record, use its tablet type instead of the initial one
390+
if err == nil {
391+
log.Infof("Found existing tablet record with --init-tablet-type-lookup enabled, using tablet type %v from topology instead of init-tablet-type %v",
392+
existingTablet.Type, tablet.Type)
393+
tablet.Type = existingTablet.Type
394+
// If it was a PRIMARY, preserve the start time
395+
if existingTablet.Type == topodatapb.TabletType_PRIMARY {
396+
tablet.PrimaryTermStartTime = existingTablet.PrimaryTermStartTime
397+
}
398+
} else {
399+
log.Infof("No existing tablet record found, using init-tablet-type: %v", tablet.Type)
400+
}
401+
} else {
402+
log.Infof("Using init-tablet-type %v (--init-tablet-type-lookup is not enabled)", tablet.Type)
403+
}
404+
376405
tm.tmState = newTMState(tm, tablet)
377406
tm.actionSema = semaphore.NewWeighted(1)
378407
tm._waitForGrantsComplete = make(chan struct{})

0 commit comments

Comments
 (0)