forked from botlabs-gg/yagpdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
slaveimpl.go
206 lines (161 loc) · 4.19 KB
/
slaveimpl.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
package bot
import (
"github.com/jonas747/dutil/dstate"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/master"
"github.com/jonas747/yagpdb/master/slave"
"github.com/sirupsen/logrus"
"os"
"runtime"
"runtime/debug"
"sync"
"sync/atomic"
"time"
)
// Slave implementation
type SlaveImpl struct{}
var _ slave.Bot = (*SlaveImpl)(nil)
func (s *SlaveImpl) FullStart() {
logrus.Println("Starting full start")
stateLock.Lock()
stateTmp := state
state = StateRunningWithMaster
stateLock.Unlock()
if stateTmp != StateSoftStarting {
InitPlugins()
go ShardManager.Start()
go MonitorLoading()
}
}
func (s *SlaveImpl) SoftStart() {
logrus.Println("Starting soft start")
stateLock.Lock()
state = StateSoftStarting
stateLock.Unlock()
InitPlugins()
go ShardManager.Start()
}
func (s *SlaveImpl) Shutdown() {
ShardManager.StopAll()
os.Exit(1)
}
func (s *SlaveImpl) StartShardTransferFrom() int {
stateLock.Lock()
state = StateShardMigrationFrom
stateLock.Unlock()
var wg sync.WaitGroup
StopAllPlugins(&wg)
wg.Wait()
return ShardManager.GetNumShards()
}
func (s *SlaveImpl) StartShardTransferTo(numShards int) {
ShardManager.SetNumShards(numShards)
InitPlugins()
err := ShardManager.Init()
if err != nil {
panic("Failed initializing shard manager: " + err.Error())
}
stateLock.Lock()
state = StateShardMigrationTo
stateLock.Unlock()
atomic.StoreInt32(botStartedFired, 1)
}
func (s *SlaveImpl) StopShard(shard int) (sessionID string, sequence int64) {
err := ShardManager.Sessions[shard].Close()
if err != nil {
logrus.WithError(err).Error("Failed stopping shard: ", err)
}
// Wait a second to be sure we dont have any event handlers still running populating the state for this shard
time.Sleep(time.Second)
s.SendGuilds(shard)
sessionID, sequence = ShardManager.Sessions[shard].GatewayManager.GetSessionInfo()
return
}
func (s *SlaveImpl) SendGuilds(shard int) {
numShards := ShardManager.GetNumShards()
started := time.Now()
// Send the guilds on this shard
guildsToSend := make([]*dstate.GuildState, 0)
State.RLock()
for _, v := range State.Guilds {
shardID := (v.ID >> 22) % int64(numShards)
if int(shardID) == shard {
guildsToSend = append(guildsToSend, v)
}
}
State.RUnlock()
workChan := make(chan *dstate.GuildState)
var wg sync.WaitGroup
// To speed this up we use multiple workers, this has to be done in a relatively short timespan otherwise we won't be able to resume
worker := func() {
for gs := range workChan {
State.Lock()
delete(State.Guilds, gs.ID)
State.Unlock()
gs.RLock()
channels := make([]int64, 0, len(gs.Channels))
for _, c := range gs.Channels {
channels = append(channels, c.ID)
}
SlaveClient.Send(master.EvtGuildState, &master.GuildStateData{GuildState: gs}, true)
gs.RUnlock()
State.Lock()
for _, c := range channels {
delete(State.Channels, c)
}
State.Unlock()
}
wg.Done()
}
for i := 0; i < 10; i++ {
wg.Add(1)
go worker()
}
for _, v := range guildsToSend {
workChan <- v
}
close(workChan)
wg.Wait()
runtime.GC()
debug.FreeOSMemory()
logrus.Println("Took ", time.Since(started), " to transfer ", len(guildsToSend), "guildstates")
}
func (s *SlaveImpl) StartShard(shard int, sessionID string, sequence int64) {
ShardManager.Sessions[shard].GatewayManager.SetSessionInfo(sessionID, sequence)
err := ShardManager.Sessions[shard].GatewayManager.Open()
if err != nil {
logrus.WithError(err).Error("Failed migrating shard")
}
numShards := ShardManager.GetNumShards()
if numShards-1 == shard {
// Done!
logrus.Println("shard migration complete!")
stateLock.Lock()
state = StateRunningWithMaster
stateLock.Unlock()
BotStarted()
}
}
func (s *SlaveImpl) LoadGuildState(gs *master.GuildStateData) {
guild := gs.GuildState
for _, c := range guild.Channels {
c.Owner = guild
c.Guild = guild
}
for _, m := range guild.Members {
m.Guild = guild
}
State.Lock()
State.Guilds[guild.ID] = guild
for _, c := range guild.Channels {
State.Channels[c.ID] = c
}
State.Unlock()
for _, v := range common.Plugins {
if guildMigrationHandler, ok := v.(ShardMigrationHandler); ok {
if ok {
guildMigrationHandler.GuildMigrated(guild, true)
}
}
}
}