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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
|
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
// vim: ts=8 sw=2 smarttab ft=cpp
#include <gtest/gtest.h>
#include "rgw_ratelimit.h"
using namespace std::chrono_literals;
TEST(RGWRateLimit, op_limit_not_enabled)
{
// info.enabled = false, so no limit
std::atomic_bool replacing;
std::condition_variable cv;
RateLimiter ratelimit(replacing, cv);
RGWRateLimitInfo info;
auto time = ceph::coarse_real_clock::now();
std::string key = "uuser123";
bool success = ratelimit.should_rate_limit("PUT", key, time, &info);
EXPECT_EQ(false, success);
}
TEST(RGWRateLimit, reject_op_over_limit)
{
// check that request is being rejected because there are not enough tokens
std::atomic_bool replacing;
std::condition_variable cv;
RateLimiter ratelimit(replacing, cv);
RGWRateLimitInfo info;
info.enabled = true;
info.max_read_ops = 1;
auto time = ceph::coarse_real_clock::now();
std::string key = "uuser123";
bool success = ratelimit.should_rate_limit("GET", key, time, &info);
time = ceph::coarse_real_clock::now();
success = ratelimit.should_rate_limit("GET", key, time, &info);
EXPECT_EQ(true, success);
}
TEST(RGWRateLimit, accept_op_after_giveback)
{
// check that giveback is working fine
std::atomic_bool replacing;
std::condition_variable cv;
RateLimiter ratelimit(replacing, cv);
RGWRateLimitInfo info;
info.enabled = true;
info.max_read_ops = 1;
auto time = ceph::coarse_real_clock::now();
std::string key = "uuser123";
bool success = ratelimit.should_rate_limit("GET", key, time, &info);
ratelimit.giveback_tokens("GET", key);
time = ceph::coarse_real_clock::now();
success = ratelimit.should_rate_limit("GET", key, time, &info);
EXPECT_EQ(false, success);
}
TEST(RGWRateLimit, accept_op_after_refill)
{
// check that tokens are being filled properly
std::atomic_bool replacing;
std::condition_variable cv;
RateLimiter ratelimit(replacing, cv);
RGWRateLimitInfo info;
info.enabled = true;
info.max_read_ops = 1;
auto time = ceph::coarse_real_clock::now();
std::string key = "uuser123";
bool success = ratelimit.should_rate_limit("GET", key, time, &info);
time += 61s;
success = ratelimit.should_rate_limit("GET", key, time, &info);
EXPECT_EQ(false, success);
}
TEST(RGWRateLimit, reject_bw_over_limit)
{
// check that a newer request is rejected if there is no enough tokens (bw)
std::atomic_bool replacing;
std::condition_variable cv;
RateLimiter ratelimit(replacing, cv);
RGWRateLimitInfo info;
info.enabled = true;
info.max_read_bytes = 1;
auto time = ceph::coarse_real_clock::now();
std::string key = "uuser123";
bool success = ratelimit.should_rate_limit("GET", key, time, &info);
ratelimit.decrease_bytes("GET",key, 2, &info);
time = ceph::coarse_real_clock::now();
success = ratelimit.should_rate_limit("GET", key, time, &info);
EXPECT_EQ(true, success);
}
TEST(RGWRateLimit, accept_bw)
{
// check that when there are enough tokens (bw) the request is still being served
std::atomic_bool replacing;
std::condition_variable cv;
RateLimiter ratelimit(replacing, cv);
RGWRateLimitInfo info;
info.enabled = true;
info.max_read_bytes = 2;
auto time = ceph::coarse_real_clock::now();
std::string key = "uuser123";
bool success = ratelimit.should_rate_limit("GET", key, time, &info);
ratelimit.decrease_bytes("GET",key, 1, &info);
time = ceph::coarse_real_clock::now();
success = ratelimit.should_rate_limit("GET", key, time, &info);
EXPECT_EQ(false, success);
}
TEST(RGWRateLimit, check_bw_debt_at_max_120secs)
{
// check that the bandwidth debt is not larger than 120 seconds
std::atomic_bool replacing;
std::condition_variable cv;
RateLimiter ratelimit(replacing, cv);
RGWRateLimitInfo info;
info.enabled = true;
info.max_read_bytes = 2;
auto time = ceph::coarse_real_clock::now();
std::string key = "uuser123";
bool success = ratelimit.should_rate_limit("GET", key, time, &info);
ratelimit.decrease_bytes("GET",key, 100, &info);
time += 121s;
success = ratelimit.should_rate_limit("GET", key, time, &info);
EXPECT_EQ(false, success);
}
TEST(RGWRateLimit, check_that_bw_limit_not_affect_ops)
{
// check that high read bytes limit, does not affect ops limit
std::atomic_bool replacing;
std::condition_variable cv;
RateLimiter ratelimit(replacing, cv);
RGWRateLimitInfo info;
info.enabled = true;
info.max_read_ops = 1;
info.max_read_bytes = 100000000;
auto time = ceph::coarse_real_clock::now();
std::string key = "uuser123";
bool success = ratelimit.should_rate_limit("GET", key, time, &info);
ratelimit.decrease_bytes("GET",key, 10000, &info);
time = ceph::coarse_real_clock::now();
success = ratelimit.should_rate_limit("GET", key, time, &info);
EXPECT_EQ(true, success);
}
TEST(RGWRateLimit, read_limit_does_not_affect_writes)
{
// read limit does not affect writes
std::atomic_bool replacing;
std::condition_variable cv;
RateLimiter ratelimit(replacing, cv);
RGWRateLimitInfo info;
info.enabled = true;
info.max_read_ops = 1;
info.max_read_bytes = 100000000;
auto time = ceph::coarse_real_clock::now();
std::string key = "uuser123";
bool success = ratelimit.should_rate_limit("PUT", key, time, &info);
ratelimit.decrease_bytes("PUT",key, 10000, &info);
time = ceph::coarse_real_clock::now();
success = ratelimit.should_rate_limit("PUT", key, time, &info);
EXPECT_EQ(false, success);
}
TEST(RGWRateLimit, write_limit_does_not_affect_reads)
{
// write limit does not affect reads
std::atomic_bool replacing;
std::condition_variable cv;
RateLimiter ratelimit(replacing, cv);
RGWRateLimitInfo info;
info.enabled = true;
info.max_write_ops = 1;
info.max_write_bytes = 100000000;
auto time = ceph::coarse_real_clock::now();
std::string key = "uuser123";
bool success = ratelimit.should_rate_limit("GET", key, time, &info);
ratelimit.decrease_bytes("GET",key, 10000, &info);
time = ceph::coarse_real_clock::now();
success = ratelimit.should_rate_limit("GET", key, time, &info);
EXPECT_EQ(false, success);
}
TEST(RGWRateLimit, allow_unlimited_access)
{
// 0 values in RGWRateLimitInfo should allow unlimited access
std::atomic_bool replacing;
std::condition_variable cv;
RateLimiter ratelimit(replacing, cv);
RGWRateLimitInfo info;
info.enabled = true;
auto time = ceph::coarse_real_clock::now();
std::string key = "uuser123";
bool success = ratelimit.should_rate_limit("GET", key, time, &info);
EXPECT_EQ(false, success);
}
TEST(RGWRateLimitGC, NO_GC_AHEAD_OF_TIME)
{
// Test if GC is not starting the replace before getting to map_size * 0.9
// Please make sure to change those values when you change the map_size in the code
std::shared_ptr<ActiveRateLimiter> ratelimit(new ActiveRateLimiter(g_ceph_context));
ratelimit->start();
auto active = ratelimit->get_active();
RGWRateLimitInfo info;
auto time = ceph::coarse_real_clock::now();
std::string key = "uuser123";
active->should_rate_limit("GET", key, time, &info);
auto activegc = ratelimit->get_active();
EXPECT_EQ(activegc, active);
}
TEST(RGWRateLimiterGC, GC_IS_WORKING)
{
// Test if GC is replacing the active RateLimiter
// Please make sure to change those values when you change the map_size in the code
std::shared_ptr<ActiveRateLimiter> ratelimit(new ActiveRateLimiter(g_ceph_context));
ratelimit->start();
auto active = ratelimit->get_active();
RGWRateLimitInfo info;
info.enabled = true;
auto time = ceph::coarse_real_clock::now();
std::string key = "-1";
for(int i = 0; i < 2000000; i++)
{
active->should_rate_limit("GET", key, time, &info);
key = std::to_string(i);
}
auto activegc = ratelimit->get_active();
EXPECT_NE(activegc, active);
}
TEST(RGWRateLimitEntry, op_limit_not_enabled)
{
// info.enabled = false, so no limit
RateLimiterEntry entry;
RGWRateLimitInfo info;
auto time = ceph::coarse_real_clock::now().time_since_epoch();
bool success = entry.should_rate_limit(false, &info, time);
EXPECT_EQ(false, success);
}
TEST(RGWRateLimitEntry, reject_op_over_limit)
{
// check that request is being rejected because there are not enough tokens
RGWRateLimitInfo info;
RateLimiterEntry entry;
info.enabled = true;
info.max_read_ops = 1;
auto time = ceph::coarse_real_clock::now().time_since_epoch();
bool success = entry.should_rate_limit(true, &info, time);
time = ceph::coarse_real_clock::now().time_since_epoch();
success = entry.should_rate_limit(true, &info, time);
EXPECT_EQ(true, success);
}
TEST(RGWRateLimitEntry, accept_op_after_giveback)
{
// check that giveback is working fine
RGWRateLimitInfo info;
RateLimiterEntry entry;
info.enabled = true;
info.max_read_ops = 1;
auto time = ceph::coarse_real_clock::now().time_since_epoch();
bool success = entry.should_rate_limit(true, &info, time);
entry.giveback_tokens(true);
time = ceph::coarse_real_clock::now().time_since_epoch();
success = entry.should_rate_limit(true, &info, time);
EXPECT_EQ(false, success);
}
TEST(RGWRateLimitEntry, accept_op_after_refill)
{
// check that tokens are being filled properly
RateLimiterEntry entry;
RGWRateLimitInfo info;
info.enabled = true;
info.max_read_ops = 1;
auto time = ceph::coarse_real_clock::now().time_since_epoch();
bool success = entry.should_rate_limit(true, &info, time);
time += 61s;
success = entry.should_rate_limit(true, &info, time);
EXPECT_EQ(false, success);
}
TEST(RGWRateLimitEntry, reject_bw_over_limit)
{
// check that a newer request is rejected if there is no enough tokens (bw)
RateLimiterEntry entry;
RGWRateLimitInfo info;
info.enabled = true;
info.max_read_bytes = 1;
auto time = ceph::coarse_real_clock::now().time_since_epoch();
bool success = entry.should_rate_limit(true, &info, time);
entry.decrease_bytes(true, 2, &info);
time = ceph::coarse_real_clock::now().time_since_epoch();
success = entry.should_rate_limit(true, &info, time);
EXPECT_EQ(true, success);
}
TEST(RGWRateLimitEntry, accept_bw)
{
// check that when there are enough tokens (bw) the request is still being served
RateLimiterEntry entry;
RGWRateLimitInfo info;
info.enabled = true;
info.max_read_bytes = 2;
auto time = ceph::coarse_real_clock::now().time_since_epoch();
bool success = entry.should_rate_limit(true, &info, time);
entry.decrease_bytes(true, 1, &info);
time = ceph::coarse_real_clock::now().time_since_epoch();
success = entry.should_rate_limit(true, &info, time);
EXPECT_EQ(false, success);
}
TEST(RGWRateLimitEntry, check_bw_debt_at_max_120secs)
{
// check that the bandwidth debt is not larger than 120 seconds
RateLimiterEntry entry;
RGWRateLimitInfo info;
info.enabled = true;
info.max_read_bytes = 2;
auto time = ceph::coarse_real_clock::now().time_since_epoch();
bool success = entry.should_rate_limit(true, &info, time);
entry.decrease_bytes(true, 100, &info);
time += 121s;
success = entry.should_rate_limit(true, &info, time);
EXPECT_EQ(false, success);
}
TEST(RGWRateLimitEntry, check_that_bw_limit_not_affect_ops)
{
// check that high read bytes limit, does not affect ops limit
RateLimiterEntry entry;
RGWRateLimitInfo info;
info.enabled = true;
info.max_read_ops = 1;
info.max_read_bytes = 100000000;
auto time = ceph::coarse_real_clock::now().time_since_epoch();
bool success = entry.should_rate_limit(true, &info, time);
entry.decrease_bytes(true, 10000, &info);
time = ceph::coarse_real_clock::now().time_since_epoch();
success = entry.should_rate_limit(true, &info, time);
EXPECT_EQ(true, success);
}
TEST(RGWRateLimitEntry, read_limit_does_not_affect_writes)
{
// read limit does not affect writes
RateLimiterEntry entry;
RGWRateLimitInfo info;
info.enabled = true;
info.max_read_ops = 1;
info.max_read_bytes = 100000000;
auto time = ceph::coarse_real_clock::now().time_since_epoch();
bool success = entry.should_rate_limit(false, &info, time);
entry.decrease_bytes(false, 10000, &info);
time = ceph::coarse_real_clock::now().time_since_epoch();
success = entry.should_rate_limit(false, &info, time);
EXPECT_EQ(false, success);
}
TEST(RGWRateLimitEntry, write_limit_does_not_affect_reads)
{
// write limit does not affect reads
RateLimiterEntry entry;
RGWRateLimitInfo info;
info.enabled = true;
info.max_write_ops = 1;
info.max_write_bytes = 100000000;
auto time = ceph::coarse_real_clock::now().time_since_epoch();
std::string key = "uuser123";
bool success = entry.should_rate_limit(true, &info, time);
entry.decrease_bytes(true, 10000, &info);
time = ceph::coarse_real_clock::now().time_since_epoch();
success = entry.should_rate_limit(true, &info, time);
EXPECT_EQ(false, success);
}
TEST(RGWRateLimitEntry, allow_unlimited_access)
{
// 0 values in RGWRateLimitInfo should allow unlimited access (default value)
RateLimiterEntry entry;
RGWRateLimitInfo info;
info.enabled = true;
auto time = ceph::coarse_real_clock::now().time_since_epoch();
bool success = entry.should_rate_limit(true, &info, time);
EXPECT_EQ(false, success);
}
|