Search in sources :

Example 51 with RaftEndpoint

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;
}
Also used : RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint)

Example 52 with RaftEndpoint

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;
}
Also used : RaftNodeImpl(com.hazelcast.cp.internal.raft.impl.RaftNodeImpl) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) RaftStateStore(com.hazelcast.cp.internal.raft.impl.persistence.RaftStateStore) NopRaftStateStore(com.hazelcast.cp.internal.raft.impl.persistence.NopRaftStateStore) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint)

Example 53 with RaftEndpoint

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));
            });
        }
    }
}
Also used : RaftNodeImpl(com.hazelcast.cp.internal.raft.impl.RaftNodeImpl) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) ArrayList(java.util.ArrayList) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) CPGroupId(com.hazelcast.cp.CPGroupId) CPGroup(com.hazelcast.cp.CPGroup) HazelcastInstance(com.hazelcast.core.HazelcastInstance) CreateRaftGroupOp(com.hazelcast.cp.internal.raftop.metadata.CreateRaftGroupOp) HashSet(java.util.HashSet) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 54 with RaftEndpoint

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);
}
Also used : RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 55 with RaftEndpoint

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());
}
Also used : RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) RaftEndpoint(com.hazelcast.cp.internal.raft.impl.RaftEndpoint) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

RaftEndpoint (com.hazelcast.cp.internal.raft.impl.RaftEndpoint)57 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)14 QuickTest (com.hazelcast.test.annotation.QuickTest)14 Test (org.junit.Test)14 RaftNodeImpl (com.hazelcast.cp.internal.raft.impl.RaftNodeImpl)8 RaftState (com.hazelcast.cp.internal.raft.impl.state.RaftState)7 ArrayList (java.util.ArrayList)7 CPMember (com.hazelcast.cp.CPMember)6 RaftLog (com.hazelcast.cp.internal.raft.impl.log.RaftLog)6 UUID (java.util.UUID)6 HazelcastInstance (com.hazelcast.core.HazelcastInstance)5 CPGroupId (com.hazelcast.cp.CPGroupId)5 TestRaftEndpoint (com.hazelcast.cp.internal.raft.impl.testing.TestRaftEndpoint)5 LinkedHashSet (java.util.LinkedHashSet)4 LogEntry (com.hazelcast.cp.internal.raft.impl.log.LogEntry)3 CPSubsystemException (com.hazelcast.cp.exception.CPSubsystemException)2 NotLeaderException (com.hazelcast.cp.exception.NotLeaderException)2 CannotCreateRaftGroupException (com.hazelcast.cp.internal.exception.CannotCreateRaftGroupException)2 RaftStateStore (com.hazelcast.cp.internal.raft.impl.persistence.RaftStateStore)2 LeaderState (com.hazelcast.cp.internal.raft.impl.state.LeaderState)2