@@ -847,48 +847,47 @@ void MultiTopicsConsumerImpl::getBrokerConsumerStatsAsync(const BrokerConsumerSt
847847 Lock lock (mutex_);
848848 MultiTopicsBrokerConsumerStatsPtr statsPtr =
849849 std::make_shared<MultiTopicsBrokerConsumerStatsImpl>(numberTopicPartitions_->load ());
850- LatchPtr latchPtr = std::make_shared<Latch >(numberTopicPartitions_->load ());
850+ auto latchPtr = std::make_shared<std:: atomic_size_t >(numberTopicPartitions_->load ());
851851 lock.unlock ();
852852
853853 size_t i = 0 ;
854- consumers_.forEachValue ([this , &latchPtr, &statsPtr, &i, callback](const ConsumerImplPtr& consumer) {
855- size_t index = i++;
856- auto weakSelf = weak_from_this ();
857- consumer->getBrokerConsumerStatsAsync ([this , weakSelf, latchPtr, statsPtr, index, callback](
858- Result result, const BrokerConsumerStats& stats) {
859- auto self = weakSelf.lock ();
860- if (self) {
861- handleGetConsumerStats (result, stats, latchPtr, statsPtr, index, callback);
862- }
854+ auto failedResult = std::make_shared<std::atomic<Result>>(ResultOk);
855+ consumers_.forEachValue (
856+ [this , &latchPtr, &statsPtr, &i, callback, &failedResult](const ConsumerImplPtr& consumer) {
857+ size_t index = i++;
858+ auto weakSelf = weak_from_this ();
859+ consumer->getBrokerConsumerStatsAsync (
860+ [this , weakSelf, latchPtr, statsPtr, index, callback, failedResult](
861+ Result result, const BrokerConsumerStats& stats) {
862+ auto self = weakSelf.lock ();
863+ if (!self) {
864+ return ;
865+ }
866+ if (result == ResultOk) {
867+ std::lock_guard<std::mutex> lock{mutex_};
868+ statsPtr->add (stats, index);
869+ } else {
870+ // Store the first failed result as the final failed result
871+ auto expected = ResultOk;
872+ failedResult->compare_exchange_strong (expected, result);
873+ }
874+ if (--*latchPtr == 0 ) {
875+ if (auto firstFailedResult = failedResult->load (std::memory_order_acquire);
876+ firstFailedResult == ResultOk) {
877+ callback (ResultOk, BrokerConsumerStats{statsPtr});
878+ } else {
879+ // Fail the whole operation if any of the consumers failed
880+ callback (firstFailedResult, {});
881+ }
882+ }
883+ });
863884 });
864- });
865885}
866886
867887void MultiTopicsConsumerImpl::getLastMessageIdAsync (const BrokerGetLastMessageIdCallback& callback) {
868888 callback (ResultOperationNotSupported, GetLastMessageIdResponse ());
869889}
870890
871- void MultiTopicsConsumerImpl::handleGetConsumerStats (Result res,
872- const BrokerConsumerStats& brokerConsumerStats,
873- const LatchPtr& latchPtr,
874- const MultiTopicsBrokerConsumerStatsPtr& statsPtr,
875- size_t index,
876- const BrokerConsumerStatsCallback& callback) {
877- Lock lock (mutex_);
878- if (res == ResultOk) {
879- latchPtr->countdown ();
880- statsPtr->add (brokerConsumerStats, index);
881- } else {
882- lock.unlock ();
883- callback (res, BrokerConsumerStats ());
884- return ;
885- }
886- if (latchPtr->getCount () == 0 ) {
887- lock.unlock ();
888- callback (ResultOk, BrokerConsumerStats (statsPtr));
889- }
890- }
891-
892891std::shared_ptr<TopicName> MultiTopicsConsumerImpl::topicNamesValid (const std::vector<std::string>& topics) {
893892 TopicNamePtr topicNamePtr = std::shared_ptr<TopicName>();
894893
0 commit comments