Search in sources :

Example 1 with MemberId

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

the class RaftFuzzTest method createClient.

/**
 * Creates a Raft client.
 */
private RaftClient createClient() throws Exception {
    MemberId memberId = nextNodeId();
    RaftClientProtocol protocol;
    if (USE_NETTY) {
        Address address = Address.from(++port);
        MessagingService messagingManager = new NettyMessagingService("test", address, new MessagingConfig()).start().join();
        addressMap.put(memberId, address);
        protocol = new RaftClientMessagingProtocol(messagingManager, PROTOCOL_SERIALIZER, addressMap::get);
    } else {
        protocol = protocolFactory.newClientProtocol(memberId);
    }
    RaftClient client = RaftClient.builder().withMemberId(memberId).withProtocol(protocol).build();
    client.connect(members.stream().map(RaftMember::memberId).collect(Collectors.toList())).join();
    clients.add(client);
    return client;
}
Also used : MessagingConfig(io.atomix.cluster.messaging.MessagingConfig) MemberId(io.atomix.cluster.MemberId) RaftClientProtocol(io.atomix.protocols.raft.protocol.RaftClientProtocol) Address(io.atomix.utils.net.Address) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService) RaftClientMessagingProtocol(io.atomix.protocols.raft.test.protocol.RaftClientMessagingProtocol) RaftClient(io.atomix.protocols.raft.RaftClient) MessagingService(io.atomix.cluster.messaging.MessagingService) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService)

Example 2 with MemberId

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

the class RaftFuzzTest method scheduleRestart.

/**
 * Schedules the given server to be shutdown for a period of time and then restarted.
 */
