Coverage Report

Created: 2025-07-24 14:30

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/common/cpp/s3_rate_limiter.h
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
#pragma once
19
#include <bvar/bvar.h>
20
21
#include <functional>
22
#include <memory>
23
#include <shared_mutex>
24
25
namespace doris {
26
enum class S3RateLimitType : int {
27
    GET = 0,
28
    PUT,
29
    UNKNOWN,
30
};
31
32
extern std::string to_string(S3RateLimitType type);
33
extern S3RateLimitType string_to_s3_rate_limit_type(std::string_view value);
34
35
0
inline auto metric_func_factory(bvar::Adder<int64_t>& ns_bvar, bvar::Adder<int64_t>& req_num_bvar) {
36
0
    return [&](int64_t ns) {
37
0
        if (ns > 0) {
38
0
            ns_bvar << ns;
39
0
            req_num_bvar << 1;
40
0
        }
41
0
    };
42
0
}
43
44
class S3RateLimiter {
45
public:
46
    static constexpr size_t default_burst_seconds = 1;
47
48
    S3RateLimiter(size_t max_speed, size_t max_burst, size_t limit);
49
    ~S3RateLimiter();
50
51
    // Use `amount` remain_tokens, sleeps if required or throws exception on limit overflow.
52
    // Returns duration of sleep in nanoseconds (to distinguish sleeping on different kinds of S3RateLimiters for metrics)
53
    int64_t add(size_t amount);
54
55
private:
56
    std::pair<size_t, double> _update_remain_token(long now, size_t amount);
57
    size_t _count {0};
58
    const size_t _max_speed {0}; // in tokens per second. which indicates the QPS
59
    const size_t _max_burst {0}; // in tokens. which indicates the token bucket size
60
    const uint64_t _limit {0};   // 0 - not limited.
61
    class SimpleSpinLock;
62
    std::unique_ptr<SimpleSpinLock> _mutex;
63
    // Amount of remain_tokens available in token bucket. Updated in `add` method.
64
    double _remain_tokens {0};
65
    long _prev_ns_count {0}; // Previous `add` call time (in nanoseconds).
66
};
67
68
class S3RateLimiterHolder {
69
public:
70
    S3RateLimiterHolder(size_t max_speed, size_t max_burst, size_t limit,
71
                        std::function<void(int64_t)> metric_func);
72
    ~S3RateLimiterHolder();
73
74
    int64_t add(size_t amount);
75
76
    int reset(size_t max_speed, size_t max_burst, size_t limit);
77
78
private:
79
    std::shared_mutex rate_limiter_rw_lock;
80
    std::unique_ptr<S3RateLimiter> rate_limiter;
81
    // Record the correspoding sleeping time(unit is ms)
82
    std::function<void(int64_t)> metric_func;
83
};
84
85
} // namespace doris