use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.
the class LocalRaftGroup method getFollowerEndpoints.
public RaftEndpoint[] getFollowerEndpoints() {
RaftEndpoint leaderEndpoint = getLeaderEndpoint();
RaftEndpoint[] n = new RaftEndpoint[members.length - 1];
int i = 0;
for (RaftEndpoint member : members) {
if (!member.equals(leaderEndpoint)) {
n[i++] = member;
}
}
if (i != n.length) {
throw new IllegalArgumentException();
}
return n;
}
use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.
the class LocalRaftGroup method createNewRaftNode.
public RaftNodeImpl createNewRaftNode() {
int oldSize = this.integrations.length;
int newSize = oldSize + 1;
RaftEndpoint[] endpoints = new RaftEndpoint[newSize];
LocalRaftIntegration[] integrations = new LocalRaftIntegration[newSize];
RaftNodeImpl[] nodes = new RaftNodeImpl[newSize];
System.arraycopy(this.members, 0, endpoints, 0, oldSize);
System.arraycopy(this.integrations, 0, integrations, 0, oldSize);
System.arraycopy(this.nodes, 0, nodes, 0, oldSize);
LocalRaftIntegration integration = createNewLocalRaftIntegration();
createdNodeCount++;
integrations[oldSize] = integration;
RaftEndpoint endpoint = integration.getLocalEndpoint();
endpoints[oldSize] = endpoint;
RaftStateStore raftStateStore = raftStateStoreFactory.apply(endpoint, raftAlgorithmConfig);
RaftNodeImpl node = newRaftNode(groupId, endpoint, asList(initialMembers), raftAlgorithmConfig, integration, raftStateStore);
nodes[oldSize] = node;
this.members = endpoints;
this.integrations = integrations;
this.nodes = nodes;
node.start();
initDiscovery();
return node;
}
use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.
the class MetadataRaftGroupTest method when_raftGroupIsCreated_onNonMetadataMembers_thenLeaderShouldBeElected.
@Test
public void when_raftGroupIsCreated_onNonMetadataMembers_thenLeaderShouldBeElected() throws ExecutionException, InterruptedException {
int metadataGroupSize = 3;
int otherRaftGroupSize = 2;
instances = newInstances(metadataGroupSize + otherRaftGroupSize, metadataGroupSize, 0);
HazelcastInstance leaderInstance = getLeaderInstance(instances, getMetadataGroupId(instances[0]));
RaftService raftService = getRaftService(leaderInstance);
Collection<CPMemberInfo> allEndpoints = raftService.getMetadataGroupManager().getActiveMembers();
assertTrueEventually(() -> assertNotNull(raftService.getMetadataGroupManager().getGroup(getMetadataGroupId(leaderInstance))));
CPGroup metadataGroup = raftService.getMetadataGroupManager().getGroup(getMetadataGroupId(leaderInstance));
Collection<CPMemberInfo> endpoints = new HashSet<>(otherRaftGroupSize);
for (CPMemberInfo endpoint : allEndpoints) {
if (!metadataGroup.members().contains(endpoint)) {
endpoints.add(endpoint);
}
}
assertEquals(otherRaftGroupSize, endpoints.size());
List<RaftEndpoint> groupEndpoints = new ArrayList<>();
for (CPMemberInfo member : endpoints) {
groupEndpoints.add(member.toRaftEndpoint());
}
RaftOp op = new CreateRaftGroupOp("test", groupEndpoints, RandomPicker.getInt(Integer.MAX_VALUE));
InternalCompletableFuture<CPGroupSummary> f = raftService.getInvocationManager().invoke(getMetadataGroupId(leaderInstance), op);
f.whenCompleteAsync((group, t) -> {
if (t == null) {
raftService.getInvocationManager().triggerRaftNodeCreation(group);
}
});
CPGroupId groupId = f.get().id();
for (HazelcastInstance instance : instances) {
if (endpoints.contains(instance.getCPSubsystem().getLocalCPMember())) {
assertTrueEventually(() -> {
RaftNodeImpl raftNode = getRaftNode(instance, groupId);
assertNotNull(raftNode);
assertNotNull("Leader is null on " + raftNode, getLeaderMember(raftNode));
});
}
}
}
use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.
the class RaftLogTest method setSnapshot_multipleTimes.
@Test
public void setSnapshot_multipleTimes() {
LogEntry[] entries = new LogEntry[] { new LogEntry(1, 1, null), new LogEntry(1, 2, null), new LogEntry(1, 3, null), new LogEntry(1, 4, null), new LogEntry(1, 5, null) };
log.appendEntries(entries);
int truncated = log.setSnapshot(new SnapshotEntry(1, 2, null, 0, Collections.<RaftEndpoint>emptySet()));
assertEquals(2, truncated);
for (int i = 1; i <= 2; i++) {
assertFalse(log.containsLogEntry(i));
assertNull(log.getLogEntry(i));
}
for (int i = 3; i <= 5; i++) {
assertTrue(log.containsLogEntry(i));
assertNotNull(log.getLogEntry(i));
}
Object snapshot = new Object();
truncated = log.setSnapshot(new SnapshotEntry(1, 4, snapshot, 0, Collections.<RaftEndpoint>emptySet()));
assertEquals(2, truncated);
for (int i = 1; i <= 4; i++) {
assertFalse(log.containsLogEntry(i));
assertNull(log.getLogEntry(i));
}
assertTrue(log.containsLogEntry(5));
assertNotNull(log.getLogEntry(5));
LogEntry lastLogEntry = log.lastLogOrSnapshotEntry();
assertEquals(5, lastLogEntry.index());
assertEquals(1, lastLogEntry.term());
assertSame(lastLogEntry, log.getLogEntry(lastLogEntry.index()));
assertEquals(log.lastLogOrSnapshotIndex(), 5);
assertEquals(log.lastLogOrSnapshotTerm(), 1);
assertEquals(log.snapshotIndex(), 4);
LogEntry snapshotEntry = log.snapshot();
assertEquals(4, snapshotEntry.index());
assertEquals(1, snapshotEntry.term());
assertEquals(snapshotEntry.operation(), snapshot);
}
use of com.hazelcast.cp.internal.raft.impl.RaftEndpoint in project hazelcast by hazelcast.
the class RaftLogTest method setSnapshot.
@Test
public void setSnapshot() {
LogEntry[] entries = new LogEntry[] { new LogEntry(1, 1, null), new LogEntry(1, 2, null), new LogEntry(1, 3, null), new LogEntry(1, 4, null), new LogEntry(1, 5, null) };
log.appendEntries(entries);
int truncated = log.setSnapshot(new SnapshotEntry(1, 3, null, 0, Collections.<RaftEndpoint>emptySet()));
assertEquals(3, truncated);
for (int i = 1; i <= 3; i++) {
assertFalse(log.containsLogEntry(i));
assertNull(log.getLogEntry(i));
}
for (int i = 4; i <= 5; i++) {
assertTrue(log.containsLogEntry(i));
assertNotNull(log.getLogEntry(i));
}
LogEntry lastLogEntry = log.lastLogOrSnapshotEntry();
assertEquals(5, lastLogEntry.index());
assertEquals(1, lastLogEntry.term());
assertSame(lastLogEntry, log.getLogEntry(lastLogEntry.index()));
assertEquals(log.lastLogOrSnapshotIndex(), 5);
assertEquals(log.lastLogOrSnapshotTerm(), 1);
assertEquals(log.snapshotIndex(), 3);
LogEntry snapshot = log.snapshot();
assertEquals(3, snapshot.index());
assertEquals(1, snapshot.term());
}
Aggregations