Repository navigation
rgw: add Retry-After header and configurable rate-limit response - #68210
Conversation
|
Thank you for your contribution. Since you, the author, are not a member of the Ceph GitHub Org yet, our CI will not automatically run. Any member of the Ceph Org may comment "ok - to - test" (without the dashes) to allow the Jenkins jobs to run. |
14 similar comments
|
Thank you for your contribution. Since you, the author, are not a member of the Ceph GitHub Org yet, our CI will not automatically run. Any member of the Ceph Org may comment "ok - to - test" (without the dashes) to allow the Jenkins jobs to run. |
|
Thank you for your contribution. Since you, the author, are not a member of the Ceph GitHub Org yet, our CI will not automatically run. Any member of the Ceph Org may comment "ok - to - test" (without the dashes) to allow the Jenkins jobs to run. |
|
Thank you for your contribution. Since you, the author, are not a member of the Ceph GitHub Org yet, our CI will not automatically run. Any member of the Ceph Org may comment "ok - to - test" (without the dashes) to allow the Jenkins jobs to run. |
|
Thank you for your contribution. Since you, the author, are not a member of the Ceph GitHub Org yet, our CI will not automatically run. Any member of the Ceph Org may comment "ok - to - test" (without the dashes) to allow the Jenkins jobs to run. |
|
Thank you for your contribution. Since you, the author, are not a member of the Ceph GitHub Org yet, our CI will not automatically run. Any member of the Ceph Org may comment "ok - to - test" (without the dashes) to allow the Jenkins jobs to run. |
|
Thank you for your contribution. Since you, the author, are not a member of the Ceph GitHub Org yet, our CI will not automatically run. Any member of the Ceph Org may comment "ok - to - test" (without the dashes) to allow the Jenkins jobs to run. |
|
Thank you for your contribution. Since you, the author, are not a member of the Ceph GitHub Org yet, our CI will not automatically run. Any member of the Ceph Org may comment "ok - to - test" (without the dashes) to allow the Jenkins jobs to run. |
|
Thank you for your contribution. Since you, the author, are not a member of the Ceph GitHub Org yet, our CI will not automatically run. Any member of the Ceph Org may comment "ok - to - test" (without the dashes) to allow the Jenkins jobs to run. |
|
Thank you for your contribution. Since you, the author, are not a member of the Ceph GitHub Org yet, our CI will not automatically run. Any member of the Ceph Org may comment "ok - to - test" (without the dashes) to allow the Jenkins jobs to run. |
|
Thank you for your contribution. Since you, the author, are not a member of the Ceph GitHub Org yet, our CI will not automatically run. Any member of the Ceph Org may comment "ok - to - test" (without the dashes) to allow the Jenkins jobs to run. |
|
Thank you for your contribution. Since you, the author, are not a member of the Ceph GitHub Org yet, our CI will not automatically run. Any member of the Ceph Org may comment "ok - to - test" (without the dashes) to allow the Jenkins jobs to run. |
|
Thank you for your contribution. Since you, the author, are not a member of the Ceph GitHub Org yet, our CI will not automatically run. Any member of the Ceph Org may comment "ok - to - test" (without the dashes) to allow the Jenkins jobs to run. |
|
Thank you for your contribution. Since you, the author, are not a member of the Ceph GitHub Org yet, our CI will not automatically run. Any member of the Ceph Org may comment "ok - to - test" (without the dashes) to allow the Jenkins jobs to run. |
|
Thank you for your contribution. Since you, the author, are not a member of the Ceph GitHub Org yet, our CI will not automatically run. Any member of the Ceph Org may comment "ok - to - test" (without the dashes) to allow the Jenkins jobs to run. |
Config Diff Tool Output+ added: rgw_ratelimit_http_status (rgw.yaml.in)
+ added: rgw_ratelimit_response_delay_ms (rgw.yaml.in)
The above configuration changes are found in the PR. Please update the relevant release documentation if necessary. |
|
/config check ok |
cbodley
left a comment
There was a problem hiding this comment.
thanks @lumir-sliva! also tagging @mkogan1 for review
| if (is_s3_rate_limited) { | ||
| const auto interval = g_conf().get_val<uint64_t>("rgw_ratelimit_interval"); | ||
| dump_header(s, "Retry-After", static_cast<long long>(interval)); |
There was a problem hiding this comment.
this is a useful feature, but rgw_ratelimit_interval=60 doesn't seem like the right value to return here
with the default rgw_ratelimit_interval=60 seconds and a --max-read-ops=10, a rate-limited client would expect their next request to succeed after 60/10 seconds. if we return Retry-After: 6, such a client could get steady performance with one successful request every 6 seconds
if we always return rgw_ratelimit_interval=60, client performance would instead be bursty. they'll get 10 successful read ops quickly, then wait 60 seconds, then another 10 ops, then wait another 60 seconds
for accurate estimates of the Retry-After value, we'd need to teach the functions like RateLimiterEntry::should_rate_limit_read() to calculate and return this delay instead of just returning true/false
| - name: rgw_ratelimit_response_delay_ms | ||
| type: uint | ||
| level: advanced | ||
| desc: Delay in milliseconds before sending a rate-limit error response | ||
| long_desc: When a request is rate-limited, the gateway will wait this many | ||
| milliseconds before sending the error response. This slows down clients | ||
| that immediately retry upon receiving a rate-limit error, reducing the | ||
| impact of retry storms on the gateway. Set to 0 to disable. On the Beast | ||
| frontend the delay suspends the coroutine without blocking a worker thread; | ||
| on thread-per-request frontends the delay is skipped. Values above 30 | ||
| seconds are almost certainly a misconfiguration. |
There was a problem hiding this comment.
while coroutines do avoid tying up a thread during the wait, these sleeping requests will still count against the rgw_max_concurrent_requests throttle. so a rate-limited client could continue to flood the server with requests and deny service to other clients
to work around this, we could potentially modify process_request() to drop this concurrent-request throttle before doing the sleep. but this means the number of such sleeps is now unbounded, so rate-limited clients that flood the server could exhaust its memory and crash
i think the server's best (or least-bad?) approach to this issue is to abort the requests as early as possible
| const bool is_s3_rate_limited = | ||
| (err_no == -ERR_RATE_LIMITED && !(s->prot_flags & RGW_REST_SWIFT)); | ||
| if (is_s3_rate_limited) { | ||
| const auto http_status = g_conf().get_val<uint64_t>("rgw_ratelimit_http_status"); |
There was a problem hiding this comment.
The current docs don't mention the response behavior at all — operators have no guidance on what status code clients receive, how to configure load balancers to handle it, or what retry strategy clients should use. That alone causes confusion (as this issue shows).
maybe this would be better solved through documentation. the problem with server-side configuration like rgw_ratelimit_http_status is that you can't choose one strategy that works for all clients. when configured in a proxy instead, these error mappings could depend on extra information like user-agent
a23b0f2 to
b601eed
Compare
|
Thanks for the thorough review Casey. Addressed all three points, pushed:
The PR is now just Retry-After with a smart value + docs. No new config options. |
b601eed to
4705d29
Compare
| int64_t compute_delay(int64_t ops_needed, int64_t ops_limit, | ||
| int64_t bw_needed, int64_t bw_limit) const { | ||
| const int64_t interval = g_ceph_context->_conf->rgw_ratelimit_interval; | ||
| int64_t delay = 0; | ||
| if (ops_needed > 0 && ops_limit > 0) { | ||
| // tokens replenish at ops_limit * fixed_point per interval | ||
| delay = (ops_needed * interval + | ||
| ops_limit * fixed_point_rgw_ratelimit - 1) / | ||
| (ops_limit * fixed_point_rgw_ratelimit); | ||
| } |
There was a problem hiding this comment.
it took me a minute to follow this math, but it looks right to me now. please add a unit test in src/test/rgw/test_rgw_ratelimit.cc to exercise the corner cases between rate-limited and not
i think it would be simpler if this function just did one calculation instead of two. the main reason is that "bw" calculations only apply to read/write ops, and there was a recent proposal to add a separate "latency" limit to listing requests. where necessary, callers can just call it twice and take the max themselves. here's what i had in mind:
// Compute the retry delay in seconds from token deficits.
// limit and needed are in fixed-point units
static int64_t compute_delay(int64_t limit, int64_t needed, int64_t interval)
{
if (limit <= 0) {
return 0;
}
return (needed * interval + limit - 1) / limit;
}
int64_t should_rate_limit_read(int64_t ops_limit, int64_t bw_limit)
{
const int64_t interval = g_ceph_context->_conf->rgw_ratelimit_interval;
const int64_t ops_delay = compute_delay(ops_limit * fixed_point_rgw_ratelimit,
fixed_point_rgw_ratelimit - read.ops, interval);
const int64_t bw_delay = compute_delay(bw_limit * fixed_point_rgw_ratelimit,
-read.bytes, interval);
const int64_t delay = std::max({ops_delay, bw_delay, 0});
// we don't want to reduce ops' tokens if we've rejected it.
if (delay <= 0 && read.ops >= fixed_point_rgw_ratelimit) {
read.ops -= fixed_point_rgw_ratelimit;
}
return delay;
}TEST(RGWRateLimitEntry, compute_delay)
{
EXPECT_EQ(0, RGWRateLimitEntry::compute_delay(0, 0, 1));
EXPECT_EQ(0, RGWRateLimitEntry::compute_delay(0, 1, 1));
EXPECT_EQ(0, RGWRateLimitEntry::compute_delay(0, 1000, 1));
EXPECT_EQ(0, RGWRateLimitEntry::compute_delay(0, 1001, 1));
EXPECT_EQ(0, RGWRateLimitEntry::compute_delay(1000, 0, 1));
EXPECT_EQ(1, RGWRateLimitEntry::compute_delay(1000, 1, 1));
EXPECT_EQ(1, RGWRateLimitEntry::compute_delay(1000, 1000, 1));
EXPECT_EQ(2, RGWRateLimitEntry::compute_delay(1000, 1001, 1));
EXPECT_EQ(2, RGWRateLimitEntry::compute_delay(1000, 2000, 1));
EXPECT_EQ(3, RGWRateLimitEntry::compute_delay(1000, 2001, 1));
EXPECT_EQ(0, RGWRateLimitEntry::compute_delay(10000, 0, 100));
EXPECT_EQ(1, RGWRateLimitEntry::compute_delay(10000, 1, 100));
EXPECT_EQ(1, RGWRateLimitEntry::compute_delay(10000, 100, 100));
EXPECT_EQ(2, RGWRateLimitEntry::compute_delay(10000, 101, 100));
EXPECT_EQ(2, RGWRateLimitEntry::compute_delay(10000, 200, 100));
EXPECT_EQ(3, RGWRateLimitEntry::compute_delay(10000, 201, 100));
}what do you think?
p.s. please update other test cases to verify the returned delay instead of true/false:
bool success = ratelimit.should_rate_limit("PUT", key, time, &info, "");
EXPECT_EQ(false, success);There was a problem hiding this comment.
Pushed the refactor + tests:
compute_delayis now a class-static(limit, needed, interval)helper, public so the unit test can call it directly. Each dimension (ops, bw) calls it independently and the caller takes the max, so a future listing-latency limit can slot in without reshaping the helper.- Two small deviations from your sketch: (1) early-return on
needed <= 0insidecompute_delayso it doesn't rely on a downstream max-with-0 to clamp big negatives on the bytes path, and (2) the 1-second floor lives in theshould_rate_limit_*callers, keepingcompute_delaya pure ceil-division. Happy to change either back if you'd rather. - Added
TEST(RGWRateLimitEntry, compute_delay)using your cases verbatim plus the negative-deficit guard. - Mechanically rewrote the existing tests from
EXPECT_EQ(true/false, success)to delay-based checks. Two representative cases (RGWRateLimit.reject_op_over_limitandRGWRateLimitEntry.reject_op_over_limit) now assert the exact delay (60s with default interval andmax_read_ops=1) so the helper has end-to-end coverage as well as unit coverage.
A few things I noticed but deliberately left for a follow-up so this PR stays focused — happy to file a tracker for them: no jitter on the returned delay (synchronized retries from many clients), the ceil-up can leave a polite client one tick short of replenishment, and the bytes-limited estimate doesn't know the size of the next request. Let me know if you'd rather see any of those bundled here instead.
There was a problem hiding this comment.
thanks for raising these other issues
no jitter on the returned delay (synchronized retries from many clients)
i think it will be hard to avoid the need for extra retries whenever multiple clients share a rate limit configuration, since any delay we return in the Retry-After header is likely going to be invalidated by successful requests in the meantime
would adding jitter to the delays reduce the number of retries somehow? or is the concern about fairness?
because we expect retries to be common, it's important that the server can reject rate-limited requests as cheaply as possible. for bucket-level operations, we should be able to satisfy these requests out of our metadata cache without going to rados at all. but for object operations, we do have to read the head object's metadata in rgw_build_object_policies() in order to do authorization in verify_permission(). ideally we would be able to apply rate limits before that object read happens
There was a problem hiding this comment.
the ceil-up can leave a polite client one tick short of replenishment
can you clarify this part? are you saying that compute_delay() is overestimating the replenishment rate, such that a retry still gets rejected?
There was a problem hiding this comment.
the bytes-limited estimate doesn't know the size of the next request
yeah, the rate limit checks themselves don't take the current request's size into account at all. some requests like PutObject with Transfer-Encoding: chunked don't know the size up front. so we just accept a request if bytes > 0, and call decrease_bytes() as we read/write to push back later rate-limit checks
because the rate-limit check doesn't take size into account either, i'd expect the estimate from compute_delay() to still accurately predict when it will accept the next request
There was a problem hiding this comment.
Not quite — the math itself is fine, ceil-up always biases slightly long.
The edge case is that increase_tokens() runs on-demand (not continuously), and the time_in_ms calculation truncates. So a client retrying at exactly t = Retry-After with no intervening requests could see one more rejection before the next tick fills the gap. Vanishingly unlikely in practice — the ceil bias, ~60ms tick granularity, and network RTT all work as natural buffers.
4705d29 to
234c0ac
Compare
When RGW rejects an S3 request with -ERR_RATE_LIMITED, return an HTTP Retry-After header that tells the client how many seconds to wait before retrying. The value is computed from the current token-bucket deficit and replenishment rate, so a polite client that honors the header will retry right around when its tokens recover. The delay is threaded out of the rate limiter through req_state and onto the response in abort_early(). The rate-limit check now returns int64_t seconds (0 = allowed) instead of bool, with a 1-second floor on the rejected path. The seconds-to-recover calculation is factored into a small static helper RateLimiterEntry::compute_delay(limit, needed, interval) so it can be unit-tested directly. Each dimension (ops, bytes) calls it independently and the caller takes the max, leaving room for additional limit dimensions in the future. The user-facing documentation is updated to describe the new header and to suggest proxy-layer remapping for operators who need 429 instead of the existing 503. Tracker: https://tracker.ceph.com/issues/64083 Tracker: https://tracker.ceph.com/issues/67655 Signed-off-by: Lumir Sliva <[email protected]>
234c0ac to
3dae903
Compare
|
Pr testing completed. Tacker approved by @ivancich. More details : https://tracker.ceph.com/issues/76230 |
@cbodley please merge it, thx |
|
thanks @lumir-sliva! |
Backport-Id: 01a0fca1-b10e-7c21-97cc-f83d8a61565c Backport-PR: #20 Upstream-PRs: ceph#68210
…change (#25) Backport-Id: 01a11a76-84c1-7371-840f-b82c5fbf1a72 Backport-PR: #25 Upstream-PRs: ceph#64762, ceph#66509, ceph#68210
Backport-Id: 01a0fca1-b10e-7c21-97cc-f83d8a61565c Backport-PR: #20 Upstream-PRs: ceph#68210
Backport-Id: 01a14463-7538-78b6-84eb-94e4df488626 Backport-PR: #37 Upstream-PRs: ceph#68210
Rate-limited S3 requests currently return a bare 503 SlowDown with no
guidance for clients on when to retry, so well-behaved clients can't
back off appropriately and aggressive clients (e.g. aws-sdk-go/Loki,
tracker #67655) keep retrying in a tight loop. Approach was outlined
on tracker #64083 before submitting.
This PR adds a
Retry-Afterheader to rate-limited responses, witha value computed from the actual token-bucket state — it decrements
as tokens replenish, so a polite client retrying when the header
expires will land right as its tokens recover. The status code stays
503 for S3 (
SlowDown) and 498 for Swift (Rate Limited); operatorswho need 429 can remap in a proxy layer.
How the delay is computed
RateLimiterEntry::should_rate_limit_*()now returnsint64_tseconds (0 = allowed) instead of
bool. The delay is derived fromthe current token deficit and the configured replenishment rate via
a small static helper:
Each dimension (ops, bytes) calls the helper independently and the
caller takes the max.
delay > 0is the sole rate-limit decision —no separate boolean checks. Keeping each dimension separate leaves
room for additional limits (e.g. a future listing latency limit) to
slot in without reshaping the helper.
The delay is threaded out of the rate limiter through
req_state::ratelimit_retry_afterand onto the response inabort_early().Tests
TEST(RGWRateLimitEntry, compute_delay)exercises the helperdirectly: disabled limit, zero/negative deficit, ceil division at
interval=1 and interval=100.
test_rgw_ratelimit.cccases converted fromEXPECT_EQ(true/false, success)to delay-based checks(
EXPECT_EQ(0, delay)/EXPECT_GT(delay, 0)).RGWRateLimit.reject_op_over_limitandRGWRateLimitEntry.reject_op_over_limit) assert the exact delayvalue (
EXPECT_EQ(60, delay)with default interval=60s andmax_read_ops=1), providing end-to-end coverage from the helperthrough
should_rate_limit_read.Lab verification (ceph-dev-tt4 vstart)
The decrementing behavior over time is the key validation that the
token-bucket state is actually being used (rather than a static
estimate).
Tracker: https://tracker.ceph.com/issues/64083
Tracker: https://tracker.ceph.com/issues/67655
Checklist