io.aeron.archive.client.ArchiveException Java Examples

The following examples show how to use io.aeron.archive.client.ArchiveException. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: RecordingCoordinator.java    From artio with Apache License 2.0 6 votes vote down vote up
private boolean startRecording(
    final int streamId,
    final int sessionId,
    final SourceLocation local)
{

    if (recordingAlreadyStarted(sessionId))
    {
        return true;
    }

    try
    {
        final String channel = ChannelUri.addSessionId(this.channel, sessionId);
        final long registrationId = archive.startRecording(channel, streamId, local);
        trackedRegistrationIds.add(registrationId);

        return true;
    }
    catch (final ArchiveException e)
    {
        errorHandler.onError(e);

        return false;
    }
}
 
Example #2
Source File: RecordingWriterTests.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
void onBlockThrowsArchiveExceptionIfCurrentThreadWasInterrupted() throws IOException
{
    final Image image = mockImage(0L);
    final RecordingWriter recordingWriter = new RecordingWriter(
        1, 0, SEGMENT_LENGTH, image, new Context().archiveDir(archiveDir), null, null, null);
    recordingWriter.init();

    assertFalse(Thread.interrupted());
    try
    {
        Thread.currentThread().interrupt();
        final ArchiveException exception = assertThrows(
            ArchiveException.class,
            () -> recordingWriter.onBlock(new UnsafeBuffer(allocate(32)), 0, 10, 5, 8));
        assertEquals(GENERIC, exception.errorCode());
        assertEquals("file closed by interrupt, recording aborted", exception.getMessage());
        assertTrue(recordingWriter.isClosed());
    }
    finally
    {
        assertTrue(Thread.interrupted());
    }
}
 
Example #3
Source File: Catalog.java    From aeron with Apache License 2.0 6 votes vote down vote up
static long parseSegmentFilePosition(final String filename)
{
    final int dashOffset = filename.indexOf('-');
    if (-1 == dashOffset)
    {
        throw new ArchiveException("invalid filename format: " + filename);
    }

    final int positionOffset = dashOffset + 1;
    final int positionLength = filename.length() - positionOffset - RECORDING_SEGMENT_SUFFIX.length();
    if (0 >= positionLength)
    {
        throw new ArchiveException("no position encoded in the segment file: " + filename);
    }

    return parseLongAscii(filename, positionOffset, positionLength);
}
 
Example #4
Source File: ControlResponseProxy.java    From aeron with Apache License 2.0 6 votes vote down vote up
private static void checkResult(final ControlSession session, final long result)
{
    if (result == Publication.NOT_CONNECTED)
    {
        session.abort();
        throw new ArchiveException(
            "response publication is not connected: " + session, AeronException.Category.WARN);
    }

    if (result == Publication.CLOSED)
    {
        session.abort();
        throw new ArchiveException("response publication is closed: " + session);
    }

    if (result == Publication.MAX_POSITION_EXCEEDED)
    {
        session.abort();
        throw new ArchiveException("response publication at max position: " + session);
    }
}
 
Example #5
Source File: ReplaySession.java    From aeron with Apache License 2.0 6 votes vote down vote up
static boolean notHeaderAligned(
    final FileChannel channel,
    final UnsafeBuffer buffer,
    final int segmentOffset,
    final int termOffset,
    final int termId,
    final int streamId) throws IOException
{
    final ByteBuffer byteBuffer = buffer.byteBuffer();
    byteBuffer.clear().limit(HEADER_LENGTH);
    if (HEADER_LENGTH != channel.read(byteBuffer, segmentOffset))
    {
        throw new ArchiveException("failed to read fragment header");
    }

    return isInvalidHeader(buffer, streamId, termId, termOffset);
}
 
Example #6
Source File: ArchiveMarkFile.java    From aeron with Apache License 2.0 6 votes vote down vote up
public static int alignedTotalFileLength(final Archive.Context ctx)
{
    final int headerContentLength =
        MarkFileHeaderEncoder.BLOCK_LENGTH +
        (4 * VarAsciiEncodingEncoder.lengthEncodingLength()) +
        ctx.controlChannel().length() +
        ctx.localControlChannel().length() +
        ctx.recordingEventsChannel().length() +
        ctx.aeronDirectoryName().length();

    if (headerContentLength > HEADER_LENGTH)
    {
        throw new ArchiveException(
            "MarkFile length required " + headerContentLength + " greater than " + HEADER_LENGTH);
    }

    return HEADER_LENGTH + ctx.errorBufferLength();
}
 
