Skip to content

Commit

Permalink
feat(dispatcher): add new #canClaimFragmentBatch API
Browse files Browse the repository at this point in the history
Adds a new public API method to the Dispatcher,
`#canClaimFragmentBatch(int, int)`. This method determines whether the batch
with the given fragment count and of the given length can actually be
claimed for this dispatcher instance.

This method is mostly useful to determine if something will be claimable
before you wish to claim it, mostly to avoid having to abort your claim
unnecessarily if the batch isn't finished yet when calling this method.
  • Loading branch information
npepinpe committed Feb 16, 2022
1 parent 67b0e3f commit 9281e81
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,19 @@ public long claimFragmentBatch(
LogBufferAppender.claimedBatchLength(fragmentCount, batchLength));
}

/**
* Returns whether a batch of length {@code batchLength}, containing {@code fragmentCount}
* fragments, can be claimed by this dispatcher.
*
* @param fragmentCount the count of fragments in the batch
* @param batchLength the total length of the batch (all fragments included), unframed
* @return true if the batch can be claimed, false otherwise
*/
public boolean canClaimFragmentBatch(final int fragmentCount, final int batchLength) {
final int framedLength = LogBufferAppender.claimedBatchLength(fragmentCount, batchLength);
return framedLength < maxFragmentLength;
}

private synchronized long offer(
final BiFunction<LogBufferPartition, Integer, Integer> claimer,
final int fragmentCount,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,34 @@ public void shouldNotClaimBeyondPublisherLimit() {
verify(logBufferPartition0).getTailCounterVolatile();
}

@Test
public void canClaimFragmentBatch() {
// given
final int fragmentCount = 2;
final int batchLength = dispatcher.getMaxFragmentLength() / 2;

// when
final var canClaimFragmentBatch = dispatcher.canClaimFragmentBatch(fragmentCount, batchLength);

// then
assertThat(canClaimFragmentBatch).isTrue();
}

@Test
public void cannotClaimFragmentBatch() {
// given - a fragment of max length, unframed
final int fragmentCount = 1;
final int batchLength = dispatcher.getMaxFragmentLength();

// when
final var canClaimFragmentBatch = dispatcher.canClaimFragmentBatch(fragmentCount, batchLength);

// then
assertThat(canClaimFragmentBatch)
.as("cannot claim when the unframed, unaligned batch is the max fragment length")
.isFalse();
}

@Test
public void shouldClaimFragment() {
// given
Expand Down

0 comments on commit 9281e81

Please sign in to comment.