main.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. "net/http/pprof"
  20. "os"
  21. "strings"
  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. const pprofPrefix = "/debug/pprof-tester"
  33. func main() {
  34. endpointStr := flag.String("agent-endpoints", "localhost:9027", "HTTP RPC endpoints of agents. Do not specify the schema.")
  35. clientPorts := flag.String("client-ports", "", "etcd client port for each agent endpoint")
  36. peerPorts := flag.String("peer-ports", "", "etcd peer port for each agent endpoint")
  37. failpointPorts := flag.String("failpoint-ports", "", "etcd failpoint port for each agent endpoint")
  38. datadir := flag.String("data-dir", "agent.etcd", "etcd data directory location on agent machine.")
  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. stressQPS := flag.Int("stress-qps", 10000, "maximum number of stresser requests per second.")
  44. schedCases := flag.String("schedule-cases", "", "test case schedule")
  45. consistencyCheck := flag.Bool("consistency-check", true, "true to check consistency (revision, hash)")
  46. stresserType := flag.String("stresser", "keys,lease", "comma separated list of stressers (keys, lease, v2keys, nop, election-runner, watch-runner, lock-racer-runner, lease-runner).")
  47. etcdRunnerPath := flag.String("etcd-runner", "", "specify a path of etcd runner binary")
  48. failureTypes := flag.String("failures", "default,failpoints", "specify failures (concat of \"default\" and \"failpoints\").")
  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. agents[i].datadir = *datadir
  63. }
  64. c := &cluster{agents: agents}
  65. if err := c.bootstrap(); err != nil {
  66. plog.Fatal(err)
  67. }
  68. defer c.Terminate()
  69. // ensure cluster is fully booted to know failpoints are available
  70. c.WaitHealth()
  71. var failures []failure
  72. if failureTypes != nil && *failureTypes != "" {
  73. failures = makeFailures(*failureTypes, 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. scfg: scfg,
  113. stresserType: *stresserType,
  114. doChecks: *consistencyCheck,
  115. }
  116. sh := statusHandler{status: &t.status}
  117. http.Handle("/status", sh)
  118. http.Handle("/metrics", prometheus.Handler())
  119. if *enablePprof {
  120. http.Handle(pprofPrefix+"/", http.HandlerFunc(pprof.Index))
  121. http.Handle(pprofPrefix+"/profile", http.HandlerFunc(pprof.Profile))
  122. http.Handle(pprofPrefix+"/symbol", http.HandlerFunc(pprof.Symbol))
  123. http.Handle(pprofPrefix+"/cmdline", http.HandlerFunc(pprof.Cmdline))
  124. http.Handle(pprofPrefix+"/trace", http.HandlerFunc(pprof.Trace))
  125. http.Handle(pprofPrefix+"/heap", pprof.Handler("heap"))
  126. http.Handle(pprofPrefix+"/goroutine", pprof.Handler("goroutine"))
  127. http.Handle(pprofPrefix+"/threadcreate", pprof.Handler("threadcreate"))
  128. http.Handle(pprofPrefix+"/block", pprof.Handler("block"))
  129. }
  130. go func() { plog.Fatal(http.ListenAndServe(":9028", nil)) }()
  131. t.runLoop()
  132. }
  133. // portsFromArg converts a comma separated list into a slice of ints
  134. func portsFromArg(arg string, n, defaultPort int) []int {
  135. ret := make([]int, n)
  136. if len(arg) == 0 {
  137. for i := range ret {
  138. ret[i] = defaultPort
  139. }
  140. return ret
  141. }
  142. s := strings.Split(arg, ",")
  143. if len(s) != n {
  144. fmt.Printf("expected %d ports, got %d (%s)\n", n, len(s), arg)
  145. os.Exit(1)
  146. }
  147. for i := range s {
  148. if _, err := fmt.Sscanf(s[i], "%d", &ret[i]); err != nil {
  149. fmt.Println(err)
  150. os.Exit(1)
  151. }
  152. }
  153. return ret
  154. }
  155. func makeFailures(types string, c *cluster) []failure {
  156. var failures []failure
  157. fails := strings.Split(types, ",")
  158. for i := range fails {
  159. switch fails[i] {
  160. case "default":
  161. defaultFailures := []failure{
  162. newFailureKillAll(),
  163. newFailureKillMajority(),
  164. newFailureKillOne(),
  165. newFailureKillLeader(),
  166. newFailureKillOneForLongTime(),
  167. newFailureKillLeaderForLongTime(),
  168. newFailureIsolate(),
  169. newFailureIsolateAll(),
  170. newFailureSlowNetworkOneMember(),
  171. newFailureSlowNetworkLeader(),
  172. newFailureSlowNetworkAll(),
  173. }
  174. failures = append(failures, defaultFailures...)
  175. case "failpoints":
  176. fpFailures, fperr := failpointFailures(c)
  177. if len(fpFailures) == 0 {
  178. plog.Infof("no failpoints found (%v)", fperr)
  179. }
  180. failures = append(failures, fpFailures...)
  181. default:
  182. plog.Errorf("unknown failure: %s\n", fails[i])
  183. os.Exit(1)
  184. }
  185. }
  186. return failures
  187. }