Search in sources :

Example 1 with HeaderFlyweight

use of io.aeron.protocol.HeaderFlyweight 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);
}
Also used : CACHE_LINE_LENGTH(org.agrona.BitUtil.CACHE_LINE_LENGTH) MigrationUtils.fullVersionString(io.aeron.archive.MigrationUtils.fullVersionString) NATIVE_BYTE_ORDER(org.agrona.BufferUtil.NATIVE_BYTE_ORDER) IntConsumer(java.util.function.IntConsumer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ByteBuffer(java.nio.ByteBuffer) FILE_IO_MAX_LENGTH_DEFAULT(io.aeron.archive.Archive.Configuration.FILE_IO_MAX_LENGTH_DEFAULT) org.agrona(org.agrona) Collectors.toMap(java.util.stream.Collectors.toMap) Path(java.nio.file.Path) HDR_TYPE_PAD(io.aeron.protocol.HeaderFlyweight.HDR_TYPE_PAD) NULL_POSITION(io.aeron.archive.client.AeronArchive.NULL_POSITION) LogBufferDescriptor.positionBitsToShift(io.aeron.logbuffer.LogBufferDescriptor.positionBitsToShift) HEADER_LENGTH(io.aeron.protocol.DataHeaderFlyweight.HEADER_LENGTH) VERIFY_ALL_SEGMENT_FILES(io.aeron.archive.ArchiveTool.VerifyOption.VERIFY_ALL_SEGMENT_FILES) StandardOpenOption(java.nio.file.StandardOpenOption) Math.min(java.lang.Math.min) INVALID(io.aeron.archive.codecs.RecordingState.INVALID) VALID(io.aeron.archive.codecs.RecordingState.VALID) Stream(java.util.stream.Stream) LITTLE_ENDIAN(java.nio.ByteOrder.LITTLE_ENDIAN) EpochClock(org.agrona.concurrent.EpochClock) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) ReplaySession.isInvalidHeader(io.aeron.archive.ReplaySession.isInvalidHeader) java.util(java.util) Checksums.newInstance(io.aeron.archive.checksum.Checksums.newInstance) Catalog(io.aeron.archive.Catalog) HeaderFlyweight(io.aeron.protocol.HeaderFlyweight) Configuration(io.aeron.driver.Configuration) READ_WRITE(java.nio.channels.FileChannel.MapMode.READ_WRITE) io.aeron.archive.codecs(io.aeron.archive.codecs) MutableInteger(org.agrona.collections.MutableInteger) PrintStream(java.io.PrintStream) FrameDescriptor(io.aeron.logbuffer.FrameDescriptor) Collections.emptySet(java.util.Collections.emptySet) Files(java.nio.file.Files) SESSION_ID_FIELD_OFFSET(io.aeron.protocol.DataHeaderFlyweight.SESSION_ID_FIELD_OFFSET) AeronArchive.segmentFileBasePosition(io.aeron.archive.client.AeronArchive.segmentFileBasePosition) IOException(java.io.IOException) CommonContext(io.aeron.CommonContext) Checksum(io.aeron.archive.checksum.Checksum) AeronException(io.aeron.exceptions.AeronException) File(java.io.File) INSTANCE(org.agrona.concurrent.SystemEpochClock.INSTANCE) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) APPLY_CHECKSUM(io.aeron.archive.ArchiveTool.VerifyOption.APPLY_CHECKSUM) CncFileDescriptor(io.aeron.CncFileDescriptor) MarkFileHeaderDecoder(io.aeron.archive.codecs.mark.MarkFileHeaderDecoder) HDR_TYPE_DATA(io.aeron.protocol.HeaderFlyweight.HDR_TYPE_DATA) FileChannel(java.nio.channels.FileChannel) BitUtil.align(org.agrona.BitUtil.align) CATALOG_FILE_NAME(io.aeron.archive.Archive.Configuration.CATALOG_FILE_NAME) MappedByteBuffer(java.nio.MappedByteBuffer) LogBufferDescriptor.computeTermIdFromPosition(io.aeron.logbuffer.LogBufferDescriptor.computeTermIdFromPosition) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 2 with HeaderFlyweight

