org.apache.ratis.protocol.RaftPeerId Java Examples
The following examples show how to use
org.apache.ratis.protocol.RaftPeerId.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestRaftServerJmx.java From incubator-ratis with Apache License 2.0 | 6 votes |
static void runRegister(boolean expectToSucceed, String name, JmxRegister jmx) { final RaftServerMXBean mBean = new RaftServerMXBean() { @Override public String getId() { return null; } @Override public String getLeaderId() { return null; } @Override public long getCurrentTerm() { return 0; } @Override public String getGroupId() { return null; } @Override public String getRole() { return null; } @Override public List<String> getFollowers() { return null; } }; final RaftPeerId id = RaftPeerId.valueOf(name); final RaftGroupId groupId = RaftGroupId.randomId(); final boolean succeeded = RaftServerImpl.registerMBean(id, groupId, mBean, jmx); Assert.assertEquals(expectToSucceed, succeeded); }
Example #2
Source File: PeerConfiguration.java From ratis with Apache License 2.0 | 6 votes |
boolean hasMajority(Collection<RaftPeerId> others, RaftPeerId selfId) { Preconditions.assertTrue(!others.contains(selfId)); int num = 0; if (contains(selfId)) { num++; } for (RaftPeerId other : others) { if (contains(other)) { num++; } if (num > size() / 2) { return true; } } return false; }
Example #3
Source File: RetryCacheTests.java From ratis with Apache License 2.0 | 6 votes |
void runTestBasicRetry(CLUSTER cluster) throws Exception { RaftTestUtil.waitForLeader(cluster); final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage(false).getId(); long oldLastApplied = cluster.getLeader().getState().getLastAppliedIndex(); final RaftClient client = cluster.createClient(leaderId); final RaftClientRpc rpc = client.getClientRpc(); final long callId = 999; final long seqNum = 111; RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, seqNum, new SimpleMessage("message")); assertReply(rpc.sendRequest(r), client, callId); // retry with the same callId for (int i = 0; i < 5; i++) { assertReply(rpc.sendRequest(r), client, callId); } assertServer(cluster, client.getId(), callId, oldLastApplied); client.close(); }
Example #4
Source File: FakeRatisFollower.java From hadoop-ozone with Apache License 2.0 | 6 votes |
public static RequestVoteReplyProto requestVote(RaftPeerId raftPeerId, RequestVoteRequestProto request) { addLatency(); System.out.println("Request vote response"); return RequestVoteReplyProto.newBuilder() .setServerReply( RaftRpcReplyProto.newBuilder() .setSuccess(true) .setRequestorId(request.getServerRequest().getRequestorId()) .setReplyId(raftPeerId.toByteString()) .setCallId(request.getServerRequest().getCallId()) .setRaftGroupId(request.getServerRequest().getRaftGroupId()) ) .setTerm(request.getCandidateTerm()) .build(); }
Example #5
Source File: PeerConfiguration.java From incubator-ratis with Apache License 2.0 | 6 votes |
boolean hasMajority(Collection<RaftPeerId> others, RaftPeerId selfId) { Preconditions.assertTrue(!others.contains(selfId)); int num = 0; if (contains(selfId)) { num++; } for (RaftPeerId other : others) { if (contains(other)) { num++; } if (num > size() / 2) { return true; } } return false; }
Example #6
Source File: RaftBasicTests.java From incubator-ratis with Apache License 2.0 | 6 votes |
static CompletableFuture<Void> killAndRestartServer( RaftPeerId id, long killSleepMs, long restartSleepMs, MiniRaftCluster cluster, Logger LOG) { final CompletableFuture<Void> future = new CompletableFuture<>(); new Thread(() -> { try { Thread.sleep(killSleepMs); cluster.killServer(id); Thread.sleep(restartSleepMs); LOG.info("restart server: " + id); cluster.restartServer(id, false); future.complete(null); } catch (Exception e) { ExitUtils.terminate(-1, "Failed to kill/restart server: " + id, e, LOG); } }).start(); return future; }
Example #7
Source File: GroupManagementBaseTest.java From incubator-ratis with Apache License 2.0 | 6 votes |
@Test public void testGroupAlreadyExists() throws Exception { final MiniRaftCluster cluster = getCluster(1); cluster.start(); final RaftPeer peer = cluster.getPeers().get(0); final RaftPeerId peerId = peer.getId(); final RaftGroup group = RaftGroup.valueOf(cluster.getGroupId(), peer); try (final RaftClient client = cluster.createClient()) { Assert.assertEquals(group, cluster.getRaftServerImpl(peerId).getGroup()); try { client.groupAdd(group, peer.getId()); } catch (IOException ex) { // HadoopRPC throws RemoteException, which makes it hard to check if // the exception is instance of AlreadyExistsException Assert.assertTrue(ex.toString().contains(AlreadyExistsException.class.getCanonicalName())); } Assert.assertEquals(group, cluster.getRaftServerImpl(peerId).getGroup()); cluster.shutdown(); } }
Example #8
Source File: TestRaftServerJmx.java From ratis with Apache License 2.0 | 6 votes |
static void runRegister(boolean expectToSucceed, String name, JmxRegister jmx) { final RaftServerMXBean mBean = new RaftServerMXBean() { @Override public String getId() { return null; } @Override public String getLeaderId() { return null; } @Override public long getCurrentTerm() { return 0; } @Override public String getGroupId() { return null; } @Override public String getRole() { return null; } @Override public List<String> getFollowers() { return null; } }; final RaftPeerId id = RaftPeerId.valueOf(name); final RaftGroupId groupId = RaftGroupId.randomId(); final boolean succeeded = RaftServerImpl.registerMBean(id, groupId, mBean, jmx); Assert.assertEquals(expectToSucceed, succeeded); }
Example #9
Source File: ServerRestartTests.java From incubator-ratis with Apache License 2.0 | 6 votes |
static void assertCorruptedLogHeader(RaftPeerId id, File openLogFile, int partialLength, MiniRaftCluster cluster, Logger LOG) throws Exception { Preconditions.assertTrue(partialLength < SegmentedRaftLogFormat.getHeaderLength()); try(final RandomAccessFile raf = new RandomAccessFile(openLogFile, "rw")) { SegmentedRaftLogFormat.applyHeaderTo(header -> { LOG.info("header = {}", StringUtils.bytes2HexString(header)); final byte[] corrupted = new byte[header.length]; System.arraycopy(header, 0, corrupted, 0, partialLength); LOG.info("corrupted = {}", StringUtils.bytes2HexString(corrupted)); raf.write(corrupted); return null; }); } final RaftServerImpl server = cluster.restartServer(id, false); server.getProxy().close(); }
Example #10
Source File: FileInfo.java From incubator-ratis with Apache License 2.0 | 5 votes |
private CompletableFuture<Integer> submitWrite( CheckedSupplier<Integer, IOException> task, ExecutorService executor, RaftPeerId id, long index) { final CompletableFuture<Integer> f = writeQueue.submit(task, executor, e -> new IOException("Failed " + task, e)); final WriteInfo info = new WriteInfo(f, lastWriteIndex.getAndSet(index)); CollectionUtils.putNew(index, info, writeInfos, () -> id + ":writeInfos"); return f; }
Example #11
Source File: ServerProtoUtils.java From incubator-ratis with Apache License 2.0 | 5 votes |
static RequestVoteReplyProto toRequestVoteReplyProto( RaftPeerId requestorId, RaftGroupMemberId replyId, boolean success, long term, boolean shouldShutdown) { return RequestVoteReplyProto.newBuilder() .setServerReply(toRaftRpcReplyProtoBuilder(requestorId, replyId, success)) .setTerm(term) .setShouldShutdown(shouldShutdown) .build(); }
Example #12
Source File: MiniRaftClusterWithHadoopRpc.java From ratis with Apache License 2.0 | 5 votes |
@Override protected RaftServerProxy newRaftServer( RaftPeerId id, StateMachine.Registry stateMachineRegistry , RaftGroup group, RaftProperties properties) throws IOException { final Configuration hconf = new Configuration(hadoopConf); final String address = "0.0.0.0:" + getPort(id, group); HadoopConfigKeys.Ipc.setAddress(hconf, address); return ServerImplUtils.newRaftServer(id, group, stateMachineRegistry, properties, HadoopFactory.newRaftParameters(hconf)); }
Example #13
Source File: MiniRaftCluster.java From ratis with Apache License 2.0 | 5 votes |
private RaftServerProxy newRaftServer(RaftPeerId id, RaftGroup group, boolean format) { LOG.info("newRaftServer: {}, {}, format? {}", id, group, format); try { final File dir = getStorageDir(id); if (format) { FileUtils.deleteFully(dir); LOG.info("Formatted directory {}", dir); } final RaftProperties prop = new RaftProperties(properties); RaftServerConfigKeys.setStorageDirs(prop, Collections.singletonList(dir)); return newRaftServer(id, getStateMachineRegistry(properties), group, prop); } catch (IOException e) { throw new RuntimeException(e); } }
Example #14
Source File: MiniRaftCluster.java From incubator-ratis with Apache License 2.0 | 5 votes |
public static RaftGroup initRaftGroup(Collection<String> ids) { final RaftPeer[] peers = ids.stream() .map(RaftPeerId::valueOf) .map(id -> new RaftPeer(id, NetUtils.createLocalServerAddress())) .toArray(RaftPeer[]::new); return RaftGroup.valueOf(RaftGroupId.randomId(), peers); }
Example #15
Source File: MiniRaftClusterWithHadoopRpc.java From incubator-ratis with Apache License 2.0 | 5 votes |
@Override protected RaftServerProxy newRaftServer( RaftPeerId id, StateMachine.Registry stateMachineRegistry , RaftGroup group, RaftProperties properties) throws IOException { final Configuration hconf = new Configuration(hadoopConf); final String address = "0.0.0.0:" + getPort(id, group); HadoopConfigKeys.Ipc.setAddress(hconf, address); return ServerImplUtils.newRaftServer(id, group, stateMachineRegistry, properties, HadoopFactory.newRaftParameters(hconf)); }
Example #16
Source File: GrpcOutputStream.java From incubator-ratis with Apache License 2.0 | 5 votes |
public GrpcOutputStream(RaftProperties prop, ClientId clientId, RaftGroup group, RaftPeerId leaderId, GrpcTlsConfig tlsConfig) { final int bufferSize = GrpcConfigKeys.OutputStream.bufferSize(prop).getSizeInt(); buf = new byte[bufferSize]; count = 0; this.clientId = clientId; streamer = new GrpcClientStreamer(prop, group, leaderId, clientId, tlsConfig); }
Example #17
Source File: MiniRaftClusterWithSimulatedRpc.java From ratis with Apache License 2.0 | 5 votes |
@Override protected RaftServerProxy newRaftServer( RaftPeerId id, StateMachine.Registry stateMachineRegistry , RaftGroup group, RaftProperties properties) throws IOException { serverRequestReply.addPeer(id); client2serverRequestReply.addPeer(id); return ServerImplUtils.newRaftServer(id, group, stateMachineRegistry, properties, parameters); }
Example #18
Source File: LeaderElectionTests.java From incubator-ratis with Apache License 2.0 | 5 votes |
@Test public void testChangeLeader() throws Exception { SegmentedRaftLogTestUtils.setRaftLogWorkerLogLevel(Level.TRACE); LOG.info("Running testChangeLeader"); final MiniRaftCluster cluster = newCluster(3); cluster.start(); RaftPeerId leader = RaftTestUtil.waitForLeader(cluster).getId(); for(int i = 0; i < 10; i++) { leader = RaftTestUtil.changeLeader(cluster, leader, IllegalStateException::new); ExitUtils.assertNotTerminated(); } SegmentedRaftLogTestUtils.setRaftLogWorkerLogLevel(Level.INFO); cluster.shutdown(); }
Example #19
Source File: PeerProxyMap.java From incubator-ratis with Apache License 2.0 | 5 votes |
public PROXY getProxy(RaftPeerId id) throws IOException { Objects.requireNonNull(id, "id == null"); PeerAndProxy p = peers.get(id); if (p == null) { synchronized (resetLock) { p = Objects.requireNonNull(peers.get(id), () -> name + ": Server " + id + " not found: peers=" + peers.keySet()); } } return p.getProxy(); }
Example #20
Source File: ServerProtoUtils.java From ratis with Apache License 2.0 | 5 votes |
public static RequestVoteRequestProto toRequestVoteRequestProto( RaftPeerId requestorId, RaftPeerId replyId, RaftGroupId groupId, long term, TermIndex lastEntry) { final RequestVoteRequestProto.Builder b = RequestVoteRequestProto.newBuilder() .setServerRequest(toRaftRpcRequestProtoBuilder(requestorId, replyId, groupId)) .setCandidateTerm(term); if (lastEntry != null) { b.setCandidateLastEntry(toTermIndexProto(lastEntry)); } return b.build(); }
Example #21
Source File: RaftServerTestUtil.java From ratis with Apache License 2.0 | 5 votes |
public static void waitAndCheckNewConf(MiniRaftCluster cluster, RaftPeer[] peers, int numOfRemovedPeers, Collection<RaftPeerId> deadPeers) throws Exception { final TimeDuration sleepTime = cluster.getTimeoutMax().apply(n -> n * (numOfRemovedPeers + 2)); JavaUtils.attempt(() -> waitAndCheckNewConf(cluster, peers, deadPeers), 10, sleepTime, "waitAndCheckNewConf", LOG); }
Example #22
Source File: RaftTestUtil.java From ratis with Apache License 2.0 | 5 votes |
static RaftPeerId waitAndKillLeader(MiniRaftCluster cluster) throws InterruptedException { final RaftServerImpl leader = waitForLeader(cluster); Assert.assertNotNull(leader); LOG.info("killing leader = " + leader); cluster.killServer(leader.getId()); return leader.getId(); }
Example #23
Source File: FileInfo.java From ratis with Apache License 2.0 | 5 votes |
CompletableFuture<Integer> submitWrite( long offset, ByteString data, boolean close, ExecutorService executor, RaftPeerId id, long index) { final Supplier<String> name = () -> "write(" + getRelativePath() + ", " + offset + ", " + close + ") @" + id + ":" + index; final CheckedSupplier<Integer, IOException> task = LogUtils.newCheckedSupplier(LOG, () -> write(offset, data, close), name); return submitWrite(task, executor, id, index); }
Example #24
Source File: MiniRaftClusterWithGrpc.java From incubator-ratis with Apache License 2.0 | 5 votes |
@Override protected RaftServerProxy newRaftServer( RaftPeerId id, StateMachine.Registry stateMachineRegistry, RaftGroup group, RaftProperties properties) throws IOException { GrpcConfigKeys.Server.setPort(properties, getPort(id, group)); return ServerImplUtils.newRaftServer(id, group, stateMachineRegistry, properties, null); }
Example #25
Source File: TestRaftServerWithGrpc.java From incubator-ratis with Apache License 2.0 | 5 votes |
void runTestUnsupportedMethods(MiniRaftClusterWithGrpc cluster) throws Exception { final RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId(); final RaftServerRpc rpc = cluster.getServer(leaderId).getFactory().newRaftServerRpc(cluster.getServer(leaderId)); testFailureCase("appendEntries", () -> rpc.appendEntries(null), UnsupportedOperationException.class); testFailureCase("installSnapshot", () -> rpc.installSnapshot(null), UnsupportedOperationException.class); }
Example #26
Source File: TestRaftServerWithGrpc.java From incubator-ratis with Apache License 2.0 | 5 votes |
void runTestServerRestartOnException(MiniRaftClusterWithGrpc cluster) throws Exception { final RaftServerImpl leader = RaftTestUtil.waitForLeader(cluster); final RaftPeerId leaderId = leader.getId(); final RaftProperties p = getProperties(); GrpcConfigKeys.Server.setPort(p, leader.getServerRpc().getInetSocketAddress().getPort()); // Create a raft server proxy with server rpc bound to a different address // compared to leader. This helps in locking the raft storage directory to // be used by next raft server proxy instance. final StateMachine stateMachine = cluster.getLeader().getStateMachine(); RaftServerConfigKeys.setStorageDir(p, Collections.singletonList(cluster.getStorageDir(leaderId))); ServerImplUtils.newRaftServer(leaderId, cluster.getGroup(), gid -> stateMachine, p, null); // Close the server rpc for leader so that new raft server can be bound to it. cluster.getLeader().getServerRpc().close(); // Create a raft server proxy with server rpc bound to same address as // the leader. This step would fail as the raft storage has been locked by // the raft server proxy created earlier. Raft server proxy should close // the rpc server on failure. RaftServerConfigKeys.setStorageDir(p, Collections.singletonList(cluster.getStorageDir(leaderId))); testFailureCase("start a new server with the same address", () -> ServerImplUtils.newRaftServer(leaderId, cluster.getGroup(), gid -> stateMachine, p, null).start(), IOException.class, OverlappingFileLockException.class); // Try to start a raft server rpc at the leader address. cluster.getServer(leaderId).getFactory().newRaftServerRpc(cluster.getServer(leaderId)); }
Example #27
Source File: PeerConfiguration.java From incubator-ratis with Apache License 2.0 | 5 votes |
List<RaftPeer> getOtherPeers(RaftPeerId selfId) { List<RaftPeer> others = new ArrayList<>(); for (Map.Entry<RaftPeerId, RaftPeer> entry : peers.entrySet()) { if (!selfId.equals(entry.getValue().getId())) { others.add(entry.getValue()); } } return others; }
Example #28
Source File: ServerProtoUtils.java From incubator-ratis with Apache License 2.0 | 5 votes |
static RequestVoteRequestProto toRequestVoteRequestProto( RaftGroupMemberId requestorId, RaftPeerId replyId, long term, TermIndex lastEntry) { final RequestVoteRequestProto.Builder b = RequestVoteRequestProto.newBuilder() .setServerRequest(toRaftRpcRequestProtoBuilder(requestorId, replyId)) .setCandidateTerm(term); if (lastEntry != null) { b.setCandidateLastEntry(toTermIndexProto(lastEntry)); } return b.build(); }
Example #29
Source File: MiniRaftClusterWithNetty.java From incubator-ratis with Apache License 2.0 | 5 votes |
@Override protected RaftServerProxy newRaftServer( RaftPeerId id, StateMachine.Registry stateMachineRegistry , RaftGroup group, RaftProperties properties) throws IOException { NettyConfigKeys.Server.setPort(properties, getPort(id, group)); return ServerImplUtils.newRaftServer(id, group, stateMachineRegistry, properties, null); }
Example #30
Source File: TestGrpcServerMetrics.java From incubator-ratis with Apache License 2.0 | 5 votes |
@BeforeClass public static void setUp() throws Exception { RaftServerImpl raftServer = mock(RaftServerImpl.class); ServerState serverStateMock = mock(ServerState.class); when(raftServer.getState()).thenReturn(serverStateMock); when(serverStateMock.getLastLeaderElapsedTimeMs()).thenReturn(1000L); raftGroupId = RaftGroupId.randomId(); raftPeerId = RaftPeerId.valueOf("TestId"); followerId = RaftPeerId.valueOf("FollowerId"); RaftGroupMemberId raftGroupMemberId = RaftGroupMemberId.valueOf(raftPeerId, raftGroupId); when(raftServer.getMemberId()).thenReturn(raftGroupMemberId); grpcServerMetrics = new GrpcServerMetrics(raftGroupMemberId.toString()); ratisMetricRegistry = grpcServerMetrics.getRegistry(); }