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();
}
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;
});
});
}
Aggregations