Search in sources :

Example 1 with MessagingService

use of io.atomix.cluster.messaging.MessagingService in project atomix by atomix.

the class RaftPerformanceTest method createClient.

/**
 * Creates a Raft client.
 */
private RaftClient createClient() throws Exception {
    Member member = nextNode();
    RaftClientProtocol protocol;
    if (USE_NETTY) {
        MessagingService messagingService = new NettyMessagingService("test", member.address(), new MessagingConfig()).start().join();
        protocol = new RaftClientMessagingProtocol(messagingService, PROTOCOL_SERIALIZER, addressMap::get);
    } else {
        protocol = protocolFactory.newClientProtocol(member.id());
    }
    RaftClient client = RaftClient.builder().withMemberId(member.id()).withPartitionId(PartitionId.from("test", 1)).withProtocol(protocol).withThreadModel(ThreadModel.SHARED_THREAD_POOL).build();
    client.connect(members.stream().map(Member::id).collect(Collectors.toList())).join();
    clients.add(client);
    return client;
}
Also used : MessagingConfig(io.atomix.cluster.messaging.MessagingConfig) RaftClientProtocol(io.atomix.protocols.raft.protocol.RaftClientProtocol) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService) RaftClientMessagingProtocol(io.atomix.protocols.raft.test.protocol.RaftClientMessagingProtocol) Member(io.atomix.cluster.Member) RaftMember(io.atomix.protocols.raft.cluster.RaftMember) DefaultRaftMember(io.atomix.protocols.raft.cluster.impl.DefaultRaftMember) RaftClient(io.atomix.protocols.raft.RaftClient) MessagingService(io.atomix.cluster.messaging.MessagingService) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService) ManagedMessagingService(io.atomix.cluster.messaging.ManagedMessagingService)

Example 2 with MessagingService

use of io.atomix.cluster.messaging.MessagingService in project atomix by atomix.

the class RaftFuzzTest method createClient.

/**
 * Creates a Raft client.
 */
private RaftClient createClient() throws Exception {
    MemberId memberId = nextNodeId();
    RaftClientProtocol protocol;
    if (USE_NETTY) {
        Address address = Address.from(++port);
        MessagingService messagingManager = new NettyMessagingService("test", address, new MessagingConfig()).start().join();
        addressMap.put(memberId, address);
        protocol = new RaftClientMessagingProtocol(messagingManager, PROTOCOL_SERIALIZER, addressMap::get);
    } else {
        protocol = protocolFactory.newClientProtocol(memberId);
    }
    RaftClient client = RaftClient.builder().withMemberId(memberId).withProtocol(protocol).build();
    client.connect(members.stream().map(RaftMember::memberId).collect(Collectors.toList())).join();
    clients.add(client);
    return client;
}
Also used : MessagingConfig(io.atomix.cluster.messaging.MessagingConfig) MemberId(io.atomix.cluster.MemberId) RaftClientProtocol(io.atomix.protocols.raft.protocol.RaftClientProtocol) Address(io.atomix.utils.net.Address) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService) RaftClientMessagingProtocol(io.atomix.protocols.raft.test.protocol.RaftClientMessagingProtocol) RaftClient(io.atomix.protocols.raft.RaftClient) MessagingService(io.atomix.cluster.messaging.MessagingService) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService)

Example 3 with MessagingService

use of io.atomix.cluster.messaging.MessagingService in project zeppelin by apache.

the class ClusterManager method start.

