Search in sources :

Example 1 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)

Example 2 with Endpoint

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

the class RaftPerformanceTest method nextNode.

/**
 * Returns the next unique member identifier.
 *
 * @return The next unique member identifier.
 */
private Node nextNode(Node.Type type) {
    Endpoint endpoint = Endpoint.from("localhost", ++port);
    Node node = Node.builder(NodeId.from(String.valueOf(++nextId))).withType(type).withEndpoint(endpoint).build();
    endpointMap.put(node.id(), endpoint);
    return node;
}
Also used : Endpoint(io.atomix.messaging.Endpoint) Node(io.atomix.cluster.Node)

Example 3 with Endpoint

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

the class MessageDecoder method decode.

@Override
// suppress switch fall through warning
@SuppressWarnings("squid:S128")
protected void decode(ChannelHandlerContext context, ByteBuf buffer, List<Object> out) throws Exception {
    switch(currentState) {
        case READ_SENDER_IP:
            if (buffer.readableBytes() < BYTE_SIZE) {
                return;
            }
            buffer.markReaderIndex();
            int octetsLength = buffer.readByte();
            if (buffer.readableBytes() < octetsLength) {
                buffer.resetReaderIndex();
                return;
            }
            byte[] octets = new byte[octetsLength];
            buffer.readBytes(octets);
            senderIp = InetAddress.getByAddress(octets);
            currentState = DecoderState.READ_SENDER_PORT;
        case READ_SENDER_PORT:
            if (buffer.readableBytes() < INT_SIZE) {
                return;
            }
            senderPort = buffer.readInt();
            currentState = DecoderState.READ_TYPE;
        case READ_TYPE:
            if (buffer.readableBytes() < BYTE_SIZE) {
                return;
            }
            type = InternalMessage.Type.forId(buffer.readByte());
            currentState = DecoderState.READ_PREAMBLE;
        case READ_PREAMBLE:
            if (buffer.readableBytes() < INT_SIZE) {
                return;
            }
            preamble = buffer.readInt();
            currentState = DecoderState.READ_MESSAGE_ID;
        case READ_MESSAGE_ID:
            if (buffer.readableBytes() < LONG_SIZE) {
                return;
            }
            messageId = buffer.readLong();
            currentState = DecoderState.READ_CONTENT_LENGTH;
        case READ_CONTENT_LENGTH:
            if (buffer.readableBytes() < INT_SIZE) {
                return;
            }
            contentLength = buffer.readInt();
            currentState = DecoderState.READ_CONTENT;
        case READ_CONTENT:
            if (buffer.readableBytes() < contentLength) {
                return;
            }
            if (contentLength > 0) {
                // TODO: Perform a sanity check on the size before allocating
                content = new byte[contentLength];
                buffer.readBytes(content);
            } else {
                content = EMPTY_PAYLOAD;
            }
            switch(type) {
                case REQUEST:
                    currentState = DecoderState.READ_SUBJECT_LENGTH;
                    break;
                case REPLY:
                    currentState = DecoderState.READ_STATUS;
                    break;
                default:
                    checkState(false, "Must not be here");
            }
            break;
        default:
            break;
    }
    switch(type) {
        case REQUEST:
            switch(currentState) {
                case READ_SUBJECT_LENGTH:
                    if (buffer.readableBytes() < SHORT_SIZE) {
                        return;
                    }
                    subjectLength = buffer.readShort();
                    currentState = DecoderState.READ_SUBJECT;
                case READ_SUBJECT:
                    if (buffer.readableBytes() < subjectLength) {
                        return;
                    }
                    final String subject = readString(buffer, subjectLength, UTF_8);
                    InternalRequest message = new InternalRequest(preamble, messageId, new Endpoint(senderIp, senderPort), subject, content);
                    out.add(message);
                    currentState = DecoderState.READ_TYPE;
                    break;
                default:
                    break;
            }
            break;
        case REPLY:
            switch(currentState) {
                case READ_STATUS:
                    if (buffer.readableBytes() < BYTE_SIZE) {
                        return;
                    }
                    InternalReply.Status status = InternalReply.Status.forId(buffer.readByte());
                    InternalReply message = new InternalReply(preamble, messageId, content, status);
                    out.add(message);
                    currentState = DecoderState.READ_TYPE;
                    break;
                default:
                    break;
            }
            break;
        default:
            checkState(false, "Must not be here");
    }
}
Also used : Endpoint(io.atomix.messaging.Endpoint) Endpoint(io.atomix.messaging.Endpoint)

Example 4 with Endpoint

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

the class NettyMessagingServiceTest method setUp.

@Before
public void setUp() throws Exception {
    ep1 = new Endpoint(InetAddress.getByName("127.0.0.1"), findAvailablePort(5001));
    netty1 = (ManagedMessagingService) NettyMessagingService.builder().withEndpoint(ep1).build().start().join();
    ep2 = new Endpoint(InetAddress.getByName("127.0.0.1"), findAvailablePort(5003));
    netty2 = (ManagedMessagingService) NettyMessagingService.builder().withEndpoint(ep2).build().start().join();
    invalidEndPoint = new Endpoint(InetAddress.getByName(IP_STRING), 5003);
}
Also used : Endpoint(io.atomix.messaging.Endpoint) Before(org.junit.Before)

Example 5 with Endpoint

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

the class RaftFuzzTest method createServer.

/**
 * Creates a Raft server.
 */
private RaftServer createServer(RaftMember member) {
    RaftServerProtocol protocol;
    if (USE_NETTY) {
        try {
            Endpoint endpoint = new Endpoint(InetAddress.getLocalHost(), ++port);
            MessagingService messagingManager = NettyMessagingService.builder().withEndpoint(endpoint).build().start().join();
            messagingServices.add(messagingManager);
            endpointMap.put(member.nodeId(), endpoint);
            protocol = new RaftServerMessagingProtocol(messagingManager, protocolSerializer, endpointMap::get);
        } catch (UnknownHostException e) {
            throw new RuntimeException(e);
        }
    } else {
        protocol = protocolFactory.newServerProtocol(member.nodeId());
    }
    RaftServer.Builder builder = RaftServer.builder(member.nodeId()).withProtocol(protocol).withStorage(RaftStorage.builder().withStorageLevel(StorageLevel.DISK).withDirectory(new File(String.format("target/fuzz-logs/%s", member.nodeId()))).withSerializer(storageSerializer).withMaxSegmentSize(1024 * 1024).build()).addPrimitiveType(TestPrimitiveType.INSTANCE);
    RaftServer server = builder.build();
    servers.add(server);
    return server;
}
Also used : RaftServerProtocol(io.atomix.protocols.raft.protocol.RaftServerProtocol) RaftServerMessagingProtocol(io.atomix.protocols.raft.protocol.RaftServerMessagingProtocol) Endpoint(io.atomix.messaging.Endpoint) UnknownHostException(java.net.UnknownHostException) File(java.io.File) NettyMessagingService(io.atomix.messaging.impl.NettyMessagingService) MessagingService(io.atomix.messaging.MessagingService)

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