Skip to content

Commit

Permalink
[Java] Simplify term gap scanning logic by removing `ALIGNED_HEADER_L…
Browse files Browse the repository at this point in the history
…ENGTH` and streamlining non-zero frame lookup.
  • Loading branch information
vyazelenko committed Jan 24, 2025
1 parent f2933f8 commit d22a389
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 10 deletions.
15 changes: 5 additions & 10 deletions aeron-client/src/main/java/io/aeron/logbuffer/TermGapScanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package io.aeron.logbuffer;

import org.agrona.BitUtil;
import org.agrona.concurrent.UnsafeBuffer;

import static io.aeron.logbuffer.FrameDescriptor.FRAME_ALIGNMENT;
Expand All @@ -31,8 +30,6 @@
*/
public class TermGapScanner
{
private static final int ALIGNED_HEADER_LENGTH = BitUtil.align(HEADER_LENGTH, FRAME_ALIGNMENT);

/**
* Handler for notifying of gaps in the log.
*/
Expand Down Expand Up @@ -82,19 +79,17 @@ public static int scanForGap(
final int gapBeginOffset = offset;
if (offset < limitOffset)
{
final int limit = limitOffset - ALIGNED_HEADER_LENGTH;
while (offset < limit)
offset += HEADER_LENGTH;
while (offset < limitOffset)
{
offset += FRAME_ALIGNMENT;

if (0 != termBuffer.getIntVolatile(offset))
if (0 != frameLengthVolatile(termBuffer, offset))
{
offset -= ALIGNED_HEADER_LENGTH;
break;
}
offset += HEADER_LENGTH;
}

final int gapLength = (offset - gapBeginOffset) + ALIGNED_HEADER_LENGTH;
final int gapLength = offset - gapBeginOffset;
handler.onGap(termId, gapBeginOffset, gapLength);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import static org.agrona.BitUtil.align;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.verifyNoMoreInteractions;

class TermGapScannerTest
{
Expand Down Expand Up @@ -51,6 +52,7 @@ void shouldReportGapAtBeginningOfBuffer()
assertEquals(0, TermGapScanner.scanForGap(termBuffer, TERM_ID, 0, highWaterMark, gapHandler));

verify(gapHandler).onGap(TERM_ID, 0, frameOffset);
verifyNoMoreInteractions(gapHandler);
}

@Test
Expand All @@ -67,6 +69,7 @@ void shouldReportSingleGapWhenBufferNotFull()
assertEquals(tail, TermGapScanner.scanForGap(termBuffer, TERM_ID, tail, highWaterMark, gapHandler));

verify(gapHandler).onGap(TERM_ID, tail, align(HEADER_LENGTH, FRAME_ALIGNMENT));
verifyNoMoreInteractions(gapHandler);
}

@Test
Expand All @@ -83,6 +86,7 @@ void shouldReportSingleGapWhenBufferIsFull()
assertEquals(tail, TermGapScanner.scanForGap(termBuffer, TERM_ID, tail, highWaterMark, gapHandler));

verify(gapHandler).onGap(TERM_ID, tail, align(HEADER_LENGTH, FRAME_ALIGNMENT));
verifyNoMoreInteractions(gapHandler);
}

@Test
Expand All @@ -100,4 +104,37 @@ void shouldReportNoGapWhenHwmIsInPadding()

verifyNoInteractions(gapHandler);
}

@Test
void shouldReportSingleHeaderGap()
{
final int offset = 8192 + 384;
when(termBuffer.getIntVolatile(offset)).thenReturn(0);
when(termBuffer.getIntVolatile(offset + HEADER_LENGTH)).thenReturn(128);

assertEquals(
offset, TermGapScanner.scanForGap(termBuffer, TERM_ID, offset, LOG_BUFFER_CAPACITY, gapHandler));

verify(termBuffer).getIntVolatile(offset);
verify(termBuffer).getIntVolatile(offset + HEADER_LENGTH);
verify(gapHandler).onGap(TERM_ID, offset, HEADER_LENGTH);
verifyNoMoreInteractions(gapHandler, termBuffer);
}

@Test
void shouldReportGapAtTheEndOfTheBuffer()
{
final int offset = LOG_BUFFER_CAPACITY - 128;
when(termBuffer.getIntVolatile(offset)).thenReturn(0);

assertEquals(
offset, TermGapScanner.scanForGap(termBuffer, TERM_ID, offset, LOG_BUFFER_CAPACITY, gapHandler));

verify(termBuffer).getIntVolatile(offset);
verify(termBuffer).getIntVolatile(offset + HEADER_LENGTH);
verify(termBuffer).getIntVolatile(offset + 2 * HEADER_LENGTH);
verify(termBuffer).getIntVolatile(offset + 3 * HEADER_LENGTH);
verify(gapHandler).onGap(TERM_ID, offset, 128);
verifyNoMoreInteractions(gapHandler, termBuffer);
}
}

0 comments on commit d22a389

Please sign in to comment.