failure_case_failpoints.go 4.1 KB

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