Java Code Examples for org.apache.ratis.statemachine.impl.SimpleStateMachineStorage#getTermIndexFromSnapshotFile()

The following examples show how to use org.apache.ratis.statemachine.impl.SimpleStateMachineStorage#getTermIndexFromSnapshotFile() . 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: TestRaftStorage.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Test
public void testSnapshotFileName() throws Exception {
  final long term = ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
  final long index = ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
  final String name = SimpleStateMachineStorage.getSnapshotFileName(term, index);
  System.out.println("name = " + name);
  final File file = new File(storageDir, name);
  final TermIndex ti = SimpleStateMachineStorage.getTermIndexFromSnapshotFile(file);
  System.out.println("file = " + file);
  Assert.assertEquals(term, ti.getTerm());
  Assert.assertEquals(index, ti.getIndex());
  System.out.println("ti = " + ti);

  final File foo = new File(storageDir, "foo");
  try {
    SimpleStateMachineStorage.getTermIndexFromSnapshotFile(foo);
    Assert.fail();
  } catch(IllegalArgumentException iae) {
    System.out.println("Good " + iae);
  }
}
 
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: TestRaftStorage.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Test
public void testSnapshotFileName() throws Exception {
  final long term = ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
  final long index = ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
  final String name = SimpleStateMachineStorage.getSnapshotFileName(term, index);
  System.out.println("name = " + name);
  final File file = new File(storageDir, name);
  final TermIndex ti = SimpleStateMachineStorage.getTermIndexFromSnapshotFile(file);
  System.out.println("file = " + file);
  Assert.assertEquals(term, ti.getTerm());
  Assert.assertEquals(index, ti.getIndex());
  System.out.println("ti = " + ti);

  final File foo = new File(storageDir, "foo");
  try {
    SimpleStateMachineStorage.getTermIndexFromSnapshotFile(foo);
    Assert.fail();
  } catch(IllegalArgumentException iae) {
    System.out.println("Good " + iae);
  }
}
 
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: 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 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();
}