Search in sources :

Example 1 with RaftGroup

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

the class Client method run.

@Override
public void run() throws Exception {
    RaftProperties raftProperties = new RaftProperties();
    RaftGroup raftGroup = new RaftGroup(RaftGroupId.valueOf(ByteString.copyFromUtf8(raftGroupId)), parsePeers(peers));
    RaftClient.Builder builder = RaftClient.newBuilder().setProperties(raftProperties);
    builder.setRaftGroup(raftGroup);
    builder.setClientRpc(new GrpcFactory(new Parameters()).newRaftClientRpc(ClientId.randomId(), raftProperties));
    RaftClient client = builder.build();
    operation(client);
}
Also used : Parameters(org.apache.ratis.conf.Parameters) GrpcFactory(org.apache.ratis.grpc.GrpcFactory) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftGroup(org.apache.ratis.protocol.RaftGroup) RaftClient(org.apache.ratis.client.RaftClient)

Example 2 with RaftGroup

use of org.apache.ratis.protocol.RaftGroup in project alluxio by Alluxio.

the class RaftJournalSystem method updateGroup.

/**
 * Updates raft group with the current values from raft server.
 */
public synchronized void updateGroup() {
    RaftGroup newGroup = getCurrentGroup();
    if (!newGroup.equals(mRaftGroup)) {
        LOG.info("Raft group updated: old {}, new {}", mRaftGroup, newGroup);
        mRaftGroup = newGroup;
    }
}
Also used : RaftGroup(org.apache.ratis.protocol.RaftGroup)

Example 3 with RaftGroup

use of org.apache.ratis.protocol.RaftGroup in project alluxio by Alluxio.

the class JournalStateMachine method takeSnapshot.

@Override
public long takeSnapshot() {
    if (mIsLeader) {
        try {
            Preconditions.checkState(mServer.getGroups().iterator().hasNext());
            RaftGroup group = mServer.getGroups().iterator().next();
            Preconditions.checkState(group.getGroupId().equals(mRaftGroupId));
            if (group.getPeers().size() < 2) {
                SAMPLING_LOG.warn("No follower to perform delegated snapshot. Please add more masters to " + "the quorum or manually take snapshot using 'alluxio fsadmin journal checkpoint'");
                return RaftLog.INVALID_LOG_INDEX;
            }
        } catch (IOException e) {
            SAMPLING_LOG.warn("Failed to get raft group info: {}", e.getMessage());
        }
        long index = mSnapshotManager.maybeCopySnapshotFromFollower();
        if (index != RaftLog.INVALID_LOG_INDEX) {
            mSnapshotLastIndex = index;
        }
        mLastCheckPointTime = System.currentTimeMillis();
        return index;
    } else {
        return takeLocalSnapshot();
    }
}
Also used : RaftGroup(org.apache.ratis.protocol.RaftGroup) IOException(java.io.IOException)

Example 4 with RaftGroup

use of org.apache.ratis.protocol.RaftGroup 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();
    RaftPeer[] peers = getPeers();
    final int port = NetUtils.createSocketAddr(getPeer(peerId).getAddress()).getPort();
    GrpcConfigKeys.Server.setPort(properties, port);
    properties.setInt(GrpcConfigKeys.OutputStream.RETRY_TIMES_KEY, Integer.MAX_VALUE);
    RaftServerConfigKeys.setStorageDir(properties, storageDir);
    StateMachine stateMachine = new ArithmeticStateMachine();
    RaftGroup raftGroup = new RaftGroup(RaftGroupId.valueOf(ByteString.copyFromUtf8(raftGroupId)), peers);
    RaftServer raftServer = RaftServer.newBuilder().setServerId(RaftPeerId.valueOf(id)).setStateMachine(stateMachine).setProperties(properties).setGroup(raftGroup).build();
    raftServer.start();
}
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) RaftPeer(org.apache.ratis.protocol.RaftPeer)

Example 5 with RaftGroup

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

the class ReinitializationBaseTest method runTestReinitializeMultiGroups.

