Search in sources :

Example 1 with HeartbeatMembershipProtocolConfig

use of io.atomix.cluster.protocol.HeartbeatMembershipProtocolConfig in project atomix by atomix.

the class DefaultClusterMembershipServiceTest method testClusterService.

@Test
public void testClusterService() throws Exception {
    TestMessagingServiceFactory messagingServiceFactory = new TestMessagingServiceFactory();
    TestUnicastServiceFactory unicastServiceFactory = new TestUnicastServiceFactory();
    TestBroadcastServiceFactory broadcastServiceFactory = new TestBroadcastServiceFactory();
    Collection<Node> bootstrapLocations = buildBootstrapNodes(3);
    Member localMember1 = buildMember(1);
    BootstrapService bootstrapService1 = new TestBootstrapService(messagingServiceFactory.newMessagingService(localMember1.address()).start().join(), 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().setFailureTimeout(Duration.ofSeconds(2))));
    Member localMember2 = buildMember(2);
    BootstrapService bootstrapService2 = new TestBootstrapService(messagingServiceFactory.newMessagingService(localMember2.address()).start().join(), 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().setFailureTimeout(Duration.ofSeconds(2))));
    Member localMember3 = buildMember(3);
    BootstrapService bootstrapService3 = new TestBootstrapService(messagingServiceFactory.newMessagingService(localMember3.address()).start().join(), unicastServiceFactory.newUnicastService(localMember3.address()).start().join(), broadcastServiceFactory.newBroadcastService().start().join());
    ManagedClusterMembershipService clusterService3 = new DefaultClusterMembershipService(localMember3, Version.from("1.0.1"), new DefaultNodeDiscoveryService(bootstrapService3, localMember3, new BootstrapDiscoveryProvider(bootstrapLocations)), bootstrapService3, new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig().setFailureTimeout(Duration.ofSeconds(2))));
    assertNull(clusterService1.getMember(MemberId.from("1")));
    assertNull(clusterService1.getMember(MemberId.from("2")));
    assertNull(clusterService1.getMember(MemberId.from("3")));
    CompletableFuture.allOf(new CompletableFuture[] { clusterService1.start(), clusterService2.start(), clusterService3.start() }).join();
    Thread.sleep(5000);
    assertEquals(3, clusterService1.getMembers().size());
    assertEquals(3, clusterService2.getMembers().size());
    assertEquals(3, clusterService3.getMembers().size());
    assertTrue(clusterService1.getLocalMember().isActive());
    assertTrue(clusterService1.getMember(MemberId.from("1")).isActive());
    assertTrue(clusterService1.getMember(MemberId.from("2")).isActive());
    assertTrue(clusterService1.getMember(MemberId.from("3")).isActive());
    assertEquals("1.0.0", clusterService1.getMember("1").version().toString());
    assertEquals("1.0.0", clusterService1.getMember("2").version().toString());
    assertEquals("1.0.1", clusterService1.getMember("3").version().toString());
    Member anonymousMember = buildMember(4);
    BootstrapService ephemeralBootstrapService = new TestBootstrapService(messagingServiceFactory.newMessagingService(anonymousMember.address()).start().join(), unicastServiceFactory.newUnicastService(anonymousMember.address()).start().join(), broadcastServiceFactory.newBroadcastService().start().join());
    ManagedClusterMembershipService ephemeralClusterService = new DefaultClusterMembershipService(anonymousMember, Version.from("1.1.0"), new DefaultNodeDiscoveryService(ephemeralBootstrapService, anonymousMember, new BootstrapDiscoveryProvider(bootstrapLocations)), ephemeralBootstrapService, new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig().setFailureTimeout(Duration.ofSeconds(2))));
    assertFalse(ephemeralClusterService.getLocalMember().isActive());
    assertNull(ephemeralClusterService.getMember(MemberId.from("1")));
    assertNull(ephemeralClusterService.getMember(MemberId.from("2")));
    assertNull(ephemeralClusterService.getMember(MemberId.from("3")));
    assertNull(ephemeralClusterService.getMember(MemberId.from("4")));
    assertNull(ephemeralClusterService.getMember(MemberId.from("5")));
    ephemeralClusterService.start().join();
    Thread.sleep(1000);
    assertEquals(4, clusterService1.getMembers().size());
    assertEquals(4, clusterService2.getMembers().size());
    assertEquals(4, clusterService3.getMembers().size());
    assertEquals(4, ephemeralClusterService.getMembers().size());
    assertEquals("1.0.0", clusterService1.getMember("1").version().toString());
    assertEquals("1.0.0", clusterService1.getMember("2").version().toString());
    assertEquals("1.0.1", clusterService1.getMember("3").version().toString());
    assertEquals("1.1.0", clusterService1.getMember("4").version().toString());
    clusterService1.stop().join();
    Thread.sleep(5000);
    assertEquals(3, clusterService2.getMembers().size());
    assertNull(clusterService2.getMember(MemberId.from("1")));
    assertTrue(clusterService2.getMember(MemberId.from("2")).isActive());
    assertTrue(clusterService2.getMember(MemberId.from("3")).isActive());
    assertTrue(clusterService2.getMember(MemberId.from("4")).isActive());
    ephemeralClusterService.stop().join();
    Thread.sleep(5000);
    assertEquals(2, clusterService2.getMembers().size());
    assertNull(clusterService2.getMember(MemberId.from("1")));
    assertTrue(clusterService2.getMember(MemberId.from("2")).isActive());
    assertTrue(clusterService2.getMember(MemberId.from("3")).isActive());
    assertNull(clusterService2.getMember(MemberId.from("4")));
    Thread.sleep(2500);
    assertEquals(2, clusterService2.getMembers().size());
    assertNull(clusterService2.getMember(MemberId.from("1")));
    assertTrue(clusterService2.getMember(MemberId.from("2")).isActive());
    assertTrue(clusterService2.getMember(MemberId.from("3")).isActive());
    assertNull(clusterService2.getMember(MemberId.from("4")));
    TestClusterMembershipEventListener eventListener = new TestClusterMembershipEventListener();
    clusterService2.addListener(eventListener);
    ClusterMembershipEvent event;
    clusterService3.getLocalMember().properties().put("foo", "bar");
    event = eventListener.nextEvent();
    assertEquals(ClusterMembershipEvent.Type.METADATA_CHANGED, event.type());
    assertEquals("bar", event.subject().properties().get("foo"));
    clusterService3.getLocalMember().properties().put("foo", "baz");
    event = eventListener.nextEvent();
    assertEquals(ClusterMembershipEvent.Type.METADATA_CHANGED, event.type());
    assertEquals("baz", event.subject().properties().get("foo"));
    CompletableFuture.allOf(new CompletableFuture[] { clusterService1.stop(), clusterService2.stop(), clusterService3.stop() }).join();
}
Also used : TestUnicastServiceFactory(io.atomix.cluster.messaging.impl.TestUnicastServiceFactory) BootstrapDiscoveryProvider(io.atomix.cluster.discovery.BootstrapDiscoveryProvider) Node(io.atomix.cluster.Node) ManagedClusterMembershipService(io.atomix.cluster.ManagedClusterMembershipService) HeartbeatMembershipProtocolConfig(io.atomix.cluster.protocol.HeartbeatMembershipProtocolConfig) TestMessagingServiceFactory(io.atomix.cluster.messaging.impl.TestMessagingServiceFactory) CompletableFuture(java.util.concurrent.CompletableFuture) ClusterMembershipEvent(io.atomix.cluster.ClusterMembershipEvent) TestBootstrapService(io.atomix.cluster.TestBootstrapService) TestBroadcastServiceFactory(io.atomix.cluster.messaging.impl.TestBroadcastServiceFactory) Member(io.atomix.cluster.Member) HeartbeatMembershipProtocol(io.atomix.cluster.protocol.HeartbeatMembershipProtocol) TestBootstrapService(io.atomix.cluster.TestBootstrapService) BootstrapService(io.atomix.cluster.BootstrapService) Test(org.junit.Test)

