use of io.atomix.cluster.ClusterService in project atomix by atomix.
the class RaftPartitionGroup method buildPartitions.
private Collection<PartitionMetadata> buildPartitions(ClusterService clusterService) {
int partitionSize = this.partitionSize;
if (partitionSize == 0) {
partitionSize = clusterService.getNodes().size();
}
List<NodeId> sorted = new ArrayList<>(clusterService.getNodes().stream().filter(node -> node.type() == Node.Type.CORE).map(Node::id).collect(Collectors.toSet()));
Collections.sort(sorted);
int length = sorted.size();
int count = Math.min(partitionSize, length);
Set<PartitionMetadata> metadata = Sets.newHashSet();
for (int i = 0; i < partitions.size(); i++) {
PartitionId partitionId = sortedPartitionIds.get(i);
Set<NodeId> set = new HashSet<>(count);
for (int j = 0; j < count; j++) {
set.add(sorted.get((i + j) % length));
}
metadata.add(new PartitionMetadata(partitionId, set));
}
return metadata;
}
use of io.atomix.cluster.ClusterService in project atomix by atomix.
the class Atomix method start.
/**
* Starts the Atomix instance.
* <p>
* The returned future will be completed once this instance completes startup. Note that in order to complete startup,
* all partitions must be able to form. For Raft partitions, that requires that a majority of the nodes in each
* partition be started concurrently.
*
* @return a future to be completed once the instance has completed startup
*/
@Override
public synchronized CompletableFuture<Atomix> start() {
if (closeFuture != null) {
return Futures.exceptionalFuture(new IllegalStateException("Atomix instance " + (closeFuture.isDone() ? "shutdown" : "shutting down")));
}
if (openFuture != null) {
return openFuture;
}
openFuture = messagingService.start().thenComposeAsync(v -> metadataService.start(), context).thenComposeAsync(v -> clusterService.start(), context).thenComposeAsync(v -> clusterMessagingService.start(), context).thenComposeAsync(v -> clusterEventingService.start(), context).thenComposeAsync(v -> systemPartitionGroup.open(new DefaultPartitionManagementService(metadataService, clusterService, clusterMessagingService, primitiveTypes, null, null)), context).thenComposeAsync(v -> {
ManagedPrimaryElectionService electionService = new DefaultPrimaryElectionService(systemPartitionGroup, RaftProtocol.builder().withMinTimeout(Duration.ofMillis(250)).withMaxTimeout(Duration.ofSeconds(5)).withReadConsistency(ReadConsistency.LINEARIZABLE).withCommunicationStrategy(CommunicationStrategy.LEADER).withRecoveryStrategy(Recovery.RECOVER).withMaxRetries(5).build());
ManagedSessionIdService sessionIdService = new IdGeneratorSessionIdService(systemPartitionGroup);
return electionService.start().thenComposeAsync(v2 -> sessionIdService.start(), context).thenApply(v2 -> new DefaultPartitionManagementService(metadataService, clusterService, clusterMessagingService, primitiveTypes, electionService, sessionIdService));
}, context).thenComposeAsync(partitionManagementService -> partitions.open((PartitionManagementService) partitionManagementService), context).thenComposeAsync(v -> primitives.start(), context).thenApplyAsync(v -> {
metadataService.addNode(clusterService.getLocalNode());
started.set(true);
LOGGER.info("Started");
return this;
}, context);
return openFuture;
}
use of io.atomix.cluster.ClusterService in project atomix by atomix.
the class DefaultClusterEventingServiceTest method testClusterEventService.
@Test
public void testClusterEventService() throws Exception {
TestMessagingServiceFactory factory = new TestMessagingServiceFactory();
ClusterMetadata clusterMetadata = buildClusterMetadata(1, 1, 2, 3);
Node localNode1 = buildNode(1, Node.Type.CORE);
MessagingService messagingService1 = factory.newMessagingService(localNode1.endpoint()).start().join();
ClusterService clusterService1 = new DefaultClusterService(localNode1, new TestClusterMetadataService(clusterMetadata), messagingService1).start().join();
ClusterEventingService eventService1 = new DefaultClusterEventingService(clusterService1, messagingService1).start().join();
Node localNode2 = buildNode(2, Node.Type.CORE);
MessagingService messagingService2 = factory.newMessagingService(localNode2.endpoint()).start().join();
ClusterService clusterService2 = new DefaultClusterService(localNode2, new TestClusterMetadataService(clusterMetadata), messagingService2).start().join();
ClusterEventingService eventService2 = new DefaultClusterEventingService(clusterService2, messagingService2).start().join();
Node localNode3 = buildNode(3, Node.Type.CORE);
MessagingService messagingService3 = factory.newMessagingService(localNode3.endpoint()).start().join();
ClusterService clusterService3 = new DefaultClusterService(localNode3, new TestClusterMetadataService(clusterMetadata), messagingService3).start().join();
ClusterEventingService eventService3 = new DefaultClusterEventingService(clusterService3, messagingService3).start().join();
Thread.sleep(100);
Set<Integer> events = new CopyOnWriteArraySet<>();
eventService1.<String>subscribe("test1", SERIALIZER::decode, message -> {
assertEquals(message, "Hello world!");
events.add(1);
}, MoreExecutors.directExecutor()).join();
eventService2.<String>subscribe("test1", SERIALIZER::decode, message -> {
assertEquals(message, "Hello world!");
events.add(2);
}, MoreExecutors.directExecutor()).join();
eventService2.<String>subscribe("test1", SERIALIZER::decode, message -> {
assertEquals(message, "Hello world!");
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));
}
Aggregations