-
Notifications
You must be signed in to change notification settings - Fork 5.8k
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
br/cmd: implement the operator pause-gc-and-schedulers
command
#43562
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9a2f7c9
cherry-pick
YuJuncen e9bda58
make clippy happy
YuJuncen 02c58ad
fix lint
YuJuncen aee73ee
rename the command to pause-gc-and-schedulers
YuJuncen e484d5c
make bazel_prepare
YuJuncen ba5a542
Merge branch 'master' of https://github.com/pingcap/tidb into operato…
YuJuncen fca8160
address comments
YuJuncen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// Copyright 2023 PingCAP, Inc. Licensed under Apache-2.0. | ||
|
||
package main | ||
|
||
import ( | ||
"github.com/pingcap/errors" | ||
"github.com/pingcap/tidb/br/pkg/task" | ||
"github.com/pingcap/tidb/br/pkg/task/operator" | ||
"github.com/pingcap/tidb/br/pkg/utils" | ||
"github.com/pingcap/tidb/br/pkg/version/build" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
func newOpeartorCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "operator <subcommand>", | ||
Short: "utilities for operators like tidb-operator.", | ||
PersistentPreRunE: func(c *cobra.Command, args []string) error { | ||
if err := Init(c); err != nil { | ||
return errors.Trace(err) | ||
} | ||
build.LogInfo(build.BR) | ||
utils.LogEnvVariables() | ||
task.LogArguments(c) | ||
return nil | ||
}, | ||
Hidden: true, | ||
} | ||
cmd.AddCommand(newPauseGcCommand()) | ||
return cmd | ||
} | ||
|
||
func newPauseGcCommand() *cobra.Command { | ||
cmd := &cobra.Command{ | ||
Use: "pause-gc-and-schedulers", | ||
Short: "pause gc and schedulers to the ts until the program exits.", | ||
Args: cobra.NoArgs, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
cfg := operator.PauseGcConfig{} | ||
if err := cfg.ParseFromFlags(cmd.Flags()); err != nil { | ||
return err | ||
} | ||
ctx := GetDefaultContext() | ||
return operator.PauseGCAndScheduler(ctx, &cfg) | ||
}, | ||
} | ||
operator.DefineFlagsForPauseGcConfig(cmd.Flags()) | ||
return cmd | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
load("@io_bazel_rules_go//go:def.bzl", "go_library") | ||
|
||
go_library( | ||
name = "operator", | ||
srcs = [ | ||
"cmd.go", | ||
"config.go", | ||
], | ||
importpath = "github.com/pingcap/tidb/br/pkg/task/operator", | ||
visibility = ["//visibility:public"], | ||
deps = [ | ||
"//br/pkg/logutil", | ||
"//br/pkg/pdutil", | ||
"//br/pkg/task", | ||
"//br/pkg/utils", | ||
"@com_github_pingcap_log//:log", | ||
"@com_github_spf13_pflag//:pflag", | ||
"@org_golang_x_sync//errgroup", | ||
"@org_uber_go_zap//:zap", | ||
], | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright 2023 PingCAP, Inc. Licensed under Apache-2.0. | ||
|
||
package operator | ||
3pointer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"strings" | ||
"time" | ||
|
||
"github.com/pingcap/log" | ||
"github.com/pingcap/tidb/br/pkg/logutil" | ||
"github.com/pingcap/tidb/br/pkg/pdutil" | ||
"github.com/pingcap/tidb/br/pkg/task" | ||
"github.com/pingcap/tidb/br/pkg/utils" | ||
"go.uber.org/zap" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
func dialPD(ctx context.Context, cfg *task.Config) (*pdutil.PdController, error) { | ||
pdAddrs := strings.Join(cfg.PD, ",") | ||
var tc *tls.Config | ||
if cfg.TLS.IsEnabled() { | ||
var err error | ||
tc, err = cfg.TLS.ToTLSConfig() | ||
if err != nil { | ||
return nil, err | ||
} | ||
} | ||
mgr, err := pdutil.NewPdController(ctx, pdAddrs, tc, cfg.TLS.ToPDSecurityOption()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return mgr, nil | ||
} | ||
|
||
func cleanUpWith(f func(ctx context.Context)) { | ||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) | ||
3pointer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
defer cancel() | ||
f(ctx) | ||
} | ||
|
||
// PauseGCAndScheduler blocks the current goroutine and pause the GC safepoint and remove the scheduler by the config. | ||
// This function will block until the context being canceled. | ||
func PauseGCAndScheduler(ctx context.Context, cfg *PauseGcConfig) error { | ||
mgr, err := dialPD(ctx, &cfg.Config) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
eg, ectx := errgroup.WithContext(ctx) | ||
|
||
eg.Go(func() error { return pauseGCKeeper(ectx, cfg, mgr) }) | ||
eg.Go(func() error { return pauseSchedulerKeeper(ectx, mgr) }) | ||
|
||
return eg.Wait() | ||
} | ||
|
||
func pauseGCKeeper(ctx context.Context, cfg *PauseGcConfig, ctl *pdutil.PdController) error { | ||
// Note: should we remove the service safepoint as soon as this exits? | ||
sp := utils.BRServiceSafePoint{ | ||
ID: utils.MakeSafePointID(), | ||
TTL: int64(cfg.TTL.Seconds()), | ||
3pointer marked this conversation as resolved.
Show resolved
Hide resolved
|
||
BackupTS: cfg.SafePoint, | ||
} | ||
if sp.BackupTS == 0 { | ||
rts, err := ctl.GetMinResolvedTS(ctx) | ||
if err != nil { | ||
return err | ||
} | ||
log.Info("No service safepoint provided, using the minimal resolved TS.", zap.Uint64("min-resolved-ts", rts)) | ||
sp.BackupTS = rts | ||
} | ||
err := utils.StartServiceSafePointKeeper(ctx, ctl.GetPDClient(), sp) | ||
if err != nil { | ||
return err | ||
} | ||
log.Info("GC is paused.", zap.Object("safepoint", sp)) | ||
// Note: in fact we can directly return here. | ||
// But the name `keeper` implies once the function exits, | ||
// the GC should be resume, so let's block here. | ||
<-ctx.Done() | ||
return nil | ||
} | ||
|
||
func pauseSchedulerKeeper(ctx context.Context, ctl *pdutil.PdController) error { | ||
undo, err := ctl.RemoveAllPDSchedulers(ctx) | ||
if undo != nil { | ||
defer cleanUpWith(func(ctx context.Context) { | ||
if err := undo(ctx); err != nil { | ||
log.Warn("failed to restore pd scheduler.", logutil.ShortError(err)) | ||
} | ||
}) | ||
} | ||
if err != nil { | ||
return err | ||
} | ||
log.Info("Schedulers are paused.") | ||
// Wait until the context canceled. | ||
// So we can properly do the clean up work. | ||
<-ctx.Done() | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright 2023 PingCAP, Inc. Licensed under Apache-2.0. | ||
|
||
package operator | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/pingcap/tidb/br/pkg/task" | ||
"github.com/spf13/pflag" | ||
) | ||
|
||
type PauseGcConfig struct { | ||
task.Config | ||
|
||
SafePoint uint64 `json:"safepoint" yaml:"safepoint"` | ||
TTL time.Duration `json:"ttl" yaml:"ttl"` | ||
} | ||
|
||
func DefineFlagsForPauseGcConfig(f *pflag.FlagSet) { | ||
_ = f.DurationP("ttl", "i", 5*time.Minute, "The time-to-live of the safepoint.") | ||
_ = f.Uint64P("safepoint", "t", 0, "The GC safepoint to be kept.") | ||
} | ||
|
||
// ParseFromFlags fills the config via the flags. | ||
func (cfg *PauseGcConfig) ParseFromFlags(flags *pflag.FlagSet) error { | ||
if err := cfg.Config.ParseFromFlags(flags); err != nil { | ||
return err | ||
} | ||
|
||
var err error | ||
cfg.SafePoint, err = flags.GetUint64("safepoint") | ||
if err != nil { | ||
return err | ||
} | ||
cfg.TTL, err = flags.GetDuration("ttl") | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.
I think this shouldn't be public function. 🤔️