use of org.apache.kafka.raft.OffsetAndEpoch 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.OffsetAndEpoch 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.OffsetAndEpoch in project kafka by apache.
the class SnapshotsTest method testValidSnapshotFilename.
@Test
public void testValidSnapshotFilename() {
OffsetAndEpoch snapshotId = new OffsetAndEpoch(TestUtils.RANDOM.nextInt(Integer.MAX_VALUE), TestUtils.RANDOM.nextInt(Integer.MAX_VALUE));
Path path = Snapshots.snapshotPath(TestUtils.tempDirectory().toPath(), snapshotId);
SnapshotPath snapshotPath = Snapshots.parse(path).get();
assertEquals(path, snapshotPath.path);
assertEquals(snapshotId, snapshotPath.snapshotId);
assertFalse(snapshotPath.partial);
assertFalse(snapshotPath.deleted);
}
use of org.apache.kafka.raft.OffsetAndEpoch in project kafka by apache.
the class SnapshotsTest method testValidPartialSnapshotFilename.
@Test
public void testValidPartialSnapshotFilename() throws IOException {
OffsetAndEpoch snapshotId = new OffsetAndEpoch(TestUtils.RANDOM.nextInt(Integer.MAX_VALUE), TestUtils.RANDOM.nextInt(Integer.MAX_VALUE));
Path path = Snapshots.createTempFile(TestUtils.tempDirectory().toPath(), snapshotId);
// Delete it as we only need the path for testing
Files.delete(path);
SnapshotPath snapshotPath = Snapshots.parse(path).get();
assertEquals(path, snapshotPath.path);
assertEquals(snapshotId, snapshotPath.snapshotId);
assertTrue(snapshotPath.partial);
}
use of org.apache.kafka.raft.OffsetAndEpoch in project kafka by apache.
the class SnapshotsTest method testDeleteSnapshot.
@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testDeleteSnapshot(boolean renameBeforeDeleting) throws IOException {
OffsetAndEpoch snapshotId = new OffsetAndEpoch(TestUtils.RANDOM.nextInt(Integer.MAX_VALUE), TestUtils.RANDOM.nextInt(Integer.MAX_VALUE));
Path logDirPath = TestUtils.tempDirectory().toPath();
try (FileRawSnapshotWriter snapshot = FileRawSnapshotWriter.create(logDirPath, snapshotId, Optional.empty())) {
snapshot.freeze();
Path snapshotPath = Snapshots.snapshotPath(logDirPath, snapshotId);
assertTrue(Files.exists(snapshotPath));
if (renameBeforeDeleting)
// rename snapshot before deleting
Utils.atomicMoveWithFallback(snapshotPath, Snapshots.deleteRename(snapshotPath, snapshotId), false);
assertTrue(Snapshots.deleteIfExists(logDirPath, snapshot.snapshotId()));
assertFalse(Files.exists(snapshotPath));
assertFalse(Files.exists(Snapshots.deleteRename(snapshotPath, snapshotId)));
}
}
Aggregations