use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.
the class RaftTestUtil method waitForLeader.
static RaftServerImpl waitForLeader(MiniRaftCluster cluster, boolean tolerateMultipleLeaders, RaftGroupId groupId) throws InterruptedException {
final long sleepTime = (cluster.getMaxTimeout() * 3) >> 1;
LOG.info(cluster.printServers(groupId));
RaftServerImpl leader = null;
for (int i = 0; leader == null && i < 10; i++) {
Thread.sleep(sleepTime);
try {
leader = cluster.getLeader(groupId);
} catch (IllegalStateException e) {
if (!tolerateMultipleLeaders) {
throw e;
}
}
}
LOG.info(cluster.printServers(groupId));
return leader;
}
use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.
the class TestRaftStream method testWriteWithOffset.
@Test
public void testWriteWithOffset() throws Exception {
LOG.info("Running testWriteWithOffset");
GrpcConfigKeys.OutputStream.setBufferSize(prop, SizeInBytes.valueOf(ByteValue.BUFFERSIZE));
cluster = MiniRaftClusterWithGRpc.FACTORY.newCluster(NUM_SERVERS, prop);
cluster.start();
RaftServerImpl leader = waitForLeader(cluster);
RaftOutputStream out = new RaftOutputStream(prop, ClientId.randomId(), cluster.getGroup(), leader.getId());
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();
final RaftLog log = leader.getState().getLog();
// 0.5 + 1 + 2.5 + 4 = 8
Assert.assertEquals(8, leader.getState().getLastAppliedIndex());
Assert.assertEquals(8, log.getLastCommittedIndex());
TermIndex[] entries = log.getEntries(1, 9);
byte[] actual = new byte[ByteValue.BUFFERSIZE * 8];
totalSize = 0;
for (TermIndex e : entries) {
byte[] eValue = log.get(e.getIndex()).getSmLogEntry().getData().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.server.impl.RaftServerImpl in project incubator-ratis by apache.
the class TestRaftStream method testWriteAndFlush.
@Test
public void testWriteAndFlush() throws Exception {
LOG.info("Running testWriteAndFlush");
GrpcConfigKeys.OutputStream.setBufferSize(prop, SizeInBytes.valueOf(ByteValue.BUFFERSIZE));
cluster = MiniRaftClusterWithGRpc.FACTORY.newCluster(NUM_SERVERS, prop);
cluster.start();
RaftServerImpl leader = waitForLeader(cluster);
RaftOutputStream out = new RaftOutputStream(prop, ClientId.randomId(), cluster.getGroup(), leader.getId());
int[] lengths = new int[] { 1, 500, 1023, 1024, 1025, 2048, 3000, 3072 };
ByteValue[] values = new ByteValue[lengths.length];
for (int i = 0; i < values.length; i++) {
values[i] = new ByteValue(lengths[i], (byte) 9);
}
List<byte[]> expectedTxs = new ArrayList<>();
for (ByteValue v : values) {
byte[] data = v.genData();
expectedTxs.addAll(v.getTransactions());
out.write(data);
out.flush();
// make sure after the flush the data has been committed
Assert.assertEquals(expectedTxs.size(), leader.getState().getLastAppliedIndex());
}
out.close();
try {
out.write(0);
fail("The OutputStream has been closed");
} catch (IOException ignored) {
}
LOG.info("Start to check leader's log");
final AtomicInteger index = new AtomicInteger(0);
checkLog(leader.getState().getLog(), expectedTxs.size(), () -> expectedTxs.get(index.getAndIncrement()));
}
use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.
the class TestSegmentedRaftLog method testAppendEntriesWithInconsistency.
/**
* Test append with inconsistent entries
*/
@Test
public void testAppendEntriesWithInconsistency() throws Exception {
// prepare the log for truncation
List<SegmentRange> ranges = prepareRanges(5, 200, 0);
List<LogEntryProto> entries = prepareLogEntries(ranges, null);
RaftServerImpl server = mock(RaftServerImpl.class);
RetryCache retryCache = RetryCacheTestUtil.createRetryCache();
when(server.getRetryCache()).thenReturn(retryCache);
doCallRealMethod().when(server).failClientRequest(any(LogEntryProto.class));
try (SegmentedRaftLog raftLog = new SegmentedRaftLog(peerId, server, storage, -1, properties)) {
raftLog.open(RaftServerConstants.INVALID_LOG_INDEX, null);
entries.stream().forEach(entry -> RetryCacheTestUtil.createEntry(retryCache, entry));
// append entries to the raftlog
entries.stream().map(raftLog::appendEntry).forEach(CompletableFuture::join);
}
// append entries whose first 100 entries are the same with existing log,
// and the next 100 are with different term
SegmentRange r1 = new SegmentRange(550, 599, 2, false);
SegmentRange r2 = new SegmentRange(600, 649, 3, false);
SegmentRange r3 = new SegmentRange(650, 749, 10, false);
List<LogEntryProto> newEntries = prepareLogEntries(Arrays.asList(r1, r2, r3), null);
try (SegmentedRaftLog raftLog = new SegmentedRaftLog(peerId, server, storage, -1, properties)) {
raftLog.open(RaftServerConstants.INVALID_LOG_INDEX, null);
raftLog.append(newEntries.toArray(new LogEntryProto[newEntries.size()])).forEach(CompletableFuture::join);
checkFailedEntries(entries, 650, retryCache);
checkEntries(raftLog, entries, 0, 650);
checkEntries(raftLog, newEntries, 100, 100);
Assert.assertEquals(newEntries.get(newEntries.size() - 1), getLastEntry(raftLog));
Assert.assertEquals(newEntries.get(newEntries.size() - 1).getIndex(), raftLog.getLatestFlushedIndex());
}
// load the raftlog again and check
try (SegmentedRaftLog raftLog = new SegmentedRaftLog(peerId, server, storage, -1, properties)) {
raftLog.open(RaftServerConstants.INVALID_LOG_INDEX, null);
checkEntries(raftLog, entries, 0, 650);
checkEntries(raftLog, newEntries, 100, 100);
Assert.assertEquals(newEntries.get(newEntries.size() - 1), getLastEntry(raftLog));
Assert.assertEquals(newEntries.get(newEntries.size() - 1).getIndex(), raftLog.getLatestFlushedIndex());
RaftLogCache cache = raftLog.getRaftLogCache();
Assert.assertEquals(5, cache.getNumOfSegments());
}
}
use of org.apache.ratis.server.impl.RaftServerImpl in project incubator-ratis by apache.
the class RaftSnapshotBaseTest method getSnapshotFile.
static File getSnapshotFile(MiniRaftCluster cluster, int i) {
final RaftServerImpl leader = cluster.getLeader();
final SimpleStateMachine4Testing sm = SimpleStateMachine4Testing.get(leader.getProxy());
return sm.getStateMachineStorage().getSnapshotFile(leader.getState().getCurrentTerm(), i);
}
Aggregations