Example #7
Source File: ControlSessionDemuxer.java    From aeron with Apache License 2.0 5 votes vote down vote up
private ControlSession getControlSession(final long controlSessionId, final long correlationId)
{
    final ControlSession controlSession = controlSessionByIdMap.get(controlSessionId);
    if (controlSession == null)
    {
        final String message = "unknown controlSessionId=" + controlSessionId +
            " for correlationId=" + correlationId +
            " from source=" + image.sourceIdentity();

        throw new ArchiveException(message, correlationId, AeronException.Category.WARN);
    }

    return controlSession;
}
 
Example #8
Source File: ArchiveConductor.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void deleteSegmentFile(
    final long correlationId,
    final long recordingId,
    final long segmentBasePosition,
    final ControlSession controlSession)
{
    final File file = new File(archiveDir, segmentFileName(recordingId, segmentBasePosition));
    if (file.exists() && !file.delete())
    {
        final String msg = "failed to delete " + file;
        controlSession.sendErrorResponse(correlationId, msg, controlResponseProxy);
        throw new ArchiveException(msg);
    }
}
 
Example #9
Source File: RecordingEventsProxy.java    From aeron with Apache License 2.0 5 votes vote down vote up
private static void checkResult(final long result)
{
    if (result == Publication.CLOSED)
    {
        throw new ArchiveException("recording events publication is closed");
    }

    if (result == Publication.MAX_POSITION_EXCEEDED)
    {
        throw new ArchiveException("recording events publication at max position");
    }
}
 
Example #10
Source File: ReplaySession.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void verifyChecksum(final Checksum checksum, final int frameOffset, final int alignedLength)
{
    final int computedChecksum = checksum.compute(
        replayBufferAddress, frameOffset + HEADER_LENGTH, alignedLength - HEADER_LENGTH);
    final int recordedChecksum = frameSessionId(replayBuffer, frameOffset);

    if (computedChecksum != recordedChecksum)
    {
        final String message = "CRC checksum mismatch at offset=" + frameOffset + ": recorded checksum=" +
            recordedChecksum + ", computed checksum=" + computedChecksum;
        throw new ArchiveException(message);
    }
}
 
Example #11
Source File: AeronUtil.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
static long replayFullRecording(
    final AeronArchive aeronArchive, final long recordingId, final String replayChannel, final int replayStreamId)
{
    while (true)
    {
        try
        {
            return aeronArchive.startReplay(recordingId, 0, MAX_VALUE, replayChannel, replayStreamId);
        }
        catch (final ArchiveException ignore)
        {
            yieldUninterruptedly();
        }
    }
}
 
Example #12
Source File: Catalog.java    From aeron with Apache License 2.0 5 votes vote down vote up
static void validateMaxEntries(final long maxEntries)
{
    if (maxEntries < 1 || maxEntries > MAX_ENTRIES)
    {
        throw new ArchiveException(
            "Catalog max entries must be between 1 and " + MAX_ENTRIES + ": maxEntries=" + maxEntries);
    }
}
 
Example #13
Source File: Catalog.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void refreshAndFixDescriptor(
    @SuppressWarnings("unused") final RecordingDescriptorHeaderEncoder unused,
    final RecordingDescriptorHeaderDecoder headerDecoder,
    final RecordingDescriptorEncoder encoder,
    final RecordingDescriptorDecoder decoder,
    final Checksum checksum,
    final UnsafeBuffer buffer)
{
    final long recordingId = decoder.recordingId();
    if (VALID == headerDecoder.valid() && 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());
    }

    nextRecordingId = recordingId + 1;
}
 
Example #14
Source File: Catalog.java    From aeron with Apache License 2.0 5 votes vote down vote up
static String findSegmentFileWithHighestPosition(final String[] segmentFiles)
{
    if (null == segmentFiles || 0 == segmentFiles.length)
    {
        return null;
    }

    long maxSegmentPosition = NULL_POSITION;
    String maxFileName = null;

    for (final String filename : segmentFiles)
    {
        final long filePosition = parseSegmentFilePosition(filename);
        if (filePosition < 0)
        {
            throw new ArchiveException("negative position encoded in the file name: " + filename);
        }

        if (filePosition > maxSegmentPosition)
        {
            maxSegmentPosition = filePosition;
            maxFileName = filename;
        }
    }

    return maxFileName;
}
 
