Search in sources :

Example 1 with PartitionId

use of io.atomix.primitive.partition.PartitionId 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 PartitionId

use of io.atomix.primitive.partition.PartitionId in project atomix by atomix.

the class DocumentTreeProxyBuilder method buildAsync.

@Override
@SuppressWarnings("unchecked")
public CompletableFuture<DocumentTree<V>> buildAsync() {
    PrimitiveProtocol protocol = protocol();
    PartitionGroup<?> partitions = managementService.getPartitionService().getPartitionGroup(protocol);
    Map<PartitionId, CompletableFuture<AsyncDocumentTree<V>>> trees = Maps.newConcurrentMap();
    for (Partition partition : partitions.getPartitions()) {
        trees.put(partition.id(), partition.getPrimitiveClient().newProxy(name(), primitiveType(), protocol).connect().thenApply(proxy -> {
            DocumentTreeProxy rawTree = new DocumentTreeProxy(proxy);
            return new TranscodingAsyncDocumentTree<>(rawTree, serializer()::encode, serializer()::decode);
        }));
    }
    Partitioner<DocumentPath> partitioner = key -> {
        int bucket = (key == null) ? 0 : Math.abs(Hashing.murmur3_32().hashUnencodedChars(key.pathElements().size() == 1 ? key.pathElements().get(0) : key.pathElements().get(1)).asInt()) % NUM_BUCKETS;
        return partitions.getPartitionIds().get(Hashing.consistentHash(bucket, partitions.getPartitionIds().size()));
    };
    return Futures.allOf(Lists.newArrayList(trees.values())).thenApply(t -> {
        AsyncDocumentTree<V> tree = new PartitionedAsyncDocumentTree<>(name(), Maps.transformValues(trees, v -> v.getNow(null)), partitioner);
        if (relaxedReadConsistency()) {
            tree = new CachingAsyncDocumentTree<>(tree);
        }
        return tree.sync();
    });
}
Also used : Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) AsyncDocumentTree(io.atomix.core.tree.AsyncDocumentTree) Hashing(com.google.common.hash.Hashing) DocumentPath(io.atomix.core.tree.DocumentPath) CompletableFuture(java.util.concurrent.CompletableFuture) Maps(com.google.common.collect.Maps) PartitionGroup(io.atomix.primitive.partition.PartitionGroup) DocumentTree(io.atomix.core.tree.DocumentTree) PartitionId(io.atomix.primitive.partition.PartitionId) DocumentTreeBuilder(io.atomix.core.tree.DocumentTreeBuilder) Lists(com.google.common.collect.Lists) Partition(io.atomix.primitive.partition.Partition) PrimitiveManagementService(io.atomix.primitive.PrimitiveManagementService) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol) Map(java.util.Map) Partitioner(io.atomix.primitive.partition.Partitioner) Futures(io.atomix.utils.concurrent.Futures) Partition(io.atomix.primitive.partition.Partition) PartitionId(io.atomix.primitive.partition.PartitionId) CompletableFuture(java.util.concurrent.CompletableFuture) DocumentPath(io.atomix.core.tree.DocumentPath) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol)

Example 3 with PartitionId

use of io.atomix.primitive.partition.PartitionId in project atomix by atomix.

the class PrimaryElectorService method enter.

/**
 * Applies an {@link PrimaryElectorOperations.Enter} commit.
 *
 * @param commit commit entry
 * @return topic leader. If no previous leader existed this is the node that just entered the race.
 */
