From 99bbe1d4c985a2e96aad2d5525d5f482c8ac090a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Qi=CE=BC=24hi=D0=AFu=C3=AD?= <39378935+srstack@users.noreply.github.com> Date: Tue, 4 Jan 2022 17:58:35 +0800 Subject: [PATCH] cluster: scale-out topology cannot inherit the global configuration (#1701) --- pkg/cluster/manager/check.go | 2 +- pkg/cluster/manager/scale_out.go | 2 +- pkg/cluster/spec/parse_topology.go | 20 +++++++++++++++++++- pkg/cluster/spec/parse_topology_test.go | 10 ++++++++++ 4 files changed, 31 insertions(+), 3 deletions(-) diff --git a/pkg/cluster/manager/check.go b/pkg/cluster/manager/check.go index 896c746d4d..ef8a3eef36 100644 --- a/pkg/cluster/manager/check.go +++ b/pkg/cluster/manager/check.go @@ -78,7 +78,7 @@ func (m *Manager) CheckCluster(clusterOrTopoName, scaleoutTopo string, opt Check topo.MonitoredOptions = currTopo.MonitoredOptions topo.ServerConfigs = currTopo.ServerConfigs - if err := spec.ParseTopologyYaml(scaleoutTopo, &topo); err != nil { + if err := spec.ParseTopologyYaml(scaleoutTopo, &topo, true); err != nil { return err } spec.ExpandRelativeDir(&topo) diff --git a/pkg/cluster/manager/scale_out.go b/pkg/cluster/manager/scale_out.go index f1f132a998..c16027be12 100644 --- a/pkg/cluster/manager/scale_out.go +++ b/pkg/cluster/manager/scale_out.go @@ -88,7 +88,7 @@ func (m *Manager) ScaleOut( // The no tispark master error is ignored, as if the tispark master is removed from the topology // file for some reason (manual edit, for example), it is still possible to scale-out it to make // the whole topology back to normal state. - if err := spec.ParseTopologyYaml(topoFile, newPart); err != nil && + if err := spec.ParseTopologyYaml(topoFile, newPart, true); err != nil && !errors.Is(perrs.Cause(err), spec.ErrNoTiSparkMaster) { return err } diff --git a/pkg/cluster/spec/parse_topology.go b/pkg/cluster/spec/parse_topology.go index 0c37abc754..828bc22a24 100644 --- a/pkg/cluster/spec/parse_topology.go +++ b/pkg/cluster/spec/parse_topology.go @@ -56,7 +56,8 @@ To generate a sample topology file: } // ParseTopologyYaml read yaml content from `file` and unmarshal it to `out` -func ParseTopologyYaml(file string, out Topology) error { +// ignoreGlobal ignore global variables in file, only ignoreGlobal with a index of 0 is effective +func ParseTopologyYaml(file string, out Topology, ignoreGlobal ...bool) error { suggestionProps := map[string]string{ "File": file, } @@ -68,6 +69,23 @@ func ParseTopologyYaml(file string, out Topology) error { return err } + // keep the global config in out + if len(ignoreGlobal) > 0 && ignoreGlobal[0] { + var newTopo map[string]interface{} + if err := yaml.Unmarshal(yamlFile, &newTopo); err != nil { + return err + } + for k := range newTopo { + switch k { + case "global", + "monitored", + "server_configs": + delete(newTopo, k) + } + } + yamlFile, _ = yaml.Marshal(newTopo) + } + if err = yaml.UnmarshalStrict(yamlFile, out); err != nil { return ErrTopologyParseFailed. Wrap(err, "Failed to parse topology file %s", file). diff --git a/pkg/cluster/spec/parse_topology_test.go b/pkg/cluster/spec/parse_topology_test.go index e0d6540c9d..3dfb6a60bc 100644 --- a/pkg/cluster/spec/parse_topology_test.go +++ b/pkg/cluster/spec/parse_topology_test.go @@ -61,6 +61,16 @@ func (s *topoSuite) TestParseTopologyYaml(c *check.C) { c.Assert(err, check.IsNil) } +func (s *topoSuite) TestParseTopologyYamlIgnoreGlobal(c *check.C) { + file := filepath.Join("testdata", "topology_err.yaml") + topo := Specification{} + err := ParseTopologyYaml(file, &topo, true) + if topo.GlobalOptions.DeployDir == "/tidb/deploy" { + c.Error("Can not ignore global variables") + } + c.Assert(err, check.IsNil) +} + func (s *topoSuite) TestRelativePath(c *check.C) { // test relative path withTempFile(`