main.go 6.1 KB

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