Search in sources :

Example 31 with Checksum

use of io.aeron.archive.checksum.Checksum in project aeron by real-logic.

the class ReplaySessionTest method shouldThrowArchiveExceptionIfCrcFails.

@Test
public void shouldThrowArchiveExceptionIfCrcFails() {
    final long length = 4 * FRAME_LENGTH;
    final long correlationId = 1L;
    final Checksum checksum = crc32();
    try (ReplaySession replaySession = replaySession(RECORDING_POSITION + 2 * FRAME_LENGTH, length, correlationId, mockReplayPub, mockControlSession, null, checksum)) {
        when(mockReplayPub.isClosed()).thenReturn(false);
        when(mockReplayPub.isConnected()).thenReturn(false);
        replaySession.doWork();
        assertEquals(ReplaySession.State.INIT, replaySession.state());
        when(mockReplayPub.isConnected()).thenReturn(true);
        final ArchiveException exception = assertThrows(ArchiveException.class, replaySession::doWork);
        assertEquals(ArchiveException.GENERIC, exception.errorCode());
        assertThat(exception.getMessage(), Matchers.startsWith("ERROR - CRC checksum mismatch at offset=0"));
        verify(mockReplayPub, never()).tryClaim(anyInt(), any(BufferClaim.class));
    }
}
Also used : Checksum(io.aeron.archive.checksum.Checksum) ArchiveException(io.aeron.archive.client.ArchiveException) BufferClaim(io.aeron.logbuffer.BufferClaim) Test(org.junit.jupiter.api.Test)

Example 32 with Checksum

use of io.aeron.archive.checksum.Checksum in project aeron by real-logic.

the class CatalogTest method extendRecordingShouldUpdateChecksum.

@Test
void extendRecordingShouldUpdateChecksum() {
    final Checksum checksum = crc32();
    try (Catalog catalog = new Catalog(archiveDir, null, 0, CAPACITY, clock, checksum, segmentFileBuffer)) {
        final long recordingId = catalog.addNewRecording(0L, 0L, 0, SEGMENT_LENGTH, TERM_LENGTH, MTU_LENGTH, 6, 1, "channelNew", "channelNew?tag=X", "sourceX");
        assertChecksum(catalog, recordingId, 1691549102);
        catalog.extendRecording(recordingId, 555, 13, 31);
        assertChecksum(catalog, recordingId, -1694749833);
    }
}
Also used : Checksum(io.aeron.archive.checksum.Checksum) Catalog(io.aeron.archive.Catalog) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 33 with Checksum

use of io.aeron.archive.checksum.Checksum in project aeron by real-logic.

the class ReplaySession method replay.

private int replay() throws IOException {
    if (!publication.isConnected()) {
        state(State.INACTIVE);
        return 0;
    }
    if (replayPosition >= stopPosition && null != limitPosition && noNewData(replayPosition, stopPosition)) {
        return 0;
    }
    if (termOffset == termLength) {
        nextTerm();
    }
    int workCount = 0;
    final int bytesRead = readRecording(stopPosition - replayPosition);
    if (bytesRead > 0) {
        int batchOffset = 0;
        int paddingFrameLength = 0;
        final int sessionId = publication.sessionId();
        final int streamId = publication.streamId();
        final long remaining = replayLimit - replayPosition;
        final Checksum checksum = this.checksum;
        final UnsafeBuffer replayBuffer = this.replayBuffer;
        while (batchOffset < bytesRead && batchOffset < remaining) {
            final int frameLength = frameLength(replayBuffer, batchOffset);
            if (frameLength <= 0) {
                raiseError(frameLength, bytesRead, batchOffset, remaining);
            }
            final int frameType = frameType(replayBuffer, batchOffset);
            final int alignedLength = align(frameLength, FRAME_ALIGNMENT);
            if (HDR_TYPE_DATA == frameType) {
                if (batchOffset + alignedLength > bytesRead) {
                    break;
                }
                if (null != checksum) {
                    verifyChecksum(checksum, batchOffset, alignedLength);
                }
                replayBuffer.putInt(batchOffset + SESSION_ID_FIELD_OFFSET, sessionId, LITTLE_ENDIAN);
                replayBuffer.putInt(batchOffset + STREAM_ID_FIELD_OFFSET, streamId, LITTLE_ENDIAN);
                batchOffset += alignedLength;
            } else if (HDR_TYPE_PAD == frameType) {
                paddingFrameLength = frameLength;
                break;
            }
        }
        if (batchOffset > 0) {
            final long position = publication.offerBlock(replayBuffer, 0, batchOffset);
            if (hasPublicationAdvanced(position, batchOffset)) {
                workCount++;
            } else {
                paddingFrameLength = 0;
            }
        }
        if (paddingFrameLength > 0) {
            final long position = publication.appendPadding(paddingFrameLength - HEADER_LENGTH);
            if (hasPublicationAdvanced(position, align(paddingFrameLength, FRAME_ALIGNMENT))) {
                workCount++;
            }
        }
    }
    return workCount;
}
Also used : Checksum(io.aeron.archive.checksum.Checksum) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer)

