stress_runner.go 2.5 KB

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