private void scheduleRestart(boolean remove, int serverIndex, ThreadContext context) {
    shutdownTimers.put(serverIndex, context.schedule(Duration.ofSeconds(randomNumber(120) + 10), () -> {
        shutdownTimers.remove(serverIndex);
        RaftServer server = servers.get(serverIndex);
        CompletableFuture<Void> leaveFuture;
        if (remove) {
            System.out.println("Removing server: " + server.cluster().getMember().memberId());
            leaveFuture = server.leave();
        } else {
            System.out.println("Shutting down server: " + server.cluster().getMember().memberId());
            leaveFuture = server.shutdown();
        }
        leaveFuture.whenComplete((result, error) -> {
            restartTimers.put(serverIndex, context.schedule(Duration.ofSeconds(randomNumber(120) + 10), () -> {
                restartTimers.remove(serverIndex);
                RaftServer newServer = createServer(server.cluster().getMember());
                servers.set(serverIndex, newServer);
                CompletableFuture<RaftServer> joinFuture;
                if (remove) {
                    System.out.println("Adding server: " + newServer.cluster().getMember().memberId());
                    joinFuture = newServer.join(members.get(members.size() - 1).memberId());
                } else {
                    System.out.println("Bootstrapping server: " + newServer.cluster().getMember().memberId());
                    joinFuture = newServer.bootstrap(members.stream().map(RaftMember::memberId).collect(Collectors.toList()));
                }
                joinFuture.whenComplete((result2, error2) -> {
                    scheduleRestarts(context);
                });
            }));
        });
    }));
}
Also used : ServiceExecutor(io.atomix.primitive.service.ServiceExecutor) Address(io.atomix.utils.net.Address) JoinRequest(io.atomix.protocols.raft.protocol.JoinRequest) MetadataRequest(io.atomix.protocols.raft.protocol.MetadataRequest) PrimitiveOperation.operation(io.atomix.primitive.operation.PrimitiveOperation.operation) DefaultOperationId(io.atomix.primitive.operation.impl.DefaultOperationId) PrimitiveConfig(io.atomix.primitive.config.PrimitiveConfig) Duration(java.time.Duration) Map(java.util.Map) VoteResponse(io.atomix.protocols.raft.protocol.VoteResponse) AppendRequest(io.atomix.protocols.raft.protocol.AppendRequest) Path(java.nio.file.Path) ReconfigureRequest(io.atomix.protocols.raft.protocol.ReconfigureRequest) QueryRequest(io.atomix.protocols.raft.protocol.QueryRequest) InitializeEntry(io.atomix.protocols.raft.storage.log.entry.InitializeEntry) PrimitiveBuilder(io.atomix.primitive.PrimitiveBuilder) RaftStorage(io.atomix.protocols.raft.storage.RaftStorage) ResetRequest(io.atomix.protocols.raft.protocol.ResetRequest) CommandResponse(io.atomix.protocols.raft.protocol.CommandResponse) SingleThreadContext(io.atomix.utils.concurrent.SingleThreadContext) FileVisitResult(java.nio.file.FileVisitResult) CountDownLatch(java.util.concurrent.CountDownLatch) KeepAliveRequest(io.atomix.protocols.raft.protocol.KeepAliveRequest) ServiceConfig(io.atomix.primitive.service.ServiceConfig) ConfigureRequest(io.atomix.protocols.raft.protocol.ConfigureRequest) KeepAliveEntry(io.atomix.protocols.raft.storage.log.entry.KeepAliveEntry) MetadataEntry(io.atomix.protocols.raft.storage.log.entry.MetadataEntry) Serializer(io.atomix.utils.serializer.Serializer) KeepAliveResponse(io.atomix.protocols.raft.protocol.KeepAliveResponse) QueryEntry(io.atomix.protocols.raft.storage.log.entry.QueryEntry) BackupInput(io.atomix.primitive.service.BackupInput) RaftClient(io.atomix.protocols.raft.RaftClient) JoinResponse(io.atomix.protocols.raft.protocol.JoinResponse) OperationType(io.atomix.primitive.operation.OperationType) CloseSessionRequest(io.atomix.protocols.raft.protocol.CloseSessionRequest) ArrayList(java.util.ArrayList) StorageLevel(io.atomix.storage.StorageLevel) OperationId(io.atomix.primitive.operation.OperationId) PrimitiveOperation(io.atomix.primitive.operation.PrimitiveOperation) OpenSessionEntry(io.atomix.protocols.raft.storage.log.entry.OpenSessionEntry) AppendResponse(io.atomix.protocols.raft.protocol.AppendResponse) PollResponse(io.atomix.protocols.raft.protocol.PollResponse) Commit(io.atomix.primitive.service.Commit) Files(java.nio.file.Files) MessagingService(io.atomix.cluster.messaging.MessagingService) OpenSessionResponse(io.atomix.protocols.raft.protocol.OpenSessionResponse) IOException(java.io.IOException) File(java.io.File) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbstractPrimitiveService(io.atomix.primitive.service.AbstractPrimitiveService) QueryResponse(io.atomix.protocols.raft.protocol.QueryResponse) Paths(java.nio.file.Paths) PublishRequest(io.atomix.protocols.raft.protocol.PublishRequest) RaftMember(io.atomix.protocols.raft.cluster.RaftMember) RaftServerMessagingProtocol(io.atomix.protocols.raft.test.protocol.RaftServerMessagingProtocol) Random(java.util.Random) MessagingConfig(io.atomix.cluster.messaging.MessagingConfig) VoteRequest(io.atomix.protocols.raft.protocol.VoteRequest) CommandRequest(io.atomix.protocols.raft.protocol.CommandRequest) LeaveRequest(io.atomix.protocols.raft.protocol.LeaveRequest) SessionId(io.atomix.primitive.session.SessionId) ConfigureResponse(io.atomix.protocols.raft.protocol.ConfigureResponse) PrimitiveManagementService(io.atomix.primitive.PrimitiveManagementService) RaftResponse(io.atomix.protocols.raft.protocol.RaftResponse) LocalRaftProtocolFactory(io.atomix.protocols.raft.test.protocol.LocalRaftProtocolFactory) InstallRequest(io.atomix.protocols.raft.protocol.InstallRequest) NettyMessagingService(io.atomix.cluster.messaging.impl.NettyMessagingService) RaftClientMessagingProtocol(io.atomix.protocols.raft.test.protocol.RaftClientMessagingProtocol) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) ReconfigureResponse(io.atomix.protocols.raft.protocol.ReconfigureResponse) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) UUID(java.util.UUID) ReadConsistency(io.atomix.protocols.raft.ReadConsistency) Instant(java.time.Instant) CommandEntry(io.atomix.protocols.raft.storage.log.entry.CommandEntry) Collectors(java.util.stream.Collectors) ThreadContext(io.atomix.utils.concurrent.ThreadContext) CloseSessionResponse(io.atomix.protocols.raft.protocol.CloseSessionResponse) MetadataResponse(io.atomix.protocols.raft.protocol.MetadataResponse) List(java.util.List) PrimitiveService(io.atomix.primitive.service.PrimitiveService) BackupOutput(io.atomix.primitive.service.BackupOutput) InstallResponse(io.atomix.protocols.raft.protocol.InstallResponse) RaftServerProtocol(io.atomix.protocols.raft.protocol.RaftServerProtocol) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) RaftServer(io.atomix.protocols.raft.RaftServer) RaftClientProtocol(io.atomix.protocols.raft.protocol.RaftClientProtocol) HashSet(java.util.HashSet) ConfigurationEntry(io.atomix.protocols.raft.storage.log.entry.ConfigurationEntry) DefaultRaftMember(io.atomix.protocols.raft.cluster.impl.DefaultRaftMember) MemberId(io.atomix.cluster.MemberId) RaftError(io.atomix.protocols.raft.RaftError) Scheduled(io.atomix.utils.concurrent.Scheduled) PollRequest(io.atomix.protocols.raft.protocol.PollRequest) Scheduler(io.atomix.utils.concurrent.Scheduler) CloseSessionEntry(io.atomix.protocols.raft.storage.log.entry.CloseSessionEntry) CommunicationStrategy(io.atomix.protocols.raft.session.CommunicationStrategy) LeaveResponse(io.atomix.protocols.raft.protocol.LeaveResponse) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) Maps(com.google.common.collect.Maps) OpenSessionRequest(io.atomix.protocols.raft.protocol.OpenSessionRequest) Configuration(io.atomix.protocols.raft.storage.system.Configuration) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Namespace(io.atomix.utils.serializer.Namespace) PrimitiveType(io.atomix.primitive.PrimitiveType) SessionClient(io.atomix.primitive.session.SessionClient) Collections(java.util.Collections) CompletableFuture(java.util.concurrent.CompletableFuture) RaftServer(io.atomix.protocols.raft.RaftServer)

