Search in sources :

Example 41 with ArchiveException

use of io.aeron.archive.client.ArchiveException in project aeron by real-logic.

the class RecordingWriter method onBlock.

/**
 * {@inheritDoc}
 */
public void onBlock(final DirectBuffer termBuffer, final int termOffset, final int length, final int sessionId, final int termId) {
    try {
        final boolean isPaddingFrame = termBuffer.getShort(typeOffset(termOffset)) == PADDING_FRAME_TYPE;
        final int dataLength = isPaddingFrame ? HEADER_LENGTH : length;
        final ByteBuffer byteBuffer;
        if (null == checksum || isPaddingFrame) {
            byteBuffer = termBuffer.byteBuffer();
            byteBuffer.limit(termOffset + dataLength).position(termOffset);
        } else {
            checksumBuffer.putBytes(0, termBuffer, termOffset, dataLength);
            computeChecksum(checksum, checksumBuffer, dataLength);
            byteBuffer = checksumBuffer.byteBuffer();
            byteBuffer.limit(dataLength).position(0);
        }
        int fileOffset = segmentOffset;
        do {
            fileOffset += recordingFileChannel.write(byteBuffer, fileOffset);
        } while (byteBuffer.remaining() > 0);
        if (forceWrites) {
            recordingFileChannel.force(forceMetadata);
        }
        segmentOffset += length;
        if (segmentOffset >= segmentLength) {
            onFileRollOver();
        }
    } catch (final ClosedByInterruptException ex) {
        close();
        throw new ArchiveException("file closed by interrupt, recording aborted", ex, ArchiveException.GENERIC);
    } catch (final IOException ex) {
        close();
        checkErrorType(ex, length);
    } catch (final Exception ex) {
        close();
        LangUtil.rethrowUnchecked(ex);
    }
}
Also used : ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) IOException(java.io.IOException) ArchiveException(io.aeron.archive.client.ArchiveException) ByteBuffer(java.nio.ByteBuffer) IOException(java.io.IOException) ClosedByInterruptException(java.nio.channels.ClosedByInterruptException) ArchiveException(io.aeron.archive.client.ArchiveException)

Example 42 with ArchiveException

use of io.aeron.archive.client.ArchiveException in project aeron by real-logic.

the class BasicArchiveTest method purgeRecordingFailsIfThereAreActiveReplays.

@Test
@InterruptAfter(10)
@SuppressWarnings("try")
public void purgeRecordingFailsIfThereAreActiveReplays() {
    final String messagePrefix = "Message-Prefix-";
    final int messageCount = 10;
    final long stopPosition;
    final long subscriptionId = aeronArchive.startRecording(RECORDED_CHANNEL, RECORDED_STREAM_ID, LOCAL);
    final long recordingIdFromCounter;
    final int sessionId;
    try (Subscription subscription = aeron.addSubscription(RECORDED_CHANNEL, RECORDED_STREAM_ID);
        Publication publication = aeron.addPublication(RECORDED_CHANNEL, RECORDED_STREAM_ID)) {
        sessionId = publication.sessionId();
        final CountersReader counters = aeron.countersReader();
        final int counterId = awaitRecordingCounterId(counters, sessionId);
        recordingIdFromCounter = RecordingPos.getRecordingId(counters, counterId);
        assertEquals(CommonContext.IPC_CHANNEL, RecordingPos.getSourceIdentity(counters, counterId));
        offer(publication, messageCount, messagePrefix);
        consume(subscription, messageCount, messagePrefix);
        stopPosition = publication.position();
        awaitPosition(counters, counterId, stopPosition);
        final long joinPosition = subscription.imageBySessionId(sessionId).joinPosition();
        assertEquals(joinPosition, aeronArchive.getStartPosition(recordingIdFromCounter));
        assertEquals(stopPosition, aeronArchive.getRecordingPosition(recordingIdFromCounter));
        assertEquals(NULL_VALUE, aeronArchive.getStopPosition(recordingIdFromCounter));
    }
    aeronArchive.stopRecording(subscriptionId);
    final long recordingId = aeronArchive.findLastMatchingRecording(0, "alias=" + RECORDED_CHANNEL_ALIAS, RECORDED_STREAM_ID, sessionId);
    assertEquals(recordingIdFromCounter, recordingId);
    assertEquals(stopPosition, aeronArchive.getStopPosition(recordingIdFromCounter));
    final long position = 0L;
    final long length = stopPosition - position;
    try (Subscription ignore = aeronArchive.replay(recordingId, position, length, REPLAY_CHANNEL, REPLAY_STREAM_ID)) {
        final ArchiveException exception = assertThrows(ArchiveException.class, () -> aeronArchive.purgeRecording(recordingId));
        assertThat(exception.getMessage(), endsWith("error: cannot purge recording with active replay " + recordingId));
        final String[] segmentFiles = Catalog.listSegmentFiles(archiveDir, recordingId);
        assertNotNull(segmentFiles);
        assertNotEquals(0, segmentFiles.length);
        for (final String segmentFile : segmentFiles) {
            assertTrue(new File(archiveDir, segmentFile).exists());
        }
    }
}
Also used : ArchiveException(io.aeron.archive.client.ArchiveException) File(java.io.File) CountersReader(org.agrona.concurrent.status.CountersReader) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 43 with ArchiveException

use of io.aeron.archive.client.ArchiveException in project aeron by real-logic.

the class BasicArchiveTest method purgeRecordingFailsIfRecordingIsActive.