Example #15
Source File: RecordingCoordinator.java    From artio with Apache License 2.0 5 votes vote down vote up
private void extendRecording(
    final int streamId, final LibraryExtendPosition libraryExtendPosition, final int sessionId)
{
    try
    {
        final String recordingChannel = ChannelUri.addSessionId(channel, sessionId);
        final long registrationId = archive.extendRecording(
            libraryExtendPosition.recordingId, recordingChannel, streamId, LOCAL);
        trackedRegistrationIds.add(registrationId);
    }
    catch (final ArchiveException e)
    {
        errorHandler.onError(e);
    }
}
 
Example #16
Source File: ReplaySessionTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@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("CRC checksum mismatch at offset=0"));
        verify(mockReplayPub, never()).tryClaim(anyInt(), any(BufferClaim.class));
    }
}
 
Example #17
Source File: CatalogTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
void shouldThrowExceptionAfterFailureOnPageStraddle() throws Exception
{
    final long newRecordingId = newRecording();
    final File segmentFile = new File(archiveDir, segmentFileName(newRecordingId, 0));
    try (FileChannel log = FileChannel.open(segmentFile.toPath(), READ, WRITE, CREATE))
    {
        final ByteBuffer bb = allocate(HEADER_LENGTH);
        final DataHeaderFlyweight flyweight = new DataHeaderFlyweight(bb);
        flyweight.frameLength(PAGE_SIZE - 128);
        log.write(bb);

        bb.clear();
        flyweight.frameLength(256);
        log.write(bb, PAGE_SIZE - 128);

        bb.clear();
        bb.put(0, (byte)0).limit(1).position(0);
        log.write(bb, PAGE_SIZE + 127);
    }

    final ArchiveException exception = assertThrows(
        ArchiveException.class,
        () ->
        {
            final Catalog catalog = new Catalog(archiveDir, null, 0, MAX_ENTRIES, clock, null, null);
            catalog.close();
        });
    assertThat(exception.getMessage(), containsString(segmentFile.getAbsolutePath()));
}
 
Example #18
Source File: ArchiveConductor.java    From aeron with Apache License 2.0 4 votes vote down vote up
private void extendRecordingSession(
    final ControlSession controlSession,
    final long correlationId,
    final long recordingId,
    final String strippedChannel,
    final String originalChannel,
    final Image image,
    final boolean autoStop)
{
    try
    {
        if (recordingSessionByIdMap.containsKey(recordingId))
        {
            final String msg = "cannot extend active recording for " + recordingId;
            controlSession.attemptErrorResponse(correlationId, ACTIVE_RECORDING, msg, controlResponseProxy);
            throw new ArchiveException(msg);
        }

        catalog.recordingSummary(recordingId, recordingSummary);
        validateImageForExtendRecording(correlationId, controlSession, image, recordingSummary);

        final Counter position = RecordingPos.allocate(
            aeron,
            counterMetadataBuffer,
            recordingId,
            image.sessionId(),
            image.subscription().streamId(),
            strippedChannel,
            image.sourceIdentity());

        position.setOrdered(image.joinPosition());

        final RecordingSession session = new RecordingSession(
            correlationId,
            recordingId,
            recordingSummary.startPosition,
            recordingSummary.segmentFileLength,
            originalChannel,
            recordingEventsProxy,
            image,
            position,
            archiveDirChannel,
            ctx,
            controlSession,
            ctx.recordChecksumBuffer(),
            ctx.recordChecksum(),
            autoStop);

        recordingSessionByIdMap.put(recordingId, session);
        catalog.extendRecording(recordingId, controlSession.sessionId(), correlationId, image.sessionId());
        recorder.addSession(session);

        controlSession.attemptSignal(
            correlationId,
            recordingId,
            image.subscription().registrationId(),
            image.joinPosition(),
            RecordingSignal.EXTEND);
    }
    catch (final Exception ex)
    {
        errorHandler.onError(ex);
        if (autoStop)
        {
            removeRecordingSubscription(image.subscription().registrationId());
            CloseHelper.close(errorHandler, image.subscription());
        }
    }
}
 