public static <T extends Throwable> void runTestReinitializeMultiGroups(MiniRaftCluster cluster, int[] idIndex, int chosen, CheckedBiConsumer<MiniRaftCluster, RaftGroup, T> checker) throws IOException, InterruptedException, T {
    if (chosen < 0) {
        chosen = ThreadLocalRandom.current().nextInt(idIndex.length);
    }
    final String type = cluster.getClass().getSimpleName() + Arrays.toString(idIndex) + "chosen=" + chosen;
    LOG.info("\n\nrunTestReinitializeMultiGroups with " + type + ": " + cluster.printServers());
    // Start server with an empty conf
    final RaftGroup emptyGroup = new RaftGroup(RaftGroupId.randomId());
    final List<RaftPeerId> ids = Arrays.stream(MiniRaftCluster.generateIds(idIndex[idIndex.length - 1], 0)).map(RaftPeerId::valueOf).collect(Collectors.toList());
    ids.forEach(id -> cluster.putNewServer(id, emptyGroup, true));
    LOG.info("putNewServer: " + cluster.printServers());
    cluster.start();
    LOG.info("start: " + cluster.printServers());
    // Make sure that there are no leaders.
    TimeUnit.SECONDS.sleep(1);
    Assert.assertNull(cluster.getLeader());
    // Reinitialize servers to three groups
    final List<RaftPeer> allPeers = cluster.getPeers();
    Collections.sort(allPeers, Comparator.comparing(p -> p.getId().toString()));
    final RaftGroup[] groups = new RaftGroup[idIndex.length];
    for (int i = 0; i < idIndex.length; i++) {
        final RaftGroupId gid = RaftGroupId.randomId();
        final int previous = i == 0 ? 0 : idIndex[i - 1];
        final RaftPeer[] peers = allPeers.subList(previous, idIndex[i]).toArray(RaftPeer.emptyArray());
        groups[i] = new RaftGroup(gid, peers);
        LOG.info(i + ") starting " + groups[i]);
        for (RaftPeer p : peers) {
            try (final RaftClient client = cluster.createClient(p.getId(), emptyGroup)) {
                client.reinitialize(groups[i], p.getId());
            }
        }
        Assert.assertNotNull(RaftTestUtil.waitForLeader(cluster, true, gid));
        checker.accept(cluster, groups[i]);
    }
    printThreadCount(type, "start groups");
    LOG.info("start groups: " + cluster.printServers());
    // randomly close two of the groups (i.e. reinitialize to empty peers)
    LOG.info("chosen = " + chosen + ", " + groups[chosen]);
    for (int i = 0; i < groups.length; i++) {
        if (i != chosen) {
            final RaftGroup g = groups[i];
            final RaftGroup newGroup = new RaftGroup(g.getGroupId());
            LOG.info(i + ") close " + cluster.printServers(g.getGroupId()));
            for (RaftPeer p : g.getPeers()) {
                try (final RaftClient client = cluster.createClient(p.getId(), g)) {
                    client.reinitialize(newGroup, p.getId());
                }
            }
        }
    }
    printThreadCount(type, "close groups");
    LOG.info("close groups: " + cluster.printServers());
    // update chosen group to use all the peers
    final RaftGroup newGroup = new RaftGroup(groups[chosen].getGroupId());
    for (int i = 0; i < groups.length; i++) {
        if (i != chosen) {
            LOG.info(i + ") reinitialize: " + cluster.printServers(groups[i].getGroupId()));
            for (RaftPeer p : groups[i].getPeers()) {
                try (final RaftClient client = cluster.createClient(p.getId(), groups[i])) {
                    client.reinitialize(newGroup, p.getId());
                }
            }
        }
    }
    LOG.info(chosen + ") setConfiguration: " + cluster.printServers(groups[chosen].getGroupId()));
    try (final RaftClient client = cluster.createClient(groups[chosen])) {
        client.setConfiguration(allPeers.toArray(RaftPeer.emptyArray()));
    }
    Assert.assertNotNull(RaftTestUtil.waitForLeader(cluster, true));
    checker.accept(cluster, groups[chosen]);
    LOG.info("update groups: " + cluster.printServers());
    printThreadCount(type, "update groups");
    cluster.shutdown();
    printThreadCount(type, "shutdown");
}
Also used : Arrays(java.util.Arrays) RaftGroup(org.apache.ratis.protocol.RaftGroup) CheckedBiConsumer(org.apache.ratis.util.CheckedBiConsumer) LoggerFactory(org.slf4j.LoggerFactory) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) Level(org.apache.log4j.Level) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) MiniRaftCluster(org.apache.ratis.MiniRaftCluster) JavaUtils(org.apache.ratis.util.JavaUtils) LogUtils(org.apache.ratis.util.LogUtils) Logger(org.slf4j.Logger) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) Test(org.junit.Test) IOException(java.io.IOException) BaseTest(org.apache.ratis.BaseTest) Collectors(java.util.stream.Collectors) RaftTestUtil(org.apache.ratis.RaftTestUtil) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) Comparator(java.util.Comparator) Collections(java.util.Collections) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) RaftGroup(org.apache.ratis.protocol.RaftGroup) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

RaftGroup (org.apache.ratis.protocol.RaftGroup)7 IOException (java.io.IOException)3 RaftClient (org.apache.ratis.client.RaftClient)3 RaftProperties (org.apache.ratis.conf.RaftProperties)3 RaftPeer (org.apache.ratis.protocol.RaftPeer)3 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)3 BaseTest (org.apache.ratis.BaseTest)2 MiniRaftCluster (org.apache.ratis.MiniRaftCluster)2 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)2 Test (org.junit.Test)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 List (java.util.List)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 Level (org.apache.log4j.Level)1 RaftTestUtil (org.apache.ratis.RaftTestUtil)1