org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo Java Examples

The following examples show how to use org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo. 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: ContainerStateMachine.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
private long loadSnapshot(SingleFileSnapshotInfo snapshot)
    throws IOException {
  if (snapshot == null) {
    TermIndex empty =
        TermIndex.newTermIndex(0, RaftLog.INVALID_LOG_INDEX);
    LOG.info("{}: The snapshot info is null. Setting the last applied index" +
            "to:{}", gid, empty);
    setLastAppliedTermIndex(empty);
    return empty.getIndex();
  }

  final File snapshotFile = snapshot.getFile().getPath().toFile();
  final TermIndex last =
      SimpleStateMachineStorage.getTermIndexFromSnapshotFile(snapshotFile);
  LOG.info("{}: Setting the last applied index to {}", gid, last);
  setLastAppliedTermIndex(last);

  // initialize the dispatcher with snapshot so that it build the missing
  // container list
  buildMissingContainerSet(snapshotFile);
  return last.getIndex();
}
 
Example #2
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 #3
Source File: InstallSnapshotNotificationTests.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<TermIndex> notifyInstallSnapshotFromLeader(
    RaftProtos.RoleInfoProto roleInfoProto,
    TermIndex termIndex) {
  final SingleFileSnapshotInfo leaderSnapshotInfo = (SingleFileSnapshotInfo) leaderSnapshotInfoRef.get();
  LOG.info("{}: leaderSnapshotInfo = {}", getId(), leaderSnapshotInfo);
  if (leaderSnapshotInfo == null) {
    return super.notifyInstallSnapshotFromLeader(roleInfoProto, termIndex);
  }

  try {
    Path leaderSnapshotFile = leaderSnapshotInfo.getFile().getPath();
    File followerSnapshotFilePath = new File(getSMdir(),
        leaderSnapshotFile.getFileName().toString());
    Files.copy(leaderSnapshotFile, followerSnapshotFilePath.toPath());
  } catch (IOException e) {
    LOG.error("Failed notifyInstallSnapshotFromLeader", e);
    return JavaUtils.completeExceptionally(e);
  }
  return CompletableFuture.completedFuture(leaderSnapshotInfo.getTermIndex());
}
 
Example #4
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 #5
Source File: LogStateMachine.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);
    this.length = in.readLong();
    this.state = (State) in.readObject();
  } catch (ClassNotFoundException e) {
    throw new IllegalStateException(e);
  }
  return last.getIndex();
}
 
Example #6
Source File: TestFreonWithDatanodeFastRestart.java    From hadoop-ozone with Apache License 2.0 5 votes vote down vote up
@Test
@Ignore("TODO:HDDS-1160")
public void testRestart() throws Exception {
  startFreon();
  StateMachine sm = getStateMachine();
  TermIndex termIndexBeforeRestart = sm.getLastAppliedTermIndex();
  cluster.restartHddsDatanode(0, false);
  sm = getStateMachine();
  SimpleStateMachineStorage storage =
      (SimpleStateMachineStorage)sm.getStateMachineStorage();
  SingleFileSnapshotInfo snapshotInfo = storage.getLatestSnapshot();
  TermIndex termInSnapshot = snapshotInfo.getTermIndex();
  String expectedSnapFile =
      storage.getSnapshotFile(termIndexBeforeRestart.getTerm(),
          termIndexBeforeRestart.getIndex()).getAbsolutePath();
  Assert.assertEquals(expectedSnapFile,
      snapshotInfo.getFile().getPath().toString());
  Assert.assertEquals(termInSnapshot, termIndexBeforeRestart);

  // After restart the term index might have progressed to apply pending
  // transactions.
  TermIndex termIndexAfterRestart = sm.getLastAppliedTermIndex();
  Assert.assertTrue(termIndexAfterRestart.getIndex() >=
      termIndexBeforeRestart.getIndex());
  // TODO: fix me
  // Give some time for the datanode to register again with SCM.
  // If we try to use the pipeline before the datanode registers with SCM
  // we end up in "NullPointerException: scmId cannot be null" in
  // datanode statemachine and datanode crashes.
  // This has to be fixed. Check HDDS-830.
  // Until then this sleep should help us!
  Thread.sleep(5000);
  startFreon();
}
 
Example #7
Source File: CounterStateMachine.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
/**
 * Load the state of the state machine from the storage.
 *
 * @param snapshot to load
 * @return the index of the snapshot or -1 if snapshot is invalid
 * @throws IOException if any error happens during read from storage
 */
