main.go 6.6 KB

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