Search in sources :

Example 11 with MemberId

use of io.atomix.cluster.MemberId in project atomix by atomix.

the class AntiEntropyMapDelegate method handleUpdateRequests.

private void handleUpdateRequests(UpdateRequest<String> request) {
    final Set<String> keys = request.keys();
    final MemberId sender = request.sender();
    final List<MemberId> peers = ImmutableList.of(sender);
    keys.forEach(key -> queueUpdate(new UpdateEntry(key, items.get(key)), peers));
}
Also used : MemberId(io.atomix.cluster.MemberId)

Example 12 with MemberId

use of io.atomix.cluster.MemberId in project atomix by atomix.

the class AntiEntropyMapDelegate method requestBootstrapFromPeers.

/**
 * Requests all updates from each peer in the provided list of peers.
 * <p>
 * The returned future will be completed once at least one peer bootstraps this map or bootstrap requests to all peers
 * fail.
 *
 * @param peers the list of peers from which to request updates
 * @return a future to be completed once updates have been received from at least one peer
 */
private CompletableFuture<Void> requestBootstrapFromPeers(List<MemberId> peers) {
    if (peers.isEmpty()) {
        return CompletableFuture.completedFuture(null);
    }
    CompletableFuture<Void> future = new CompletableFuture<>();
    final int totalPeers = peers.size();
    AtomicBoolean successful = new AtomicBoolean();
    AtomicInteger totalCount = new AtomicInteger();
    AtomicReference<Throwable> lastError = new AtomicReference<>();
    // successful bootstrap response, the future will be completed with the last exception.
    for (MemberId peer : peers) {
        requestBootstrapFromPeer(peer).whenComplete((result, error) -> {
            if (error == null) {
                if (successful.compareAndSet(false, true)) {
                    future.complete(null);
                } else if (totalCount.incrementAndGet() == totalPeers) {
                    Throwable e = lastError.get();
                    if (e != null) {
                        future.completeExceptionally(e);
                    }
                }
            } else {
                if (!successful.get() && totalCount.incrementAndGet() == totalPeers) {
                    future.completeExceptionally(error);
                } else {
                    lastError.set(error);
                }
            }
        });
    }
    return future;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MemberId(io.atomix.cluster.MemberId) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 13 with MemberId

use of io.atomix.cluster.MemberId in project atomix by atomix.

the class DistributedLogTest method createClient.

/**
 * Creates a Raft client.
 */
private DistributedLogSessionClient createClient() throws Throwable {
    MemberId memberId = nextMemberId();
    DistributedLogSessionClient client = DistributedLogSessionClient.builder().withClientName("test").withPartitionId(PartitionId.from("test", 1)).withMembershipService(new TestClusterMembershipService(memberId, nodes)).withSessionIdProvider(() -> CompletableFuture.completedFuture(nextSessionId())).withPrimaryElection(election).withProtocol(protocolFactory.newClientProtocol(memberId)).build();
    clients.add(client);
    return client;
}
Also used : MemberId(io.atomix.cluster.MemberId) TestClusterMembershipService(io.atomix.cluster.TestClusterMembershipService)

Example 14 with MemberId

use of io.atomix.cluster.MemberId in project atomix by atomix.

the class DistributedLogTest method clearTests.

@Before
@After
public void clearTests() throws Exception {
    Futures.allOf(servers.stream().map(s -> s.stop().exceptionally(v -> null)).collect(Collectors.toList())).get(30, TimeUnit.SECONDS);
    Futures.allOf(clients.stream().map(c -> c.close().exceptionally(v -> null)).collect(Collectors.toList())).get(30, TimeUnit.SECONDS);
    Path directory = Paths.get("target/test-logs/");
    if (Files.exists(directory)) {
        Files.walkFileTree(directory, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                Files.delete(file);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                Files.delete(dir);
                return FileVisitResult.CONTINUE;
            }
        });
    }
    nodes = new ArrayList<>();
    memberId = 0;
    sessionId = 0;
    clients = new ArrayList<>();
    servers = new ArrayList<>();
    protocolFactory = new TestLogProtocolFactory();
    election = new TestPrimaryElection(PartitionId.from("test", 1));
}
Also used : ServiceExecutor(io.atomix.primitive.service.ServiceExecutor) Arrays(java.util.Arrays) TimeoutException(java.util.concurrent.TimeoutException) MemberGroupStrategy(io.atomix.primitive.partition.MemberGroupStrategy) SessionId(io.atomix.primitive.session.SessionId) PrimitiveManagementService(io.atomix.primitive.PrimitiveManagementService) Duration(java.time.Duration) PrimitiveConfig(io.atomix.primitive.config.PrimitiveConfig) After(org.junit.After) Session(io.atomix.primitive.session.Session) Path(java.nio.file.Path) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) ConcurrentTestCase(net.jodah.concurrentunit.ConcurrentTestCase) PrimitiveBuilder(io.atomix.primitive.PrimitiveBuilder) Predicate(java.util.function.Predicate) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) FileVisitResult(java.nio.file.FileVisitResult) List(java.util.List) PrimitiveService(io.atomix.primitive.service.PrimitiveService) ServiceConfig(io.atomix.primitive.service.ServiceConfig) PrimaryElection(io.atomix.primitive.partition.PrimaryElection) BackupOutput(io.atomix.primitive.service.BackupOutput) Serializer(io.atomix.utils.serializer.Serializer) BackupInput(io.atomix.primitive.service.BackupInput) DefaultSerializers(io.atomix.utils.serializer.serializers.DefaultSerializers) CompletableFuture(java.util.concurrent.CompletableFuture) EventType(io.atomix.primitive.event.EventType) ArrayList(java.util.ArrayList) PartitionId(io.atomix.primitive.partition.PartitionId) OperationId(io.atomix.primitive.operation.OperationId) LogSession(io.atomix.primitive.log.LogSession) TestClusterMembershipService(io.atomix.cluster.TestClusterMembershipService) MemberId(io.atomix.cluster.MemberId) Futures(io.atomix.utils.concurrent.Futures) Before(org.junit.Before) TestLogProtocolFactory(io.atomix.protocols.log.protocol.TestLogProtocolFactory) Files(java.nio.file.Files) Commit(io.atomix.primitive.service.Commit) TestPrimaryElection(io.atomix.primitive.partition.TestPrimaryElection) IOException(java.io.IOException) Test(org.junit.Test) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) File(java.io.File) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AbstractPrimitiveService(io.atomix.primitive.service.AbstractPrimitiveService) Paths(java.nio.file.Paths) PrimitiveType(io.atomix.primitive.PrimitiveType) Assert.assertEquals(org.junit.Assert.assertEquals) Path(java.nio.file.Path) TestPrimaryElection(io.atomix.primitive.partition.TestPrimaryElection) FileVisitResult(java.nio.file.FileVisitResult) IOException(java.io.IOException) TestLogProtocolFactory(io.atomix.protocols.log.protocol.TestLogProtocolFactory) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Before(org.junit.Before) After(org.junit.After)

