-
Notifications
You must be signed in to change notification settings - Fork 914
/
monitor.go
232 lines (184 loc) · 6.01 KB
/
monitor.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
package orchestrator
import (
"fmt"
"time"
"github.com/botlabs-gg/yagpdb/v2/lib/dshardorchestrator"
)
type monitor struct {
orchestrator *Orchestrator
started time.Time
stopChan chan bool
lastTimeLaunchedNode time.Time
lastTimeStartedShardBucket time.Time
shardsLastSeenTimes []time.Time
}
func (mon *monitor) run() {
mon.started = time.Now()
ticker := time.NewTicker(time.Second)
// allows time for shards to connect before we start launching stuff
time.Sleep(time.Second * 10)
for {
select {
case <-ticker.C:
mon.tick()
case <-mon.stopChan:
return
}
}
}
func (mon *monitor) stop() {
close(mon.stopChan)
}
func (mon *monitor) ensureTotalShards() int {
mon.orchestrator.mu.Lock()
defer mon.orchestrator.mu.Unlock()
// totalshards has been set already
totalShards := mon.orchestrator.totalShards
if totalShards != 0 {
return totalShards
}
// has not been set, try to set it
totalShards, err := mon.orchestrator.getTotalShardCount()
if err != nil {
mon.orchestrator.Log(dshardorchestrator.LogError, err, "monitor: failed fetching total shard count, retrying in a second")
return 0
}
if totalShards == 0 {
mon.orchestrator.Log(dshardorchestrator.LogError, err, "monitor: ShardCountProvider returned 0 without error, retrying in a second")
return 0
}
// successfully set it
mon.orchestrator.totalShards = totalShards
mon.orchestrator.Log(dshardorchestrator.LogInfo, nil, fmt.Sprintf("monitor: set total shard count to %d", totalShards))
return totalShards
}
func (mon *monitor) tick() {
if !mon.orchestrator.EnsureAllShardsRunning {
// currently this is the only purpose of the monitor, it may be extended to perform more as it could be a reliable way of handling a bunch of things
return
}
totalShards := mon.ensureTotalShards()
if totalShards == 0 {
return
}
if time.Since(mon.lastTimeStartedShardBucket) < time.Second*5 {
return
}
if mon.shardsLastSeenTimes == nil {
mon.shardsLastSeenTimes = make([]time.Time, totalShards)
for i := range mon.shardsLastSeenTimes {
mon.shardsLastSeenTimes[i] = time.Now()
}
}
runningShards := make([]bool, totalShards)
// find the disconnect times for all shards that have disconnected
fullNodeStatuses := mon.orchestrator.GetFullNodesStatus()
for _, ns := range fullNodeStatuses {
for _, s := range ns.Shards {
if !ns.Connected {
continue
}
runningShards[s] = true
mon.shardsLastSeenTimes[s] = time.Now()
}
}
// find out which shards to start, the key is the bucket
shardsToStart := make(map[int][]int)
// shardsToStart := make([]int, 0)
nToStart := 0
OUTER:
for i, lastTimeConnected := range mon.shardsLastSeenTimes {
if runningShards[i] || time.Since(lastTimeConnected) < mon.orchestrator.MaxNodeDowntimeBeforeRestart {
continue
}
// check if this shard is in a shard migration, in which case ignore it
for _, ns := range fullNodeStatuses {
if ns.MigratingShard == i && (ns.MigratingFrom != "" || ns.MigratingTo != "") {
continue OUTER
}
}
// if were running multi-host mode, check to make sure we don't start a shard were not responsible for
if !mon.orchestrator.isResponsibleForShard(i) {
continue
}
bucket := mon.nodeSlotForShard(i)
shardsToStart[bucket] = append(shardsToStart[bucket], i)
nToStart++
}
if len(shardsToStart) < 1 {
return
}
mon.orchestrator.Log(dshardorchestrator.LogInfo, nil, fmt.Sprintf("monitor: need to start %d shards...", nToStart))
// start one
// reason we don't start them all at once is that there's no real point in that, you can only start a shard every 5 seconds anyways
// attempt to start it on a existing node before starting a new node
for _, v := range fullNodeStatuses {
if !v.Connected || !v.SessionEstablished || v.Blacklisted {
continue
}
if len(v.Shards) >= mon.orchestrator.MaxShardsPerNode {
continue
}
canStartBucket := -1
FINDAVAILBUCKET:
for bucket := range shardsToStart {
for _, vs := range v.Shards {
vb := mon.nodeSlotForShard(vs)
if bucket != vb {
// mistmatched buckets, can't start this shard here
continue FINDAVAILBUCKET
}
}
// no mismatched shard buckets
canStartBucket = bucket
}
mon.orchestrator.Log(dshardorchestrator.LogInfo, nil, fmt.Sprintf("monitor: canStartBucket: %d", canStartBucket))
if canStartBucket == -1 {
continue
}
starting := shardsToStart[canStartBucket]
if mon.orchestrator.ShardBucketSize != 0 && len(starting) > mon.orchestrator.ShardBucketSize {
starting = starting[:mon.orchestrator.ShardBucketSize]
}
mon.orchestrator.Log(dshardorchestrator.LogInfo, nil, fmt.Sprintf("monitor: starting: %v", starting))
err := mon.orchestrator.StartShards(v.ID, starting...)
if err != nil {
mon.orchestrator.Log(dshardorchestrator.LogError, err, "monitor: failed starting shards")
}
mon.lastTimeStartedShardBucket = time.Now()
return
}
// if we got here that means that there's no more nodes available, so start one
if time.Since(mon.lastTimeLaunchedNode) < time.Second*30 {
// allow 5 seconds wait time in between each node launch
mon.orchestrator.Log(dshardorchestrator.LogDebug, nil, "monitor: can't start new node, on cooldown")
return
}
if mon.orchestrator.NodeLauncher == nil {
mon.orchestrator.Log(dshardorchestrator.LogError, nil, "monitor: can't start new node, no node launcher set...")
return
}
_, err := mon.orchestrator.NodeLauncher.LaunchNewNode()
if err != nil {
mon.orchestrator.Log(dshardorchestrator.LogError, err, "monitor: failed starting a new node")
return
}
mon.lastTimeLaunchedNode = time.Now()
}
func (mon *monitor) bucketForShard(shard int) int {
bs := mon.orchestrator.ShardBucketSize
if bs > 1 {
return shard / bs
}
// not using buckets
return 0
}
// same as bucketForShard but also applies the bucketsPerNode to for assigning buckets to nodes
func (mon *monitor) nodeSlotForShard(shard int) int {
bucket := mon.bucketForShard(shard)
if mon.orchestrator.BucketsPerNode != 0 {
return bucket / mon.orchestrator.BucketsPerNode
}
// buckets per node is not set, ignore
return bucket
}