-
Notifications
You must be signed in to change notification settings - Fork 1
[broker-30] Implement delivering retained messages #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
crazyrokr
wants to merge
12
commits into
develop
Choose a base branch
from
feature-broker-30
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
26129f5
[broker-30] Implement delivering retained messages
crazyrokr c6ada4d
[broker-30] Rewrite retained messages collecting
crazyrokr 94b32db
Merge branch 'develop' into feature-broker-30
crazyrokr 01bac5b
[broker-30] Cleanup code after merge
crazyrokr c903510
[broker-30] Fix corner cases in retained messages
crazyrokr e1f5c2a
[broker-30] Handle SubscribeRetainHandling
crazyrokr a8829ac
Merge branch 'develop' into feature-broker-30
crazyrokr a1b87dd
[broker-30] Fix build
crazyrokr fd848eb
Merge branch 'develop' into feature-broker-30
crazyrokr be8070b
[broker-30] SubscriberNode refactoring
crazyrokr 0c219dd
[broker-30] Revert wrong access level modifier
crazyrokr 62900ca
[broker-30] Avoid tree branch locking
crazyrokr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
4 changes: 4 additions & 0 deletions
4
model/src/main/java/javasabr/mqtt/model/subscribtion/tree/package-info.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| @NullMarked | ||
| package javasabr.mqtt.model.subscribtion.tree; | ||
|
|
||
| import org.jspecify.annotations.NullMarked; |
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
31 changes: 31 additions & 0 deletions
31
model/src/main/java/javasabr/mqtt/model/topic/tree/ConcurrentRetainedMessageTree.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package javasabr.mqtt.model.topic.tree; | ||
|
|
||
| import javasabr.mqtt.model.publishing.Publish; | ||
| import javasabr.mqtt.model.topic.TopicFilter; | ||
| import javasabr.mqtt.model.topic.TopicName; | ||
| import javasabr.rlib.common.ThreadSafe; | ||
| import lombok.AccessLevel; | ||
| import lombok.experimental.FieldDefaults; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| @FieldDefaults(level = AccessLevel.PRIVATE, makeFinal = true) | ||
| public class ConcurrentRetainedMessageTree implements ThreadSafe { | ||
|
|
||
| TopicMessageNode rootNode; | ||
|
|
||
| public ConcurrentRetainedMessageTree() { | ||
| this.rootNode = new TopicMessageNode(); | ||
| } | ||
|
|
||
| public void retainMessage(Publish message) { | ||
| rootNode.retainMessage(0, message, message.topicName()); | ||
| } | ||
|
|
||
| public @Nullable Publish getRetainedMessage(TopicName topicName) { | ||
| return rootNode.getRetainedMessage(0, topicName); | ||
| } | ||
|
|
||
| public @Nullable Publish getRetainedMessage(TopicFilter topicFilter) { | ||
| return rootNode.getRetainedMessage(0, topicFilter); | ||
| } | ||
| } |
94 changes: 94 additions & 0 deletions
94
model/src/main/java/javasabr/mqtt/model/topic/tree/TopicMessageNode.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| package javasabr.mqtt.model.topic.tree; | ||
|
|
||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.function.Supplier; | ||
| import javasabr.mqtt.base.util.DebugUtils; | ||
| import javasabr.mqtt.model.publishing.Publish; | ||
| import javasabr.mqtt.model.topic.TopicFilter; | ||
| import javasabr.mqtt.model.topic.TopicName; | ||
| import javasabr.rlib.collections.dictionary.DictionaryFactory; | ||
| import javasabr.rlib.collections.dictionary.LockableRefToRefDictionary; | ||
| import lombok.AccessLevel; | ||
| import lombok.Getter; | ||
| import lombok.experimental.Accessors; | ||
| import lombok.experimental.FieldDefaults; | ||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| @Getter(AccessLevel.PACKAGE) | ||
| @Accessors(fluent = true, chain = false) | ||
| @FieldDefaults(level = AccessLevel.PRIVATE) | ||
| class TopicMessageNode { | ||
crazyrokr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| private final static Supplier<TopicMessageNode> TOPIC_NODE_FACTORY = TopicMessageNode::new; | ||
|
|
||
| static { | ||
| DebugUtils.registerIncludedFields("childNodes", "retainedMessage"); | ||
| } | ||
|
|
||
| @Nullable | ||
| volatile LockableRefToRefDictionary<String, TopicMessageNode> childNodes; | ||
| final AtomicReference<@Nullable Publish> retainedMessage = new AtomicReference<>(); | ||
|
|
||
| public void retainMessage(int level, Publish message, TopicName topicFilter) { | ||
| if (level + 1 == topicFilter.levelsCount()) { | ||
| retainedMessage.set(message); | ||
| return; | ||
| } | ||
| TopicMessageNode childNode = getOrCreateChildNode(topicFilter.segment(level)); | ||
| childNode.retainMessage(level + 1, message, topicFilter); | ||
| } | ||
|
|
||
| @Nullable | ||
| public Publish getRetainedMessage(int level, TopicName topicName) { | ||
| if (level + 1 == topicName.levelsCount()) { | ||
| return retainedMessage.get(); | ||
| } | ||
| TopicMessageNode childNode = getOrCreateChildNode(topicName.segment(level)); | ||
| return childNode.getRetainedMessage(level + 1, topicName); | ||
| } | ||
|
|
||
| @Nullable | ||
| public Publish getRetainedMessage(int level, TopicFilter topicName) { | ||
| if (level + 1 == topicName.levelsCount()) { | ||
| return retainedMessage.get(); | ||
| } | ||
| TopicMessageNode childNode = getOrCreateChildNode(topicName.segment(level)); | ||
| return childNode.getRetainedMessage(level + 1, topicName); | ||
| } | ||
|
|
||
| private TopicMessageNode getOrCreateChildNode(String segment) { | ||
| LockableRefToRefDictionary<String, TopicMessageNode> childNodes = getOrCreateChildNodes(); | ||
| long stamp = childNodes.readLock(); | ||
| try { | ||
| TopicMessageNode topicFilterNode = childNodes.get(segment); | ||
| if (topicFilterNode != null) { | ||
| return topicFilterNode; | ||
| } | ||
| } finally { | ||
| childNodes.readUnlock(stamp); | ||
| } | ||
| stamp = childNodes.writeLock(); | ||
| try { | ||
| return childNodes.getOrCompute(segment, TOPIC_NODE_FACTORY); | ||
| } finally { | ||
| childNodes.writeUnlock(stamp); | ||
| } | ||
| } | ||
|
|
||
| private LockableRefToRefDictionary<String, TopicMessageNode> getOrCreateChildNodes() { | ||
| if (childNodes == null) { | ||
| synchronized (this) { | ||
| if (childNodes == null) { | ||
| childNodes = DictionaryFactory.stampedLockBasedRefToRefDictionary(); | ||
| } | ||
| } | ||
| } | ||
| //noinspection ConstantConditions | ||
| return childNodes; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return DebugUtils.toJsonString(this); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.