Copilot commented on code in PR #3116:
URL: https://github.com/apache/brpc/pull/3116#discussion_r2425113703


##########
src/bvar/recorder.h:
##########
@@ -286,6 +294,100 @@ inline IntRecorder& IntRecorder::operator<<(int64_t 
sample) {
                  n, _compress(num + 1, sum + complement)));
     return *this;
 }
+#else // WITH_BABYLON_COUNTER
+class IntRecorder : public Variable {
+public:
+    typedef Stat value_type;
+    typedef detail::AddStat Op;
+    typedef detail::MinusStat InvOp;
+    typedef detail::ReducerSampler<IntRecorder, value_type, Op, InvOp> 
sampler_type;
+
+    COMMON_VARIABLE_CONSTRUCTOR(IntRecorder);
+
+    DISALLOW_COPY_AND_MOVE(IntRecorder);
+
+    ~IntRecorder() override {
+        hide();
+        if (NULL != _sampler) {
+            _sampler->destroy();
+        }
+    }
+
+    // Note: The input type is acutally int. Use int64_t to check overflow.
+    IntRecorder& operator<<(int64_t value) {
+        if (BAIDU_UNLIKELY((int64_t)(int)value != value)) {
+            const char* reason = NULL;
+            if (value > std::numeric_limits<int>::max()) {
+                reason = "overflows";
+                value = std::numeric_limits<int>::max();
+            } else {
+                reason = "underflows";
+                value = std::numeric_limits<int>::min();
+            }
+            // Truncate to be max or min of int. We're using 44 bits to store 
the
+            // sum thus following aggregations are not likely to be 
over/underflow.
+            if (!name().empty()) {
+                LOG(WARNING) << "Input=" << value << " to `" << name()
+                           << "\' " << reason;
+            } else if (!_debug_name.empty()) {
+                LOG(WARNING) << "Input=" << value << " to `" << _debug_name
+                           << "\' " << reason;
+            } else {
+                LOG(WARNING) << "Input=" << value << " to IntRecorder("
+                           << (void*)this << ") " << reason;
+            }
+        }
+
+        _summer << value;
+        return *this;
+    }
+
+    int64_t average() const {
+        return get_value().get_average_int();
+    }
+
+    double average(double) const {
+        return get_value().get_average_double();
+    }
+
+    value_type get_value() const {
+        auto summary = _summer.value();
+        return value_type{summary.sum, static_cast<ssize_t>(summary.num)};
+    }
+
+    value_type reset() {
+        LOG_EVERY_SECOND(ERROR) << "IntRecorder with babylon counter should 
never call reset()";

Review Comment:
   Error message should be more descriptive about why reset() cannot be called 
and what alternative should be used.
   ```suggestion
           LOG_EVERY_SECOND(ERROR) << "IntRecorder with babylon counter does 
not support reset() because concurrent aggregation cannot be reset safely. If 
you need reset functionality, use a different recorder type (e.g., the default 
IntRecorder) that supports reset().";
   ```



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to