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