Search in sources :

Example 1 with LogTruncation

use of org.apache.kafka.clients.consumer.internals.SubscriptionState.LogTruncation in project kafka by apache.

the class SubscriptionStateTest method testTruncationDetectionWithResetPolicy.

@Test
public void testTruncationDetectionWithResetPolicy() {
    Node broker1 = new Node(1, "localhost", 9092);
    state.assignFromUser(Collections.singleton(tp0));
    int currentEpoch = 10;
    long initialOffset = 10L;
    int initialOffsetEpoch = 5;
    long divergentOffset = 5L;
    int divergentOffsetEpoch = 7;
    SubscriptionState.FetchPosition initialPosition = new SubscriptionState.FetchPosition(initialOffset, Optional.of(initialOffsetEpoch), new Metadata.LeaderAndEpoch(Optional.of(broker1), Optional.of(currentEpoch)));
    state.seekUnvalidated(tp0, initialPosition);
    assertTrue(state.awaitingValidation(tp0));
    Optional<LogTruncation> truncationOpt = state.maybeCompleteValidation(tp0, initialPosition, new EpochEndOffset().setLeaderEpoch(divergentOffsetEpoch).setEndOffset(divergentOffset));
    assertEquals(Optional.empty(), truncationOpt);
    assertFalse(state.awaitingValidation(tp0));
    SubscriptionState.FetchPosition updatedPosition = new SubscriptionState.FetchPosition(divergentOffset, Optional.of(divergentOffsetEpoch), new Metadata.LeaderAndEpoch(Optional.of(broker1), Optional.of(currentEpoch)));
    assertEquals(updatedPosition, state.position(tp0));
}
Also used : EpochEndOffset(org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset) Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) LogTruncation(org.apache.kafka.clients.consumer.internals.SubscriptionState.LogTruncation) Test(org.junit.jupiter.api.Test)

Example 2 with LogTruncation

use of org.apache.kafka.clients.consumer.internals.SubscriptionState.LogTruncation in project kafka by apache.

the class SubscriptionStateTest method testMaybeCompleteValidationAfterOffsetReset.

@Test
public void testMaybeCompleteValidationAfterOffsetReset() {
    Node broker1 = new Node(1, "localhost", 9092);
    state.assignFromUser(Collections.singleton(tp0));
    int currentEpoch = 10;
    long initialOffset = 10L;
    int initialOffsetEpoch = 5;
    SubscriptionState.FetchPosition initialPosition = new SubscriptionState.FetchPosition(initialOffset, Optional.of(initialOffsetEpoch), new Metadata.LeaderAndEpoch(Optional.of(broker1), Optional.of(currentEpoch)));
    state.seekUnvalidated(tp0, initialPosition);
    assertTrue(state.awaitingValidation(tp0));
    state.requestOffsetReset(tp0);
    Optional<LogTruncation> truncationOpt = state.maybeCompleteValidation(tp0, initialPosition, new EpochEndOffset().setLeaderEpoch(initialOffsetEpoch).setEndOffset(initialOffset + 5));
    assertEquals(Optional.empty(), truncationOpt);
    assertFalse(state.awaitingValidation(tp0));
    assertTrue(state.isOffsetResetNeeded(tp0));
    assertNull(state.position(tp0));
}
Also used : EpochEndOffset(org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset) Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) LogTruncation(org.apache.kafka.clients.consumer.internals.SubscriptionState.LogTruncation) Test(org.junit.jupiter.api.Test)

Example 3 with LogTruncation

use of org.apache.kafka.clients.consumer.internals.SubscriptionState.LogTruncation in project kafka by apache.

the class SubscriptionStateTest method testTruncationDetectionWithoutResetPolicy.

@Test
public void testTruncationDetectionWithoutResetPolicy() {
    Node broker1 = new Node(1, "localhost", 9092);
    state = new SubscriptionState(new LogContext(), OffsetResetStrategy.NONE);
    state.assignFromUser(Collections.singleton(tp0));
    int currentEpoch = 10;
    long initialOffset = 10L;
    int initialOffsetEpoch = 5;
    long divergentOffset = 5L;
    int divergentOffsetEpoch = 7;
    SubscriptionState.FetchPosition initialPosition = new SubscriptionState.FetchPosition(initialOffset, Optional.of(initialOffsetEpoch), new Metadata.LeaderAndEpoch(Optional.of(broker1), Optional.of(currentEpoch)));
    state.seekUnvalidated(tp0, initialPosition);
    assertTrue(state.awaitingValidation(tp0));
    Optional<LogTruncation> truncationOpt = state.maybeCompleteValidation(tp0, initialPosition, new EpochEndOffset().setLeaderEpoch(divergentOffsetEpoch).setEndOffset(divergentOffset));
    assertTrue(truncationOpt.isPresent());
    LogTruncation truncation = truncationOpt.get();
    assertEquals(Optional.of(new OffsetAndMetadata(divergentOffset, Optional.of(divergentOffsetEpoch), "")), truncation.divergentOffsetOpt);
    assertEquals(initialPosition, truncation.fetchPosition);
    assertTrue(state.awaitingValidation(tp0));
}
Also used : EpochEndOffset(org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset) Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) LogContext(org.apache.kafka.common.utils.LogContext) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) LogTruncation(org.apache.kafka.clients.consumer.internals.SubscriptionState.LogTruncation) Test(org.junit.jupiter.api.Test)

