This repository has been archived by the owner on Apr 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 99
/
global.go
291 lines (258 loc) · 8.89 KB
/
global.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
/*
Copyright 2018-2022 Mailgun Technologies Inc
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package gubernator
import (
"context"
"github.com/mailgun/holster/v4/syncutil"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"google.golang.org/protobuf/proto"
)
// globalManager manages async hit queue and updates peers in
// the cluster periodically when a global rate limit we own updates.
type globalManager struct {
hitsQueue chan *RateLimitReq
broadcastQueue chan *RateLimitReq
wg syncutil.WaitGroup
conf BehaviorConfig
log FieldLogger
instance *V1Instance // TODO circular import? V1Instance also holds a reference to globalManager
metricGlobalSendDuration prometheus.Summary
metricGlobalSendQueueLength prometheus.Gauge
metricBroadcastDuration prometheus.Summary
metricGlobalQueueLength prometheus.Gauge
}
func newGlobalManager(conf BehaviorConfig, instance *V1Instance) *globalManager {
gm := globalManager{
log: instance.log,
hitsQueue: make(chan *RateLimitReq, conf.GlobalBatchLimit),
broadcastQueue: make(chan *RateLimitReq, conf.GlobalBatchLimit),
instance: instance,
conf: conf,
metricGlobalSendDuration: prometheus.NewSummary(prometheus.SummaryOpts{
Name: "gubernator_global_send_duration",
Help: "The duration of GLOBAL async sends in seconds.",
Objectives: map[float64]float64{0.5: 0.05, 0.99: 0.001},
}),
metricGlobalSendQueueLength: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "gubernator_global_send_queue_length",
Help: "The count of requests queued up for global broadcast. This is only used for GetRateLimit requests using global behavior.",
}),
metricBroadcastDuration: prometheus.NewSummary(prometheus.SummaryOpts{
Name: "gubernator_broadcast_duration",
Help: "The duration of GLOBAL broadcasts to peers in seconds.",
Objectives: map[float64]float64{0.5: 0.05, 0.99: 0.001},
}),
metricGlobalQueueLength: prometheus.NewGauge(prometheus.GaugeOpts{
Name: "gubernator_global_queue_length",
Help: "The count of requests queued up for global broadcast. This is only used for GetRateLimit requests using global behavior.",
}),
}
gm.runAsyncHits()
gm.runBroadcasts()
return &gm
}
func (gm *globalManager) QueueHit(r *RateLimitReq) {
if r.Hits != 0 {
gm.hitsQueue <- r
}
}
func (gm *globalManager) QueueUpdate(req *RateLimitReq) {
if req.Hits != 0 {
gm.broadcastQueue <- req
}
}
// runAsyncHits collects async hit requests in a forever loop,
// aggregates them in one request, and sends them to
// the owning peers.
// The updates are sent both when the batch limit is hit
// and in a periodic frequency determined by GlobalSyncWait.
func (gm *globalManager) runAsyncHits() {
var interval = NewInterval(gm.conf.GlobalSyncWait)
hits := make(map[string]*RateLimitReq)
gm.wg.Until(func(done chan struct{}) bool {
select {
case r := <-gm.hitsQueue:
// Aggregate the hits into a single request
key := r.HashKey()
_, ok := hits[key]
if ok {
// If any of our hits includes a request to RESET_REMAINING
// ensure the owning peer gets this behavior
if HasBehavior(r.Behavior, Behavior_RESET_REMAINING) {
SetBehavior(&hits[key].Behavior, Behavior_RESET_REMAINING, true)
}
hits[key].Hits += r.Hits
} else {
hits[key] = r
}
gm.metricGlobalSendQueueLength.Set(float64(len(hits)))
// Send the hits if we reached our batch limit
if len(hits) == gm.conf.GlobalBatchLimit {
gm.sendHits(hits)
hits = make(map[string]*RateLimitReq)
gm.metricGlobalSendQueueLength.Set(0)
return true
}
// If this is our first queued hit since last send
// queue the next interval
if len(hits) == 1 {
interval.Next()
}
case <-interval.C:
if len(hits) != 0 {
gm.sendHits(hits)
hits = make(map[string]*RateLimitReq)
gm.metricGlobalSendQueueLength.Set(0)
}
case <-done:
interval.Stop()
return false
}
return true
})
}
// sendHits takes the hits collected by runAsyncHits and sends them to their
// owning peers
func (gm *globalManager) sendHits(hits map[string]*RateLimitReq) {
type pair struct {
client *PeerClient
req GetPeerRateLimitsReq
}
defer prometheus.NewTimer(gm.metricGlobalSendDuration).ObserveDuration()
peerRequests := make(map[string]*pair)
// Assign each request to a peer
for _, r := range hits {
peer, err := gm.instance.GetPeer(context.Background(), r.HashKey())
if err != nil {
gm.log.WithError(err).Errorf("while getting peer for hash key '%s'", r.HashKey())
continue
}
p, ok := peerRequests[peer.Info().GRPCAddress]
if ok {
p.req.Requests = append(p.req.Requests, r)
} else {
peerRequests[peer.Info().GRPCAddress] = &pair{
client: peer,
req: GetPeerRateLimitsReq{Requests: []*RateLimitReq{r}},
}
}
}
fan := syncutil.NewFanOut(gm.conf.GlobalPeerRequestsConcurrency)
// Send the rate limit requests to their respective owning peers.
for _, p := range peerRequests {
fan.Run(func(in interface{}) error {
p := in.(*pair)
ctx, cancel := context.WithTimeout(context.Background(), gm.conf.GlobalTimeout)
_, err := p.client.GetPeerRateLimits(ctx, &p.req)
cancel()
if err != nil {
gm.log.WithError(err).
Errorf("while sending global hits to '%s'", p.client.Info().GRPCAddress)
}
return nil
}, p)
}
fan.Wait()
}
// runBroadcasts collects status changes for global rate limits in a forever loop,
// and broadcasts the changes to each peer in the cluster.
// The updates are sent both when the batch limit is hit
// and in a periodic frequency determined by GlobalSyncWait.
func (gm *globalManager) runBroadcasts() {
var interval = NewInterval(gm.conf.GlobalSyncWait)
updates := make(map[string]*RateLimitReq)
gm.wg.Until(func(done chan struct{}) bool {
select {
case update := <-gm.broadcastQueue:
updates[update.HashKey()] = update
gm.metricGlobalQueueLength.Set(float64(len(updates)))
// Send the hits if we reached our batch limit
if len(updates) >= gm.conf.GlobalBatchLimit {
gm.broadcastPeers(context.Background(), updates)
updates = make(map[string]*RateLimitReq)
gm.metricGlobalQueueLength.Set(0)
return true
}
// If this is our first queued hit since last send
// queue the next interval
if len(updates) == 1 {
interval.Next()
}
case <-interval.C:
if len(updates) == 0 {
break
}
gm.broadcastPeers(context.Background(), updates)
updates = make(map[string]*RateLimitReq)
gm.metricGlobalQueueLength.Set(0)
case <-done:
interval.Stop()
return false
}
return true
})
}
// broadcastPeers broadcasts global rate limit statuses to all other peers
func (gm *globalManager) broadcastPeers(ctx context.Context, updates map[string]*RateLimitReq) {
defer prometheus.NewTimer(gm.metricBroadcastDuration).ObserveDuration()
var req UpdatePeerGlobalsReq
reqState := RateLimitReqState{IsOwner: false}
gm.metricGlobalQueueLength.Set(float64(len(updates)))
for _, update := range updates {
// Get current rate limit state.
grlReq := proto.Clone(update).(*RateLimitReq)
grlReq.Hits = 0
status, err := gm.instance.workerPool.GetRateLimit(ctx, grlReq, reqState)
if err != nil {
gm.log.WithError(err).Error("while retrieving rate limit status")
continue
}
updateReq := &UpdatePeerGlobal{
Key: update.HashKey(),
Algorithm: update.Algorithm,
Duration: update.Duration,
Status: status,
CreatedAt: *update.CreatedAt,
}
req.Globals = append(req.Globals, updateReq)
}
fan := syncutil.NewFanOut(gm.conf.GlobalPeerRequestsConcurrency)
for _, peer := range gm.instance.GetPeerList() {
// Exclude ourselves from the update
if peer.Info().IsOwner {
continue
}
fan.Run(func(in interface{}) error {
peer := in.(*PeerClient)
ctx, cancel := context.WithTimeout(ctx, gm.conf.GlobalTimeout)
_, err := peer.UpdatePeerGlobals(ctx, &req)
cancel()
if err != nil {
// Only log if it's an unknown error
if !errors.Is(err, context.Canceled) && errors.Is(err, context.DeadlineExceeded) {
gm.log.WithError(err).Errorf("while broadcasting global updates to '%s'", peer.Info().GRPCAddress)
}
}
return nil
}, peer)
}
fan.Wait()
}
// Close stops all goroutines and shuts down all the peers.
func (gm *globalManager) Close() {
gm.wg.Stop()
for _, peer := range gm.instance.GetPeerList() {
_ = peer.Shutdown(context.Background())
}
}