main.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. // Copyright 2015 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 main
  15. import (
  16. "flag"
  17. "fmt"
  18. "io/ioutil"
  19. "net/http"
  20. "os"
  21. "strings"
  22. "github.com/coreos/etcd/pkg/debugutil"
  23. "github.com/coreos/pkg/capnslog"
  24. "github.com/prometheus/client_golang/prometheus/promhttp"
  25. "golang.org/x/time/rate"
  26. "google.golang.org/grpc/grpclog"
  27. )
  28. var plog = capnslog.NewPackageLogger("github.com/coreos/etcd", "etcd-tester")
  29. const (
  30. defaultClientPort = 2379
  31. defaultPeerPort = 2380
  32. defaultFailpointPort = 2381
  33. )
  34. func main() {
  35. endpointStr := flag.String("agent-endpoints", "localhost:9027", "HTTP RPC endpoints of agents. Do not specify the schema.")
  36. clientPorts := flag.String("client-ports", "", "etcd client port for each agent endpoint")
  37. peerPorts := flag.String("peer-ports", "", "etcd peer port for each agent endpoint")
  38. failpointPorts := flag.String("failpoint-ports", "", "etcd failpoint port for each agent endpoint")
  39. stressKeyLargeSize := flag.Uint("stress-key-large-size", 32*1024+1, "the size of each large key written into etcd.")
  40. stressKeySize := flag.Uint("stress-key-size", 100, "the size of each small key written into etcd.")
  41. stressKeySuffixRange := flag.Uint("stress-key-count", 250000, "the count of key range written into etcd.")
  42. stressKeyTxnSuffixRange := flag.Uint("stress-key-txn-count", 100, "the count of key range written into etcd txn (max 100).")
  43. stressKeyTxnOps := flag.Uint("stress-key-txn-ops", 1, "number of operations per a transaction (max 64).")
  44. limit := flag.Int("limit", -1, "the limit of rounds to run failure set (-1 to run without limits).")
  45. exitOnFailure := flag.Bool("exit-on-failure", false, "exit tester on first failure")
  46. stressQPS := flag.Int("stress-qps", 10000, "maximum number of stresser requests per second.")
  47. schedCases := flag.String("schedule-cases", "", "test case schedule")
  48. consistencyCheck := flag.Bool("consistency-check", true, "true to check consistency (revision, hash)")
  49. stresserType := flag.String("stresser", "keys,lease", "comma separated list of stressers (keys, lease, v2keys, nop, election-runner, watch-runner, lock-racer-runner, lease-runner).")
  50. etcdRunnerPath := flag.String("etcd-runner", "", "specify a path of etcd runner binary")
  51. failureTypes := flag.String("failures", "default,failpoints", "specify failures (concat of \"default\" and \"failpoints\").")
  52. failpoints := flag.String("failpoints", `panic("etcd-tester")`, `comma separated list of failpoint terms to inject (e.g. 'panic("etcd-tester"),1*sleep(1000)')`)
  53. externalFailures := flag.String("external-failures", "", "specify a path of script for enabling/disabling an external fault injector")
  54. enablePprof := flag.Bool("enable-pprof", false, "true to enable pprof")
  55. flag.Parse()
  56. // to discard gRPC-side balancer logs
  57. grpclog.SetLoggerV2(grpclog.NewLoggerV2(ioutil.Discard, ioutil.Discard, ioutil.Discard))
  58. eps := strings.Split(*endpointStr, ",")
  59. cports := portsFromArg(*clientPorts, len(eps), defaultClientPort)
  60. pports := portsFromArg(*peerPorts, len(eps), defaultPeerPort)
  61. fports := portsFromArg(*failpointPorts, len(eps), defaultFailpointPort)
  62. agents := make([]agentConfig, len(eps))
  63. for i := range eps {
  64. agents[i].endpoint = eps[i]
  65. agents[i].clientPort = cports[i]
  66. agents[i].peerPort = pports[i]
  67. agents[i].failpointPort = fports[i]
  68. }
  69. c := &cluster{agents: agents}
  70. if err := c.bootstrap(); err != nil {
  71. plog.Fatal(err)
  72. }
  73. defer c.Terminate()
  74. // ensure cluster is fully booted to know failpoints are available
  75. c.WaitHealth()
  76. var failures []failure
  77. if failureTypes != nil && *failureTypes != "" {
  78. types, failpoints := strings.Split(*failureTypes, ","), strings.Split(*failpoints, ",")
  79. failures = makeFailures(types, failpoints, c)
  80. }
  81. if externalFailures != nil && *externalFailures != "" {
  82. if len(failures) != 0 {
  83. plog.Errorf("specify only one of -failures or -external-failures")
  84. os.Exit(1)
  85. }
  86. failures = append(failures, newFailureExternal(*externalFailures))
  87. }
  88. if len(failures) == 0 {
  89. plog.Infof("no failures\n")
  90. failures = append(failures, newFailureNop())
  91. }
  92. schedule := failures
  93. if schedCases != nil && *schedCases != "" {
  94. cases := strings.Split(*schedCases, " ")
  95. schedule = make([]failure, len(cases))
  96. for i := range cases {
  97. caseNum := 0
  98. n, err := fmt.Sscanf(cases[i], "%d", &caseNum)
  99. if n == 0 || err != nil {
  100. plog.Fatalf(`couldn't parse case "%s" (%v)`, cases[i], err)
  101. }
  102. schedule[i] = failures[caseNum]
  103. }
  104. }
  105. scfg := stressConfig{
  106. rateLimiter: rate.NewLimiter(rate.Limit(*stressQPS), *stressQPS),
  107. keyLargeSize: int(*stressKeyLargeSize),
  108. keySize: int(*stressKeySize),
  109. keySuffixRange: int(*stressKeySuffixRange),
  110. keyTxnSuffixRange: int(*stressKeyTxnSuffixRange),
  111. keyTxnOps: int(*stressKeyTxnOps),
  112. numLeases: 10,
  113. keysPerLease: 10,
  114. etcdRunnerPath: *etcdRunnerPath,
  115. }
  116. if scfg.keyTxnSuffixRange > 100 {
  117. plog.Fatalf("stress-key-txn-count is maximum 100, got %d", scfg.keyTxnSuffixRange)
  118. }
  119. if scfg.keyTxnOps > 64 {
  120. plog.Fatalf("stress-key-txn-ops is maximum 64, got %d", scfg.keyTxnOps)
  121. }
  122. t := &tester{
  123. failures: schedule,
  124. cluster: c,
  125. limit: *limit,
  126. exitOnFailure: *exitOnFailure,
  127. scfg: scfg,
  128. stresserType: *stresserType,
  129. doChecks: *consistencyCheck,
  130. }
  131. sh := statusHandler{status: &t.status}
  132. http.Handle("/status", sh)
  133. http.Handle("/metrics", promhttp.Handler())
  134. if *enablePprof {
  135. for p, h := range debugutil.PProfHandlers() {
  136. http.Handle(p, h)
  137. }
  138. }
  139. go func() { plog.Fatal(http.ListenAndServe(":9028", nil)) }()
  140. t.runLoop()
  141. }
  142. // portsFromArg converts a comma separated list into a slice of ints
  143. func portsFromArg(arg string, n, defaultPort int) []int {
  144. ret := make([]int, n)
  145. if len(arg) == 0 {
  146. for i := range ret {
  147. ret[i] = defaultPort
  148. }
  149. return ret
  150. }
  151. s := strings.Split(arg, ",")
  152. if len(s) != n {
  153. fmt.Printf("expected %d ports, got %d (%s)\n", n, len(s), arg)
  154. os.Exit(1)
  155. }
  156. for i := range s {
  157. if _, err := fmt.Sscanf(s[i], "%d", &ret[i]); err != nil {
  158. fmt.Println(err)
  159. os.Exit(1)
  160. }
  161. }
  162. return ret
  163. }
  164. func makeFailures(types, failpoints []string, c *cluster) []failure {
  165. var failures []failure
  166. for i := range types {
  167. switch types[i] {
  168. case "default":
  169. defaultFailures := []failure{
  170. newFailureKillAll(),
  171. newFailureKillMajority(),
  172. newFailureKillOne(),
  173. newFailureKillLeader(),
  174. newFailureKillOneForLongTime(),
  175. newFailureKillLeaderForLongTime(),
  176. newFailureIsolate(),
  177. newFailureIsolateAll(),
  178. newFailureSlowNetworkOneMember(),
  179. newFailureSlowNetworkLeader(),
  180. newFailureSlowNetworkAll(),
  181. }
  182. failures = append(failures, defaultFailures...)
  183. case "failpoints":
  184. fpFailures, fperr := failpointFailures(c, failpoints)
  185. if len(fpFailures) == 0 {
  186. plog.Infof("no failpoints found (%v)", fperr)
  187. }
  188. failures = append(failures, fpFailures...)
  189. default:
  190. plog.Errorf("unknown failure: %s\n", types[i])
  191. os.Exit(1)
  192. }
  193. }
  194. return failures
  195. }