Skip to content

Let MessageGroupQueue producers in whenever there is room - #11736

Open
LindseyZ1205 wants to merge 1 commit into
spring-projects:mainfrom
LindseyZ1205:message-group-queue-room
Open

LindseyZ1205 wants to merge 1 commit into
spring-projects:mainfrom
LindseyZ1205:message-group-queue-room

Conversation

@LindseyZ1205

Copy link
Copy Markdown
Contributor

When several producers wait for room in a full bounded MessageGroupQueue, a drainTo() wakes up only one of them: it signals the "not full" condition once, however many messages it removes. The QueueChannel.clear() calls it, so with a QueueChannel from MessageChannels.queue(messageStore, groupId).capacity(2), two senders waiting in send() and a clear(), one sender gets in and the other one keeps waiting, although the queue has room for it. With a send timeout, that sender fails the send when the timeout expires.

The offer(message, timeout, unit) also rejects the message when there is no time left to wait, even if the queue has room for it: offer(message, 0, TimeUnit.MILLISECONDS) returns false even for an empty queue, while the BlockingQueue contract (and the JDK queues) add the message when there is room.

This signals all the waiting producers in the drainTo() (each of them checks the size of the queue again), and adds the message in the timed offer() whenever there is room.

I ran the MessageGroupQueueTests (the two new tests fail without the change), the QueueChannelTests, the PriorityChannelTests and the MessageGroupQueueTests of the JDBC module (with RUN_LONG_INTEGRATION_TESTS=true), and Checkstyle on the two files. As the snapshot dependencies of main aren't in my cache, I compiled this branch's MessageGroupQueue and the tests against the core module of ee9c722; the core classes they use are the same there as on main, apart from the MessageGroupQueue itself.

The `drainTo()` signals the "not full" condition only once, however
many messages it removes.
When several producers wait in the `put()` or the
`offer(message, timeout, unit)` of a full bounded queue, e.g. senders
to a `QueueChannel` from the `MessageChannels.queue(messageStore, groupId)`
with a `capacity()`, a `QueueChannel.clear()` wakes up only one of them.
The others keep waiting (or time out and fail the send), although the
queue has room for them.

The `offer(message, timeout, unit)` also rejects the message when there
is no time left to wait, even if the queue has room for it:
with a zero timeout, it does not add the message even to an empty queue,
unlike the `BlockingQueue` contract.

* Signal all the waiting producers in the `drainTo()`;
  each of them checks the size of the queue again
* Add the message in the `offer(message, timeout, unit)` whenever there
  is room, regardless of the time left

Signed-off-by: Yingzi Zhang <yingzizhang1205@gmail.com>
@cppwfs cppwfs added status: next Mark accepted PR but for the next major/minor in: core labels Nov 16, 2026
@Test
void timedOfferWithoutTimeToWaitAddsMessageIfThereIsRoom() throws InterruptedException {
MessageGroupQueue queue = new MessageGroupQueue(new SimpleMessageStore(), "FOO", 1);
assertThat(queue.offer(new GenericMessage<>("foo"), 0, TimeUnit.MILLISECONDS)).isTrue();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coding standards require that test code does not use terms like foo and bar. Please replace with meaningful names.

assertThat(queue.offer(new GenericMessage<>("bar"), 0, TimeUnit.MILLISECONDS)).isFalse();
assertThat(queue.size()).isEqualTo(1);
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test for unbounded queue. i.e An unbounded queue rejecting a message because there was no time to wait. It would create a MessageGroupQueue without a capacity, call offer() with a zero timeout, and assert that it returns true and that the queue size is 1.

@artembilan artembilan added type: bug for: backport-to-7.0.x for: backport-to-7.1.x and removed status: next Mark accepted PR but for the next major/minor labels Nov 16, 2026
@artembilan artembilan added this to the 7.2.1 milestone Nov 16, 2026

@artembilan artembilan left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I treat this as a bug, so for next patch version plus back-ports.

Thanks

if (timeoutInNanos > 0) {
offered = this.doOffer(message);
}
offered = this.doOffer(message);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No this. on method calls, please.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants