-
Notifications
You must be signed in to change notification settings - Fork 938
/
migration.go
338 lines (273 loc) · 8.62 KB
/
migration.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
package serverstats
import (
"emperror.dev/errors"
"github.com/jonas747/yagpdb/common"
"github.com/jonas747/yagpdb/premium"
"time"
)
func StartMigrationToV2Format() error {
existing, err := GetMigrationV2Progress()
if err != nil {
return errors.WithStackIf(err)
}
if existing.MsgPeriods.IsRunning() || existing.MemberPeriods.IsRunning() {
return errors.New("Migration already running")
}
msgPeriodsExists, err := common.TableExists("server_stats_periods")
if err != nil {
return errors.WithStackIf(err)
}
memberPeriodsExist, err := common.TableExists("server_stats_member_periods")
if err != nil {
return errors.WithStackIf(err)
}
if !msgPeriodsExists || !memberPeriodsExist {
logger.Info("Attempted migration without the old v1 tables existing")
return nil
}
premiumGuilds, err := premium.AllGuildsOncePremium()
if err != nil {
return errors.WithStackIf(err)
}
go runV2Migration(premiumGuilds, existing)
return nil
}
func runV2Migration(premiumGuilds map[int64]time.Time, lastProgress *MigrationProgress) {
err := migrateV2Chunk("serverstats_migration_v2_progress_msgs", lastProgress.MsgPeriods.LastID, migrateChunkV2Messages)
// err := runMsgMigrationV2(lastProgress.MsgPeriods.LastID)
if err != nil {
logger.WithError(err).Error("Failed running message v1 -> v2 table migration")
return
}
err = migrateV2Chunk("serverstats_migration_v2_progress_members", lastProgress.MemberPeriods.LastID, migrateChunkV2Members)
// err = runMemberMigrationV2(premiumGuilds, lastProgress.MsgPeriods.LastID)
if err != nil {
logger.WithError(err).Error("Failed running member v1 -> v2 table migration")
return
}
}
func migrateV2Chunk(name string, lastID int64, f func(lastID int64, premiumGuilds map[int64]time.Time) (newLastID int64, err error)) error {
defer func() {
updateSubProgress(name, lastID, false)
}()
for {
premiumGuilds, err := premium.AllGuildsOncePremium()
if err != nil {
return errors.WithStackIf(err)
}
started := time.Now()
newLastID, err := f(lastID, premiumGuilds)
if err != nil {
return errors.WithStackIf(err)
}
logger.Infof("Took %s to migrate chunk %s:%d to v2 format", time.Since(started), name, newLastID)
if newLastID == lastID {
break
}
lastID = newLastID
time.Sleep(time.Second)
}
return nil
}
func migrateChunkV2Messages(lastID int64, premiumGuilds map[int64]time.Time) (newLastID int64, err error) {
const qGetOld = `SELECT id, started, guild_id, count FROM server_stats_periods
WHERE id > $1 ORDER BY ID ASC LIMIT 5000;`
const qSetNew = `INSERT INTO server_stats_periods_compressed
(guild_id, t, premium, num_messages, num_members, max_online, joins, leaves, max_voice)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
ON CONFLICT (guild_id, t) DO UPDATE
SET num_messages = server_stats_periods_compressed.num_messages + $4`
// get a chunk of old rows
rows, err := common.PQ.Query(qGetOld, lastID)
if err != nil {
return lastID, errors.WithStackIf(err)
}
// sum them up in this map
guildSums := make(map[time.Time]map[int64]int)
for rows.Next() {
var t time.Time
var guildID int64
var count int
err = rows.Scan(&lastID, &t, &guildID, &count)
if err != nil {
rows.Close()
return lastID, errors.WithStackIf(err)
}
t = t.Truncate(time.Hour * 24)
if tBucket, ok := guildSums[t]; ok {
tBucket[guildID] = tBucket[guildID] + count
} else {
guildSums[t] = make(map[int64]int)
guildSums[t][guildID] = count
}
}
tx, err := common.PQ.Begin()
if err != nil {
return lastID, errors.WithStackIf(err)
}
// convert them to the new format
for t, guilds := range guildSums {
for g, count := range guilds {
_, isPremium := premiumGuilds[g]
_, err = tx.Exec(qSetNew, g, t, isPremium, count, 0, 0, 0, 0, 0)
if err != nil {
tx.Rollback()
return lastID, errors.WithStackIf(err)
}
}
}
err = tx.Commit()
if err != nil {
return lastID, errors.WithStackIf(err)
}
if common.RedisPool != nil {
err = updateSubProgress("serverstats_migration_v2_progress_msgs", lastID, true)
}
return lastID, err
}
func migrateChunkV2Members(lastID int64, premiumGuilds map[int64]time.Time) (newLastID int64, err error) {
const qGetOld = `SELECT id, guild_id, created_at, num_members, joins, leaves, max_online
FROM server_stats_member_periods
WHERE id > $1 ORDER BY ID ASC LIMIT 5000;`
const qSetNew = `INSERT INTO server_stats_periods_compressed
(guild_id, t, premium, num_messages, num_members, max_online, joins, leaves, max_voice)
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)
ON CONFLICT (guild_id, t) DO UPDATE
SET num_members = GREATEST(server_stats_periods_compressed.num_members, $5),
max_online = GREATEST(server_stats_periods_compressed.max_online, $6),
max_voice = GREATEST(server_stats_periods_compressed.max_voice, $9),
joins = server_stats_periods_compressed.joins + $7,
leaves = server_stats_periods_compressed.leaves + $8
`
// get a chunk of old rows
rows, err := common.PQ.Query(qGetOld, lastID)
if err != nil {
return lastID, errors.WithStackIf(err)
}
// sum them up in this map
guildSums := make(map[time.Time]map[int64]*migratedv2Row)
for rows.Next() {
var t time.Time
var guildID int64
var numMembers, joins, leaves, maxOnline int
err = rows.Scan(&lastID, &guildID, &t, &numMembers, &joins, &leaves, &maxOnline)
if err != nil {
rows.Close()
return lastID, errors.WithStackIf(err)
}
t = t.Truncate(time.Hour * 24)
if tBucket, ok := guildSums[t]; ok {
if row, ok := tBucket[guildID]; ok {
// merge with existing
row.Joins += joins
row.Leaves += leaves
if maxOnline > row.MaxOnline {
row.MaxOnline = maxOnline
}
if numMembers > row.NumMembers {
row.NumMembers = numMembers
}
} else {
// new row
tBucket[guildID] = &migratedv2Row{
GuildID: guildID,
Joins: joins,
Leaves: leaves,
NumMembers: numMembers,
MaxOnline: maxOnline,
}
}
} else {
// new day and row
guildSums[t] = make(map[int64]*migratedv2Row)
guildSums[t][guildID] = &migratedv2Row{
GuildID: guildID,
Joins: joins,
Leaves: leaves,
NumMembers: numMembers,
MaxOnline: maxOnline,
}
}
}
tx, err := common.PQ.Begin()
if err != nil {
return lastID, errors.WithStackIf(err)
}
// convert them to the new format
for t, guilds := range guildSums {
for g, row := range guilds {
_, isPremium := premiumGuilds[g]
_, err = tx.Exec(qSetNew, g, t, isPremium, 0, row.NumMembers, row.MaxOnline, row.Joins, row.Leaves, 0)
if err != nil {
tx.Rollback()
return lastID, errors.WithStackIf(err)
}
}
}
err = tx.Commit()
if err != nil {
return lastID, errors.WithStackIf(err)
}
if common.RedisPool != nil {
err = updateSubProgress("serverstats_migration_v2_progress_members", lastID, true)
}
return lastID, err
}
type migratedv2Row struct {
GuildID int64
Joins int
Leaves int
NumMembers int
MaxOnline int
}
func updateSubProgress(key string, lastID int64, running bool) error {
prog := &MigrationSubProgress{
LastUpdated: time.Now(),
LastID: lastID,
Running: running,
}
return common.SetRedisJson(key, prog)
}
type MigrationSubProgress struct {
LastUpdated time.Time
LastID int64
Running bool
}
func (m *MigrationSubProgress) IsRunning() bool {
if time.Since(m.LastUpdated) > time.Minute*1 {
return false
}
return m.Running
}
type MigrationProgress struct {
MsgPeriods *MigrationSubProgress
MemberPeriods *MigrationSubProgress
}
func GetMigrationV2Progress() (*MigrationProgress, error) {
var msgsProgress *MigrationSubProgress
err := common.GetRedisJson("serverstats_migration_v2_progress_msgs", &msgsProgress)
if err != nil {
return nil, err
}
if msgsProgress == nil {
msgsProgress = &MigrationSubProgress{
LastID: -1,
}
logger.Infof("starting migration to v2 format for messages from sctatch")
}
var memberProgress *MigrationSubProgress
err = common.GetRedisJson("serverstats_migration_v2_progress_members", &memberProgress)
if err != nil {
return nil, err
}
if memberProgress == nil {
memberProgress = &MigrationSubProgress{
LastID: -1,
}
logger.Infof("starting migration to v2 format for members from sctatch")
}
return &MigrationProgress{
MsgPeriods: msgsProgress,
MemberPeriods: memberProgress,
}, nil
}