Example 3 with MemberId

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

the class PrimaryBackupTest method createClient.

/**
 * Creates a Raft client.
 */
private PrimaryBackupClient createClient() throws Throwable {
    MemberId memberId = nextMemberId();
    PrimaryBackupClient client = PrimaryBackupClient.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 4 with MemberId

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

the class PrimaryBackupTest 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);
    nodes = new ArrayList<>();
    memberId = 0;
    sessionId = 0;
    clients = new ArrayList<>();
    servers = new ArrayList<>();
    protocolFactory = new TestPrimaryBackupProtocolFactory();
    election = new TestPrimaryElection(PartitionId.from("test", 1));
}
Also used : BackupInput(io.atomix.primitive.service.BackupInput) ServiceExecutor(io.atomix.primitive.service.ServiceExecutor) DefaultSerializers(io.atomix.utils.serializer.serializers.DefaultSerializers) TimeoutException(java.util.concurrent.TimeoutException) CompletableFuture(java.util.concurrent.CompletableFuture) Role(io.atomix.protocols.backup.PrimaryBackupServer.Role) EventType(io.atomix.primitive.event.EventType) TestPrimaryBackupProtocolFactory(io.atomix.protocols.backup.protocol.TestPrimaryBackupProtocolFactory) MemberGroupStrategy(io.atomix.primitive.partition.MemberGroupStrategy) PartitionId(io.atomix.primitive.partition.PartitionId) ArrayList(java.util.ArrayList) SessionId(io.atomix.primitive.session.SessionId) OperationId(io.atomix.primitive.operation.OperationId) PrimitiveOperation.operation(io.atomix.primitive.operation.PrimitiveOperation.operation) PrimitiveManagementService(io.atomix.primitive.PrimitiveManagementService) PrimitiveConfig(io.atomix.primitive.config.PrimitiveConfig) After(org.junit.After) Duration(java.time.Duration) TestClusterMembershipService(io.atomix.cluster.TestClusterMembershipService) MemberId(io.atomix.cluster.MemberId) Session(io.atomix.primitive.session.Session) Futures(io.atomix.utils.concurrent.Futures) ConcurrentTestCase(net.jodah.concurrentunit.ConcurrentTestCase) Before(org.junit.Before) PrimitiveBuilder(io.atomix.primitive.PrimitiveBuilder) Commit(io.atomix.primitive.service.Commit) TestPrimaryElection(io.atomix.primitive.partition.TestPrimaryElection) Test(org.junit.Test) Collectors(java.util.stream.Collectors) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) PrimitiveService(io.atomix.primitive.service.PrimitiveService) AbstractPrimitiveService(io.atomix.primitive.service.AbstractPrimitiveService) Ignore(org.junit.Ignore) ServiceConfig(io.atomix.primitive.service.ServiceConfig) PrimitiveType(io.atomix.primitive.PrimitiveType) PrimaryElection(io.atomix.primitive.partition.PrimaryElection) Replication(io.atomix.primitive.Replication) BackupOutput(io.atomix.primitive.service.BackupOutput) SessionClient(io.atomix.primitive.session.SessionClient) Serializer(io.atomix.utils.serializer.Serializer) Assert.assertEquals(org.junit.Assert.assertEquals) TestPrimaryElection(io.atomix.primitive.partition.TestPrimaryElection) TestPrimaryBackupProtocolFactory(io.atomix.protocols.backup.protocol.TestPrimaryBackupProtocolFactory) Before(org.junit.Before) After(org.junit.After)

Example 5 with MemberId

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

the class RaftPartitionGroup method buildPartitions.

private Collection<PartitionMetadata> buildPartitions() {
    List<MemberId> sorted = new ArrayList<>(config.getMembers().stream().map(MemberId::from).collect(Collectors.toSet()));
    Collections.sort(sorted);
    int partitionSize = this.partitionSize;
    if (partitionSize == 0) {
        partitionSize = sorted.size();
    }
    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<MemberId> 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 : MemberId(io.atomix.cluster.MemberId) ArrayList(java.util.ArrayList) PartitionMetadata(io.atomix.primitive.partition.PartitionMetadata) PartitionId(io.atomix.primitive.partition.PartitionId) HashSet(java.util.HashSet)

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