metrics_test.go 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2017 The etcd Authors
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package integration
  15. import (
  16. "context"
  17. "strconv"
  18. "testing"
  19. "time"
  20. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  21. "github.com/coreos/etcd/pkg/testutil"
  22. )
  23. // TestMetricDbSizeBoot checks that the db size metric is set on boot.
  24. func TestMetricDbSizeBoot(t *testing.T) {
  25. defer testutil.AfterTest(t)
  26. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  27. defer clus.Terminate(t)
  28. v, err := clus.Members[0].Metric("etcd_debugging_mvcc_db_total_size_in_bytes")
  29. if err != nil {
  30. t.Fatal(err)
  31. }
  32. if v == "0" {
  33. t.Fatalf("expected non-zero, got %q", v)
  34. }
  35. }
  36. // TestMetricDbSizeDefrag checks that the db size metric is set after defrag.
  37. func TestMetricDbSizeDefrag(t *testing.T) {
  38. defer testutil.AfterTest(t)
  39. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  40. defer clus.Terminate(t)
  41. kvc := toGRPC(clus.Client(0)).KV
  42. mc := toGRPC(clus.Client(0)).Maintenance
  43. // expand the db size
  44. numPuts := 10
  45. putreq := &pb.PutRequest{Key: []byte("k"), Value: make([]byte, 4096)}
  46. for i := 0; i < numPuts; i++ {
  47. if _, err := kvc.Put(context.TODO(), putreq); err != nil {
  48. t.Fatal(err)
  49. }
  50. }
  51. // wait for backend txn sync
  52. time.Sleep(500 * time.Millisecond)
  53. beforeDefrag, err := clus.Members[0].Metric("etcd_debugging_mvcc_db_total_size_in_bytes")
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. bv, err := strconv.Atoi(beforeDefrag)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. if expected := numPuts * len(putreq.Value); bv < expected {
  62. t.Fatalf("expected db size greater than %d, got %d", expected, bv)
  63. }
  64. // clear out historical keys
  65. creq := &pb.CompactionRequest{Revision: int64(numPuts), Physical: true}
  66. if _, err := kvc.Compact(context.TODO(), creq); err != nil {
  67. t.Fatal(err)
  68. }
  69. // defrag should give freed space back to fs
  70. mc.Defragment(context.TODO(), &pb.DefragmentRequest{})
  71. afterDefrag, err := clus.Members[0].Metric("etcd_debugging_mvcc_db_total_size_in_bytes")
  72. if err != nil {
  73. t.Fatal(err)
  74. }
  75. av, err := strconv.Atoi(afterDefrag)
  76. if err != nil {
  77. t.Fatal(err)
  78. }
  79. if bv <= av {
  80. t.Fatalf("expected less than %d, got %d after defrag", bv, av)
  81. }
  82. }