wyxxxcat commented on code in PR #55544: URL: https://github.com/apache/doris/pull/55544#discussion_r2329680194
########## cloud/src/common/bvars.h: ########## @@ -179,6 +184,180 @@ class mBvarWrapper { bvar::MultiDimension<BvarType> counter_; }; +template <int N = 60> +class BvarLatencyRecorderWithStatus { +private: + bvar::LatencyRecorder recorder_; + bvar::PassiveStatus<int64_t> max_status_; + bvar::PassiveStatus<int64_t> avg_status_; + + static int64_t get_max_latency(void* arg) { + auto* self = static_cast<BvarLatencyRecorderWithStatus*>(arg); + int64_t value = self->recorder_.max_latency(); + return value >= 0 ? value : 0; + } + + static int64_t get_avg_latency(void* arg) { + auto* self = static_cast<BvarLatencyRecorderWithStatus*>(arg); + int64_t value = self->recorder_.latency(); + return value >= 0 ? value : 0; + } + +public: + BvarLatencyRecorderWithStatus(const std::string& prefix, const std::string& metric_name) + : recorder_(N), + max_status_(metric_name + "_max", get_max_latency, this), + avg_status_(metric_name + "_avg", get_avg_latency, this) { + recorder_.hide(); + } + + BvarLatencyRecorderWithStatus(const std::string& metric_name) + : recorder_(N), + max_status_(metric_name + "_max", get_max_latency, this), + avg_status_(metric_name + "_avg", get_avg_latency, this) { + recorder_.hide(); + } + + void put(int64_t value) { recorder_ << value; } + + void operator<<(int64_t value) { recorder_ << value; } +}; + +class DeferredTimer { +public: + using on_timer_t = void (*)(void*); + + DeferredTimer(on_timer_t timer_func, size_t interval_s, void* arg = nullptr) + : _timer_func(timer_func), _interval_s(interval_s), _arg(arg), _arg_valid(true) {} + + ~DeferredTimer() { stop(); } + + bool start() { + if (_started) return true; + + if (!bthread_self()) { + LOG(WARNING) << "Bthread not initialized, timer will not start"; + return false; + } + + if (bthread_timer_add(&_timer, butil::seconds_from_now(_interval_s), safe_callback_wrapper, + this) != 0) { + LOG(WARNING) << "Failed to add bthread timer"; + return false; + } + _started = true; + return true; + } + + void stop() { + if (_started) { + bthread_timer_del(_timer); + _started = false; + } + _arg_valid = false; + } + + void set_invalidate_arg() { _arg_valid = false; } + +private: + static void safe_callback_wrapper(void* timer_ptr) { + auto* timer = static_cast<DeferredTimer*>(timer_ptr); + if (timer && timer->_timer_func && timer->_arg_valid && timer->_arg) { + timer->_timer_func(timer->_arg); + } + } + + on_timer_t _timer_func; + int _interval_s; + void* _arg; + bthread_timer_t _timer; + bool _started {false}; + std::atomic<bool> _arg_valid {true}; +}; + +template <int N = 60> +class mBvarLatencyRecorderWithStatus { +private: + std::string _metric_name; + std::map<std::list<std::string>, std::shared_ptr<bvar::LatencyRecorder>> recorder_; Review Comment: bvar::MultiDimension::key_type is std::list\<std::string\> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@doris.apache.org For additional commands, e-mail: commits-h...@doris.apache.org