-
Notifications
You must be signed in to change notification settings - Fork 914
/
shardmemberfetcher.go
604 lines (485 loc) · 14 KB
/
shardmemberfetcher.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
package shardmemberfetcher
import (
"errors"
"fmt"
"sync"
"time"
"github.com/botlabs-gg/yagpdb/v2/bot/eventsystem"
"github.com/botlabs-gg/yagpdb/v2/common"
"github.com/botlabs-gg/yagpdb/v2/lib/discordgo"
"github.com/botlabs-gg/yagpdb/v2/lib/dstate"
"github.com/botlabs-gg/yagpdb/v2/lib/dstate/inmemorytracker"
"github.com/karlseguin/ccache"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)
type ReadyTracker interface {
IsShardReady(shardID int) bool
}
var metricsRequests = promauto.NewCounter(prometheus.CounterOpts{
Name: "yagpdb_memberfetcher_requests_total",
Help: "The total number members added to queue",
})
var metricsProcessed = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "yagpdb_memberfetcher_processed_total",
Help: "The total number of processed queue items",
}, []string{"type"})
var metricsFailed = promauto.NewCounterVec(prometheus.CounterOpts{
Name: "yagpdb_memberfetcher_failed_total",
Help: "The total number of failed queue items",
}, []string{"type"})
var metricsGatewayChunkFailed = promauto.NewCounter(prometheus.CounterOpts{
Name: "yagpdb_memberfetcher_gateway_chunk_fail_total",
Help: "The number of failed",
})
type GatewayRequestFunc func(guildID int64, userIDs []int64, nonce string) error
type Manager struct {
state dstate.StateTracker
totalShards int64
fetchers []*shardMemberFetcher
fetchersMu sync.RWMutex
gwRequestFunc GatewayRequestFunc
failedUsersCache *ccache.Cache
rTracker ReadyTracker
}
func NewManager(totalShards int64, state dstate.StateTracker, f GatewayRequestFunc, rt ReadyTracker) *Manager {
return &Manager{
totalShards: totalShards,
state: state,
gwRequestFunc: f,
failedUsersCache: ccache.New(ccache.Configure()),
rTracker: rt,
}
}
func (m *Manager) GetMember(guildID, userID int64) (*dstate.MemberState, error) {
return m.getMember(guildID, userID, false)
}
func (m *Manager) GetMembers(guildID int64, userIDs ...int64) ([]*dstate.MemberState, error) {
result := make([]*dstate.MemberState, 0, len(userIDs))
resultChan := make(chan *MemberFetchResult)
requests := make([]*MemberFetchRequest, 0, len(userIDs))
for _, v := range userIDs {
// check state first
ms := m.state.GetMember(guildID, v)
if ms != nil && ms.Member != nil {
result = append(result, ms)
continue
}
// otherwise create a request
requests = append(requests, &MemberFetchRequest{
resp: resultChan,
Member: v,
Guild: guildID,
})
}
fetcher := m.findCreateFetcher(guildID)
fetcher.reqChan <- requests
for range requests {
r := <-resultChan
if r.Member != nil {
result = append(result, r.Member)
}
}
return result, nil
}
func (m *Manager) getMember(guildID, userID int64, joinedAt bool) (*dstate.MemberState, error) {
// check from state first
ms := m.state.GetMember(guildID, userID)
if ms != nil && ms.Member != nil && (!joinedAt || ms.Member.JoinedAt != "") {
return ms, nil
}
// make the request
resultChan := make(chan *MemberFetchResult)
req := []*MemberFetchRequest{
{
resp: resultChan,
Member: userID,
Guild: guildID,
},
}
fetcher := m.findCreateFetcher(guildID)
fetcher.reqChan <- req
result := <-resultChan
return result.Member, result.Err
}
func (m *Manager) findCreateFetcher(guildID int64) *shardMemberFetcher {
shardID := (guildID >> 22) % m.totalShards
// fast path
m.fetchersMu.RLock()
for _, v := range m.fetchers {
if v.shardID == shardID {
m.fetchersMu.RUnlock()
return v
}
}
m.fetchersMu.RUnlock()
// no result, slow path
m.fetchersMu.Lock()
defer m.fetchersMu.Unlock()
// check again as it could have changed inbetween upgrading the lock
for _, v := range m.fetchers {
if v.shardID == shardID {
return v
}
}
// still no result, make a new fetcher
fetcher := &shardMemberFetcher{
state: m.state,
gwRequestFunc: m.gwRequestFunc,
reqChan: make(chan []*MemberFetchRequest),
shardID: shardID,
failedCache: m.failedUsersCache,
sortedQueue: make(map[int64][]*MemberFetchRequest),
fetchingSingle: make(map[guildMemberIDPair]bool),
fetchingGWState: &FetchingGWState{Finished: true},
finishedSingle: make(chan *MemberFetchResult),
finishedGateway: make(chan *discordgo.GuildMembersChunk),
rTracker: m.rTracker,
}
go fetcher.run()
m.fetchers = append(m.fetchers, fetcher)
return fetcher
}
func (m *Manager) HandleGuildmembersChunk(evt *eventsystem.EventData) {
shard := evt.Session.ShardID
m.fetchersMu.RLock()
for _, v := range m.fetchers {
if v.shardID == int64(shard) {
v.finishedGateway <- evt.GuildMembersChunk()
}
}
m.fetchersMu.RUnlock()
}
type shardMemberFetcher struct {
state dstate.StateTracker
reqChan chan []*MemberFetchRequest
shardID int64
fetchingGWState *FetchingGWState
// key is guildID
sortedQueue map[int64][]*MemberFetchRequest
fetchingSingle map[guildMemberIDPair]bool
finishedSingle chan *MemberFetchResult
finishedGateway chan *discordgo.GuildMembersChunk
failedCache *ccache.Cache
gwRequestFunc GatewayRequestFunc
rTracker ReadyTracker
}
func (s *shardMemberFetcher) run() {
guildFetcherTicker := time.NewTicker(time.Second)
for {
select {
case reqSlice := <-s.reqChan:
s.addToQueue(reqSlice)
case finG := <-s.finishedGateway:
s.handleFinishedGateway(finG)
case finS := <-s.finishedSingle:
s.handleFinishedSingle(finS)
case <-guildFetcherTicker.C:
s.checkSendNextGatewayRequest()
}
}
}
func (s *shardMemberFetcher) addToQueue(elems []*MemberFetchRequest) {
guildID := int64(0)
for _, v := range elems {
s.sortedQueue[v.Guild] = append(s.sortedQueue[v.Guild], v)
guildID = v.Guild
}
metricsRequests.Add(float64(len(elems)))
if len(elems) > 2 && s.fetchingGWState.Finished && time.Since(s.fetchingGWState.Started) > time.Second {
s.checkSendNextGatewayRequest()
s.checkSendNextAPICall(guildID)
} else {
s.checkSendNextAPICall(guildID)
}
}
func (s *shardMemberFetcher) handleFinishedSingle(elem *MemberFetchResult) {
guildID := elem.GuildID
doneRequests := make([]*MemberFetchRequest, 0, 1)
newQueue := make([]*MemberFetchRequest, 0, len(s.sortedQueue[guildID]))
// find the finished requests and remake the queue without the finished requests
for _, v := range s.sortedQueue[guildID] {
if v.Member == elem.MemberID {
doneRequests = append(doneRequests, v)
continue
}
// not finished
newQueue = append(newQueue, v)
}
s.sortedQueue[guildID] = newQueue
// remove the fetching status
delete(s.fetchingSingle, guildMemberIDPair{
GuildID: elem.GuildID,
MemberID: elem.MemberID,
})
// send the results
for _, req := range doneRequests {
go s.sendResult(req, elem)
}
// cache errors
if elem.Err != nil {
failedCacheKey := discordgo.StrID(guildID) + ":" + discordgo.StrID(elem.MemberID)
s.failedCache.Set(failedCacheKey, true, time.Minute)
}
s.checkSendNextAPICall(guildID)
}
func (s *shardMemberFetcher) handleFinishedGateway(chunk *discordgo.GuildMembersChunk) {
guildID := chunk.GuildID
doneRequests := make([]*MemberFetchRequest, 0, len(chunk.Members))
newQueue := make([]*MemberFetchRequest, 0, len(s.sortedQueue[guildID]))
// find the finished requests and remake the queue without the finished requests
// remove the fetching status
if s.fetchingGWState.Nonce == chunk.Nonce {
// mark all the members we passed to the request done
OUTER_MATCHED_ALL:
for _, v := range s.sortedQueue[guildID] {
for _, done := range s.fetchingGWState.Members {
if v.Member == done {
doneRequests = append(doneRequests, v)
continue OUTER_MATCHED_ALL
}
}
newQueue = append(newQueue, v)
}
// Also mark these requests done
s.fetchingGWState.Members = nil
s.fetchingGWState.Finished = true
} else {
// mismatched... but still check our queues in case we got lucky!
OUTER:
for _, v := range s.sortedQueue[guildID] {
for _, done := range chunk.Members {
if v.Member == done.User.ID {
doneRequests = append(doneRequests, v)
continue OUTER
}
}
newQueue = append(newQueue, v)
}
}
s.sortedQueue[guildID] = newQueue
// send the results
OUTER_SEND:
for _, req := range doneRequests {
for _, v := range chunk.Members {
if req.Member == v.User.ID {
go s.sendGWResult(req, v)
continue OUTER_SEND
}
}
// not found, sendn il
go s.sendGWResult(req, nil)
}
// add to state if we can
cast, ok := s.state.(*inmemorytracker.InMemoryTracker)
if !ok {
return
}
for _, v := range chunk.Members {
if ms := s.state.GetMember(guildID, v.User.ID); ms != nil && ms.Member != nil {
continue // already in state
}
ms := dstate.MemberStateFromMember(v)
ms.GuildID = guildID
cast.SetMember(ms)
}
}
func (s *shardMemberFetcher) sendResult(req *MemberFetchRequest, result *MemberFetchResult) {
req.resp <- result
}
func (s *shardMemberFetcher) sendGWResult(req *MemberFetchRequest, member *discordgo.Member) {
if member == nil {
metricsFailed.With(prometheus.Labels{"type": "gateway"}).Inc()
req.resp <- &MemberFetchResult{
Member: nil,
Err: errors.New("not found"),
GuildID: req.Guild,
MemberID: req.Member,
}
} else {
metricsProcessed.With(prometheus.Labels{"type": "gateway"}).Add(1)
ms := dstate.MemberStateFromMember(member)
ms.GuildID = req.Guild
req.resp <- &MemberFetchResult{
Err: nil,
Member: ms,
GuildID: req.Guild,
MemberID: req.Member,
}
}
}
func (s *shardMemberFetcher) checkSendNextAPICall(guildID int64) {
for k := range s.fetchingSingle {
if k.GuildID == guildID {
// already fetching on this guild
return
}
}
// fetch
if q, ok := s.sortedQueue[guildID]; ok && len(q) > 0 {
var next *MemberFetchRequest
if !s.fetchingGWState.Finished && s.fetchingGWState.GuildID == guildID {
OUTER:
for _, rq := range q {
for _, mID := range s.fetchingGWState.Members {
if mID == rq.Member {
continue OUTER
}
}
next = rq
break
}
} else {
next = q[0]
}
if next == nil {
// nothing more to do, already fetching from gateway
return
}
s.fetchingSingle[guildMemberIDPair{
GuildID: guildID,
MemberID: next.Member,
}] = true
go s.fetchSingle(next)
}
}
func (s *shardMemberFetcher) fetchSingle(req *MemberFetchRequest) {
var err error
var result *dstate.MemberState
defer func() {
// we always wanna mark it was finished even if it panics somehow as otherwise it would get completely stuck
s.finishedSingle <- &MemberFetchResult{
Err: err,
Member: result,
GuildID: req.Guild,
MemberID: req.Member,
}
}()
result, err = s.fetchSingleInner(req)
}
func (s *shardMemberFetcher) fetchSingleInner(req *MemberFetchRequest) (*dstate.MemberState, error) {
// check if its already in state first
gs := s.state.GetGuild(req.Guild)
if gs == nil {
metricsFailed.With(prometheus.Labels{"type": "state"}).Inc()
return nil, errors.New("guild not found in state")
}
// it was already existant in the state
result := s.state.GetMember(req.Guild, req.Member)
if result != nil && result.Member != nil {
metricsProcessed.With(prometheus.Labels{"type": "state"}).Inc()
return result, nil
}
// fetch from api
var m *discordgo.Member
m, err := common.BotSession.GuildMember(req.Guild, req.Member)
if err != nil {
metricsFailed.With(prometheus.Labels{"type": "http"}).Inc()
return nil, err
}
metricsProcessed.With(prometheus.Labels{"type": "http"}).Inc()
result = dstate.MemberStateFromMember(m)
result.GuildID = req.Guild // yes this field is not set...
// add to state if we can
if cast, ok := s.state.(*inmemorytracker.InMemoryTracker); ok {
if state_ms := s.state.GetMember(req.Guild, req.Member); state_ms == nil || state_ms.Member == nil {
// make a copy because we handed out references above and the below function might mutate it
cast.SetMember(result)
}
}
return result, nil
}
func (s *shardMemberFetcher) checkSendNextGatewayRequest() {
if !s.rTracker.IsShardReady(int(s.shardID)) {
return
}
if !s.fetchingGWState.Finished {
if time.Since(s.fetchingGWState.Started) > time.Minute {
s.fetchingGWState.Finished = true
s.fetchingGWState.Members = nil
metricsGatewayChunkFailed.Inc()
// trigger The number of failed case it never finished
s.checkSendNextAPICall(s.fetchingGWState.GuildID)
} else {
return
}
}
if time.Since(s.fetchingGWState.Started) < time.Second {
// Keep it strictly 1 per second
return
}
// find the biggest queue
biggestQueueLen := 0
biggestQueueGuild := int64(0)
for g, q := range s.sortedQueue {
l := len(q)
if l > biggestQueueLen {
biggestQueueGuild = g
biggestQueueLen = l
}
}
// no point in issuing a guild request if its below 2
if biggestQueueLen < 1 {
return
}
// fetch from this guild
// generate a nonce
ids := make([]int64, 0, 100)
OUTER:
for _, v := range s.sortedQueue[biggestQueueGuild] {
for _, existing := range ids {
if v.Member == existing {
// already added this member to the list
continue OUTER
}
}
if _, ok := s.fetchingSingle[guildMemberIDPair{
GuildID: biggestQueueGuild,
MemberID: v.Member,
}]; ok {
// fetching this through normal API
continue
}
ids = append(ids, v.Member)
if len(ids) >= 100 {
break
}
}
if len(ids) < 1 {
// in the cases of the queue being big but somehow they're all busy (don't think this is possible with the min requirement being len of 2 but whatever)
return
}
nonce := fmt.Sprintf("shard_member_fetcher:%d", time.Now().UnixNano())
s.fetchingGWState.GuildID = biggestQueueGuild
s.fetchingGWState.Nonce = nonce
s.fetchingGWState.Started = time.Now()
s.fetchingGWState.Members = ids
s.gwRequestFunc(biggestQueueGuild, ids, nonce)
}
type guildMemberIDPair struct {
GuildID int64
MemberID int64
}
type FetchingGWState struct {
Members []int64
Started time.Time
GuildID int64
Finished bool
Nonce string
}
type MemberFetchRequest struct {
Member int64
Guild int64
resp chan *MemberFetchResult
}
type MemberFetchResult struct {
Err error
Member *dstate.MemberState
GuildID int64
MemberID int64
}
type GWFetchResult struct {
GuildID int64
Members []*discordgo.Member
Nonce string
}