Skip to content

Commit

Permalink
integration: test mvcc db size metric is updated following defrag
Browse files Browse the repository at this point in the history
  • Loading branch information
heyitsanthony committed Jun 21, 2017
1 parent b7e8ad6 commit 9d3a8d3
Showing 1 changed file with 59 additions and 2 deletions.
61 changes: 59 additions & 2 deletions integration/metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
package integration

import (
"context"
"strconv"
"testing"

pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
"github.com/coreos/etcd/pkg/testutil"
)

// TestMetricDbSize checks that the db size metric is set on boot.
func TestMetricDbSize(t *testing.T) {
// TestMetricDbSizeBoot checks that the db size metric is set on boot.
func TestMetricDbSizeBoot(t *testing.T) {
defer testutil.AfterTest(t)
clus := NewClusterV3(t, &ClusterConfig{Size: 1})
defer clus.Terminate(t)
Expand All @@ -35,3 +38,57 @@ func TestMetricDbSize(t *testing.T) {
t.Fatalf("expected non-zero, got %q", v)
}
}

// TestMetricDbSizeDefrag checks that the db size metric is set after defrag.
func TestMetricDbSizeDefrag(t *testing.T) {
defer testutil.AfterTest(t)
clus := NewClusterV3(t, &ClusterConfig{Size: 1})
defer clus.Terminate(t)

kvc := toGRPC(clus.Client(0)).KV
mc := toGRPC(clus.Client(0)).Maintenance

// expand the db size
numPuts := 20
putreq := &pb.PutRequest{Key: []byte("k"), Value: make([]byte, 4096)}
putreq.Value[0] = 1
for i := 0; i < numPuts; i++ {
if _, err := kvc.Put(context.TODO(), putreq); err != nil {
t.Fatal(err)
}
}

beforeDefrag, err := clus.Members[0].Metric("etcd_debugging_mvcc_db_total_size_in_bytes")
if err != nil {
t.Fatal(err)
}
bv, err := strconv.Atoi(beforeDefrag)
if err != nil {
t.Fatal(err)
}
if expected := numPuts * len(putreq.Value); bv < expected {
t.Fatalf("expected db size greater than %d, got %d", expected, bv)
}

// clear out historical keys
creq := &pb.CompactionRequest{Revision: int64(numPuts), Physical: true}
if _, err := kvc.Compact(context.TODO(), creq); err != nil {
t.Fatal(err)
}

// defrag should give freed space back to fs
mc.Defragment(context.TODO(), &pb.DefragmentRequest{})
afterDefrag, err := clus.Members[0].Metric("etcd_debugging_mvcc_db_total_size_in_bytes")
if err != nil {
t.Fatal(err)
}

av, err := strconv.Atoi(afterDefrag)
if err != nil {
t.Fatal(err)
}

if bv <= av {
t.Fatalf("expected less than %d, got %d after defrag", bv, av)
}
}

0 comments on commit 9d3a8d3

Please sign in to comment.