stresser.go 4.7 KB

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