protected PrimaryTerm enter(Commit<? extends PrimaryElectorOperations.Enter> commit) {
    try {
        PartitionId partitionId = commit.value().partitionId();
        PrimaryTerm oldTerm = term(partitionId);
        Registration registration = new Registration(commit.value().member(), commit.session().sessionId().id());
        PrimaryTerm newTerm = elections.compute(partitionId, (k, v) -> {
            if (v == null) {
                return new ElectionState(partitionId, registration, elections);
            } else {
                if (!v.isDuplicate(registration)) {
                    return new ElectionState(v).addRegistration(registration);
                } else {
                    return v;
                }
            }
        }).term();
        if (!Objects.equals(oldTerm, newTerm)) {
            notifyTermChange(partitionId, newTerm);
            scheduleRebalance();
        }
        return newTerm;
    } catch (Exception e) {
        getLogger().error("State machine operation failed", e);
        throw Throwables.propagate(e);
    }
}
Also used : ServiceExecutor(io.atomix.primitive.service.ServiceExecutor) Arrays(java.util.Arrays) BufferInput(io.atomix.storage.buffer.BufferInput) CHANGE(io.atomix.primitive.partition.impl.PrimaryElectorEvents.CHANGE) PrimaryElectionEvent(io.atomix.primitive.partition.PrimaryElectionEvent) HashMap(java.util.HashMap) PartitionId(io.atomix.primitive.partition.PartitionId) LinkedHashMap(java.util.LinkedHashMap) Lists(com.google.common.collect.Lists) Duration(java.time.Duration) Map(java.util.Map) PrimaryTerm(io.atomix.primitive.partition.PrimaryTerm) KryoNamespace(io.atomix.utils.serializer.KryoNamespace) Session(io.atomix.primitive.session.Session) Scheduled(io.atomix.utils.concurrent.Scheduled) LinkedList(java.util.LinkedList) Commit(io.atomix.primitive.service.Commit) MoreObjects(com.google.common.base.MoreObjects) Throwables(com.google.common.base.Throwables) Set(java.util.Set) Collectors(java.util.stream.Collectors) Sets(com.google.common.collect.Sets) Member(io.atomix.primitive.partition.Member) BufferOutput(io.atomix.storage.buffer.BufferOutput) Objects(java.util.Objects) List(java.util.List) AbstractPrimitiveService(io.atomix.primitive.service.AbstractPrimitiveService) Optional(java.util.Optional) Serializer(io.atomix.utils.serializer.Serializer) PartitionId(io.atomix.primitive.partition.PartitionId) PrimaryTerm(io.atomix.primitive.partition.PrimaryTerm)

Example 4 with PartitionId

use of io.atomix.primitive.partition.PartitionId in project atomix by atomix.

the class ConsistentMapProxyBuilder method buildAsync.

@Override
@SuppressWarnings("unchecked")
public CompletableFuture<ConsistentMap<K, V>> buildAsync() {
    PrimitiveProtocol protocol = protocol();
    PartitionGroup<?> partitions = managementService.getPartitionService().getPartitionGroup(protocol);
    Map<PartitionId, CompletableFuture<AsyncConsistentMap<byte[], byte[]>>> maps = Maps.newConcurrentMap();
    for (Partition partition : partitions.getPartitions()) {
        maps.put(partition.id(), partition.getPrimitiveClient().newProxy(name(), primitiveType(), protocol).connect().thenApply(proxy -> new TranscodingAsyncConsistentMap<>(new ConsistentMapProxy(proxy), BaseEncoding.base16()::encode, BaseEncoding.base16()::decode, Function.identity(), Function.identity())));
    }
    Partitioner<byte[]> partitioner = key -> {
        int bucket = Math.abs(Hashing.murmur3_32().hashBytes(key).asInt()) % NUM_BUCKETS;
        return partitions.getPartitionIds().get(Hashing.consistentHash(bucket, partitions.getPartitionIds().size()));
    };
    return Futures.allOf(Lists.newArrayList(maps.values())).thenApply(m -> {
        AsyncConsistentMap<byte[], byte[]> partitionedMap = new PartitionedAsyncConsistentMap<>(name(), Maps.transformValues(maps, v -> v.getNow(null)), partitioner);
        Serializer serializer = serializer();
        AsyncConsistentMap<K, V> map = new TranscodingAsyncConsistentMap<>(partitionedMap, key -> serializer.encode(key), bytes -> serializer.decode(bytes), value -> value == null ? null : serializer.encode(value), bytes -> serializer.decode(bytes));
        if (!nullValues()) {
            map = new NotNullAsyncConsistentMap<>(map);
        }
        if (relaxedReadConsistency()) {
            map = new CachingAsyncConsistentMap<>(map);
        }
        if (readOnly()) {
            map = new UnmodifiableAsyncConsistentMap<>(map);
        }
        return map.sync();
    });
}
Also used : BaseEncoding(com.google.common.io.BaseEncoding) ConsistentMapBuilder(io.atomix.core.map.ConsistentMapBuilder) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Hashing(com.google.common.hash.Hashing) CompletableFuture(java.util.concurrent.CompletableFuture) Maps(com.google.common.collect.Maps) PartitionGroup(io.atomix.primitive.partition.PartitionGroup) Function(java.util.function.Function) PartitionId(io.atomix.primitive.partition.PartitionId) Lists(com.google.common.collect.Lists) AsyncConsistentMap(io.atomix.core.map.AsyncConsistentMap) Partition(io.atomix.primitive.partition.Partition) PrimitiveManagementService(io.atomix.primitive.PrimitiveManagementService) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol) ConsistentMap(io.atomix.core.map.ConsistentMap) Map(java.util.Map) Partitioner(io.atomix.primitive.partition.Partitioner) Serializer(io.atomix.utils.serializer.Serializer) Futures(io.atomix.utils.concurrent.Futures) Partition(io.atomix.primitive.partition.Partition) PartitionId(io.atomix.primitive.partition.PartitionId) CompletableFuture(java.util.concurrent.CompletableFuture) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol) Serializer(io.atomix.utils.serializer.Serializer)

