Skip to content

Commit

Permalink
config: check undefined config item (#1362)
Browse files Browse the repository at this point in the history
* check the undefined config
  • Loading branch information
Connor1996 authored and nolouch committed Dec 10, 2018
1 parent 5fb6ba4 commit ceb8cec
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
18 changes: 18 additions & 0 deletions server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -316,9 +316,27 @@ func (m *configMetaData) Child(path ...string) *configMetaData {
}
}

func (m *configMetaData) CheckUndecoded() error {
if m.meta == nil {
return nil
}
undecoded := m.meta.Undecoded()
if len(undecoded) == 0 {
return nil
}
errInfo := "Config contains undefined item: "
for _, key := range undecoded {
errInfo += key.String() + ", "
}
return errors.New(errInfo[:len(errInfo)-2])
}

// Adjust is used to adjust the PD configurations.
func (c *Config) Adjust(meta *toml.MetaData) error {
configMetaData := newConfigMetadata(meta)
if err := configMetaData.CheckUndecoded(); err != nil {
return err
}

if c.Name == "" {
hostname, err := os.Hostname()
Expand Down
15 changes: 15 additions & 0 deletions server/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,19 @@ leader-schedule-limit = 0
// When undefined, use default values.
c.Assert(cfg.PreVote, IsTrue)
c.Assert(cfg.Schedule.MaxMergeRegionKeys, Equals, uint64(defaultMaxMergeRegionKeys))

// Check undefined config fields
cfgData = `
type = "pd"
name = ""
lease = 0
[schedule]
type = "random-merge"
`
cfg = NewConfig()
meta, err = toml.Decode(cfgData, &cfg)
c.Assert(err, IsNil)
err = cfg.Adjust(&meta)
c.Assert(err, NotNil)
}
11 changes: 5 additions & 6 deletions server/coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,12 +199,11 @@ func (c *coordinator) run() {
}
s, err := schedule.CreateScheduler(schedulerCfg.Type, c.opController, schedulerCfg.Args...)
if err != nil {
log.Errorf("can not create scheduler %s: %v", schedulerCfg.Type, err)
} else {
log.Infof("create scheduler %s", s.GetName())
if err = c.addScheduler(s, schedulerCfg.Args...); err != nil {
log.Errorf("can not add scheduler %s: %v", s.GetName(), err)
}
log.Fatalf("can not create scheduler %s: %v", schedulerCfg.Type, err)
}
log.Infof("create scheduler %s", s.GetName())
if err = c.addScheduler(s, schedulerCfg.Args...); err != nil {
log.Errorf("can not add scheduler %s: %v", s.GetName(), err)
}

// only record valid scheduler config
Expand Down

0 comments on commit ceb8cec

Please sign in to comment.