Example 2 with HeartbeatMembershipProtocolConfig

use of io.atomix.cluster.protocol.HeartbeatMembershipProtocolConfig in project atomix by atomix.

the class RaftPerformanceTest method createServer.

/**
 * Creates a Raft server.
 */
private RaftServer createServer(Member member, List<Node> members) {
    RaftServerProtocol protocol;
    ManagedMessagingService messagingService;
    if (USE_NETTY) {
        messagingService = (ManagedMessagingService) new NettyMessagingService("test", member.address(), new MessagingConfig()).start().join();
        messagingServices.add(messagingService);
        protocol = new RaftServerMessagingProtocol(messagingService, PROTOCOL_SERIALIZER, addressMap::get);
    } else {
        protocol = protocolFactory.newServerProtocol(member.id());
    }
    BootstrapService bootstrapService = new BootstrapService() {

        @Override
        public MessagingService getMessagingService() {
            return messagingService;
        }

        @Override
        public UnicastService getUnicastService() {
            return new UnicastServiceAdapter();
        }

        @Override
        public BroadcastService getBroadcastService() {
            return new BroadcastServiceAdapter();
        }
    };
    RaftServer.Builder builder = RaftServer.builder(member.id()).withProtocol(protocol).withThreadModel(ThreadModel.SHARED_THREAD_POOL).withMembershipService(new DefaultClusterMembershipService(member, Version.from("1.0.0"), new DefaultNodeDiscoveryService(bootstrapService, member, new BootstrapDiscoveryProvider(members)), bootstrapService, new HeartbeatMembershipProtocol(new HeartbeatMembershipProtocolConfig()))).withStorage(RaftStorage.builder().withStorageLevel(StorageLevel.DISK).withDirectory(new File(String.format("target/perf-logs/%s", member.id()))).withNamespace(STORAGE_NAMESPACE).withMaxSegmentSize(1024 * 1024 * 64).withDynamicCompaction().withFlushOnCommit(false).build());
    RaftServer server = builder.build();
    servers.add(server);
    return server;
}
Also used : RaftServerProtocol(io.atomix.protocols.raft.protocol.RaftServerProtocol) RaftServerMessagingProtocol(io.atomix.protocols.raft.test.protocol.RaftServerMessagingProtocol) DefaultNodeDiscoveryService(io.atomix.cluster.impl.DefaultNodeDiscoveryService) RaftServer(io.atomix.protocols.raft.RaftServer) BootstrapDiscoveryProvider(io.atomix.cluster.discovery.BootstrapDiscoveryProvider) ManagedMessagingService(io.atomix.cluster.messaging.ManagedMessagingService) HeartbeatMembershipProtocolConfig(io.atomix.cluster.protocol.HeartbeatMembershipProtocolConfig) MessagingConfig(io.atomix.cluster.messaging.MessagingConfig) DefaultClusterMembershipService(io.atomix.cluster.impl.DefaultClusterMembershipService) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService) HeartbeatMembershipProtocol(io.atomix.cluster.protocol.HeartbeatMembershipProtocol) File(java.io.File) BootstrapService(io.atomix.cluster.BootstrapService)