Example 5 with PartitionId

use of io.atomix.primitive.partition.PartitionId in project atomix by atomix.

the class LeaderElectorProxyBuilder method buildAsync.

@Override
@SuppressWarnings("unchecked")
public CompletableFuture<LeaderElector<T>> buildAsync() {
    PrimitiveProtocol protocol = protocol();
    PartitionGroup<?> partitions = managementService.getPartitionService().getPartitionGroup(protocol);
    Map<PartitionId, CompletableFuture<AsyncLeaderElector<T>>> electors = Maps.newConcurrentMap();
    for (Partition partition : partitions.getPartitions()) {
        electors.put(partition.id(), newLeaderElector(partition.getPrimitiveClient().newProxy(name(), primitiveType(), protocol)));
    }
    Partitioner<String> partitioner = topic -> partitions.getPartition(topic).id();
    return Futures.allOf(new ArrayList<>(electors.values())).thenApply(e -> new PartitionedAsyncLeaderElector<T>(name(), Maps.transformValues(electors, v -> v.getNow(null)), partitioner).sync());
}
Also used : LeaderElectorBuilder(io.atomix.core.election.LeaderElectorBuilder) AsyncLeaderElector(io.atomix.core.election.AsyncLeaderElector) LeaderElector(io.atomix.core.election.LeaderElector) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) CompletableFuture(java.util.concurrent.CompletableFuture) Maps(com.google.common.collect.Maps) PartitionGroup(io.atomix.primitive.partition.PartitionGroup) PartitionId(io.atomix.primitive.partition.PartitionId) ArrayList(java.util.ArrayList) Partition(io.atomix.primitive.partition.Partition) PrimitiveProxy(io.atomix.primitive.proxy.PrimitiveProxy) PrimitiveManagementService(io.atomix.primitive.PrimitiveManagementService) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol) Map(java.util.Map) Partitioner(io.atomix.primitive.partition.Partitioner) Futures(io.atomix.utils.concurrent.Futures) Partition(io.atomix.primitive.partition.Partition) CompletableFuture(java.util.concurrent.CompletableFuture) ArrayList(java.util.ArrayList) PartitionId(io.atomix.primitive.partition.PartitionId) PrimitiveProtocol(io.atomix.primitive.PrimitiveProtocol)

Aggregations

PartitionId (io.atomix.primitive.partition.PartitionId)7 Partition (io.atomix.primitive.partition.Partition)5 Map (java.util.Map)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 Preconditions.checkNotNull (com.google.common.base.Preconditions.checkNotNull)4 Lists (com.google.common.collect.Lists)4 Maps (com.google.common.collect.Maps)4 PrimitiveProtocol (io.atomix.primitive.PrimitiveProtocol)4 PartitionGroup (io.atomix.primitive.partition.PartitionGroup)4 Futures (io.atomix.utils.concurrent.Futures)4 PrimitiveManagementService (io.atomix.primitive.PrimitiveManagementService)3 Partitioner (io.atomix.primitive.partition.Partitioner)3 Collectors (java.util.stream.Collectors)3 MoreObjects.toStringHelper (com.google.common.base.MoreObjects.toStringHelper)2 Sets (com.google.common.collect.Sets)2 Hashing (com.google.common.hash.Hashing)2 NodeId (io.atomix.cluster.NodeId)2 Member (io.atomix.primitive.partition.Member)2 PartitionManagementService (io.atomix.primitive.partition.PartitionManagementService)2 PrimaryTerm (io.atomix.primitive.partition.PrimaryTerm)2