Search in sources :

Example 71 with MutableLong

use of org.agrona.collections.MutableLong in project aeron by real-logic.

the class ReplayedBasicSubscriber method findLatestRecording.

private static long findLatestRecording(final AeronArchive archive) {
    final MutableLong lastRecordingId = new MutableLong();
    final RecordingDescriptorConsumer consumer = (controlSessionId, correlationId, recordingId, startTimestamp, stopTimestamp, startPosition, stopPosition, initialTermId, segmentFileLength, termBufferLength, mtuLength, sessionId, streamId, strippedChannel, originalChannel, sourceIdentity) -> lastRecordingId.set(recordingId);
    final long fromRecordingId = 0L;
    final int recordCount = 100;
    final int foundCount = archive.listRecordingsForUri(fromRecordingId, recordCount, CHANNEL, STREAM_ID, consumer);
    if (foundCount == 0) {
        throw new IllegalStateException("no recordings found");
    }
    return lastRecordingId.get();
}
Also used : MutableLong(org.agrona.collections.MutableLong) Subscription(io.aeron.Subscription) RecordingDescriptorConsumer(io.aeron.archive.client.RecordingDescriptorConsumer) SampleConfiguration(io.aeron.samples.SampleConfiguration) AeronArchive(io.aeron.archive.client.AeronArchive) ChannelUri(io.aeron.ChannelUri) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) FragmentHandler(io.aeron.logbuffer.FragmentHandler) SamplesUtil(io.aeron.samples.SamplesUtil) SigInt(org.agrona.concurrent.SigInt) MutableLong(org.agrona.collections.MutableLong) RecordingDescriptorConsumer(io.aeron.archive.client.RecordingDescriptorConsumer)

Example 72 with MutableLong

use of org.agrona.collections.MutableLong in project aeron by real-logic.

the class TestCluster method validateRecordingLogWithReplay.

public void validateRecordingLogWithReplay(final int nodeId) {
    final TestNode node = node(nodeId);
    final ConsensusModule.Context consensusModuleCtx = node.consensusModule().context();
    final AeronArchive.Context clone = consensusModuleCtx.archiveContext().clone();
    try (AeronArchive aeronArchive = AeronArchive.connect(clone);
        RecordingLog recordingLog = new RecordingLog(consensusModuleCtx.clusterDir(), false)) {
        final RecordingLog.Entry lastTerm = recordingLog.findLastTerm();
        assertNotNull(lastTerm);
        final long recordingId = lastTerm.recordingId;
        final long recordingPosition = aeronArchive.getRecordingPosition(recordingId);
        final Subscription replay = aeronArchive.replay(recordingId, 0, recordingPosition, "aeron:udp?endpoint=localhost:6666", 100001);
        final MutableLong position = new MutableLong();
        final MessageHeaderDecoder messageHeaderDecoder = new MessageHeaderDecoder();
        final NewLeadershipTermEventDecoder newLeadershipTermEventDecoder = new NewLeadershipTermEventDecoder();
        while (position.get() < recordingPosition) {
            replay.poll((buffer, offset, length, header) -> {
                messageHeaderDecoder.wrap(buffer, offset);
                if (NewLeadershipTermEventDecoder.TEMPLATE_ID == messageHeaderDecoder.templateId()) {
                    newLeadershipTermEventDecoder.wrapAndApplyHeader(buffer, offset, messageHeaderDecoder);
                    final RecordingLog.Entry termEntry = recordingLog.findTermEntry(newLeadershipTermEventDecoder.leadershipTermId());
                    assertNotNull(termEntry);
                    assertEquals(newLeadershipTermEventDecoder.termBaseLogPosition(), termEntry.termBaseLogPosition);
                    if (0 < newLeadershipTermEventDecoder.leadershipTermId()) {
                        final RecordingLog.Entry previousTermEntry = recordingLog.findTermEntry(newLeadershipTermEventDecoder.leadershipTermId() - 1);
                        assertNotNull(previousTermEntry);
                        assertEquals(newLeadershipTermEventDecoder.termBaseLogPosition(), previousTermEntry.logPosition, previousTermEntry.toString());
                    }
                }
                position.set(header.position());
            }, 10);
        }
    }
}
Also used : MessageHeaderDecoder(io.aeron.cluster.codecs.MessageHeaderDecoder) MutableLong(org.agrona.collections.MutableLong) NewLeadershipTermEventDecoder(io.aeron.cluster.codecs.NewLeadershipTermEventDecoder) AeronArchive(io.aeron.archive.client.AeronArchive)

Example 73 with MutableLong

use of org.agrona.collections.MutableLong in project aeron by real-logic.

the class ReplicateRecordingTest method shouldReplicateStoppedRecordingsConcurrently.