Example 4 with LogTruncation

use of org.apache.kafka.clients.consumer.internals.SubscriptionState.LogTruncation in project kafka by apache.

the class SubscriptionStateTest method testTruncationDetectionUnknownDivergentOffsetWithResetPolicy.

@Test
public void testTruncationDetectionUnknownDivergentOffsetWithResetPolicy() {
    Node broker1 = new Node(1, "localhost", 9092);
    state = new SubscriptionState(new LogContext(), OffsetResetStrategy.EARLIEST);
    state.assignFromUser(Collections.singleton(tp0));
    int currentEpoch = 10;
    long initialOffset = 10L;
    int initialOffsetEpoch = 5;
    SubscriptionState.FetchPosition initialPosition = new SubscriptionState.FetchPosition(initialOffset, Optional.of(initialOffsetEpoch), new Metadata.LeaderAndEpoch(Optional.of(broker1), Optional.of(currentEpoch)));
    state.seekUnvalidated(tp0, initialPosition);
    assertTrue(state.awaitingValidation(tp0));
    Optional<LogTruncation> truncationOpt = state.maybeCompleteValidation(tp0, initialPosition, new EpochEndOffset().setLeaderEpoch(UNDEFINED_EPOCH).setEndOffset(UNDEFINED_EPOCH_OFFSET));
    assertEquals(Optional.empty(), truncationOpt);
    assertFalse(state.awaitingValidation(tp0));
    assertTrue(state.isOffsetResetNeeded(tp0));
    assertEquals(OffsetResetStrategy.EARLIEST, state.resetStrategy(tp0));
}
Also used : EpochEndOffset(org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset) Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) LogContext(org.apache.kafka.common.utils.LogContext) LogTruncation(org.apache.kafka.clients.consumer.internals.SubscriptionState.LogTruncation) Test(org.junit.jupiter.api.Test)

Example 5 with LogTruncation

use of org.apache.kafka.clients.consumer.internals.SubscriptionState.LogTruncation in project kafka by apache.

the class SubscriptionStateTest method testMaybeCompleteValidationAfterPositionChange.

@Test
public void testMaybeCompleteValidationAfterPositionChange() {
    Node broker1 = new Node(1, "localhost", 9092);
    state.assignFromUser(Collections.singleton(tp0));
    int currentEpoch = 10;
    long initialOffset = 10L;
    int initialOffsetEpoch = 5;
    long updateOffset = 20L;
    int updateOffsetEpoch = 8;
    SubscriptionState.FetchPosition initialPosition = new SubscriptionState.FetchPosition(initialOffset, Optional.of(initialOffsetEpoch), new Metadata.LeaderAndEpoch(Optional.of(broker1), Optional.of(currentEpoch)));
    state.seekUnvalidated(tp0, initialPosition);
    assertTrue(state.awaitingValidation(tp0));
    SubscriptionState.FetchPosition updatePosition = new SubscriptionState.FetchPosition(updateOffset, Optional.of(updateOffsetEpoch), new Metadata.LeaderAndEpoch(Optional.of(broker1), Optional.of(currentEpoch)));
    state.seekUnvalidated(tp0, updatePosition);
    Optional<LogTruncation> truncationOpt = state.maybeCompleteValidation(tp0, initialPosition, new EpochEndOffset().setLeaderEpoch(initialOffsetEpoch).setEndOffset(initialOffset + 5));
    assertEquals(Optional.empty(), truncationOpt);
    assertTrue(state.awaitingValidation(tp0));
    assertEquals(updatePosition, state.position(tp0));
}
Also used : EpochEndOffset(org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset) Node(org.apache.kafka.common.Node) Metadata(org.apache.kafka.clients.Metadata) OffsetAndMetadata(org.apache.kafka.clients.consumer.OffsetAndMetadata) LogTruncation(org.apache.kafka.clients.consumer.internals.SubscriptionState.LogTruncation) Test(org.junit.jupiter.api.Test)

Aggregations

Metadata (org.apache.kafka.clients.Metadata)7 OffsetAndMetadata (org.apache.kafka.clients.consumer.OffsetAndMetadata)7 LogTruncation (org.apache.kafka.clients.consumer.internals.SubscriptionState.LogTruncation)7 Node (org.apache.kafka.common.Node)7 EpochEndOffset (org.apache.kafka.common.message.OffsetForLeaderEpochResponseData.EpochEndOffset)7 Test (org.junit.jupiter.api.Test)7 LogContext (org.apache.kafka.common.utils.LogContext)3