Example 3 with HeartbeatMembershipProtocolConfig

use of io.atomix.cluster.protocol.HeartbeatMembershipProtocolConfig in project atomix by atomix.

the class AtomixConfigTest method testAtomixConfig.

@Test
public void testAtomixConfig() throws Exception {
    AtomixConfig config = Atomix.config(getClass().getClassLoader().getResource("test.conf").getPath());
    ClusterConfig cluster = config.getClusterConfig();
    assertEquals("test", cluster.getClusterId());
    MemberConfig node = cluster.getNodeConfig();
    assertEquals("one", node.getId().id());
    assertEquals("localhost:5000", node.getAddress().toString());
    assertEquals("foo", node.getZoneId());
    assertEquals("bar", node.getRackId());
    assertEquals("baz", node.getHostId());
    assertEquals("bar", node.getProperties().getProperty("foo"));
    assertEquals("baz", node.getProperties().getProperty("bar"));
    MulticastConfig multicast = cluster.getMulticastConfig();
    assertTrue(multicast.isEnabled());
    assertEquals("230.0.1.1", multicast.getGroup().getHostAddress());
    assertEquals(56789, multicast.getPort());
    HeartbeatMembershipProtocolConfig protocol = (HeartbeatMembershipProtocolConfig) cluster.getProtocolConfig();
    assertEquals(Duration.ofMillis(200), protocol.getHeartbeatInterval());
    assertEquals(12, protocol.getPhiFailureThreshold());
    assertEquals(Duration.ofSeconds(15), protocol.getFailureTimeout());
    MembershipConfig membership = cluster.getMembershipConfig();
    assertEquals(Duration.ofSeconds(1), membership.getBroadcastInterval());
    assertEquals(12, membership.getReachabilityThreshold());
    assertEquals(Duration.ofSeconds(15), membership.getReachabilityTimeout());
    MulticastDiscoveryConfig discovery = (MulticastDiscoveryConfig) cluster.getDiscoveryConfig();
    assertEquals(MulticastDiscoveryProvider.TYPE, discovery.getType());
    assertEquals(Duration.ofSeconds(1), discovery.getBroadcastInterval());
    assertEquals(12, discovery.getFailureThreshold());
    assertEquals(Duration.ofSeconds(15), discovery.getFailureTimeout());
    MessagingConfig messaging = cluster.getMessagingConfig();
    assertEquals(2, messaging.getInterfaces().size());
    assertEquals("127.0.0.1", messaging.getInterfaces().get(0));
    assertEquals("0.0.0.0", messaging.getInterfaces().get(1));
    assertEquals(5000, messaging.getPort().intValue());
    assertEquals(Duration.ofSeconds(10), messaging.getConnectTimeout());
    assertTrue(messaging.getTlsConfig().isEnabled());
    assertEquals("keystore.jks", messaging.getTlsConfig().getKeyStore());
    assertEquals("foo", messaging.getTlsConfig().getKeyStorePassword());
    assertEquals("truststore.jks", messaging.getTlsConfig().getTrustStore());
    assertEquals("bar", messaging.getTlsConfig().getTrustStorePassword());
    RaftPartitionGroupConfig managementGroup = (RaftPartitionGroupConfig) config.getManagementGroup();
    assertEquals(RaftPartitionGroup.TYPE, managementGroup.getType());
    assertEquals(1, managementGroup.getPartitions());
    assertEquals(Duration.ofSeconds(5), managementGroup.getElectionTimeout());
    assertEquals(Duration.ofMillis(500), managementGroup.getHeartbeatInterval());
    assertEquals(Duration.ofSeconds(10), managementGroup.getDefaultSessionTimeout());
    assertEquals(new MemorySize(1024 * 1024 * 16), managementGroup.getStorageConfig().getSegmentSize());
    RaftPartitionGroupConfig groupOne = (RaftPartitionGroupConfig) config.getPartitionGroups().get("one");
    assertEquals(RaftPartitionGroup.TYPE, groupOne.getType());
    assertEquals("one", groupOne.getName());
    assertEquals(7, groupOne.getPartitions());
    PrimaryBackupPartitionGroupConfig groupTwo = (PrimaryBackupPartitionGroupConfig) config.getPartitionGroups().get("two");
    assertEquals(PrimaryBackupPartitionGroup.TYPE, groupTwo.getType());
    assertEquals("two", groupTwo.getName());
    assertEquals(32, groupTwo.getPartitions());
    LogPartitionGroupConfig groupThree = (LogPartitionGroupConfig) config.getPartitionGroups().get("three");
    assertEquals(LogPartitionGroup.TYPE, groupThree.getType());
    assertEquals("three", groupThree.getName());
    assertEquals(3, groupThree.getPartitions());
    ConsensusProfileConfig consensusProfile = (ConsensusProfileConfig) config.getProfiles().get(0);
    assertEquals(ConsensusProfile.TYPE, consensusProfile.getType());
    assertEquals("management", consensusProfile.getManagementGroup());
    assertEquals("consensus", consensusProfile.getDataGroup());
    assertEquals(3, consensusProfile.getPartitions());
    assertTrue(consensusProfile.getMembers().containsAll(Arrays.asList("one", "two", "three")));
    DataGridProfileConfig dataGridProfile = (DataGridProfileConfig) config.getProfiles().get(1);
    assertEquals(DataGridProfile.TYPE, dataGridProfile.getType());
    assertEquals("management", dataGridProfile.getManagementGroup());
    assertEquals("data", dataGridProfile.getDataGroup());
    assertEquals(32, dataGridProfile.getPartitions());
    AtomicMapConfig fooDefaults = config.getPrimitiveDefault("atomic-map");
    assertEquals("atomic-map", fooDefaults.getType().name());
    assertEquals("two", ((MultiPrimaryProtocolConfig) fooDefaults.getProtocolConfig()).getGroup());
    AtomicMapConfig foo = config.getPrimitive("foo");
    assertEquals("atomic-map", foo.getType().name());
    assertTrue(foo.isNullValues());
    DistributedSetConfig bar = config.getPrimitive("bar");
    assertTrue(bar.getCacheConfig().isEnabled());
    MultiPrimaryProtocolConfig multiPrimary = (MultiPrimaryProtocolConfig) bar.getProtocolConfig();
    assertEquals(MultiPrimaryProtocol.TYPE, multiPrimary.getType());
    assertEquals(Replication.SYNCHRONOUS, multiPrimary.getReplication());
    assertEquals(Duration.ofSeconds(1), multiPrimary.getRetryDelay());
    AtomicValueConfig baz = config.getPrimitive("baz");
    MultiRaftProtocolConfig multiRaft = (MultiRaftProtocolConfig) baz.getProtocolConfig();
    assertEquals(ReadConsistency.SEQUENTIAL, multiRaft.getReadConsistency());
    assertEquals(Recovery.RECOVER, multiRaft.getRecoveryStrategy());
    assertEquals(Duration.ofSeconds(2), multiRaft.getRetryDelay());
    DistributedLogConfig log = config.getPrimitive("log");
    assertEquals("log", log.getType().name());
    DistributedLogProtocolConfig logConfig = (DistributedLogProtocolConfig) log.getProtocolConfig();
    assertEquals(DistributedLogProtocol.TYPE, logConfig.getType());
    assertEquals("three", logConfig.getGroup());
}
Also used : MultiRaftProtocolConfig(io.atomix.protocols.raft.MultiRaftProtocolConfig) MemorySize(io.atomix.utils.memory.MemorySize) MembershipConfig(io.atomix.cluster.MembershipConfig) DistributedLogConfig(io.atomix.core.log.DistributedLogConfig) MulticastDiscoveryConfig(io.atomix.cluster.discovery.MulticastDiscoveryConfig) DistributedSetConfig(io.atomix.core.set.DistributedSetConfig) DistributedLogProtocolConfig(io.atomix.protocols.log.DistributedLogProtocolConfig) HeartbeatMembershipProtocolConfig(io.atomix.cluster.protocol.HeartbeatMembershipProtocolConfig) AtomicValueConfig(io.atomix.core.value.AtomicValueConfig) MessagingConfig(io.atomix.cluster.messaging.MessagingConfig) AtomicMapConfig(io.atomix.core.map.AtomicMapConfig) RaftPartitionGroupConfig(io.atomix.protocols.raft.partition.RaftPartitionGroupConfig) PrimaryBackupPartitionGroupConfig(io.atomix.protocols.backup.partition.PrimaryBackupPartitionGroupConfig) ConsensusProfileConfig(io.atomix.core.profile.ConsensusProfileConfig) MultiPrimaryProtocolConfig(io.atomix.protocols.backup.MultiPrimaryProtocolConfig) MulticastConfig(io.atomix.cluster.MulticastConfig) MemberConfig(io.atomix.cluster.MemberConfig) DataGridProfileConfig(io.atomix.core.profile.DataGridProfileConfig) ClusterConfig(io.atomix.cluster.ClusterConfig) LogPartitionGroupConfig(io.atomix.protocols.log.partition.LogPartitionGroupConfig) Test(org.junit.Test)