Example #19
Source File: Catalog.java    From aeron with Apache License 2.0 4 votes vote down vote up
long addNewRecording(
    final long startPosition,
    final long stopPosition,
    final long startTimestamp,
    final long stopTimestamp,
    final int imageInitialTermId,
    final int segmentFileLength,
    final int termBufferLength,
    final int mtuLength,
    final int sessionId,
    final int streamId,
    final String strippedChannel,
    final String originalChannel,
    final String sourceIdentity)
{
    if (nextRecordingId > maxRecordingId)
    {
        throw new ArchiveException("catalog is full, max recordings reached: " + maxEntries());
    }

    final int combinedStringsLen = strippedChannel.length() + sourceIdentity.length() + originalChannel.length();
    if (combinedStringsLen > maxDescriptorStringsCombinedLength)
    {
        throw new ArchiveException("combined length of channel:'" + strippedChannel +
            "' and sourceIdentity:'" + sourceIdentity +
            "' and originalChannel:'" + originalChannel +
            "' exceeds max allowed:" + maxDescriptorStringsCombinedLength);
    }

    final long recordingId = nextRecordingId++;

    catalogBuffer.wrap(catalogByteBuffer, recordingDescriptorOffset(recordingId), recordLength);
    descriptorEncoder
        .wrap(catalogBuffer, DESCRIPTOR_HEADER_LENGTH)
        .recordingId(recordingId)
        .startTimestamp(startTimestamp)
        .stopTimestamp(stopTimestamp)
        .startPosition(startPosition)
        .stopPosition(stopPosition)
        .initialTermId(imageInitialTermId)
        .segmentFileLength(segmentFileLength)
        .termBufferLength(termBufferLength)
        .mtuLength(mtuLength)
        .sessionId(sessionId)
        .streamId(streamId)
        .strippedChannel(strippedChannel)
        .originalChannel(originalChannel)
        .sourceIdentity(sourceIdentity);

    descriptorHeaderEncoder
        .wrap(catalogBuffer, 0)
        .length(descriptorEncoder.encodedLength())
        .valid(VALID);

    forceWrites(catalogChannel, forceWrites, forceMetadata);

    return recordingId;
}
 
Example #20
Source File: Indexer.java    From artio with Apache License 2.0 4 votes vote down vote up
private void catchIndexUp(final AeronArchive aeronArchive, final ErrorHandler errorHandler)
{
    final IdleStrategy idleStrategy = CommonConfiguration.backoffIdleStrategy();
    final AgentInvoker aeronInvoker = aeronArchive.context().aeron().conductorAgentInvoker();

    for (int i = 0, size = indices.size(); i < size; i++)
    {
        final Index index = indices.get(i);

        index.readLastPosition((aeronSessionId, recordingId, indexStoppedPosition) ->
        {
            try
            {
                final long recordingStoppedPosition = aeronArchive.getStopPosition(recordingId);
                if (recordingStoppedPosition > indexStoppedPosition)
                {
                    DebugLogger.log(
                        LogTag.INDEX,
                        catchupFormatter,
                        index.getName(),
                        recordingId,
                        recordingStoppedPosition,
                        indexStoppedPosition);

                    final long length = recordingStoppedPosition - indexStoppedPosition;
                    try (Subscription subscription = aeronArchive.replay(
                        recordingId, indexStoppedPosition, length, IPC_CHANNEL, archiveReplayStream))
                    {
                        // Only do 1 replay at a time
                        while (subscription.imageCount() != 1)
                        {
                            idle(idleStrategy, aeronInvoker, 0);
                            aeronArchive.checkForErrorResponse();
                        }
                        idleStrategy.reset();

                        final Image replayImage = subscription.imageAtIndex(0);

                        final FragmentHandler handler = (buffer, offset, srcLength, header) ->
                            index.onCatchup(buffer, offset, srcLength, header, recordingId);

                        while (replayImage.position() < recordingStoppedPosition)
                        {
                            final int workCount = replayImage.poll(handler, LIMIT);
                            idle(idleStrategy, aeronInvoker, workCount);
                        }
                        idleStrategy.reset();
                    }
                }
            }
            catch (final ArchiveException ex)
            {
                errorHandler.onError(ex);
            }
        });
    }
}