tester.go 7.6 KB

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