stresser.go 5.3 KB

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