cluster_run.go 8.5 KB

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