Search in sources :

Example 66 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class TestPeerProxyMap method testCloseDeadLock.

@Test(timeout = 10_000)
public void testCloseDeadLock() throws Exception {
    final PeerProxyMap<DummyProxy> map = new PeerProxyMap<>("test", DummyProxy::new);
    final RaftPeerId id = RaftPeerId.valueOf("s0");
    final RaftPeer peer = RaftPeer.newBuilder().setId(id).build();
    map.computeIfAbsent(peer);
    final DummyProxy proxy = map.getProxy(id);
    final Thread t = new Thread(() -> {
        // hold proxy lock and then getProxy(..) which requires resetLock
        synchronized (proxy) {
            LOG.info("Acquired lock");
            try {
                HUNDRED_MILLIS.sleep();
                LOG.info("Try getProxy");
                final DummyProxy newProxy = map.getProxy(id);
                Assert.assertNotSame(proxy, newProxy);
            } catch (Exception e) {
                setFirstException(e);
            }
            LOG.info("Will release lock");
        }
    });
    t.start();
    // hold resetLock and then call close() with requires proxy lock
    map.resetProxy(id);
    t.join();
}
Also used : RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftPeer(org.apache.ratis.protocol.RaftPeer) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 67 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class Server method run.

@Override
public void run() throws Exception {
    RaftPeerId peerId = RaftPeerId.valueOf(id);
    RaftProperties properties = new RaftProperties();
    final int port = NetUtils.createSocketAddr(getPeer(peerId).getAddress()).getPort();
    GrpcConfigKeys.Server.setPort(properties, port);
    Optional.ofNullable(getPeer(peerId).getClientAddress()).ifPresent(address -> GrpcConfigKeys.Client.setPort(properties, NetUtils.createSocketAddr(address).getPort()));
    Optional.ofNullable(getPeer(peerId).getAdminAddress()).ifPresent(address -> GrpcConfigKeys.Admin.setPort(properties, NetUtils.createSocketAddr(address).getPort()));
    RaftServerConfigKeys.setStorageDir(properties, Collections.singletonList(storageDir));
    StateMachine stateMachine = new ArithmeticStateMachine();
    final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(getRaftGroupId())), getPeers());
    RaftServer raftServer = RaftServer.newBuilder().setServerId(RaftPeerId.valueOf(id)).setStateMachine(stateMachine).setProperties(properties).setGroup(raftGroup).build();
    raftServer.start();
    for (; raftServer.getLifeCycleState() != LifeCycle.State.CLOSED; ) {
        TimeUnit.SECONDS.sleep(1);
    }
}
Also used : StateMachine(org.apache.ratis.statemachine.StateMachine) ArithmeticStateMachine(org.apache.ratis.examples.arithmetic.ArithmeticStateMachine) RaftServer(org.apache.ratis.server.RaftServer) RaftProperties(org.apache.ratis.conf.RaftProperties) ArithmeticStateMachine(org.apache.ratis.examples.arithmetic.ArithmeticStateMachine) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftGroup(org.apache.ratis.protocol.RaftGroup)

Example 68 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class LeaderStateImpl method newAppendEntriesRequestProto.

@Override
public AppendEntriesRequestProto newAppendEntriesRequestProto(FollowerInfo follower, List<LogEntryProto> entries, TermIndex previous, long callId) {
    final boolean initializing = isAttendingVote(follower);
    final RaftPeerId targetId = follower.getPeer().getId();
    return ServerProtoUtils.toAppendEntriesRequestProto(server.getMemberId(), targetId, currentTerm, entries, ServerImplUtils.effectiveCommitIndex(raftLog.getLastCommittedIndex(), previous, entries.size()), initializing, previous, server.getCommitInfos(), callId);
}
Also used : RaftPeerId(org.apache.ratis.protocol.RaftPeerId)

Example 69 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class LeaderStateImpl method getMajorityMin.

private Optional<MinMajorityMax> getMajorityMin(ToLongFunction<FollowerInfo> followerIndex, LongSupplier logIndex, long gapThreshold) {
    final RaftPeerId selfId = server.getId();
    final RaftConfigurationImpl conf = server.getRaftConf();
    final List<RaftPeerId> followers = voterLists.get(0);
    final boolean includeSelf = conf.containsInConf(selfId);
    if (followers.isEmpty() && !includeSelf) {
        return Optional.empty();
    }
    final long[] indicesInNewConf = getSorted(followers, includeSelf, followerIndex, logIndex);
    final MinMajorityMax newConf = MinMajorityMax.valueOf(indicesInNewConf, gapThreshold);
    if (!conf.isTransitional()) {
        return Optional.of(newConf);
    } else {
        // configuration is in transitional state
        final List<RaftPeerId> oldFollowers = voterLists.get(1);
        final boolean includeSelfInOldConf = conf.containsInOldConf(selfId);
        if (oldFollowers.isEmpty() && !includeSelfInOldConf) {
            return Optional.empty();
        }
        final long[] indicesInOldConf = getSorted(oldFollowers, includeSelfInOldConf, followerIndex, logIndex);
        final MinMajorityMax oldConf = MinMajorityMax.valueOf(indicesInOldConf, followerMaxGapThreshold);
        return Optional.of(newConf.combine(oldConf));
    }
}
Also used : RaftPeerId(org.apache.ratis.protocol.RaftPeerId)

Example 70 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.

the class LeaderStateImpl method checkHealth.

@Override
public void checkHealth(FollowerInfo follower) {
    final TimeDuration elapsedTime = follower.getLastRpcResponseTime().elapsedTime();
    if (elapsedTime.compareTo(server.properties().rpcSlownessTimeout()) > 0) {
        server.getStateMachine().leaderEvent().notifyFollowerSlowness(server.getInfo().getRoleInfoProto());
    }
    final RaftPeerId followerId = follower.getPeer().getId();
    raftServerMetrics.recordFollowerHeartbeatElapsedTime(followerId, elapsedTime.toLong(TimeUnit.NANOSECONDS));
}
Also used : TimeDuration(org.apache.ratis.util.TimeDuration) RaftPeerId(org.apache.ratis.protocol.RaftPeerId)

Aggregations

RaftPeerId (org.apache.ratis.protocol.RaftPeerId)101 RaftClient (org.apache.ratis.client.RaftClient)58 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)45 IOException (java.io.IOException)36 Test (org.junit.Test)32 RaftPeer (org.apache.ratis.protocol.RaftPeer)31 RaftServer (org.apache.ratis.server.RaftServer)31 BaseTest (org.apache.ratis.BaseTest)21 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)20 CompletableFuture (java.util.concurrent.CompletableFuture)18 RaftProperties (org.apache.ratis.conf.RaftProperties)18 File (java.io.File)17 ArrayList (java.util.ArrayList)15 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)15 RaftGroup (org.apache.ratis.protocol.RaftGroup)14 TimeDuration (org.apache.ratis.util.TimeDuration)14 List (java.util.List)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 Collectors (java.util.stream.Collectors)11 RaftLog (org.apache.ratis.server.raftlog.RaftLog)11