Search in sources :

Example 1 with ClusterMembershipService

use of io.atomix.cluster.ClusterMembershipService 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 2 with ClusterMembershipService

use of io.atomix.cluster.ClusterMembershipService in project atomix by atomix.

the class DefaultPartitionService method start.

@Override
@SuppressWarnings("unchecked")
public CompletableFuture<PartitionService> start() {
    groupMembershipService.addListener(groupMembershipEventListener);
    return groupMembershipService.start().thenCompose(v -> {
        PartitionGroupMembership systemGroupMembership = groupMembershipService.getSystemMembership();
        if (systemGroupMembership != null) {
            if (systemGroup == null) {
                systemGroup = ((PartitionGroup.Type) systemGroupMembership.config().getType()).newPartitionGroup(systemGroupMembership.config());
            }
            systemElectionService = new DefaultPrimaryElectionService(systemGroup);
            systemSessionIdService = new ReplicatedSessionIdService(systemGroup);
            electionService = new HashBasedPrimaryElectionService(clusterMembershipService, groupMembershipService, communicationService);
            return electionService.start().thenCompose(s -> {
                PartitionManagementService managementService = new DefaultPartitionManagementService(clusterMembershipService, communicationService, primitiveTypeRegistry, electionService, new DefaultSessionIdService());
                if (systemGroupMembership.members().contains(clusterMembershipService.getLocalMember().id())) {
                    return systemGroup.join(managementService);
                } else {
                    return systemGroup.connect(managementService);
                }
            });
        } else {
            return Futures.exceptionalFuture(new ConfigurationException("No system partition group found"));
        }
    }).thenCompose(v -> systemElectionService.start().thenCompose(v2 -> systemSessionIdService.start()).thenApply(v2 -> new DefaultPartitionManagementService(clusterMembershipService, communicationService, primitiveTypeRegistry, systemElectionService, systemSessionIdService))).thenCompose(managementService -> {
        this.partitionManagementService = (PartitionManagementService) managementService;
        List<CompletableFuture> futures = groupMembershipService.getMemberships().stream().map(membership -> {
            ManagedPartitionGroup group;
            synchronized (groups) {
                group = groups.get(membership.group());
                if (group == null) {
                    group = ((PartitionGroup.Type) membership.config().getType()).newPartitionGroup(membership.config());
                    groups.put(group.name(), group);
                }
            }
            if (membership.members().contains(clusterMembershipService.getLocalMember().id())) {
                return group.join(partitionManagementService);
            } else {
                return group.connect(partitionManagementService);
            }
        }).collect(Collectors.toList());
        return CompletableFuture.allOf(futures.toArray(new CompletableFuture[futures.size()])).thenApply(v -> {
            LOGGER.info("Started");
            started.set(true);
            return this;
        });
    });
}
Also used : ManagedPartitionGroup(io.atomix.primitive.partition.ManagedPartitionGroup) PartitionGroup(io.atomix.primitive.partition.PartitionGroup) ManagedPrimaryElectionService(io.atomix.primitive.partition.ManagedPrimaryElectionService) PartitionGroupMembershipEventListener(io.atomix.primitive.partition.PartitionGroupMembershipEventListener) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) PrimitiveTypeRegistry(io.atomix.primitive.PrimitiveTypeRegistry) ManagedPartitionService(io.atomix.primitive.partition.ManagedPartitionService) DefaultSessionIdService(io.atomix.primitive.session.impl.DefaultSessionIdService) CompletableFuture(java.util.concurrent.CompletableFuture) ConfigurationException(io.atomix.utils.config.ConfigurationException) ClusterMembershipService(io.atomix.cluster.ClusterMembershipService) PartitionGroupTypeRegistry(io.atomix.primitive.partition.PartitionGroupTypeRegistry) Map(java.util.Map) ClusterCommunicationService(io.atomix.cluster.messaging.ClusterCommunicationService) ManagedPartitionGroup(io.atomix.primitive.partition.ManagedPartitionGroup) Futures(io.atomix.utils.concurrent.Futures) Logger(org.slf4j.Logger) ReplicatedSessionIdService(io.atomix.primitive.session.impl.ReplicatedSessionIdService) ManagedSessionIdService(io.atomix.primitive.session.ManagedSessionIdService) Collection(java.util.Collection) ManagedPartitionGroupMembershipService(io.atomix.primitive.partition.ManagedPartitionGroupMembershipService) Maps(com.google.common.collect.Maps) PartitionGroup(io.atomix.primitive.partition.PartitionGroup) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) PartitionManagementService(io.atomix.primitive.partition.PartitionManagementService) PartitionService(io.atomix.primitive.partition.PartitionService) PartitionGroupMembershipEvent(io.atomix.primitive.partition.PartitionGroupMembershipEvent) PartitionGroupMembership(io.atomix.primitive.partition.PartitionGroupMembership) ReplicatedSessionIdService(io.atomix.primitive.session.impl.ReplicatedSessionIdService) CompletableFuture(java.util.concurrent.CompletableFuture) PartitionGroupMembership(io.atomix.primitive.partition.PartitionGroupMembership) ManagedPartitionGroup(io.atomix.primitive.partition.ManagedPartitionGroup) PartitionManagementService(io.atomix.primitive.partition.PartitionManagementService) ConfigurationException(io.atomix.utils.config.ConfigurationException) DefaultSessionIdService(io.atomix.primitive.session.impl.DefaultSessionIdService)

Aggregations

ClusterMembershipService (io.atomix.cluster.ClusterMembershipService)2 Collection (java.util.Collection)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Collectors (java.util.stream.Collectors)2 Maps (com.google.common.collect.Maps)1 MoreExecutors (com.google.common.util.concurrent.MoreExecutors)1 BootstrapService (io.atomix.cluster.BootstrapService)1 ManagedClusterMembershipService (io.atomix.cluster.ManagedClusterMembershipService)1 Member (io.atomix.cluster.Member)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 ClusterCommunicationService (io.atomix.cluster.messaging.ClusterCommunicationService)1 ClusterEventService (io.atomix.cluster.messaging.ClusterEventService)1 ManagedClusterEventService (io.atomix.cluster.messaging.ManagedClusterEventService)1 MessagingService (io.atomix.cluster.messaging.MessagingService)1 HeartbeatMembershipProtocol (io.atomix.cluster.protocol.HeartbeatMembershipProtocol)1 HeartbeatMembershipProtocolConfig (io.atomix.cluster.protocol.HeartbeatMembershipProtocolConfig)1