use of org.apache.ratis.proto.RaftProtos.LogEntryProto 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);
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class RaftTestUtil method getStateMachineLogEntries.
static List<LogEntryProto> getStateMachineLogEntries(RaftLog log) {
final List<LogEntryProto> entries = new ArrayList<>();
for (LogEntryProto e : getLogEntryProtos(log)) {
final String s = LogProtoUtils.toLogEntryString(e);
if (e.hasStateMachineLogEntry()) {
LOG.info(s + ", " + e.getStateMachineLogEntry().toString().trim().replace("\n", ", "));
entries.add(e);
} else if (e.hasConfigurationEntry()) {
LOG.info("Found {}, ignoring it.", s);
} else if (e.hasMetadataEntry()) {
LOG.info("Found {}, ignoring it.", s);
} else {
throw new AssertionError("Unexpected LogEntryBodyCase " + e.getLogEntryBodyCase() + " at " + s);
}
}
return entries;
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class RaftTestUtil method countEntries.
static EnumMap<LogEntryBodyCase, AtomicLong> countEntries(RaftLog raftLog) throws Exception {
final EnumMap<LogEntryBodyCase, AtomicLong> counts = new EnumMap<>(LogEntryBodyCase.class);
for (long i = 0; i < raftLog.getNextIndex(); i++) {
final LogEntryProto e = raftLog.get(i);
counts.computeIfAbsent(e.getLogEntryBodyCase(), c -> new AtomicLong()).incrementAndGet();
}
return counts;
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto in project incubator-ratis by apache.
the class SimpleStateMachine4Testing method query.
/**
* Query the n-th log entry.
* @param request an index represented in a UTF-8 String, or an empty message.
* @return a completed future of the n-th log entry,
* where n is the last applied index if the request is empty,
* otherwise, n is the index represented in the request.
*/
@Override
public CompletableFuture<Message> query(Message request) {
final String string = request.getContent().toStringUtf8();
Exception exception;
try {
LOG.info("query " + string);
final LogEntryProto entry = dataMap.get(string);
if (entry != null) {
return CompletableFuture.completedFuture(Message.valueOf(entry.toByteString()));
}
exception = new IndexOutOfBoundsException(getId() + ": LogEntry not found for query " + string);
} catch (Exception e) {
LOG.warn("Failed request " + request, e);
exception = e;
}
return JavaUtils.completeExceptionally(new StateMachineException("Failed request " + request, exception));
}
use of org.apache.ratis.proto.RaftProtos.LogEntryProto 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());
}
}
Aggregations