Search in sources :

Example 1 with LeaderDemotedException

use of com.hazelcast.cp.exception.LeaderDemotedException in project hazelcast by hazelcast.

the class RaftNodeImpl method invalidateFuturesFrom.

/**
 * Invalidates futures registered with indexes {@code >= entryIndex}. Note that {@code entryIndex} is inclusive.
 * {@link LeaderDemotedException} is set a result to futures.
 */
public void invalidateFuturesFrom(long entryIndex) {
    int count = 0;
    Iterator<Entry<Long, InternalCompletableFuture>> iterator = futures.entrySet().iterator();
    while (iterator.hasNext()) {
        Entry<Long, InternalCompletableFuture> entry = iterator.next();
        long index = entry.getKey();
        if (index >= entryIndex) {
            entry.getValue().completeExceptionally(new LeaderDemotedException(state.localEndpoint(), state.leader()));
            iterator.remove();
            count++;
        }
    }
    if (count > 0) {
        logger.warning("Invalidated " + count + " futures from log index: " + entryIndex);
    }
}
Also used : LeaderDemotedException(com.hazelcast.cp.exception.LeaderDemotedException) Entry(java.util.Map.Entry) LogEntry(com.hazelcast.cp.internal.raft.impl.log.LogEntry) SnapshotEntry(com.hazelcast.cp.internal.raft.impl.log.SnapshotEntry) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture)

Example 2 with LeaderDemotedException

use of com.hazelcast.cp.exception.LeaderDemotedException in project hazelcast by hazelcast.

the class RaftNodeImpl method toFollower.

/**
 * Switches this node to follower role by clearing the known leader
 * endpoint and (pre) candidate states, and updating the term. If this Raft
 * node was leader before switching to the follower state, it may have some
 * queries waiting to be executed. Those queries are also failed with
 * {@link LeaderDemotedException}. After the state switch,
 * {@link RaftIntegration#onNodeStatusChange(RaftNodeStatus)} is called.
 *
 * @param term the new term to switch
 */
public void toFollower(int term) {
    LeaderState leaderState = state.leaderState();
    if (leaderState != null) {
        for (BiTuple<Object, InternalCompletableFuture> t : leaderState.queryState().operations()) {
            t.element2.completeExceptionally(new LeaderDemotedException(state.localEndpoint(), null));
        }
    }
    state.toFollower(term);
    printMemberState();
}
Also used : LeaderDemotedException(com.hazelcast.cp.exception.LeaderDemotedException) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) LeaderState(com.hazelcast.cp.internal.raft.impl.state.LeaderState)

Example 3 with LeaderDemotedException

use of com.hazelcast.cp.exception.LeaderDemotedException in project hazelcast by hazelcast.

the class RaftNodeImpl method forceSetTerminatedStatus.

@Override
public InternalCompletableFuture forceSetTerminatedStatus() {
    InternalCompletableFuture resultFuture = raftIntegration.newCompletableFuture();
    if (isTerminatedOrSteppedDown()) {
        if (logger.isFineEnabled()) {
            logger.fine("Already stepped down or terminated, not setting `TERMINATED` status.");
        }
        resultFuture.complete(null);
        return resultFuture;
    }
    execute(() -> {
        Throwable failure = null;
        try {
            if (isTerminatedOrSteppedDown()) {
                return;
            } else if (status == INITIAL) {
                setStatus(TERMINATED);
                return;
            }
            invalidateFuturesFrom(state.commitIndex() + 1);
            LeaderState leaderState = state.leaderState();
            if (leaderState != null) {
                for (BiTuple<Object, InternalCompletableFuture> t : leaderState.queryState().operations()) {
                    t.element2.completeExceptionally(new LeaderDemotedException(state.localEndpoint(), null));
                }
            }
            state.completeLeadershipTransfer(new LeaderDemotedException(state.localEndpoint(), null));
            setStatus(TERMINATED);
        } catch (Throwable t) {
            failure = t;
            logger.severe("Failure during force-termination", t);
            if (status != TERMINATED && status != STEPPED_DOWN) {
                setStatus(TERMINATED);
            }
        } finally {
            if (failure == null) {
                resultFuture.complete(null);
            } else {
                resultFuture.completeExceptionally(failure);
            }
        }
    });
    return resultFuture;
}
Also used : LeaderDemotedException(com.hazelcast.cp.exception.LeaderDemotedException) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) LeaderState(com.hazelcast.cp.internal.raft.impl.state.LeaderState)

Example 4 with LeaderDemotedException

use of com.hazelcast.cp.exception.LeaderDemotedException in project hazelcast by hazelcast.

the class LinearizableQueryTest method when_leaderDemotesToFollowerWhileThereIsOngoingQuery_then_queryFails.

