Browse Source

embed: fix revision-based compaction with default value

Signed-off-by: Gyuho Lee <gyuhox@gmail.com>
Gyuho Lee 7 years ago
parent
commit
83d1c3d5ec
2 changed files with 32 additions and 12 deletions
  1. 10 0
      embed/config_test.go
  2. 22 12
      embed/etcd.go

+ 10 - 0
embed/config_test.go

@@ -157,3 +157,13 @@ func TestAutoCompactionModeInvalid(t *testing.T) {
 		t.Errorf("expected non-nil error, got %v", err)
 		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)
+	}
+}

+ 22 - 12
embed/etcd.go

@@ -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.
 	// AutoCompactionRetention defaults to "0" if not set.
 	if len(cfg.AutoCompactionRetention) == 0 {
 	if len(cfg.AutoCompactionRetention) == 0 {
 		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{
 	srvcfg := etcdserver.ServerConfig{
@@ -562,3 +553,22 @@ func (e *Etcd) errHandler(err error) {
 	case e.errc <- err:
 	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
+}