Example 4 with HeartbeatMembershipProtocolConfig

use of io.atomix.cluster.protocol.HeartbeatMembershipProtocolConfig 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)

Aggregations

HeartbeatMembershipProtocolConfig (io.atomix.cluster.protocol.HeartbeatMembershipProtocolConfig)4 BootstrapService (io.atomix.cluster.BootstrapService)3 BootstrapDiscoveryProvider (io.atomix.cluster.discovery.BootstrapDiscoveryProvider)3 HeartbeatMembershipProtocol (io.atomix.cluster.protocol.HeartbeatMembershipProtocol)3 Test (org.junit.Test)3 ManagedClusterMembershipService (io.atomix.cluster.ManagedClusterMembershipService)2 Member (io.atomix.cluster.Member)2 Node (io.atomix.cluster.Node)2 TestBootstrapService (io.atomix.cluster.TestBootstrapService)2 DefaultClusterMembershipService (io.atomix.cluster.impl.DefaultClusterMembershipService)2 DefaultNodeDiscoveryService (io.atomix.cluster.impl.DefaultNodeDiscoveryService)2 MessagingConfig (io.atomix.cluster.messaging.MessagingConfig)2 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 ClusterConfig (io.atomix.cluster.ClusterConfig)1 ClusterMembershipEvent (io.atomix.cluster.ClusterMembershipEvent)1 ClusterMembershipService (io.atomix.cluster.ClusterMembershipService)1 MemberConfig (io.atomix.cluster.MemberConfig)1 MembershipConfig (io.atomix.cluster.MembershipConfig)1 MulticastConfig (io.atomix.cluster.MulticastConfig)1 MulticastDiscoveryConfig (io.atomix.cluster.discovery.MulticastDiscoveryConfig)1