Java Code Examples for org.apache.ratis.server.protocol.TermIndex#getIndex()

The following examples show how to use org.apache.ratis.server.protocol.TermIndex#getIndex() . 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: ArithmeticStateMachine.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Override
public long takeSnapshot() {
  final Map<String, Double> copy;
  final TermIndex last;
  try(final AutoCloseableLock readLock = readLock()) {
    copy = new HashMap<>(variables);
    last = getLastAppliedTermIndex();
  }

  final File snapshotFile =  storage.getSnapshotFile(last.getTerm(), last.getIndex());
  LOG.info("Taking a snapshot to file {}", snapshotFile);

  try(final ObjectOutputStream out = new ObjectOutputStream(
      new BufferedOutputStream(new FileOutputStream(snapshotFile)))) {
    out.writeObject(copy);
  } catch(IOException ioe) {
    LOG.warn("Failed to write snapshot file \"" + snapshotFile
        + "\", last applied index=" + last);
  }

  return last.getIndex();
}
 
Example 2
Source File: ArithmeticStateMachine.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Override
public long takeSnapshot() {
  final Map<String, Double> copy;
  final TermIndex last;
  try(AutoCloseableLock readLock = readLock()) {
    copy = new HashMap<>(variables);
    last = getLastAppliedTermIndex();
  }

  final File snapshotFile =  storage.getSnapshotFile(last.getTerm(), last.getIndex());
  LOG.info("Taking a snapshot to file {}", snapshotFile);

  try(ObjectOutputStream out = new ObjectOutputStream(
      new BufferedOutputStream(new FileOutputStream(snapshotFile)))) {
    out.writeObject(copy);
  } catch(IOException ioe) {
    LOG.warn("Failed to write snapshot file \"" + snapshotFile
        + "\", last applied index=" + last);
  }

  return last.getIndex();
}
 
Example 3
Source File: ArithmeticStateMachine.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
private long load(SingleFileSnapshotInfo snapshot, boolean reload) throws IOException {
  if (snapshot == null) {
    LOG.warn("The snapshot info is null.");
    return RaftServerConstants.INVALID_LOG_INDEX;
  }
  final File snapshotFile = snapshot.getFile().getPath().toFile();
  if (!snapshotFile.exists()) {
    LOG.warn("The snapshot file {} does not exist for snapshot {}", snapshotFile, snapshot);
    return RaftServerConstants.INVALID_LOG_INDEX;
  }

  final TermIndex last = SimpleStateMachineStorage.getTermIndexFromSnapshotFile(snapshotFile);
  try(AutoCloseableLock writeLock = writeLock();
      ObjectInputStream in = new ObjectInputStream(
          new BufferedInputStream(new FileInputStream(snapshotFile)))) {
    if (reload) {
      reset();
    }
    setLastAppliedTermIndex(last);
    variables.putAll(JavaUtils.cast(in.readObject()));
  } catch (ClassNotFoundException e) {
    throw new IllegalStateException(e);
  }
  return last.getIndex();
}
 
Example 4
Source File: LogStateMachine.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Override
public long takeSnapshot() {
  final TermIndex last;
  try(AutoCloseableLock readLock = readLock()) {
    last = getLastAppliedTermIndex();
  }

  final File snapshotFile =  storage.getSnapshotFile(last.getTerm(), last.getIndex());
  LOG.info("Taking a snapshot to file {}", snapshotFile);

  try(AutoCloseableLock readLock = readLock();
      ObjectOutputStream out = new ObjectOutputStream(
      new BufferedOutputStream(new FileOutputStream(snapshotFile)))) {
    out.writeLong(length);
    out.writeLong(dataRecordsSize);
    out.writeObject(state);
  } catch(IOException ioe) {
    LOG.warn("Failed to write snapshot file \"" + snapshotFile
        + "\", last applied index=" + last);
  }

  return last.getIndex();
}
 
Example 5
Source File: ArithmeticStateMachine.java    From ratis with Apache License 2.0 6 votes vote down vote up
private long load(SingleFileSnapshotInfo snapshot, boolean reload) throws IOException {
  if (snapshot == null) {
    LOG.warn("The snapshot info is null.");
    return RaftServerConstants.INVALID_LOG_INDEX;
  }
  final File snapshotFile = snapshot.getFile().getPath().toFile();
  if (!snapshotFile.exists()) {
    LOG.warn("The snapshot file {} does not exist for snapshot {}", snapshotFile, snapshot);
    return RaftServerConstants.INVALID_LOG_INDEX;
  }

  final TermIndex last = SimpleStateMachineStorage.getTermIndexFromSnapshotFile(snapshotFile);
  try(final AutoCloseableLock writeLock = writeLock();
      final ObjectInputStream in = new ObjectInputStream(
          new BufferedInputStream(new FileInputStream(snapshotFile)))) {
    if (reload) {
      reset();
    }
    setLastAppliedTermIndex(last);
    variables.putAll(JavaUtils.cast(in.readObject()));
  } catch (ClassNotFoundException e) {
    throw new IllegalStateException(e);
  }
  return last.getIndex();
}
 
