forked from jacexh/ultron
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscheduler.go
170 lines (139 loc) · 3.97 KB
/
scheduler.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
package ultron
import (
"context"
"errors"
"fmt"
"sync"
"time"
"github.com/wosai/ultron/v2/pkg/statistics"
"go.uber.org/zap"
)
type (
// scheduler master进程任务调度者
scheduler struct {
ctx context.Context
cancel context.CancelFunc
plan *plan
supervisor *slaveSupervisor
eventbus reportBus
mu sync.RWMutex
}
)
func newScheduler(sup *slaveSupervisor) *scheduler {
return &scheduler{
supervisor: sup,
eventbus: defaultEventBus,
}
}
func (s *scheduler) start(plan *plan) error {
if plan == nil {
return errors.New("cannot start with an empty plan")
}
if err := plan.check(); err != nil {
return err
}
s.mu.Lock()
s.ctx, s.cancel = context.WithCancel(context.Background())
if err := s.supervisor.StartNewPlan(s.ctx, plan.Name()); err != nil {
s.mu.Unlock()
return err
}
s.plan = plan
s.mu.Unlock()
_, _, stage, err := plan.stopCurrentAndStartNext(-1, statistics.SummaryReport{})
if err != nil {
return err
}
if err := s.supervisor.NextStage(s.ctx, stage.GetStrategy(), stage.GetTimer()); err != nil {
return err
}
return nil
}
func (s *scheduler) stop(done bool) error {
s.mu.RLock()
plan := s.plan
cancel := s.cancel
s.mu.RUnlock()
if plan == nil {
return nil
}
ps := plan.Status()
switch {
case ps == StatusFinished && done: // 正常结束
case ps == StatusRunning && !done: // 被中断
plan.interrupt()
case !done && (ps == StatusReady || ps == StatusFinished || ps == StatusInterrupted):
return nil
default:
Logger.Warn("unexpected parameters", zap.Bool("done", done), zap.Int("plan_status", int(ps)))
return errors.New("unknown status")
}
var err error
if err = s.supervisor.Stop(s.ctx, done); err != nil {
Logger.Warn("failed to stop slaves", zap.Error(err))
}
cancel()
Logger.Info("canceled all running jobs")
report, aggErr := s.supervisor.Aggregate(true, statistics.Tag{Key: KeyPlan, Value: plan.Name()})
switch {
case err == nil && aggErr != nil:
return aggErr
case err != nil && aggErr == nil:
return err
case err != nil && aggErr != nil:
return fmt.Errorf("recent error: %w last error:%s", aggErr, err.Error())
default:
s.eventbus.publishReport(report)
return nil
}
}
func (s *scheduler) nextStage(stage Stage) error {
return s.supervisor.NextStage(s.ctx, stage.GetStrategy(), stage.GetTimer())
}
// patrol scheduler核心逻辑
func (s *scheduler) patrol(every time.Duration) error {
ticker := time.NewTicker(every)
defer ticker.Stop()
s.mu.RLock()
plan := s.plan
ctx := s.ctx
s.mu.RUnlock()
if plan.Status() != StatusRunning {
return errors.New("failed to patrol cause the plan is not running")
}
stageIndex, _ := plan.Current()
patrol:
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
report, err := s.supervisor.Aggregate(false, statistics.Tag{Key: KeyPlan, Value: plan.Name()})
if err != nil {
Logger.Warn("failed to aggregate stats report", zap.Error(err))
continue patrol
}
s.eventbus.publishReport(report)
stopped, next, stage, err := plan.stopCurrentAndStartNext(stageIndex, report)
switch {
case err != nil && errors.Is(err, ErrPlanClosed) && stopped: // 当前在最后一个阶段并且执行完成了,此时plan已经完成
Logger.Info("current plan is closed")
s.stop(true) // TODO: 是否还要做点什么?不做的话会拿到下一次聚合报告?
return nil
case err != nil && errors.Is(err, ErrPlanClosed) && !stopped: // 计划早已经结束,不干了
Logger.Info("this plan is complete, stop patrol")
return nil
case err != nil && !errors.Is(err, ErrPlanClosed):
Logger.Error("occur error on checking the test plan", zap.Error(err))
continue patrol
case err == nil && stopped: // 下一阶段
Logger.Info("start the next stage")
if err := s.nextStage(stage); err != nil {
Logger.Error("failed to send the configurations of next stage to slaves", zap.Error(err))
}
stageIndex = next
default: // 继续巡查
}
}
}
}