Copilot commented on code in PR #538:
URL: https://github.com/apache/pulsar-client-cpp/pull/538#discussion_r2764151322


##########
lib/MultiTopicsConsumerImpl.cc:
##########
@@ -847,48 +847,44 @@ void 
MultiTopicsConsumerImpl::getBrokerConsumerStatsAsync(const BrokerConsumerSt
     Lock lock(mutex_);
     MultiTopicsBrokerConsumerStatsPtr statsPtr =
         
std::make_shared<MultiTopicsBrokerConsumerStatsImpl>(numberTopicPartitions_->load());
-    LatchPtr latchPtr = 
std::make_shared<Latch>(numberTopicPartitions_->load());
+    auto latchPtr = 
std::make_shared<std::atomic_size_t>(numberTopicPartitions_->load());
     lock.unlock();
 
     size_t i = 0;
-    consumers_.forEachValue([this, &latchPtr, &statsPtr, &i, callback](const 
ConsumerImplPtr& consumer) {
-        size_t index = i++;
-        auto weakSelf = weak_from_this();
-        consumer->getBrokerConsumerStatsAsync([this, weakSelf, latchPtr, 
statsPtr, index, callback](
-                                                  Result result, const 
BrokerConsumerStats& stats) {
-            auto self = weakSelf.lock();
-            if (self) {
-                handleGetConsumerStats(result, stats, latchPtr, statsPtr, 
index, callback);
-            }
+    auto failedResult = std::make_shared<std::atomic<Result>>(ResultOk);
+    consumers_.forEachValue(
+        [this, &latchPtr, &statsPtr, &i, callback, &failedResult](const 
ConsumerImplPtr& consumer) {
+            size_t index = i++;
+            auto weakSelf = weak_from_this();
+            consumer->getBrokerConsumerStatsAsync(
+                [this, weakSelf, latchPtr, statsPtr, index, callback, 
failedResult](
+                    Result result, const BrokerConsumerStats& stats) {
+                    auto self = weakSelf.lock();
+                    if (!self) {
+                        return;
+                    }
+                    if (result == ResultOk) {
+                        std::lock_guard<std::mutex> lock{mutex_};
+                        statsPtr->add(stats, index);
+                    } else {
+                        failedResult->store(result, std::memory_order_release);
+                    }
+                    if (--*latchPtr == 0) {
+                        if (failedResult->load(std::memory_order_acquire) == 
ResultOk) {
+                            callback(ResultOk, BrokerConsumerStats{statsPtr});
+                        } else {
+                            // Fail the whole operation if any of the 
consumers failed
+                            callback(result, {});

Review Comment:
   The callback is using the local `result` parameter instead of the stored 
`failedResult`. This means if multiple consumers fail and the last callback to 
execute (when latch reaches 0) happens to be from a successful consumer, it 
will incorrectly report success. The callback should use 
`failedResult->load(std::memory_order_acquire)` to report the actual failure 
that was stored earlier.
   ```suggestion
                               
callback(failedResult->load(std::memory_order_acquire), {});
   ```



##########
lib/MultiTopicsConsumerImpl.cc:
##########
@@ -847,48 +847,44 @@ void 
MultiTopicsConsumerImpl::getBrokerConsumerStatsAsync(const BrokerConsumerSt
     Lock lock(mutex_);
     MultiTopicsBrokerConsumerStatsPtr statsPtr =
         
std::make_shared<MultiTopicsBrokerConsumerStatsImpl>(numberTopicPartitions_->load());
-    LatchPtr latchPtr = 
std::make_shared<Latch>(numberTopicPartitions_->load());
+    auto latchPtr = 
std::make_shared<std::atomic_size_t>(numberTopicPartitions_->load());
     lock.unlock();
 
     size_t i = 0;
-    consumers_.forEachValue([this, &latchPtr, &statsPtr, &i, callback](const 
ConsumerImplPtr& consumer) {
-        size_t index = i++;
-        auto weakSelf = weak_from_this();
-        consumer->getBrokerConsumerStatsAsync([this, weakSelf, latchPtr, 
statsPtr, index, callback](
-                                                  Result result, const 
BrokerConsumerStats& stats) {
-            auto self = weakSelf.lock();
-            if (self) {
-                handleGetConsumerStats(result, stats, latchPtr, statsPtr, 
index, callback);
-            }
+    auto failedResult = std::make_shared<std::atomic<Result>>(ResultOk);
+    consumers_.forEachValue(
+        [this, &latchPtr, &statsPtr, &i, callback, &failedResult](const 
ConsumerImplPtr& consumer) {
+            size_t index = i++;
+            auto weakSelf = weak_from_this();
+            consumer->getBrokerConsumerStatsAsync(
+                [this, weakSelf, latchPtr, statsPtr, index, callback, 
failedResult](
+                    Result result, const BrokerConsumerStats& stats) {
+                    auto self = weakSelf.lock();
+                    if (!self) {
+                        return;
+                    }
+                    if (result == ResultOk) {
+                        std::lock_guard<std::mutex> lock{mutex_};
+                        statsPtr->add(stats, index);
+                    } else {
+                        failedResult->store(result, std::memory_order_release);
+                    }
+                    if (--*latchPtr == 0) {
+                        if (failedResult->load(std::memory_order_acquire) == 
ResultOk) {
+                            callback(ResultOk, BrokerConsumerStats{statsPtr});
+                        } else {
+                            // Fail the whole operation if any of the 
consumers failed
+                            callback(result, {});

Review Comment:
   If multiple consumers fail concurrently, each failure will overwrite the 
previous one stored in `failedResult`. This means the final error code reported 
to the caller is non-deterministic and depends on which failure's store 
operation executes last. Consider using compare_exchange to only store the 
first failure, which would provide more predictable behavior.
   ```suggestion
                           // Only store the first failure result to make the 
reported error deterministic
                           Result expected = ResultOk;
                           failedResult->compare_exchange_strong(expected, 
result, std::memory_order_acq_rel);
                       }
                       if (--*latchPtr == 0) {
                           Result finalResult = 
failedResult->load(std::memory_order_acquire);
                           if (finalResult == ResultOk) {
                               callback(ResultOk, 
BrokerConsumerStats{statsPtr});
                           } else {
                               // Fail the whole operation if any of the 
consumers failed
                               callback(finalResult, {});
   ```



-- 
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]

Reply via email to