-
-
Notifications
You must be signed in to change notification settings - Fork 71
Add hard strict priority mode #279
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
Merged
sonus21
merged 5 commits into
sonus21:master
from
igorjava2025:add_hard_strict_priority
Mar 14, 2026
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
222 changes: 222 additions & 0 deletions
222
rqueue-core/src/main/java/com/github/sonus21/rqueue/listener/HardStrictPriorityPoller.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,222 @@ | ||
| /* | ||
| * Copyright (c) 2026 Sonu Kumar | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * You may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and limitations under the License. | ||
| * | ||
| */ | ||
|
|
||
| package com.github.sonus21.rqueue.listener; | ||
|
|
||
| import com.github.sonus21.rqueue.core.RqueueBeanProvider; | ||
| import com.github.sonus21.rqueue.core.middleware.Middleware; | ||
| import com.github.sonus21.rqueue.listener.RqueueMessageListenerContainer.QueueStateMgr; | ||
| import com.github.sonus21.rqueue.utils.Constants; | ||
| import com.github.sonus21.rqueue.utils.QueueThreadPool; | ||
| import com.github.sonus21.rqueue.utils.TimeoutUtils; | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.function.Function; | ||
| import java.util.stream.Collectors; | ||
| import org.slf4j.event.Level; | ||
| import org.springframework.messaging.MessageHeaders; | ||
|
|
||
| /** | ||
| * Use it only with priority queues. Message processing can be slow. The hard strict priority | ||
| * algorithm is better in HardStrictPriorityPoller than in StrictPriorityPoller More details see in | ||
| * <a href="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/sonus21/rqueue/issues/276">GitHub project issue</a> | ||
| */ | ||
| class HardStrictPriorityPoller extends RqueueMessagePoller { | ||
|
|
||
| private final RqueueBeanProvider rqueueBeanProvider; | ||
|
|
||
| private final Map<String, QueueDetail> queueNameToDetail; | ||
| private final Map<String, QueueThreadPool> queueNameToThread; | ||
| private final Map<String, Long> queueDeactivationTime = new HashMap<>(); | ||
| private final HardStrictPriorityPollerProperties hardStrictPriorityPollerProperties; | ||
|
|
||
| HardStrictPriorityPoller( | ||
| String groupName, | ||
| final List<QueueDetail> queueDetails, | ||
| final Map<String, QueueThreadPool> queueNameToThread, | ||
| RqueueBeanProvider rqueueBeanProvider, | ||
| QueueStateMgr queueStateMgr, | ||
| List<Middleware> middlewares, | ||
| long pollingInterval, | ||
| long backoffTime, | ||
| PostProcessingHandler postProcessingHandler, | ||
| MessageHeaders messageHeaders, | ||
| HardStrictPriorityPollerProperties hardStrictPriorityPollerProperties) { | ||
| super( | ||
| "HardStrict-" + groupName, | ||
| rqueueBeanProvider, | ||
| queueStateMgr, | ||
| middlewares, | ||
| pollingInterval, | ||
| backoffTime, | ||
| postProcessingHandler, | ||
| messageHeaders); | ||
|
|
||
| this.rqueueBeanProvider = rqueueBeanProvider; | ||
| // Sort queues by priority once during initialization | ||
| List<QueueDetail> queueDetailList = new ArrayList<>(queueDetails); | ||
| queueDetailList.sort( | ||
| (o1, o2) -> | ||
| o2.getPriority().get(Constants.DEFAULT_PRIORITY_KEY) | ||
| - o1.getPriority().get(Constants.DEFAULT_PRIORITY_KEY)); | ||
|
|
||
| this.queues = queueDetailList.stream().map(QueueDetail::getName).collect(Collectors.toList()); | ||
| this.queueNameToDetail = | ||
| queueDetailList.stream() | ||
| .collect(Collectors.toMap(QueueDetail::getName, Function.identity())); | ||
| this.queueNameToThread = queueNameToThread; | ||
| this.hardStrictPriorityPollerProperties = | ||
| hardStrictPriorityPollerProperties != null | ||
| ? hardStrictPriorityPollerProperties | ||
| : new HardStrictPriorityPollerProperties(); | ||
| } | ||
|
|
||
| @Override | ||
| public void start() { | ||
| log(Level.DEBUG, "Running, Ordered Queues: {}", null, queues); | ||
| while (true) { | ||
| if (shouldExit()) { | ||
| return; | ||
| } | ||
|
|
||
| boolean messageFoundInAnyQueue = false; | ||
|
|
||
| try { | ||
| for (String queue : queues) { | ||
| if (eligibleForPolling(queue) && !isDeactivated(queue)) { | ||
| QueueThreadPool queueThreadPool = queueNameToThread.get(queue); | ||
| QueueDetail queueDetail = queueNameToDetail.get(queue); | ||
| poll(-1, queue, queueDetail, queueThreadPool); | ||
|
|
||
| if (hardStrictPriorityPollerProperties.getAfterPollSleepInterval() != null) { | ||
| TimeoutUtils.sleepLog( | ||
| hardStrictPriorityPollerProperties.getAfterPollSleepInterval(), false); | ||
| } | ||
|
|
||
| if (existMessagesInCurrentQueueOrHigherPriorityQueue(queue, queues)) { | ||
| // break current cycle and start new cycle | ||
| // it allow to process queue with the higher priority | ||
| messageFoundInAnyQueue = true; | ||
| break; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // If no messages were found across all queues, sleep for the polling interval | ||
| if (!messageFoundInAnyQueue) { | ||
| TimeoutUtils.sleepLog(pollingInterval, false); | ||
| } | ||
|
|
||
| } catch (Throwable e) { | ||
| log(Level.ERROR, "Exception in the poller {}", e, e.getMessage()); | ||
| if (shouldExit()) { | ||
| return; | ||
| } | ||
| TimeoutUtils.sleepLog(backoffTime, false); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| boolean existMessagesInCurrentQueueOrHigherPriorityQueue( | ||
| String currentQueue, List<String> queues) { | ||
| for (String queue : queues) { | ||
| if (eligibleForPolling(queue) && !isDeactivated(queue)) { | ||
| QueueDetail queueDetail = queueNameToDetail.get(queue); | ||
| if (existAvailableMessagesForPoll(queueDetail)) { | ||
| // the current or higher priority queue contains messages that need to be processed. | ||
| return true; | ||
| } | ||
| } | ||
| // we check all queues from the highest priority to current queue | ||
| if (queue.equals(currentQueue)) { | ||
| return false; | ||
| } | ||
| } | ||
| // unexpected behavior, need more details if it occurs | ||
| log(Level.WARN, "current queue '{}' not found in queues list '{}'", null, currentQueue, queues); | ||
| return false; | ||
| } | ||
|
|
||
| protected boolean existAvailableMessagesForPoll(QueueDetail queueDetail) { | ||
| boolean readyMessagesExists = | ||
| rqueueBeanProvider | ||
| .getRqueueMessageTemplate() | ||
| .findFirstElementFromList(queueDetail.getQueueName()) | ||
| .isPresent(); | ||
| if (readyMessagesExists) { | ||
| log( | ||
| Level.TRACE, | ||
| "readyMessages exists for queue '{}', existAvailableMessagesForPoll = true.", | ||
| null, | ||
| queueDetail.getName()); | ||
| return true; | ||
| } | ||
|
|
||
| // Only check delayed messages with score <= current time | ||
| long currentTime = System.currentTimeMillis(); | ||
| boolean delayedMessagesExists = | ||
| rqueueBeanProvider | ||
| .getRqueueMessageTemplate() | ||
| .findFirstElementFromZsetWithScore(queueDetail.getScheduledQueueName()) | ||
| .filter(element -> element.getScore() <= currentTime) | ||
| .isPresent(); | ||
|
|
||
| if (delayedMessagesExists) { | ||
| log( | ||
| Level.TRACE, | ||
| "delayedMessages exists for scheduled queue '{}' and currentTime '{}'," | ||
| + " existAvailableMessagesForPoll = true.", | ||
| null, | ||
| queueDetail.getScheduledQueueName(), | ||
| currentTime); | ||
| return true; | ||
| } | ||
|
|
||
| return false; | ||
| } | ||
|
|
||
| private boolean isDeactivated(String queue) { | ||
| Long deactivationTime = queueDeactivationTime.get(queue); | ||
| if (deactivationTime == null) { | ||
| return false; | ||
| } | ||
| if (System.currentTimeMillis() - deactivationTime > pollingInterval) { | ||
| queueDeactivationTime.remove(queue); | ||
| return false; | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| long getSemaphoreWaitTime() { | ||
| return hardStrictPriorityPollerProperties.getSemaphoreWaitTime() != null | ||
| ? hardStrictPriorityPollerProperties.getSemaphoreWaitTime() | ||
| : 20L; | ||
| } | ||
|
|
||
| @Override | ||
| void deactivate(int index, String queue, DeactivateType deactivateType) { | ||
| if (deactivateType == DeactivateType.POLL_FAILED) { | ||
| // Pause in case of connection errors or polling failures | ||
| TimeoutUtils.sleepLog(backoffTime, false); | ||
| } else { | ||
| // Mark deactivation time if the queue is empty | ||
| queueDeactivationTime.put(queue, System.currentTimeMillis()); | ||
| } | ||
| } | ||
| } |
33 changes: 33 additions & 0 deletions
33
.../src/main/java/com/github/sonus21/rqueue/listener/HardStrictPriorityPollerProperties.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,33 @@ | ||
| package com.github.sonus21.rqueue.listener; | ||
|
|
||
| public class HardStrictPriorityPollerProperties { | ||
| // set such default values for parameters "afterPollSleepInterval" and "semaphoreWaitTime" | ||
| // local load tests have correct strict priority algorithm work and good performance with them | ||
| private Long afterPollSleepInterval = 30L; | ||
| private Long semaphoreWaitTime = 15L; | ||
|
|
||
| public Long getAfterPollSleepInterval() { | ||
| return afterPollSleepInterval; | ||
| } | ||
|
|
||
| public void setAfterPollSleepInterval(Long afterPollSleepInterval) { | ||
| validateTimeInterval(afterPollSleepInterval); | ||
| this.afterPollSleepInterval = afterPollSleepInterval; | ||
| } | ||
|
|
||
| public Long getSemaphoreWaitTime() { | ||
| return this.semaphoreWaitTime; | ||
| } | ||
|
|
||
| public void setSemaphoreWaitTime(Long semaphoreWaitTime) { | ||
| validateTimeInterval(semaphoreWaitTime); | ||
| this.semaphoreWaitTime = semaphoreWaitTime; | ||
|
igorjava2025 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| private void validateTimeInterval(Long value) { | ||
| if (value == null || value > 0) { | ||
| return; | ||
| } | ||
| throw new IllegalArgumentException("Value must be positive: " + value); | ||
| } | ||
| } | ||
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
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.