Example 6
Source File: RaftLog.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
/**
 * Validate the term and index of entry w.r.t RaftLog
 */
protected void validateLogEntry(LogEntryProto entry) {
  if (entry.hasMetadataEntry()) {
    return;
  }
  long latestSnapshotIndex = getSnapshotIndex();
  TermIndex lastTermIndex = getLastEntryTermIndex();
  if (lastTermIndex != null) {
    long lastIndex = lastTermIndex.getIndex() > latestSnapshotIndex ?
        lastTermIndex.getIndex() : latestSnapshotIndex;
    Preconditions.assertTrue(entry.getTerm() >= lastTermIndex.getTerm(),
        "Entry term less than RaftLog's last term: %d, entry: %s", lastTermIndex.getTerm(), entry);
    Preconditions.assertTrue(entry.getIndex() == lastIndex + 1,
        "Difference between entry index and RaftLog's last index %d (or snapshot index %d) " +
            "is greater than 1, entry: %s",
        lastTermIndex.getIndex(), latestSnapshotIndex, entry);
  } else {
    Preconditions.assertTrue(entry.getIndex() == latestSnapshotIndex + 1,
        "Difference between entry index and RaftLog's latest snapshot index %d is greater than 1 " +
            "and in between log entries are not present, entry: %s",
        latestSnapshotIndex, entry);
  }
}
 
Example 7
Source File: LogStateMachine.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Override
public long takeSnapshot() {
  final TermIndex last;
  try(final AutoCloseableLock readLock = readLock()) {
    last = getLastAppliedTermIndex();
  }

  final File snapshotFile =  storage.getSnapshotFile(last.getTerm(), last.getIndex());
  LOG.info("Taking a snapshot to file {}", snapshotFile);

  try(final AutoCloseableLock readLock = readLock();
      final ObjectOutputStream out = new ObjectOutputStream(
      new BufferedOutputStream(new FileOutputStream(snapshotFile)))) {
    out.writeLong(length);
    out.writeObject(state);
  } catch(IOException ioe) {
    LOG.warn("Failed to write snapshot file \"" + snapshotFile
        + "\", last applied index=" + last);
  }

  return last.getIndex();
}
 
Example 8
Source File: RaftStorageTestUtils.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
static void printLog(RaftLog log, Consumer<String> println) {
  if (log == null) {
    println.accept("log == null");
    return;
  }

  final TermIndex last;
  final long flushed, committed;
  try(AutoCloseableLock readlock = log.readLock()) {
    last = log.getLastEntryTermIndex();
    flushed = log.getFlushIndex();
    committed = log.getLastCommittedIndex();
  }
  final StringBuilder b = new StringBuilder();
  for(long i = 0; i <= last.getIndex(); i++) {
    b.setLength(0);
    b.append(i == flushed? 'f': ' ');
    b.append(i == committed? 'c': ' ');
    b.append(String.format("%3d: ", i));
    try {
      b.append(ServerProtoUtils.toLogEntryString(log.get(i)));
    } catch (RaftLogIOException e) {
      b.append(e);
    }
    println.accept(b.toString());
  }
}
 
Example 9
Source File: RaftLogCache.java    From ratis with Apache License 2.0 5 votes vote down vote up
TruncateIndices computeTruncateIndices(Consumer<TermIndex> failClientRequest, LogEntryProto... entries) {
  int arrayIndex = 0;
  long truncateIndex = -1;

  try(AutoCloseableLock readLock = closedSegments.readLock()) {
    final Iterator<TermIndex> i = iterator(entries[0].getIndex());
    for(; i.hasNext() && arrayIndex < entries.length; arrayIndex++) {
      final TermIndex storedEntry = i.next();
      Preconditions.assertTrue(storedEntry.getIndex() == entries[arrayIndex].getIndex(),
          "The stored entry's index %s is not consistent with the received entries[%s]'s index %s",
          storedEntry.getIndex(), arrayIndex, entries[arrayIndex].getIndex());

      if (storedEntry.getTerm() != entries[arrayIndex].getTerm()) {
        // we should truncate from the storedEntry's arrayIndex
        truncateIndex = storedEntry.getIndex();
        if (LOG.isTraceEnabled()) {
          LOG.trace("{}: truncate to {}, arrayIndex={}, ti={}, storedEntry={}, entries={}",
              name, truncateIndex, arrayIndex,
              ServerProtoUtils.toTermIndex(entries[arrayIndex]), storedEntry,
              ServerProtoUtils.toString(entries));
        }

        // fail all requests starting at truncateIndex
        failClientRequest.accept(storedEntry);
        for(; i.hasNext(); ) {
          failClientRequest.accept(i.next());
        }
        break;
      }
    }
  }
  return new TruncateIndices(arrayIndex, truncateIndex);
}
 
