shibd commented on code in PR #13079:
URL: https://github.com/apache/pulsar/pull/13079#discussion_r949755029
##########
pulsar-client-cpp/lib/stats/ConsumerStatsImpl.cc:
##########
@@ -30,58 +31,76 @@ ConsumerStatsImpl::ConsumerStatsImpl(std::string
consumerStr, ExecutorServicePtr
: consumerStr_(consumerStr),
executor_(executor),
timer_(executor_->createDeadlineTimer()),
- statsIntervalInSeconds_(statsIntervalInSeconds) {
-
timer_->expires_from_now(boost::posix_time::seconds(statsIntervalInSeconds_));
- timer_->async_wait(std::bind(&pulsar::ConsumerStatsImpl::flushAndReset,
this, std::placeholders::_1));
+ statsIntervalInSeconds_(statsIntervalInSeconds),
+ callbackLock_(std::make_shared<stats::AsyncCallbackLock>()) {
+ scheduleReport();
}
-ConsumerStatsImpl::ConsumerStatsImpl(const ConsumerStatsImpl& stats)
- : consumerStr_(stats.consumerStr_),
- numBytesRecieved_(stats.numBytesRecieved_),
- receivedMsgMap_(stats.receivedMsgMap_),
- ackedMsgMap_(stats.ackedMsgMap_),
- totalNumBytesRecieved_(stats.totalNumBytesRecieved_),
- totalReceivedMsgMap_(stats.totalReceivedMsgMap_),
- totalAckedMsgMap_(stats.totalAckedMsgMap_),
- statsIntervalInSeconds_(stats.statsIntervalInSeconds_) {}
-
void ConsumerStatsImpl::flushAndReset(const boost::system::error_code& ec) {
if (ec) {
LOG_DEBUG("Ignoring timer cancelled event, code[" << ec << "]");
return;
}
- Lock lock(mutex_);
- ConsumerStatsImpl tmp = *this;
- numBytesRecieved_ = 0;
- receivedMsgMap_.clear();
- ackedMsgMap_.clear();
- lock.unlock();
+ const bool isLogLevelEnabled = logger()->isEnabled(Logger::LEVEL_INFO);
+ std::string logMessage;
-
timer_->expires_from_now(boost::posix_time::seconds(statsIntervalInSeconds_));
- timer_->async_wait(std::bind(&pulsar::ConsumerStatsImpl::flushAndReset,
this, std::placeholders::_1));
- LOG_INFO(tmp);
+ {
+ Lock lockRcv(rcvMutex_);
+ Lock lockAck(ackMutex_);
+
+ if (isLogLevelEnabled) {
+ std::ostringstream ss;
+ ss << *this;
+ logMessage = ss.str();
+ }
+
+ numBytesReceived_ = 0;
+ receivedMsgMap_.clear();
+ ackedMsgMap_.clear();
+ };
+
+ scheduleReport();
+
+ if (isLogLevelEnabled) {
+ LOG_INFO(logMessage);
+ }
}
ConsumerStatsImpl::~ConsumerStatsImpl() {
- Lock lock(mutex_);
- if (timer_) {
+ if (callbackLock_) {
+ Lock lock(callbackLock_->mutex);
+
+ callbackLock_->enabled = false;
timer_->cancel();
}
}
+void ConsumerStatsImpl::scheduleReport() {
+ auto callbackLock = callbackLock_;
+ auto flushCallback = [this, callbackLock](const boost::system::error_code&
error_code) {
+ Lock lock(callbackLock->mutex);
Review Comment:
When has `error_code`, need to ignore this execute.
```java
if(!error_code) {
Lock lock(callbackLock->mutex);
if (callbackLock->enabled) {
flushAndReset(error_code);
}
}
```
--
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]