embed: fix revision-based compaction with default value

Signed-off-by: Gyuho Lee <gyuhox@gmail.com>
release-3.4
Gyuho Lee 2018-02-20 17:21:07 -08:00
parent 4aa0320439
commit 83d1c3d5ec
2 changed files with 32 additions and 12 deletions

View File

@ -157,3 +157,13 @@ func TestAutoCompactionModeInvalid(t *testing.T) {
t.Errorf("expected non-nil error, got %v", err)
}
}
func TestAutoCompactionModeParse(t *testing.T) {
dur, err := parseCompactionRetention("revision", "1")
if err != nil {
t.Error(err)
}
if dur != 1 {
t.Fatalf("AutoCompactionRetention expected 1, got %d", dur)
}
}

View File

@ -134,22 +134,13 @@ func StartEtcd(inCfg *Config) (e *Etcd, err error) {
}
}
var (
autoCompactionRetention time.Duration
h int
)
// AutoCompactionRetention defaults to "0" if not set.
if len(cfg.AutoCompactionRetention) == 0 {
cfg.AutoCompactionRetention = "0"
}
h, err = strconv.Atoi(cfg.AutoCompactionRetention)
if err == nil {
autoCompactionRetention = time.Duration(int64(h)) * time.Hour
} else {
autoCompactionRetention, err = time.ParseDuration(cfg.AutoCompactionRetention)
if err != nil {
return nil, fmt.Errorf("error parsing AutoCompactionRetention: %v", err)
}
autoCompactionRetention, err := parseCompactionRetention(cfg.AutoCompactionMode, cfg.AutoCompactionRetention)
if err != nil {
return e, err
}
srvcfg := etcdserver.ServerConfig{
@ -562,3 +553,22 @@ func (e *Etcd) errHandler(err error) {
case e.errc <- err:
}
}
func parseCompactionRetention(mode, retention string) (ret time.Duration, err error) {
h, err := strconv.Atoi(retention)
if err == nil {
switch mode {
case CompactorModeRevision:
ret = time.Duration(int64(h))
case CompactorModePeriodic:
ret = time.Duration(int64(h)) * time.Hour
}
} else {
// periodic compaction
ret, err = time.ParseDuration(retention)
if err != nil {
return 0, fmt.Errorf("error parsing CompactionRetention: %v", err)
}
}
return ret, nil
}