Skip to content

Commit

Permalink
config: check undefined config item (#1362) (#1374)
Browse files Browse the repository at this point in the history
* check the undefined config
  • Loading branch information
Connor1996 authored and nolouch committed Dec 12, 2018
1 parent f684248 commit 5672765
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 @@ -313,9 +313,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
}
adjustString(&c.Name, defaultName)
adjustString(&c.DataDir, fmt.Sprintf("default.%s", c.Name))

Expand Down
15 changes: 15 additions & 0 deletions server/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,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 @@ -230,12 +230,11 @@ func (c *coordinator) run() {
}
s, err := schedule.CreateScheduler(schedulerCfg.Type, c.limiter, 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 5672765

Please sign in to comment.