org.agrona.concurrent.status.AtomicCounter Java Examples

The following examples show how to use org.agrona.concurrent.status.AtomicCounter. 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: SampleUtil.java    From artio with Apache License 2.0 6 votes vote down vote up
public static void runAgentUntilSignal(
    final Agent agent, final MediaDriver mediaDriver) throws InterruptedException
{
    final AtomicCounter errorCounter =
        mediaDriver.context().countersManager().newCounter("exchange_agent_errors");
    final AgentRunner runner = new AgentRunner(
        CommonConfiguration.backoffIdleStrategy(),
        Throwable::printStackTrace,
        errorCounter,
        agent);

    final Thread thread = AgentRunner.startOnThread(runner);

    final AtomicBoolean running = new AtomicBoolean(true);
    SigInt.register(() -> running.set(false));

    while (running.get())
    {
        Thread.sleep(100);
    }

    thread.join();
}
 
Example #2
Source File: ReceiveDestinationTransport.java    From aeron with Apache License 2.0 6 votes vote down vote up
public void openChannel(final DriverConductorProxy conductorProxy, final AtomicCounter statusIndicator)
{
    if (conductorProxy.notConcurrent())
    {
        openDatagramChannel(statusIndicator);
    }
    else
    {
        try
        {
            openDatagramChannel(statusIndicator);
        }
        catch (final Exception ex)
        {
            conductorProxy.channelEndpointError(statusIndicator.id(), ex);
            throw ex;
        }
    }

    LocalSocketAddressStatus.updateBindAddress(
        localSocketAddressIndicator, bindAddressAndPort(), context.countersMetaDataBuffer());
    localSocketAddressIndicator.setOrdered(ChannelEndpointStatus.ACTIVE);
}
 
Example #3
Source File: DriverConductor.java    From aeron with Apache License 2.0 6 votes vote down vote up
private AeronClient getOrAddClient(final long clientId)
{
    AeronClient client = findClient(clients, clientId);
    if (null == client)
    {
        final AtomicCounter counter = ClientHeartbeatTimestamp.allocate(tempBuffer, countersManager, clientId);
        counter.setOrdered(cachedEpochClock.time());

        client = new AeronClient(
            clientId,
            clientLivenessTimeoutNs,
            ctx.systemCounters().get(SystemCounterDescriptor.CLIENT_TIMEOUTS),
            counter);
        clients.add(client);

        clientProxy.onCounterReady(clientId, counter.id());
    }

    return client;
}
 
Example #4
Source File: AgentRunner.java    From agrona with Apache License 2.0 6 votes vote down vote up
/**
 * Create an agent runner and initialise it.
 *
 * @param idleStrategy to use for Agent run loop
 * @param errorHandler to be called if an {@link Throwable} is encountered
 * @param errorCounter to be incremented each time an exception is encountered. This may be null.
 * @param agent        to be run in this thread.
 */
public AgentRunner(
    final IdleStrategy idleStrategy,
    final ErrorHandler errorHandler,
    final AtomicCounter errorCounter,
    final Agent agent)
{
    Objects.requireNonNull(idleStrategy, "idleStrategy");
    Objects.requireNonNull(errorHandler, "errorHandler");
    Objects.requireNonNull(agent, "agent");

    this.idleStrategy = idleStrategy;
    this.errorHandler = errorHandler;
    this.errorCounter = errorCounter;
    this.agent = agent;
}
 
Example #5
Source File: ClusterNodeRestartTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
@Timeout(10)
public void shouldRestartServiceFromEmptySnapshot()
{
    final AtomicLong serviceMsgCount = new AtomicLong(0);

    launchService(serviceMsgCount);
    connectClient();

    final AtomicCounter controlToggle = getControlToggle();
    assertTrue(ClusterControl.ToggleState.SNAPSHOT.toggle(controlToggle));

    Tests.awaitValue(clusteredMediaDriver.consensusModule().context().snapshotCounter(), 1);

    forceCloseForRestart();

    serviceState.set(null);
    launchClusteredMediaDriver(false);
    launchService(serviceMsgCount);
    connectClient();

    Tests.await(() -> null != serviceState.get());
    assertEquals("0", serviceState.get());

    ClusterTests.failOnClusterError();
}
 
