use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class TestClientProtoUtils method runTestToRaftClientRequestProto.
void runTestToRaftClientRequestProto(int n, SizeInBytes messageSize) throws Exception {
final ClientId clientId = ClientId.randomId();
final RaftPeerId leaderId = RaftPeerId.valueOf("s0");
final RaftGroupId groupId = RaftGroupId.randomId();
TimeDuration toProto = TimeDuration.ZERO;
TimeDuration toRequest = TimeDuration.ZERO;
for (int i = 0; i < n; i++) {
final ByteString bytes = newByteString(messageSize.getSizeInt(), i);
final RaftClientRequest request = RaftClientRequest.newBuilder().setClientId(clientId).setServerId(leaderId).setGroupId(groupId).setCallId(1).setMessage(() -> bytes).setType(RaftClientRequest.writeRequestType()).build();
final Timestamp startTime = Timestamp.currentTime();
final RaftClientRequestProto proto = ClientProtoUtils.toRaftClientRequestProto(request);
final TimeDuration p = startTime.elapsedTime();
final RaftClientRequest computed = ClientProtoUtils.toRaftClientRequest(proto);
final TimeDuration r = startTime.elapsedTime().subtract(p);
Assert.assertEquals(request.getMessage().getContent(), computed.getMessage().getContent());
toProto = toProto.add(p);
toRequest = toRequest.add(r);
}
System.out.printf("%nmessageSize=%s, n=%d%n", messageSize, n);
print("toProto ", toProto, n);
print("toRequest", toRequest, n);
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class TestRetryCacheMetrics method setUp.
@BeforeClass
public static void setUp() {
RaftGroupId raftGroupId = RaftGroupId.randomId();
RaftPeerId raftPeerId = RaftPeerId.valueOf("TestId");
RaftGroupMemberId raftGroupMemberId = RaftGroupMemberId.valueOf(raftPeerId, raftGroupId);
retryCache = new RetryCacheImpl(RaftServerConfigKeys.RetryCache.EXPIRY_TIME_DEFAULT, null);
final RaftServerMetricsImpl raftServerMetrics = RaftServerMetricsImpl.computeIfAbsentRaftServerMetrics(raftGroupMemberId, () -> null, retryCache::getStatistics);
ratisMetricRegistry = raftServerMetrics.getRegistry();
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class ServerRestartTests method runTestRestartCommitIndex.
void runTestRestartCommitIndex(MiniRaftCluster cluster) throws Exception {
final SimpleMessage[] messages = SimpleMessage.create(100);
final List<CompletableFuture<Void>> futures = new ArrayList<>(messages.length);
for (int i = 0; i < messages.length; i++) {
final CompletableFuture<Void> f = new CompletableFuture<>();
futures.add(f);
final SimpleMessage m = messages[i];
new Thread(() -> {
try (final RaftClient client = cluster.createClient()) {
Assert.assertTrue(client.io().send(m).isSuccess());
} catch (IOException e) {
throw new IllegalStateException("Failed to send " + m, e);
}
f.complete(null);
}).start();
}
JavaUtils.allOf(futures).get();
final List<RaftPeerId> ids = new ArrayList<>();
final RaftServer.Division leader = cluster.getLeader();
final RaftLog leaderLog = leader.getRaftLog();
final RaftPeerId leaderId = leader.getId();
ids.add(leaderId);
RaftTestUtil.getStateMachineLogEntries(leaderLog);
// check that the last metadata entry is written to the log
JavaUtils.attempt(() -> assertLastLogEntry(leader), 20, HUNDRED_MILLIS, "leader last metadata entry", LOG);
final long lastIndex = leaderLog.getLastEntryTermIndex().getIndex();
LOG.info("{}: leader lastIndex={}", leaderId, lastIndex);
final LogEntryProto lastEntry = leaderLog.get(lastIndex);
LOG.info("{}: leader lastEntry entry[{}] = {}", leaderId, lastIndex, LogProtoUtils.toLogEntryString(lastEntry));
final long loggedCommitIndex = lastEntry.getMetadataEntry().getCommitIndex();
final LogEntryProto lastCommittedEntry = leaderLog.get(loggedCommitIndex);
LOG.info("{}: leader lastCommittedEntry = entry[{}] = {}", leaderId, loggedCommitIndex, LogProtoUtils.toLogEntryString(lastCommittedEntry));
final SimpleStateMachine4Testing leaderStateMachine = SimpleStateMachine4Testing.get(leader);
final TermIndex lastAppliedTermIndex = leaderStateMachine.getLastAppliedTermIndex();
LOG.info("{}: leader lastAppliedTermIndex = {}", leaderId, lastAppliedTermIndex);
// check follower logs
for (RaftServer.Division s : cluster.iterateDivisions()) {
if (!s.getId().equals(leaderId)) {
ids.add(s.getId());
JavaUtils.attempt(() -> RaftTestUtil.assertSameLog(leaderLog, s.getRaftLog()), 10, HUNDRED_MILLIS, "assertRaftLog-" + s.getId(), LOG);
}
}
// take snapshot and truncate last (metadata) entry
leaderStateMachine.takeSnapshot();
leaderLog.truncate(lastIndex);
// kill all servers
ids.forEach(cluster::killServer);
// Restart and kill servers one by one so that they won't talk to each other.
for (RaftPeerId id : ids) {
cluster.restartServer(id, false);
final RaftServer.Division server = cluster.getDivision(id);
final RaftLog raftLog = server.getRaftLog();
JavaUtils.attemptRepeatedly(() -> {
Assert.assertTrue(raftLog.getLastCommittedIndex() >= loggedCommitIndex);
return null;
}, 10, HUNDRED_MILLIS, id + "(commitIndex >= loggedCommitIndex)", LOG);
JavaUtils.attemptRepeatedly(() -> {
Assert.assertTrue(server.getInfo().getLastAppliedIndex() >= loggedCommitIndex);
return null;
}, 10, HUNDRED_MILLIS, id + "(lastAppliedIndex >= loggedCommitIndex)", LOG);
LOG.info("{}: commitIndex={}, lastAppliedIndex={}", id, raftLog.getLastCommittedIndex(), server.getInfo().getLastAppliedIndex());
cluster.killServer(id);
}
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class ServerRestartTests method runTestRestartWithCorruptedLogEntry.
private void runTestRestartWithCorruptedLogEntry(CLUSTER cluster) throws Exception {
// this is the only server
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
final RaftPeerId id = leader.getId();
// send a few messages
final SimpleMessage[] messages = SimpleMessage.create(10);
final SimpleMessage lastMessage = messages[messages.length - 1];
try (final RaftClient client = cluster.createClient()) {
for (SimpleMessage m : messages) {
Assert.assertTrue(client.io().send(m).isSuccess());
}
// assert that the last message exists
Assert.assertTrue(client.io().sendReadOnly(lastMessage).isSuccess());
}
final RaftLog log = leader.getRaftLog();
final long size = TestSegmentedRaftLog.getOpenSegmentSize(log);
leader.getRaftServer().close();
// corrupt the log
final File openLogFile = JavaUtils.attemptRepeatedly(() -> getOpenLogFile(leader), 10, HUNDRED_MILLIS, id + "-getOpenLogFile", LOG);
try (final RandomAccessFile raf = new RandomAccessFile(openLogFile, "rw")) {
final long mid = size / 2;
raf.seek(mid);
for (long i = mid; i < size; i++) {
raf.write(0);
}
}
// after the log is corrupted and the server is restarted, the last entry should no longer exist.
cluster.restartServer(id, false);
testFailureCase("last-entry-not-found", () -> {
try (final RaftClient client = cluster.createClient()) {
client.io().sendReadOnly(lastMessage);
}
}, StateMachineException.class, IndexOutOfBoundsException.class);
}
use of org.apache.ratis.protocol.RaftPeerId in project incubator-ratis by apache.
the class TestRaftServerJmx method runRegister.
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;
}
@Override
public List<String> getGroups() {
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);
}
Aggregations