Skip to content

Minor, added some methods to ReadDemand that would increase readability #17793

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
20 changes: 16 additions & 4 deletions logstash-core/src/main/java/org/logstash/ackedqueue/Queue.java
Original file line number Diff line number Diff line change
Expand Up @@ -932,10 +932,9 @@ private Batch deserialize() {
private boolean awaitReadDemand(final long timeoutMillis, final int elementsNeeded) throws InterruptedException {
assert this.lock.isHeldByCurrentThread();

final long deadlineMillis = Math.addExact(System.currentTimeMillis(), timeoutMillis);
this.readDemand = new ReadDemand(deadlineMillis, elementsNeeded);
this.readDemand = ReadDemand.fromExpectedTimeout(timeoutMillis, elementsNeeded);

boolean unElapsed = this.notEmpty.awaitUntil(new Date(deadlineMillis));
boolean unElapsed = this.notEmpty.awaitUntil(this.readDemand.expectedExpiry());
this.readDemand = null;
return unElapsed;
}
Expand All @@ -946,7 +945,7 @@ private void maybeSignalReadDemand(boolean forceSignal) {
// if we're not forcing, and if the current read demand has
// neither been met nor expired, this method becomes a no-op.
if (!forceSignal && Objects.nonNull(readDemand)) {
if (unreadCount < readDemand.elementsNeeded && System.currentTimeMillis() < readDemand.deadlineMillis) {
if (readDemand.isDeferrable(unreadCount)) {
return;
}
}
Expand All @@ -962,5 +961,18 @@ private static class ReadDemand {
this.deadlineMillis = deadlineMillis;
this.elementsNeeded = elementsNeeded;
}

boolean isDeferrable(long elementsAvailable) {
return elementsAvailable < elementsNeeded && System.currentTimeMillis() < deadlineMillis;
}

static ReadDemand fromExpectedTimeout(long timeoutMillis, int elementsNeeded) {
final long deadlineMillis = Math.addExact(System.currentTimeMillis(), timeoutMillis);
return new ReadDemand(deadlineMillis, elementsNeeded);
}

public Date expectedExpiry() {
return new Date(deadlineMillis);
}
}
}