Example 34 with Checksum

use of io.aeron.archive.checksum.Checksum 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 35 with Checksum

use of io.aeron.archive.checksum.Checksum in project aeron by real-logic.

the class Catalog method refreshAndFixDescriptor.

private void refreshAndFixDescriptor(final RecordingDescriptorHeaderDecoder headerDecoder, final RecordingDescriptorEncoder encoder, final RecordingDescriptorDecoder decoder, final Checksum checksum, final UnsafeBuffer buffer) {
    final long recordingId = decoder.recordingId();
    if (VALID == headerDecoder.state() && NULL_POSITION == decoder.stopPosition()) {
        final String[] segmentFiles = listSegmentFiles(archiveDir, recordingId);
        final String maxSegmentFile = findSegmentFileWithHighestPosition(segmentFiles);
        encoder.stopPosition(computeStopPosition(archiveDir, maxSegmentFile, decoder.startPosition(), decoder.termBufferLength(), decoder.segmentFileLength(), checksum, buffer, (segmentFile) -> {
            throw new ArchiveException("Found potentially incomplete last fragment straddling page boundary in file: " + segmentFile.getAbsolutePath() + "\nRun `ArchiveTool verify` for corrective action!");
        }));
        encoder.stopTimestamp(epochClock.time());
    }
}
Also used : AsciiEncoding.parseLongAscii(org.agrona.AsciiEncoding.parseLongAscii) RECORDING_SEGMENT_SUFFIX(io.aeron.archive.Archive.Configuration.RECORDING_SEGMENT_SUFFIX) 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) BitUtil(org.agrona.BitUtil) org.agrona(org.agrona) READ_WRITE(java.nio.channels.FileChannel.MapMode.READ_WRITE) io.aeron.archive.codecs(io.aeron.archive.codecs) NULL_POSITION(io.aeron.archive.client.AeronArchive.NULL_POSITION) READ_ONLY(java.nio.channels.FileChannel.MapMode.READ_ONLY) FrameDescriptor(io.aeron.logbuffer.FrameDescriptor) HEADER_LENGTH(io.aeron.protocol.DataHeaderFlyweight.HEADER_LENGTH) Aeron(io.aeron.Aeron) Predicate(java.util.function.Predicate) StandardOpenOption(java.nio.file.StandardOpenOption) IOException(java.io.IOException) Math.min(java.lang.Math.min) Checksum(io.aeron.archive.checksum.Checksum) File(java.io.File) NULL_TIMESTAMP(io.aeron.archive.client.AeronArchive.NULL_TIMESTAMP) RecordingDescriptorDecoder(io.aeron.archive.codecs.RecordingDescriptorDecoder) INVALID(io.aeron.archive.codecs.RecordingState.INVALID) VALID(io.aeron.archive.codecs.RecordingState.VALID) ByteOrder.nativeOrder(java.nio.ByteOrder.nativeOrder) ArchiveException(io.aeron.archive.client.ArchiveException) EpochClock(org.agrona.concurrent.EpochClock) Math.max(java.lang.Math.max) FileChannel(java.nio.channels.FileChannel) MappedByteBuffer(java.nio.MappedByteBuffer) ArchiveException(io.aeron.archive.client.ArchiveException)

Aggregations

Checksum (io.aeron.archive.checksum.Checksum)36 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 Catalog (io.aeron.archive.Catalog)22 AeronArchive (io.aeron.archive.client.AeronArchive)16 Test (org.junit.jupiter.api.Test)16 INVALID (io.aeron.archive.codecs.RecordingState.INVALID)14 VALID (io.aeron.archive.codecs.RecordingState.VALID)14 File (java.io.File)14 IOException (java.io.IOException)14 ByteBuffer (java.nio.ByteBuffer)14 FileChannel (java.nio.channels.FileChannel)14 EpochClock (org.agrona.concurrent.EpochClock)14 RECORDING_SEGMENT_SUFFIX (io.aeron.archive.Archive.Configuration.RECORDING_SEGMENT_SUFFIX)12 APPLY_CHECKSUM (io.aeron.archive.ArchiveTool.VerifyOption.APPLY_CHECKSUM)12 VERIFY_ALL_SEGMENT_FILES (io.aeron.archive.ArchiveTool.VerifyOption.VERIFY_ALL_SEGMENT_FILES)12 LogBufferDescriptor.computeTermIdFromPosition (io.aeron.logbuffer.LogBufferDescriptor.computeTermIdFromPosition)12 LogBufferDescriptor.positionBitsToShift (io.aeron.logbuffer.LogBufferDescriptor.positionBitsToShift)12 DataHeaderFlyweight (io.aeron.protocol.DataHeaderFlyweight)12 PrintStream (java.io.PrintStream)12 Path (java.nio.file.Path)12