public void start() {
    if (!zConf.isClusterMode()) {
        return;
    }
    // RaftClient Thread
    new Thread(new Runnable() {

        @Override
        public void run() {
            LOGGER.info("RaftClientThread run() >>>");
            int raftClientPort = 0;
            try {
                raftClientPort = RemoteInterpreterUtils.findRandomAvailablePortOnAllLocalInterfaces();
            } catch (IOException e) {
                LOGGER.error(e.getMessage());
            }
            MemberId memberId = MemberId.from(zeplServerHost + ":" + raftClientPort);
            Address address = Address.from(zeplServerHost, raftClientPort);
            raftAddressMap.put(memberId, address);
            MessagingService messagingManager = NettyMessagingService.builder().withAddress(address).build().start().join();
            RaftClientProtocol protocol = new RaftClientMessagingProtocol(messagingManager, protocolSerializer, raftAddressMap::get);
            raftClient = RaftClient.builder().withMemberId(memberId).withPartitionId(PartitionId.from("partition", 1)).withProtocol(protocol).build();
            raftClient.connect(clusterMemberIds).join();
            raftSessionClient = createProxy(raftClient);
            LOGGER.info("RaftClientThread run() <<<");
        }
    }).start();
    // Cluster Meta Consume Thread
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                while (getRunning().get()) {
                    ClusterMetaEntity metaEntity = clusterMetaQueue.peek();
                    if (null != metaEntity) {
                        // Determine whether the client is connected
                        int retry = 0;
                        while (!raftInitialized()) {
                            retry++;
                            if (0 == retry % 30) {
                                LOGGER.warn("Raft incomplete initialization! retry[{}]", retry);
                            }
                            Thread.sleep(100);
                        }
                        boolean success = false;
                        switch(metaEntity.getOperation()) {
                            case DELETE_OPERATION:
                                success = deleteClusterMeta(metaEntity);
                                break;
                            case PUT_OPERATION:
                                success = putClusterMeta(metaEntity);
                                break;
                        }
                        if (true == success) {
                            // The operation was successfully deleted
                            clusterMetaQueue.remove(metaEntity);
                            LOGGER.info("Cluster Meta Consume success! {}", metaEntity);
                        } else {
                            LOGGER.error("Cluster Meta Consume faild!");
                        }
                    } else {
                        Thread.sleep(100);
                    }
                }
            } catch (InterruptedException e) {
                LOGGER.error(e.getMessage());
            }
        }
    }).start();
}
Also used : RaftClientProtocol(io.atomix.protocols.raft.protocol.RaftClientProtocol) Address(io.atomix.utils.net.Address) InetAddress(java.net.InetAddress) IOException(java.io.IOException) MessagingService(io.atomix.cluster.messaging.MessagingService) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService) MemberId(io.atomix.cluster.MemberId) ClusterMetaEntity(org.apache.zeppelin.cluster.meta.ClusterMetaEntity) RaftClientMessagingProtocol(org.apache.zeppelin.cluster.protocol.RaftClientMessagingProtocol)

Example 4 with MessagingService

use of io.atomix.cluster.messaging.MessagingService in project atomix by atomix.

the class DefaultClusterEventServiceTest method testClusterEventService.

