Search in sources :

Example 11 with ArchiveException

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

the class ClusteredServiceAgent method onTakeSnapshot.

private long onTakeSnapshot(final long logPosition, final long leadershipTermId) {
    try (AeronArchive archive = AeronArchive.connect(ctx.archiveContext().clone());
        ExclusivePublication publication = aeron.addExclusivePublication(ctx.snapshotChannel(), ctx.snapshotStreamId())) {
        final String channel = ChannelUri.addSessionId(ctx.snapshotChannel(), publication.sessionId());
        archive.startRecording(channel, ctx.snapshotStreamId(), LOCAL, true);
        final CountersReader counters = aeron.countersReader();
        final int counterId = awaitRecordingCounter(publication.sessionId(), counters, archive);
        final long recordingId = RecordingPos.getRecordingId(counters, counterId);
        snapshotState(publication, logPosition, leadershipTermId);
        checkForClockTick();
        archive.checkForErrorResponse();
        service.onTakeSnapshot(publication);
        awaitRecordingComplete(recordingId, publication.position(), counters, counterId, archive);
        return recordingId;
    } catch (final ArchiveException ex) {
        if (ex.errorCode() == ArchiveException.STORAGE_SPACE) {
            throw new AgentTerminationException(ex);
        }
        throw ex;
    }
}
Also used : ArchiveException(io.aeron.archive.client.ArchiveException) AeronArchive(io.aeron.archive.client.AeronArchive) CountersReader(org.agrona.concurrent.status.CountersReader)

Example 12 with ArchiveException

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

the class ArchiveAuthenticationTest method shouldNotBeAbleToConnectWithRejectOnConnectRequest.

@Test
@InterruptAfter(10)
public void shouldNotBeAbleToConnectWithRejectOnConnectRequest() {
    final MutableLong authenticatorSessionId = new MutableLong(-1L);
    final CredentialsSupplier credentialsSupplier = spy(new CredentialsSupplier() {

        public byte[] encodedCredentials() {
            return NULL_CREDENTIAL;
        }

        public byte[] onChallenge(final byte[] encodedChallenge) {
            assertEquals(CHALLENGE_STRING, new String(encodedChallenge));
            return encodedCredentials;
        }
    });
    final Authenticator authenticator = spy(new Authenticator() {

        public void onConnectRequest(final long sessionId, final byte[] encodedCredentials, final long nowMs) {
            authenticatorSessionId.value = sessionId;
            assertEquals(0, encodedCredentials.length);
        }

        public void onChallengeResponse(final long sessionId, final byte[] encodedCredentials, final long nowMs) {
            fail();
        }

        public void onConnectedSession(final SessionProxy sessionProxy, final long nowMs) {
            assertEquals(sessionProxy.sessionId(), authenticatorSessionId.value);
            sessionProxy.reject();
        }

        public void onChallengedSession(final SessionProxy sessionProxy, final long nowMs) {
            fail();
        }
    });
    launchArchivingMediaDriver(() -> authenticator);
    try {
        connectClient(credentialsSupplier);
    } catch (final ArchiveException ex) {
        assertEquals(ArchiveException.AUTHENTICATION_REJECTED, ex.errorCode());
        return;
    }
    fail("should have seen exception");
}
Also used : MutableLong(org.agrona.collections.MutableLong) CredentialsSupplier(io.aeron.security.CredentialsSupplier) SessionProxy(io.aeron.security.SessionProxy) ArchiveException(io.aeron.archive.client.ArchiveException) Authenticator(io.aeron.security.Authenticator) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 13 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 14 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 15 with ArchiveException

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

the class CatalogTest method shouldThrowExceptionAfterFailureOnPageStraddle.

@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, CAPACITY, clock, null, segmentFileBuffer);
        catalog.close();
    });
    assertThat(exception.getMessage(), containsString(segmentFile.getAbsolutePath()));
}
Also used : FileChannel(java.nio.channels.FileChannel) DataHeaderFlyweight(io.aeron.protocol.DataHeaderFlyweight) ArchiveException(io.aeron.archive.client.ArchiveException) File(java.io.File) ByteBuffer(java.nio.ByteBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) Catalog(io.aeron.archive.Catalog) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

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