Example 10
Source File: LeaderState.java    From ratis with Apache License 2.0 5 votes vote down vote up
private boolean committedConf(TermIndex[] entries) {
  final long currentCommitted = raftLog.getLastCommittedIndex();
  for (TermIndex entry : entries) {
    if (entry.getIndex() <= currentCommitted && raftLog.isConfigEntry(entry)) {
      return true;
    }
  }
  return false;
}
 
Example 11
Source File: RaftServerImpl.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
/**
 * check if the remote peer is not included in the current conf
 * and should shutdown. should shutdown if all the following stands:
 * 1. this is a leader
 * 2. current conf is stable and has been committed
 * 3. candidate id is not included in conf
 * 4. candidate's last entry's index < conf's index
 */
private boolean shouldSendShutdown(RaftPeerId candidateId,
    TermIndex candidateLastEntry) {
  return isLeader()
      && getRaftConf().isStable()
      && getState().isConfCommitted()
      && !getRaftConf().containsInConf(candidateId)
      && candidateLastEntry.getIndex() < getRaftConf().getLogEntryIndex()
      && role.getLeaderState().map(ls -> !ls.isBootStrappingPeer(candidateId)).orElse(false);
}
 
Example 12
Source File: RaftServerImpl.java    From ratis with Apache License 2.0 5 votes vote down vote up
/**
 * check if the remote peer is not included in the current conf
 * and should shutdown. should shutdown if all the following stands:
 * 1. this is a leader
 * 2. current conf is stable and has been committed
 * 3. candidate id is not included in conf
 * 4. candidate's last entry's index < conf's index
 */
private boolean shouldSendShutdown(RaftPeerId candidateId,
    TermIndex candidateLastEntry) {
  return isLeader()
      && getRaftConf().isStable()
      && getState().isConfCommitted()
      && !getRaftConf().containsInConf(candidateId)
      && candidateLastEntry.getIndex() < getRaftConf().getLogEntryIndex()
      && role.getLeaderState().map(ls -> !ls.isBootStrappingPeer(candidateId)).orElse(false);
}
 
Example 13
Source File: LeaderState.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private boolean committedConf(TermIndex[] entries) {
  final long currentCommitted = raftLog.getLastCommittedIndex();
  for (TermIndex entry : entries) {
    if (entry.getIndex() <= currentCommitted && raftLog.isConfigEntry(entry)) {
      return true;
    }
  }
  return false;
}
 
Example 14
Source File: ServerState.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
boolean isLogUpToDate(TermIndex candidateLastEntry) {
  TermIndex local = log.getLastEntryTermIndex();
  // need to take into account snapshot
  SnapshotInfo snapshot = server.getStateMachine().getLatestSnapshot();
   if (local == null && snapshot == null) {
    return true;
  } else if (candidateLastEntry == null) {
    return false;
  }
  if (local == null || (snapshot != null && snapshot.getIndex() > local.getIndex())) {
    local = snapshot.getTermIndex();
  }
  return local.compareTo(candidateLastEntry) <= 0;
}
 
Example 15
Source File: SegmentedRaftLogCache.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
TruncateIndices computeTruncateIndices(Consumer<TermIndex> failClientRequest, LogEntryProto... entries) {
  int arrayIndex = 0;
  long truncateIndex = -1;

  try(AutoCloseableLock readLock = closedSegments.readLock()) {
    final Iterator<TermIndex> i = iterator(entries[0].getIndex());
    for(; i.hasNext() && arrayIndex < entries.length; arrayIndex++) {
      final TermIndex storedEntry = i.next();
      Preconditions.assertTrue(storedEntry.getIndex() == entries[arrayIndex].getIndex(),
          "The stored entry's index %s is not consistent with the received entries[%s]'s index %s",
          storedEntry.getIndex(), arrayIndex, entries[arrayIndex].getIndex());

      if (storedEntry.getTerm() != entries[arrayIndex].getTerm()) {
        // we should truncate from the storedEntry's arrayIndex
        truncateIndex = storedEntry.getIndex();
        if (LOG.isTraceEnabled()) {
          LOG.trace("{}: truncate to {}, arrayIndex={}, ti={}, storedEntry={}, entries={}",
              name, truncateIndex, arrayIndex,
              ServerProtoUtils.toTermIndex(entries[arrayIndex]), storedEntry,
              ServerProtoUtils.toString(entries));
        }

        // fail all requests starting at truncateIndex
        failClientRequest.accept(storedEntry);
        for(; i.hasNext(); ) {
          failClientRequest.accept(i.next());
        }
        break;
      }
    }
  }
  return new TruncateIndices(arrayIndex, truncateIndex);
}
 