Example #6
Source File: Tests.java    From aeron with Apache License 2.0 6 votes vote down vote up
public static void awaitValue(final AtomicCounter counter, final long value)
{
    long counterValue;
    while ((counterValue = counter.get()) < value)
    {
        Thread.yield();
        if (Thread.interrupted())
        {
            unexpectedInterruptStackTrace("awaiting=" + value + " counter=" + counterValue);
            fail("unexpected interrupt");
        }

        if (counter.isClosed())
        {
            unexpectedInterruptStackTrace("awaiting=" + value + " counter=" + counterValue);
        }
    }
}
 
Example #7
Source File: ClusterNodeRestartTest.java    From aeron with Apache License 2.0 6 votes vote down vote up
@Test
@Timeout(10)
public void shouldTakeMultipleSnapshots()
{
    final AtomicLong serviceMsgCount = new AtomicLong(0);
    launchService(serviceMsgCount);
    connectClient();

    final AtomicCounter controlToggle = getControlToggle();

    for (int i = 0; i < 3; i++)
    {
        assertTrue(ClusterControl.ToggleState.SNAPSHOT.toggle(controlToggle));

        while (controlToggle.get() != ClusterControl.ToggleState.NEUTRAL.code())
        {
            Tests.sleep(1, "snapshot ", i);
        }
    }

    assertEquals(3L, clusteredMediaDriver.consensusModule().context().snapshotCounter().get());

    ClusterTests.failOnClusterError();
}
 
Example #8
Source File: SenderEndPointTest.java    From artio with Apache License 2.0 6 votes vote down vote up
private AtomicCounter fakeCounter()
{
    final AtomicLong value = new AtomicLong();
    final AtomicCounter atomicCounter = mock(AtomicCounter.class);
    final Answer<Long> get = inv -> value.get();
    final Answer<?> set = inv ->
    {
        value.set(inv.getArgument(0));
        return null;
    };

    final Answer<?> add = (inv) -> value.getAndAdd(inv.getArgument(0));

    when(atomicCounter.get()).then(get);
    when(atomicCounter.getWeak()).then(get);

    doAnswer(set).when(atomicCounter).set(anyLong());
    doAnswer(set).when(atomicCounter).setOrdered(anyLong());
    doAnswer(set).when(atomicCounter).setWeak(anyLong());

    when(atomicCounter.getAndAdd(anyLong())).then(add);
    when(atomicCounter.getAndAddOrdered(anyLong())).then(add);

    return atomicCounter;
}
 
Example #9
Source File: LocalSocketAddressStatus.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Allocate a counter to represent a local socket address associated with a channel.
 *
 * @param tempBuffer      for building up the key and label.
 * @param countersManager which will allocate the counter.
 * @param channelStatusId with which the new counter is associated.
 * @param name            for the counter to put in the label.
 * @param typeId          to categorise the counter.
 * @return the allocated counter.
 */
public static AtomicCounter allocate(
    final MutableDirectBuffer tempBuffer,
    final CountersManager countersManager,
    final int channelStatusId,
    final String name,
    final int typeId)
{
    tempBuffer.putInt(0, channelStatusId);
    tempBuffer.putInt(LOCAL_SOCKET_ADDRESS_LENGTH_OFFSET, 0);

    final int keyLength = INITIAL_LENGTH;

    int labelLength = 0;
    labelLength += tempBuffer.putStringWithoutLengthAscii(keyLength + labelLength, name);
    labelLength += tempBuffer.putStringWithoutLengthAscii(keyLength + labelLength, ": ");
    labelLength += tempBuffer.putIntAscii(keyLength + labelLength, channelStatusId);
    labelLength += tempBuffer.putStringWithoutLengthAscii(keyLength + labelLength, " ");

    return countersManager.newCounter(typeId, tempBuffer, 0, keyLength, tempBuffer, keyLength, labelLength);
}
 
