stresser_runner.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. "os/exec"
  19. "syscall"
  20. "github.com/coreos/etcd/functional/rpcpb"
  21. "go.uber.org/zap"
  22. "golang.org/x/time/rate"
  23. )
  24. type runnerStresser struct {
  25. stype rpcpb.Stresser
  26. etcdClientEndpoint string
  27. lg *zap.Logger
  28. cmd *exec.Cmd
  29. cmdStr string
  30. args []string
  31. rl *rate.Limiter
  32. reqRate int
  33. errc chan error
  34. donec chan struct{}
  35. }
  36. func newRunnerStresser(
  37. stype rpcpb.Stresser,
  38. ep string,
  39. lg *zap.Logger,
  40. cmdStr string,
  41. args []string,
  42. rl *rate.Limiter,
  43. reqRate int,
  44. ) *runnerStresser {
  45. rl.SetLimit(rl.Limit() - rate.Limit(reqRate))
  46. return &runnerStresser{
  47. stype: stype,
  48. etcdClientEndpoint: ep,
  49. cmdStr: cmdStr,
  50. args: args,
  51. rl: rl,
  52. reqRate: reqRate,
  53. errc: make(chan error, 1),
  54. donec: make(chan struct{}),
  55. }
  56. }
  57. func (rs *runnerStresser) setupOnce() (err error) {
  58. if rs.cmd != nil {
  59. return nil
  60. }
  61. rs.cmd = exec.Command(rs.cmdStr, rs.args...)
  62. stderr, err := rs.cmd.StderrPipe()
  63. if err != nil {
  64. return err
  65. }
  66. go func() {
  67. defer close(rs.donec)
  68. out, err := ioutil.ReadAll(stderr)
  69. if err != nil {
  70. rs.errc <- err
  71. } else {
  72. rs.errc <- fmt.Errorf("(%v %v) stderr %v", rs.cmdStr, rs.args, string(out))
  73. }
  74. }()
  75. return rs.cmd.Start()
  76. }
  77. func (rs *runnerStresser) Stress() (err error) {
  78. rs.lg.Info(
  79. "stress START",
  80. zap.String("stress-type", rs.stype.String()),
  81. )
  82. if err = rs.setupOnce(); err != nil {
  83. return err
  84. }
  85. return syscall.Kill(rs.cmd.Process.Pid, syscall.SIGCONT)
  86. }
  87. func (rs *runnerStresser) Pause() map[string]int {
  88. rs.lg.Info(
  89. "stress STOP",
  90. zap.String("stress-type", rs.stype.String()),
  91. )
  92. syscall.Kill(rs.cmd.Process.Pid, syscall.SIGSTOP)
  93. return nil
  94. }
  95. func (rs *runnerStresser) Close() map[string]int {
  96. syscall.Kill(rs.cmd.Process.Pid, syscall.SIGINT)
  97. rs.cmd.Wait()
  98. <-rs.donec
  99. rs.rl.SetLimit(rs.rl.Limit() + rate.Limit(rs.reqRate))
  100. return nil
  101. }
  102. func (rs *runnerStresser) ModifiedKeys() int64 {
  103. return 1
  104. }