tester.go 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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. "fmt"
  17. "time"
  18. )
  19. type tester struct {
  20. failures []failure
  21. cluster *cluster
  22. limit int
  23. consistencyCheck bool
  24. status Status
  25. currentRevision int64
  26. }
  27. // compactQPS is rough number of compact requests per second.
  28. // Previous tests showed etcd can compact about 60,000 entries per second.
  29. const compactQPS = 50000
  30. func (tt *tester) runLoop() {
  31. tt.status.Since = time.Now()
  32. tt.status.RoundLimit = tt.limit
  33. tt.status.cluster = tt.cluster
  34. for _, f := range tt.failures {
  35. tt.status.Failures = append(tt.status.Failures, f.Desc())
  36. }
  37. var (
  38. round int
  39. prevCompactRev int64
  40. )
  41. for {
  42. tt.status.setRound(round)
  43. tt.status.setCase(-1) // -1 so that logPrefix doesn't print out 'case'
  44. roundTotalCounter.Inc()
  45. var failed bool
  46. for j, f := range tt.failures {
  47. caseTotalCounter.WithLabelValues(f.Desc()).Inc()
  48. tt.status.setCase(j)
  49. if err := tt.cluster.WaitHealth(); err != nil {
  50. plog.Printf("%s wait full health error: %v", tt.logPrefix(), err)
  51. if err := tt.cleanup(); err != nil {
  52. return
  53. }
  54. failed = true
  55. break
  56. }
  57. plog.Printf("%s injecting failure %q", tt.logPrefix(), f.Desc())
  58. if err := f.Inject(tt.cluster, round); err != nil {
  59. plog.Printf("%s injection error: %v", tt.logPrefix(), err)
  60. if err := tt.cleanup(); err != nil {
  61. return
  62. }
  63. failed = true
  64. break
  65. }
  66. plog.Printf("%s injected failure", tt.logPrefix())
  67. plog.Printf("%s recovering failure %q", tt.logPrefix(), f.Desc())
  68. if err := f.Recover(tt.cluster, round); err != nil {
  69. plog.Printf("%s recovery error: %v", tt.logPrefix(), err)
  70. if err := tt.cleanup(); err != nil {
  71. return
  72. }
  73. failed = true
  74. break
  75. }
  76. plog.Printf("%s recovered failure", tt.logPrefix())
  77. if tt.cluster.v2Only {
  78. plog.Printf("%s succeed!", tt.logPrefix())
  79. continue
  80. }
  81. if !tt.consistencyCheck {
  82. if err := tt.updateRevision(); err != nil {
  83. plog.Warningf("%s functional-tester returning with tt.updateRevision error (%v)", tt.logPrefix(), err)
  84. return
  85. }
  86. continue
  87. }
  88. var err error
  89. failed, err = tt.checkConsistency()
  90. if err != nil {
  91. plog.Warningf("%s functional-tester returning with tt.checkConsistency error (%v)", tt.logPrefix(), err)
  92. return
  93. }
  94. if failed {
  95. break
  96. }
  97. plog.Printf("%s succeed!", tt.logPrefix())
  98. }
  99. // -1 so that logPrefix doesn't print out 'case'
  100. tt.status.setCase(-1)
  101. if failed {
  102. continue
  103. }
  104. revToCompact := max(0, tt.currentRevision-10000)
  105. compactN := revToCompact - prevCompactRev
  106. timeout := 10 * time.Second
  107. if prevCompactRev != 0 && compactN > 0 {
  108. timeout += time.Duration(compactN/compactQPS) * time.Second
  109. }
  110. prevCompactRev = revToCompact
  111. plog.Printf("%s compacting %d entries (timeout %v)", tt.logPrefix(), compactN, timeout)
  112. if err := tt.compact(revToCompact, timeout); err != nil {
  113. plog.Warningf("%s functional-tester compact got error (%v)", tt.logPrefix(), err)
  114. if err := tt.cleanup(); err != nil {
  115. return
  116. }
  117. }
  118. if round > 0 && round%500 == 0 { // every 500 rounds
  119. if err := tt.defrag(); err != nil {
  120. plog.Warningf("%s functional-tester returning with error (%v)", tt.logPrefix(), err)
  121. return
  122. }
  123. }
  124. round++
  125. if round == tt.limit {
  126. plog.Printf("%s functional-tester is finished", tt.logPrefix())
  127. break
  128. }
  129. }
  130. }
  131. func (tt *tester) updateRevision() error {
  132. revs, _, err := tt.cluster.getRevisionHash()
  133. for _, rev := range revs {
  134. tt.currentRevision = rev
  135. break // just need get one of the current revisions
  136. }
  137. return err
  138. }
  139. func (tt *tester) checkConsistency() (failed bool, err error) {
  140. tt.cancelStressers()
  141. defer tt.startStressers()
  142. plog.Printf("%s updating current revisions...", tt.logPrefix())
  143. var (
  144. revs map[string]int64
  145. hashes map[string]int64
  146. rerr error
  147. ok bool
  148. )
  149. for i := 0; i < 7; i++ {
  150. time.Sleep(time.Second)
  151. revs, hashes, rerr = tt.cluster.getRevisionHash()
  152. if rerr != nil {
  153. plog.Printf("%s #%d failed to get current revisions (%v)", tt.logPrefix(), i, rerr)
  154. continue
  155. }
  156. if tt.currentRevision, ok = getSameValue(revs); ok {
  157. break
  158. }
  159. plog.Printf("%s #%d inconsistent current revisions %+v", tt.logPrefix(), i, revs)
  160. }
  161. plog.Printf("%s updated current revisions with %d", tt.logPrefix(), tt.currentRevision)
  162. if !ok || rerr != nil {
  163. plog.Printf("%s checking current revisions failed [revisions: %v]", tt.logPrefix(), revs)
  164. failed = true
  165. err = tt.cleanup()
  166. return
  167. }
  168. plog.Printf("%s all members are consistent with current revisions [revisions: %v]", tt.logPrefix(), revs)
  169. plog.Printf("%s checking current storage hashes...", tt.logPrefix())
  170. if _, ok = getSameValue(hashes); !ok {
  171. plog.Printf("%s checking current storage hashes failed [hashes: %v]", tt.logPrefix(), hashes)
  172. failed = true
  173. err = tt.cleanup()
  174. return
  175. }
  176. plog.Printf("%s all members are consistent with storage hashes", tt.logPrefix())
  177. return
  178. }
  179. func (tt *tester) compact(rev int64, timeout time.Duration) error {
  180. plog.Printf("%s compacting storage (current revision %d, compact revision %d)", tt.logPrefix(), tt.currentRevision, rev)
  181. if err := tt.cluster.compactKV(rev, timeout); err != nil {
  182. return err
  183. }
  184. plog.Printf("%s compacted storage (compact revision %d)", tt.logPrefix(), rev)
  185. plog.Printf("%s checking compaction (compact revision %d)", tt.logPrefix(), rev)
  186. if err := tt.cluster.checkCompact(rev); err != nil {
  187. plog.Warningf("%s checkCompact error (%v)", tt.logPrefix(), err)
  188. return err
  189. }
  190. plog.Printf("%s confirmed compaction (compact revision %d)", tt.logPrefix(), rev)
  191. return nil
  192. }
  193. func (tt *tester) defrag() error {
  194. plog.Printf("%s defragmenting...", tt.logPrefix())
  195. if err := tt.cluster.defrag(); err != nil {
  196. plog.Warningf("%s defrag error (%v)", tt.logPrefix(), err)
  197. if cerr := tt.cleanup(); cerr != nil {
  198. return fmt.Errorf("%s, %s", err, cerr)
  199. }
  200. return err
  201. }
  202. plog.Printf("%s defragmented...", tt.logPrefix())
  203. return nil
  204. }
  205. func (tt *tester) logPrefix() string {
  206. var (
  207. rd = tt.status.getRound()
  208. cs = tt.status.getCase()
  209. prefix = fmt.Sprintf("[round#%d case#%d]", rd, cs)
  210. )
  211. if cs == -1 {
  212. prefix = fmt.Sprintf("[round#%d]", rd)
  213. }
  214. return prefix
  215. }
  216. func (tt *tester) cleanup() error {
  217. roundFailedTotalCounter.Inc()
  218. desc := "compact/defrag"
  219. if tt.status.Case != -1 {
  220. desc = tt.failures[tt.status.Case].Desc()
  221. }
  222. caseFailedTotalCounter.WithLabelValues(desc).Inc()
  223. plog.Printf("%s cleaning up...", tt.logPrefix())
  224. if err := tt.cluster.Cleanup(); err != nil {
  225. plog.Warningf("%s cleanup error: %v", tt.logPrefix(), err)
  226. return err
  227. }
  228. if err := tt.cluster.Bootstrap(); err != nil {
  229. plog.Warningf("%s cleanup Bootstrap error: %v", tt.logPrefix(), err)
  230. return err
  231. }
  232. return nil
  233. }
  234. func (tt *tester) cancelStressers() {
  235. plog.Printf("%s canceling the stressers...", tt.logPrefix())
  236. for _, s := range tt.cluster.Stressers {
  237. s.Cancel()
  238. }
  239. plog.Printf("%s canceled stressers", tt.logPrefix())
  240. }
  241. func (tt *tester) startStressers() {
  242. plog.Printf("%s starting the stressers...", tt.logPrefix())
  243. for _, s := range tt.cluster.Stressers {
  244. go s.Stress()
  245. }
  246. plog.Printf("%s started stressers", tt.logPrefix())
  247. }