main.go 6.8 KB

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