From e2922452d1feed9baf1bacbb7240480d4409adb7 Mon Sep 17 00:00:00 2001 From: nexustar Date: Thu, 6 Jan 2022 16:40:37 +0800 Subject: [PATCH 1/6] cluster: support configure grafana in server_configs (#1703) --- components/dm/spec/topology_dm.go | 10 ++- pkg/cluster/ansible/import_test.go | 1 + pkg/cluster/ansible/test-data/meta.yaml | 1 + pkg/cluster/spec/grafana.go | 37 +++++++++- pkg/cluster/spec/grafana_test.go | 91 +++++++++++++++++++++++++ pkg/cluster/spec/spec.go | 7 ++ pkg/cluster/spec/spec_manager_test.go | 3 + 7 files changed, 147 insertions(+), 3 deletions(-) diff --git a/components/dm/spec/topology_dm.go b/components/dm/spec/topology_dm.go index 6870fe12fd..1b06f4c11f 100644 --- a/components/dm/spec/topology_dm.go +++ b/components/dm/spec/topology_dm.go @@ -89,8 +89,9 @@ type ( type ( // DMServerConfigs represents the server runtime configuration DMServerConfigs struct { - Master map[string]interface{} `yaml:"master"` - Worker map[string]interface{} `yaml:"worker"` + Master map[string]interface{} `yaml:"master"` + Worker map[string]interface{} `yaml:"worker"` + Grafana map[string]string `yaml:"grafana"` } // Specification represents the specification of topology.yaml @@ -838,3 +839,8 @@ func getPort(v reflect.Value) string { } return "" } + +// GetGrafanaConfig returns global grafana configurations +func (s *Specification) GetGrafanaConfig() map[string]string { + return s.ServerConfigs.Grafana +} diff --git a/pkg/cluster/ansible/import_test.go b/pkg/cluster/ansible/import_test.go index f02a2b4bb6..e06c24da32 100644 --- a/pkg/cluster/ansible/import_test.go +++ b/pkg/cluster/ansible/import_test.go @@ -129,6 +129,7 @@ server_configs: pump: {} drainer: {} cdc: {} + grafana: {} tidb_servers: [] tikv_servers: [] tiflash_servers: [] diff --git a/pkg/cluster/ansible/test-data/meta.yaml b/pkg/cluster/ansible/test-data/meta.yaml index 2239b5b4d3..d179ff869e 100644 --- a/pkg/cluster/ansible/test-data/meta.yaml +++ b/pkg/cluster/ansible/test-data/meta.yaml @@ -24,6 +24,7 @@ topology: pump: {} drainer: {} cdc: {} + grafana: {} tidb_servers: - host: 172.16.1.218 ssh_port: 9999 diff --git a/pkg/cluster/spec/grafana.go b/pkg/cluster/spec/grafana.go index 285ddc44a7..6158dd0eb6 100644 --- a/pkg/cluster/spec/grafana.go +++ b/pkg/cluster/spec/grafana.go @@ -17,6 +17,7 @@ import ( "context" "crypto/tls" "fmt" + "os" "path/filepath" "reflect" "strings" @@ -27,6 +28,7 @@ import ( "github.com/pingcap/tiup/pkg/cluster/template/config" "github.com/pingcap/tiup/pkg/cluster/template/scripts" "github.com/pingcap/tiup/pkg/meta" + "gopkg.in/ini.v1" ) // GrafanaSpec represents the Grafana topology specification in topology.yaml @@ -38,6 +40,7 @@ type GrafanaSpec struct { IgnoreExporter bool `yaml:"ignore_exporter,omitempty"` Port int `yaml:"port" default:"3000"` DeployDir string `yaml:"deploy_dir,omitempty"` + Config map[string]string `yaml:"config,omitempty" validate:"config:ignore"` ResourceControl meta.ResourceControl `yaml:"resource_control,omitempty" validate:"resource_control:editable"` Arch string `yaml:"arch,omitempty"` OS string `yaml:"os,omitempty"` @@ -181,11 +184,22 @@ func (i *GrafanaInstance) InitConfig( return err } + userConfig := i.topo.GetGrafanaConfig() + if userConfig == nil { + userConfig = make(map[string]string) + } + for k, v := range spec.Config { + userConfig[k] = v + } + err := mergeAdditionalGrafanaConf(fp, userConfig) + if err != nil { + return err + } + dst = filepath.Join(paths.Deploy, "conf", "grafana.ini") if err := e.Transfer(ctx, fp, dst, false, 0, false); err != nil { return err } - if err := i.installDashboards(ctx, e, paths.Deploy, clusterName, clusterVersion); err != nil { return errors.Annotate(err, "install dashboards") } @@ -330,3 +344,24 @@ func (i *GrafanaInstance) ScaleConfig( i.topo = topo.Merge(i.topo) return i.InitConfig(ctx, e, clusterName, clusterVersion, deployUser, paths) } + +func mergeAdditionalGrafanaConf(source string, addition map[string]string) error { + bytes, err := os.ReadFile(source) + if err != nil { + return err + } + result, err := ini.Load(bytes) + if err != nil { + return err + } + for k, v := range addition { + // convert "log.file.level to [log.file] level" + for i := len(k) - 1; i >= 0; i-- { + if k[i] == '.' { + result.Section(k[:i]).Key(k[i+1:]).SetValue(v) + break + } + } + } + return result.SaveTo(source) +} diff --git a/pkg/cluster/spec/grafana_test.go b/pkg/cluster/spec/grafana_test.go index 437c54a58a..66c677035c 100644 --- a/pkg/cluster/spec/grafana_test.go +++ b/pkg/cluster/spec/grafana_test.go @@ -15,6 +15,7 @@ package spec import ( "context" + "fmt" "os" "os/user" "path" @@ -67,3 +68,93 @@ func TestLocalDashboards(t *testing.T) { assert.FileExists(t, path.Join(deployDir, "dashboards", f.Name())) } } + +func TestMergeAdditionalGrafanaConf(t *testing.T) { + file, err := os.CreateTemp("", "tiup-cluster-spec-test") + if err != nil { + panic(fmt.Sprintf("create temp file: %s", err)) + } + defer os.Remove(file.Name()) + + _, err = file.WriteString(`#################################### SMTP / Emailing ########################## +[smtp] +;enabled = false +;host = localhost:25 +;user = +;password = +;cert_file = +;key_file = +;skip_verify = false +;from_address = admin@grafana.localhost + +[emails] +;welcome_email_on_sign_up = false + +#################################### Logging ########################## +[log] +# Either "console", "file", "syslog". Default is console and file +# Use space to separate multiple modes, e.g. "console file" +mode = file + +# Either "trace", "debug", "info", "warn", "error", "critical", default is "info" +;level = info +# For "console" mode only +[log.console] +;level = + +# log line format, valid options are text, console and json +;format = console + +# For "file" mode only +[log.file] +level = info +`) + assert.Nil(t, err) + + expected := `# ################################### SMTP / Emailing ########################## +[smtp] +enabled = true + +; enabled = false +; host = localhost:25 +; user = +; password = +; cert_file = +; key_file = +; skip_verify = false +; from_address = admin@grafana.localhost +[emails] + +; welcome_email_on_sign_up = false +# ################################### Logging ########################## +[log] +# Either "console", "file", "syslog". Default is console and file +# Use space to separate multiple modes, e.g. "console file" +mode = file + +# Either "trace", "debug", "info", "warn", "error", "critical", default is "info" +; level = info +# For "console" mode only +[log.console] + +; level = +# log line format, valid options are text, console and json +; format = console +# For "file" mode only +[log.file] +level = warning + +` + + addition := map[string]string{ + "log.file.level": "warning", + "smtp.enabled": "true", + } + + err = mergeAdditionalGrafanaConf(file.Name(), addition) + assert.Nil(t, err) + result, err := os.ReadFile(file.Name()) + assert.Nil(t, err) + + assert.Equal(t, expected, string(result)) +} diff --git a/pkg/cluster/spec/spec.go b/pkg/cluster/spec/spec.go index eb7e653321..985e8c3887 100644 --- a/pkg/cluster/spec/spec.go +++ b/pkg/cluster/spec/spec.go @@ -101,6 +101,7 @@ type ( Pump map[string]interface{} `yaml:"pump"` Drainer map[string]interface{} `yaml:"drainer"` CDC map[string]interface{} `yaml:"cdc"` + Grafana map[string]string `yaml:"grafana"` } // Specification represents the specification of topology.yaml @@ -153,6 +154,7 @@ type Topology interface { TLSConfig(dir string) (*tls.Config, error) Merge(that Topology) Topology FillHostArch(hostArchmap map[string]string) error + GetGrafanaConfig() map[string]string ScaleOutTopology } @@ -856,3 +858,8 @@ func setHostArch(field reflect.Value, hostArch map[string]string) error { return nil } + +// GetGrafanaConfig returns global grafana configurations +func (s *Specification) GetGrafanaConfig() map[string]string { + return s.ServerConfigs.Grafana +} diff --git a/pkg/cluster/spec/spec_manager_test.go b/pkg/cluster/spec/spec_manager_test.go index fcb52fe0ad..f9f48cb392 100644 --- a/pkg/cluster/spec/spec_manager_test.go +++ b/pkg/cluster/spec/spec_manager_test.go @@ -116,6 +116,9 @@ func (t *TestTopology) CountDir(host string, dir string) int { return 0 } +func (t *TestTopology) GetGrafanaConfig() map[string]string { + return nil +} func TestSpec(t *testing.T) { dir, err := os.MkdirTemp("", "test-*") assert.Nil(t, err) From e97056edffa90d824105dfed76e2e1fbb51eb50f Mon Sep 17 00:00:00 2001 From: nexustar Date: Thu, 6 Jan 2022 17:12:39 +0800 Subject: [PATCH 2/6] cluster: ngm server default enabled during deployment (#1699) --- embed/examples/cluster/minimal.yaml | 2 ++ embed/examples/cluster/multi-dc.yaml | 2 ++ embed/examples/cluster/topology.example.yaml | 2 ++ pkg/cluster/manager/upgrade.go | 5 +++++ pkg/cluster/spec/monitoring.go | 2 +- pkg/cluster/spec/spec.go | 7 +++++++ pkg/cluster/spec/validate.go | 5 ++--- 7 files changed, 21 insertions(+), 4 deletions(-) diff --git a/embed/examples/cluster/minimal.yaml b/embed/examples/cluster/minimal.yaml index b8c11ff335..447f7943d9 100644 --- a/embed/examples/cluster/minimal.yaml +++ b/embed/examples/cluster/minimal.yaml @@ -224,6 +224,8 @@ monitoring_servers: # ssh_port: 22 # # Prometheus Service communication port. # port: 9090 + # # ng-monitoring servive communication port + # ng_port: 12020 # # Prometheus deployment file, startup script, configuration file storage directory. # deploy_dir: "/tidb-deploy/prometheus-8249" # # Prometheus data storage directory. diff --git a/embed/examples/cluster/multi-dc.yaml b/embed/examples/cluster/multi-dc.yaml index 05013d42e6..37e8ded064 100644 --- a/embed/examples/cluster/multi-dc.yaml +++ b/embed/examples/cluster/multi-dc.yaml @@ -276,6 +276,8 @@ monitoring_servers: # ssh_port: 22 # # Prometheus Service communication port. # port: 9090 + # # ng-monitoring servive communication port + # ng_port: 12020 # # Prometheus deployment file, startup script, configuration file storage directory. # deploy_dir: "/tidb-deploy/prometheus-8249" # # Prometheus data storage directory. diff --git a/embed/examples/cluster/topology.example.yaml b/embed/examples/cluster/topology.example.yaml index b2cf3d301c..bebc7f7487 100644 --- a/embed/examples/cluster/topology.example.yaml +++ b/embed/examples/cluster/topology.example.yaml @@ -286,6 +286,8 @@ monitoring_servers: # ssh_port: 22 # # Prometheus Service communication port. # port: 9090 + # # ng-monitoring servive communication port + # ng_port: 12020 # # Prometheus deployment file, startup script, configuration file storage directory. # deploy_dir: "/tidb-deploy/prometheus-8249" # # Prometheus data storage directory. diff --git a/pkg/cluster/manager/upgrade.go b/pkg/cluster/manager/upgrade.go index a6e259c29d..f3f4de863d 100644 --- a/pkg/cluster/manager/upgrade.go +++ b/pkg/cluster/manager/upgrade.go @@ -53,6 +53,11 @@ func (m *Manager) Upgrade(name string, clusterVersion string, opt operator.Optio topo := metadata.GetTopology() base := metadata.GetBaseMeta() + // Adjust topo by new version + if clusterTopo, ok := topo.(*spec.Specification); ok { + clusterTopo.AdjustByVersion(clusterVersion) + } + var ( downloadCompTasks []task.Task // tasks which are used to download components copyCompTasks []task.Task // tasks which are used to copy components to remote host diff --git a/pkg/cluster/spec/monitoring.go b/pkg/cluster/spec/monitoring.go index dcde45027b..da01b25c44 100644 --- a/pkg/cluster/spec/monitoring.go +++ b/pkg/cluster/spec/monitoring.go @@ -41,7 +41,7 @@ type PrometheusSpec struct { Patched bool `yaml:"patched,omitempty"` IgnoreExporter bool `yaml:"ignore_exporter,omitempty"` Port int `yaml:"port" default:"9090"` - NgPort int `yaml:"ng_port,omitempty" validate:"ng_port:editable"` + NgPort int `yaml:"ng_port,omitempty" validate:"ng_port:editable"` // ng_port is usable since v5.3.0 and default as 12020 since v5.4.0, so the default value is set in spec.go/AdjustByVersion DeployDir string `yaml:"deploy_dir,omitempty"` DataDir string `yaml:"data_dir,omitempty"` LogDir string `yaml:"log_dir,omitempty"` diff --git a/pkg/cluster/spec/spec.go b/pkg/cluster/spec/spec.go index 985e8c3887..4337efe188 100644 --- a/pkg/cluster/spec/spec.go +++ b/pkg/cluster/spec/spec.go @@ -403,6 +403,13 @@ func (s *Specification) AdjustByVersion(clusterVersion string) { server.DataDir = "" } } + if semver.Compare(clusterVersion, "v5.4.0") >= 0 { + for _, m := range s.Monitors { + if m.NgPort == 0 { + m.NgPort = 12020 + } + } + } } // GetDashboardAddress returns the cluster's dashboard addr diff --git a/pkg/cluster/spec/validate.go b/pkg/cluster/spec/validate.go index e751903366..4c289d4674 100644 --- a/pkg/cluster/spec/validate.go +++ b/pkg/cluster/spec/validate.go @@ -568,9 +568,8 @@ func (s *Specification) portInvalidDetect() error { for i := 0; i < compSpec.NumField(); i++ { if strings.HasSuffix(compSpec.Type().Field(i).Name, "Port") { port := int(compSpec.Field(i).Int()) - portTags := strings.Split(compSpec.Type().Field(i).Tag.Get("yaml"), ",") - // when use not specify ng_port, its default value is 0 - if port == 0 && len(portTags) > 1 && portTags[1] == "omitempty" { + // for NgPort, 0 means default and -1 means disable + if compSpec.Type().Field(i).Name == "NgPort" && (port == -1 || port == 0) { continue } if port < 1 || port > 65535 { From b98df00f09967fc5acfcedb7e9c61a74c2cff673 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dani=C3=ABl=20van=20Eeden?= Date: Thu, 6 Jan 2022 11:08:38 +0100 Subject: [PATCH 3/6] mirror: Fix typos and naming (#1705) --- doc/user/mirrors.md | 87 +++++++++++++++------------------- pkg/repository/clone_mirror.go | 18 +++---- 2 files changed, 47 insertions(+), 58 deletions(-) diff --git a/doc/user/mirrors.md b/doc/user/mirrors.md index 3a3ebc3081..3d5a43b9c2 100644 --- a/doc/user/mirrors.md +++ b/doc/user/mirrors.md @@ -1,55 +1,44 @@ # Build a private mirror -When building a private cloud, it is common to use an isolated network environment where the official mirror of TiUP is not accessible, so we provide a solution for building a private mirror, which is mainly implemented by the mirros component, which can also be used for offline deployment. +When building a private cloud, it is common to use an isolated network environment where the official mirror of TiUP is not accessible, so we provide a solution for building a private mirror, which is mainly implemented by the mirror component, which can also be used for offline deployment. ## Mirrors component introduction -First, let's look at the `mirrors' help file. +First, let's look at the `mirror' help file. ```bash -$ tiup mirrors --help -Starting component `mirrors`: /Users/joshua/.tiup/components/mirrors/v0.0.1/mirrors -Build a local mirrors and download all selected components +$ tiup mirror --help +The 'mirror' command is used to manage a component repository for TiUP, you can use +it to create a private repository, or to add new component to an existing repository. +The repository can be used either online or offline. +It also provides some useful utilities to help managing keys, users and versions +of components or the repository itself. Usage: - tiup mirrors [global-version] [flags] - -Examples: - tiup mirrors local-path --arch amd64,arm --os linux,darwin # Specify the architectures and OSs - tiup mirrors local-path --full # Build a full local mirrors - tiup mirrors local-path --tikv v4 # Specify the version via prefix - tiup mirrors local-path --tidb all --pd all # Download all version for specific component - -Flags: - --overwrite Overwrite the exists tarball - -f, --full Build a full mirrors repository - -a, --arch strings Specify the downloading architecture (default [amd64]) - -o, --os strings Specify the downloading os (default [linux,darwin]) - --tidb strings Specify the versions for component tidb - --tikv strings Specify the versions for component tikv - --pd strings Specify the versions for component pd - --playground strings Specify the versions for component playground - --client strings Specify the versions for component client - --prometheus strings Specify the versions for component prometheus - --package strings Specify the versions for component package - --grafana strings Specify the versions for component grafana - --alertmanager strings Specify the versions for component alertmanager - --blackbox_exporter strings Specify the versions for component blackbox_exporter - --node_exporter strings Specify the versions for component node_exporter - --pushgateway strings Specify the versions for component pushgateway - --tiflash strings Specify the versions for component tiflash - --drainer strings Specify the versions for component drainer - --pump strings Specify the versions for component pump - --cluster strings Specify the versions for component cluster - --mirrors strings Specify the versions for component mirrors - --bench strings Specify the versions for component bench - --insight strings Specify the versions for component insight - --doc strings Specify the versions for component doc - --ctl strings Specify the versions for component ctl - -h, --help help for tiup + tiup mirror [flags] + +Available Commands: + init Initialize an empty repository + sign Add signatures to a manifest file + genkey Generate a new key pair + clone Clone a local mirror from remote mirror and download all selected components + merge Merge two or more offline mirror + publish Publish a component + show Show mirror address + set Set mirror address + modify Modify published component + renew Renew the manifest of a published component. + grant grant a new owner + rotate Rotate root.json + +Global Flags: + --help Help for this command + --skip-version-check Skip the strict version check, by default a version must be a valid SemVer string + +Use "tiup mirror [command] --help" for more information about a command. ``` -Its basic use is `tiup mirrors [global-version] [flags]`, the target-dir is the directory in which the cloned data needs to be placed. global-version is used to quickly set a common version for all components. +Its basic use is `tiup mirror clone [global-version] [flags]`, the target-dir is the directory in which the cloned data needs to be placed. global-version is used to quickly set a common version for all components. Then this order has very scary dozens of flags and even more later. But there is no need to be intimidated by the number of these flags, which are in fact of four types. @@ -68,16 +57,16 @@ If `--full` is specified, the official image will be cloned intact. ### 3. Platform limitation If you only want to clone packages for a particular platform, you can use `-os` and `-arch` to qualify: -- `tiup mirros ---os=linux` -- Just want to clone amd64 architecture: `tiup mirros --arch=amd64` -- Just want to clone linux/amd64: `tiup mirros --os=linux --arch=amd64` +- `tiup mirror clone ---os=linux` +- Just want to clone amd64 architecture: `tiup mirror clone --arch=amd64` +- Just want to clone linux/amd64: `tiup mirror clone --os=linux --arch=amd64` ### 4. Component version limited If you want to clone only one version of a component and not all versions, use `--=` to qualify, for example " -- Just want to clone the v4 version of tidb: `tiup mirrors --tidb v4` -- Just want to clone the v4 version of tidb, and all versions of tikv: `tiup mirros --tidb v4 --tikv all` -- Clone specific versions of all components that start a cluster: `tiup mirrors v4.0.0-rc` +- Just want to clone the v4 version of tidb: `tiup mirror clone --tidb v4` +- Just want to clone the v4 version of tidb, and all versions of tikv: `tiup mirror clone --tidb v4 --tikv all` +- Clone specific versions of all components that start a cluster: `tiup mirror clone v4.0.0-rc` ## The real thing @@ -86,7 +75,7 @@ If you want to clone only one version of a component and not all versions, use ` For example, if we want to install a v4.0.0-rc TiDB cluster in an isolated environment, we can execute the following command on a machine connected to the extranet to pull the required components: ```bash -tiup mirrors package --os=linux v4.0.0-rc +tiup mirror package --os=linux v4.0.0-rc ``` This command creates a directory called `package` in the current directory that contains the package of components necessary to start a cluster, which is then packaged by the tar command and sent to a central control unit in an isolated environment: @@ -110,7 +99,7 @@ export TIUP_MIRRORS=/path/to/mirror tiup cluster xxx ``` -`/path/to/mirror` is the location of in `tiup mirrors `, or if in /tmp/package: +`/path/to/mirror` is the location of in `tiup mirror clone `, or if in /tmp/package: ```bash export TIUP_MIRRORS=/tmp/package ``` diff --git a/pkg/repository/clone_mirror.go b/pkg/repository/clone_mirror.go index 975f95a7ac..9965c8be2f 100644 --- a/pkg/repository/clone_mirror.go +++ b/pkg/repository/clone_mirror.go @@ -82,15 +82,15 @@ func CloneMirror(repo *V1Repository, } var ( - initTime = time.Now() - expirsAt = initTime.Add(50 * 365 * 24 * time.Hour) - root = v1manifest.NewRoot(initTime) - index = v1manifest.NewIndex(initTime) + initTime = time.Now() + expiresAt = initTime.Add(50 * 365 * 24 * time.Hour) + root = v1manifest.NewRoot(initTime) + index = v1manifest.NewIndex(initTime) ) // All offline expires at 50 years to prevent manifests stale - root.SetExpiresAt(expirsAt) - index.SetExpiresAt(expirsAt) + root.SetExpiresAt(expiresAt) + index.SetExpiresAt(expiresAt) keys := map[string][]*v1manifest.KeyInfo{} for _, ty := range []string{ @@ -148,7 +148,7 @@ func CloneMirror(repo *V1Repository, } snapshot := v1manifest.NewSnapshot(initTime) - snapshot.SetExpiresAt(expirsAt) + snapshot.SetExpiresAt(expiresAt) componentManifests, err := cloneComponents(repo, components, selectedVersions, tidbClusterVersionMapper, targetDir, tmpDir, options) if err != nil { @@ -156,7 +156,7 @@ func CloneMirror(repo *V1Repository, } for name, component := range componentManifests { - component.SetExpiresAt(expirsAt) + component.SetExpiresAt(expiresAt) fname := fmt.Sprintf("%s.json", name) // TODO: support external owner signedManifests[component.ID], err = v1manifest.SignManifest(component, ownerkeyInfo) @@ -179,7 +179,7 @@ func CloneMirror(repo *V1Repository, // Initialize timestamp timestamp := v1manifest.NewTimestamp(initTime) - timestamp.SetExpiresAt(expirsAt) + timestamp.SetExpiresAt(expiresAt) manifests[v1manifest.ManifestTypeTimestamp] = timestamp manifests[v1manifest.ManifestTypeSnapshot] = snapshot From ad8844432970b0f77c33b93a9cd69ac929d3471e Mon Sep 17 00:00:00 2001 From: Allen Zhong Date: Thu, 6 Jan 2022 18:38:37 +0800 Subject: [PATCH 4/6] cluster/deploy: adjust hint to add --init (#1710) --- pkg/cluster/manager/deploy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/cluster/manager/deploy.go b/pkg/cluster/manager/deploy.go index bf63ad08ea..02580d712d 100644 --- a/pkg/cluster/manager/deploy.go +++ b/pkg/cluster/manager/deploy.go @@ -411,7 +411,7 @@ func (m *Manager) Deploy( return err } - hint := color.New(color.Bold).Sprintf("%s start %s", tui.OsArgs0(), name) + hint := color.New(color.Bold).Sprintf("%s start %s --init", tui.OsArgs0(), name) m.logger.Infof("Cluster `%s` deployed successfully, you can start it with command: `%s`", name, hint) return nil } From e7e5689c4755ae52610c5dc9d936bd07da1e142c Mon Sep 17 00:00:00 2001 From: nexustar Date: Thu, 6 Jan 2022 19:08:39 +0800 Subject: [PATCH 5/6] cluster: use initial_commit_ts in drainer config file instead of start script (#1706) --- embed/templates/scripts/run_drainer.sh.tpl | 3 +-- pkg/cluster/ansible/service.go | 3 ++- pkg/cluster/spec/drainer.go | 4 +--- pkg/cluster/spec/spec.go | 18 ++++++++++++++++++ pkg/cluster/template/scripts/drainer.go | 8 -------- 5 files changed, 22 insertions(+), 14 deletions(-) diff --git a/embed/templates/scripts/run_drainer.sh.tpl b/embed/templates/scripts/run_drainer.sh.tpl index ce8a76bafa..fcc1fb4012 100644 --- a/embed/templates/scripts/run_drainer.sh.tpl +++ b/embed/templates/scripts/run_drainer.sh.tpl @@ -29,5 +29,4 @@ exec bin/drainer \ --pd-urls="{{template "PDList" .Endpoints}}" \ --data-dir="{{.DataDir}}" \ --log-file="{{.LogDir}}/drainer.log" \ - --config=conf/drainer.toml \ - --initial-commit-ts={{.CommitTs}} 2>> "{{.LogDir}}/drainer_stderr.log" + --config=conf/drainer.toml 2>> "{{.LogDir}}/drainer_stderr.log" diff --git a/pkg/cluster/ansible/service.go b/pkg/cluster/ansible/service.go index 7b98161516..b93b4a46a8 100644 --- a/pkg/cluster/ansible/service.go +++ b/pkg/cluster/ansible/service.go @@ -196,7 +196,8 @@ func parseDirs(ctx context.Context, user string, ins spec.InstanceSpec, sshTimeo if strings.Contains(line, "--initial-commit-ts=") { tsArg := strings.Split(line, " ")[4] // 4 whitespaces ahead tmpTs, _ := strconv.Atoi(strings.TrimPrefix(tsArg, "--initial-commit-ts=")) - newIns.CommitTS = int64(tmpTs) + newIns.Config = make(map[string]interface{}) + newIns.Config["initial_commit_ts"] = int64(tmpTs) } } return newIns, nil diff --git a/pkg/cluster/spec/drainer.go b/pkg/cluster/spec/drainer.go index 6066648108..80676d46c4 100644 --- a/pkg/cluster/spec/drainer.go +++ b/pkg/cluster/spec/drainer.go @@ -38,7 +38,7 @@ type DrainerSpec struct { DeployDir string `yaml:"deploy_dir,omitempty"` DataDir string `yaml:"data_dir,omitempty"` LogDir string `yaml:"log_dir,omitempty"` - CommitTS int64 `yaml:"commit_ts" default:"-1" validate:"commit_ts:editable"` + CommitTS *int64 `yaml:"commit_ts,omitempty" validate:"commit_ts:editable"` // do not use it anymore, exist for compatibility Offline bool `yaml:"offline,omitempty"` NumaNode string `yaml:"numa_node,omitempty" validate:"numa_node:editable"` Config map[string]interface{} `yaml:"config,omitempty" validate:"config:ignore"` @@ -168,8 +168,6 @@ func (i *DrainerInstance) InitConfig( paths.Log, ).WithPort(spec.Port).WithNumaNode(spec.NumaNode).AppendEndpoints(topo.Endpoints(deployUser)...) - cfg.WithCommitTs(spec.CommitTS) - fp := filepath.Join(paths.Cache, fmt.Sprintf("run_drainer_%s_%d.sh", i.GetHost(), i.GetPort())) if err := cfg.ConfigToFile(fp); err != nil { diff --git a/pkg/cluster/spec/spec.go b/pkg/cluster/spec/spec.go index 4337efe188..f8b6370e87 100644 --- a/pkg/cluster/spec/spec.go +++ b/pkg/cluster/spec/spec.go @@ -356,6 +356,9 @@ func (s *Specification) UnmarshalYAML(unmarshal func(interface{}) error) error { } } + // --initial-commit-ts should not be recorded at run_drainer.sh #1682 + s.removeCommitTS() + return s.Validate() } @@ -866,6 +869,21 @@ func setHostArch(field reflect.Value, hostArch map[string]string) error { return nil } +// when upgrade form old tiup-cluster, replace spec.CommitTS with spec.Config["initial_commit_ts"] +func (s *Specification) removeCommitTS() { + _, ok1 := s.ServerConfigs.Drainer["initial_commit_ts"] + for _, spec := range s.Drainers { + _, ok2 := spec.Config["initial_commit_ts"] + if !ok1 && !ok2 && spec.CommitTS != nil && *spec.CommitTS != -1 { + if spec.Config == nil { + spec.Config = make(map[string]interface{}) + } + spec.Config["initial_commit_ts"] = *spec.CommitTS + } + spec.CommitTS = nil + } +} + // GetGrafanaConfig returns global grafana configurations func (s *Specification) GetGrafanaConfig() map[string]string { return s.ServerConfigs.Grafana diff --git a/pkg/cluster/template/scripts/drainer.go b/pkg/cluster/template/scripts/drainer.go index ed2831965a..c1f0c1030e 100644 --- a/pkg/cluster/template/scripts/drainer.go +++ b/pkg/cluster/template/scripts/drainer.go @@ -31,7 +31,6 @@ type DrainerScript struct { DataDir string LogDir string NumaNode string - CommitTs int64 Endpoints []*PDScript } @@ -44,7 +43,6 @@ func NewDrainerScript(nodeID, ip, deployDir, dataDir, logDir string) *DrainerScr DeployDir: deployDir, DataDir: dataDir, LogDir: logDir, - CommitTs: -1, } } @@ -60,12 +58,6 @@ func (c *DrainerScript) WithNumaNode(numa string) *DrainerScript { return c } -// WithCommitTs set CommitTs field of DrainerScript -func (c *DrainerScript) WithCommitTs(ts int64) *DrainerScript { - c.CommitTs = ts - return c -} - // AppendEndpoints add new DrainerScript to Endpoints field func (c *DrainerScript) AppendEndpoints(ends ...*PDScript) *DrainerScript { c.Endpoints = append(c.Endpoints, ends...) From 410c25c0f2daf73738db83802486ed7a1c10eb21 Mon Sep 17 00:00:00 2001 From: nexustar Date: Thu, 6 Jan 2022 19:24:37 +0800 Subject: [PATCH 6/6] playground: fix panic when boot monitor (#1712) --- components/playground/playground.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/components/playground/playground.go b/components/playground/playground.go index e334abe7f6..69bac0d2cb 100644 --- a/components/playground/playground.go +++ b/components/playground/playground.go @@ -1041,7 +1041,7 @@ func (p *Playground) bootMonitor(ctx context.Context, env *environment.Environme } p.instanceWaiter.Go(func() error { - err := p.monitor.wait() + err := monitor.wait() if err != nil && atomic.LoadInt32(&p.curSig) == 0 { fmt.Printf("Prometheus quit: %v\n", err) } else { @@ -1077,7 +1077,7 @@ func (p *Playground) bootNGMonitoring(ctx context.Context, env *environment.Envi } p.instanceWaiter.Go(func() error { - err := p.ngmonitoring.wait() + err := ngm.wait() if err != nil && atomic.LoadInt32(&p.curSig) == 0 { fmt.Printf("ng-monitoring quit: %v\n", err) } else { @@ -1146,7 +1146,7 @@ func (p *Playground) bootGrafana(ctx context.Context, env *environment.Environme } p.instanceWaiter.Go(func() error { - err := p.grafana.wait() + err := grafana.wait() if err != nil && atomic.LoadInt32(&p.curSig) == 0 { fmt.Printf("Grafana quit: %v\n", err) } else {