-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathingest_limits_test.go
298 lines (288 loc) · 8.03 KB
/
ingest_limits_test.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
292
293
294
295
296
297
298
package distributor
import (
"context"
"errors"
"testing"
"time"
"github.com/coder/quartz"
"github.com/prometheus/client_golang/prometheus"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"github.com/grafana/loki/v3/pkg/logproto"
)
// mockIngestLimitsFrontendClient mocks the RPC calls for tests.
type mockIngestLimitsFrontendClient struct {
t *testing.T
calls atomic.Uint64
expectedRequest *logproto.ExceedsLimitsRequest
response *logproto.ExceedsLimitsResponse
responseErr error
}
// Implements the ingestLimitsFrontendClient interface.
func (c *mockIngestLimitsFrontendClient) exceedsLimits(_ context.Context, r *logproto.ExceedsLimitsRequest) (*logproto.ExceedsLimitsResponse, error) {
c.calls.Add(1)
require.Equal(c.t, c.expectedRequest, r)
if c.responseErr != nil {
return nil, c.responseErr
}
return c.response, nil
}
func TestIngestLimits_EnforceLimits(t *testing.T) {
clock := quartz.NewMock(t)
clock.Set(time.Date(2025, 1, 1, 0, 0, 0, 0, time.UTC))
tests := []struct {
name string
tenant string
streams []KeyedStream
expectedRequest *logproto.ExceedsLimitsRequest
response *logproto.ExceedsLimitsResponse
responseErr error
expectedStreams []KeyedStream
expectedReasons map[uint64][]string
expectedErr string
}{{
// This test also asserts that streams are returned unmodified.
name: "error should be returned if limits cannot be checked",
tenant: "test",
streams: []KeyedStream{{
HashKey: 1000, // Should not be used.
HashKeyNoShard: 1,
Stream: logproto.Stream{
Labels: "foo",
Entries: []logproto.Entry{{
Timestamp: clock.Now(),
Line: "bar",
StructuredMetadata: []logproto.LabelAdapter{{
Name: "baz",
Value: "qux",
}},
}},
},
}, {
HashKey: 2000, // Should not be used.
HashKeyNoShard: 2,
Stream: logproto.Stream{
Labels: "bar",
Entries: []logproto.Entry{{
Timestamp: clock.Now(),
Line: "baz",
StructuredMetadata: []logproto.LabelAdapter{{
Name: "qux",
Value: "corge",
}},
}},
},
}},
expectedRequest: &logproto.ExceedsLimitsRequest{
Tenant: "test",
Streams: []*logproto.StreamMetadata{{
StreamHash: 1,
EntriesSize: 0x3,
StructuredMetadataSize: 0x6,
}, {
StreamHash: 2,
EntriesSize: 0x3,
StructuredMetadataSize: 0x8,
}},
},
responseErr: errors.New("failed to check limits"),
expectedErr: "failed to check limits",
}, {
name: "exceeds limits",
tenant: "test",
streams: []KeyedStream{{
HashKey: 1000, // Should not be used.
HashKeyNoShard: 1,
}},
expectedRequest: &logproto.ExceedsLimitsRequest{
Tenant: "test",
Streams: []*logproto.StreamMetadata{{
StreamHash: 1,
}},
},
response: &logproto.ExceedsLimitsResponse{
Tenant: "test",
Results: []*logproto.ExceedsLimitsResult{{
StreamHash: 1,
Reason: "test",
}},
},
expectedStreams: []KeyedStream{},
expectedReasons: map[uint64][]string{1: {"test"}},
}, {
name: "one of two streams exceeds limits",
tenant: "test",
streams: []KeyedStream{{
HashKey: 1000, // Should not be used.
HashKeyNoShard: 1,
}, {
HashKey: 2000, // Should not be used.
HashKeyNoShard: 2,
}},
expectedRequest: &logproto.ExceedsLimitsRequest{
Tenant: "test",
Streams: []*logproto.StreamMetadata{{
StreamHash: 1,
}, {
StreamHash: 2,
}},
},
response: &logproto.ExceedsLimitsResponse{
Tenant: "test",
Results: []*logproto.ExceedsLimitsResult{{
StreamHash: 1,
Reason: "test",
}},
},
expectedStreams: []KeyedStream{{
HashKey: 2000, // Should not be used.
HashKeyNoShard: 2,
}},
expectedReasons: map[uint64][]string{1: {"test"}},
}, {
name: "does not exceed limits",
tenant: "test",
streams: []KeyedStream{{
HashKey: 1000, // Should not be used.
HashKeyNoShard: 1,
}, {
HashKey: 2000, // Should not be used.
HashKeyNoShard: 2,
}},
expectedRequest: &logproto.ExceedsLimitsRequest{
Tenant: "test",
Streams: []*logproto.StreamMetadata{{
StreamHash: 1,
}, {
StreamHash: 2,
}},
},
response: &logproto.ExceedsLimitsResponse{
Tenant: "test",
Results: []*logproto.ExceedsLimitsResult{},
},
expectedStreams: []KeyedStream{{
HashKey: 1000, // Should not be used.
HashKeyNoShard: 1,
}, {
HashKey: 2000, // Should not be used.
HashKeyNoShard: 2,
}},
expectedReasons: nil,
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
mockClient := mockIngestLimitsFrontendClient{
t: t,
expectedRequest: test.expectedRequest,
response: test.response,
responseErr: test.responseErr,
}
l := newIngestLimits(&mockClient, prometheus.NewRegistry())
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
streams, reasons, err := l.enforceLimits(ctx, test.tenant, test.streams)
if test.expectedErr != "" {
require.EqualError(t, err, test.expectedErr)
// The streams should be returned unmodified.
require.Equal(t, test.streams, streams)
require.Nil(t, reasons)
} else {
require.Nil(t, err)
require.Equal(t, test.expectedStreams, streams)
require.Equal(t, test.expectedReasons, reasons)
}
})
}
}
// This test asserts that when checking ingest limits the expected proto
// message is sent, and that for a given response, the result contains the
// expected streams each with their expected reasons.
func TestIngestLimits_ExceedsLimits(t *testing.T) {
tests := []struct {
name string
tenant string
streams []KeyedStream
expectedRequest *logproto.ExceedsLimitsRequest
response *logproto.ExceedsLimitsResponse
responseErr error
expectedExceedsLimits bool
expectedReasons map[uint64][]string
expectedErr string
}{{
name: "error should be returned if limits cannot be checked",
tenant: "test",
streams: []KeyedStream{{
HashKeyNoShard: 1,
}},
expectedRequest: &logproto.ExceedsLimitsRequest{
Tenant: "test",
Streams: []*logproto.StreamMetadata{{
StreamHash: 1,
}},
},
responseErr: errors.New("failed to check limits"),
expectedErr: "failed to check limits",
}, {
name: "exceeds limits",
tenant: "test",
streams: []KeyedStream{{
HashKeyNoShard: 1,
}},
expectedRequest: &logproto.ExceedsLimitsRequest{
Tenant: "test",
Streams: []*logproto.StreamMetadata{{
StreamHash: 1,
}},
},
response: &logproto.ExceedsLimitsResponse{
Tenant: "test",
Results: []*logproto.ExceedsLimitsResult{{
StreamHash: 1,
Reason: "test",
}},
},
expectedExceedsLimits: true,
expectedReasons: map[uint64][]string{1: {"test"}},
}, {
name: "does not exceed limits",
tenant: "test",
streams: []KeyedStream{{
HashKeyNoShard: 1,
}},
expectedRequest: &logproto.ExceedsLimitsRequest{
Tenant: "test",
Streams: []*logproto.StreamMetadata{{
StreamHash: 1,
}},
},
response: &logproto.ExceedsLimitsResponse{
Tenant: "test",
Results: []*logproto.ExceedsLimitsResult{},
},
expectedReasons: nil,
}}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
mockClient := mockIngestLimitsFrontendClient{
t: t,
expectedRequest: test.expectedRequest,
response: test.response,
responseErr: test.responseErr,
}
l := newIngestLimits(&mockClient, prometheus.NewRegistry())
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
exceedsLimits, reasons, err := l.exceedsLimits(ctx, test.tenant, test.streams)
if test.expectedErr != "" {
require.EqualError(t, err, test.expectedErr)
require.False(t, exceedsLimits)
require.Nil(t, reasons)
} else {
require.Nil(t, err)
require.Equal(t, test.expectedExceedsLimits, exceedsLimits)
require.Equal(t, test.expectedReasons, reasons)
}
})
}
}