/
limiter.go
290 lines (241 loc) · 9.64 KB
/
limiter.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
package ingester
import (
"fmt"
"math"
"github.com/pkg/errors"
"github.com/cortexproject/cortex/pkg/util"
"github.com/cortexproject/cortex/pkg/util/validation"
)
var (
errMaxSeriesPerMetricLimitExceeded = errors.New("per-metric series limit exceeded")
errMaxMetadataPerMetricLimitExceeded = errors.New("per-metric metadata limit exceeded")
errMaxSeriesPerUserLimitExceeded = errors.New("per-user series limit exceeded")
errMaxMetadataPerUserLimitExceeded = errors.New("per-user metric metadata limit exceeded")
)
// RingCount is the interface exposed by a ring implementation which allows
// to count members
type RingCount interface {
HealthyInstancesCount() int
ZonesCount() int
}
// Limiter implements primitives to get the maximum number of series
// an ingester can handle for a specific tenant
type Limiter struct {
limits *validation.Overrides
ring RingCount
replicationFactor int
shuffleShardingEnabled bool
shardByAllLabels bool
zoneAwarenessEnabled bool
AdminLimitMessage string
}
// NewLimiter makes a new in-memory series limiter
func NewLimiter(
limits *validation.Overrides,
ring RingCount,
shardingStrategy string,
shardByAllLabels bool,
replicationFactor int,
zoneAwarenessEnabled bool,
AdminLimitMessage string,
) *Limiter {
return &Limiter{
limits: limits,
ring: ring,
replicationFactor: replicationFactor,
shuffleShardingEnabled: shardingStrategy == util.ShardingStrategyShuffle,
shardByAllLabels: shardByAllLabels,
zoneAwarenessEnabled: zoneAwarenessEnabled,
AdminLimitMessage: AdminLimitMessage,
}
}
// AssertMaxSeriesPerMetric limit has not been reached compared to the current
// number of series in input and returns an error if so.
func (l *Limiter) AssertMaxSeriesPerMetric(userID string, series int) error {
if actualLimit := l.maxSeriesPerMetric(userID); series < actualLimit {
return nil
}
return errMaxSeriesPerMetricLimitExceeded
}
// AssertMaxMetadataPerMetric limit has not been reached compared to the current
// number of metadata per metric in input and returns an error if so.
func (l *Limiter) AssertMaxMetadataPerMetric(userID string, metadata int) error {
if actualLimit := l.maxMetadataPerMetric(userID); metadata < actualLimit {
return nil
}
return errMaxMetadataPerMetricLimitExceeded
}
// AssertMaxSeriesPerUser limit has not been reached compared to the current
// number of series in input and returns an error if so.
func (l *Limiter) AssertMaxSeriesPerUser(userID string, series int) error {
if actualLimit := l.maxSeriesPerUser(userID); series < actualLimit {
return nil
}
return errMaxSeriesPerUserLimitExceeded
}
// AssertMaxMetricsWithMetadataPerUser limit has not been reached compared to the current
// number of metrics with metadata in input and returns an error if so.
func (l *Limiter) AssertMaxMetricsWithMetadataPerUser(userID string, metrics int) error {
if actualLimit := l.maxMetadataPerUser(userID); metrics < actualLimit {
return nil
}
return errMaxMetadataPerUserLimitExceeded
}
// MaxSeriesPerQuery returns the maximum number of series a query is allowed to hit.
func (l *Limiter) MaxSeriesPerQuery(userID string) int {
return l.limits.MaxSeriesPerQuery(userID)
}
// FormatError returns the input error enriched with the actual limits for the given user.
// It acts as pass-through if the input error is unknown.
func (l *Limiter) FormatError(userID string, err error) error {
switch err {
case errMaxSeriesPerUserLimitExceeded:
return l.formatMaxSeriesPerUserError(userID)
case errMaxSeriesPerMetricLimitExceeded:
return l.formatMaxSeriesPerMetricError(userID)
case errMaxMetadataPerUserLimitExceeded:
return l.formatMaxMetadataPerUserError(userID)
case errMaxMetadataPerMetricLimitExceeded:
return l.formatMaxMetadataPerMetricError(userID)
default:
return err
}
}
func (l *Limiter) formatMaxSeriesPerUserError(userID string) error {
actualLimit := l.maxSeriesPerUser(userID)
localLimit := l.limits.MaxLocalSeriesPerUser(userID)
globalLimit := l.limits.MaxGlobalSeriesPerUser(userID)
return fmt.Errorf("per-user series limit of %d exceeded, %s (local limit: %d global limit: %d actual local limit: %d)",
minNonZero(localLimit, globalLimit), l.AdminLimitMessage, localLimit, globalLimit, actualLimit)
}
func (l *Limiter) formatMaxSeriesPerMetricError(userID string) error {
actualLimit := l.maxSeriesPerMetric(userID)
localLimit := l.limits.MaxLocalSeriesPerMetric(userID)
globalLimit := l.limits.MaxGlobalSeriesPerMetric(userID)
return fmt.Errorf("per-metric series limit of %d exceeded, %s (local limit: %d global limit: %d actual local limit: %d)",
minNonZero(localLimit, globalLimit), l.AdminLimitMessage, localLimit, globalLimit, actualLimit)
}
func (l *Limiter) formatMaxMetadataPerUserError(userID string) error {
actualLimit := l.maxMetadataPerUser(userID)
localLimit := l.limits.MaxLocalMetricsWithMetadataPerUser(userID)
globalLimit := l.limits.MaxGlobalMetricsWithMetadataPerUser(userID)
return fmt.Errorf("per-user metric metadata limit of %d exceeded, %s (local limit: %d global limit: %d actual local limit: %d)",
minNonZero(localLimit, globalLimit), l.AdminLimitMessage, localLimit, globalLimit, actualLimit)
}
func (l *Limiter) formatMaxMetadataPerMetricError(userID string) error {
actualLimit := l.maxMetadataPerMetric(userID)
localLimit := l.limits.MaxLocalMetadataPerMetric(userID)
globalLimit := l.limits.MaxGlobalMetadataPerMetric(userID)
return fmt.Errorf("per-metric metadata limit of %d exceeded, %s (local limit: %d global limit: %d actual local limit: %d)",
minNonZero(localLimit, globalLimit), l.AdminLimitMessage, localLimit, globalLimit, actualLimit)
}
func (l *Limiter) maxSeriesPerMetric(userID string) int {
localLimit := l.limits.MaxLocalSeriesPerMetric(userID)
globalLimit := l.limits.MaxGlobalSeriesPerMetric(userID)
if globalLimit > 0 {
if l.shardByAllLabels {
// We can assume that series are evenly distributed across ingesters
// so we do convert the global limit into a local limit
localLimit = minNonZero(localLimit, l.convertGlobalToLocalLimit(userID, globalLimit))
} else {
// Given a metric is always pushed to the same set of ingesters (based on
// the replication factor), we can configure the per-ingester local limit
// equal to the global limit.
localLimit = minNonZero(localLimit, globalLimit)
}
}
// If both the local and global limits are disabled, we just
// use the largest int value
if localLimit == 0 {
localLimit = math.MaxInt32
}
return localLimit
}
func (l *Limiter) maxMetadataPerMetric(userID string) int {
localLimit := l.limits.MaxLocalMetadataPerMetric(userID)
globalLimit := l.limits.MaxGlobalMetadataPerMetric(userID)
if globalLimit > 0 {
if l.shardByAllLabels {
localLimit = minNonZero(localLimit, l.convertGlobalToLocalLimit(userID, globalLimit))
} else {
localLimit = minNonZero(localLimit, globalLimit)
}
}
if localLimit == 0 {
localLimit = math.MaxInt32
}
return localLimit
}
func (l *Limiter) maxSeriesPerUser(userID string) int {
return l.maxByLocalAndGlobal(
userID,
l.limits.MaxLocalSeriesPerUser,
l.limits.MaxGlobalSeriesPerUser,
)
}
func (l *Limiter) maxMetadataPerUser(userID string) int {
return l.maxByLocalAndGlobal(
userID,
l.limits.MaxLocalMetricsWithMetadataPerUser,
l.limits.MaxGlobalMetricsWithMetadataPerUser,
)
}
func (l *Limiter) maxByLocalAndGlobal(userID string, localLimitFn, globalLimitFn func(string) int) int {
localLimit := localLimitFn(userID)
// The global limit is supported only when shard-by-all-labels is enabled,
// otherwise we wouldn't get an even split of series/metadata across ingesters and
// can't take a "local decision" without any centralized coordination.
if l.shardByAllLabels {
// We can assume that series/metadata are evenly distributed across ingesters
// so we do convert the global limit into a local limit
globalLimit := globalLimitFn(userID)
localLimit = minNonZero(localLimit, l.convertGlobalToLocalLimit(userID, globalLimit))
}
// If both the local and global limits are disabled, we just
// use the largest int value
if localLimit == 0 {
localLimit = math.MaxInt32
}
return localLimit
}
func (l *Limiter) convertGlobalToLocalLimit(userID string, globalLimit int) int {
if globalLimit == 0 {
return 0
}
// Given we don't need a super accurate count (ie. when the ingesters
// topology changes) and we prefer to always be in favor of the tenant,
// we can use a per-ingester limit equal to:
// (global limit / number of ingesters) * replication factor
numIngesters := l.ring.HealthyInstancesCount()
// May happen because the number of ingesters is asynchronously updated.
// If happens, we just temporarily ignore the global limit.
if numIngesters == 0 {
return 0
}
// If the number of available ingesters is greater than the tenant's shard
// size, then we should honor the shard size because series/metadata won't
// be written to more ingesters than it.
if shardSize := l.getShardSize(userID); shardSize > 0 {
// We use Min() to protect from the case the expected shard size is > available ingesters.
numIngesters = min(numIngesters, util.ShuffleShardExpectedInstances(shardSize, l.getNumZones()))
}
return int((float64(globalLimit) / float64(numIngesters)) * float64(l.replicationFactor))
}
func (l *Limiter) getShardSize(userID string) int {
if !l.shuffleShardingEnabled {
return 0
}
return l.limits.IngestionTenantShardSize(userID)
}
func (l *Limiter) getNumZones() int {
if l.zoneAwarenessEnabled {
return max(l.ring.ZonesCount(), 1)
}
return 1
}
func minNonZero(first, second int) int {
if first == 0 || (second != 0 && first > second) {
return second
}
return first
}