@Test
@InterruptAfter(10)
public void purgeRecordingFailsIfRecordingIsActive() {
    final String messagePrefix = "Message-Prefix-";
    final int messageCount = 10;
    final long stopPosition;
    final long subscriptionId = aeronArchive.startRecording(RECORDED_CHANNEL, RECORDED_STREAM_ID, LOCAL);
    try {
        final long recordingIdFromCounter;
        final int sessionId;
        try (Subscription subscription = aeron.addSubscription(RECORDED_CHANNEL, RECORDED_STREAM_ID);
            Publication publication = aeron.addPublication(RECORDED_CHANNEL, RECORDED_STREAM_ID)) {
            sessionId = publication.sessionId();
            final CountersReader counters = aeron.countersReader();
            final int counterId = awaitRecordingCounterId(counters, sessionId);
            recordingIdFromCounter = RecordingPos.getRecordingId(counters, counterId);
            assertEquals(CommonContext.IPC_CHANNEL, RecordingPos.getSourceIdentity(counters, counterId));
            offer(publication, messageCount, messagePrefix);
            consume(subscription, messageCount, messagePrefix);
            stopPosition = publication.position();
            awaitPosition(counters, counterId, stopPosition);
            final long joinPosition = subscription.imageBySessionId(sessionId).joinPosition();
            assertEquals(joinPosition, aeronArchive.getStartPosition(recordingIdFromCounter));
            assertEquals(stopPosition, aeronArchive.getRecordingPosition(recordingIdFromCounter));
            assertEquals(NULL_VALUE, aeronArchive.getStopPosition(recordingIdFromCounter));
            final long recordingId = aeronArchive.findLastMatchingRecording(0, "alias=" + RECORDED_CHANNEL_ALIAS, RECORDED_STREAM_ID, sessionId);
            assertEquals(recordingIdFromCounter, recordingId);
            final ArchiveException exception = assertThrows(ArchiveException.class, () -> aeronArchive.purgeRecording(recordingId));
            assertThat(exception.getMessage(), endsWith("error: cannot purge active recording " + recordingId));
            final String[] segmentFiles = Catalog.listSegmentFiles(archiveDir, recordingId);
            assertNotNull(segmentFiles);
            assertNotEquals(0, segmentFiles.length);
            for (final String segmentFile : segmentFiles) {
                assertTrue(new File(archiveDir, segmentFile).exists());
            }
        }
    } finally {
        aeronArchive.stopRecording(subscriptionId);
    }
}
Also used : ArchiveException(io.aeron.archive.client.ArchiveException) File(java.io.File) CountersReader(org.agrona.concurrent.status.CountersReader) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 44 with ArchiveException

use of io.aeron.archive.client.ArchiveException 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)

Example 45 with ArchiveException

use of io.aeron.archive.client.ArchiveException in project aeron by real-logic.

the class Catalog method growCatalog.

void growCatalog(final long maxCatalogCapacity, final int frameLength) {
    final long oldCapacity = capacity;
    final long recordingOffset = nextRecordingDescriptorOffset;
    final long targetCapacity = recordingOffset + frameLength;
    if (targetCapacity > maxCatalogCapacity) {
        if (maxCatalogCapacity == oldCapacity) {
            throw new ArchiveException("catalog is full, max capacity reached: " + maxCatalogCapacity);
        } else {
            throw new ArchiveException("recording is too big: total recording length is " + frameLength + " bytes," + " available space is " + (maxCatalogCapacity - recordingOffset) + " bytes");
        }
    }
    long newCapacity = oldCapacity;
    while (newCapacity < targetCapacity) {
        newCapacity = min(newCapacity + (newCapacity >> 1), maxCatalogCapacity);
    }
    final MappedByteBuffer mappedByteBuffer;
    try {
        unmapAndCloseChannel();
        catalogChannel = FileChannel.open(catalogFile.toPath(), READ, WRITE, SPARSE);
        mappedByteBuffer = catalogChannel.map(READ_WRITE, 0, newCapacity);
    } catch (final Exception ex) {
        close();
        LangUtil.rethrowUnchecked(ex);
        return;
    }
    capacity = newCapacity;
    initBuffers(mappedByteBuffer);
    final UnsafeBuffer catalogHeaderBuffer = new UnsafeBuffer(catalogByteBuffer);
    catalogHeaderDecoder.wrap(catalogHeaderBuffer, 0, CatalogHeaderDecoder.BLOCK_LENGTH, CatalogHeaderDecoder.SCHEMA_VERSION);
    catalogHeaderEncoder.wrap(catalogHeaderBuffer, 0);
    catalogResized(oldCapacity, newCapacity);
}
Also used : MappedByteBuffer(java.nio.MappedByteBuffer) ArchiveException(io.aeron.archive.client.ArchiveException) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) IOException(java.io.IOException) ArchiveException(io.aeron.archive.client.ArchiveException)

Aggregations

ArchiveException (io.aeron.archive.client.ArchiveException)48 Test (org.junit.jupiter.api.Test)26 File (java.io.File)14 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 InterruptAfter (io.aeron.test.InterruptAfter)12 Catalog (io.aeron.archive.Catalog)10 IOException (java.io.IOException)10 CountersReader (org.agrona.concurrent.status.CountersReader)10 ByteBuffer (java.nio.ByteBuffer)8 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)8 Context (io.aeron.archive.Archive.Context)6 Checksum (io.aeron.archive.checksum.Checksum)6 AeronArchive (io.aeron.archive.client.AeronArchive)6 MappedByteBuffer (java.nio.MappedByteBuffer)6 Aeron (io.aeron.Aeron)4 CommonContext (io.aeron.CommonContext)4 MediaDriver (io.aeron.driver.MediaDriver)4 FrameDescriptor (io.aeron.logbuffer.FrameDescriptor)4 DataHeaderFlyweight (io.aeron.protocol.DataHeaderFlyweight)4 Authenticator (io.aeron.security.Authenticator)4