use of org.apache.kafka.raft.OffsetAndEpoch in project kafka by apache.
the class FileRawSnapshotTest method testWritingSnapshot.
@Test
public void testWritingSnapshot() throws IOException {
OffsetAndEpoch offsetAndEpoch = new OffsetAndEpoch(10L, 3);
int bufferSize = 256;
int numberOfBatches = 10;
int expectedSize = 0;
try (FileRawSnapshotWriter snapshot = createSnapshotWriter(tempDir, offsetAndEpoch)) {
assertEquals(0, snapshot.sizeInBytes());
UnalignedMemoryRecords records = buildRecords(ByteBuffer.wrap(randomBytes(bufferSize)));
for (int i = 0; i < numberOfBatches; i++) {
snapshot.append(records);
expectedSize += records.sizeInBytes();
}
assertEquals(expectedSize, snapshot.sizeInBytes());
snapshot.freeze();
}
// File should exist and the size should be the sum of all the buffers
assertTrue(Files.exists(Snapshots.snapshotPath(tempDir, offsetAndEpoch)));
assertEquals(expectedSize, Files.size(Snapshots.snapshotPath(tempDir, offsetAndEpoch)));
}
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)));
}
}
use of org.apache.kafka.raft.OffsetAndEpoch in project kafka by apache.
the class Snapshots method parse.
public static Optional<SnapshotPath> parse(Path path) {
Path filename = path.getFileName();
if (filename == null) {
return Optional.empty();
}
String name = filename.toString();
boolean partial = false;
boolean deleted = false;
if (name.endsWith(PARTIAL_SUFFIX)) {
partial = true;
} else if (name.endsWith(DELETE_SUFFIX)) {
deleted = true;
} else if (!name.endsWith(SUFFIX)) {
return Optional.empty();
}
long endOffset = Long.parseLong(name.substring(0, OFFSET_WIDTH));
int epoch = Integer.parseInt(name.substring(OFFSET_WIDTH + 1, OFFSET_WIDTH + EPOCH_WIDTH + 1));
return Optional.of(new SnapshotPath(path, new OffsetAndEpoch(endOffset, epoch), partial, deleted));
}
Aggregations