Search in sources :

Example 51 with DirectBuffer

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

the class DriverTool method main.

/**
 * Main method for launching the process.
 *
 * @param args passed to the process.
 */
public static void main(final String[] args) {
    boolean printPidOnly = false;
    boolean terminateDriver = false;
    if (0 != args.length) {
        checkForHelp(args);
        if (args[0].equals("pid")) {
            printPidOnly = true;
        } else if (args[0].equals("terminate")) {
            terminateDriver = true;
        }
    }
    final File cncFile = CommonContext.newDefaultCncFile();
    final MappedByteBuffer cncByteBuffer = IoUtil.mapExistingFile(cncFile, "cnc");
    final DirectBuffer cncMetaData = createMetaDataBuffer(cncByteBuffer);
    final int cncVersion = cncMetaData.getInt(cncVersionOffset(0));
    checkVersion(cncVersion);
    final ManyToOneRingBuffer toDriver = new ManyToOneRingBuffer(createToDriverBuffer(cncByteBuffer, cncMetaData));
    if (printPidOnly) {
        System.out.println(pid(cncMetaData));
    } else if (terminateDriver) {
        final DriverProxy driverProxy = new DriverProxy(toDriver, toDriver.nextCorrelationId());
        if (!driverProxy.terminateDriver(null, 0, 0)) {
            throw new AeronException("could not send termination request.");
        }
    } else {
        System.out.println("Command `n Control file: " + cncFile);
        System.out.format("Version: %d, PID: %d%n", cncVersion, pid(cncMetaData));
        printDateActivityAndStartTimestamps(startTimestampMs(cncMetaData), toDriver.consumerHeartbeatTime());
    }
}
Also used : DirectBuffer(org.agrona.DirectBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) ManyToOneRingBuffer(org.agrona.concurrent.ringbuffer.ManyToOneRingBuffer) AeronException(io.aeron.exceptions.AeronException) DriverProxy(io.aeron.DriverProxy) File(java.io.File)

Example 52 with DirectBuffer

use of org.agrona.DirectBuffer in project Aeron by real-logic.

the class FragmentedMessageTest method shouldReceivePublishedMessage.

@Theory
@Test(timeout = 10000)
public void shouldReceivePublishedMessage(final String channel, final ThreadingMode threadingMode) throws Exception {
    final MediaDriver.Context ctx = new MediaDriver.Context();
    ctx.threadingMode(threadingMode);
    final FragmentAssembler adapter = new FragmentAssembler(mockFragmentHandler);
    try (MediaDriver ignore = MediaDriver.launch(ctx);
        Aeron aeron = Aeron.connect();
        Publication publication = aeron.addPublication(channel, STREAM_ID);
        Subscription subscription = aeron.addSubscription(channel, STREAM_ID)) {
        final UnsafeBuffer srcBuffer = new UnsafeBuffer(new byte[ctx.mtuLength() * 4]);
        final int offset = 0;
        final int length = srcBuffer.capacity() / 4;
        for (int i = 0; i < 4; i++) {
            srcBuffer.setMemory(i * length, length, (byte) (65 + i));
        }
        while (publication.offer(srcBuffer, offset, srcBuffer.capacity()) < 0L) {
            Thread.yield();
        }
        final int expectedFragmentsBecauseOfHeader = 5;
        int numFragments = 0;
        do {
            numFragments += subscription.poll(adapter, FRAGMENT_COUNT_LIMIT);
        } while (numFragments < expectedFragmentsBecauseOfHeader);
        final ArgumentCaptor<DirectBuffer> bufferArg = ArgumentCaptor.forClass(DirectBuffer.class);
        final ArgumentCaptor<Header> headerArg = ArgumentCaptor.forClass(Header.class);
        verify(mockFragmentHandler, times(1)).onFragment(bufferArg.capture(), eq(offset), eq(srcBuffer.capacity()), headerArg.capture());
        final DirectBuffer capturedBuffer = bufferArg.getValue();
        for (int i = 0; i < srcBuffer.capacity(); i++) {
            assertThat("same at i=" + i, capturedBuffer.getByte(i), is(srcBuffer.getByte(i)));
        }
        assertThat(headerArg.getValue().flags(), is(END_FRAG_FLAG));
    } finally {
        ctx.deleteAeronDirectory();
    }
}
Also used : DataPoint(org.junit.experimental.theories.DataPoint) DirectBuffer(org.agrona.DirectBuffer) MediaDriver(io.aeron.driver.MediaDriver) Header(io.aeron.logbuffer.Header) UnsafeBuffer(org.agrona.concurrent.UnsafeBuffer) Theory(org.junit.experimental.theories.Theory) Test(org.junit.Test)

Example 53 with DirectBuffer

use of org.agrona.DirectBuffer in project Aeron by real-logic.

the class AeronStat method mapCounters.

public static CountersReader mapCounters() {
    final File cncFile = CommonContext.newDefaultCncFile();
    System.out.println("Command `n Control file " + cncFile);
    final MappedByteBuffer cncByteBuffer = IoUtil.mapExistingFile(cncFile, "cnc");
    final DirectBuffer cncMetaData = createMetaDataBuffer(cncByteBuffer);
    final int cncVersion = cncMetaData.getInt(cncVersionOffset(0));
    if (CncFileDescriptor.CNC_VERSION != cncVersion) {
        throw new IllegalStateException("CnC version not supported: file version=" + cncVersion);
    }
    return new CountersReader(createCountersMetaDataBuffer(cncByteBuffer, cncMetaData), createCountersValuesBuffer(cncByteBuffer, cncMetaData));
}
Also used : DirectBuffer(org.agrona.DirectBuffer) MappedByteBuffer(java.nio.MappedByteBuffer) File(java.io.File) CountersReader(org.agrona.concurrent.status.CountersReader)

