Skip to content

Commit

Permalink
embed: fix revision-based compaction with default value
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <gyuhox@gmail.com>
  • Loading branch information
gyuho committed Feb 21, 2018
1 parent 4aa0320 commit 83d1c3d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
10 changes: 10 additions & 0 deletions embed/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
34 changes: 22 additions & 12 deletions embed/etcd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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
}

0 comments on commit 83d1c3d

Please sign in to comment.