Search in sources :

Example 1 with ClusterService

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;
}
Also used : Node(io.atomix.cluster.Node) NodeId(io.atomix.cluster.NodeId) RaftProtocol(io.atomix.protocols.raft.RaftProtocol) ClusterEvent(io.atomix.cluster.ClusterEvent) LoggerFactory(org.slf4j.LoggerFactory) PartitionMetadata(io.atomix.primitive.partition.PartitionMetadata) CompletableFuture(java.util.concurrent.CompletableFuture) PartitionId(io.atomix.primitive.partition.PartitionId) ArrayList(java.util.ArrayList) StorageLevel(io.atomix.storage.StorageLevel) HashSet(java.util.HashSet) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) Lists(com.google.common.collect.Lists) Partition(io.atomix.primitive.partition.Partition) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol) Map(java.util.Map) ManagedPartitionGroup(io.atomix.primitive.partition.ManagedPartitionGroup) Futures(io.atomix.utils.concurrent.Futures) Logger(org.slf4j.Logger) Collection(java.util.Collection) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Set(java.util.Set) Recovery(io.atomix.primitive.Recovery) Maps(com.google.common.collect.Maps) PartitionGroup(io.atomix.primitive.partition.PartitionGroup) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) File(java.io.File) ClusterEventListener(io.atomix.cluster.ClusterEventListener) List(java.util.List) PartitionManagementService(io.atomix.primitive.partition.PartitionManagementService) Type(io.atomix.primitive.PrimitiveProtocol.Type) ClusterService(io.atomix.cluster.ClusterService) Collections(java.util.Collections) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) Node(io.atomix.cluster.Node) NodeId(io.atomix.cluster.NodeId) ArrayList(java.util.ArrayList) PartitionMetadata(io.atomix.primitive.partition.PartitionMetadata) PartitionId(io.atomix.primitive.partition.PartitionId) HashSet(java.util.HashSet)

Example 2 with ClusterService

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;
}
Also used : ManagedClusterMetadataService(io.atomix.cluster.ManagedClusterMetadataService) Node(io.atomix.cluster.Node) Arrays(java.util.Arrays) ManagedPrimaryElectionService(io.atomix.primitive.partition.ManagedPrimaryElectionService) ClusterEventingService(io.atomix.cluster.messaging.ClusterEventingService) DistributedPrimitive(io.atomix.primitive.DistributedPrimitive) RaftProtocol(io.atomix.protocols.raft.RaftProtocol) LoggerFactory(org.slf4j.LoggerFactory) PrimitiveTypeRegistry(io.atomix.primitive.PrimitiveTypeRegistry) DistributedPrimitiveBuilder(io.atomix.primitive.DistributedPrimitiveBuilder) NettyMessagingService(io.atomix.messaging.impl.NettyMessagingService) TransactionBuilder(io.atomix.core.transaction.TransactionBuilder) Managed(io.atomix.utils.Managed) InetAddress(java.net.InetAddress) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) CorePrimitivesService(io.atomix.core.impl.CorePrimitivesService) Duration(java.time.Duration) ManagedPartitionGroup(io.atomix.primitive.partition.ManagedPartitionGroup) DefaultPrimaryElectionService(io.atomix.primitive.partition.impl.DefaultPrimaryElectionService) ManagedSessionIdService(io.atomix.primitive.session.ManagedSessionIdService) Collection(java.util.Collection) Set(java.util.Set) Endpoint(io.atomix.messaging.Endpoint) ManagedClusterEventingService(io.atomix.cluster.messaging.ManagedClusterEventingService) ReadConsistency(io.atomix.protocols.raft.ReadConsistency) RaftPartitionGroup(io.atomix.protocols.raft.partition.RaftPartitionGroup) SingleThreadContext(io.atomix.utils.concurrent.SingleThreadContext) ThreadContext(io.atomix.utils.concurrent.ThreadContext) ClusterMessagingService(io.atomix.cluster.messaging.ClusterMessagingService) CommunicationStrategy(io.atomix.protocols.raft.proxy.CommunicationStrategy) DefaultClusterMessagingService(io.atomix.cluster.messaging.impl.DefaultClusterMessagingService) MoreObjects.toStringHelper(com.google.common.base.MoreObjects.toStringHelper) DefaultPartitionService(io.atomix.primitive.partition.impl.DefaultPartitionService) IdGeneratorSessionIdService(io.atomix.core.generator.impl.IdGeneratorSessionIdService) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ManagedPartitionService(io.atomix.primitive.partition.ManagedPartitionService) CompletableFuture(java.util.concurrent.CompletableFuture) ManagedClusterService(io.atomix.cluster.ManagedClusterService) DefaultClusterEventingService(io.atomix.cluster.messaging.impl.DefaultClusterEventingService) ArrayList(java.util.ArrayList) DefaultClusterService(io.atomix.cluster.impl.DefaultClusterService) Futures(io.atomix.utils.concurrent.Futures) PrimaryBackupPartitionGroup(io.atomix.protocols.backup.partition.PrimaryBackupPartitionGroup) ManagedMessagingService(io.atomix.messaging.ManagedMessagingService) ClusterMetadata(io.atomix.cluster.ClusterMetadata) Logger(org.slf4j.Logger) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) ClusterMetadataService(io.atomix.cluster.ClusterMetadataService) Recovery(io.atomix.primitive.Recovery) UnknownHostException(java.net.UnknownHostException) File(java.io.File) DefaultClusterMetadataService(io.atomix.cluster.impl.DefaultClusterMetadataService) ManagedClusterMessagingService(io.atomix.cluster.messaging.ManagedClusterMessagingService) MessagingService(io.atomix.messaging.MessagingService) PartitionManagementService(io.atomix.primitive.partition.PartitionManagementService) PrimitiveType(io.atomix.primitive.PrimitiveType) PartitionService(io.atomix.primitive.partition.PartitionService) DefaultPartitionManagementService(io.atomix.primitive.partition.impl.DefaultPartitionManagementService) ClusterService(io.atomix.cluster.ClusterService) Collections(java.util.Collections) ManagedPrimaryElectionService(io.atomix.primitive.partition.ManagedPrimaryElectionService) DefaultPrimaryElectionService(io.atomix.primitive.partition.impl.DefaultPrimaryElectionService) DefaultPartitionManagementService(io.atomix.primitive.partition.impl.DefaultPartitionManagementService) IdGeneratorSessionIdService(io.atomix.core.generator.impl.IdGeneratorSessionIdService) ManagedSessionIdService(io.atomix.primitive.session.ManagedSessionIdService)