Example 54 with DirectBuffer

use of org.agrona.DirectBuffer in project nd4j by deeplearning4j.

the class BaseTransport method sendCommandToShard.

/**
 * This command is possible to issue only from Client
 *
 * @param message
 */
protected synchronized void sendCommandToShard(VoidMessage message) {
    // TODO: we want LocalTransport to be used in such cases
    if (nodeRole == NodeRole.SHARD) {
        message.setTargetId(shardIndex);
        messages.add(message);
        return;
    }
    // log.info("Sending CS: {}", message.getClass().getCanonicalName());
    message.setTargetId(targetIndex);
    DirectBuffer buffer = message.asUnsafeBuffer();
    long result = publicationForShards.offer(buffer);
    if (result < 0)
        for (int i = 0; i < 5 && result < 0; i++) {
            try {
                // TODO: make this configurable
                Thread.sleep(1000);
            } catch (Exception e) {
            }
            result = publicationForShards.offer(buffer);
        }
    if (result < 0)
        throw new RuntimeException("Unable to send message over the wire. Error code: " + result);
}
Also used : DirectBuffer(org.agrona.DirectBuffer) ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException)

Example 55 with DirectBuffer

use of org.agrona.DirectBuffer in project nd4j by deeplearning4j.

the class RoutedTransport method sendCoordinationCommand.

/**
 * This method implements Shard -> Shards comms
 *
 * @param message
 */
@Override
protected void sendCoordinationCommand(VoidMessage message) {
    // log.info("Sending [{}] to all Shards...", message.getClass().getSimpleName());
    message.setOriginatorId(this.originatorId);
    // if we're the only shard - we just put message into the queue
    if (nodeRole == NodeRole.SHARD && voidConfiguration.getNumberOfShards() == 1) {
        try {
            messages.put(message);
            return;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    final DirectBuffer buffer = message.asUnsafeBuffer();
    // TODO: check which approach is faster, lambda, direct roll through list, or queue approach
    shards.parallelStream().forEach((rc) -> {
        RetransmissionHandler.TransmissionStatus res;
        long retr = 0;
        boolean delivered = false;
        long address = HashUtil.getLongHash(rc.getIp() + ":" + rc.getPort());
        if (originatorId == address) {
            // this is local delivery
            try {
                messages.put(message);
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
            return;
        }
        // log.info("Trying to send [{}] to {}", message.getClass().getSimpleName(), address);
        while (!delivered) {
            synchronized (rc.locker) {
                res = RetransmissionHandler.getTransmissionStatus(rc.getPublication().offer(buffer));
            }
            switch(res) {
                case NOT_CONNECTED:
                    {
                        if (!rc.getActivated().get()) {
                            retr++;
                            if (retr > 20)
                                throw new ND4JIllegalStateException("Can't connect to Shard: [" + rc.getPublication().channel() + "]");
                            try {
                                Thread.sleep(voidConfiguration.getRetransmitTimeout());
                            } catch (Exception e) {
                            }
                        } else {
                            throw new ND4JIllegalStateException("Shards reassignment is to be implemented yet");
                        }
                    }
                    break;
                case ADMIN_ACTION:
                case BACKPRESSURE:
                    {
                        try {
                            Thread.sleep(voidConfiguration.getRetransmitTimeout());
                        } catch (Exception e) {
                        }
                    }
                    break;
                case MESSAGE_SENT:
                    delivered = true;
                    rc.getActivated().set(true);
                    break;
            }
            if (!delivered)
                log.info("Attempting to resend message");
        }
    });
}
Also used : DirectBuffer(org.agrona.DirectBuffer) RetransmissionHandler(org.nd4j.parameterserver.distributed.logic.RetransmissionHandler) ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException) ND4JIllegalStateException(org.nd4j.linalg.exception.ND4JIllegalStateException)

Aggregations

DirectBuffer (org.agrona.DirectBuffer)116 Header (io.aeron.logbuffer.Header)21 UnsafeBuffer (org.agrona.concurrent.UnsafeBuffer)21 MutableDirectBuffer (org.agrona.MutableDirectBuffer)20 File (java.io.File)19 MediaDriver (io.aeron.driver.MediaDriver)18 CountersReader (org.agrona.concurrent.status.CountersReader)15 ThreadingMode (io.aeron.driver.ThreadingMode)12 FragmentHandler (io.aeron.logbuffer.FragmentHandler)12 MappedByteBuffer (java.nio.MappedByteBuffer)12 ClusteredService (io.aeron.cluster.service.ClusteredService)10 InterruptAfter (io.aeron.test.InterruptAfter)10 Test (org.junit.jupiter.api.Test)10 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 Publication (io.aeron.Publication)9 ClientSession (io.aeron.cluster.service.ClientSession)9 ClusteredServiceContainer (io.aeron.cluster.service.ClusteredServiceContainer)8 Assertions (org.junit.jupiter.api.Assertions)8 Mockito.mock (org.mockito.Mockito.mock)8