Example 15 with MemberId

use of io.atomix.cluster.MemberId in project atomix by atomix.

the class SynchronousReplicator method replicate.

@Override
public CompletableFuture<Void> replicate(BackupOperation operation) {
    if (context.followers().isEmpty()) {
        context.setCommitIndex(operation.index());
        return CompletableFuture.completedFuture(null);
    }
    CompletableFuture<Void> future = new CompletableFuture<>();
    futures.put(operation.index(), future);
    for (MemberId backup : context.followers()) {
        queues.computeIfAbsent(backup, BackupQueue::new).add(operation);
    }
    return future;
}
Also used : MemberId(io.atomix.cluster.MemberId) CompletableFuture(java.util.concurrent.CompletableFuture)

Aggregations

MemberId (io.atomix.cluster.MemberId)24 CompletableFuture (java.util.concurrent.CompletableFuture)10 ArrayList (java.util.ArrayList)9 List (java.util.List)7 Consumer (java.util.function.Consumer)7 Collectors (java.util.stream.Collectors)7 PrimitiveType (io.atomix.primitive.PrimitiveType)6 Futures (io.atomix.utils.concurrent.Futures)6 SessionId (io.atomix.primitive.session.SessionId)5 RaftMember (io.atomix.protocols.raft.cluster.RaftMember)5 Configuration (io.atomix.protocols.raft.storage.system.Configuration)5 Instant (java.time.Instant)5 Collections (java.util.Collections)5 Map (java.util.Map)5 TestClusterMembershipService (io.atomix.cluster.TestClusterMembershipService)4 PrimitiveBuilder (io.atomix.primitive.PrimitiveBuilder)4 PrimitiveManagementService (io.atomix.primitive.PrimitiveManagementService)4 PrimitiveConfig (io.atomix.primitive.config.PrimitiveConfig)4 PartitionId (io.atomix.primitive.partition.PartitionId)4 AbstractPrimitiveService (io.atomix.primitive.service.AbstractPrimitiveService)4