metrics_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. "fmt"
  18. "net/http"
  19. "strconv"
  20. "testing"
  21. "time"
  22. "go.etcd.io/etcd/etcdserver"
  23. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  24. "go.etcd.io/etcd/pkg/testutil"
  25. "go.etcd.io/etcd/pkg/transport"
  26. )
  27. // TestMetricDbSizeBoot checks that the db size metric is set on boot.
  28. func TestMetricDbSizeBoot(t *testing.T) {
  29. defer testutil.AfterTest(t)
  30. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  31. defer clus.Terminate(t)
  32. v, err := clus.Members[0].Metric("etcd_debugging_mvcc_db_total_size_in_bytes")
  33. if err != nil {
  34. t.Fatal(err)
  35. }
  36. if v == "0" {
  37. t.Fatalf("expected non-zero, got %q", v)
  38. }
  39. }
  40. func TestMetricDbSizeDefrag(t *testing.T) {
  41. testMetricDbSizeDefrag(t, "etcd")
  42. }
  43. func TestMetricDbSizeDefragDebugging(t *testing.T) {
  44. testMetricDbSizeDefrag(t, "etcd_debugging")
  45. }
  46. // testMetricDbSizeDefrag checks that the db size metric is set after defrag.
  47. func testMetricDbSizeDefrag(t *testing.T, name string) {
  48. defer testutil.AfterTest(t)
  49. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  50. defer clus.Terminate(t)
  51. kvc := toGRPC(clus.Client(0)).KV
  52. mc := toGRPC(clus.Client(0)).Maintenance
  53. // expand the db size
  54. numPuts := 25 // large enough to write more than 1 page
  55. putreq := &pb.PutRequest{Key: []byte("k"), Value: make([]byte, 4096)}
  56. for i := 0; i < numPuts; i++ {
  57. time.Sleep(10 * time.Millisecond) // to execute multiple backend txn
  58. if _, err := kvc.Put(context.TODO(), putreq); err != nil {
  59. t.Fatal(err)
  60. }
  61. }
  62. // wait for backend txn sync
  63. time.Sleep(500 * time.Millisecond)
  64. expected := numPuts * len(putreq.Value)
  65. beforeDefrag, err := clus.Members[0].Metric(name + "_mvcc_db_total_size_in_bytes")
  66. if err != nil {
  67. t.Fatal(err)
  68. }
  69. bv, err := strconv.Atoi(beforeDefrag)
  70. if err != nil {
  71. t.Fatal(err)
  72. }
  73. if bv < expected {
  74. t.Fatalf("expected db size greater than %d, got %d", expected, bv)
  75. }
  76. beforeDefragInUse, err := clus.Members[0].Metric("etcd_mvcc_db_total_size_in_use_in_bytes")
  77. if err != nil {
  78. t.Fatal(err)
  79. }
  80. biu, err := strconv.Atoi(beforeDefragInUse)
  81. if err != nil {
  82. t.Fatal(err)
  83. }
  84. if biu < expected {
  85. t.Fatalf("expected db size in use is greater than %d, got %d", expected, biu)
  86. }
  87. // clear out historical keys, in use bytes should free pages
  88. creq := &pb.CompactionRequest{Revision: int64(numPuts), Physical: true}
  89. if _, kerr := kvc.Compact(context.TODO(), creq); kerr != nil {
  90. t.Fatal(kerr)
  91. }
  92. validateAfterCompactionInUse := func() error {
  93. // Put to move PendingPages to FreePages
  94. if _, err = kvc.Put(context.TODO(), putreq); err != nil {
  95. t.Fatal(err)
  96. }
  97. time.Sleep(500 * time.Millisecond)
  98. afterCompactionInUse, err := clus.Members[0].Metric("etcd_mvcc_db_total_size_in_use_in_bytes")
  99. if err != nil {
  100. t.Fatal(err)
  101. }
  102. aciu, err := strconv.Atoi(afterCompactionInUse)
  103. if err != nil {
  104. t.Fatal(err)
  105. }
  106. if biu <= aciu {
  107. return fmt.Errorf("expected less than %d, got %d after compaction", biu, aciu)
  108. }
  109. return nil
  110. }
  111. // backend rollbacks read transaction asynchronously (PR #10523),
  112. // which causes the result to be flaky. Retry 3 times.
  113. maxRetry, retry := 3, 0
  114. for {
  115. err := validateAfterCompactionInUse()
  116. if err == nil {
  117. break
  118. }
  119. retry++
  120. if retry >= maxRetry {
  121. t.Fatalf(err.Error())
  122. }
  123. }
  124. // defrag should give freed space back to fs
  125. mc.Defragment(context.TODO(), &pb.DefragmentRequest{})
  126. afterDefrag, err := clus.Members[0].Metric(name + "_mvcc_db_total_size_in_bytes")
  127. if err != nil {
  128. t.Fatal(err)
  129. }
  130. av, err := strconv.Atoi(afterDefrag)
  131. if err != nil {
  132. t.Fatal(err)
  133. }
  134. if bv <= av {
  135. t.Fatalf("expected less than %d, got %d after defrag", bv, av)
  136. }
  137. afterDefragInUse, err := clus.Members[0].Metric("etcd_mvcc_db_total_size_in_use_in_bytes")
  138. if err != nil {
  139. t.Fatal(err)
  140. }
  141. adiu, err := strconv.Atoi(afterDefragInUse)
  142. if err != nil {
  143. t.Fatal(err)
  144. }
  145. if adiu > av {
  146. t.Fatalf("db size in use (%d) is expected less than db size (%d) after defrag", adiu, av)
  147. }
  148. }
  149. func TestMetricQuotaBackendBytes(t *testing.T) {
  150. defer testutil.AfterTest(t)
  151. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  152. defer clus.Terminate(t)
  153. qs, err := clus.Members[0].Metric("etcd_server_quota_backend_bytes")
  154. if err != nil {
  155. t.Fatal(err)
  156. }
  157. qv, err := strconv.ParseFloat(qs, 64)
  158. if err != nil {
  159. t.Fatal(err)
  160. }
  161. if int64(qv) != etcdserver.DefaultQuotaBytes {
  162. t.Fatalf("expected %d, got %f", etcdserver.DefaultQuotaBytes, qv)
  163. }
  164. }
  165. func TestMetricsHealth(t *testing.T) {
  166. defer testutil.AfterTest(t)
  167. clus := NewClusterV3(t, &ClusterConfig{Size: 1})
  168. defer clus.Terminate(t)
  169. tr, err := transport.NewTransport(transport.TLSInfo{}, 5*time.Second)
  170. if err != nil {
  171. t.Fatal(err)
  172. }
  173. u := clus.Members[0].ClientURLs[0]
  174. u.Path = "/health"
  175. resp, err := tr.RoundTrip(&http.Request{
  176. Header: make(http.Header),
  177. Method: http.MethodGet,
  178. URL: &u,
  179. })
  180. resp.Body.Close()
  181. if err != nil {
  182. t.Fatal(err)
  183. }
  184. hv, err := clus.Members[0].Metric("etcd_server_health_failures")
  185. if err != nil {
  186. t.Fatal(err)
  187. }
  188. if hv != "0" {
  189. t.Fatalf("expected '0' from etcd_server_health_failures, got %q", hv)
  190. }
  191. }