forked from jacexh/ultron
-
Notifications
You must be signed in to change notification settings - Fork 4
/
runner.go
241 lines (208 loc) · 5.91 KB
/
runner.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package ultron
import (
"errors"
"net"
"net/http"
"os"
"os/signal"
"sync"
"syscall"
"time"
"github.com/wosai/ultron/v2/pkg/genproto"
"go.uber.org/zap"
"google.golang.org/grpc"
)
type (
MasterRunner interface {
Launch(...grpc.ServerOption) error // 服务启动
StartPlan(Plan) error // 开始执行某个测试计划
StopPlan() // 停止当前计划
SubscribeReport(...ReportHandleFunc) // 订阅聚合报告
}
SlaveRunner interface {
Connect(string, ...grpc.DialOption) error // 连接master
SubscribeResult(...ResultHandleFunc) // 订阅Attacker的执行结果
Assign(Task) // 指派压测任务
}
LocalRunner interface {
Launch() error
Assign(Task)
SubscribeReport(...ReportHandleFunc)
SubscribeResult(...ResultHandleFunc)
StartPlan(Plan) error
StopPlan()
}
masterRunner struct {
scheduler *scheduler
plan Plan
eventbus *eventbus
supervisor *slaveSupervisor
rpc *grpc.Server
rest *http.Server
mu sync.RWMutex
}
localRunner struct {
master *masterRunner
slave *slaveRunner
}
)
func NewMasterRunner() MasterRunner {
runner := newMasterRunner()
go func(r *masterRunner) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
sig := <-sigs
Logger.Warn("caught quit signal, try to shutdown ultron master server", zap.String("signal", sig.String()))
if r.scheduler != nil {
if err := r.scheduler.stop(false); err != nil {
Logger.Error("failed to interrupt current test plan", zap.Error(err))
os.Exit(1)
}
r.eventbus.close()
}
os.Exit(0)
}(runner)
return runner
}
func NewSlaveRunner() SlaveRunner {
runner := newSlaveRunner()
go func(r *slaveRunner) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
sig := <-sigs
Logger.Warn("caught quit signal, try to disconnect to ultron server", zap.String("signal", sig.String()))
r.subscribeStream.CloseSend()
os.Exit(0)
}(runner)
return runner
}
func newMasterRunner() *masterRunner {
return &masterRunner{
eventbus: defaultEventBus,
}
}
// Launch 主线程,如果发生错误则关闭
func (r *masterRunner) Launch(opts ...grpc.ServerOption) error {
Logger.Info("loaded configurations", zap.Any("configrations", loadedOption))
serverOption := loadedOption.Server
// eventbus初始化
r.eventbus.subscribeReport(printReportToConsole(os.Stdout))
r.eventbus.subscribeReport(printJsonReport(os.Stdout))
r.eventbus.start()
start := make(chan struct{}, 1)
go func() { // http server
router := buildHTTPRouter(r)
r.rest = &http.Server{
Addr: serverOption.HTTPAddr,
Handler: router,
}
Logger.Info("ultron http server is running", zap.String("address", serverOption.HTTPAddr))
if err := r.rest.ListenAndServe(); err != nil {
Logger.Fatal("a error has occurend inside http server", zap.Error(err))
}
}()
go func() { // grpc server
lis, err := net.Listen("tcp", serverOption.GRPCAddr)
if err != nil {
Logger.Fatal("failed to launch grpc server", zap.Error(err))
}
r.rpc = grpc.NewServer(opts...)
r.supervisor = newSlaveSupervisor()
genproto.RegisterUltronAPIServer(r.rpc, r.supervisor)
Logger.Info("ultron grpc server is running", zap.String("connect_address", serverOption.GRPCAddr))
start <- struct{}{}
if err := r.rpc.Serve(lis); err != nil {
Logger.Fatal("a error has occurend inside grpc server", zap.Error(err))
}
}()
<-start
return nil
}
func (r *masterRunner) StartPlan(p Plan) error {
if p == nil {
err := errors.New("empty plan")
Logger.Error("cannot start with empty plan", zap.Error(err))
return err
}
r.mu.Lock()
if r.plan != nil && r.plan.Status() == StatusRunning {
r.mu.Unlock()
err := errors.New("cannot start a new plan before shutdown current running plan")
Logger.Error("failed to start a new plan", zap.Error(err))
return err
}
Logger.Info("start plan", zap.String("plan_name", p.Name()))
scheduler := newScheduler(r.supervisor)
r.scheduler = scheduler
r.plan = p
r.mu.Unlock()
if err := scheduler.start(p.(*plan)); err != nil {
Logger.Error("failed to start a new plan", zap.Error(err))
return err
}
go func() {
if err := scheduler.patrol(5 * time.Second); err != nil {
Logger.Warn("patrol mission is complete", zap.Error(err))
}
}()
return nil
}
func (r *masterRunner) StopPlan() {
r.mu.RLock()
if r.scheduler == nil {
r.mu.RUnlock()
return
}
r.mu.RUnlock()
if err := r.scheduler.stop(false); err != nil {
Logger.Error("failed to stop plan", zap.Error(err))
}
}
func (r *masterRunner) SubscribeReport(fns ...ReportHandleFunc) {
for _, fn := range fns {
r.eventbus.subscribeReport(fn)
}
}
var _ LocalRunner = (*localRunner)(nil)
func NewLocalRunner() LocalRunner {
runner := &localRunner{
master: newMasterRunner(),
slave: newSlaveRunner(),
}
go func(r *masterRunner) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
sig := <-sigs
Logger.Warn("caught quit signal, try to shutdown ultron master server", zap.String("signal", sig.String()))
if r.scheduler != nil {
if err := r.scheduler.stop(false); err != nil {
Logger.Error("failed to interrupt current test plan", zap.Error(err))
os.Exit(1)
}
r.eventbus.close()
}
os.Exit(0)
}(runner.master)
return runner
}
func (lr *localRunner) Launch() error {
if err := lr.master.Launch(); err != nil {
return err
}
return lr.slave.Connect(":2021", grpc.WithInsecure())
}
func (lr *localRunner) Assign(t Task) {
lr.slave.Assign(t)
}
func (lr *localRunner) SubscribeResult(fns ...ResultHandleFunc) {
lr.slave.SubscribeResult(fns...)
}
func (lr *localRunner) SubscribeReport(fns ...ReportHandleFunc) {
lr.master.SubscribeReport(fns...)
}
func (lr *localRunner) StartPlan(p Plan) error {
return lr.master.StartPlan(p)
}
func (lr *localRunner) StopPlan() {
lr.master.StopPlan()
}