main.go 6.5 KB

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