@Test(timeout = 300_000)
public void when_leaderDemotesToFollowerWhileThereIsOngoingQuery_then_queryFails() throws Exception {
    group = newGroup();
    group.start();
    RaftNodeImpl oldLeader = group.waitUntilLeaderElected();
    final int[] split = group.createMajoritySplitIndexes(false);
    group.split(split);
    InternalCompletableFuture queryFuture = oldLeader.query(new QueryRaftRunnable(), LINEARIZABLE);
    assertTrueEventually(() -> {
        for (int ix : split) {
            RaftEndpoint newLeader = getLeaderMember(group.getNode(ix));
            assertNotNull(newLeader);
            assertNotEquals(oldLeader.getLocalMember(), newLeader);
        }
    });
    RaftNodeImpl newLeader = group.getNode(getLeaderMember(group.getNode(split[0])));
    newLeader.replicate(new ApplyRaftRunnable("value1")).get();
    group.merge();
    group.waitUntilLeaderElected();
    assertEquals(oldLeader.getLeader(), newLeader.getLocalMember());
    try {
        queryFuture.joinInternal();
        fail();
    } catch (LeaderDemotedException ignored) {
    }
}
Also used : LeaderDemotedException(com.hazelcast.cp.exception.LeaderDemotedException) ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) QueryRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.QueryRaftRunnable) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 5 with LeaderDemotedException

use of com.hazelcast.cp.exception.LeaderDemotedException in project hazelcast by hazelcast.

the class LocalRaftTest method when_leaderStaysInMinorityDuringSplit_then_itCannotCommitNewEntries.

@Test
public void when_leaderStaysInMinorityDuringSplit_then_itCannotCommitNewEntries() throws ExecutionException, InterruptedException {
    group = newGroup(3, newRaftConfigWithNoSnapshotting(100));
    group.start();
    RaftNodeImpl leader = group.waitUntilLeaderElected();
    leader.replicate(new ApplyRaftRunnable("val1")).get();
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : group.getNodes()) {
            assertEquals(1, getCommitIndex(raftNode));
        }
    });
    RaftNodeImpl[] followers = group.getNodesExcept(leader.getLocalMember());
    group.split(leader.getLocalMember());
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : followers) {
            RaftEndpoint leaderEndpoint = getLeaderMember(raftNode);
            assertNotNull(leaderEndpoint);
            assertNotEquals(leader.getLocalMember(), leaderEndpoint);
        }
    });
    List<InternalCompletableFuture> isolatedFutures = new ArrayList<>();
    for (int i = 0; i < 10; i++) {
        isolatedFutures.add(leader.replicate(new ApplyRaftRunnable("isolated" + i)));
    }
    RaftNodeImpl newLeader = group.getNode(getLeaderMember(followers[0]));
    for (int i = 0; i < 10; i++) {
        newLeader.replicate(new ApplyRaftRunnable("valNew" + i)).get();
    }
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : followers) {
            assertEquals(11, getCommitIndex(raftNode));
        }
    });
    group.merge();
    RaftNodeImpl finalLeader = group.waitUntilLeaderElected();
    assertNotEquals(leader.getLocalMember(), finalLeader.getLocalMember());
    for (InternalCompletableFuture f : isolatedFutures) {
        try {
            f.joinInternal();
            fail();
        } catch (LeaderDemotedException ignored) {
        }
    }
    assertTrueEventually(() -> {
        for (RaftNodeImpl raftNode : followers) {
            RaftDataService service = group.getService(raftNode);
            assertEquals(11, service.size());
            assertEquals("val1", service.get(1));
            for (int i = 0; i < 10; i++) {
                assertEquals(("valNew" + i), service.get(i + 2));
            }
        }
    });
}
Also used : LeaderDemotedException(com.hazelcast.cp.exception.LeaderDemotedException) ApplyRaftRunnable(com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable) RaftDataService(com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService) InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) ArrayList(java.util.ArrayList) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Aggregations

LeaderDemotedException (com.hazelcast.cp.exception.LeaderDemotedException)5 InternalCompletableFuture (com.hazelcast.spi.impl.InternalCompletableFuture)5 ApplyRaftRunnable (com.hazelcast.cp.internal.raft.impl.dataservice.ApplyRaftRunnable)2 LeaderState (com.hazelcast.cp.internal.raft.impl.state.LeaderState)2 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)2 QuickTest (com.hazelcast.test.annotation.QuickTest)2 Test (org.junit.Test)2 QueryRaftRunnable (com.hazelcast.cp.internal.raft.impl.dataservice.QueryRaftRunnable)1 RaftDataService (com.hazelcast.cp.internal.raft.impl.dataservice.RaftDataService)1 LogEntry (com.hazelcast.cp.internal.raft.impl.log.LogEntry)1 SnapshotEntry (com.hazelcast.cp.internal.raft.impl.log.SnapshotEntry)1 ArrayList (java.util.ArrayList)1 Entry (java.util.Map.Entry)1