Example #10
Source File: ChannelEndpointStatus.java    From aeron with Apache License 2.0 6 votes vote down vote up
/**
 * Allocate an indicator for tracking the status of a channel endpoint.
 *
 * @param tempBuffer      to be used for labels and metadata.
 * @param name            of the counter for the label.
 * @param typeId          of the counter for classification.
 * @param countersManager from which to allocated the underlying storage.
 * @param channel         for the stream of messages.
 * @return a new {@link AtomicCounter} for tracking the status.
 */
public static AtomicCounter allocate(
    final MutableDirectBuffer tempBuffer,
    final String name,
    final int typeId,
    final CountersManager countersManager,
    final String channel)
{
    final int keyLength = tempBuffer.putStringWithoutLengthAscii(
        CHANNEL_OFFSET + SIZE_OF_INT, channel, 0, MAX_CHANNEL_LENGTH);
    tempBuffer.putInt(CHANNEL_OFFSET, keyLength);

    int labelLength = 0;
    labelLength += tempBuffer.putStringWithoutLengthAscii(keyLength + labelLength, name);
    labelLength += tempBuffer.putStringWithoutLengthAscii(keyLength + labelLength, ": ");
    labelLength += tempBuffer.putStringWithoutLengthAscii(
        keyLength + labelLength, channel, 0, MAX_LABEL_LENGTH - labelLength);

    if (labelLength < MAX_LABEL_LENGTH)
    {
        tempBuffer.putByte(keyLength + labelLength, (byte)' ');
        labelLength += 1;
    }

    return countersManager.newCounter(typeId, tempBuffer, 0, keyLength, tempBuffer, keyLength, labelLength);
}
 
Example #11
Source File: SingleNodeCluster.java    From aeron with Apache License 2.0 6 votes vote down vote up
void takeSnapshot()
{
    final AtomicCounter snapshotCounter = clusteredMediaDriver.consensusModule().context().snapshotCounter();
    final long snapshotCount = snapshotCounter.get();

    final AtomicCounter controlToggle = ClusterControl.findControlToggle(
        clusteredMediaDriver.mediaDriver().context().countersManager(),
        clusteredMediaDriver.consensusModule().context().clusterId());
    ClusterControl.ToggleState.SNAPSHOT.toggle(controlToggle);

    idleStrategy.reset();
    while (snapshotCounter.get() <= snapshotCount)
    {
        idleStrategy.idle();
    }
}
 
Example #12
Source File: ClusterBackupMediaDriver.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Launch a new {@link ClusterBackupMediaDriver} with provided contexts.
 *
 * @param driverCtx        for configuring the {@link MediaDriver}.
 * @param archiveCtx       for configuring the {@link Archive}.
 * @param clusterBackupCtx for the configuration of the {@link ClusterBackup}.
 * @return a new {@link ClusterBackupMediaDriver} with the provided contexts.
 */
public static ClusterBackupMediaDriver launch(
    final MediaDriver.Context driverCtx,
    final Archive.Context archiveCtx,
    final ClusterBackup.Context clusterBackupCtx)
{
    MediaDriver driver = null;
    Archive archive = null;
    ClusterBackup clusterBackup = null;

    try
    {
        driver = MediaDriver.launch(driverCtx
            .spiesSimulateConnection(true));

        final int errorCounterId = SystemCounterDescriptor.ERRORS.id();
        final AtomicCounter errorCounter = null == archiveCtx.errorCounter() ?
            new AtomicCounter(driverCtx.countersValuesBuffer(), errorCounterId) : archiveCtx.errorCounter();

        final ErrorHandler errorHandler = null == archiveCtx.errorHandler() ?
            driverCtx.errorHandler() : archiveCtx.errorHandler();

        archive = Archive.launch(archiveCtx
            .aeronDirectoryName(driverCtx.aeronDirectoryName())
            .mediaDriverAgentInvoker(driver.sharedAgentInvoker())
            .errorHandler(errorHandler)
            .errorCounter(errorCounter));

        clusterBackup = ClusterBackup.launch(clusterBackupCtx
            .aeronDirectoryName(driverCtx.aeronDirectoryName()));

        return new ClusterBackupMediaDriver(driver, archive, clusterBackup);
    }
    catch (final Throwable throwable)
    {
        CloseHelper.quietCloseAll(driver, archive, clusterBackup);
        throw throwable;
    }
}
 
