Browse Source

Merge pull request #9330 from gyuho/auto-compact-defrag

ctlv3/command: auto compact and defrag after "check perf"
Gyuho Lee 7 years ago
parent
commit
b03fd4cbc3
1 changed files with 39 additions and 3 deletions
  1. 39 3
      etcdctl/ctlv3/command/check.go

+ 39 - 3
etcdctl/ctlv3/command/check.go

@@ -33,8 +33,10 @@ import (
 )
 )
 
 
 var (
 var (
-	checkPerfLoad   string
-	checkPerfPrefix string
+	checkPerfLoad        string
+	checkPerfPrefix      string
+	checkPerfAutoCompact bool
+	checkPerfAutoDefrag  bool
 )
 )
 
 
 type checkPerfCfg struct {
 type checkPerfCfg struct {
@@ -90,6 +92,8 @@ func NewCheckPerfCommand() *cobra.Command {
 	// TODO: support customized configuration
 	// TODO: support customized configuration
 	cmd.Flags().StringVar(&checkPerfLoad, "load", "s", "The performance check's workload model. Accepted workloads: s(small), m(medium), l(large), xl(xLarge)")
 	cmd.Flags().StringVar(&checkPerfLoad, "load", "s", "The performance check's workload model. Accepted workloads: s(small), m(medium), l(large), xl(xLarge)")
 	cmd.Flags().StringVar(&checkPerfPrefix, "prefix", "/etcdctl-check-perf/", "The prefix for writing the performance check's keys.")
 	cmd.Flags().StringVar(&checkPerfPrefix, "prefix", "/etcdctl-check-perf/", "The prefix for writing the performance check's keys.")
+	cmd.Flags().BoolVar(&checkPerfAutoCompact, "auto-compact", false, "Compact storage with last revision after test is finished.")
+	cmd.Flags().BoolVar(&checkPerfAutoDefrag, "auto-defrag", false, "Defragment storage after test is finished.")
 
 
 	return cmd
 	return cmd
 }
 }
@@ -175,12 +179,22 @@ func newCheckPerfCommand(cmd *cobra.Command, args []string) {
 	s := <-sc
 	s := <-sc
 
 
 	ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
 	ctx, cancel = context.WithTimeout(context.Background(), 30*time.Second)
-	_, err = clients[0].Delete(ctx, checkPerfPrefix, v3.WithPrefix())
+	dresp, err := clients[0].Delete(ctx, checkPerfPrefix, v3.WithPrefix())
 	cancel()
 	cancel()
 	if err != nil {
 	if err != nil {
 		ExitWithError(ExitError, err)
 		ExitWithError(ExitError, err)
 	}
 	}
 
 
+	if checkPerfAutoCompact {
+		compact(clients[0], dresp.Header.Revision)
+	}
+
+	if checkPerfAutoDefrag {
+		for _, ep := range clients[0].Endpoints() {
+			defrag(clients[0], ep)
+		}
+	}
+
 	ok = true
 	ok = true
 	if len(s.ErrorDist) != 0 {
 	if len(s.ErrorDist) != 0 {
 		fmt.Println("FAIL: too many errors")
 		fmt.Println("FAIL: too many errors")
@@ -216,3 +230,25 @@ func newCheckPerfCommand(cmd *cobra.Command, args []string) {
 		os.Exit(ExitError)
 		os.Exit(ExitError)
 	}
 	}
 }
 }
+
+func compact(c *v3.Client, rev int64) {
+	fmt.Printf("Compacting with revision %d\n", rev)
+	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+	_, err := c.Compact(ctx, rev, v3.WithCompactPhysical())
+	cancel()
+	if err != nil {
+		ExitWithError(ExitError, err)
+	}
+	fmt.Printf("Compacted with revision %d\n", rev)
+}
+
+func defrag(c *v3.Client, ep string) {
+	fmt.Printf("Defragmenting %q\n", ep)
+	ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
+	_, err := c.Defragment(ctx, ep)
+	cancel()
+	if err != nil {
+		ExitWithError(ExitError, err)
+	}
+	fmt.Printf("Defragmented %q\n", ep)
+}