Search in sources :

Example 1 with IConnection

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

the class Context method term.

/**
     * terminate this context
     */
public synchronized void term() {
    clientScheduleService.stop();
    for (IConnection conn : connections.values()) {
        conn.close();
    }
    connections = null;
    //we need to release resources associated with client channel factory
    clientChannelFactory.releaseExternalResources();
}
Also used : IConnection(org.apache.storm.messaging.IConnection)

Example 2 with IConnection

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

the class Context method bind.

/**
     * establish a server with a binding port
     */
public synchronized IConnection bind(String storm_id, int port) {
    IConnection server = new Server(storm_conf, port);
    connections.put(key(storm_id, port), server);
    return server;
}
Also used : IConnection(org.apache.storm.messaging.IConnection)

Example 3 with IConnection

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

the class NettyTest method doTestLoad.

private void doTestLoad(Map<String, Object> stormConf) throws Exception {
    LOG.info("2 test load");
    String reqMessage = "0123456789abcdefghijklmnopqrstuvwxyz";
    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);
            /*
                 * This test sends a broadcast to all connected clients from the server, so we need to wait until the server has registered
                 * the client as connected before sending load metrics.
                 *
                 * It's not enough to wait until the client reports that the channel is open, because the server event loop may not have
                 * finished running channelActive for the new channel. If we send metrics too early, the server will broadcast to no one.
                 *
                 * By waiting for the response here, we ensure that the client will be registered at the server before we send load metrics.
                 */
            waitForNotNull(response);
            Map<Integer, Double> taskToLoad = new HashMap<>();
            taskToLoad.put(1, 0.0);
            taskToLoad.put(2, 1.0);
            server.sendLoadMetrics(taskToLoad);
            List<Integer> tasks = new ArrayList<>();
            tasks.add(1);
            tasks.add(2);
            Testing.whileTimeout(Testing.TEST_TIMEOUT_MS, () -> client.getLoad(tasks).isEmpty(), sleep());
            Map<Integer, Load> load = client.getLoad(tasks);
            assertThat(load.get(1).getBoltLoad(), is(0.0));
            assertThat(load.get(2).getBoltLoad(), is(1.0));
        }
    } finally {
        context.term();
    }
}
Also used : Load(org.apache.storm.grouping.Load) IContext(org.apache.storm.messaging.IContext) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AtomicReference(java.util.concurrent.atomic.AtomicReference) IConnection(org.apache.storm.messaging.IConnection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TaskMessage(org.apache.storm.messaging.TaskMessage)

Example 4 with IConnection

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

the class NettyTest method doTestServerAlwaysReconnects.

private void doTestServerAlwaysReconnects(Map<String, Object> stormConf) throws Exception {
    LOG.info("6. test server always reconnects");
    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)) {
            byte[] messageBytes = reqMessage.getBytes(StandardCharsets.UTF_8);
            send(client, taskId, messageBytes);
            try (IConnection server = context.bind(null, port, mkConnectionCallback(response::set), null)) {
                waitUntilReady(client, server);
                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 5 with IConnection

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

the class NettyTest method doTestBasic.

private void doTestBasic(Map<String, Object> stormConf) throws Exception {
    LOG.info("1. Should send and receive a basic message");
    String reqMessage = "0123456789abcdefghijklmnopqrstuvwxyz";
    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