Search in sources :

Example 1 with RaftClientTestContext

use of org.apache.kafka.raft.RaftClientTestContext in project kafka by apache.

the class SnapshotWriterReaderTest method testWritingSnapshot.

@Test
public void testWritingSnapshot() throws Exception {
    int recordsPerBatch = 3;
    int batches = 3;
    int delimiterCount = 2;
    long magicTimestamp = 0xDEADBEEF;
    OffsetAndEpoch id = new OffsetAndEpoch(recordsPerBatch * batches, 3);
    List<List<String>> expected = buildRecords(recordsPerBatch, batches);
    RaftClientTestContext.Builder contextBuilder = new RaftClientTestContext.Builder(localId, voters);
    for (List<String> batch : expected) {
        contextBuilder.appendToLog(id.epoch, batch);
    }
    RaftClientTestContext context = contextBuilder.build();
    context.pollUntil(() -> context.currentLeader().equals(OptionalInt.of(localId)));
    int epoch = context.currentEpoch();
    context.advanceLocalLeaderHighWatermarkToLogEndOffset();
    try (SnapshotWriter<String> snapshot = context.client.createSnapshot(id.offset - 1, id.epoch, magicTimestamp).get()) {
        assertEquals(id, snapshot.snapshotId());
        expected.forEach(batch -> assertDoesNotThrow(() -> snapshot.append(batch)));
        snapshot.freeze();
    }
    try (SnapshotReader<String> reader = readSnapshot(context, id, Integer.MAX_VALUE)) {
        RawSnapshotReader snapshot = context.log.readSnapshot(id).get();
        int recordCount = validateDelimiters(snapshot, magicTimestamp);
        assertEquals((recordsPerBatch * batches) + delimiterCount, recordCount);
        assertSnapshot(expected, reader);
    }
}
Also used : OffsetAndEpoch(org.apache.kafka.raft.OffsetAndEpoch) RaftClientTestContext(org.apache.kafka.raft.RaftClientTestContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 2 with RaftClientTestContext

use of org.apache.kafka.raft.RaftClientTestContext in project kafka by apache.

the class SnapshotWriterReaderTest method testAbortedSnapshot.

@Test
public void testAbortedSnapshot() throws Exception {
    int recordsPerBatch = 3;
    int batches = 3;
    OffsetAndEpoch id = new OffsetAndEpoch(recordsPerBatch * batches, 3);
    List<List<String>> expected = buildRecords(recordsPerBatch, batches);
    RaftClientTestContext.Builder contextBuilder = new RaftClientTestContext.Builder(localId, voters);
    for (List<String> batch : expected) {
        contextBuilder.appendToLog(id.epoch, batch);
    }
    RaftClientTestContext context = contextBuilder.build();
    context.pollUntil(() -> context.currentLeader().equals(OptionalInt.of(localId)));
    int epoch = context.currentEpoch();
    context.advanceLocalLeaderHighWatermarkToLogEndOffset();
    try (SnapshotWriter<String> snapshot = context.client.createSnapshot(id.offset - 1, id.epoch, 0).get()) {
        assertEquals(id, snapshot.snapshotId());
        expected.forEach(batch -> {
            assertDoesNotThrow(() -> snapshot.append(batch));
        });
    }
    assertEquals(Optional.empty(), context.log.readSnapshot(id));
}
Also used : OffsetAndEpoch(org.apache.kafka.raft.OffsetAndEpoch) RaftClientTestContext(org.apache.kafka.raft.RaftClientTestContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Example 3 with RaftClientTestContext

use of org.apache.kafka.raft.RaftClientTestContext in project kafka by apache.

the class SnapshotWriterReaderTest method testSnapshotDelimiters.

@Test
public void testSnapshotDelimiters() throws Exception {
    int recordsPerBatch = 1;
    int batches = 0;
    int delimiterCount = 2;
    long magicTimestamp = 0xDEADBEEF;
    OffsetAndEpoch id = new OffsetAndEpoch(recordsPerBatch * batches, 3);
    RaftClientTestContext.Builder contextBuilder = new RaftClientTestContext.Builder(localId, voters);
    RaftClientTestContext context = contextBuilder.build();
    context.pollUntil(() -> context.currentLeader().equals(OptionalInt.of(localId)));
    context.advanceLocalLeaderHighWatermarkToLogEndOffset();
    // Create an empty snapshot and freeze it immediately
    try (SnapshotWriter<String> snapshot = context.client.createSnapshot(id.offset - 1, id.epoch, magicTimestamp).get()) {
        assertEquals(id, snapshot.snapshotId());
        snapshot.freeze();
    }
    // Verify that an empty snapshot has only the Header and Footer
    try (SnapshotReader<String> reader = readSnapshot(context, id, Integer.MAX_VALUE)) {
        RawSnapshotReader snapshot = context.log.readSnapshot(id).get();
        int recordCount = validateDelimiters(snapshot, magicTimestamp);
        assertEquals((recordsPerBatch * batches) + delimiterCount, recordCount);
    }
}
Also used : OffsetAndEpoch(org.apache.kafka.raft.OffsetAndEpoch) RaftClientTestContext(org.apache.kafka.raft.RaftClientTestContext) Test(org.junit.jupiter.api.Test)

Example 4 with RaftClientTestContext

use of org.apache.kafka.raft.RaftClientTestContext in project kafka by apache.

the class SnapshotWriterReaderTest method testAppendToFrozenSnapshot.

@Test
public void testAppendToFrozenSnapshot() throws Exception {
    int recordsPerBatch = 3;
    int batches = 3;
    OffsetAndEpoch id = new OffsetAndEpoch(recordsPerBatch * batches, 3);
    List<List<String>> expected = buildRecords(recordsPerBatch, batches);
    RaftClientTestContext.Builder contextBuilder = new RaftClientTestContext.Builder(localId, voters);
    for (List<String> batch : expected) {
        contextBuilder.appendToLog(id.epoch, batch);
    }
    RaftClientTestContext context = contextBuilder.build();
    context.pollUntil(() -> context.currentLeader().equals(OptionalInt.of(localId)));
    int epoch = context.currentEpoch();
    context.advanceLocalLeaderHighWatermarkToLogEndOffset();
    try (SnapshotWriter<String> snapshot = context.client.createSnapshot(id.offset - 1, id.epoch, 0).get()) {
        assertEquals(id, snapshot.snapshotId());
        expected.forEach(batch -> {
            assertDoesNotThrow(() -> snapshot.append(batch));
        });
        snapshot.freeze();
        assertThrows(RuntimeException.class, () -> snapshot.append(expected.get(0)));
    }
}
Also used : OffsetAndEpoch(org.apache.kafka.raft.OffsetAndEpoch) RaftClientTestContext(org.apache.kafka.raft.RaftClientTestContext) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.jupiter.api.Test)

Aggregations

OffsetAndEpoch (org.apache.kafka.raft.OffsetAndEpoch)4 RaftClientTestContext (org.apache.kafka.raft.RaftClientTestContext)4 Test (org.junit.jupiter.api.Test)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3