private long load(SingleFileSnapshotInfo snapshot) throws IOException {
  //check the snapshot nullity
  if (snapshot == null) {
    LOG.warn("The snapshot info is null.");
    return RaftLog.INVALID_LOG_INDEX;
  }

  //check the existance of the snapshot file
  final File snapshotFile = snapshot.getFile().getPath().toFile();
  if (!snapshotFile.exists()) {
    LOG.warn("The snapshot file {} does not exist for snapshot {}",
        snapshotFile, snapshot);
    return RaftLog.INVALID_LOG_INDEX;
  }

  //load the TermIndex object for the snapshot using the file name pattern of
  // the snapshot
  final TermIndex last =
      SimpleStateMachineStorage.getTermIndexFromSnapshotFile(snapshotFile);

  //read the file and cast it to the AtomicInteger and set the counter
  try (ObjectInputStream in = new ObjectInputStream(
      new BufferedInputStream(new FileInputStream(snapshotFile)))) {
    //set the last applied termIndex to the termIndex of the snapshot
    setLastAppliedTermIndex(last);

    //read, cast and set the counter
    counter = JavaUtils.cast(in.readObject());
  } catch (ClassNotFoundException e) {
    throw new IllegalStateException(e);
  }

  return last.getIndex();
}
 
Example #8
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 #9
Source File: SimpleStateMachine4Testing.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
private synchronized long loadSnapshot(SingleFileSnapshotInfo snapshot)
    throws IOException {
  if (snapshot == null || !snapshot.getFile().getPath().toFile().exists()) {
    LOG.info("The snapshot file {} does not exist",
        snapshot == null ? null : snapshot.getFile());
    return RaftServerConstants.INVALID_LOG_INDEX;
  } else {
    LOG.info("Loading snapshot {}", snapshot);
    final long endIndex = snapshot.getIndex();
    try (SegmentedRaftLogInputStream in = new SegmentedRaftLogInputStream(
        snapshot.getFile().getPath().toFile(), 0, endIndex, false)) {
      LogEntryProto entry;
      while ((entry = in.nextEntry()) != null) {
        put(entry);
        updateLastAppliedTermIndex(entry.getTerm(), entry.getIndex());
      }
    }
    // The end index is greater than last entry in indexMap as it also
    // includes the configuration and metadata entries
    Preconditions.assertTrue(
        !indexMap.isEmpty() && endIndex >= indexMap.lastKey(),
        "endIndex=%s, indexMap=%s", endIndex, indexMap);
    this.endIndexLastCkpt = endIndex;
    setLastAppliedTermIndex(snapshot.getTermIndex());
    this.storage.loadLatestSnapshot();
    return endIndex;
  }
}
 
Example #10
Source File: SimpleStateMachine4Testing.java    From ratis with Apache License 2.0 5 votes vote down vote up
private synchronized long loadSnapshot(SingleFileSnapshotInfo snapshot)
    throws IOException {
  if (snapshot == null || !snapshot.getFile().getPath().toFile().exists()) {
    LOG.info("The snapshot file {} does not exist",
        snapshot == null ? null : snapshot.getFile());
    return RaftServerConstants.INVALID_LOG_INDEX;
  } else {
    LOG.info("Loading snapshot with t:{}, i:{}, file:{}", snapshot.getTerm(),
        snapshot.getIndex(), snapshot.getFile().getPath());
    final long endIndex = snapshot.getIndex();
    try (LogInputStream in = new LogInputStream(
        snapshot.getFile().getPath().toFile(), 0, endIndex, false)) {
      LogEntryProto entry;
      while ((entry = in.nextEntry()) != null) {
        put(entry);
        updateLastAppliedTermIndex(entry.getTerm(), entry.getIndex());
      }
    }
    Preconditions.assertTrue(
        !indexMap.isEmpty() && endIndex == indexMap.lastKey(),
        "endIndex=%s, indexMap=%s", endIndex, indexMap);
    this.endIndexLastCkpt = endIndex;
    setLastAppliedTermIndex(snapshot.getTermIndex());
    this.storage.loadLatestSnapshot();
    return endIndex;
  }
}
 
Example #11
Source File: ArithmeticStateMachine.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
public long loadSnapshot(SingleFileSnapshotInfo snapshot) throws IOException {
  return load(snapshot, false);
}
 
Example #12
Source File: LogStateMachine.java    From incubator-ratis with Apache License 2.0 4 votes vote down vote up
private long loadSnapshot(SingleFileSnapshotInfo snapshot) throws IOException {
  return load(snapshot, false);
}
 
Example #13
Source File: ArithmeticStateMachine.java    From ratis with Apache License 2.0 4 votes vote down vote up
public long loadSnapshot(SingleFileSnapshotInfo snapshot) throws IOException {
  return load(snapshot, false);
}
 
Example #14
Source File: LogStateMachine.java    From ratis with Apache License 2.0 4 votes vote down vote up
private long loadSnapshot(SingleFileSnapshotInfo snapshot) throws IOException {
  return load(snapshot, false);
}