stresser.go 5.2 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. "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. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  27. "golang.org/x/net/context"
  28. "google.golang.org/grpc"
  29. "google.golang.org/grpc/grpclog"
  30. "google.golang.org/grpc/transport"
  31. )
  32. func init() {
  33. grpclog.SetLogger(log.New(ioutil.Discard, "", 0))
  34. }
  35. type Stresser interface {
  36. // Stress starts to stress the etcd cluster
  37. Stress() error
  38. // Cancel cancels the stress test on the etcd cluster
  39. Cancel()
  40. // Report reports the success and failure of the stress test
  41. Report() (success int, failure int)
  42. }
  43. type stresser struct {
  44. Endpoint string
  45. KeySize int
  46. KeySuffixRange int
  47. N int
  48. mu sync.Mutex
  49. wg *sync.WaitGroup
  50. cancel func()
  51. conn *grpc.ClientConn
  52. success int
  53. }
  54. func (s *stresser) Stress() error {
  55. // TODO: add backoff option
  56. conn, err := grpc.Dial(s.Endpoint, grpc.WithInsecure())
  57. if err != nil {
  58. return fmt.Errorf("%v (%s)", err, s.Endpoint)
  59. }
  60. defer conn.Close()
  61. ctx, cancel := context.WithCancel(context.Background())
  62. wg := &sync.WaitGroup{}
  63. wg.Add(s.N)
  64. s.mu.Lock()
  65. s.conn = conn
  66. s.cancel = cancel
  67. s.wg = wg
  68. s.mu.Unlock()
  69. kvc := pb.NewKVClient(conn)
  70. for i := 0; i < s.N; i++ {
  71. go func(i int) {
  72. defer wg.Done()
  73. for {
  74. // TODO: 10-second is enough timeout to cover leader failure
  75. // and immediate leader election. Find out what other cases this
  76. // could be timed out.
  77. putctx, putcancel := context.WithTimeout(ctx, 10*time.Second)
  78. _, err := kvc.Put(putctx, &pb.PutRequest{
  79. Key: []byte(fmt.Sprintf("foo%d", rand.Intn(s.KeySuffixRange))),
  80. Value: []byte(randStr(s.KeySize)),
  81. })
  82. putcancel()
  83. if err != nil {
  84. shouldContinue := false
  85. switch grpc.ErrorDesc(err) {
  86. case context.DeadlineExceeded.Error():
  87. // This retries when request is triggered at the same time as
  88. // leader failure. When we terminate the leader, the request to
  89. // that leader cannot be processed, and times out. Also requests
  90. // to followers cannot be forwarded to the old leader, so timing out
  91. // as well. We want to keep stressing until the cluster elects a
  92. // new leader and start processing requests again.
  93. shouldContinue = true
  94. case etcdserver.ErrStopped.Error():
  95. // one of the etcd nodes stopped from failure injection
  96. shouldContinue = true
  97. case transport.ErrConnClosing.Desc:
  98. // server closed the transport (failure injected node)
  99. shouldContinue = true
  100. // default:
  101. // errors from stresser.Cancel method:
  102. // rpc error: code = 1 desc = context canceled (type grpc.rpcError)
  103. // rpc error: code = 2 desc = grpc: the client connection is closing (type grpc.rpcError)
  104. }
  105. if shouldContinue {
  106. continue
  107. }
  108. return
  109. }
  110. s.mu.Lock()
  111. s.success++
  112. s.mu.Unlock()
  113. }
  114. }(i)
  115. }
  116. <-ctx.Done()
  117. return nil
  118. }
  119. func (s *stresser) Cancel() {
  120. s.mu.Lock()
  121. cancel, conn, wg := s.cancel, s.conn, s.wg
  122. s.mu.Unlock()
  123. cancel()
  124. wg.Wait()
  125. conn.Close()
  126. }
  127. func (s *stresser) Report() (int, int) {
  128. s.mu.Lock()
  129. defer s.mu.Unlock()
  130. // TODO: find a better way to report v3 tests
  131. return s.success, -1
  132. }
  133. type stresserV2 struct {
  134. Endpoint string
  135. KeySize int
  136. KeySuffixRange int
  137. N int
  138. // TODO: not implemented
  139. Interval time.Duration
  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. }