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