cluster_tester.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. "go.uber.org/zap"
  20. )
  21. // compactQPS is rough number of compact requests per second.
  22. // Previous tests showed etcd can compact about 60,000 entries per second.
  23. const compactQPS = 50000
  24. // StartTester starts tester.
  25. func (clus *Cluster) StartTester() {
  26. // TODO: upate status
  27. clus.startStresser()
  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. "doRound failed; returning",
  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. "compacting",
  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 failed",
  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 failed",
  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.lg.Warn(
  82. "defrag failed; returning",
  83. zap.Int("round", clus.rd),
  84. zap.Int("case", clus.cs),
  85. zap.Error(err),
  86. )
  87. clus.failed()
  88. return
  89. }
  90. }
  91. }
  92. clus.lg.Info(
  93. "functional-tester passed",
  94. zap.Int("round", clus.rd),
  95. zap.Int("case", clus.cs),
  96. )
  97. }
  98. func (clus *Cluster) doRound() error {
  99. if clus.Tester.FailureShuffle {
  100. clus.shuffleFailures()
  101. }
  102. clus.lg.Info(
  103. "starting round",
  104. zap.Int("round", clus.rd),
  105. zap.Strings("failures", clus.failureStrings()),
  106. )
  107. for i, f := range clus.failures {
  108. clus.cs = i
  109. caseTotalCounter.WithLabelValues(f.Desc()).Inc()
  110. clus.lg.Info("wait health before injecting failures")
  111. if err := clus.WaitHealth(); err != nil {
  112. return fmt.Errorf("wait full health error: %v", err)
  113. }
  114. clus.lg.Info(
  115. "injecting failure",
  116. zap.Int("round", clus.rd),
  117. zap.Int("case", clus.cs),
  118. zap.String("desc", f.Desc()),
  119. )
  120. if err := f.Inject(clus); err != nil {
  121. return fmt.Errorf("injection error: %v", err)
  122. }
  123. clus.lg.Info(
  124. "injected failure",
  125. zap.Int("round", clus.rd),
  126. zap.Int("case", clus.cs),
  127. zap.String("desc", f.Desc()),
  128. )
  129. // if run local, recovering server may conflict
  130. // with stressing client ports
  131. // TODO: use unix for local tests
  132. clus.lg.Info(
  133. "recovering failure",
  134. zap.Int("round", clus.rd),
  135. zap.Int("case", clus.cs),
  136. zap.String("desc", f.Desc()),
  137. )
  138. if err := f.Recover(clus); err != nil {
  139. return fmt.Errorf("recovery error: %v", err)
  140. }
  141. clus.lg.Info(
  142. "recovered failure",
  143. zap.Int("round", clus.rd),
  144. zap.Int("case", clus.cs),
  145. zap.String("desc", f.Desc()),
  146. )
  147. clus.lg.Info("pausing stresser after failure recovery, before wait health")
  148. clus.pauseStresser()
  149. clus.lg.Info("wait health after recovering failures")
  150. if err := clus.WaitHealth(); err != nil {
  151. return fmt.Errorf("wait full health error: %v", err)
  152. }
  153. clus.lg.Info("check consistency after recovering failures")
  154. if err := clus.checkConsistency(); err != nil {
  155. return fmt.Errorf("tt.checkConsistency error (%v)", err)
  156. }
  157. clus.lg.Info(
  158. "failure case passed",
  159. zap.Int("round", clus.rd),
  160. zap.Int("case", clus.cs),
  161. zap.String("desc", f.Desc()),
  162. )
  163. }
  164. clus.lg.Info(
  165. "finished round",
  166. zap.Int("round", clus.rd),
  167. zap.Strings("failures", clus.failureStrings()),
  168. )
  169. return nil
  170. }
  171. func (clus *Cluster) updateRevision() error {
  172. revs, _, err := clus.getRevisionHash()
  173. for _, rev := range revs {
  174. clus.currentRevision = rev
  175. break // just need get one of the current revisions
  176. }
  177. clus.lg.Info(
  178. "updated current revision",
  179. zap.Int64("current-revision", clus.currentRevision),
  180. )
  181. return err
  182. }
  183. func (clus *Cluster) compact(rev int64, timeout time.Duration) (err error) {
  184. clus.lg.Info("pausing stresser before compact")
  185. clus.pauseStresser()
  186. defer func() {
  187. if err == nil {
  188. err = clus.startStresser()
  189. }
  190. }()
  191. clus.lg.Info(
  192. "compacting storage",
  193. zap.Int64("current-revision", clus.currentRevision),
  194. zap.Int64("compact-revision", rev),
  195. )
  196. if err = clus.compactKV(rev, timeout); err != nil {
  197. return err
  198. }
  199. clus.lg.Info(
  200. "compacted storage",
  201. zap.Int64("current-revision", clus.currentRevision),
  202. zap.Int64("compact-revision", rev),
  203. )
  204. clus.lg.Info(
  205. "checking compaction",
  206. zap.Int64("current-revision", clus.currentRevision),
  207. zap.Int64("compact-revision", rev),
  208. )
  209. if err = clus.checkCompact(rev); err != nil {
  210. clus.lg.Warn(
  211. "checkCompact failed",
  212. zap.Int64("current-revision", clus.currentRevision),
  213. zap.Int64("compact-revision", rev),
  214. zap.Error(err),
  215. )
  216. return err
  217. }
  218. clus.lg.Info(
  219. "confirmed compaction",
  220. zap.Int64("current-revision", clus.currentRevision),
  221. zap.Int64("compact-revision", rev),
  222. )
  223. return nil
  224. }
  225. func (clus *Cluster) failed() {
  226. if !clus.Tester.ExitOnFailure {
  227. return
  228. }
  229. clus.lg.Info(
  230. "exiting on failure",
  231. zap.Int("round", clus.rd),
  232. zap.Int("case", clus.cs),
  233. )
  234. clus.DestroyEtcdAgents()
  235. os.Exit(2)
  236. }
  237. func (clus *Cluster) cleanup() error {
  238. defer clus.failed()
  239. roundFailedTotalCounter.Inc()
  240. desc := "compact/defrag"
  241. if clus.cs != -1 {
  242. desc = clus.failures[clus.cs].Desc()
  243. }
  244. caseFailedTotalCounter.WithLabelValues(desc).Inc()
  245. clus.closeStresser()
  246. if err := clus.FailArchive(); err != nil {
  247. clus.lg.Warn(
  248. "cleanup failed",
  249. zap.Int("round", clus.rd),
  250. zap.Int("case", clus.cs),
  251. zap.Error(err),
  252. )
  253. return err
  254. }
  255. if err := clus.Restart(); err != nil {
  256. clus.lg.Warn(
  257. "restart failed",
  258. zap.Int("round", clus.rd),
  259. zap.Int("case", clus.cs),
  260. zap.Error(err),
  261. )
  262. return err
  263. }
  264. clus.updateStresserChecker()
  265. return nil
  266. }