Search in sources :

Example 6 with Endpoint

use of io.atomix.messaging.Endpoint in project atomix by atomix.

the class RaftFuzzTest method createClient.

/**
 * Creates a Raft client.
 */
private RaftClient createClient() throws Exception {
    NodeId nodeId = nextNodeId();
    RaftClientProtocol protocol;
    if (USE_NETTY) {
        Endpoint endpoint = new Endpoint(InetAddress.getLocalHost(), ++port);
        MessagingService messagingManager = NettyMessagingService.builder().withEndpoint(endpoint).build().start().join();
        endpointMap.put(nodeId, endpoint);
        protocol = new RaftClientMessagingProtocol(messagingManager, protocolSerializer, endpointMap::get);
    } else {
        protocol = protocolFactory.newClientProtocol(nodeId);
    }
    RaftClient client = RaftClient.builder().withNodeId(nodeId).withProtocol(protocol).build();
    client.connect(members.stream().map(RaftMember::nodeId).collect(Collectors.toList())).join();
    clients.add(client);
    return client;
}
Also used : RaftClientProtocol(io.atomix.protocols.raft.protocol.RaftClientProtocol) Endpoint(io.atomix.messaging.Endpoint) NodeId(io.atomix.cluster.NodeId) RaftClientMessagingProtocol(io.atomix.protocols.raft.protocol.RaftClientMessagingProtocol) NettyMessagingService(io.atomix.messaging.impl.NettyMessagingService) MessagingService(io.atomix.messaging.MessagingService)

Example 7 with Endpoint

use of io.atomix.messaging.Endpoint in project atomix by atomix.

the class NettyMessagingService method getChannel.

private CompletableFuture<Channel> getChannel(Endpoint endpoint, String messageType) {
    List<CompletableFuture<Channel>> channelPool = getChannelPool(endpoint);
    int offset = getChannelOffset(messageType);
    CompletableFuture<Channel> channelFuture = channelPool.get(offset);
    if (channelFuture == null || channelFuture.isCompletedExceptionally()) {
        synchronized (channelPool) {
            channelFuture = channelPool.get(offset);
            if (channelFuture == null || channelFuture.isCompletedExceptionally()) {
                channelFuture = openChannel(endpoint);
                channelPool.set(offset, channelFuture);
            }
        }
    }
    final CompletableFuture<Channel> future = new CompletableFuture<>();
    final CompletableFuture<Channel> finalFuture = channelFuture;
    finalFuture.whenComplete((channel, error) -> {
        if (error == null) {
            if (!channel.isActive()) {
                final CompletableFuture<Channel> currentFuture;
                synchronized (channelPool) {
                    currentFuture = channelPool.get(offset);
                    if (currentFuture == finalFuture) {
                        channelPool.set(offset, null);
                    }
                }
                final ClientConnection connection = clientConnections.remove(channel);
                if (connection != null) {
                    connection.close();
                }
                if (currentFuture == finalFuture) {
                    getChannel(endpoint, messageType).whenComplete((recursiveResult, recursiveError) -> {
                        if (recursiveError == null) {
                            future.complete(recursiveResult);
                        } else {
                            future.completeExceptionally(recursiveError);
                        }
                    });
                } else {
                    currentFuture.whenComplete((recursiveResult, recursiveError) -> {
                        if (recursiveError == null) {
                            future.complete(recursiveResult);
                        } else {
                            future.completeExceptionally(recursiveError);
                        }
                    });
                }
            } else {
                future.complete(channel);
            }
        } else {
            future.completeExceptionally(error);
        }
    });
    return future;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) ServerChannel(io.netty.channel.ServerChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EpollSocketChannel(io.netty.channel.epoll.EpollSocketChannel) EpollServerSocketChannel(io.netty.channel.epoll.EpollServerSocketChannel) Channel(io.netty.channel.Channel) Endpoint(io.atomix.messaging.Endpoint)

Example 8 with Endpoint

use of io.atomix.messaging.Endpoint in project atomix by atomix.

the class NettyMessagingServiceTest method testSendAndReceive.

@Test
// FIXME disabled on 9/29/16 due to random failures
@Ignore
public void testSendAndReceive() {
    String subject = nextSubject();
    AtomicBoolean handlerInvoked = new AtomicBoolean(false);
    AtomicReference<byte[]> request = new AtomicReference<>();
    AtomicReference<Endpoint> sender = new AtomicReference<>();
    BiFunction<Endpoint, byte[], byte[]> handler = (ep, data) -> {
        handlerInvoked.set(true);
        sender.set(ep);
        request.set(data);
        return "hello there".getBytes();
    };
    netty2.registerHandler(subject, handler, MoreExecutors.directExecutor());
    CompletableFuture<byte[]> response = netty1.sendAndReceive(ep2, subject, "hello world".getBytes());
    assertTrue(Arrays.equals("hello there".getBytes(), response.join()));
    assertTrue(handlerInvoked.get());
    assertTrue(Arrays.equals(request.get(), "hello world".getBytes()));
    assertEquals(ep1, sender.get());
}
Also used : MoreExecutors(com.google.common.util.concurrent.MoreExecutors) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) InetAddress(java.net.InetAddress) ServerSocket(java.net.ServerSocket) After(org.junit.After) ConnectException(java.net.ConnectException) Assert.fail(org.junit.Assert.fail) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) ManagedMessagingService(io.atomix.messaging.ManagedMessagingService) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Endpoint(io.atomix.messaging.Endpoint) Test(org.junit.Test) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) UUID(java.util.UUID) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) Assert.assertEquals(org.junit.Assert.assertEquals) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Endpoint(io.atomix.messaging.Endpoint) AtomicReference(java.util.concurrent.atomic.AtomicReference) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 9 with Endpoint

