-
Notifications
You must be signed in to change notification settings - Fork 948
/
Copy pathbgworker.go
751 lines (618 loc) · 18.5 KB
/
bgworker.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
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
package serverstats
import (
"fmt"
"sync"
"time"
"emperror.dev/errors"
"github.com/botlabs-gg/yagpdb/v2/common"
"github.com/botlabs-gg/yagpdb/v2/common/backgroundworkers"
"github.com/botlabs-gg/yagpdb/v2/common/config"
"github.com/botlabs-gg/yagpdb/v2/premium"
"github.com/botlabs-gg/yagpdb/v2/serverstats/messagestatscollector"
"github.com/mediocregopher/radix/v3"
)
const (
RedisKeyLastHourlyRan = "serverstats_last_hourly_worker_ran"
)
var (
confDisableCompression = config.RegisterOption("yagpdb.serverstats.disable_compression", "Disables compression of serverstats", false)
confDisableNewCompression = config.RegisterOption("yagpdb.serverstats.disable_new_compression", "Disables compression of serverstats", false)
)
var _ backgroundworkers.BackgroundWorkerPlugin = (*Plugin)(nil)
func (p *Plugin) RunBackgroundWorker() {
compressorLegacy := &Compressor{}
go compressorLegacy.runLoopLegacy(p)
compressorNew := &Compressor{}
go compressorNew.runLoop(p)
err := StartMigrationToV2Format()
if err != nil {
logger.WithError(err).Error("failed starting migration to v2 format")
}
}
func (p *Plugin) StopBackgroundWorker(wg *sync.WaitGroup) {
wg.Add(1)
p.stopStatsLoop <- wg
p.stopStatsLoop <- wg
}
type Compressor struct {
lastCompressed time.Time
}
func (c *Compressor) runLoop(p *Plugin) {
time.Sleep(time.Second)
for {
// find the next time we should run a compression
_, wait, err := c.updateCompress(time.Now(), false)
if err != nil {
wait = time.Second
logger.WithError(err).Errorf("failed compressing stats: %+v", err)
}
logger.Info("wait is ", wait)
after := time.After(wait)
select {
case wg := <-p.stopStatsLoop:
wg.Done()
logger.Infof("stopped compressor")
return
case <-after:
continue
}
}
}
func (c *Compressor) runLoopLegacy(p *Plugin) {
cleanupTicker := time.NewTicker(time.Hour * 6)
time.Sleep(time.Second)
for {
// find the next time we should run a compression
_, wait, err := c.updateCompress(time.Now(), true)
if err != nil {
wait = time.Second
logger.WithError(err).Errorf("failed compressing stats: %+v", err)
}
logger.Info("wait is ", wait)
after := time.After(wait)
select {
case wg := <-p.stopStatsLoop:
wg.Done()
logger.Infof("stopped compressor")
return
case <-after:
continue
case <-cleanupTicker.C:
// run cleanup of temporary stats
logger.Info("Cleaning up server stats")
started := time.Now()
p.cleanupOldStats(time.Now().Add(time.Hour * -30))
logger.Infof("Took %s to ckean up stats", time.Since(started))
}
}
}
// returns true if a compression was ran
func (c *Compressor) updateCompress(t time.Time, legacy bool) (ran bool, next time.Duration, err error) {
truncatedDay := t.Truncate(time.Hour * 24)
if truncatedDay == c.lastCompressed {
// should not compress
next = truncatedDay.Add((time.Hour * 25)).Sub(t)
return false, next, nil
}
started := time.Now()
if legacy {
if !confDisableCompression.GetBool() {
err = c.RunCompressionLegacy(truncatedDay)
}
} else {
if !confDisableNewCompression.GetBool() {
err = c.runCompression(t.AddDate(0, 0, -1))
}
}
if err != nil {
return true, 0, errors.WithStackIf(err)
}
if legacy {
logger.Infof("took %s to compress LEGACY stats", time.Since(started))
} else {
logger.Infof("took %s to compress NEW stats", time.Since(started))
}
c.lastCompressed = t
next = truncatedDay.Add((time.Hour * 25)).Sub(t)
return true, next, nil
}
func (p *Plugin) getLastTimeRanHourly() time.Time {
var last int64
err := common.RedisPool.Do(radix.Cmd(&last, "GET", RedisKeyLastHourlyRan))
if err != nil {
logger.WithError(err).Error("[serverstats] failed getting last hourly worker run time")
}
return time.Unix(last, 0)
}
func (c *Compressor) RunCompressionLegacy(t time.Time) error {
logger.Info("Compressing server stats...")
// first get a list of active guilds to clean
activeGuildsMsgs, activeGuildsMisc, err := getActiveGuilds(t)
if err != nil {
return errors.WithStackIf(err)
}
// process misc stats and message stats combined
for _, v := range activeGuildsMisc {
err := compressGuildLegacy(t, v, common.ContainsInt64Slice(activeGuildsMsgs, v), true)
if err != nil {
return errors.WithStackIf(err)
}
time.Sleep(time.Millisecond * 10)
}
// process the message stats only
for _, v := range activeGuildsMsgs {
if common.ContainsInt64Slice(activeGuildsMisc, v) {
continue
}
err := compressGuildLegacy(t, v, true, false)
if err != nil {
return errors.WithStackIf(err)
}
time.Sleep(time.Millisecond * 10)
}
return nil
}
func compressGuildLegacy(t time.Time, guildID int64, activeMsgs bool, misc bool) error {
var result []*CompressedStats
var err error
// combine both into a compressed per day format
if misc {
result, err = compressGuildMiscStats(t, guildID)
if err != nil {
return errors.WithStackIf(err)
}
}
if activeMsgs {
result, err = compressGuildMessageStats(t, guildID, result)
if err != nil {
return errors.WithStackIf(err)
}
}
if len(result) < 1 {
logger.Infof("No stats to update even tough guild was marked active? weeeeird.... %d", guildID)
return nil
}
// commit results into the compressed stats table
const updateQ = `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,
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;`
isPremium, err := premium.IsGuildPremium(guildID)
if err != nil {
return errors.WithStackIf(err)
}
tx, err := common.PQ.Begin()
if err != nil {
return errors.WithStackIf(err)
}
for _, v := range result {
_, err = tx.Exec(updateQ, guildID, v.T, isPremium, v.NumMessages, v.TotalMembers, v.MaxOnline, v.Joins, v.Leaves, v.MaxVoice)
if err != nil {
tx.Rollback()
return errors.WithStackIf(err)
}
}
// and finally delete the uncrompressed stats
if activeMsgs {
_, err = tx.Exec("UPDATE server_stats_hourly_periods_messages SET compressed=true WHERE t < $1 AND guild_id = $2;", t, guildID)
if err != nil {
tx.Rollback()
return errors.WithStackIf(err)
}
}
if misc {
_, err = tx.Exec("UPDATE server_stats_hourly_periods_misc SET compressed=true WHERE t < $1 AND guild_id = $2;", t, guildID)
if err != nil {
tx.Rollback()
return errors.WithStackIf(err)
}
}
err = tx.Commit()
if err != nil {
return errors.WithStackIf(err)
}
return nil
}
type CompressedStats struct {
GuildID int64
T time.Time
NumMessages int
MaxOnline int
TotalMembers int
Joins int
Leaves int
MaxVoice int
}
func compressGuildMessageStats(t time.Time, guildID int64, mergeWith []*CompressedStats) ([]*CompressedStats, error) {
const q = `SELECT t, count
FROM server_stats_hourly_periods_messages
WHERE t < $1 AND guild_id = $2 AND compressed=false;
`
rows, err := common.PQ.Query(q, t, guildID)
if err != nil {
return nil, errors.WithStackIf(err)
}
OUTER:
for rows.Next() {
var t time.Time
var count int
err = rows.Scan(&t, &count)
if err != nil {
return nil, errors.WithStackIf(err)
}
roundedToDay := t.Truncate(time.Hour * 24)
for _, r := range mergeWith {
if r.T != roundedToDay {
continue
}
// found existing compressed day result, merge this with that one
r.NumMessages += count
continue OUTER
}
// no existing compressed day result, make a new one
mergeWith = append(mergeWith, &CompressedStats{
GuildID: guildID,
T: roundedToDay,
NumMessages: count,
})
}
return mergeWith, nil
}
func compressGuildMiscStats(t time.Time, guildID int64) ([]*CompressedStats, error) {
const q = `SELECT t, num_members, max_online, joins, leaves, max_voice
FROM server_stats_hourly_periods_misc
WHERE t < $1 AND guild_id = $2 AND compressed=false;
`
rows, err := common.PQ.Query(q, t, guildID)
if err != nil {
return nil, errors.WithStackIf(err)
}
result := make([]*CompressedStats, 0)
OUTER:
for rows.Next() {
var t time.Time
var numMembers, maxOnline, joins, leaves, maxVoice int
err = rows.Scan(&t, &numMembers, &maxOnline, &joins, &leaves, &maxVoice)
if err != nil {
return nil, errors.WithStackIf(err)
}
roundedToDay := t.Truncate(time.Hour * 24)
for _, r := range result {
if r.T != roundedToDay {
continue
}
// found existing compressed day result, merge this with that one
r.Joins += joins
r.Leaves += leaves
if r.TotalMembers < numMembers {
r.TotalMembers = numMembers
}
if r.MaxOnline < maxOnline {
r.MaxOnline = maxOnline
}
if r.MaxVoice < maxVoice {
r.MaxVoice = maxVoice
}
continue OUTER
}
// no existing compressed day result, make a new one
result = append(result, &CompressedStats{
GuildID: guildID,
T: roundedToDay,
TotalMembers: numMembers,
MaxOnline: maxOnline,
Joins: joins,
Leaves: leaves,
MaxVoice: maxVoice,
})
}
return result, nil
}
func getActiveGuilds(t time.Time) (activeMsgs []int64, activeMisc []int64, err error) {
const qMsgs = `SELECT DISTINCT guild_id FROM server_stats_hourly_periods_messages WHERE t < $1 AND compressed=false`
rows, err := common.PQ.Query(qMsgs, t)
if err != nil {
return nil, nil, errors.WithStackIf(err)
}
for rows.Next() {
var g int64
err = rows.Scan(&g)
if err != nil {
rows.Close()
return nil, nil, errors.WithStackIf(err)
}
activeMsgs = append(activeMsgs, g)
}
rows.Close()
const qMisc = `SELECT DISTINCT guild_id FROM server_stats_hourly_periods_misc WHERE t < $1 AND compressed=false`
rows, err = common.PQ.Query(qMisc, t)
if err != nil {
return nil, nil, errors.WithStackIf(err)
}
for rows.Next() {
var g int64
err = rows.Scan(&g)
if err != nil {
rows.Close()
return nil, nil, errors.WithStackIf(err)
}
activeMisc = append(activeMisc, g)
}
rows.Close()
return
}
func (p *Plugin) cleanupOldStats(t time.Time) error {
// and finally delete the already compressed stats
_, err := common.PQ.Exec("DELETE FROM server_stats_hourly_periods_messages WHERE t < $1 AND compressed = true;", t)
if err != nil {
return errors.WithStackIf(err)
}
_, err = common.PQ.Exec("DELETE FROM server_stats_hourly_periods_misc WHERE t < $1 AND compressed = true;", t)
if err != nil {
return errors.WithStackIf(err)
}
return nil
}
func (c *Compressor) runCompression(t time.Time) error {
// check up to 5 days back
logger.Infof("Running compression, t is %d:%d: %s", t.Year(), t.YearDay(), t)
for i := 0; i < 5; i++ {
newT := t.AddDate(0, 0, -i)
year := newT.Year()
day := newT.YearDay()
err := c.runCompressionDay(year, day)
if err != nil {
return err
}
}
return nil
}
type GuildStatsFrame struct {
Messages int
TotalMembers int
OnlineMembers int
Joins int
Leaves int
}
const keyCompressionCompressionRanDays = "serverstats_compression_days_ran"
func (c *Compressor) hasCompressionRan(year, day int) (bool, error) {
var ran bool
err := common.RedisPool.Do(radix.Cmd(&ran, "SISMEMBER", keyCompressionCompressionRanDays, fmt.Sprintf("%d:%d", year, day)))
return ran, err
}
func (c *Compressor) runCompressionDay(year, day int) error {
logger.Infof("Running compression for %d:%d", year, day)
alreadyRan, err := c.hasCompressionRan(year, day)
if err != nil {
return err
}
if alreadyRan {
logger.Infof("Stats compression already ran for %d: %d", year, day)
return c.cleanTempRedisStats(year, day)
}
stats, err := c.collectStats(year, day)
if err != nil {
return errors.WithStackIf(err)
}
if len(stats) > 0 {
err = c.saveCollectedStats(year, day, stats)
if err != nil {
return err
}
} else {
logger.Infof("No stats for %d:%d", year, day)
}
return c.cleanTempRedisStats(year, day)
}
func (c *Compressor) cleanTempRedisStats(year, day int) error {
// clean message stats
var activeGuilds []int64
err := common.RedisPool.Do(radix.Cmd(&activeGuilds, "SMEMBERS", messagestatscollector.KeyActiveGuilds(year, day)))
if err != nil {
return errors.WithStackIf(err)
}
for _, g := range activeGuilds {
err = common.RedisPool.Do(radix.Cmd(nil, "DEL", messagestatscollector.KeyMessageStats(g, year, day)))
if err != nil {
return errors.WithStackIf(err)
}
}
err = common.RedisPool.Do(radix.Cmd(nil, "DEL", messagestatscollector.KeyActiveGuilds(year, day)))
if err != nil {
return errors.WithStackIf(err)
}
// clean other stats
err = common.RedisPool.Do(radix.Cmd(nil, "DEL", keyTotalMembers(year, day)))
if err != nil {
return errors.WithStackIf(err)
}
err = common.RedisPool.Do(radix.Cmd(nil, "DEL", keyOnlineMembers(year, day)))
if err != nil {
return errors.WithStackIf(err)
}
err = common.RedisPool.Do(radix.Cmd(nil, "DEL", keyJoinedMembers(year, day)))
if err != nil {
return errors.WithStackIf(err)
}
err = common.RedisPool.Do(radix.Cmd(nil, "DEL", keyLeftMembers(year, day)))
if err != nil {
return errors.WithStackIf(err)
}
// finally, clean up this state of this day
err = common.RedisPool.Do(radix.Cmd(nil, "DEL", keyCompressionCompressionRanDays, fmt.Sprintf("%d:%d", year, day)))
if err != nil {
return errors.WithStackIf(err)
}
return err
}
func (c *Compressor) saveCollectedStats(year, day int, stats map[int64]*GuildStatsFrame) error {
t := time.Date(year, 1, day, 0, 0, 0, 0, time.UTC)
allPremiumGuilds, err := premium.AllGuildsOncePremium()
if err != nil {
return err
}
tx, err := common.PQ.Begin()
if err != nil {
return err
}
const updateQ = `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,
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;`
for g, s := range stats {
_, isPremium := allPremiumGuilds[g]
_, err = tx.Exec(updateQ, g, t, isPremium, s.Messages, s.TotalMembers, s.OnlineMembers, s.Joins, s.Leaves, 0)
if err != nil {
tx.Rollback()
return errors.WithStackIf(err)
}
}
// mark this day as compressed
err = common.RedisPool.Do(radix.Cmd(nil, "SADD", keyCompressionCompressionRanDays, fmt.Sprintf("%d:%d", year, day)))
if err != nil {
tx.Rollback()
return errors.WithStackIf(err)
}
err = tx.Commit()
if err != nil {
tx.Rollback()
// try to rollback marking this guild as compressed
err2 := common.RedisPool.Do(radix.Cmd(nil, "SREM", keyCompressionCompressionRanDays, fmt.Sprintf("%d:%d", year, day)))
if err2 != nil {
// this requires manual internvention to repair, broken connection to db or something in the middle of commit?
// but atleast this wont produce duplicate stats
logger.WithError(err2).Error("FAILED UN-MARKING GUILD AS COMPRESSED")
}
return errors.WithStackIf(err)
}
return nil
}
func (c *Compressor) collectStats(year, day int) (map[int64]*GuildStatsFrame, error) {
stats, err := c.collectMessageStats(year, day)
if err != nil {
return stats, errors.WithStackIf(err)
}
err = c.collectTotalMembers(year, day, stats)
if err != nil {
return stats, errors.WithStackIf(err)
}
err = c.collectOnlineMembers(year, day, stats)
if err != nil {
return stats, errors.WithStackIf(err)
}
err = c.collectJoins(year, day, stats)
if err != nil {
return stats, errors.WithStackIf(err)
}
err = c.collectLeaves(year, day, stats)
if err != nil {
return stats, errors.WithStackIf(err)
}
return stats, nil
}
func (c *Compressor) collectMessageStats(year, day int) (map[int64]*GuildStatsFrame, error) {
var activeGuilds []int64
err := common.RedisPool.Do(radix.Cmd(&activeGuilds, "SMEMBERS", messagestatscollector.KeyActiveGuilds(year, day)))
if err != nil {
return nil, err
}
result := make(map[int64]*GuildStatsFrame)
if len(activeGuilds) < 1 {
return result, nil
}
for _, g := range activeGuilds {
raw := make(map[int64]int64)
err = common.RedisPool.Do(radix.Cmd(&raw, "ZRANGE", messagestatscollector.KeyMessageStats(g, year, day), "0", "-1", "WITHSCORES"))
if err != nil {
return nil, err
}
combined := 0
for _, v := range raw {
combined += int(v)
}
result[g] = &GuildStatsFrame{
Messages: combined,
}
}
return result, nil
}
func (c *Compressor) collectTotalMembers(year, day int, stats map[int64]*GuildStatsFrame) error {
raw := make(map[int64]int64)
err := common.RedisPool.Do(radix.Cmd(&raw, "ZRANGE", keyTotalMembers(year, day), "0", "-1", "WITHSCORES"))
if err != nil {
return err
}
for g, v := range raw {
if current, ok := stats[g]; ok {
current.TotalMembers += int(v)
} else {
stats[g] = &GuildStatsFrame{
TotalMembers: int(v),
}
}
}
return nil
}
func (c *Compressor) collectJoins(year, day int, stats map[int64]*GuildStatsFrame) error {
raw := make(map[int64]int64)
err := common.RedisPool.Do(radix.Cmd(&raw, "ZRANGE", keyJoinedMembers(year, day), "0", "-1", "WITHSCORES"))
if err != nil {
return err
}
for g, v := range raw {
if current, ok := stats[g]; ok {
current.Joins += int(v)
} else {
stats[g] = &GuildStatsFrame{
Joins: int(v),
}
}
}
return nil
}
func (c *Compressor) collectLeaves(year, day int, stats map[int64]*GuildStatsFrame) error {
raw := make(map[int64]int64)
err := common.RedisPool.Do(radix.Cmd(&raw, "ZRANGE", keyLeftMembers(year, day), "0", "-1", "WITHSCORES"))
if err != nil {
return err
}
for g, v := range raw {
if current, ok := stats[g]; ok {
current.Leaves += int(v)
} else {
stats[g] = &GuildStatsFrame{
Leaves: int(v),
}
}
}
return nil
}
func (c *Compressor) collectOnlineMembers(year, day int, stats map[int64]*GuildStatsFrame) error {
raw := make(map[int64]int64)
err := common.RedisPool.Do(radix.Cmd(&raw, "ZRANGE", keyOnlineMembers(year, day), "0", "-1", "WITHSCORES"))
if err != nil {
return err
}
for g, v := range raw {
if current, ok := stats[g]; ok {
current.OnlineMembers += int(v)
} else {
stats[g] = &GuildStatsFrame{
OnlineMembers: int(v),
}
}
}
return nil
}