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

The following examples show how to use org.apache.ratis.proto.RaftProtos.LogEntryProto#hasConfigurationEntry() . 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: LogSegment.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
private void append(boolean keepEntryInCache, LogEntryProto entry) {
  Objects.requireNonNull(entry, "entry == null");
  if (records.isEmpty()) {
    Preconditions.assertTrue(entry.getIndex() == startIndex,
        "gap between start index %s and first entry to append %s",
        startIndex, entry.getIndex());
  }

  final LogRecord currentLast = getLastRecord();
  if (currentLast != null) {
    Preconditions.assertTrue(entry.getIndex() == currentLast.getTermIndex().getIndex() + 1,
        "gap between entries %s and %s", entry.getIndex(), currentLast.getTermIndex().getIndex());
  }

  final LogRecord record = new LogRecord(totalSize, entry);
  records.add(record);
  if (keepEntryInCache) {
    entryCache.put(record.getTermIndex(), entry);
  }
  if (entry.hasConfigurationEntry()) {
    configEntries.add(record.getTermIndex());
  }
  totalSize += getEntrySize(entry);
  endIndex = entry.getIndex();
}
 
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: LogStateMachine.java    From ratis with Apache License 2.0 5 votes vote down vote up
/**
 * Process read log entries request
 * @param msg message
 * @return reply message
 */
private CompletableFuture<Message> processReadRequest(LogServiceRequestProto proto) {

  ReadLogRequestProto msgProto = proto.getReadNextQuery();
  long startRecordId = msgProto.getStartRecordId();
  int num = msgProto.getNumRecords();
  Throwable t = verifyState(State.OPEN);
  List<byte[]> list = new ArrayList<byte[]>();
  LOG.info("Start Index: {}", startRecordId);
  LOG.info("Total to read: {}", num);
  if (t == null) {
    for (long index = startRecordId; index < startRecordId + num; index++) {
      try {
        LogEntryProto entry = log.get(index);
        LOG.info("Index: {} Entry: {}", index, entry);
        if (entry == null || entry.hasConfigurationEntry()) {
          continue;
        }
        //TODO: how to distinguish log records from
        // DML commands logged by the service?
        list.add(entry.getStateMachineLogEntry().getLogData().toByteArray());
      } catch (RaftLogIOException e) {
        t = e;
        list = null;
        break;
      }
    }
  }
  return CompletableFuture.completedFuture(
    Message.valueOf(LogServiceProtoUtil.toReadLogReplyProto(list, t).toByteString()));
}
 
Example 7
Source File: LogSegment.java    From ratis with Apache License 2.0 5 votes vote down vote up
private void append(boolean keepEntryInCache, LogEntryProto... entries) {
  Preconditions.assertTrue(entries != null && entries.length > 0);
  final long term = entries[0].getTerm();
  if (records.isEmpty()) {
    Preconditions.assertTrue(entries[0].getIndex() == startIndex,
        "gap between start index %s and first entry to append %s",
        startIndex, entries[0].getIndex());
  }
  for (LogEntryProto entry : entries) {
    // all these entries should be of the same term
    Preconditions.assertTrue(entry.getTerm() == term,
        "expected term:%s, term of the entry:%s", term, entry.getTerm());
    final LogRecord currentLast = getLastRecord();
    if (currentLast != null) {
      Preconditions.assertTrue(
          entry.getIndex() == currentLast.getTermIndex().getIndex() + 1,
          "gap between entries %s and %s", entry.getIndex(),
          currentLast.getTermIndex().getIndex());
    }

    final LogRecord record = new LogRecord(totalSize, entry);
    records.add(record);
    if (keepEntryInCache) {
      entryCache.put(record.getTermIndex(), entry);
    }
    if (entry.hasConfigurationEntry()) {
      configEntries.add(record.getTermIndex());
    }
    totalSize += getEntrySize(entry);
    endIndex = entry.getIndex();
  }
}
 
Example 8
Source File: ServerState.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
void setRaftConf(LogEntryProto entry) {
  if (entry.hasConfigurationEntry()) {
    setRaftConf(entry.getIndex(), ServerProtoUtils.toRaftConfiguration(entry));
  }
}
 
Example 9
Source File: ServerState.java    From ratis with Apache License 2.0 4 votes vote down vote up
void setRaftConf(LogEntryProto entry) {
  if (entry.hasConfigurationEntry()) {
    setRaftConf(entry.getIndex(), ServerProtoUtils.toRaftConfiguration(entry));
  }
}