-
Notifications
You must be signed in to change notification settings - Fork 451
/
query_limits.go
218 lines (187 loc) · 5.58 KB
/
query_limits.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
// Copyright (c) 2020 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package limits
import (
"fmt"
"time"
"github.com/m3db/m3/src/x/instrument"
"github.com/uber-go/tally"
"go.uber.org/atomic"
)
const defaultLookback = time.Second * 15
type queryLimits struct {
docsLimit *lookbackLimit
bytesReadLimit *lookbackLimit
}
type lookbackLimit struct {
name string
options LookbackLimitOptions
metrics lookbackLimitMetrics
recent *atomic.Int64
stopCh chan struct{}
}
type lookbackLimitMetrics struct {
recentCount tally.Gauge
recentMax tally.Gauge
total tally.Counter
exceeded tally.Counter
}
var (
_ QueryLimits = (*queryLimits)(nil)
_ LookbackLimit = (*lookbackLimit)(nil)
)
// DefaultLookbackLimitOptions returns a new query limits manager.
func DefaultLookbackLimitOptions() LookbackLimitOptions {
return LookbackLimitOptions{
// Default to no limit.
Limit: 0,
Lookback: defaultLookback,
}
}
// NewQueryLimits returns a new query limits manager.
func NewQueryLimits(
docsLimitOpts LookbackLimitOptions,
bytesReadLimitOpts LookbackLimitOptions,
instrumentOpts instrument.Options,
) (QueryLimits, error) {
if err := docsLimitOpts.validate(); err != nil {
return nil, err
}
if err := bytesReadLimitOpts.validate(); err != nil {
return nil, err
}
docsLimit := newLookbackLimit(instrumentOpts, docsLimitOpts, "docs-matched")
bytesReadLimit := newLookbackLimit(instrumentOpts, bytesReadLimitOpts, "disk-bytes-read")
return &queryLimits{
docsLimit: docsLimit,
bytesReadLimit: bytesReadLimit,
}, nil
}
func newLookbackLimit(
instrumentOpts instrument.Options,
opts LookbackLimitOptions,
name string,
) *lookbackLimit {
return &lookbackLimit{
name: name,
options: opts,
metrics: newLookbackLimitMetrics(instrumentOpts, name),
recent: atomic.NewInt64(0),
stopCh: make(chan struct{}),
}
}
func newLookbackLimitMetrics(instrumentOpts instrument.Options, name string) lookbackLimitMetrics {
scope := instrumentOpts.
MetricsScope().
SubScope("query-limit")
return lookbackLimitMetrics{
recentCount: scope.Gauge(fmt.Sprintf("recent-count-%s", name)),
recentMax: scope.Gauge(fmt.Sprintf("recent-max-%s", name)),
total: scope.Counter(fmt.Sprintf("total-%s", name)),
exceeded: scope.Tagged(map[string]string{"limit": name}).Counter("exceeded"),
}
}
func (q *queryLimits) DocsLimit() LookbackLimit {
return q.docsLimit
}
func (q *queryLimits) BytesReadLimit() LookbackLimit {
return q.bytesReadLimit
}
func (q *queryLimits) Start() {
q.docsLimit.start()
q.bytesReadLimit.start()
}
func (q *queryLimits) Stop() {
q.docsLimit.stop()
q.bytesReadLimit.stop()
}
func (q *queryLimits) AnyExceeded() error {
if err := q.docsLimit.exceeded(); err != nil {
return err
}
return q.bytesReadLimit.exceeded()
}
// Inc increments the current value and returns an error if above the limit.
func (q *lookbackLimit) Inc(val int) error {
if val < 0 {
return fmt.Errorf("invalid negative query limit inc %d", val)
}
if val == 0 {
return q.exceeded()
}
// Add the new stats to the global state.
valI64 := int64(val)
recent := q.recent.Add(valI64)
// Update metrics.
q.metrics.recentCount.Update(float64(recent))
q.metrics.total.Inc(valI64)
// Enforce limit (if specified).
return q.checkLimit(recent)
}
func (q *lookbackLimit) exceeded() error {
return q.checkLimit(q.recent.Load())
}
func (q *lookbackLimit) checkLimit(recent int64) error {
if q.options.Limit > 0 && recent > q.options.Limit {
q.metrics.exceeded.Inc(1)
return fmt.Errorf(
"query aborted due to limit: name=%s, limit=%d, current=%d, within=%s",
q.name, q.options.Limit, recent, q.options.Lookback)
}
return nil
}
func (q *lookbackLimit) start() {
ticker := time.NewTicker(q.options.Lookback)
go func() {
for {
select {
case <-ticker.C:
q.reset()
case <-q.stopCh:
ticker.Stop()
return
}
}
}()
}
func (q *lookbackLimit) stop() {
close(q.stopCh)
}
func (q *lookbackLimit) current() int64 {
return q.recent.Load()
}
func (q *lookbackLimit) reset() {
// Update peak gauge only on resets so it only tracks
// the peak values for each lookback period.
recent := q.recent.Load()
q.metrics.recentMax.Update(float64(recent))
// Update the standard recent gauge to reflect drop back to zero.
q.metrics.recentCount.Update(0)
q.recent.Store(0)
}
func (opts LookbackLimitOptions) validate() error {
if opts.Limit < 0 {
return fmt.Errorf("query limit requires limit >= 0 (%d)", opts.Limit)
}
if opts.Lookback <= 0 {
return fmt.Errorf("query limit requires lookback > 0 (%d)", opts.Lookback)
}
return nil
}