Skip to content

Commit

Permalink
manager: check for global configs and prompt user for confirmation on…
Browse files Browse the repository at this point in the history
… scale-out
  • Loading branch information
AstroProfundis committed Apr 14, 2021
1 parent 3a1296b commit 04117e6
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 5 deletions.
40 changes: 40 additions & 0 deletions pkg/cluster/manager/scale_out.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/pingcap/tiup/pkg/logger/log"
"github.com/pingcap/tiup/pkg/set"
"github.com/pingcap/tiup/pkg/utils"
"gopkg.in/yaml.v3"
)

// ScaleOut scale out the cluster.
Expand All @@ -47,6 +48,12 @@ func (m *Manager) ScaleOut(
return err
}

// check for the input topology to let user confirm if there're any
// global configs set
if err := checkForGlobalConfigs(topoFile); err != nil {
return err
}

metadata, err := m.meta(name)
// allow specific validation errors so that user can recover a broken
// cluster if it is somehow in a bad state.
Expand All @@ -57,6 +64,7 @@ func (m *Manager) ScaleOut(

topo := metadata.GetTopology()
base := metadata.GetBaseMeta()

// Inherit existing global configuration. We must assign the inherited values before unmarshalling
// because some default value rely on the global options and monitored options.
newPart := topo.NewPart()
Expand Down Expand Up @@ -168,3 +176,35 @@ func validateNewTopo(topo spec.Topology) (err error) {
})
return err
}

// checkForGlobalConfigs checks the input scale out topology to make sure users are aware
// of the global config fields in it will be ignored.
func checkForGlobalConfigs(topoFile string) error {
yamlFile, err := spec.ReadYamlFile(topoFile)
if err != nil {
return err
}

var newPart map[string]interface{}
if err := yaml.Unmarshal(yamlFile, &newPart); err != nil {
return err
}

for k := range newPart {
switch k {
case "global",
"monitored",
"server_configs":
log.Warnf(
`You have one or more of ["global", "monitored", "server_configs"] fields configured in
the scale out topology, but they will be ignored during the scaling out process.
If you want to use configs different from the existing cluster, cancel now and
set them in the specification fileds for each host.`)
if err := cliutil.PromptForConfirmOrAbortError("Do you want to continue? [y/N]: "); err != nil {
return err
}
return nil // user confirmed, skip futher checks
}
}
return nil
}
23 changes: 18 additions & 5 deletions pkg/cluster/spec/parse_topology.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,15 @@ var (
ErrTopologyParseFailed = errNSTopolohy.NewType("parse_failed", errutil.ErrTraitPreCheck)
)

// ParseTopologyYaml read yaml content from `file` and unmarshal it to `out`
func ParseTopologyYaml(file string, out Topology) error {
// ReadYamlFile read yaml content from file`
func ReadYamlFile(file string) ([]byte, error) {
suggestionProps := map[string]string{
"File": file,
}

zap.L().Debug("Parse topology file", zap.String("file", file))

yamlFile, err := os.ReadFile(file)
if err != nil {
return ErrTopologyReadFailed.
return nil, ErrTopologyReadFailed.
Wrap(err, "Failed to read topology file %s", file).
WithProperty(cliutil.SuggestionFromTemplate(`
Please check whether your topology file {{ColorKeyword}}{{.File}}{{ColorReset}} exists and try again.
Expand All @@ -54,6 +52,21 @@ To generate a sample topology file:
{{ColorCommand}}{{OsArgs0}} template topology > topo.yaml{{ColorReset}}
`, suggestionProps))
}
return yamlFile, nil
}

// ParseTopologyYaml read yaml content from `file` and unmarshal it to `out`
func ParseTopologyYaml(file string, out Topology) error {
suggestionProps := map[string]string{
"File": file,
}

zap.L().Debug("Parse topology file", zap.String("file", file))

yamlFile, err := ReadYamlFile(file)
if err != nil {
return err
}

if err = yaml.UnmarshalStrict(yamlFile, out); err != nil {
return ErrTopologyParseFailed.
Expand Down

0 comments on commit 04117e6

Please sign in to comment.