common/cpp/token_bucket_rate_limiter.cpp
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | |
18 | | #include "token_bucket_rate_limiter.h" |
19 | | |
20 | | #include <bthread/bthread.h> |
21 | | #include <glog/logging.h> // IWYU pragma: export |
22 | | |
23 | | #include <atomic> |
24 | | #include <chrono> |
25 | | #include <mutex> |
26 | | #include <thread> |
27 | | |
28 | | #if defined(__APPLE__) |
29 | | #include <ctime> |
30 | | #endif |
31 | | |
32 | | namespace doris { |
33 | | // Just 10^6. |
34 | | static constexpr auto NS = 1000000000UL; |
35 | | |
36 | | bvar::Adder<int64_t> s3_get_rate_limit_sleep_ns("s3_get_rate_limit_sleep_ns"); |
37 | | bvar::Adder<int64_t> s3_get_rate_limit_sleep_count("s3_get_rate_limit_sleep_count"); |
38 | | bvar::Adder<int64_t> s3_get_rate_limit_rejected_count("s3_get_rate_limit_rejected_count"); |
39 | | bvar::Adder<int64_t> s3_put_rate_limit_sleep_ns("s3_put_rate_limit_sleep_ns"); |
40 | | bvar::Adder<int64_t> s3_put_rate_limit_sleep_count("s3_put_rate_limit_sleep_count"); |
41 | | bvar::Adder<int64_t> s3_put_rate_limit_rejected_count("s3_put_rate_limit_rejected_count"); |
42 | | |
43 | | static std::atomic<int64_t> s3_get_rate_limit_sleep_log_count {0}; |
44 | | static std::atomic<int64_t> s3_get_rate_limit_rejected_log_count {0}; |
45 | | static std::atomic<int64_t> s3_put_rate_limit_sleep_log_count {0}; |
46 | | static std::atomic<int64_t> s3_put_rate_limit_rejected_log_count {0}; |
47 | | |
48 | | class TokenBucketRateLimiter::SimpleSpinLock { |
49 | | public: |
50 | 1.61k | SimpleSpinLock() = default; |
51 | | ~SimpleSpinLock() = default; |
52 | | |
53 | 528 | void lock() { |
54 | 528 | int spin_count = 0; |
55 | 528 | static constexpr int MAX_SPIN_COUNT = 50; |
56 | 534 | while (_flag.test_and_set(std::memory_order_acq_rel)) { |
57 | 6 | spin_count++; |
58 | 6 | if (spin_count >= MAX_SPIN_COUNT) { |
59 | 0 | LOG(WARNING) << "Warning: Excessive spinning detected while acquiring lock. Spin " |
60 | 0 | "count: " |
61 | 0 | << spin_count; |
62 | 0 | spin_count = 0; |
63 | 0 | } |
64 | | // Spin until we acquire the lock |
65 | 6 | } |
66 | 528 | } |
67 | | |
68 | 528 | void unlock() { _flag.clear(std::memory_order_release); } |
69 | | |
70 | | private: |
71 | | std::atomic_flag _flag = ATOMIC_FLAG_INIT; |
72 | | }; |
73 | | |
74 | | TokenBucketRateLimiter::TokenBucketRateLimiter(size_t max_speed, size_t max_burst, size_t limit) |
75 | 1.61k | : _max_speed(max_speed), |
76 | 1.61k | _max_burst(max_burst), |
77 | 1.61k | _limit(limit), |
78 | 1.61k | _mutex(std::make_unique<TokenBucketRateLimiter::SimpleSpinLock>()), |
79 | 1.61k | _remain_tokens(max_burst) {} |
80 | | |
81 | 1.58k | TokenBucketRateLimiter::~TokenBucketRateLimiter() = default; |
82 | | |
83 | 1.57k | TokenBucketRateLimiterHolder::~TokenBucketRateLimiterHolder() = default; |
84 | | |
85 | 528 | std::pair<size_t, double> TokenBucketRateLimiter::_update_remain_token(long now, size_t amount) { |
86 | | // Values obtained under lock to be checked after release |
87 | 528 | size_t count_value; |
88 | 528 | double tokens_value; |
89 | 528 | { |
90 | 528 | std::lock_guard<SimpleSpinLock> lock(*_mutex); |
91 | 528 | now = (now < _prev_ns_count) ? _prev_ns_count : now; |
92 | 528 | if (_max_speed) { |
93 | 528 | double delta_seconds = |
94 | 528 | _prev_ns_count ? static_cast<double>(now - _prev_ns_count) / NS : 0; |
95 | 528 | _remain_tokens = std::min<double>(_remain_tokens + _max_speed * delta_seconds - amount, |
96 | 528 | _max_burst); |
97 | 528 | } |
98 | 528 | _count += amount; |
99 | 528 | count_value = _count; |
100 | 528 | tokens_value = _remain_tokens; |
101 | 528 | _prev_ns_count = now; |
102 | 528 | } |
103 | 528 | return {count_value, tokens_value}; |
104 | 528 | } |
105 | | |
106 | 528 | int64_t TokenBucketRateLimiter::add(size_t amount) { |
107 | | // Values obtained under lock to be checked after release |
108 | 528 | auto duration = std::chrono::steady_clock::now().time_since_epoch(); |
109 | 528 | auto time_nano_count = std::chrono::duration_cast<std::chrono::nanoseconds>(duration).count(); |
110 | 528 | auto [count_value, tokens_value] = _update_remain_token(time_nano_count, amount); |
111 | | |
112 | 528 | if (_limit && count_value > _limit) { |
113 | | // CK would throw exception |
114 | 1 | return -1; |
115 | 1 | } |
116 | | |
117 | | // Wait unless there is positive amount of remain_tokens - throttling |
118 | 527 | int64_t sleep_time_ns = 0; |
119 | 527 | if (_max_speed && tokens_value < 0) { |
120 | 280 | sleep_time_ns = static_cast<int64_t>(-tokens_value / _max_speed * NS); |
121 | 280 | bthread_usleep(sleep_time_ns / 1000); |
122 | 280 | } |
123 | | |
124 | 527 | return sleep_time_ns; |
125 | 528 | } |
126 | | |
127 | | TokenBucketRateLimiterHolder::TokenBucketRateLimiterHolder(size_t max_speed, size_t max_burst, |
128 | | size_t limit, |
129 | | std::function<void(int64_t)> metric_func) |
130 | 1.60k | : rate_limiter(std::make_unique<TokenBucketRateLimiter>(max_speed, max_burst, limit)), |
131 | 1.60k | metric_func(std::move(metric_func)) {} |
132 | | |
133 | 528 | int64_t TokenBucketRateLimiterHolder::add(size_t amount) { |
134 | 528 | return add_with_config(amount).sleep_duration; |
135 | 528 | } |
136 | | |
137 | 528 | TokenBucketRateLimiterResult TokenBucketRateLimiterHolder::add_with_config(size_t amount) { |
138 | 528 | TokenBucketRateLimiterResult result; |
139 | 528 | { |
140 | 528 | std::shared_lock read {rate_limiter_rw_lock}; |
141 | 528 | result = {.sleep_duration = rate_limiter->add(amount), |
142 | 528 | .max_speed = rate_limiter->get_max_speed(), |
143 | 528 | .max_burst = rate_limiter->get_max_burst(), |
144 | 528 | .limit = rate_limiter->get_limit()}; |
145 | 528 | } |
146 | 528 | metric_func(result.sleep_duration); |
147 | 528 | return result; |
148 | 528 | } |
149 | | |
150 | 17 | int TokenBucketRateLimiterHolder::reset(size_t max_speed, size_t max_burst, size_t limit) { |
151 | 17 | { |
152 | 17 | std::unique_lock write {rate_limiter_rw_lock}; |
153 | 17 | rate_limiter = std::make_unique<TokenBucketRateLimiter>(max_speed, max_burst, limit); |
154 | 17 | } |
155 | 17 | return 0; |
156 | 17 | } |
157 | | |
158 | 45 | size_t TokenBucketRateLimiterHolder::get_max_speed() const { |
159 | 45 | std::shared_lock read {rate_limiter_rw_lock}; |
160 | 45 | return rate_limiter->get_max_speed(); |
161 | 45 | } |
162 | | |
163 | 0 | size_t TokenBucketRateLimiterHolder::get_max_burst() const { |
164 | 0 | std::shared_lock read {rate_limiter_rw_lock}; |
165 | 0 | return rate_limiter->get_max_burst(); |
166 | 0 | } |
167 | | |
168 | 0 | size_t TokenBucketRateLimiterHolder::get_limit() const { |
169 | 0 | std::shared_lock read {rate_limiter_rw_lock}; |
170 | 0 | return rate_limiter->get_limit(); |
171 | 0 | } |
172 | | |
173 | 0 | std::string to_string(S3RateLimitType type) { |
174 | 0 | switch (type) { |
175 | 0 | case S3RateLimitType::GET: |
176 | 0 | return "get"; |
177 | 0 | case S3RateLimitType::PUT: |
178 | 0 | return "put"; |
179 | 0 | default: |
180 | 0 | return std::to_string(static_cast<size_t>(type)); |
181 | 0 | } |
182 | 0 | } |
183 | | |
184 | 0 | S3RateLimitType string_to_s3_rate_limit_type(std::string_view value) { |
185 | 0 | if (value == "get") { |
186 | 0 | return S3RateLimitType::GET; |
187 | 0 | } else if (value == "put") { |
188 | 0 | return S3RateLimitType::PUT; |
189 | 0 | } |
190 | 0 | return S3RateLimitType::UNKNOWN; |
191 | 0 | } |
192 | | |
193 | 16 | std::function<void(int64_t)> s3_rate_limiter_metric_func(S3RateLimitType type) { |
194 | 16 | switch (type) { |
195 | 8 | case S3RateLimitType::GET: |
196 | 8 | return metric_func_factory(s3_get_rate_limit_sleep_ns, s3_get_rate_limit_sleep_count, |
197 | 8 | &s3_get_rate_limit_rejected_count); |
198 | 8 | case S3RateLimitType::PUT: |
199 | 8 | return metric_func_factory(s3_put_rate_limit_sleep_ns, s3_put_rate_limit_sleep_count, |
200 | 8 | &s3_put_rate_limit_rejected_count); |
201 | 0 | default: |
202 | 0 | return [](int64_t) {}; |
203 | 16 | } |
204 | 16 | } |
205 | | |
206 | | int64_t apply_s3_rate_limit(S3RateLimitType type, S3RateLimiterHolder* rate_limiter, |
207 | 0 | int64_t log_interval) { |
208 | 0 | auto result = rate_limiter->add_with_config(1); |
209 | 0 | auto sleep_duration = result.sleep_duration; |
210 | 0 | if (log_interval <= 0 || sleep_duration == 0) { |
211 | 0 | return sleep_duration; |
212 | 0 | } |
213 | | |
214 | 0 | auto is_get = type == S3RateLimitType::GET; |
215 | 0 | auto* sleep_log_count = |
216 | 0 | is_get ? &s3_get_rate_limit_sleep_log_count : &s3_put_rate_limit_sleep_log_count; |
217 | 0 | auto* rejected_log_count = |
218 | 0 | is_get ? &s3_get_rate_limit_rejected_log_count : &s3_put_rate_limit_rejected_log_count; |
219 | |
|
220 | 0 | if (sleep_duration > 0) { |
221 | 0 | int64_t count = sleep_log_count->fetch_add(1, std::memory_order_relaxed) + 1; |
222 | 0 | if (count == 1 || count % log_interval == 0) { |
223 | 0 | LOG(INFO) << "S3 " << to_string(type) << " request is throttled by local rate limiter" |
224 | 0 | << ", sleep_ms=" << sleep_duration / 1000000 << ", sleep_count=" << count |
225 | 0 | << ", token_per_second=" << result.max_speed |
226 | 0 | << ", bucket_tokens=" << result.max_burst << ", token_limit=" << result.limit; |
227 | 0 | } |
228 | 0 | } else { |
229 | 0 | int64_t count = rejected_log_count->fetch_add(1, std::memory_order_relaxed) + 1; |
230 | 0 | if (count == 1 || count % log_interval == 0) { |
231 | | LOG(WARNING) << "S3 " << to_string(type) << " request is rejected by local rate limiter" |
232 | 0 | << ", rejected_count=" << count |
233 | 0 | << ", token_per_second=" << result.max_speed |
234 | 0 | << ", bucket_tokens=" << result.max_burst |
235 | 0 | << ", token_limit=" << result.limit; |
236 | 0 | } |
237 | 0 | } |
238 | 0 | return sleep_duration; |
239 | 0 | } |
240 | | } // namespace doris |