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);
}
}
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));
}
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);
}
}
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)));
}
}
Aggregations