Search in sources :

Example 11 with IConnection

use of org.apache.storm.messaging.IConnection in project storm by apache.

the class NettyTest method doTestBatch.

private void doTestBatch(Map<String, Object> stormConf) throws Exception {
    int numMessages = 100_000;
    LOG.info("Should send and receive many messages (testing with " + numMessages + " messages)");
    ArrayList<TaskMessage> responses = new ArrayList<>();
    AtomicInteger received = new AtomicInteger();
    IContext context = TransportFactory.makeContext(stormConf, null);
    try {
        try (IConnection server = context.bind(null, 0, mkConnectionCallback((message) -> {
            responses.add(message);
            received.incrementAndGet();
        }), null);
            IConnection client = context.connect(null, "localhost", server.getPort(), remoteBpStatus)) {
            waitUntilReady(client, server);
            IntStream.range(1, numMessages).forEach(i -> send(client, taskId, String.valueOf(i).getBytes(StandardCharsets.UTF_8)));
            Testing.whileTimeout(Testing.TEST_TIMEOUT_MS, () -> responses.size() < numMessages - 1, () -> {
                LOG.info("{} of {} received", responses.size(), numMessages - 1);
                sleep().run();
            });
            IntStream.range(1, numMessages).forEach(i -> {
                assertThat(new String(responses.get(i - 1).message(), StandardCharsets.UTF_8), is(String.valueOf(i)));
            });
        }
    } finally {
        context.term();
    }
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) Testing(org.apache.storm.Testing) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) TransportFactory(org.apache.storm.messaging.TransportFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Assert.assertThat(org.junit.Assert.assertThat) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) Matchers.lessThan(org.hamcrest.Matchers.lessThan) Logger(org.slf4j.Logger) IConnection(org.apache.storm.messaging.IConnection) Test(org.junit.Test) TaskMessage(org.apache.storm.messaging.TaskMessage) WorkerState(org.apache.storm.daemon.worker.WorkerState) Utils(org.apache.storm.utils.Utils) Load(org.apache.storm.grouping.Load) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) List(java.util.List) IConnectionCallback(org.apache.storm.messaging.IConnectionCallback) IContext(org.apache.storm.messaging.IContext) Config(org.apache.storm.Config) Collections(java.util.Collections) IContext(org.apache.storm.messaging.IContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) IConnection(org.apache.storm.messaging.IConnection) TaskMessage(org.apache.storm.messaging.TaskMessage)

Example 12 with IConnection

use of org.apache.storm.messaging.IConnection in project storm by apache.

the class NettyTest method doTestServerDelayed.

private void doTestServerDelayed(Map<String, Object> stormConf) throws Exception {
    LOG.info("4. test server delayed");
    String reqMessage = "0123456789abcdefghijklmnopqrstuvwxyz";
    IContext context = TransportFactory.makeContext(stormConf, null);
    try {
        AtomicReference<TaskMessage> response = new AtomicReference<>();
        int port = Utils.getAvailablePort(6700);
        try (IConnection client = context.connect(null, "localhost", port, remoteBpStatus)) {
            AtomicReference<IConnection> server = new AtomicReference<>();
            try {
                CompletableFuture<?> serverStart = CompletableFuture.runAsync(() -> {
                    try {
                        Thread.sleep(100);
                        server.set(context.bind(null, port, mkConnectionCallback(response::set), null));
                        waitUntilReady(client, server.get());
                    } catch (Exception e) {
                        throw Utils.wrapInRuntime(e);
                    }
                });
                serverStart.get(Testing.TEST_TIMEOUT_MS, TimeUnit.MILLISECONDS);
                byte[] messageBytes = reqMessage.getBytes(StandardCharsets.UTF_8);
                send(client, taskId, messageBytes);
                waitForNotNull(response);
                TaskMessage responseMessage = response.get();
                assertThat(responseMessage.task(), is(taskId));
                assertThat(responseMessage.message(), is(messageBytes));
            } finally {
                if (server.get() != null) {
                    server.get().close();
                }
            }
        }
    } finally {
        context.term();
    }
}
Also used : IContext(org.apache.storm.messaging.IContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) IConnection(org.apache.storm.messaging.IConnection) TaskMessage(org.apache.storm.messaging.TaskMessage)

Example 13 with IConnection

use of org.apache.storm.messaging.IConnection in project storm by apache.

the class NettyTest method connectToFixedPort.

private void connectToFixedPort(Map<String, Object> stormConf, int port) throws Exception {
    LOG.info("7. Should be able to rebind to a port quickly");
    String reqMessage = "0123456789abcdefghijklmnopqrstuvwxyz";
    IContext context = TransportFactory.makeContext(stormConf, null);
    try {
        AtomicReference<TaskMessage> response = new AtomicReference<>();
        try (IConnection server = context.bind(null, port, mkConnectionCallback(response::set), null);
            IConnection client = context.connect(null, "localhost", server.getPort(), remoteBpStatus)) {
            waitUntilReady(client, server);
            byte[] messageBytes = reqMessage.getBytes(StandardCharsets.UTF_8);
            send(client, taskId, messageBytes);
            waitForNotNull(response);
            TaskMessage responseMessage = response.get();
            assertThat(responseMessage.task(), is(taskId));
            assertThat(responseMessage.message(), is(messageBytes));
        }
    } finally {
        context.term();
    }
}
Also used : IContext(org.apache.storm.messaging.IContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) IConnection(org.apache.storm.messaging.IConnection) TaskMessage(org.apache.storm.messaging.TaskMessage)

Example 14 with IConnection

use of org.apache.storm.messaging.IConnection in project storm by apache.

the class NettyTest method doTestLargeMessage.

private void doTestLargeMessage(Map<String, Object> stormConf) throws Exception {
    LOG.info("3 Should send and receive a large message");
    String reqMessage = StringUtils.repeat("c", 2_048_000);
    IContext context = TransportFactory.makeContext(stormConf, null);
    try {
        AtomicReference<TaskMessage> response = new AtomicReference<>();
        try (IConnection server = context.bind(null, 0, mkConnectionCallback(response::set), null);
            IConnection client = context.connect(null, "localhost", server.getPort(), remoteBpStatus)) {
            waitUntilReady(client, server);
            byte[] messageBytes = reqMessage.getBytes(StandardCharsets.UTF_8);
            send(client, taskId, messageBytes);
            waitForNotNull(response);
            TaskMessage responseMessage = response.get();
            assertThat(responseMessage.task(), is(taskId));
            assertThat(responseMessage.message(), is(messageBytes));
        }
    } finally {
        context.term();
    }
}
Also used : IContext(org.apache.storm.messaging.IContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) IConnection(org.apache.storm.messaging.IConnection) TaskMessage(org.apache.storm.messaging.TaskMessage)

Aggregations

IConnection (org.apache.storm.messaging.IConnection)14 TaskMessage (org.apache.storm.messaging.TaskMessage)8 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 IContext (org.apache.storm.messaging.IContext)7 HashMap (java.util.HashMap)4 Map (java.util.Map)3 NodeInfo (org.apache.storm.generated.NodeInfo)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Assignment (org.apache.storm.generated.Assignment)2 Load (org.apache.storm.grouping.Load)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 UnknownHostException (java.net.UnknownHostException)1 StandardCharsets (java.nio.charset.StandardCharsets)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 List (java.util.List)1 CompletableFuture (java.util.concurrent.CompletableFuture)1