feat: add DynamicBatcher for batching items across caller threads#35849
Open
feat: add DynamicBatcher for batching items across caller threads#35849
Conversation
havardpe
reviewed
Feb 12, 2026
vespajlib/src/main/java/com/yahoo/concurrent/DynamicBatcher.java
Outdated
Show resolved
Hide resolved
vespajlib/src/main/java/com/yahoo/concurrent/DynamicBatcher.java
Outdated
Show resolved
Hide resolved
vespajlib/src/main/java/com/yahoo/concurrent/DynamicBatcher.java
Outdated
Show resolved
Hide resolved
vespajlib/src/main/java/com/yahoo/concurrent/DynamicBatcher.java
Outdated
Show resolved
Hide resolved
vespajlib/src/main/java/com/yahoo/concurrent/DynamicBatcher.java
Outdated
Show resolved
Hide resolved
vespajlib/src/main/java/com/yahoo/concurrent/DynamicBatcher.java
Outdated
Show resolved
Hide resolved
vespajlib/src/main/java/com/yahoo/concurrent/DynamicBatcher.java
Outdated
Show resolved
Hide resolved
glebashnik
reviewed
Feb 12, 2026
havardpe
reviewed
Feb 12, 2026
vespajlib/src/main/java/com/yahoo/concurrent/DynamicBatcher.java
Outdated
Show resolved
Hide resolved
vespajlib/src/main/java/com/yahoo/concurrent/DynamicBatcher.java
Outdated
Show resolved
Hide resolved
vespajlib/src/main/java/com/yahoo/concurrent/DynamicBatcher.java
Outdated
Show resolved
Hide resolved
vespajlib/src/main/java/com/yahoo/concurrent/DynamicBatcher.java
Outdated
Show resolved
Hide resolved
vespajlib/src/main/java/com/yahoo/concurrent/DynamicBatcher.java
Outdated
Show resolved
Hide resolved
vespajlib/src/main/java/com/yahoo/concurrent/DynamicBatcher.java
Outdated
Show resolved
Hide resolved
Utility that groups items from multiple caller threads into batches partitioned by a caller-supplied key. One caller thread per batch acts as executor while others wait for the result. Batches are dispatched when reaching max size or max delay timeout.
e7f456e to
cd05b8c
Compare
havardpe
approved these changes
Feb 13, 2026
Member
havardpe
left a comment
There was a problem hiding this comment.
this looks good. I only have some minor comments
| synchronized (batch.monitor) { | ||
| onWaiting.run(); | ||
| for (long deadlineMs = batch.deadline.toEpochMilli(), now = timer.instant().toEpochMilli(); | ||
| !batch.isDone && now < deadlineMs; |
Member
There was a problem hiding this comment.
it is enough to check isFilling here, since we end up waiting for completion further down unless we become the executor
| try { batch.monitor.wait(deadlineMs - now); } | ||
| catch (InterruptedException e) {} | ||
| } | ||
| if (!batch.isDone && batch.isFilling) { |
Member
There was a problem hiding this comment.
isFilling already implies not done and is the thing we should check for here
| return awaitResult(batch, index); | ||
| } | ||
|
|
||
| /** Wakes up all waiting threads. For unit testing. */ |
Member
There was a problem hiding this comment.
we wake the threads waiting on batches not yet removed from the map, which may not be all of them, but the test might be written in such a way that the comment becomes true.
| } | ||
| } | ||
|
|
||
| private Batch<K, I, O> startNewBatch(K key, I input) { |
Member
There was a problem hiding this comment.
consider HasBatchesLock suffix on method name to indicate which lock we expect the caller to have
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Utility that groups items from multiple caller threads into batches partitioned by a caller-supplied key. One caller thread per batch acts as executor while others wait for the result. Batches are dispatched when reaching max size or max delay timeout.