Example 3 with ClusterService

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));
}
Also used : ClusterMetadata(io.atomix.cluster.ClusterMetadata) Node(io.atomix.cluster.Node) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) ClusterMetadata(io.atomix.cluster.ClusterMetadata) ClusterEventingService(io.atomix.cluster.messaging.ClusterEventingService) Set(java.util.Set) Assert.assertTrue(org.junit.Assert.assertTrue) Endpoint(io.atomix.messaging.Endpoint) Test(org.junit.Test) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) UnknownHostException(java.net.UnknownHostException) ArrayList(java.util.ArrayList) InetAddress(java.net.InetAddress) DefaultClusterService(io.atomix.cluster.impl.DefaultClusterService) List(java.util.List) MessagingService(io.atomix.messaging.MessagingService) KryoNamespaces(io.atomix.utils.serializer.KryoNamespaces) ClusterService(io.atomix.cluster.ClusterService) Serializer(io.atomix.utils.serializer.Serializer) TestClusterMetadataService(io.atomix.cluster.impl.TestClusterMetadataService) Assert.assertEquals(org.junit.Assert.assertEquals) Node(io.atomix.cluster.Node) CopyOnWriteArraySet(java.util.concurrent.CopyOnWriteArraySet) ClusterEventingService(io.atomix.cluster.messaging.ClusterEventingService) MessagingService(io.atomix.messaging.MessagingService) TestClusterMetadataService(io.atomix.cluster.impl.TestClusterMetadataService) DefaultClusterService(io.atomix.cluster.impl.DefaultClusterService) ClusterService(io.atomix.cluster.ClusterService) DefaultClusterService(io.atomix.cluster.impl.DefaultClusterService) Test(org.junit.Test)

Aggregations

ClusterService (io.atomix.cluster.ClusterService)3 Node (io.atomix.cluster.Node)3 ArrayList (java.util.ArrayList)3 Set (java.util.Set)3 MoreObjects.toStringHelper (com.google.common.base.MoreObjects.toStringHelper)2 Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)2 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)2 ClusterMetadata (io.atomix.cluster.ClusterMetadata)2 DefaultClusterService (io.atomix.cluster.impl.DefaultClusterService)2 ClusterEventingService (io.atomix.cluster.messaging.ClusterEventingService)2 Endpoint (io.atomix.messaging.Endpoint)2 MessagingService (io.atomix.messaging.MessagingService)2 Recovery (io.atomix.primitive.Recovery)2 ManagedPartitionGroup (io.atomix.primitive.partition.ManagedPartitionGroup)2 PartitionManagementService (io.atomix.primitive.partition.PartitionManagementService)2 InetAddress (java.net.InetAddress)2 UnknownHostException (java.net.UnknownHostException)2 Lists (com.google.common.collect.Lists)1 Maps (com.google.common.collect.Maps)1 Sets (com.google.common.collect.Sets)1