-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
go/cmd: Audit and fix context.Background() usage #15928
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -138,8 +138,8 @@ func init() { | |
srvTopoCounts = stats.NewCountersWithSingleLabel("ResilientSrvTopoServer", "Resilient srvtopo server operations", "type") | ||
} | ||
|
||
func startMysqld(uid uint32) (mysqld *mysqlctl.Mysqld, cnf *mysqlctl.Mycnf, err error) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
func startMysqld(ctx context.Context, uid uint32) (mysqld *mysqlctl.Mysqld, cnf *mysqlctl.Mycnf, err error) { | ||
ctx, cancel := context.WithTimeout(ctx, 30*time.Second) | ||
defer cancel() | ||
|
||
mycnfFile := mysqlctl.MycnfFile(uid) | ||
|
@@ -189,17 +189,20 @@ func run(cmd *cobra.Command, args []string) (err error) { | |
cmd.Flags().Set("log_dir", "$VTDATAROOT/tmp") | ||
} | ||
|
||
ctx, cancel := context.WithCancel(cmd.Context()) | ||
defer cancel() | ||
if externalTopoServer { | ||
// Open topo server based on the command line flags defined at topo/server.go | ||
// do not create cell info as it should be done by whoever sets up the external topo server | ||
ts = topo.Open() | ||
} else { | ||
// Create topo server. We use a 'memorytopo' implementation. | ||
ts = memorytopo.NewServer(context.Background(), tpb.Cells...) | ||
ts = memorytopo.NewServer(ctx, tpb.Cells...) | ||
} | ||
defer ts.Close() | ||
|
||
// attempt to load any routing rules specified by tpb | ||
if err := vtcombo.InitRoutingRules(context.Background(), ts, tpb.GetRoutingRules()); err != nil { | ||
if err := vtcombo.InitRoutingRules(ctx, ts, tpb.GetRoutingRules()); err != nil { | ||
return fmt.Errorf("Failed to load routing rules: %w", err) | ||
} | ||
|
||
|
@@ -212,17 +215,17 @@ func run(cmd *cobra.Command, args []string) (err error) { | |
) | ||
|
||
if startMysql { | ||
mysqld.Mysqld, cnf, err = startMysqld(1) | ||
mysqld.Mysqld, cnf, err = startMysqld(ctx, 1) | ||
if err != nil { | ||
return err | ||
} | ||
servenv.OnClose(func() { | ||
ctx, cancel := context.WithTimeout(cmd.Context(), mysqlctl.DefaultShutdownTimeout+10*time.Second) | ||
defer cancel() | ||
mysqld.Shutdown(ctx, cnf, true, mysqlctl.DefaultShutdownTimeout) | ||
shutdownCtx, shutdownCancel := context.WithTimeout(cmd.Context(), mysqlctl.DefaultShutdownTimeout+10*time.Second) | ||
defer shutdownCancel() | ||
mysqld.Shutdown(shutdownCtx, cnf, true, mysqlctl.DefaultShutdownTimeout) | ||
}) | ||
// We want to ensure we can write to this database | ||
mysqld.SetReadOnly(cmd.Context(), false) | ||
mysqld.SetReadOnly(ctx, false) | ||
|
||
} else { | ||
dbconfigs.GlobalDBConfigs.InitWithSocket("", env.CollationEnv()) | ||
|
@@ -241,9 +244,9 @@ func run(cmd *cobra.Command, args []string) (err error) { | |
if err != nil { | ||
// ensure we start mysql in the event we fail here | ||
if startMysql { | ||
ctx, cancel := context.WithTimeout(cmd.Context(), mysqlctl.DefaultShutdownTimeout+10*time.Second) | ||
defer cancel() | ||
mysqld.Shutdown(ctx, cnf, true, mysqlctl.DefaultShutdownTimeout) | ||
startCtx, startCancel := context.WithTimeout(ctx, mysqlctl.DefaultShutdownTimeout+10*time.Second) | ||
defer startCancel() | ||
mysqld.Shutdown(startCtx, cnf, true, mysqlctl.DefaultShutdownTimeout) | ||
} | ||
|
||
return fmt.Errorf("initTabletMapProto failed: %w", err) | ||
|
@@ -287,20 +290,21 @@ func run(cmd *cobra.Command, args []string) (err error) { | |
|
||
// Now that we have fully initialized the tablets, rebuild the keyspace graph. | ||
for _, ks := range tpb.Keyspaces { | ||
err := topotools.RebuildKeyspace(context.Background(), logutil.NewConsoleLogger(), ts, ks.GetName(), tpb.Cells, false) | ||
err := topotools.RebuildKeyspace(cmd.Context(), logutil.NewConsoleLogger(), ts, ks.GetName(), tpb.Cells, false) | ||
if err != nil { | ||
if startMysql { | ||
ctx, cancel := context.WithTimeout(context.Background(), mysqlctl.DefaultShutdownTimeout+10*time.Second) | ||
defer cancel() | ||
mysqld.Shutdown(ctx, cnf, true, mysqlctl.DefaultShutdownTimeout) | ||
shutdownCtx, shutdownCancel := context.WithTimeout(cmd.Context(), mysqlctl.DefaultShutdownTimeout+10*time.Second) | ||
defer shutdownCancel() | ||
mysqld.Shutdown(shutdownCtx, cnf, true, mysqlctl.DefaultShutdownTimeout) | ||
} | ||
|
||
return fmt.Errorf("Couldn't build srv keyspace for (%v: %v). Got error: %w", ks, tpb.Cells, err) | ||
} | ||
} | ||
|
||
// vtgate configuration and init | ||
resilientServer = srvtopo.NewResilientServer(context.Background(), ts, srvTopoCounts) | ||
|
||
resilientServer = srvtopo.NewResilientServer(ctx, ts, srvTopoCounts) | ||
|
||
tabletTypes := make([]topodatapb.TabletType, 0, 1) | ||
if len(tabletTypesToWait) != 0 { | ||
|
@@ -324,7 +328,7 @@ func run(cmd *cobra.Command, args []string) (err error) { | |
vtgate.QueryzHandler = "/debug/vtgate/queryz" | ||
|
||
// pass nil for healthcheck, it will get created | ||
vtg := vtgate.Init(context.Background(), env, nil, resilientServer, tpb.Cells[0], tabletTypes, plannerVersion) | ||
vtg := vtgate.Init(ctx, env, nil, resilientServer, tpb.Cells[0], tabletTypes, plannerVersion) | ||
|
||
// vtctld configuration and init | ||
err = vtctld.InitVtctld(env, ts) | ||
|
@@ -333,22 +337,13 @@ func run(cmd *cobra.Command, args []string) (err error) { | |
} | ||
|
||
if vschemaPersistenceDir != "" && !externalTopoServer { | ||
startVschemaWatcher(vschemaPersistenceDir, tpb.Keyspaces, ts) | ||
startVschemaWatcher(ctx, vschemaPersistenceDir, ts) | ||
} | ||
|
||
servenv.OnRun(func() { | ||
addStatusParts(vtg) | ||
}) | ||
|
||
servenv.OnTerm(func() { | ||
log.Error("Terminating") | ||
// FIXME(alainjobart): stop vtgate | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is actually fixed now that the context is cancelled that is used to setup |
||
}) | ||
servenv.OnClose(func() { | ||
// We will still use the topo server during lameduck period | ||
// to update our state, so closing it in OnClose() | ||
ts.Close() | ||
}) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Closing the topo isn't a long running operation or anything, so we can do it with a It doesn't change the order of things, since right after |
||
servenv.RunDefault() | ||
|
||
return nil | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This creates the toplevel context here and then uses it for example for creating the memory topo and other operations too.
Once this cancels, it ensures that things like the topo and watchers etc. are closed in the right order.