2018-03-28 04:27:28 +03:00
|
|
|
// Copyright 2018 The etcd Authors
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package tester
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os/exec"
|
|
|
|
"syscall"
|
|
|
|
|
2019-05-29 00:59:02 +03:00
|
|
|
"go.etcd.io/etcd/functional/rpcpb"
|
2018-04-09 20:40:27 +03:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2018-03-28 04:27:28 +03:00
|
|
|
"golang.org/x/time/rate"
|
|
|
|
)
|
|
|
|
|
|
|
|
type runnerStresser struct {
|
2018-05-09 02:18:45 +03:00
|
|
|
stype rpcpb.StresserType
|
2018-04-12 05:22:54 +03:00
|
|
|
etcdClientEndpoint string
|
|
|
|
lg *zap.Logger
|
2018-04-09 20:40:27 +03:00
|
|
|
|
2018-03-28 04:27:28 +03:00
|
|
|
cmd *exec.Cmd
|
|
|
|
cmdStr string
|
|
|
|
args []string
|
|
|
|
rl *rate.Limiter
|
|
|
|
reqRate int
|
|
|
|
|
|
|
|
errc chan error
|
|
|
|
donec chan struct{}
|
|
|
|
}
|
|
|
|
|
2018-04-09 20:40:27 +03:00
|
|
|
func newRunnerStresser(
|
2018-05-09 02:18:45 +03:00
|
|
|
stype rpcpb.StresserType,
|
2018-04-12 05:22:54 +03:00
|
|
|
ep string,
|
2018-04-09 20:40:27 +03:00
|
|
|
lg *zap.Logger,
|
|
|
|
cmdStr string,
|
|
|
|
args []string,
|
|
|
|
rl *rate.Limiter,
|
|
|
|
reqRate int,
|
|
|
|
) *runnerStresser {
|
2018-03-28 04:27:28 +03:00
|
|
|
rl.SetLimit(rl.Limit() - rate.Limit(reqRate))
|
|
|
|
return &runnerStresser{
|
2018-04-12 05:22:54 +03:00
|
|
|
stype: stype,
|
|
|
|
etcdClientEndpoint: ep,
|
2018-05-09 23:52:54 +03:00
|
|
|
lg: lg,
|
2018-04-12 05:22:54 +03:00
|
|
|
cmdStr: cmdStr,
|
|
|
|
args: args,
|
|
|
|
rl: rl,
|
|
|
|
reqRate: reqRate,
|
|
|
|
errc: make(chan error, 1),
|
|
|
|
donec: make(chan struct{}),
|
2018-03-28 04:27:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *runnerStresser) setupOnce() (err error) {
|
|
|
|
if rs.cmd != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
rs.cmd = exec.Command(rs.cmdStr, rs.args...)
|
|
|
|
stderr, err := rs.cmd.StderrPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(rs.donec)
|
|
|
|
out, err := ioutil.ReadAll(stderr)
|
|
|
|
if err != nil {
|
|
|
|
rs.errc <- err
|
|
|
|
} else {
|
|
|
|
rs.errc <- fmt.Errorf("(%v %v) stderr %v", rs.cmdStr, rs.args, string(out))
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return rs.cmd.Start()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *runnerStresser) Stress() (err error) {
|
2018-04-09 20:40:27 +03:00
|
|
|
rs.lg.Info(
|
|
|
|
"stress START",
|
|
|
|
zap.String("stress-type", rs.stype.String()),
|
|
|
|
)
|
2018-03-28 04:27:28 +03:00
|
|
|
if err = rs.setupOnce(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return syscall.Kill(rs.cmd.Process.Pid, syscall.SIGCONT)
|
|
|
|
}
|
|
|
|
|
2018-04-05 01:48:07 +03:00
|
|
|
func (rs *runnerStresser) Pause() map[string]int {
|
2018-04-09 20:40:27 +03:00
|
|
|
rs.lg.Info(
|
|
|
|
"stress STOP",
|
|
|
|
zap.String("stress-type", rs.stype.String()),
|
|
|
|
)
|
2018-03-28 04:27:28 +03:00
|
|
|
syscall.Kill(rs.cmd.Process.Pid, syscall.SIGSTOP)
|
2018-04-05 01:48:07 +03:00
|
|
|
return nil
|
2018-03-28 04:27:28 +03:00
|
|
|
}
|
|
|
|
|
2018-04-05 01:48:07 +03:00
|
|
|
func (rs *runnerStresser) Close() map[string]int {
|
2018-03-28 04:27:28 +03:00
|
|
|
syscall.Kill(rs.cmd.Process.Pid, syscall.SIGINT)
|
|
|
|
rs.cmd.Wait()
|
|
|
|
<-rs.donec
|
|
|
|
rs.rl.SetLimit(rs.rl.Limit() + rate.Limit(rs.reqRate))
|
2018-04-05 01:48:07 +03:00
|
|
|
return nil
|
2018-03-28 04:27:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (rs *runnerStresser) ModifiedKeys() int64 {
|
|
|
|
return 1
|
|
|
|
}
|