Example #13
Source File: ClientCommandAdapter.java    From aeron with Apache License 2.0 5 votes vote down vote up
ClientCommandAdapter(
    final AtomicCounter errors,
    final ErrorHandler errorHandler,
    final RingBuffer toDriverCommands,
    final ClientProxy clientProxy,
    final DriverConductor driverConductor)
{
    this.errors = errors;
    this.errorHandler = errorHandler;
    this.toDriverCommands = toDriverCommands;
    this.clientProxy = clientProxy;
    this.conductor = driverConductor;
}
 
Example #14
Source File: StartFromTruncatedRecordingLogTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void shutdown(final int index)
{
    final AtomicCounter controlToggle = getControlToggle(index);
    assertNotNull(controlToggle);

    assertTrue(
        ClusterControl.ToggleState.SHUTDOWN.toggle(controlToggle),
        String.valueOf(ClusterControl.ToggleState.get(controlToggle)));
}
 
Example #15
Source File: DebugSendChannelEndpoint.java    From aeron with Apache License 2.0 5 votes vote down vote up
public DebugSendChannelEndpoint(
    final UdpChannel udpChannel, final AtomicCounter statusIndicator, final MediaDriver.Context context)
{
    this(
        udpChannel,
        statusIndicator,
        context,
        DebugChannelEndpointConfiguration.sendDataLossGeneratorSupplier(),
        DebugChannelEndpointConfiguration.sendControlLossGeneratorSupplier());
}
 
Example #16
Source File: ClusterNodeRestartTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
@Timeout(20)
public void shouldRestartServiceAfterShutdownWithInvalidatedSnapshot() throws InterruptedException
{
    final AtomicLong serviceMsgCount = new AtomicLong(0);

    launchService(serviceMsgCount);
    connectClient();

    sendNumberedMessageIntoCluster(0);
    sendNumberedMessageIntoCluster(1);
    sendNumberedMessageIntoCluster(2);

    Tests.awaitValue(serviceMsgCount, 3);

    final AtomicCounter controlToggle = getControlToggle();
    assertTrue(ClusterControl.ToggleState.SHUTDOWN.toggle(controlToggle));

    terminationLatch.await();
    forceCloseForRestart();

    final PrintStream mockOut = mock(PrintStream.class);
    final File clusterDir = clusteredMediaDriver.consensusModule().context().clusterDir();
    assertTrue(ClusterTool.invalidateLatestSnapshot(mockOut, clusterDir));

    verify(mockOut).println(" invalidate latest snapshot: true");

    serviceMsgCount.set(0);
    launchClusteredMediaDriver(false);
    launchService(serviceMsgCount);

    Tests.awaitValue(serviceMsgCount, 3);
    assertEquals("3", serviceState.get());

    connectClient();
    sendNumberedMessageIntoCluster(3);
    Tests.awaitValue(serviceMsgCount, 4);

    ClusterTests.failOnClusterError();
}
 
Example #17
Source File: ClusterNodeRestartTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
private AtomicCounter getControlToggle()
{
    final int clusterId = container.context().clusterId();
    final CountersReader counters = container.context().aeron().countersReader();
    final AtomicCounter controlToggle = ClusterControl.findControlToggle(counters, clusterId);
    assertNotNull(controlToggle);

    return controlToggle;
}
 
