forked from mailgun/gubernator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobal.go
254 lines (218 loc) · 6.98 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
/*
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"
"time"
"github.com/mailgun/holster/v4/clock"
"github.com/mailgun/holster/v4/ctxutil"
"github.com/mailgun/holster/v4/syncutil"
"github.com/mailgun/holster/v4/tracing"
"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 {
asyncQueue chan *RateLimitReq
broadcastQueue chan *RateLimitReq
wg syncutil.WaitGroup
conf BehaviorConfig
log FieldLogger
instance *V1Instance
asyncMetrics prometheus.Summary
broadcastMetrics prometheus.Summary
}
func newGlobalManager(conf BehaviorConfig, instance *V1Instance) *globalManager {
gm := globalManager{
log: instance.log,
asyncMetrics: prometheus.NewSummary(prometheus.SummaryOpts{
Help: "The duration of GLOBAL async sends in seconds.",
Name: "gubernator_async_durations",
Objectives: map[float64]float64{0.5: 0.05, 0.99: 0.001},
}),
broadcastMetrics: prometheus.NewSummary(prometheus.SummaryOpts{
Help: "The duration of GLOBAL broadcasts to peers in seconds.",
Name: "gubernator_broadcast_durations",
Objectives: map[float64]float64{0.5: 0.05, 0.99: 0.001},
}),
asyncQueue: make(chan *RateLimitReq, conf.GlobalBatchLimit),
broadcastQueue: make(chan *RateLimitReq, conf.GlobalBatchLimit),
instance: instance,
conf: conf,
}
gm.runAsyncHits()
gm.runBroadcasts()
return &gm
}
func (gm *globalManager) QueueHit(r *RateLimitReq) {
gm.asyncQueue <- r
}
func (gm *globalManager) QueueUpdate(r *RateLimitReq) {
gm.broadcastQueue <- r
}
// runAsyncHits collects async hit requests and queues them to
// be sent to their owning peers.
func (gm *globalManager) runAsyncHits() {
var interval = NewInterval(gm.conf.GlobalSyncWait)
hits := make(map[string]*RateLimitReq)
gm.wg.Until(func(done chan struct{}) bool {
ctx := tracing.StartScopeDebug(context.Background())
defer tracing.EndScope(ctx, nil)
select {
case r := <-gm.asyncQueue:
// Aggregate the hits into a single request
key := r.HashKey()
_, ok := hits[key]
if ok {
hits[key].Hits += r.Hits
} else {
hits[key] = r
}
// Send the hits if we reached our batch limit
if len(hits) == gm.conf.GlobalBatchLimit {
gm.sendHits(ctx, hits)
hits = make(map[string]*RateLimitReq)
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(ctx, hits)
hits = make(map[string]*RateLimitReq)
}
case <-done:
return false
}
return true
})
}
// sendHits takes the hits collected by runAsyncHits and sends them to their
// owning peers
func (gm *globalManager) sendHits(ctx context.Context, hits map[string]*RateLimitReq) {
type pair struct {
client *PeerClient
req GetPeerRateLimitsReq
}
peerRequests := make(map[string]*pair)
start := clock.Now()
// Assign each request to a peer
for _, r := range hits {
peer, err := gm.instance.GetPeer(ctx, 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}},
}
}
}
// Send the rate limit requests to their respective owning peers.
for _, p := range peerRequests {
ctx, cancel := ctxutil.WithTimeout(context.Background(), gm.conf.GlobalTimeout)
_, err := p.client.GetPeerRateLimits(ctx, &p.req)
cancel()
if err != nil {
gm.log.WithError(err).
Errorf("error sending global hits to '%s'", p.client.Info().GRPCAddress)
continue
}
}
gm.asyncMetrics.Observe(time.Since(start).Seconds())
}
// runBroadcasts collects status changes for global rate limits and broadcasts the changes to each peer in the cluster.
func (gm *globalManager) runBroadcasts() {
var interval = NewInterval(gm.conf.GlobalSyncWait)
updates := make(map[string]*RateLimitReq)
gm.wg.Until(func(done chan struct{}) bool {
ctx := tracing.StartScopeDebug(context.Background())
defer tracing.EndScope(ctx, nil)
select {
case r := <-gm.broadcastQueue:
updates[r.HashKey()] = r
// Send the hits if we reached our batch limit
if len(updates) == gm.conf.GlobalBatchLimit {
gm.broadcastPeers(ctx, updates)
updates = make(map[string]*RateLimitReq)
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 {
gm.broadcastPeers(ctx, updates)
updates = make(map[string]*RateLimitReq)
}
case <-done:
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) {
var req UpdatePeerGlobalsReq
start := clock.Now()
for _, r := range updates {
// Copy the original since we removing the GLOBAL behavior
rl := proto.Clone(r).(*RateLimitReq)
// We are only sending the status of the rate limit so
// we clear the behavior flag so we don't get queued for update again.
SetBehavior(&rl.Behavior, Behavior_GLOBAL, false)
rl.Hits = 0
status, err := gm.instance.getRateLimit(ctx, rl)
if err != nil {
gm.log.WithError(err).Errorf("while broadcasting update to peers for: '%s'", rl.HashKey())
continue
}
// Build an UpdatePeerGlobalsReq
req.Globals = append(req.Globals, &UpdatePeerGlobal{
Algorithm: rl.Algorithm,
Key: rl.HashKey(),
Status: status,
})
}
for _, peer := range gm.instance.GetPeerList() {
// Exclude ourselves from the update
if peer.Info().IsOwner {
continue
}
ctx, cancel := ctxutil.WithTimeout(context.Background(), gm.conf.GlobalTimeout)
_, err := peer.UpdatePeerGlobals(ctx, &req)
cancel()
if err != nil {
// Skip peers that are not in a ready state
if !IsNotReady(err) {
gm.log.WithError(err).Errorf("while broadcasting global updates to '%s'", peer.Info().GRPCAddress)
}
continue
}
}
gm.broadcastMetrics.Observe(time.Since(start).Seconds())
}
func (gm *globalManager) Close() {
gm.wg.Stop()
}