use of io.atomix.messaging.Endpoint in project atomix by atomix.

the class NettyMessagingServiceTest method testSendAndReceiveWithExecutor.

/*
   * Supplies executors when registering a handler and calling sendAndReceive and verifies the request handling
   * and response completion occurs on the expected thread.
   */
@Test
@Ignore
public void testSendAndReceiveWithExecutor() {
    String subject = nextSubject();
    ExecutorService completionExecutor = Executors.newSingleThreadExecutor(r -> new Thread(r, "completion-thread"));
    ExecutorService handlerExecutor = Executors.newSingleThreadExecutor(r -> new Thread(r, "handler-thread"));
    AtomicReference<String> handlerThreadName = new AtomicReference<>();
    AtomicReference<String> completionThreadName = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    BiFunction<Endpoint, byte[], byte[]> handler = (ep, data) -> {
        handlerThreadName.set(Thread.currentThread().getName());
        try {
            latch.await();
        } catch (InterruptedException e1) {
            Thread.currentThread().interrupt();
            fail("InterruptedException");
        }
        return "hello there".getBytes();
    };
    netty2.registerHandler(subject, handler, handlerExecutor);
    CompletableFuture<byte[]> response = netty1.sendAndReceive(ep2, subject, "hello world".getBytes(), completionExecutor);
    response.whenComplete((r, e) -> {
        completionThreadName.set(Thread.currentThread().getName());
    });
    latch.countDown();
    // Verify that the message was request handling and response completion happens on the correct thread.
    assertTrue(Arrays.equals("hello there".getBytes(), response.join()));
    assertEquals("completion-thread", completionThreadName.get());
    assertEquals("handler-thread", handlerThreadName.get());
}
Also used : MoreExecutors(com.google.common.util.concurrent.MoreExecutors) Arrays(java.util.Arrays) BiFunction(java.util.function.BiFunction) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) InetAddress(java.net.InetAddress) ServerSocket(java.net.ServerSocket) After(org.junit.After) ConnectException(java.net.ConnectException) Assert.fail(org.junit.Assert.fail) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) ManagedMessagingService(io.atomix.messaging.ManagedMessagingService) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Endpoint(io.atomix.messaging.Endpoint) Test(org.junit.Test) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) UUID(java.util.UUID) Executors(java.util.concurrent.Executors) CountDownLatch(java.util.concurrent.CountDownLatch) Assert.assertNull(org.junit.Assert.assertNull) Ignore(org.junit.Ignore) Assert.assertEquals(org.junit.Assert.assertEquals) Endpoint(io.atomix.messaging.Endpoint) ExecutorService(java.util.concurrent.ExecutorService) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 10 with Endpoint

use of io.atomix.messaging.Endpoint in project atomix by atomix.

the class TestMessagingService method getHandler.

/**
 * Returns the given handler for the given endpoint.
 */
private BiFunction<Endpoint, byte[], CompletableFuture<byte[]>> getHandler(Endpoint endpoint, String type) {
    TestMessagingService service = getService(endpoint);
    if (service == null) {
        return (e, p) -> Futures.exceptionalFuture(new NoRemoteHandler());
    }
    BiFunction<Endpoint, byte[], CompletableFuture<byte[]>> handler = service.handlers.get(checkNotNull(type));
    if (handler == null) {
        return (e, p) -> Futures.exceptionalFuture(new NoRemoteHandler());
    }
    return handler;
}
Also used : ManagedMessagingService(io.atomix.messaging.ManagedMessagingService) ComposableFuture(io.atomix.utils.concurrent.ComposableFuture) Executor(java.util.concurrent.Executor) BiFunction(java.util.function.BiFunction) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Endpoint(io.atomix.messaging.Endpoint) CompletableFuture(java.util.concurrent.CompletableFuture) NoRemoteHandler(io.atomix.messaging.MessagingException.NoRemoteHandler) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) MessagingService(io.atomix.messaging.MessagingService) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) Futures(io.atomix.utils.concurrent.Futures) CompletableFuture(java.util.concurrent.CompletableFuture) Endpoint(io.atomix.messaging.Endpoint) NoRemoteHandler(io.atomix.messaging.MessagingException.NoRemoteHandler)

Aggregations

Endpoint (io.atomix.messaging.Endpoint)14 MessagingService (io.atomix.messaging.MessagingService)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)5 Node (io.atomix.cluster.Node)4 ManagedMessagingService (io.atomix.messaging.ManagedMessagingService)4 BiFunction (java.util.function.BiFunction)4 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)3 InetAddress (java.net.InetAddress)3 UnknownHostException (java.net.UnknownHostException)3 Map (java.util.Map)3 ExecutorService (java.util.concurrent.ExecutorService)3 Executors (java.util.concurrent.Executors)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 Assert.assertEquals (org.junit.Assert.assertEquals)3 Assert.assertTrue (org.junit.Assert.assertTrue)3 Before (org.junit.Before)3 Test (org.junit.Test)3 Uninterruptibles (com.google.common.util.concurrent.Uninterruptibles)2 ClusterMetadata (io.atomix.cluster.ClusterMetadata)2