Example 16
Source File: LogStateMachine.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private long load(SingleFileSnapshotInfo snapshot, boolean reload) throws IOException {
  if (snapshot == null) {
    LOG.warn("The snapshot info is null.");
    return RaftServerConstants.INVALID_LOG_INDEX;
  }
  final File snapshotFile = snapshot.getFile().getPath().toFile();
  if (!snapshotFile.exists()) {
    LOG.warn("The snapshot file {} does not exist for snapshot {}", snapshotFile, snapshot);
    return RaftServerConstants.INVALID_LOG_INDEX;
  }

  final TermIndex last = SimpleStateMachineStorage.getTermIndexFromSnapshotFile(snapshotFile);
  try(AutoCloseableLock writeLock = writeLock();
      ObjectInputStream in = new ObjectInputStream(
          new BufferedInputStream(new FileInputStream(snapshotFile)))) {
    if (reload) {
      reset();
    }
    setLastAppliedTermIndex(last);
    this.length = in.readLong();
    this.dataRecordsSize = in.readLong();
    this.state = (State) in.readObject();
  } catch (ClassNotFoundException e) {
    throw new IllegalStateException(e);
  }
  return last.getIndex();
}
 
Example 17
Source File: RaftLog.java    From ratis with Apache License 2.0 5 votes vote down vote up
/**
 * @return the index of the next log entry to append.
 */
public long getNextIndex() {
  final TermIndex last = getLastEntryTermIndex();
  if (last == null) {
    // if the log is empty, the last committed index should be consistent with
    // the last index included in the latest snapshot.
    return getLastCommittedIndex() + 1;
  }
  return last.getIndex() + 1;
}
 
Example 18
Source File: OzoneManagerStateMachine.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
/**
 * Take OM Ratis snapshot is a dummy operation as when double buffer
 * flushes the lastAppliedIndex is flushed to DB and that is used as
 * snapshot index.
 *
 * @return the last applied index on the state machine which has been
 * stored in the snapshot file.
 */
@Override
public long takeSnapshot() throws IOException {
  LOG.info("Current Snapshot Index {}", getLastAppliedTermIndex());
  TermIndex lastTermIndex = getLastAppliedTermIndex();
  long lastAppliedIndex = lastTermIndex.getIndex();
  snapshotInfo.updateTermIndex(lastTermIndex.getTerm(),
      lastAppliedIndex);
  ozoneManager.getMetadataManager().getStore().flush();
  return lastAppliedIndex;
}
 
Example 19
Source File: ServerState.java    From ratis with Apache License 2.0 5 votes vote down vote up
boolean isLogUpToDate(TermIndex candidateLastEntry) {
  TermIndex local = log.getLastEntryTermIndex();
  // need to take into account snapshot
  SnapshotInfo snapshot = server.getStateMachine().getLatestSnapshot();
   if (local == null && snapshot == null) {
    return true;
  } else if (candidateLastEntry == null) {
    return false;
  }
  if (local == null || (snapshot != null && snapshot.getIndex() > local.getIndex())) {
    local = snapshot.getTermIndex();
  }
  return local.compareTo(candidateLastEntry) <= 0;
}
 
Example 20
Source File: ContainerStateMachine.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@Override
public long takeSnapshot() throws IOException {
  TermIndex ti = getLastAppliedTermIndex();
  long startTime = Time.monotonicNow();
  if (!isStateMachineHealthy()) {
    String msg =
        "Failed to take snapshot " + " for " + gid + " as the stateMachine"
            + " is unhealthy. The last applied index is at " + ti;
    StateMachineException sme = new StateMachineException(msg);
    LOG.error(msg);
    throw sme;
  }
  if (ti != null && ti.getIndex() != RaftLog.INVALID_LOG_INDEX) {
    final File snapshotFile =
        storage.getSnapshotFile(ti.getTerm(), ti.getIndex());
    LOG.info("{}: Taking a snapshot at:{} file {}", gid, ti, snapshotFile);
    try (FileOutputStream fos = new FileOutputStream(snapshotFile)) {
      persistContainerSet(fos);
      fos.flush();
      // make sure the snapshot file is synced
      fos.getFD().sync();
    } catch (IOException ioe) {
      LOG.error("{}: Failed to write snapshot at:{} file {}", gid, ti,
          snapshotFile);
      throw ioe;
    }
    LOG.info("{}: Finished taking a snapshot at:{} file:{} time:{}", gid, ti,
        snapshotFile, (Time.monotonicNow() - startTime));
    return ti.getIndex();
  }
  return -1;
}