@Test
public void testClusterEventService() throws Exception {
    TestMessagingServiceFactory messagingServiceFactory = new TestMessagingServiceFactory();
    TestUnicastServiceFactory unicastServiceFactory = new TestUnicastServiceFactory();
    TestBroadcastServiceFactory broadcastServiceFactory = new TestBroadcastServiceFactory();
    Collection<Node> bootstrapLocations = buildBootstrapNodes(3);
    Member localMember1 = buildNode(1);
    MessagingService messagingService1 = messagingServiceFactory.newMessagingService(localMember1.address()).start().join();
    BootstrapService bootstrapService1 = new TestBootstrapService(messagingService1, unicastServiceFactory.newUnicastService(localMember1.address()).start().join(), broadcastServiceFactory.newBroadcastService().start().join());
    ManagedClusterMembershipService clusterService1 = new DefaultClusterMembershipService(localMember1, Version.from("1.0.0"), new DefaultNodeDiscoveryService(bootstrapService1, localMember1, new BootstrapDiscoveryProvider(bootstrapLocations)), bootstrapService1, new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig()));
    ClusterMembershipService clusterMembershipService1 = clusterService1.start().join();
    ManagedClusterEventService clusterEventingService1 = new DefaultClusterEventService(clusterMembershipService1, messagingService1);
    ClusterEventService eventService1 = clusterEventingService1.start().join();
    Member localMember2 = buildNode(2);
    MessagingService messagingService2 = messagingServiceFactory.newMessagingService(localMember2.address()).start().join();
    BootstrapService bootstrapService2 = new TestBootstrapService(messagingService2, unicastServiceFactory.newUnicastService(localMember2.address()).start().join(), broadcastServiceFactory.newBroadcastService().start().join());
    ManagedClusterMembershipService clusterService2 = new DefaultClusterMembershipService(localMember2, Version.from("1.0.0"), new DefaultNodeDiscoveryService(bootstrapService2, localMember2, new BootstrapDiscoveryProvider(bootstrapLocations)), bootstrapService2, new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig()));
    ClusterMembershipService clusterMembershipService2 = clusterService2.start().join();
    ManagedClusterEventService clusterEventingService2 = new DefaultClusterEventService(clusterMembershipService2, messagingService2);
    ClusterEventService eventService2 = clusterEventingService2.start().join();
    Member localMember3 = buildNode(3);
    MessagingService messagingService3 = messagingServiceFactory.newMessagingService(localMember3.address()).start().join();
    BootstrapService bootstrapService3 = new TestBootstrapService(messagingService3, unicastServiceFactory.newUnicastService(localMember1.address()).start().join(), broadcastServiceFactory.newBroadcastService().start().join());
    ManagedClusterMembershipService clusterService3 = new DefaultClusterMembershipService(localMember3, Version.from("1.0.0"), new DefaultNodeDiscoveryService(bootstrapService3, localMember3, new BootstrapDiscoveryProvider(bootstrapLocations)), bootstrapService3, new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig()));
    ClusterMembershipService clusterMembershipService3 = clusterService3.start().join();
    ManagedClusterEventService clusterEventingService3 = new DefaultClusterEventService(clusterMembershipService3, messagingService3);
    ClusterEventService eventService3 = clusterEventingService3.start().join();
    Thread.sleep(100);
    Set<Integer> events = new CopyOnWriteArraySet<>();
    eventService1.<String>subscribe("test1", SERIALIZER::decode, message -> {
        assertEquals("Hello world!", message);
        events.add(1);
    }, MoreExecutors.directExecutor()).join();
    eventService2.<String>subscribe("test1", SERIALIZER::decode, message -> {
        assertEquals("Hello world!", message);
        events.add(2);
    }, MoreExecutors.directExecutor()).join();
    eventService2.<String>subscribe("test1", SERIALIZER::decode, message -> {
        assertEquals("Hello world!", message);
        events.add(3);
    }, MoreExecutors.directExecutor()).join();
    eventService3.broadcast("test1", "Hello world!", SERIALIZER::encode);
    Thread.sleep(100);
    assertEquals(3, events.size());
    events.clear();
    eventService3.unicast("test1", "Hello world!");
    Thread.sleep(100);
    assertEquals(1, events.size());
    assertTrue(events.contains(3));
    events.clear();
    eventService3.unicast("test1", "Hello world!");
    Thread.sleep(100);
    assertEquals(1, events.size());
    assertTrue(events.contains(1));
    events.clear();
    eventService3.unicast("test1", "Hello world!");
    Thread.sleep(100);
    assertEquals(1, events.size());
    assertTrue(events.contains(2));
    events.clear();
    eventService3.unicast("test1", "Hello world!");
    Thread.sleep(100);
    assertEquals(1, events.size());
    assertTrue(events.contains(3));
    events.clear();
    eventService1.<String, String>subscribe("test2", SERIALIZER::decode, message -> {
        events.add(1);
        return message;
    }, SERIALIZER::encode, MoreExecutors.directExecutor()).join();
    eventService2.<String, String>subscribe("test2", SERIALIZER::decode, message -> {
        events.add(2);
        return message;
    }, SERIALIZER::encode, MoreExecutors.directExecutor()).join();
    assertEquals("Hello world!", eventService3.send("test2", "Hello world!").join());
    assertEquals(1, events.size());
    assertTrue(events.contains(1));
    events.clear();
    assertEquals("Hello world!", eventService3.send("test2", "Hello world!").join());
    assertEquals(1, events.size());
    assertTrue(events.contains(2));
    events.clear();
    assertEquals("Hello world!", eventService3.send("test2", "Hello world!").join());
    assertEquals(1, events.size());
    assertTrue(events.contains(1));
    CompletableFuture.allOf(new CompletableFuture[] { clusterEventingService1.stop(), clusterEventingService2.stop(), clusterEventingService3.stop() }).join();
    CompletableFuture.allOf(new CompletableFuture[] { clusterService1.stop(), clusterService2.stop(), clusterService3.stop() }).join();
}
Also used : IntStream(java.util.stream.IntStream) Node(io.atomix.cluster.Node) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) Address(io.atomix.utils.net.Address) CompletableFuture(java.util.concurrent.CompletableFuture) ClusterEventService(io.atomix.cluster.messaging.ClusterEventService) Namespaces(io.atomix.utils.serializer.Namespaces) ClusterMembershipService(io.atomix.cluster.ClusterMembershipService) DefaultClusterMembershipService(io.atomix.cluster.impl.DefaultClusterMembershipService) MessagingService(io.atomix.cluster.messaging.MessagingService) Collection(java.util.Collection) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) DefaultNodeDiscoveryService(io.atomix.cluster.impl.DefaultNodeDiscoveryService) Test(org.junit.Test) Version(io.atomix.utils.Version) ManagedClusterEventService(io.atomix.cluster.messaging.ManagedClusterEventService) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) Member(io.atomix.cluster.Member) Collectors(java.util.stream.Collectors) BootstrapDiscoveryProvider(io.atomix.cluster.discovery.BootstrapDiscoveryProvider) HeartbeatMembershipProtocolConfig(io.atomix.cluster.protocol.HeartbeatMembershipProtocolConfig) ManagedClusterMembershipService(io.atomix.cluster.ManagedClusterMembershipService) TestBootstrapService(io.atomix.cluster.TestBootstrapService) HeartbeatMembershipProtocol(io.atomix.cluster.protocol.HeartbeatMembershipProtocol) Serializer(io.atomix.utils.serializer.Serializer) Assert.assertEquals(org.junit.Assert.assertEquals) BootstrapService(io.atomix.cluster.BootstrapService) DefaultNodeDiscoveryService(io.atomix.cluster.impl.DefaultNodeDiscoveryService) Node(io.atomix.cluster.Node) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) ManagedClusterMembershipService(io.atomix.cluster.ManagedClusterMembershipService) HeartbeatMembershipProtocolConfig(io.atomix.cluster.protocol.HeartbeatMembershipProtocolConfig) ManagedClusterEventService(io.atomix.cluster.messaging.ManagedClusterEventService) CompletableFuture(java.util.concurrent.CompletableFuture) ClusterMembershipService(io.atomix.cluster.ClusterMembershipService) DefaultClusterMembershipService(io.atomix.cluster.impl.DefaultClusterMembershipService) ManagedClusterMembershipService(io.atomix.cluster.ManagedClusterMembershipService) TestBootstrapService(io.atomix.cluster.TestBootstrapService) DefaultClusterMembershipService(io.atomix.cluster.impl.DefaultClusterMembershipService) Member(io.atomix.cluster.Member) HeartbeatMembershipProtocol(io.atomix.cluster.protocol.HeartbeatMembershipProtocol) TestBootstrapService(io.atomix.cluster.TestBootstrapService) BootstrapService(io.atomix.cluster.BootstrapService) BootstrapDiscoveryProvider(io.atomix.cluster.discovery.BootstrapDiscoveryProvider) MessagingService(io.atomix.cluster.messaging.MessagingService) ClusterEventService(io.atomix.cluster.messaging.ClusterEventService) ManagedClusterEventService(io.atomix.cluster.messaging.ManagedClusterEventService) Test(org.junit.Test)

