From 4aa03204392f7a2cb5f07965a98ad4283fb80422 Mon Sep 17 00:00:00 2001 From: Gyuho Lee Date: Tue, 20 Feb 2018 17:18:48 -0800 Subject: [PATCH] embed: document/validate compaction mode Signed-off-by: Gyuho Lee --- embed/config.go | 32 +++++++++++++++++++++++--------- embed/config_test.go | 9 +++++++++ 2 files changed, 32 insertions(+), 9 deletions(-) diff --git a/embed/config.go b/embed/config.go index 105165a5f..7204130bb 100644 --- a/embed/config.go +++ b/embed/config.go @@ -108,16 +108,22 @@ func init() { type Config struct { // member - CorsInfo *cors.CORSInfo - LPUrls, LCUrls []url.URL - Dir string `json:"data-dir"` - WalDir string `json:"wal-dir"` - MaxSnapFiles uint `json:"max-snapshots"` - MaxWalFiles uint `json:"max-wals"` - Name string `json:"name"` - SnapCount uint64 `json:"snapshot-count"` + CorsInfo *cors.CORSInfo + LPUrls, LCUrls []url.URL + Dir string `json:"data-dir"` + WalDir string `json:"wal-dir"` + MaxSnapFiles uint `json:"max-snapshots"` + MaxWalFiles uint `json:"max-wals"` + Name string `json:"name"` + SnapCount uint64 `json:"snapshot-count"` + + // AutoCompactionMode is either 'periodic' or 'revision'. + AutoCompactionMode string `json:"auto-compaction-mode"` + // AutoCompactionRetention is either duration string with time unit + // (e.g. '5m' for 5-minute), or revision unit (e.g. '5000'). + // If no time unit is provided and compaction mode is 'periodic', + // the unit defaults to hour. For example, '5' translates into 5-hour. AutoCompactionRetention string `json:"auto-compaction-retention"` - AutoCompactionMode string `json:"auto-compaction-mode"` // TickMs is the number of milliseconds between heartbeat ticks. // TODO: decouple tickMs and heartbeat tick (current heartbeat tick = 1). @@ -407,6 +413,7 @@ func (cfg *configYAML) configFromFile(path string) error { return cfg.Validate() } +// Validate ensures that '*embed.Config' fields are properly configured. func (cfg *Config) Validate() error { if err := checkBindURLs(cfg.LPUrls); err != nil { return err @@ -465,6 +472,13 @@ func (cfg *Config) Validate() error { return ErrUnsetAdvertiseClientURLsFlag } + switch cfg.AutoCompactionMode { + case "": + case CompactorModeRevision, CompactorModePeriodic: + default: + return fmt.Errorf("unknown auto-compaction-mode %q", cfg.AutoCompactionMode) + } + return nil } diff --git a/embed/config_test.go b/embed/config_test.go index 27b73238e..741944d47 100644 --- a/embed/config_test.go +++ b/embed/config_test.go @@ -148,3 +148,12 @@ func mustCreateCfgFile(t *testing.T, b []byte) *os.File { } return tmpfile } + +func TestAutoCompactionModeInvalid(t *testing.T) { + cfg := NewConfig() + cfg.AutoCompactionMode = "period" + err := cfg.Validate() + if err == nil { + t.Errorf("expected non-nil error, got %v", err) + } +}