failpoint.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. // Copyright 2016 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. "io/ioutil"
  18. "net/http"
  19. "strings"
  20. "sync"
  21. "time"
  22. )
  23. type failpointStats struct {
  24. // crashes counts the number of crashes for a failpoint
  25. crashes map[string]int
  26. // mu protects crashes
  27. mu sync.Mutex
  28. }
  29. var fpStats failpointStats
  30. func failpointFailures(c *cluster) (ret []failure, err error) {
  31. var fps []string
  32. fps, err = failpointPaths(c.Members[0].FailpointURL)
  33. if err != nil {
  34. return nil, err
  35. }
  36. // create failure objects for all failpoints
  37. for _, fp := range fps {
  38. if len(fp) == 0 {
  39. continue
  40. }
  41. fpFails := failuresFromFailpoint(fp)
  42. // wrap in delays so failpoint has time to trigger
  43. for i, fpf := range fpFails {
  44. if strings.Contains(fp, "Snap") {
  45. // hack to trigger snapshot failpoints
  46. fpFails[i] = &failureUntilSnapshot{fpf}
  47. } else {
  48. fpFails[i] = &failureDelay{fpf, 3 * time.Second}
  49. }
  50. }
  51. ret = append(ret, fpFails...)
  52. }
  53. fpStats.crashes = make(map[string]int)
  54. return ret, err
  55. }
  56. func failpointPaths(endpoint string) ([]string, error) {
  57. resp, err := http.Get(endpoint)
  58. if err != nil {
  59. return nil, err
  60. }
  61. defer resp.Body.Close()
  62. body, rerr := ioutil.ReadAll(resp.Body)
  63. if rerr != nil {
  64. return nil, rerr
  65. }
  66. var fps []string
  67. for _, l := range strings.Split(string(body), "\n") {
  68. fp := strings.Split(l, "=")[0]
  69. fps = append(fps, fp)
  70. }
  71. return fps, nil
  72. }
  73. func failuresFromFailpoint(fp string) []failure {
  74. inject := makeInjectFailpoint(fp, `panic("etcd-tester")`)
  75. recov := makeRecoverFailpoint(fp)
  76. return []failure{
  77. &failureOne{
  78. description: description("failpoint " + fp + " panic one"),
  79. injectMember: inject,
  80. recoverMember: recov,
  81. },
  82. &failureAll{
  83. description: description("failpoint " + fp + " panic all"),
  84. injectMember: inject,
  85. recoverMember: recov,
  86. },
  87. &failureMajority{
  88. description: description("failpoint " + fp + " panic majority"),
  89. injectMember: inject,
  90. recoverMember: recov,
  91. },
  92. &failureLeader{
  93. failureByFunc{
  94. description: description("failpoint " + fp + " panic leader"),
  95. injectMember: inject,
  96. recoverMember: recov,
  97. },
  98. 0,
  99. },
  100. }
  101. }
  102. func makeInjectFailpoint(fp, val string) injectMemberFunc {
  103. return func(m *member) (err error) {
  104. return putFailpoint(m.FailpointURL, fp, val)
  105. }
  106. }
  107. func makeRecoverFailpoint(fp string) recoverMemberFunc {
  108. return func(m *member) error {
  109. if err := delFailpoint(m.FailpointURL, fp); err == nil {
  110. return nil
  111. }
  112. // node not responding, likely dead from fp panic; restart
  113. fpStats.mu.Lock()
  114. fpStats.crashes[fp]++
  115. fpStats.mu.Unlock()
  116. return recoverStop(m)
  117. }
  118. }
  119. func putFailpoint(ep, fp, val string) error {
  120. req, _ := http.NewRequest(http.MethodPut, ep+"/"+fp, strings.NewReader(val))
  121. c := http.Client{}
  122. resp, err := c.Do(req)
  123. if err != nil {
  124. return err
  125. }
  126. resp.Body.Close()
  127. if resp.StatusCode/100 != 2 {
  128. return fmt.Errorf("failed to PUT %s=%s at %s (%v)", fp, val, ep, resp.Status)
  129. }
  130. return nil
  131. }
  132. func delFailpoint(ep, fp string) error {
  133. req, _ := http.NewRequest(http.MethodDelete, ep+"/"+fp, strings.NewReader(""))
  134. c := http.Client{}
  135. resp, err := c.Do(req)
  136. if err != nil {
  137. return err
  138. }
  139. resp.Body.Close()
  140. if resp.StatusCode/100 != 2 {
  141. return fmt.Errorf("failed to DELETE %s at %s (%v)", fp, ep, resp.Status)
  142. }
  143. return nil
  144. }