stresser.go 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. "io/ioutil"
  18. "log"
  19. "math/rand"
  20. "net"
  21. "net/http"
  22. "sync"
  23. "time"
  24. clientV2 "github.com/coreos/etcd/client"
  25. "github.com/coreos/etcd/etcdserver"
  26. "github.com/coreos/etcd/etcdserver/api/v3rpc/rpctypes"
  27. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  28. "golang.org/x/net/context"
  29. "google.golang.org/grpc"
  30. "google.golang.org/grpc/grpclog"
  31. "google.golang.org/grpc/transport"
  32. )
  33. func init() {
  34. grpclog.SetLogger(log.New(ioutil.Discard, "", 0))
  35. }
  36. type Stresser interface {
  37. // Stress starts to stress the etcd cluster
  38. Stress() error
  39. // Cancel cancels the stress test on the etcd cluster
  40. Cancel()
  41. // Report reports the success and failure of the stress test
  42. Report() (success int, failure int)
  43. }
  44. type stresser struct {
  45. Endpoint string
  46. KeySize int
  47. KeySuffixRange int
  48. N int
  49. mu sync.Mutex
  50. wg *sync.WaitGroup
  51. cancel func()
  52. conn *grpc.ClientConn
  53. success int
  54. }
  55. func (s *stresser) Stress() error {
  56. // TODO: add backoff option
  57. conn, err := grpc.Dial(s.Endpoint, grpc.WithInsecure())
  58. if err != nil {
  59. return fmt.Errorf("%v (%s)", err, s.Endpoint)
  60. }
  61. defer conn.Close()
  62. ctx, cancel := context.WithCancel(context.Background())
  63. wg := &sync.WaitGroup{}
  64. wg.Add(s.N)
  65. s.mu.Lock()
  66. s.conn = conn
  67. s.cancel = cancel
  68. s.wg = wg
  69. s.mu.Unlock()
  70. kvc := pb.NewKVClient(conn)
  71. for i := 0; i < s.N; i++ {
  72. go func(i int) {
  73. defer wg.Done()
  74. for {
  75. // TODO: 10-second is enough timeout to cover leader failure
  76. // and immediate leader election. Find out what other cases this
  77. // could be timed out.
  78. putctx, putcancel := context.WithTimeout(ctx, 10*time.Second)
  79. _, err := kvc.Put(putctx, &pb.PutRequest{
  80. Key: []byte(fmt.Sprintf("foo%d", rand.Intn(s.KeySuffixRange))),
  81. Value: []byte(randStr(s.KeySize)),
  82. })
  83. putcancel()
  84. if err != nil {
  85. shouldContinue := false
  86. switch grpc.ErrorDesc(err) {
  87. case context.DeadlineExceeded.Error():
  88. // This retries when request is triggered at the same time as
  89. // leader failure. When we terminate the leader, the request to
  90. // that leader cannot be processed, and times out. Also requests
  91. // to followers cannot be forwarded to the old leader, so timing out
  92. // as well. We want to keep stressing until the cluster elects a
  93. // new leader and start processing requests again.
  94. shouldContinue = true
  95. case etcdserver.ErrStopped.Error():
  96. // one of the etcd nodes stopped from failure injection
  97. shouldContinue = true
  98. case transport.ErrConnClosing.Desc:
  99. // server closed the transport (failure injected node)
  100. shouldContinue = true
  101. case rpctypes.ErrNotCapable.Error():
  102. // capability check has not been done (in the beginning)
  103. shouldContinue = true
  104. // default:
  105. // errors from stresser.Cancel method:
  106. // rpc error: code = 1 desc = context canceled (type grpc.rpcError)
  107. // rpc error: code = 2 desc = grpc: the client connection is closing (type grpc.rpcError)
  108. }
  109. if shouldContinue {
  110. continue
  111. }
  112. return
  113. }
  114. s.mu.Lock()
  115. s.success++
  116. s.mu.Unlock()
  117. }
  118. }(i)
  119. }
  120. <-ctx.Done()
  121. return nil
  122. }
  123. func (s *stresser) Cancel() {
  124. s.mu.Lock()
  125. cancel, conn, wg := s.cancel, s.conn, s.wg
  126. s.mu.Unlock()
  127. cancel()
  128. wg.Wait()
  129. conn.Close()
  130. }
  131. func (s *stresser) Report() (int, int) {
  132. s.mu.Lock()
  133. defer s.mu.Unlock()
  134. // TODO: find a better way to report v3 tests
  135. return s.success, -1
  136. }
  137. type stresserV2 struct {
  138. Endpoint string
  139. KeySize int
  140. KeySuffixRange int
  141. N int
  142. // TODO: not implemented
  143. Interval time.Duration
  144. mu sync.Mutex
  145. failure int
  146. success int
  147. cancel func()
  148. }
  149. func (s *stresserV2) Stress() error {
  150. cfg := clientV2.Config{
  151. Endpoints: []string{s.Endpoint},
  152. Transport: &http.Transport{
  153. Dial: (&net.Dialer{
  154. Timeout: time.Second,
  155. KeepAlive: 30 * time.Second,
  156. }).Dial,
  157. MaxIdleConnsPerHost: s.N,
  158. },
  159. }
  160. c, err := clientV2.New(cfg)
  161. if err != nil {
  162. return err
  163. }
  164. kv := clientV2.NewKeysAPI(c)
  165. ctx, cancel := context.WithCancel(context.Background())
  166. s.cancel = cancel
  167. for i := 0; i < s.N; i++ {
  168. go func() {
  169. for {
  170. setctx, setcancel := context.WithTimeout(ctx, clientV2.DefaultRequestTimeout)
  171. key := fmt.Sprintf("foo%d", rand.Intn(s.KeySuffixRange))
  172. _, err := kv.Set(setctx, key, randStr(s.KeySize), nil)
  173. setcancel()
  174. if err == context.Canceled {
  175. return
  176. }
  177. s.mu.Lock()
  178. if err != nil {
  179. s.failure++
  180. } else {
  181. s.success++
  182. }
  183. s.mu.Unlock()
  184. }
  185. }()
  186. }
  187. <-ctx.Done()
  188. return nil
  189. }
  190. func (s *stresserV2) Cancel() {
  191. s.cancel()
  192. }
  193. func (s *stresserV2) Report() (success int, failure int) {
  194. s.mu.Lock()
  195. defer s.mu.Unlock()
  196. return s.success, s.failure
  197. }
  198. func randStr(size int) string {
  199. data := make([]byte, size)
  200. for i := 0; i < size; i++ {
  201. data[i] = byte(int('a') + rand.Intn(26))
  202. }
  203. return string(data)
  204. }