use of io.aeron.archive.Catalog in project aeron by real-logic.
the class ArchiveTool method compact.
static void compact(final PrintStream out, final File archiveDir, final EpochClock epochClock) {
final File compactFile = new File(archiveDir, CATALOG_FILE_NAME + ".compact");
try {
final Path compactFilePath = compactFile.toPath();
try (FileChannel channel = FileChannel.open(compactFilePath, READ, WRITE, CREATE_NEW);
Catalog catalog = openCatalogReadOnly(archiveDir, epochClock)) {
final MappedByteBuffer mappedByteBuffer = channel.map(READ_WRITE, 0, MAX_CATALOG_LENGTH);
mappedByteBuffer.order(CatalogHeaderEncoder.BYTE_ORDER);
try {
final UnsafeBuffer unsafeBuffer = new UnsafeBuffer(mappedByteBuffer);
new CatalogHeaderEncoder().wrap(unsafeBuffer, 0).version(catalog.version()).length(CatalogHeaderEncoder.BLOCK_LENGTH).nextRecordingId(catalog.nextRecordingId()).alignment(catalog.alignment());
final MutableInteger offset = new MutableInteger(CatalogHeaderEncoder.BLOCK_LENGTH);
final MutableInteger deletedRecords = new MutableInteger();
final MutableInteger reclaimedBytes = new MutableInteger();
catalog.forEach((recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> {
final int frameLength = headerDecoder.encodedLength() + headerDecoder.length();
if (INVALID == headerDecoder.state()) {
deletedRecords.increment();
reclaimedBytes.addAndGet(frameLength);
final String[] segmentFiles = listSegmentFiles(archiveDir, descriptorDecoder.recordingId());
if (segmentFiles != null) {
for (final String segmentFile : segmentFiles) {
IoUtil.deleteIfExists(new File(archiveDir, segmentFile));
}
}
} else {
final int index = offset.getAndAdd(frameLength);
unsafeBuffer.putBytes(index, headerDecoder.buffer(), 0, frameLength);
}
});
out.println("Compaction result: deleted " + deletedRecords.get() + " records and reclaimed " + reclaimedBytes.get() + " bytes");
} finally {
BufferUtil.free(mappedByteBuffer);
}
}
final Path catalogFilePath = compactFilePath.resolveSibling(CATALOG_FILE_NAME);
Files.delete(catalogFilePath);
Files.move(compactFilePath, catalogFilePath);
} catch (final IOException ex) {
ex.printStackTrace(out);
} finally {
IoUtil.deleteIfExists(compactFile);
}
}
use of io.aeron.archive.Catalog in project aeron by real-logic.
the class ArchiveTool method createVerifyEntryProcessor.
private static CatalogEntryProcessor createVerifyEntryProcessor(final PrintStream out, final File archiveDir, final Set<VerifyOption> options, final Catalog catalog, final Checksum checksum, final EpochClock epochClock, final MutableInteger errorCount, final ActionConfirmation<File> truncateOnPageStraddle) {
final ByteBuffer buffer = BufferUtil.allocateDirectAligned(FILE_IO_MAX_LENGTH_DEFAULT, CACHE_LINE_LENGTH);
buffer.order(LITTLE_ENDIAN);
final DataHeaderFlyweight headerFlyweight = new DataHeaderFlyweight(buffer);
return (recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> verifyRecording(out, archiveDir, options, catalog, checksum, epochClock, errorCount, truncateOnPageStraddle, headerFlyweight, recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder);
}
use of io.aeron.archive.Catalog in project aeron by real-logic.
the class ArchiveTool method checksum.
static void checksum(final PrintStream out, final File archiveDir, final boolean allFiles, final Checksum checksum, final EpochClock epochClock) {
try (Catalog catalog = openCatalogReadWrite(archiveDir, epochClock, MIN_CAPACITY, checksum, null)) {
final ByteBuffer buffer = ByteBuffer.allocateDirect(align(Configuration.MAX_UDP_PAYLOAD_LENGTH, CACHE_LINE_LENGTH));
buffer.order(LITTLE_ENDIAN);
catalog.forEach((recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> {
try {
catalog.updateChecksum(recordingDescriptorOffset);
checksum(buffer, out, archiveDir, allFiles, checksum, descriptorDecoder);
} catch (final Exception ex) {
out.println("(recordingId=" + descriptorDecoder.recordingId() + ") ERR: failed to compute checksums");
out.println(ex);
}
});
}
}
Aggregations