Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ public boolean createBatch() {
private void moveUrgentMailsToBatchIfNeeded(boolean onlyMoveUrgentMails) {
checkIsMailboxThread();
Mail peek = batch.peek();
final boolean isBatchEmpty = peek == null;
if (peek != null) {
if (peek.getMailOptions().isUrgent()) {
// To ensure that urgent mails are executed in FIFO order, moving mails from queue
Expand All @@ -247,6 +248,10 @@ private void moveUrgentMailsToBatchIfNeeded(boolean onlyMoveUrgentMails) {
}
}

// When the batch is empty, also pull non-urgent mails in (not just urgent ones): otherwise a
// non-urgent mail queued behind an urgent one is put back and stranded until the next batch,
// starving processing-time timers between unaligned checkpoints.
final boolean shouldOnlyMoveUrgentMails = onlyMoveUrgentMails && !isBatchEmpty;
Comment thread
wilmerdooley marked this conversation as resolved.
final ReentrantLock lock = this.lock;
lock.lock();
try {
Expand All @@ -255,7 +260,7 @@ private void moveUrgentMailsToBatchIfNeeded(boolean onlyMoveUrgentMails) {
if (mail.getMailOptions().isUrgent()) {
batch.addFirst(mail);
} else {
if (onlyMoveUrgentMails) {
if (shouldOnlyMoveUrgentMails) {
// Put non-urgent mail back into the queue, and stop the loop
queue.addFirst(mail);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,18 @@ void testPutHighPriorityMailDuringTakingFromBatch(boolean isAsyncPut) throws Exc
assertThat(taskMailbox.tryTakeFromBatch()).hasValue(mailB);
}

@Test
void testNonUrgentMailQueuedBehindUrgentMailIsTakenFromBatch() {
Mail urgentMail = new Mail(urgent(), () -> {}, DEFAULT_PRIORITY, "urgentMail");
Mail mail = new Mail(() -> {}, DEFAULT_PRIORITY, "mail");

taskMailbox.put(urgentMail);
taskMailbox.put(mail);

assertThat(taskMailbox.tryTakeFromBatch()).hasValue(urgentMail);
assertThat(taskMailbox.tryTakeFromBatch()).hasValue(mail);
}

@ValueSource(booleans = {true, false})
@ParameterizedTest
void testMailOrderWithBatch(boolean isAsyncPut) throws Exception {
Expand Down