use of io.aeron.protocol.HeaderFlyweight in project Aeron by real-logic.

the class ArchiveTool method checksumSegmentFile.

private static void checksumSegmentFile(final ByteBuffer buffer, final PrintStream out, final File archiveDir, final Checksum checksum, final long recordingId, final String fileName, final long startPosition, final int termLength) {
    final File file = new File(archiveDir, fileName);
    final long startTermOffset = startPosition & (termLength - 1);
    final long startTermBasePosition = startPosition - startTermOffset;
    final long segmentFileBasePosition = parseSegmentFilePosition(fileName);
    try (FileChannel channel = FileChannel.open(file.toPath(), READ, WRITE)) {
        final HeaderFlyweight headerFlyweight = new HeaderFlyweight(buffer);
        final long bufferAddress = headerFlyweight.addressOffset();
        final long size = channel.size();
        long fileOffset = segmentFileBasePosition == startTermBasePosition ? startTermOffset : 0;
        while (fileOffset < size) {
            buffer.clear().limit(HEADER_LENGTH);
            if (HEADER_LENGTH != channel.read(buffer, fileOffset)) {
                out.println("(recordingId=" + recordingId + ", file=" + file + ") ERR: failed to read fragment header");
                return;
            }
            final int frameLength = headerFlyweight.frameLength();
            if (0 == frameLength) {
                break;
            }
            final int alignedLength = align(frameLength, FRAME_ALIGNMENT);
            if (HDR_TYPE_DATA == frameType(headerFlyweight, 0)) {
                final int dataLength = alignedLength - HEADER_LENGTH;
                buffer.clear().limit(dataLength);
                if (dataLength != channel.read(buffer, fileOffset + HEADER_LENGTH)) {
                    out.println("(recordingId=" + recordingId + ", file=" + file + ") ERR: failed to read " + dataLength + " byte(s) of data at offset " + (fileOffset + HEADER_LENGTH));
                    return;
                }
                int checksumResult = checksum.compute(bufferAddress, 0, dataLength);
                if (NATIVE_BYTE_ORDER != LITTLE_ENDIAN) {
                    checksumResult = Integer.reverseBytes(checksumResult);
                }
                buffer.clear();
                buffer.putInt(checksumResult).flip();
                channel.write(buffer, fileOffset + SESSION_ID_FIELD_OFFSET);
            }
            fileOffset += alignedLength;
        }
    } catch (final Exception ex) {
        out.println("(recordingId=" + recordingId + ", file=" + file + ") ERR: failed to checksum");
        ex.printStackTrace(out);
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) HeaderFlyweight(io.aeron.protocol.HeaderFlyweight) File(java.io.File) IOException(java.io.IOException) AeronException(io.aeron.exceptions.AeronException)

Example 3 with HeaderFlyweight

use of io.aeron.protocol.HeaderFlyweight 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);
}
Also used : CACHE_LINE_LENGTH(org.agrona.BitUtil.CACHE_LINE_LENGTH) MigrationUtils.fullVersionString(io.aeron.archive.MigrationUtils.fullVersionString) NATIVE_BYTE_ORDER(org.agrona.BufferUtil.NATIVE_BYTE_ORDER) IntConsumer(java.util.function.IntConsumer) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) ByteBuffer(java.nio.ByteBuffer) FILE_IO_MAX_LENGTH_DEFAULT(io.aeron.archive.Archive.Configuration.FILE_IO_MAX_LENGTH_DEFAULT) org.agrona(org.agrona) Collectors.toMap(java.util.stream.Collectors.toMap) Path(java.nio.file.Path) HDR_TYPE_PAD(io.aeron.protocol.HeaderFlyweight.HDR_TYPE_PAD) NULL_POSITION(io.aeron.archive.client.AeronArchive.NULL_POSITION) LogBufferDescriptor.positionBitsToShift(io.aeron.logbuffer.LogBufferDescriptor.positionBitsToShift) HEADER_LENGTH(io.aeron.protocol.DataHeaderFlyweight.HEADER_LENGTH) VERIFY_ALL_SEGMENT_FILES(io.aeron.archive.ArchiveTool.VerifyOption.VERIFY_ALL_SEGMENT_FILES) StandardOpenOption(java.nio.file.StandardOpenOption) Math.min(java.lang.Math.min) INVALID(io.aeron.archive.codecs.RecordingState.INVALID) VALID(io.aeron.archive.codecs.RecordingState.VALID) Stream(java.util.stream.Stream) LITTLE_ENDIAN(java.nio.ByteOrder.LITTLE_ENDIAN) EpochClock(org.agrona.concurrent.EpochClock) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) ReplaySession.isInvalidHeader(io.aeron.archive.ReplaySession.isInvalidHeader) java.util(java.util) Checksums.newInstance(io.aeron.archive.checksum.Checksums.newInstance) Catalog(io.aeron.archive.Catalog) HeaderFlyweight(io.aeron.protocol.HeaderFlyweight) Configuration(io.aeron.driver.Configuration) READ_WRITE(java.nio.channels.FileChannel.MapMode.READ_WRITE) io.aeron.archive.codecs(io.aeron.archive.codecs) MutableInteger(org.agrona.collections.MutableInteger) PrintStream(java.io.PrintStream) FrameDescriptor(io.aeron.logbuffer.FrameDescriptor) Collections.emptySet(java.util.Collections.emptySet) Files(java.nio.file.Files) SESSION_ID_FIELD_OFFSET(io.aeron.protocol.DataHeaderFlyweight.SESSION_ID_FIELD_OFFSET) AeronArchive.segmentFileBasePosition(io.aeron.archive.client.AeronArchive.segmentFileBasePosition) IOException(java.io.IOException) CommonContext(io.aeron.CommonContext) Checksum(io.aeron.archive.checksum.Checksum) AeronException(io.aeron.exceptions.AeronException) File(java.io.File) INSTANCE(org.agrona.concurrent.SystemEpochClock.INSTANCE) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) APPLY_CHECKSUM(io.aeron.archive.ArchiveTool.VerifyOption.APPLY_CHECKSUM) CncFileDescriptor(io.aeron.CncFileDescriptor) MarkFileHeaderDecoder(io.aeron.archive.codecs.mark.MarkFileHeaderDecoder) HDR_TYPE_DATA(io.aeron.protocol.HeaderFlyweight.HDR_TYPE_DATA) FileChannel(java.nio.channels.FileChannel) BitUtil.align(org.agrona.BitUtil.align) CATALOG_FILE_NAME(io.aeron.archive.Archive.Configuration.CATALOG_FILE_NAME) MappedByteBuffer(java.nio.MappedByteBuffer) LogBufferDescriptor.computeTermIdFromPosition(io.aeron.logbuffer.LogBufferDescriptor.computeTermIdFromPosition) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer)

