use of org.apache.ratis.server.raftlog.LogEntryHeader in project incubator-ratis by apache.
the class MemoryRaftLog method getEntries.
@Override
public LogEntryHeader[] getEntries(long startIndex, long endIndex) {
checkLogState();
try (AutoCloseableLock readLock = readLock()) {
if (startIndex >= entries.size()) {
return null;
}
final int from = Math.toIntExact(startIndex);
final int to = Math.toIntExact(Math.min(entries.size(), endIndex));
final LogEntryHeader[] headers = new LogEntryHeader[to - from];
for (int i = 0; i < headers.length; i++) {
headers[i] = entries.getLogEntryHeader(i);
}
return headers;
}
}
use of org.apache.ratis.server.raftlog.LogEntryHeader in project incubator-ratis by apache.
the class MemoryRaftLogTest method testEntryDoNotPerformTruncation.
@Test
public void testEntryDoNotPerformTruncation() throws Exception {
final RaftProperties prop = new RaftProperties();
prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class);
final RaftPeerId peerId = RaftPeerId.valueOf("s0");
final RaftGroupId groupId = RaftGroupId.randomId();
final RaftGroupMemberId memberId = RaftGroupMemberId.valueOf(peerId, groupId);
MemoryRaftLog raftLog = new MemoryRaftLog(memberId, () -> -1, prop);
raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
LogEntryProto[] entries1 = new LogEntryProto[2];
entries1[0] = LogEntryProto.newBuilder().setIndex(0).setTerm(0).build();
entries1[1] = LogEntryProto.newBuilder().setIndex(1).setTerm(0).build();
raftLog.append(entries1).forEach(CompletableFuture::join);
LogEntryProto[] entries2 = new LogEntryProto[1];
entries2[0] = LogEntryProto.newBuilder().setIndex(0).setTerm(0).build();
raftLog.append(entries2).forEach(CompletableFuture::join);
final LogEntryHeader[] termIndices = raftLog.getEntries(0, 10);
assertEquals(2, termIndices.length);
for (int i = 0; i < 2; i++) {
assertEquals(entries1[i].getIndex(), termIndices[i].getIndex());
assertEquals(entries1[i].getTerm(), termIndices[i].getTerm());
}
}
use of org.apache.ratis.server.raftlog.LogEntryHeader in project incubator-ratis by apache.
the class TestRaftWithGrpc method runTestUpdateViaHeartbeat.
void runTestUpdateViaHeartbeat(MiniRaftClusterWithGrpc cluster) throws Exception {
waitForLeader(cluster);
try (final RaftClient client = cluster.createClient()) {
// block append requests
cluster.getServerAliveStream().filter(impl -> !impl.getInfo().isLeader()).map(SimpleStateMachine4Testing::get).forEach(SimpleStateMachine4Testing::blockWriteStateMachineData);
CompletableFuture<RaftClientReply> replyFuture = client.async().send(new RaftTestUtil.SimpleMessage("abc"));
TimeDuration.valueOf(5, TimeUnit.SECONDS).sleep();
// replyFuture should not be completed until append request is unblocked.
Assert.assertFalse(replyFuture.isDone());
// unblock append request.
cluster.getServerAliveStream().filter(impl -> !impl.getInfo().isLeader()).map(SimpleStateMachine4Testing::get).forEach(SimpleStateMachine4Testing::unblockWriteStateMachineData);
final RaftLog leaderLog = cluster.getLeader().getRaftLog();
// The entries have been appended in the followers
// although the append entry timed out at the leader
cluster.getServerAliveStream().filter(impl -> !impl.getInfo().isLeader()).forEach(raftServer -> JavaUtils.runAsUnchecked(() -> JavaUtils.attempt(() -> {
final long leaderNextIndex = leaderLog.getNextIndex();
final LogEntryHeader[] leaderEntries = leaderLog.getEntries(0, Long.MAX_VALUE);
final RaftLog followerLog = raftServer.getRaftLog();
Assert.assertEquals(leaderNextIndex, followerLog.getNextIndex());
final LogEntryHeader[] serverEntries = followerLog.getEntries(0, Long.MAX_VALUE);
Assert.assertArrayEquals(serverEntries, leaderEntries);
}, 10, HUNDRED_MILLIS, "assertRaftLog-" + raftServer.getId(), LOG)));
// Wait for heartbeats from leader to be received by followers
Thread.sleep(500);
RaftServerTestUtil.getLogAppenders(cluster.getLeader()).forEach(logAppender -> JavaUtils.runAsUnchecked(() -> JavaUtils.attempt(() -> {
final long leaderNextIndex = leaderLog.getNextIndex();
// FollowerInfo in the leader state should have updated next and match index.
final long followerMatchIndex = logAppender.getFollower().getMatchIndex();
Assert.assertTrue(followerMatchIndex >= leaderNextIndex - 1);
Assert.assertEquals(followerMatchIndex + 1, logAppender.getFollower().getNextIndex());
}, 10, HUNDRED_MILLIS, "assertRaftLog-" + logAppender.getFollower(), LOG)));
}
}
use of org.apache.ratis.server.raftlog.LogEntryHeader in project incubator-ratis by apache.
the class OutputStreamBaseTest method runTestWriteWithOffset.
private void runTestWriteWithOffset(CLUSTER cluster) throws Exception {
final int bufferSize = ByteValue.BUFFERSIZE;
final RaftServer.Division leader = waitForLeader(cluster);
final OutputStream out = newOutputStream(cluster, bufferSize);
byte[] b1 = new byte[ByteValue.BUFFERSIZE / 2];
Arrays.fill(b1, (byte) 1);
byte[] b2 = new byte[ByteValue.BUFFERSIZE];
Arrays.fill(b2, (byte) 2);
byte[] b3 = new byte[ByteValue.BUFFERSIZE * 2 + ByteValue.BUFFERSIZE / 2];
Arrays.fill(b3, (byte) 3);
byte[] b4 = new byte[ByteValue.BUFFERSIZE * 4];
Arrays.fill(b3, (byte) 4);
byte[] expected = new byte[ByteValue.BUFFERSIZE * 8];
byte[][] data = new byte[][] { b1, b2, b3, b4 };
final Random random = new Random();
int totalSize = 0;
for (byte[] b : data) {
System.arraycopy(b, 0, expected, totalSize, b.length);
totalSize += b.length;
int written = 0;
while (written < b.length) {
int toWrite = random.nextInt(b.length - written) + 1;
LOG.info("write {} bytes", toWrite);
out.write(b, written, toWrite);
written += toWrite;
}
}
out.close();
// 0.5 + 1 + 2.5 + 4 = 8
final int expectedEntries = 8;
final RaftLog raftLog = assertRaftLog(expectedEntries, leader);
final LogEntryHeader[] entries = raftLog.getEntries(1, Long.MAX_VALUE);
final byte[] actual = new byte[ByteValue.BUFFERSIZE * expectedEntries];
totalSize = 0;
for (LogEntryHeader ti : entries) {
final LogEntryProto e = raftLog.get(ti.getIndex());
if (e.hasStateMachineLogEntry()) {
final byte[] eValue = e.getStateMachineLogEntry().getLogData().toByteArray();
Assert.assertEquals(ByteValue.BUFFERSIZE, eValue.length);
System.arraycopy(eValue, 0, actual, totalSize, eValue.length);
totalSize += eValue.length;
}
}
Assert.assertArrayEquals(expected, actual);
}
Aggregations