Java Code Examples for org.apache.ratis.proto.RaftProtos.LogEntryProto#getLogEntryBodyCase()

The following examples show how to use org.apache.ratis.proto.RaftProtos.LogEntryProto#getLogEntryBodyCase() . 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: OutputStreamBaseTest.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
private void checkLog(RaftLog raftLog, long expectedCommittedIndex,
    Supplier<byte[]> s) throws IOException {
  long committedIndex = raftLog.getLastCommittedIndex();
  Assert.assertTrue(committedIndex >= expectedCommittedIndex);
  // check the log content
  TermIndex[] entries = raftLog.getEntries(0, Long.MAX_VALUE);
  int count = 0;
  for (TermIndex entry : entries) {
    LogEntryProto log  = raftLog.get(entry.getIndex());
    if (!log.hasStateMachineLogEntry()) {
      continue;
    }
    byte[] logData = log.getStateMachineLogEntry().getLogData().toByteArray();
    byte[] expected = s.get();
    final String message = "log " + entry + " " + log.getLogEntryBodyCase()
        + " " + StringUtils.bytes2HexString(logData)
        + ", expected=" + StringUtils.bytes2HexString(expected);
    LOG.info(message);
    Assert.assertArrayEquals(message, expected, logData);
    count++;
  }
  Assert.assertEquals(expectedCommittedIndex, count);
}
 
Example 2
Source File: RaftTestUtil.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
static List<LogEntryProto> getStateMachineLogEntries(RaftLog log) {
  final List<LogEntryProto> entries = new ArrayList<>();
  for (LogEntryProto e : getLogEntryProtos(log)) {
    final String s = ServerProtoUtils.toString(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;
}
 
Example 3
Source File: RaftTestUtil.java    From ratis with Apache License 2.0 6 votes vote down vote up
static void assertLogEntries(RaftLog log, long expectedTerm, SimpleMessage... expectedMessages) {
  final List<LogEntryProto> entries = new ArrayList<>(expectedMessages.length);
  for(LogEntryProto e : getLogEntryProtos(log)) {
    final String s = ServerProtoUtils.toString(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);
    }
  }

  try {
    assertLogEntries(entries, expectedTerm, expectedMessages);
  } catch(Throwable t) {
    throw new AssertionError("entries: " + entries, t);
  }
}
 
Example 4
Source File: RaftTestUtil.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
static LogEntryProto getLastEntry(LogEntryBodyCase targetCase, RaftLog raftLog) throws Exception {
  try(AutoCloseableLock readLock = raftLog.readLock()) {
    long i = raftLog.getNextIndex() - 1;
    for(; i >= 0; i--) {
      final LogEntryProto entry = raftLog.get(i);
      if (entry.getLogEntryBodyCase() == targetCase) {
        return entry;
      }
    }
  }
  return null;
}
 
Example 5
Source File: RaftTestUtil.java    From ratis with Apache License 2.0 5 votes vote down vote up
static LogEntryProto getLastEntry(LogEntryBodyCase targetCase, RaftLog raftLog) throws Exception {
  try(AutoCloseableLock readLock = raftLog.readLock()) {
    long i = raftLog.getNextIndex() - 1;
    for(; i >= 0; i--) {
      final LogEntryProto entry = raftLog.get(i);
      if (entry.getLogEntryBodyCase() == targetCase) {
        return entry;
      }
    }
  }
  return null;
}