Example 5 with MessagingService

use of io.atomix.cluster.messaging.MessagingService in project atomix by atomix.

the class RaftFuzzTest method createServer.

/**
 * Creates a Raft server.
 */
private RaftServer createServer(RaftMember member) {
    RaftServerProtocol protocol;
    if (USE_NETTY) {
        Address address = Address.from(++port);
        MessagingService messagingManager = new NettyMessagingService("test", address, new MessagingConfig()).start().join();
        messagingServices.add(messagingManager);
        addressMap.put(member.memberId(), address);
        protocol = new RaftServerMessagingProtocol(messagingManager, PROTOCOL_SERIALIZER, addressMap::get);
    } else {
        protocol = protocolFactory.newServerProtocol(member.memberId());
    }
    RaftServer.Builder builder = RaftServer.builder(member.memberId()).withProtocol(protocol).withStorage(RaftStorage.builder().withStorageLevel(StorageLevel.DISK).withDirectory(new File(String.format("target/fuzz-logs/%s", member.memberId()))).withNamespace(STORAGE_NAMESPACE).withMaxSegmentSize(1024 * 1024).build());
    RaftServer server = builder.build();
    servers.add(server);
    return server;
}
Also used : MessagingConfig(io.atomix.cluster.messaging.MessagingConfig) RaftServerProtocol(io.atomix.protocols.raft.protocol.RaftServerProtocol) RaftServerMessagingProtocol(io.atomix.protocols.raft.test.protocol.RaftServerMessagingProtocol) Address(io.atomix.utils.net.Address) RaftServer(io.atomix.protocols.raft.RaftServer) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService) File(java.io.File) MessagingService(io.atomix.cluster.messaging.MessagingService) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService)

Aggregations

MessagingService (io.atomix.cluster.messaging.MessagingService)5 NettyMessagingService (io.atomix.cluster.messaging.impl.NettyMessagingService)4 Address (io.atomix.utils.net.Address)4 MessagingConfig (io.atomix.cluster.messaging.MessagingConfig)3 RaftClientProtocol (io.atomix.protocols.raft.protocol.RaftClientProtocol)3 Member (io.atomix.cluster.Member)2 MemberId (io.atomix.cluster.MemberId)2 RaftClient (io.atomix.protocols.raft.RaftClient)2 RaftClientMessagingProtocol (io.atomix.protocols.raft.test.protocol.RaftClientMessagingProtocol)2 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 BootstrapService (io.atomix.cluster.BootstrapService)1 ClusterMembershipService (io.atomix.cluster.ClusterMembershipService)1 ManagedClusterMembershipService (io.atomix.cluster.ManagedClusterMembershipService)1 Node (io.atomix.cluster.Node)1 TestBootstrapService (io.atomix.cluster.TestBootstrapService)1 BootstrapDiscoveryProvider (io.atomix.cluster.discovery.BootstrapDiscoveryProvider)1 DefaultClusterMembershipService (io.atomix.cluster.impl.DefaultClusterMembershipService)1 DefaultNodeDiscoveryService (io.atomix.cluster.impl.DefaultNodeDiscoveryService)1 ClusterEventService (io.atomix.cluster.messaging.ClusterEventService)1 ManagedClusterEventService (io.atomix.cluster.messaging.ManagedClusterEventService)1