Example #18
Source File: TestCluster.java    From aeron with Apache License 2.0 5 votes vote down vote up
void awaitNeutralControlToggle(final TestNode leaderNode)
{
    final AtomicCounter controlToggle = getControlToggle(leaderNode);
    while (controlToggle.get() != ClusterControl.ToggleState.NEUTRAL.code())
    {
        Tests.yield();
    }
}
 
Example #19
Source File: TestCluster.java    From aeron with Apache License 2.0 5 votes vote down vote up
AtomicCounter getControlToggle(final TestNode leaderNode)
{
    final CountersReader counters = leaderNode.countersReader();
    final int clusterId = leaderNode.consensusModule().context().clusterId();
    final AtomicCounter controlToggle = ClusterControl.findControlToggle(counters, clusterId);
    assertNotNull(controlToggle);

    return controlToggle;
}
 
Example #20
Source File: SendLocalSocketAddress.java    From aeron with Apache License 2.0 5 votes vote down vote up
public static AtomicCounter allocate(
    final MutableDirectBuffer tempBuffer, final CountersManager countersManager, final int channelStatusId)
{
    return LocalSocketAddressStatus.allocate(
        tempBuffer,
        countersManager,
        channelStatusId,
        NAME,
        LocalSocketAddressStatus.LOCAL_SOCKET_ADDRESS_STATUS_TYPE_ID);
}
 
Example #21
Source File: ClusterNodeRestartTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
@Test
@Timeout(10)
public void shouldRestartServiceFromSnapshot()
{
    final AtomicLong serviceMsgCount = new AtomicLong(0);

    launchService(serviceMsgCount);
    connectClient();

    sendNumberedMessageIntoCluster(0);
    sendNumberedMessageIntoCluster(1);
    sendNumberedMessageIntoCluster(2);

    Tests.awaitValue(serviceMsgCount, 3);

    final AtomicCounter controlToggle = getControlToggle();
    assertTrue(ClusterControl.ToggleState.SNAPSHOT.toggle(controlToggle));

    Tests.awaitValue(clusteredMediaDriver.consensusModule().context().snapshotCounter(), 1);

    forceCloseForRestart();

    serviceState.set(null);
    launchClusteredMediaDriver(false);
    launchService(serviceMsgCount);
    connectClient();

    Tests.await(() -> null != serviceState.get());
    assertEquals("3", serviceState.get());

    ClusterTests.failOnClusterError();
}
 
Example #22
Source File: StartFromTruncatedRecordingLogTest.java    From aeron with Apache License 2.0 5 votes vote down vote up
private void takeSnapshot(final int index)
{
    final ClusteredMediaDriver driver = clusteredMediaDrivers[index];
    final CountersReader counters = driver.consensusModule().context().aeron().countersReader();
    final int clusterId = driver.consensusModule().context().clusterId();
    final AtomicCounter controlToggle = ClusterControl.findControlToggle(counters, clusterId);

    assertNotNull(controlToggle);
    awaitNeutralCounter(index);
    assertTrue(ClusterControl.ToggleState.SNAPSHOT.toggle(controlToggle));
}
 
Example #23
Source File: ClusterControl.java    From aeron with Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args)
{
    checkUsage(args);

    final ToggleState toggleState = ToggleState.valueOf(args[0].toUpperCase());

    final File cncFile = CommonContext.newDefaultCncFile();
    System.out.println("Command `n Control file " + cncFile);

    final CountersReader countersReader = mapCounters(cncFile);
    final int clusterId = ClusteredServiceContainer.Configuration.clusterId();
    final AtomicCounter controlToggle = findControlToggle(countersReader, clusterId);

    if (null == controlToggle)
    {
        System.out.println("Failed to find control toggle");
        System.exit(0);
    }

    if (toggleState.toggle(controlToggle))
    {
        System.out.println(toggleState + " toggled successfully");
    }
    else
    {
        System.out.println(toggleState + " did NOT toggle: current state=" + ToggleState.get(controlToggle));
    }
}
 
