|
| 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 "brpc/backup_request_policy.h" |
| 19 | + |
| 20 | +#include <gflags/gflags.h> |
| 21 | +#include "brpc/reloadable_flags.h" |
| 22 | +#include "bvar/reducer.h" |
| 23 | +#include "bvar/window.h" |
| 24 | +#include "butil/atomicops.h" |
| 25 | +#include "butil/time.h" |
| 26 | + |
| 27 | +namespace brpc { |
| 28 | + |
| 29 | +DEFINE_double(backup_request_max_ratio, -1, |
| 30 | + "Maximum ratio of backup requests to total requests. " |
| 31 | + "Value in (0, 1]. -1 means no limit (default). Can be overridden " |
| 32 | + "per-channel via ChannelOptions.backup_request_max_ratio. " |
| 33 | + "Note: takes effect at Channel::Init() time; changing this flag " |
| 34 | + "at runtime does not affect already-created channels."); |
| 35 | + |
| 36 | +static bool validate_backup_request_max_ratio(const char*, double v) { |
| 37 | + if (v <= 0) return true; // non-positive means disabled |
| 38 | + if (v <= 1.0) return true; |
| 39 | + LOG(ERROR) << "Invalid backup_request_max_ratio=" << v |
| 40 | + << ", must be <= 0 (disabled) or in (0, 1]"; |
| 41 | + return false; |
| 42 | +} |
| 43 | +BRPC_VALIDATE_GFLAG(backup_request_max_ratio, |
| 44 | + validate_backup_request_max_ratio); |
| 45 | + |
| 46 | +DEFINE_int32(backup_request_ratio_window_size_s, 10, |
| 47 | + "Window size in seconds for computing the backup request ratio. " |
| 48 | + "Must be >= 1."); |
| 49 | + |
| 50 | +static bool validate_backup_request_ratio_window_size_s( |
| 51 | + const char*, int32_t v) { |
| 52 | + if (v >= 1) return true; |
| 53 | + LOG(ERROR) << "Invalid backup_request_ratio_window_size_s=" << v |
| 54 | + << ", must be >= 1"; |
| 55 | + return false; |
| 56 | +} |
| 57 | +BRPC_VALIDATE_GFLAG(backup_request_ratio_window_size_s, |
| 58 | + validate_backup_request_ratio_window_size_s); |
| 59 | + |
| 60 | +DEFINE_int32(backup_request_ratio_update_interval_s, 5, |
| 61 | + "Interval in seconds between ratio cache updates. Must be >= 1."); |
| 62 | + |
| 63 | +static bool validate_backup_request_ratio_update_interval_s( |
| 64 | + const char*, int32_t v) { |
| 65 | + if (v >= 1) return true; |
| 66 | + LOG(ERROR) << "Invalid backup_request_ratio_update_interval_s=" << v |
| 67 | + << ", must be >= 1"; |
| 68 | + return false; |
| 69 | +} |
| 70 | +BRPC_VALIDATE_GFLAG(backup_request_ratio_update_interval_s, |
| 71 | + validate_backup_request_ratio_update_interval_s); |
| 72 | + |
| 73 | +// Standalone statistics module for tracking backup/total request ratio |
| 74 | +// within a sliding time window. |
| 75 | +class BackupRateLimiter { |
| 76 | +public: |
| 77 | + BackupRateLimiter(double max_backup_ratio, |
| 78 | + int window_size_seconds, |
| 79 | + int update_interval_seconds) |
| 80 | + : _max_backup_ratio(max_backup_ratio) |
| 81 | + , _update_interval_us(update_interval_seconds * 1000000LL) |
| 82 | + , _total_window(&_total_count, window_size_seconds) |
| 83 | + , _backup_window(&_backup_count, window_size_seconds) |
| 84 | + , _cached_ratio(0.0) |
| 85 | + , _last_update_us(0) { |
| 86 | + } |
| 87 | + |
| 88 | + // All atomic operations use relaxed ordering intentionally. |
| 89 | + // This is best-effort rate limiting: a slightly stale ratio is |
| 90 | + // acceptable for approximate throttling. |
| 91 | + bool ShouldAllow() const { |
| 92 | + const int64_t now_us = butil::cpuwide_time_us(); |
| 93 | + int64_t last_us = _last_update_us.load(butil::memory_order_relaxed); |
| 94 | + double ratio = _cached_ratio.load(butil::memory_order_relaxed); |
| 95 | + |
| 96 | + if (now_us - last_us >= _update_interval_us) { |
| 97 | + if (_last_update_us.compare_exchange_strong( |
| 98 | + last_us, now_us, butil::memory_order_relaxed)) { |
| 99 | + int64_t total = _total_window.get_value(); |
| 100 | + int64_t backup = _backup_window.get_value(); |
| 101 | + ratio = (total > 0) ? static_cast<double>(backup) / total : 0.0; |
| 102 | + _cached_ratio.store(ratio, butil::memory_order_relaxed); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // max_backup_ratio >= 1.0 means no limit (ratio cannot exceed 1.0). |
| 107 | + return _max_backup_ratio >= 1.0 || ratio < _max_backup_ratio; |
| 108 | + } |
| 109 | + |
| 110 | + void OnRPCEnd(const Controller* controller) { |
| 111 | + _total_count << 1; |
| 112 | + if (controller->has_backup_request()) { |
| 113 | + _backup_count << 1; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | +private: |
| 118 | + double _max_backup_ratio; |
| 119 | + int64_t _update_interval_us; |
| 120 | + |
| 121 | + bvar::Adder<int64_t> _total_count; |
| 122 | + bvar::Adder<int64_t> _backup_count; |
| 123 | + bvar::Window<bvar::Adder<int64_t>> _total_window; |
| 124 | + bvar::Window<bvar::Adder<int64_t>> _backup_window; |
| 125 | + |
| 126 | + mutable butil::atomic<double> _cached_ratio; |
| 127 | + mutable butil::atomic<int64_t> _last_update_us; |
| 128 | +}; |
| 129 | + |
| 130 | +// Internal BackupRequestPolicy that composes a BackupRateLimiter |
| 131 | +// for ratio-based suppression. |
| 132 | +class RateLimitedBackupPolicy : public BackupRequestPolicy { |
| 133 | +public: |
| 134 | + RateLimitedBackupPolicy(int32_t backup_request_ms, |
| 135 | + double max_backup_ratio, |
| 136 | + int window_size_seconds, |
| 137 | + int update_interval_seconds) |
| 138 | + : _backup_request_ms(backup_request_ms) |
| 139 | + , _rate_limiter(max_backup_ratio, window_size_seconds, |
| 140 | + update_interval_seconds) { |
| 141 | + } |
| 142 | + |
| 143 | + int32_t GetBackupRequestMs(const Controller* /*controller*/) const override { |
| 144 | + return _backup_request_ms; |
| 145 | + } |
| 146 | + |
| 147 | + bool DoBackup(const Controller* /*controller*/) const override { |
| 148 | + return _rate_limiter.ShouldAllow(); |
| 149 | + } |
| 150 | + |
| 151 | + void OnRPCEnd(const Controller* controller) override { |
| 152 | + _rate_limiter.OnRPCEnd(controller); |
| 153 | + } |
| 154 | + |
| 155 | +private: |
| 156 | + int32_t _backup_request_ms; |
| 157 | + BackupRateLimiter _rate_limiter; |
| 158 | +}; |
| 159 | + |
| 160 | +BackupRequestPolicy* CreateRateLimitedBackupPolicy( |
| 161 | + int32_t backup_request_ms, |
| 162 | + double max_backup_ratio, |
| 163 | + int window_size_seconds, |
| 164 | + int update_interval_seconds) { |
| 165 | + if (max_backup_ratio <= 0 || max_backup_ratio > 1.0) { |
| 166 | + LOG(ERROR) << "Invalid max_backup_ratio=" << max_backup_ratio |
| 167 | + << ", must be in (0, 1]"; |
| 168 | + return NULL; |
| 169 | + } |
| 170 | + if (window_size_seconds < 1) { |
| 171 | + LOG(ERROR) << "Invalid window_size_seconds=" << window_size_seconds |
| 172 | + << ", must be >= 1"; |
| 173 | + return NULL; |
| 174 | + } |
| 175 | + if (update_interval_seconds < 1) { |
| 176 | + LOG(ERROR) << "Invalid update_interval_seconds=" |
| 177 | + << update_interval_seconds << ", must be >= 1"; |
| 178 | + return NULL; |
| 179 | + } |
| 180 | + return new RateLimitedBackupPolicy( |
| 181 | + backup_request_ms, max_backup_ratio, |
| 182 | + window_size_seconds, update_interval_seconds); |
| 183 | +} |
| 184 | + |
| 185 | +} // namespace brpc |
0 commit comments