-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprophet_coordinator.go
195 lines (161 loc) · 3.53 KB
/
prophet_coordinator.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
package prophet
import (
"context"
"sync"
"time"
)
// Coordinator resource coordinator
type Coordinator struct {
sync.RWMutex
cfg *Cfg
rt *Runtime
scaleChecker *replicaScaleChecker
checker *replicaChecker
limiter *scheduleLimiter
schedulers map[string]*scheduleController
opts map[uint64]Operator
runner *Runner
tasks []uint64
running bool
}
func newCoordinator(cfg *Cfg, runner *Runner, rt *Runtime) *Coordinator {
c := new(Coordinator)
c.limiter = newScheduleLimiter()
c.checker = newReplicaChecker(cfg, rt)
c.scaleChecker = newReplicaScaleChecker(rt, cfg.EnableScaleOnNewStore)
c.opts = make(map[uint64]Operator)
c.schedulers = make(map[string]*scheduleController)
c.runner = runner
c.rt = rt
c.cfg = cfg
return c
}
func (c *Coordinator) start() {
if c.running {
log.Warningf("coordinator is already started.")
return
}
for _, s := range c.cfg.Schedulers {
c.addScheduler(s)
}
c.running = true
}
func (c *Coordinator) stop() {
c.Lock()
defer c.Unlock()
c.running = false
for _, id := range c.tasks {
c.runner.StopCancelableTask(id)
}
}
func (c *Coordinator) isRunning() bool {
c.RLock()
value := c.running
c.RUnlock()
return value
}
func (c *Coordinator) addScheduler(scheduler Scheduler) error {
c.Lock()
defer c.Unlock()
if _, ok := c.schedulers[scheduler.Name()]; ok {
return ErrSchedulerExisted
}
s := newScheduleController(c, scheduler)
if err := s.Prepare(c.rt); err != nil {
return err
}
id, err := c.runner.RunCancelableTask(func(ctx context.Context) {
c.runScheduler(ctx, s)
})
if err != nil {
return err
}
c.tasks = append(c.tasks, id)
c.schedulers[s.Name()] = s
return nil
}
func (c *Coordinator) addOperator(op Operator) bool {
c.Lock()
defer c.Unlock()
id := op.ResourceID()
if _, ok := c.opts[id]; ok {
return false
}
c.limiter.addOperator(op)
c.opts[id] = op
return true
}
func (c *Coordinator) getOperator(id uint64) Operator {
c.RLock()
defer c.RUnlock()
return c.opts[id]
}
func (c *Coordinator) removeOperator(op Operator) {
c.Lock()
defer c.Unlock()
id := op.ResourceID()
c.limiter.removeOperator(op)
delete(c.opts, id)
}
func (c *Coordinator) runScheduler(ctx context.Context, s *scheduleController) {
defer s.Cleanup(c.rt)
timer := time.NewTimer(s.Interval())
defer timer.Stop()
for {
select {
case <-ctx.Done():
log.Infof("scheduler %s stopped", s.Name())
return
case <-timer.C:
timer.Reset(s.Interval())
s.Lock()
if !s.AllowSchedule() {
s.Unlock()
continue
}
for i := 0; i < c.cfg.MaxScheduleRetries; i++ {
op := s.Schedule(c.rt)
if op == nil {
continue
}
if c.addOperator(op) {
break
}
}
s.Unlock()
}
}
}
// dispatch is used for coordinator resource,
// it will coordinator when the heartbeat arrives
func (c *Coordinator) dispatch(target *ResourceRuntime) *resourceHeartbeatRsp {
log.Debugf("dispatch resource %d, %+v",
target.meta.ID(),
target.meta)
// Check existed operator.
if op := c.getOperator(target.meta.ID()); op != nil {
res, finished := op.Do(target)
if !finished {
return res
}
c.removeOperator(op)
}
// Check replica operator.
if c.limiter.operatorCount(ReplicaKind) >= c.cfg.MaxScheduleReplica {
return nil
}
if op := c.checker.Check(target); op != nil {
if c.addOperator(op) {
res, _ := op.Do(target)
return res
}
}
// Check scale-out all resources
if op := c.scaleChecker.Check(target); op != nil {
if c.addOperator(op) {
res, _ := op.Do(target)
return res
}
}
return nil
}