@Test
@InterruptAfter(10)
public void shouldReplicateStoppedRecordingsConcurrently() {
    final String messagePrefix = "Message-Prefix-";
    final int messageCount = 10;
    final long[] srcRecordingIds = new long[2];
    long position = 0;
    final long subscriptionId = srcAeronArchive.startRecording(LIVE_CHANNEL, LIVE_STREAM_ID, LOCAL);
    for (int i = 0; i < 2; i++) {
        try (Publication publication = srcAeron.addPublication(LIVE_CHANNEL, LIVE_STREAM_ID)) {
            final CountersReader counters = srcAeron.countersReader();
            final int counterId = awaitRecordingCounterId(counters, publication.sessionId());
            srcRecordingIds[i] = RecordingPos.getRecordingId(counters, counterId);
            offer(publication, messageCount, messagePrefix);
            position = publication.position();
            awaitPosition(counters, counterId, position);
        }
    }
    srcAeronArchive.stopRecording(subscriptionId);
    final MutableLong dstRecordingId = new MutableLong();
    final MutableReference<RecordingSignal> signalRef = new MutableReference<>();
    final RecordingSignalAdapter adapter = newRecordingSignalAdapter(signalRef, dstRecordingId);
    for (int i = 0; i < 2; i++) {
        dstAeronArchive.archiveProxy().replicate(srcRecordingIds[i], NULL_VALUE, SRC_CONTROL_STREAM_ID, SRC_CONTROL_REQUEST_CHANNEL, null, dstAeronArchive.context().aeron().nextCorrelationId(), dstAeronArchive.controlSessionId());
    }
    int stopCount = 0;
    while (stopCount < 2) {
        if (RecordingSignal.STOP == awaitSignal(signalRef, adapter)) {
            stopCount++;
        }
    }
    assertEquals(dstAeronArchive.getStopPosition(0), position);
    assertEquals(dstAeronArchive.getStopPosition(1), position);
}
Also used : MutableLong(org.agrona.collections.MutableLong) RecordingSignal(io.aeron.archive.codecs.RecordingSignal) MutableReference(org.agrona.collections.MutableReference) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CountersReader(org.agrona.concurrent.status.CountersReader) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 74 with MutableLong

use of org.agrona.collections.MutableLong in project aeron by real-logic.

the class ArchiveAuthenticationTest method shouldBeAbleToRecordWithAuthenticateOnChallengeResponse.

@Test
@InterruptAfter(10)
public void shouldBeAbleToRecordWithAuthenticateOnChallengeResponse() {
    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() {

        boolean challengeSuccessful = false;

        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) {
            assertEquals(sessionId, authenticatorSessionId.value);
            assertEquals(CREDENTIALS_STRING, new String(encodedCredentials));
            challengeSuccessful = true;
        }

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

        public void onChallengedSession(final SessionProxy sessionProxy, final long nowMs) {
            if (challengeSuccessful) {
                assertEquals(sessionProxy.sessionId(), authenticatorSessionId.value);
                sessionProxy.authenticate(PRINCIPAL_STRING.getBytes());
            }
        }
    });
    launchArchivingMediaDriver(() -> authenticator);
    connectClient(credentialsSupplier);
    assertEquals(aeronArchive.controlSessionId(), authenticatorSessionId.value);
    createRecording();
}
Also used : MutableLong(org.agrona.collections.MutableLong) CredentialsSupplier(io.aeron.security.CredentialsSupplier) SessionProxy(io.aeron.security.SessionProxy) Authenticator(io.aeron.security.Authenticator) Test(org.junit.jupiter.api.Test) InterruptAfter(io.aeron.test.InterruptAfter)

Example 75 with MutableLong

use of org.agrona.collections.MutableLong 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)

Aggregations

MutableLong (org.agrona.collections.MutableLong)82 Test (org.junit.jupiter.api.Test)68 InterruptAfter (io.aeron.test.InterruptAfter)44 MutableReference (org.agrona.collections.MutableReference)28 CountersReader (org.agrona.concurrent.status.CountersReader)19 RecordingSignal (io.aeron.archive.codecs.RecordingSignal)18 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 MediaDriver (io.aeron.driver.MediaDriver)14 FragmentHandler (io.aeron.logbuffer.FragmentHandler)14 DirectBuffer (org.agrona.DirectBuffer)13 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)11 RegistrationException (io.aeron.exceptions.RegistrationException)10 TestMediaDriver (io.aeron.test.driver.TestMediaDriver)10 AtomicLong (java.util.concurrent.atomic.AtomicLong)10 Assertions.assertThrows (org.junit.jupiter.api.Assertions.assertThrows)10 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)10 RegisterExtension (org.junit.jupiter.api.extension.RegisterExtension)10 AeronArchive (io.aeron.archive.client.AeronArchive)8 Authenticator (io.aeron.security.Authenticator)8 CredentialsSupplier (io.aeron.security.CredentialsSupplier)8