stress.go 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2018 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 tester
  15. import (
  16. "fmt"
  17. "time"
  18. "github.com/coreos/etcd/functional/rpcpb"
  19. "go.uber.org/zap"
  20. )
  21. // Stresser defines stressing client operations.
  22. type Stresser interface {
  23. // Stress starts to stress the etcd cluster
  24. Stress() error
  25. // Pause stops the stresser from sending requests to etcd. Resume by calling Stress.
  26. Pause() map[string]int
  27. // Close releases all of the Stresser's resources.
  28. Close() map[string]int
  29. // ModifiedKeys reports the number of keys created and deleted by stresser
  30. ModifiedKeys() int64
  31. // Checker returns an invariant checker for after the stresser is canceled.
  32. Checker() Checker
  33. }
  34. // newStresser creates stresser from a comma separated list of stresser types.
  35. func newStresser(clus *Cluster, m *rpcpb.Member) Stresser {
  36. stressers := make([]Stresser, len(clus.Tester.StressTypes))
  37. for i, stype := range clus.Tester.StressTypes {
  38. clus.lg.Info(
  39. "creating stresser",
  40. zap.String("type", stype),
  41. zap.String("endpoint", m.EtcdClientEndpoint),
  42. )
  43. switch stype {
  44. case "KV":
  45. // TODO: Too intensive stressing clients can panic etcd member with
  46. // 'out of memory' error. Put rate limits in server side.
  47. stressers[i] = &keyStresser{
  48. stype: rpcpb.StressType_KV,
  49. lg: clus.lg,
  50. m: m,
  51. keySize: int(clus.Tester.StressKeySize),
  52. keyLargeSize: int(clus.Tester.StressKeySizeLarge),
  53. keySuffixRange: int(clus.Tester.StressKeySuffixRange),
  54. keyTxnSuffixRange: int(clus.Tester.StressKeySuffixRangeTxn),
  55. keyTxnOps: int(clus.Tester.StressKeyTxnOps),
  56. clientsN: int(clus.Tester.StressClients),
  57. rateLimiter: clus.rateLimiter,
  58. }
  59. case "LEASE":
  60. stressers[i] = &leaseStresser{
  61. stype: rpcpb.StressType_LEASE,
  62. lg: clus.lg,
  63. m: m,
  64. numLeases: 10, // TODO: configurable
  65. keysPerLease: 10, // TODO: configurable
  66. rateLimiter: clus.rateLimiter,
  67. }
  68. case "ELECTION_RUNNER":
  69. reqRate := 100
  70. args := []string{
  71. "election",
  72. fmt.Sprintf("%v", time.Now().UnixNano()), // election name as current nano time
  73. "--dial-timeout=10s",
  74. "--endpoints", m.EtcdClientEndpoint,
  75. "--total-client-connections=10",
  76. "--rounds=0", // runs forever
  77. "--req-rate", fmt.Sprintf("%v", reqRate),
  78. }
  79. stressers[i] = newRunnerStresser(
  80. rpcpb.StressType_ELECTION_RUNNER,
  81. clus.lg,
  82. clus.Tester.RunnerExecPath,
  83. args,
  84. clus.rateLimiter,
  85. reqRate,
  86. )
  87. case "WATCH_RUNNER":
  88. reqRate := 100
  89. args := []string{
  90. "watcher",
  91. "--prefix", fmt.Sprintf("%v", time.Now().UnixNano()), // prefix all keys with nano time
  92. "--total-keys=1",
  93. "--total-prefixes=1",
  94. "--watch-per-prefix=1",
  95. "--endpoints", m.EtcdClientEndpoint,
  96. "--rounds=0", // runs forever
  97. "--req-rate", fmt.Sprintf("%v", reqRate),
  98. }
  99. stressers[i] = newRunnerStresser(
  100. rpcpb.StressType_WATCH_RUNNER,
  101. clus.lg,
  102. clus.Tester.RunnerExecPath,
  103. args,
  104. clus.rateLimiter,
  105. reqRate,
  106. )
  107. case "LOCK_RACER_RUNNER":
  108. reqRate := 100
  109. args := []string{
  110. "lock-racer",
  111. fmt.Sprintf("%v", time.Now().UnixNano()), // locker name as current nano time
  112. "--endpoints", m.EtcdClientEndpoint,
  113. "--total-client-connections=10",
  114. "--rounds=0", // runs forever
  115. "--req-rate", fmt.Sprintf("%v", reqRate),
  116. }
  117. stressers[i] = newRunnerStresser(
  118. rpcpb.StressType_LOCK_RACER_RUNNER,
  119. clus.lg,
  120. clus.Tester.RunnerExecPath,
  121. args,
  122. clus.rateLimiter,
  123. reqRate,
  124. )
  125. case "LEASE_RUNNER":
  126. args := []string{
  127. "lease-renewer",
  128. "--ttl=30",
  129. "--endpoints", m.EtcdClientEndpoint,
  130. }
  131. stressers[i] = newRunnerStresser(
  132. rpcpb.StressType_LEASE_RUNNER,
  133. clus.lg,
  134. clus.Tester.RunnerExecPath,
  135. args,
  136. clus.rateLimiter,
  137. 0,
  138. )
  139. }
  140. }
  141. return &compositeStresser{stressers}
  142. }