Example #24
Source File: DebugSendChannelEndpoint.java    From aeron with Apache License 2.0 5 votes vote down vote up
public DebugSendChannelEndpoint(
    final UdpChannel udpChannel,
    final AtomicCounter statusIndicator,
    final MediaDriver.Context context,
    final LossGenerator dataLossGenerator,
    final LossGenerator controlLossGenerator)
{
    super(udpChannel, statusIndicator, context);

    this.dataLossGenerator = dataLossGenerator;
    this.controlLossGenerator = controlLossGenerator;
}
 
Example #25
Source File: ClusterControl.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Toggle the control counter to trigger the requested {@link ToggleState}.
 * <p>
 * This action is thread safe and will succeed if the toggle is in the {@link ToggleState#NEUTRAL} state,
 * or if toggle is {@link ToggleState#SUSPEND} and requested state is {@link ToggleState#RESUME}.
 *
 * @param controlToggle to change to the trigger state.
 * @return true if the counter toggles or false if it is in a state other than {@link ToggleState#NEUTRAL}.
 */
public final boolean toggle(final AtomicCounter controlToggle)
{
    if (code() == RESUME.code() && controlToggle.get() == SUSPEND.code())
    {
        return controlToggle.compareAndSet(SUSPEND.code(), RESUME.code());
    }

    return controlToggle.compareAndSet(NEUTRAL.code(), code());
}
 
Example #26
Source File: DriverNameResolverCache.java    From aeron with Apache License 2.0 5 votes vote down vote up
void addOrUpdateEntry(
    final byte[] name,
    final int nameLength,
    final long nowMs,
    final byte type,
    final byte[] address,
    final int port,
    final AtomicCounter cacheEntriesCounter)
{
    final int existingEntryIndex = findEntryIndexByNameAndType(name, nameLength, type);
    final int addressLength = ResolutionEntryFlyweight.addressLength(type);
    final CacheEntry entry;

    if (INVALID_INDEX == existingEntryIndex)
    {
        entry = new CacheEntry(
            Arrays.copyOf(name, nameLength),
            type,
            nowMs,
            nowMs + timeoutMs,
            Arrays.copyOf(address, addressLength),
            port);
        entries.add(entry);
        cacheEntriesCounter.setOrdered(entries.size());
    }
    else
    {
        entry = entries.get(existingEntryIndex);
        entry.timeOfLastActivityMs = nowMs;
        entry.deadlineMs = nowMs + timeoutMs;

        if (port != entry.port || byteSubsetEquals(address, entry.address, addressLength))
        {
            entry.address = Arrays.copyOf(address, addressLength);
            entry.port = port;
        }
    }
}
 
Example #27
Source File: DebugReceiveChannelEndpointSupplier.java    From aeron with Apache License 2.0 5 votes vote down vote up
public ReceiveChannelEndpoint newInstance(
    final UdpChannel udpChannel,
    final DataPacketDispatcher dispatcher,
    final AtomicCounter statusIndicator,
    final MediaDriver.Context context)
{
    return new DebugReceiveChannelEndpoint(udpChannel, dispatcher, statusIndicator, context);
}
 
Example #28
Source File: ArchivingMediaDriver.java    From benchmarks with Apache License 2.0 5 votes vote down vote up
static ArchivingMediaDriver launchArchiveWithEmbeddedDriver()
{
    MediaDriver driver = null;
    Archive archive = null;
    try
    {
        final MediaDriver.Context driverCtx = new MediaDriver.Context()
            .dirDeleteOnStart(true)
            .spiesSimulateConnection(true);

        driver = MediaDriver.launch(driverCtx);

        final Archive.Context archiveCtx = new Archive.Context()
            .aeronDirectoryName(driverCtx.aeronDirectoryName())
            .deleteArchiveOnStart(true);

        final int errorCounterId = SystemCounterDescriptor.ERRORS.id();
        final AtomicCounter errorCounter = null == archiveCtx.errorCounter() ?
            new AtomicCounter(driverCtx.countersValuesBuffer(), errorCounterId) : archiveCtx.errorCounter();

        final ErrorHandler errorHandler = null == archiveCtx.errorHandler() ?
            driverCtx.errorHandler() : archiveCtx.errorHandler();

        archive = Archive.launch(archiveCtx
            .mediaDriverAgentInvoker(driver.sharedAgentInvoker())
            .aeronDirectoryName(driverCtx.aeronDirectoryName())
            .errorHandler(errorHandler)
            .errorCounter(errorCounter));
        return new ArchivingMediaDriver(driver, archive);
    }
    catch (final Exception ex)
    {
        CloseHelper.quietCloseAll(archive, driver);
        throw ex;
    }
}
 
Example #29
Source File: ArchivingMediaDriver.java    From aeron with Apache License 2.0 5 votes vote down vote up
/**
 * Launch a new {@link ArchivingMediaDriver} with provided contexts.
 *
 * @param driverCtx  for configuring the {@link MediaDriver}.
 * @param archiveCtx for configuring the {@link Archive}.
 * @return a new {@link ArchivingMediaDriver} with the provided contexts.
 */
public static ArchivingMediaDriver launch(final MediaDriver.Context driverCtx, final Archive.Context archiveCtx)
{
    MediaDriver driver = null;
    Archive archive = null;
    try
    {
        driver = MediaDriver.launch(driverCtx);

        final int errorCounterId = SystemCounterDescriptor.ERRORS.id();
        final AtomicCounter errorCounter = null == archiveCtx.errorCounter() ?
            new AtomicCounter(driverCtx.countersValuesBuffer(), errorCounterId) : archiveCtx.errorCounter();

        final ErrorHandler errorHandler = null == archiveCtx.errorHandler() ?
            driverCtx.errorHandler() : archiveCtx.errorHandler();

        archive = Archive.launch(archiveCtx
            .mediaDriverAgentInvoker(driver.sharedAgentInvoker())
            .aeronDirectoryName(driverCtx.aeronDirectoryName())
            .errorHandler(errorHandler)
            .errorCounter(errorCounter));

        return new ArchivingMediaDriver(driver, archive);
    }
    catch (final Exception ex)
    {
        CloseHelper.quietCloseAll(archive, driver);
        throw ex;
    }
}
 
Example #30
Source File: ReceiveChannelEndpoint.java    From aeron with Apache License 2.0 5 votes vote down vote up
public ReceiveChannelEndpoint(
    final UdpChannel udpChannel,
    final DataPacketDispatcher dispatcher,
    final AtomicCounter statusIndicator,
    final MediaDriver.Context context)
{
    super(udpChannel, udpChannel.remoteData(), udpChannel.remoteData(), null, context);

    this.dispatcher = dispatcher;
    this.statusIndicator = statusIndicator;

    shortSends = context.systemCounters().get(SHORT_SENDS);
    possibleTtlAsymmetry = context.systemCounters().get(POSSIBLE_TTL_ASYMMETRY);

    final ReceiveChannelEndpointThreadLocals threadLocals = context.receiveChannelEndpointThreadLocals();
    smBuffer = threadLocals.smBuffer();
    statusMessageFlyweight = threadLocals.statusMessageFlyweight();
    nakBuffer = threadLocals.nakBuffer();
    nakFlyweight = threadLocals.nakFlyweight();
    rttMeasurementBuffer = threadLocals.rttMeasurementBuffer();
    rttMeasurementFlyweight = threadLocals.rttMeasurementFlyweight();
    cachedNanoClock = context.cachedNanoClock();
    timeOfLastActivityNs = cachedNanoClock.nanoTime();
    receiverId = threadLocals.receiverId();

    final String groupTagValue = udpChannel.channelUri().get(CommonContext.GROUP_TAG_PARAM_NAME);
    groupTag = null == groupTagValue ? context.receiverGroupTag() : Long.valueOf(groupTagValue);

    multiRcvDestination = udpChannel.isManualControlMode() ?
        new MultiRcvDestination(context.nanoClock(), DESTINATION_ADDRESS_TIMEOUT, errorHandler) : null;
    currentControlAddress = udpChannel.localControl();
}