failure.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  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. "math/rand"
  18. "time"
  19. )
  20. type failure interface {
  21. // Inject injeccts the failure into the testing cluster at the given
  22. // round. When calling the function, the cluster should be in health.
  23. Inject(c *cluster, round int) error
  24. // Recover recovers the injected failure caused by the injection of the
  25. // given round and wait for the recovery of the testing cluster.
  26. Recover(c *cluster, round int) error
  27. // Desc returns a description of the failure
  28. Desc() string
  29. }
  30. type description string
  31. func (d description) Desc() string { return string(d) }
  32. type injectMemberFunc func(*member) error
  33. type recoverMemberFunc func(*member) error
  34. type failureByFunc struct {
  35. description
  36. injectMember injectMemberFunc
  37. recoverMember recoverMemberFunc
  38. }
  39. type failureOne failureByFunc
  40. type failureAll failureByFunc
  41. type failureMajority failureByFunc
  42. type failureLeader struct {
  43. failureByFunc
  44. idx int
  45. }
  46. type failureDelay struct {
  47. failure
  48. delayDuration time.Duration
  49. }
  50. // failureUntilSnapshot injects a failure and waits for a snapshot event
  51. type failureUntilSnapshot struct{ failure }
  52. func (f *failureOne) Inject(c *cluster, round int) error {
  53. return f.injectMember(c.Members[round%c.Size])
  54. }
  55. func (f *failureOne) Recover(c *cluster, round int) error {
  56. if err := f.recoverMember(c.Members[round%c.Size]); err != nil {
  57. return err
  58. }
  59. return c.WaitHealth()
  60. }
  61. func (f *failureAll) Inject(c *cluster, round int) error {
  62. for _, m := range c.Members {
  63. if err := f.injectMember(m); err != nil {
  64. return err
  65. }
  66. }
  67. return nil
  68. }
  69. func (f *failureAll) Recover(c *cluster, round int) error {
  70. for _, m := range c.Members {
  71. if err := f.recoverMember(m); err != nil {
  72. return err
  73. }
  74. }
  75. return c.WaitHealth()
  76. }
  77. func (f *failureMajority) Inject(c *cluster, round int) error {
  78. for i := range killMap(c.Size, round) {
  79. if err := f.injectMember(c.Members[i]); err != nil {
  80. return err
  81. }
  82. }
  83. return nil
  84. }
  85. func (f *failureMajority) Recover(c *cluster, round int) error {
  86. for i := range killMap(c.Size, round) {
  87. if err := f.recoverMember(c.Members[i]); err != nil {
  88. return err
  89. }
  90. }
  91. return nil
  92. }
  93. func (f *failureLeader) Inject(c *cluster, round int) error {
  94. idx, err := c.GetLeader()
  95. if err != nil {
  96. return err
  97. }
  98. f.idx = idx
  99. return f.injectMember(c.Members[idx])
  100. }
  101. func (f *failureLeader) Recover(c *cluster, round int) error {
  102. if err := f.recoverMember(c.Members[f.idx]); err != nil {
  103. return err
  104. }
  105. return c.WaitHealth()
  106. }
  107. func (f *failureDelay) Inject(c *cluster, round int) error {
  108. if err := f.failure.Inject(c, round); err != nil {
  109. return err
  110. }
  111. time.Sleep(f.delayDuration)
  112. return nil
  113. }
  114. func (f *failureUntilSnapshot) Inject(c *cluster, round int) error {
  115. if err := f.failure.Inject(c, round); err != nil {
  116. return err
  117. }
  118. if c.Size < 3 {
  119. return nil
  120. }
  121. start, _ := c.Report()
  122. end := start
  123. // Normal healthy cluster could accept 1000req/s at least.
  124. // Give it 3-times time to create a new snapshot.
  125. retry := snapshotCount / 1000 * 3
  126. for j := 0; j < retry; j++ {
  127. end, _ = c.Report()
  128. // If the number of proposals committed is bigger than snapshot count,
  129. // a new snapshot should have been created.
  130. if end-start > snapshotCount {
  131. return nil
  132. }
  133. time.Sleep(time.Second)
  134. }
  135. return fmt.Errorf("cluster too slow: only commit %d requests in %ds", end-start, retry)
  136. }
  137. func (f *failureUntilSnapshot) Desc() string {
  138. return f.failure.Desc() + " for a long time and expect it to recover from an incoming snapshot"
  139. }
  140. func killMap(size int, seed int) map[int]bool {
  141. m := make(map[int]bool)
  142. r := rand.New(rand.NewSource(int64(seed)))
  143. majority := size/2 + 1
  144. for {
  145. m[r.Intn(size)] = true
  146. if len(m) >= majority {
  147. return m
  148. }
  149. }
  150. }