Allow out-of-order submission of messages in LocalMessageBroker.#426
Draft
Allow out-of-order submission of messages in LocalMessageBroker.#426
LocalMessageBroker.#426Conversation
These functions are in C++20, but we're on C++17 right now and not going to upgrade.
Also rename lots of "frame" to "message".
This removes one of the two identical ways to access messages.
Previous memory management structure required two fixtures. Not anymore.
…omic. Implement all relevant TopicHeader functions for this new structure.
It'll get stopped when the testbed shuts down.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note
This PR is still work in progress.
Summary
This PR fixes out-of-order message handling in the local message broker by refactoring the message availability tracking from separate first/last atomics to a unified bitmap-based approach.
The Problem
Previously, the message broker tracked message availability using two separate counters:
first_frame_id(oldest available) andlast_frame_id(newest available + 1). This worked for sequential publishing but couldn't handle out-of-order messages. For example, if message 5 arrived before message 4, there was no way to track that 5 was available while message 4 was still missing.The Solution: Bitmap-Based Availability Tracking
Instead of tracking just a range
[first, last), we now track which specific messages in the circular buffer are available using a bitmap packed into a single atomic variable.How it works:
The
availabilityfield is a 64-bit atomic containing two pieces of information:lastmessage ID (the next ID that will be assigned). These 48 bits still allow us to publish messages for ~9 years at 1 million messages/sec.Bitmap interpretation:
last-1(the newest message)last-2last-3Example:
If
last = 10and the bitmap is0b0000000000010101(binary):This allows the broker to correctly handle out-of-order message publishing while maintaining lock-free concurrent access between publishers and subscribers.
Note: packing everything in a single 64bit atomic allows us to atomically update both at the same time. We can perform additional operations using CAS loops (done in
TryReserve(),TryReserveNext()andTryMakeAvailable().Key Changes
Refactored TopicHeader availability tracking Converted from separate
first_frame_id/last_frame_idatomics to a singleavailabilityatomic containing packedlast_idand 16-bit bitmap.Added C++17 bit operations Implemented
countl_zeroandcountr_zerofunctions (backported from C++20) inUtil.h/Util.inlto efficiently count leading/trailing zeros in the bitmap.Moved message ID to MessageHeader This is required since each nested topic has a different message ID and we need to track all.
API improvements:
GetOldestMessageId()/GetNewestMessageId()toGetBegin()/GetEnd()(half-open range semantics:[begin, end))TryReserve(),TryReserveNext(),TryMakeAvailable()methods for atomic reservation and publishing