@@ -961,6 +961,9 @@ Result ConsumerImpl::fetchSingleMessageFromBroker(Message& msg) {
961961 // Can't use break here else it may trigger a race with connection opened.
962962
963963 localLock.unlock ();
964+ Lock lock (mutexForMessageId_);
965+ lastDequedMessageId_ = msg.getMessageId ();
966+ lock.unlock ();
964967 msg = interceptors_->beforeConsume (Consumer (shared_from_this ()), msg);
965968 return ResultOk;
966969 }
@@ -1107,9 +1110,7 @@ void ConsumerImpl::messageProcessed(Message& msg, bool track) {
11071110 */
11081111void ConsumerImpl::clearReceiveQueue () {
11091112 if (duringSeek ()) {
1110- if (!hasSoughtByTimestamp_.load (std::memory_order_acquire)) {
1111- startMessageId_ = seekMessageId_.get ();
1112- }
1113+ trySetStartMessageIdToSeekMessageId ();
11131114 SeekStatus expected = SeekStatus::COMPLETED;
11141115 if (seekStatus_.compare_exchange_strong (expected, SeekStatus::NOT_STARTED)) {
11151116 auto seekCallback = seekCallback_.release ();
@@ -1561,14 +1562,18 @@ void ConsumerImpl::seekAsync(uint64_t timestamp, ResultCallback callback) {
15611562bool ConsumerImpl::isReadCompacted () { return readCompacted_; }
15621563
15631564void ConsumerImpl::hasMessageAvailableAsync (HasMessageAvailableCallback callback) {
1565+ if (!incomingMessages_.empty ()) {
1566+ callback (ResultOk, true );
1567+ return ;
1568+ }
15641569 bool compareMarkDeletePosition;
15651570 {
15661571 std::lock_guard<std::mutex> lock{mutexForMessageId_};
15671572 compareMarkDeletePosition =
15681573 (lastDequedMessageId_ == MessageId::earliest ()) &&
15691574 (startMessageId_.get ().value_or (MessageId::earliest ()) == MessageId::latest ());
15701575 }
1571- if (compareMarkDeletePosition || hasSoughtByTimestamp_.load (std::memory_order_acquire )) {
1576+ if (compareMarkDeletePosition || hasSoughtByTimestamp_.load ()) {
15721577 auto self = get_shared_this_ptr ();
15731578 getLastMessageIdAsync ([self, callback](Result result, const GetLastMessageIdResponse& response) {
15741579 if (result != ResultOk) {
@@ -1587,8 +1592,7 @@ void ConsumerImpl::hasMessageAvailableAsync(HasMessageAvailableCallback callback
15871592 callback (ResultOk, false );
15881593 }
15891594 };
1590- if (self->config_ .isStartMessageIdInclusive () &&
1591- !self->hasSoughtByTimestamp_ .load (std::memory_order_acquire)) {
1595+ if (self->config_ .isStartMessageIdInclusive () && !self->hasSoughtByTimestamp_ .load ()) {
15921596 self->seekAsync (response.getLastMessageId (), [callback, handleResponse](Result result) {
15931597 if (result != ResultOk) {
15941598 callback (result, {});
@@ -1723,7 +1727,7 @@ void ConsumerImpl::seekAsyncInternal(long requestId, SharedBuffer seek, const Se
17231727
17241728 const auto originalSeekMessageId = seekMessageId_.get ();
17251729 if (boost::get<uint64_t >(&seekArg)) {
1726- hasSoughtByTimestamp_.store (true , std::memory_order_release );
1730+ hasSoughtByTimestamp_.store (true );
17271731 } else {
17281732 seekMessageId_ = *boost::get<MessageId>(&seekArg);
17291733 }
@@ -1752,16 +1756,15 @@ void ConsumerImpl::seekAsyncInternal(long requestId, SharedBuffer seek, const Se
17521756 // It's during reconnection, complete the seek future after connection is established
17531757 seekStatus_ = SeekStatus::COMPLETED;
17541758 } else {
1755- if (!hasSoughtByTimestamp_.load (std::memory_order_acquire)) {
1756- startMessageId_ = seekMessageId_.get ();
1757- }
1759+ trySetStartMessageIdToSeekMessageId ();
17581760 seekCallback_.release ()(result);
17591761 }
17601762 } else {
17611763 LOG_ERROR (getName () << " Failed to seek: " << result);
17621764 seekMessageId_ = originalSeekMessageId;
17631765 seekStatus_ = SeekStatus::NOT_STARTED;
17641766 seekCallback_.release ()(result);
1767+ hasSoughtByTimestamp_.store (false );
17651768 }
17661769 });
17671770}
0 commit comments