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

The following examples show how to use org.apache.ratis.proto.RaftProtos.LogEntryProto#hasStateMachineLogEntry() . 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: ParseRatisLog.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private void processLogEntry(LogEntryProto proto) {
  if (proto.hasConfigurationEntry()) {
    numConfEntries++;
  } else if (proto.hasMetadataEntry()) {
    numMetadataEntries++;
  } else if (proto.hasStateMachineLogEntry()) {
    numStateMachineEntries++;
  } else {
    System.out.println("Found invalid entry" + proto.toString());
    numInvalidEntries++;
  }

  String str = ServerProtoUtils.toLogEntryString(proto, smLogToString);
  System.out.println(str);
}
 
Example 5
Source File: RaftLogMetrics.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public void onLogEntryCommit(LogEntryProto proto) {
  if (proto.hasConfigurationEntry()) {
    registry.counter(CONFIG_LOG_ENTRY_COUNT).inc();
  } else if (proto.hasMetadataEntry()) {
    registry.counter(METADATA_LOG_ENTRY_COUNT).inc();
  } else if (proto.hasStateMachineLogEntry()) {
    registry.counter(STATE_MACHINE_LOG_ENTRY_COUNT).inc();
  }
}
 
Example 6
Source File: RetryCacheTestUtil.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public static void createEntry(RetryCache cache, LogEntryProto logEntry){
  if(logEntry.hasStateMachineLogEntry()) {
    final StateMachineLogEntryProto smLogEntry = logEntry.getStateMachineLogEntry();
    final ClientId clientId = ClientId.valueOf(smLogEntry.getClientId());
    final long callId = smLogEntry.getCallId();
    cache.getOrCreateEntry(clientId, callId);
  }
}
 
Example 7
Source File: RetryCacheTestUtil.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public static void assertFailure(RetryCache cache, LogEntryProto logEntry, boolean isFailed) {
  if(logEntry.hasStateMachineLogEntry()) {
    final StateMachineLogEntryProto smLogEntry = logEntry.getStateMachineLogEntry();
    final ClientId clientId = ClientId.valueOf(smLogEntry.getClientId());
    final long callId = smLogEntry.getCallId();
    Assert.assertEquals(isFailed, cache.get(clientId, callId).isFailed());
  }
}
 
Example 8
Source File: RetryCacheTestUtil.java    From ratis with Apache License 2.0 5 votes vote down vote up
public static void createEntry(RetryCache cache, LogEntryProto logEntry){
  if(logEntry.hasStateMachineLogEntry()) {
    final StateMachineLogEntryProto smLogEntry = logEntry.getStateMachineLogEntry();
    final ClientId clientId = ClientId.valueOf(smLogEntry.getClientId());
    final long callId = smLogEntry.getCallId();
    cache.getOrCreateEntry(clientId, callId);
  }
}
 
Example 9
Source File: RetryCacheTestUtil.java    From ratis with Apache License 2.0 5 votes vote down vote up
public static void assertFailure(RetryCache cache, LogEntryProto logEntry, boolean isFailed) {
  if(logEntry.hasStateMachineLogEntry()) {
    final StateMachineLogEntryProto smLogEntry = logEntry.getStateMachineLogEntry();
    final ClientId clientId = ClientId.valueOf(smLogEntry.getClientId());
    final long callId = smLogEntry.getCallId();
    Assert.assertEquals(isFailed, cache.get(clientId, callId).isFailed());
  }
}
 
Example 10
Source File: OutputStreamBaseTest.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
private void runTestWriteWithOffset(CLUSTER cluster) throws Exception {
  final int bufferSize = ByteValue.BUFFERSIZE;
  RaftServerImpl 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 TermIndex[] entries = raftLog.getEntries(1, Long.MAX_VALUE);
  final byte[] actual = new byte[ByteValue.BUFFERSIZE * expectedEntries];
  totalSize = 0;
  for (TermIndex 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);
}