cluster_tester.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. // Copyright 2018 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 tester
  15. import (
  16. "fmt"
  17. "os"
  18. "time"
  19. "github.com/coreos/etcd/tools/functional-tester/rpcpb"
  20. "go.uber.org/zap"
  21. )
  22. // compactQPS is rough number of compact requests per second.
  23. // Previous tests showed etcd can compact about 60,000 entries per second.
  24. const compactQPS = 50000
  25. // StartTester starts tester.
  26. func (clus *Cluster) StartTester() {
  27. // TODO: upate status
  28. var preModifiedKey int64
  29. for round := 0; round < int(clus.Tester.RoundLimit) || clus.Tester.RoundLimit == -1; round++ {
  30. roundTotalCounter.Inc()
  31. clus.rd = round
  32. if err := clus.doRound(); err != nil {
  33. clus.lg.Warn(
  34. "round FAIL",
  35. zap.Int("round", clus.rd),
  36. zap.Int("case", clus.cs),
  37. zap.Error(err),
  38. )
  39. if clus.cleanup() != nil {
  40. return
  41. }
  42. // reset preModifiedKey after clean up
  43. preModifiedKey = 0
  44. continue
  45. }
  46. // -1 so that logPrefix doesn't print out 'case'
  47. clus.cs = -1
  48. revToCompact := max(0, clus.currentRevision-10000)
  49. currentModifiedKey := clus.stresser.ModifiedKeys()
  50. modifiedKey := currentModifiedKey - preModifiedKey
  51. preModifiedKey = currentModifiedKey
  52. timeout := 10 * time.Second
  53. timeout += time.Duration(modifiedKey/compactQPS) * time.Second
  54. clus.lg.Info(
  55. "compact START",
  56. zap.Int("round", clus.rd),
  57. zap.Int("case", clus.cs),
  58. zap.Duration("timeout", timeout),
  59. )
  60. if err := clus.compact(revToCompact, timeout); err != nil {
  61. clus.lg.Warn(
  62. "compact FAIL",
  63. zap.Int("round", clus.rd),
  64. zap.Int("case", clus.cs),
  65. zap.Error(err),
  66. )
  67. if err = clus.cleanup(); err != nil {
  68. clus.lg.Warn(
  69. "cleanup FAIL",
  70. zap.Int("round", clus.rd),
  71. zap.Int("case", clus.cs),
  72. zap.Error(err),
  73. )
  74. return
  75. }
  76. // reset preModifiedKey after clean up
  77. preModifiedKey = 0
  78. }
  79. if round > 0 && round%500 == 0 { // every 500 rounds
  80. if err := clus.defrag(); err != nil {
  81. clus.failed()
  82. return
  83. }
  84. }
  85. }
  86. clus.lg.Info(
  87. "functional-tester PASS",
  88. zap.Int("round", clus.rd),
  89. zap.Int("case", clus.cs),
  90. )
  91. }
  92. func (clus *Cluster) doRound() error {
  93. if clus.Tester.FailureShuffle {
  94. clus.shuffleFailures()
  95. }
  96. roundNow := time.Now()
  97. clus.lg.Info(
  98. "round START",
  99. zap.Int("round", clus.rd),
  100. zap.Strings("failures", clus.failureStrings()),
  101. zap.Int("total-failures", len(clus.failures)),
  102. )
  103. for i, fa := range clus.failures {
  104. clus.cs = i
  105. caseTotalCounter.WithLabelValues(fa.Desc()).Inc()
  106. caseNow := time.Now()
  107. clus.lg.Info(
  108. "case START",
  109. zap.Int("round", clus.rd),
  110. zap.Int("case", clus.cs),
  111. zap.String("desc", fa.Desc()),
  112. )
  113. clus.lg.Info("wait health before injecting failures")
  114. if err := clus.WaitHealth(); err != nil {
  115. return fmt.Errorf("wait full health error: %v", err)
  116. }
  117. stressStarted := false
  118. fcase := fa.FailureCase()
  119. if fcase != rpcpb.FailureCase_NO_FAIL_WITH_NO_STRESS_FOR_LIVENESS {
  120. clus.lg.Info(
  121. "stresser START",
  122. zap.Int("round", clus.rd),
  123. zap.Int("case", clus.cs),
  124. zap.String("desc", fa.Desc()),
  125. )
  126. if err := clus.stresser.Stress(); err != nil {
  127. return fmt.Errorf("start stresser error: %v", err)
  128. }
  129. stressStarted = true
  130. }
  131. clus.lg.Info(
  132. "inject START",
  133. zap.Int("round", clus.rd),
  134. zap.Int("case", clus.cs),
  135. zap.String("desc", fa.Desc()),
  136. )
  137. if err := fa.Inject(clus); err != nil {
  138. return fmt.Errorf("injection error: %v", err)
  139. }
  140. // if run local, recovering server may conflict
  141. // with stressing client ports
  142. // TODO: use unix for local tests
  143. clus.lg.Info(
  144. "recover START",
  145. zap.Int("round", clus.rd),
  146. zap.Int("case", clus.cs),
  147. zap.String("desc", fa.Desc()),
  148. )
  149. if err := fa.Recover(clus); err != nil {
  150. return fmt.Errorf("recovery error: %v", err)
  151. }
  152. if stressStarted {
  153. clus.lg.Info("stresser PAUSE")
  154. ems := clus.stresser.Pause()
  155. if fcase == rpcpb.FailureCase_NO_FAIL_WITH_STRESS && len(ems) > 0 {
  156. ess := make([]string, 0, len(ems))
  157. cnt := 0
  158. for k, v := range ems {
  159. ess = append(ess, fmt.Sprintf("%s (count: %d)", k, v))
  160. cnt += v
  161. }
  162. clus.lg.Warn(
  163. "expected no errors",
  164. zap.String("desc", fa.Desc()),
  165. zap.Strings("errors", ess),
  166. )
  167. // with network delay, some ongoing requests may fail
  168. // only return error, if more than 10% of QPS requests fail
  169. if cnt > int(clus.Tester.StressQPS)/10 {
  170. return fmt.Errorf("expected no error in %q, got %q", fcase.String(), ess)
  171. }
  172. }
  173. }
  174. clus.lg.Info("health check START")
  175. if err := clus.WaitHealth(); err != nil {
  176. return fmt.Errorf("wait full health error: %v", err)
  177. }
  178. clus.lg.Info("consistency check START")
  179. if err := clus.checkConsistency(); err != nil {
  180. return fmt.Errorf("consistency check error (%v)", err)
  181. }
  182. clus.lg.Info(
  183. "case PASS",
  184. zap.Int("round", clus.rd),
  185. zap.Int("case", clus.cs),
  186. zap.String("desc", fa.Desc()),
  187. zap.Duration("took", time.Since(caseNow)),
  188. )
  189. }
  190. clus.lg.Info(
  191. "round ALL PASS",
  192. zap.Int("round", clus.rd),
  193. zap.Strings("failures", clus.failureStrings()),
  194. zap.Duration("took", time.Since(roundNow)),
  195. )
  196. return nil
  197. }
  198. func (clus *Cluster) updateRevision() error {
  199. revs, _, err := clus.getRevisionHash()
  200. for _, rev := range revs {
  201. clus.currentRevision = rev
  202. break // just need get one of the current revisions
  203. }
  204. clus.lg.Info(
  205. "updated current revision",
  206. zap.Int64("current-revision", clus.currentRevision),
  207. )
  208. return err
  209. }
  210. func (clus *Cluster) compact(rev int64, timeout time.Duration) (err error) {
  211. if err = clus.compactKV(rev, timeout); err != nil {
  212. clus.lg.Warn(
  213. "compact FAIL",
  214. zap.Int64("current-revision", clus.currentRevision),
  215. zap.Int64("compact-revision", rev),
  216. zap.Error(err),
  217. )
  218. return err
  219. }
  220. clus.lg.Info(
  221. "compact DONE",
  222. zap.Int64("current-revision", clus.currentRevision),
  223. zap.Int64("compact-revision", rev),
  224. )
  225. if err = clus.checkCompact(rev); err != nil {
  226. clus.lg.Warn(
  227. "check compact FAIL",
  228. zap.Int64("current-revision", clus.currentRevision),
  229. zap.Int64("compact-revision", rev),
  230. zap.Error(err),
  231. )
  232. return err
  233. }
  234. clus.lg.Info(
  235. "check compact DONE",
  236. zap.Int64("current-revision", clus.currentRevision),
  237. zap.Int64("compact-revision", rev),
  238. )
  239. return nil
  240. }
  241. func (clus *Cluster) failed() {
  242. if !clus.Tester.ExitOnFailure {
  243. return
  244. }
  245. clus.lg.Info(
  246. "functional-tester FAIL",
  247. zap.Int("round", clus.rd),
  248. zap.Int("case", clus.cs),
  249. )
  250. clus.DestroyEtcdAgents()
  251. os.Exit(2)
  252. }
  253. func (clus *Cluster) cleanup() error {
  254. defer clus.failed()
  255. roundFailedTotalCounter.Inc()
  256. desc := "compact/defrag"
  257. if clus.cs != -1 {
  258. desc = clus.failures[clus.cs].Desc()
  259. }
  260. caseFailedTotalCounter.WithLabelValues(desc).Inc()
  261. clus.lg.Info(
  262. "closing stressers before archiving failure data",
  263. zap.Int("round", clus.rd),
  264. zap.Int("case", clus.cs),
  265. )
  266. clus.stresser.Close()
  267. if err := clus.FailArchive(); err != nil {
  268. clus.lg.Warn(
  269. "cleanup FAIL",
  270. zap.Int("round", clus.rd),
  271. zap.Int("case", clus.cs),
  272. zap.Error(err),
  273. )
  274. return err
  275. }
  276. if err := clus.Restart(); err != nil {
  277. clus.lg.Warn(
  278. "restart FAIL",
  279. zap.Int("round", clus.rd),
  280. zap.Int("case", clus.cs),
  281. zap.Error(err),
  282. )
  283. return err
  284. }
  285. clus.updateStresserChecker()
  286. return nil
  287. }