This repository has been archived by the owner on Apr 27, 2023. It is now read-only.
forked from istio/istio
/
memquota.go
285 lines (235 loc) · 8.05 KB
/
memquota.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
// Copyright 2017 Istio Authors
//
// 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.
// nolint: lll
//go:generate $GOPATH/src/istio.io/istio/bin/mixer_codegen.sh -a mixer/adapter/memquota/config/config.proto -x "-n memquota -t quota"
// Package memquota provides a simple in-memory quota implementation. It's
// trivial to set up, but it has various limitations:
//
// - Obviously, the data set must be able to fit in memory.
//
// - When Mixer crashes/restarts, all quota values are erased.
// This means this isn't good for allocation quotas although
// it works well enough for rate limits quotas.
//
// - Since the data is all memory-resident and there isn't any cross-node
// synchronization, this adapter can't be used in an Istio mixer where
// a single service can be handled by different mixer instances.
package memquota
import (
"context"
"fmt"
"time"
"istio.io/istio/mixer/adapter/memquota/config"
"istio.io/istio/mixer/pkg/adapter"
"istio.io/istio/mixer/pkg/status"
"istio.io/istio/mixer/template/quota"
)
type handler struct {
// common info among different quota adapters
common dedupUtil
// the counters we track for non-expiring quotas, protected by lock
cells map[string]int64
// the rolling windows we track for expiring quotas, protected by lock
windows map[string]*rollingWindow
// the limits we know about
limits map[string]*config.Params_Quota
// logger provided by the framework
logger adapter.Logger
}
// Limit is implemented by Quota and Override messages.
type Limit interface {
GetMaxAmount() int64
GetValidDuration() time.Duration
}
// matchDimensions matches configured dimensions with dimensions of the instance.
func matchDimensions(cfg map[string]string, inst map[string]interface{}) bool {
for k, val := range cfg {
rval := inst[k]
if adapter.StringEquals(rval, val) { // this dimension matches, on to next comparison.
continue
}
// rval does not match val.
return false
}
return true
}
// limit returns the limit associated with this particular request.
// Check if the instance matches an override, else return the default limit.
func limit(cfg *config.Params_Quota, instance *quota.Instance, l adapter.Logger) Limit {
for idx := range cfg.Overrides {
o := cfg.Overrides[idx]
if matchDimensions(o.Dimensions, instance.Dimensions) {
l.Debugf("quota override: %v selected for %v", o, *instance)
// all dimensions matched, we found the override.
return &o
}
}
l.Debugf("quota default: %v selected for %v", cfg.MaxAmount, *instance)
// no overrides, use default limit.
return cfg
}
func (h *handler) HandleQuota(context context.Context, instance *quota.Instance, args adapter.QuotaArgs) (adapter.QuotaResult, error) {
q := limit(h.limits[instance.Name], instance, h.logger)
if args.QuotaAmount > 0 {
return h.alloc(instance, args, q)
} else if args.QuotaAmount < 0 {
args.QuotaAmount = -args.QuotaAmount
return h.free(instance, args, q)
}
return adapter.QuotaResult{}, nil
}
func (h *handler) alloc(instance *quota.Instance, args adapter.QuotaArgs, q Limit) (adapter.QuotaResult, error) {
amount, exp, key, err := h.common.handleDedup(instance, args, func(key string, currentTime time.Time, currentTick int64) (int64, time.Time,
time.Duration) {
result := args.QuotaAmount
// we optimize storage for non-expiring quotas
if q.GetValidDuration() == 0 {
inUse := h.cells[key]
if result > q.GetMaxAmount()-inUse {
if !args.BestEffort {
return 0, time.Time{}, 0
}
// grab as much as we can
result = q.GetMaxAmount() - inUse
}
h.cells[key] = inUse + result
return result, time.Time{}, 0
}
window, ok := h.windows[key]
if !ok {
seconds := int32((q.GetValidDuration() + time.Second - 1) / time.Second)
window = newRollingWindow(q.GetMaxAmount(), int64(seconds)*ticksPerSecond)
h.windows[key] = window
}
if !window.alloc(result, currentTick) {
if !args.BestEffort {
return 0, time.Time{}, 0
}
// grab as much as we can
result = window.available()
_ = window.alloc(result, currentTick)
}
return result, currentTime.Add(q.GetValidDuration()), q.GetValidDuration()
})
h.logger.Debugf(" AccessLog %d/%d %s", amount, args.QuotaAmount, key)
return adapter.QuotaResult{
Status: status.OK,
Amount: amount,
ValidDuration: exp,
}, err
}
func (h *handler) free(instance *quota.Instance, args adapter.QuotaArgs, q Limit) (adapter.QuotaResult, error) {
amount, _, _, err := h.common.handleDedup(instance, args, func(key string, currentTime time.Time, currentTick int64) (int64, time.Time,
time.Duration) {
result := args.QuotaAmount
if q.GetValidDuration() == 0 {
inUse := h.cells[key]
if result >= inUse {
// delete the cell since it contains no useful state
delete(h.cells, key)
return inUse, time.Time{}, 0
}
h.cells[key] = inUse - result
return result, time.Time{}, 0
}
// WARNING: Releasing quota in the case of rate limits is
// inherently racy. A release can easily end up
// freeing quota in the wrong window.
window, ok := h.windows[key]
if !ok {
return 0, time.Time{}, 0
}
result = window.release(result, currentTick)
if window.available() == q.GetMaxAmount() {
// delete the cell since it contains no useful state
delete(h.windows, key)
}
return result, time.Time{}, 0
})
return adapter.QuotaResult{
Status: status.OK,
Amount: amount,
}, err
}
func (h *handler) Close() error {
h.common.ticker.Stop()
return nil
}
////////////////// Config //////////////////////////
// GetInfo returns the Info associated with this adapter implementation.
func GetInfo() adapter.Info {
return adapter.Info{
Name: "memquota",
Impl: "istio.io/istio/mixer/adapter/memquota",
Description: "Volatile memory-based quota tracking",
SupportedTemplates: []string{
quota.TemplateName,
},
DefaultConfig: &config.Params{
MinDeduplicationDuration: 1 * time.Second,
},
NewBuilder: func() adapter.HandlerBuilder { return &builder{} },
}
}
type builder struct {
adapterConfig *config.Params
quotaTypes map[string]*quota.Type
}
func (b *builder) SetQuotaTypes(types map[string]*quota.Type) { b.quotaTypes = types }
func (b *builder) SetAdapterConfig(cfg adapter.Config) { b.adapterConfig = cfg.(*config.Params) }
func (b *builder) Validate() (ce *adapter.ConfigErrors) {
ac := b.adapterConfig
if ac.MinDeduplicationDuration <= 0 {
ce = ce.Appendf("minDeduplicationDuration", "deduplication window of %v is invalid, must be > 0", ac.MinDeduplicationDuration)
}
return
}
func (b *builder) Build(context context.Context, env adapter.Env) (adapter.Handler, error) {
ac := b.adapterConfig
return b.buildWithDedup(context, env, time.NewTicker(ac.MinDeduplicationDuration))
}
func (b *builder) buildWithDedup(_ context.Context, env adapter.Env, ticker *time.Ticker) (*handler, error) {
ac := b.adapterConfig
limits := make(map[string]*config.Params_Quota, len(ac.Quotas))
for idx := range ac.Quotas {
l := ac.Quotas[idx]
limits[l.Name] = &l
}
for k := range b.quotaTypes {
if _, ok := limits[k]; !ok {
return nil, fmt.Errorf("did not find limit defined for quota %s", k)
}
}
h := &handler{
common: dedupUtil{
recentDedup: make(map[string]dedupState),
oldDedup: make(map[string]dedupState),
ticker: ticker,
getTime: time.Now,
logger: env.Logger(),
},
cells: make(map[string]int64),
windows: make(map[string]*rollingWindow),
limits: limits,
logger: env.Logger(),
}
env.ScheduleDaemon(func() {
for range h.common.ticker.C {
h.common.Lock()
h.common.reapDedup()
h.common.Unlock()
}
})
return h, nil
}