Example 4 with HeaderFlyweight

use of io.aeron.protocol.HeaderFlyweight in project aeron by real-logic.

the class ArchiveTool method checksumSegmentFile.

private static void checksumSegmentFile(final ByteBuffer buffer, final PrintStream out, final File archiveDir, final Checksum checksum, final long recordingId, final String fileName, final long startPosition, final int termLength) {
    final File file = new File(archiveDir, fileName);
    final long startTermOffset = startPosition & (termLength - 1);
    final long startTermBasePosition = startPosition - startTermOffset;
    final long segmentFileBasePosition = parseSegmentFilePosition(fileName);
    try (FileChannel channel = FileChannel.open(file.toPath(), READ, WRITE)) {
        final HeaderFlyweight headerFlyweight = new HeaderFlyweight(buffer);
        final long bufferAddress = headerFlyweight.addressOffset();
        final long size = channel.size();
        long fileOffset = segmentFileBasePosition == startTermBasePosition ? startTermOffset : 0;
        while (fileOffset < size) {
            buffer.clear().limit(HEADER_LENGTH);
            if (HEADER_LENGTH != channel.read(buffer, fileOffset)) {
                out.println("(recordingId=" + recordingId + ", file=" + file + ") ERR: failed to read fragment header");
                return;
            }
            final int frameLength = headerFlyweight.frameLength();
            if (0 == frameLength) {
                break;
            }
            final int alignedLength = align(frameLength, FRAME_ALIGNMENT);
            if (HDR_TYPE_DATA == frameType(headerFlyweight, 0)) {
                final int dataLength = alignedLength - HEADER_LENGTH;
                buffer.clear().limit(dataLength);
                if (dataLength != channel.read(buffer, fileOffset + HEADER_LENGTH)) {
                    out.println("(recordingId=" + recordingId + ", file=" + file + ") ERR: failed to read " + dataLength + " byte(s) of data at offset " + (fileOffset + HEADER_LENGTH));
                    return;
                }
                int checksumResult = checksum.compute(bufferAddress, 0, dataLength);
                if (NATIVE_BYTE_ORDER != LITTLE_ENDIAN) {
                    checksumResult = Integer.reverseBytes(checksumResult);
                }
                buffer.clear();
                buffer.putInt(checksumResult).flip();
                channel.write(buffer, fileOffset + SESSION_ID_FIELD_OFFSET);
            }
            fileOffset += alignedLength;
        }
    } catch (final Exception ex) {
        out.println("(recordingId=" + recordingId + ", file=" + file + ") ERR: failed to checksum");
        ex.printStackTrace(out);
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) HeaderFlyweight(io.aeron.protocol.HeaderFlyweight) File(java.io.File) IOException(java.io.IOException) AeronException(io.aeron.exceptions.AeronException)

Aggregations

AeronException (io.aeron.exceptions.AeronException)4 DataHeaderFlyweight (io.aeron.protocol.DataHeaderFlyweight)4 HeaderFlyweight (io.aeron.protocol.HeaderFlyweight)4 File (java.io.File)4 IOException (java.io.IOException)4 FileChannel (java.nio.channels.FileChannel)4 CncFileDescriptor (io.aeron.CncFileDescriptor)2 CommonContext (io.aeron.CommonContext)2 CATALOG_FILE_NAME (io.aeron.archive.Archive.Configuration.CATALOG_FILE_NAME)2 FILE_IO_MAX_LENGTH_DEFAULT (io.aeron.archive.Archive.Configuration.FILE_IO_MAX_LENGTH_DEFAULT)2 APPLY_CHECKSUM (io.aeron.archive.ArchiveTool.VerifyOption.APPLY_CHECKSUM)2 VERIFY_ALL_SEGMENT_FILES (io.aeron.archive.ArchiveTool.VerifyOption.VERIFY_ALL_SEGMENT_FILES)2 Catalog (io.aeron.archive.Catalog)2 MigrationUtils.fullVersionString (io.aeron.archive.MigrationUtils.fullVersionString)2 ReplaySession.isInvalidHeader (io.aeron.archive.ReplaySession.isInvalidHeader)2 Checksum (io.aeron.archive.checksum.Checksum)2 Checksums.newInstance (io.aeron.archive.checksum.Checksums.newInstance)2 NULL_POSITION (io.aeron.archive.client.AeronArchive.NULL_POSITION)2 AeronArchive.segmentFileBasePosition (io.aeron.archive.client.AeronArchive.segmentFileBasePosition)2 io.aeron.archive.codecs (io.aeron.archive.codecs)2