stresser_runner.go 2.6 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. "go.etcd.io/etcd/functional/rpcpb"
  21. "go.uber.org/zap"
  22. "golang.org/x/time/rate"
  23. )
  24. type runnerStresser struct {
  25. stype rpcpb.StresserType
  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.StresserType,
  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. lg: lg,
  50. cmdStr: cmdStr,
  51. args: args,
  52. rl: rl,
  53. reqRate: reqRate,
  54. errc: make(chan error, 1),
  55. donec: make(chan struct{}),
  56. }
  57. }
  58. func (rs *runnerStresser) setupOnce() (err error) {
  59. if rs.cmd != nil {
  60. return nil
  61. }
  62. rs.cmd = exec.Command(rs.cmdStr, rs.args...)
  63. stderr, err := rs.cmd.StderrPipe()
  64. if err != nil {
  65. return err
  66. }
  67. go func() {
  68. defer close(rs.donec)
  69. out, err := ioutil.ReadAll(stderr)
  70. if err != nil {
  71. rs.errc <- err
  72. } else {
  73. rs.errc <- fmt.Errorf("(%v %v) stderr %v", rs.cmdStr, rs.args, string(out))
  74. }
  75. }()
  76. return rs.cmd.Start()
  77. }
  78. func (rs *runnerStresser) Stress() (err error) {
  79. rs.lg.Info(
  80. "stress START",
  81. zap.String("stress-type", rs.stype.String()),
  82. )
  83. if err = rs.setupOnce(); err != nil {
  84. return err
  85. }
  86. return syscall.Kill(rs.cmd.Process.Pid, syscall.SIGCONT)
  87. }
  88. func (rs *runnerStresser) Pause() map[string]int {
  89. rs.lg.Info(
  90. "stress STOP",
  91. zap.String("stress-type", rs.stype.String()),
  92. )
  93. syscall.Kill(rs.cmd.Process.Pid, syscall.SIGSTOP)
  94. return nil
  95. }
  96. func (rs *runnerStresser) Close() map[string]int {
  97. syscall.Kill(rs.cmd.Process.Pid, syscall.SIGINT)
  98. rs.cmd.Wait()
  99. <-rs.donec
  100. rs.rl.SetLimit(rs.rl.Limit() + rate.Limit(rs.reqRate))
  101. return nil
  102. }
  103. func (rs *runnerStresser) ModifiedKeys() int64 {
  104. return 1
  105. }