-
Notifications
You must be signed in to change notification settings - Fork 927
/
service.go
291 lines (230 loc) · 6.58 KB
/
service.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
package common
import (
"bytes"
"encoding/json"
"os"
"sync"
"time"
"emperror.dev/errors"
"github.com/mediocregopher/radix/v3"
)
const ServicesRedisKey = "yag_services"
type serviceTracker struct {
host *ServiceHost
lastUpdate []byte
mu sync.Mutex
}
// ServiceType represents the type of the component
type ServiceType string
const (
ServiceTypeBot ServiceType = "bot"
ServiceTypeFrontend ServiceType = "frontend"
ServiceTypeBGWorker ServiceType = "bgworker"
ServiceTypeFeed ServiceType = "feed"
ServiceTypeOrchestator ServiceType = "orchestrator"
)
// Service represents a service or component of yagpdb
type Service struct {
Type ServiceType `json:"type"`
Name string `json:"name"`
Details string `json:"details"`
botDetailsF func() (*BotServiceDetails, error)
BotDetails *BotServiceDetails `json:"bot_details"`
}
// BotServiceDetails is bot service specific details
type BotServiceDetails struct {
RunningShards []int `json:"running_shards"`
TotalShards int `json:"total_shards"`
NodeID string `json:"node_id"`
OrchestratorMode bool `json:"orchestrator_mode"`
}
// ServiceHost represents a process that holds oen or more bot components
type ServiceHost struct {
InternalAPIAddress string `json:"internal_api_address"`
Host string `json:"host"`
PID int `json:"pid"`
Version string `json:"version"`
Services []*Service `json:"services"`
}
// ServiceTracker keeps track of the various components of yagpdb in a central location for ease of access
var ServiceTracker = newServiceTracker()
func newServiceTracker() *serviceTracker {
hostname, _ := os.Hostname()
st := &serviceTracker{
host: &ServiceHost{
Host: hostname,
PID: os.Getpid(),
Version: VERSION,
},
}
go st.run()
return st
}
func (s *serviceTracker) RegisterService(t ServiceType, name string, details string, extrasF func() (*BotServiceDetails, error)) {
s.mu.Lock()
defer s.mu.Unlock()
s.host.Services = append(s.host.Services, &Service{
Type: t,
Name: name,
Details: details,
botDetailsF: extrasF,
})
}
func (s *serviceTracker) SetAPIAddress(apiAddress string) {
s.mu.Lock()
defer s.mu.Unlock()
s.host.InternalAPIAddress = apiAddress
}
func (s *serviceTracker) run() {
t := time.NewTicker(time.Second * 5)
for {
<-t.C
s.update()
}
}
func (s *serviceTracker) update() {
s.mu.Lock()
defer s.mu.Unlock()
for _, v := range s.host.Services {
if v.botDetailsF == nil {
continue
}
botDetails, err := v.botDetailsF()
if err != nil {
logger.WithError(err).Error("failed retrieving extra service details")
v.BotDetails = &BotServiceDetails{}
continue
}
v.BotDetails = botDetails
}
serialized, err := json.Marshal(s.host)
if err != nil {
logger.WithError(err).Error("failed marshaling service host")
return
}
if !bytes.Equal(serialized, s.lastUpdate) {
err = RedisPool.Do(radix.FlatCmd(nil, "ZREM", ServicesRedisKey, s.lastUpdate))
if err != nil {
logger.WithError(err).Error("failed removing service host")
return
}
}
err = RedisPool.Do(radix.FlatCmd(nil, "ZADD", ServicesRedisKey, time.Now().Unix(), serialized))
if err != nil {
logger.WithError(err).Error("failed updating service host")
return
}
s.lastUpdate = serialized
err = RedisPool.Do(radix.FlatCmd(nil, "ZREMRANGEBYSCORE", ServicesRedisKey, 0, time.Now().Unix()-30))
if err != nil {
logger.WithError(err).Error("feailed clearing old service hosts")
return
}
}
type servicePoller struct {
cachedServiceHosts []*ServiceHost
lastPoll time.Time
mu sync.Mutex
}
var ServicePoller = &servicePoller{}
func (sp *servicePoller) getActiveServiceHosts() ([]*ServiceHost, error) {
if time.Since(sp.lastPoll) < time.Second*5 {
return sp.cachedServiceHosts, nil
}
var hosts []string
err := RedisPool.Do(radix.FlatCmd(&hosts, "ZRANGE", ServicesRedisKey, 0, -1))
if err != nil {
return nil, errors.WithStackIf(err)
}
result := make([]*ServiceHost, 0, len(hosts))
for _, v := range hosts {
var parsed *ServiceHost
err = json.Unmarshal([]byte(v), &parsed)
if err != nil {
return nil, errors.WithStackIf(err)
}
result = append(result, parsed)
}
sp.cachedServiceHosts = result
sp.lastPoll = time.Now()
return result, nil
}
// GetActiveServiceHosts returns all of the running service providers of the bot (processes)
func (sp *servicePoller) GetActiveServiceHosts() ([]*ServiceHost, error) {
sp.mu.Lock()
defer sp.mu.Unlock()
return sp.getActiveServiceHosts()
}
// GetShardAddress returns the internal api address of the specified shard
func (sp *servicePoller) GetShardAddress(shardID int) (string, error) {
sp.mu.Lock()
defer sp.mu.Unlock()
hosts, err := sp.getActiveServiceHosts()
if err != nil {
return "", err
}
for _, h := range hosts {
for _, v := range h.Services {
if v.Type == ServiceTypeBot && ContainsIntSlice(v.BotDetails.RunningShards, shardID) {
return h.InternalAPIAddress, nil
}
}
}
return "", ErrNotFound
}
// GetGuildAddress returns the internal api addrress of the specified shard
// This is preferred over GetShardAddress as it also handles cases of different total shard couns (mid upscaling for example)
func (sp *servicePoller) GetGuildAddress(guildID int64) (string, error) {
sp.mu.Lock()
defer sp.mu.Unlock()
hosts, err := sp.getActiveServiceHosts()
if err != nil {
return "", err
}
for _, h := range hosts {
for _, v := range h.Services {
if v.Type != ServiceTypeBot {
continue
}
shardID := int((guildID >> 22) % int64(v.BotDetails.TotalShards))
if ContainsIntSlice(v.BotDetails.RunningShards, shardID) {
return h.InternalAPIAddress, nil
}
}
}
return "", ErrNotFound
}
// GetNodeAddress returns the internal api address of the specified nodeID
func (sp *servicePoller) GetNodeAddress(nodeID string) (string, error) {
sp.mu.Lock()
defer sp.mu.Unlock()
hosts, err := sp.getActiveServiceHosts()
if err != nil {
return "", err
}
for _, h := range hosts {
for _, v := range h.Services {
if v.BotDetails != nil && v.BotDetails.NodeID == nodeID {
return h.InternalAPIAddress, nil
}
}
}
return "", ErrNotFound
}
// GetShardCount returns the total shard count from the first node with a bot, or ErrNotFound otherwise
func (sp *servicePoller) GetShardCount() (int, error) {
sp.mu.Lock()
defer sp.mu.Unlock()
hosts, err := sp.getActiveServiceHosts()
if err != nil {
return 0, err
}
for _, h := range hosts {
for _, v := range h.Services {
if v.Type == ServiceTypeBot {
return v.BotDetails.TotalShards, nil
}
}
}
return 0, ErrNotFound
}