org.apache.ratis.server.protocol.TermIndex Java Examples

The following examples show how to use org.apache.ratis.server.protocol.TermIndex. 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: LogAppender.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
private void assertProtos(List<LogEntryProto> protos, long nextIndex, TermIndex previous, long snapshotIndex) {
  if (protos.isEmpty()) {
    return;
  }
  final long firstIndex = protos.get(0).getIndex();
  Preconditions.assertTrue(firstIndex == nextIndex,
      () -> follower.getName() + ": firstIndex = " + firstIndex + " != nextIndex = " + nextIndex);
  if (firstIndex > RaftLog.LEAST_VALID_LOG_INDEX) {
    // Check if nextIndex is 1 greater than the snapshotIndex. If yes, then
    // we do not have to check for the existence of previous.
    if (nextIndex != snapshotIndex + 1) {
      Objects.requireNonNull(previous,
          () -> follower.getName() + ": Previous TermIndex not found for firstIndex = " + firstIndex);
      Preconditions.assertTrue(previous.getIndex() == firstIndex - 1,
          () -> follower.getName() + ": Previous = " + previous + " but firstIndex = " + firstIndex);
    }
  }
}
 
Example #2
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 #3
Source File: RaftServerImpl.java    From ratis with Apache License 2.0 6 votes vote down vote up
private void preAppendEntriesAsync(RaftPeerId leaderId, RaftGroupId leaderGroupId, long leaderTerm,
    TermIndex previous, long leaderCommit, boolean initializing, LogEntryProto... entries) throws IOException {
  CodeInjectionForTesting.execute(APPEND_ENTRIES, getId(),
      leaderId, leaderTerm, previous, leaderCommit, initializing, entries);

  final LifeCycle.State currentState = assertLifeCycleState(STARTING, RUNNING);
  if (currentState == STARTING) {
    if (role.getCurrentRole() == null) {
      throw new ServerNotReadyException("The role of Server " + getId() + " is not yet initialized.");
    }
  }
  assertGroup(leaderId, leaderGroupId);

  try {
    validateEntries(leaderTerm, previous, entries);
  } catch (IllegalArgumentException e) {
    throw new IOException(e);
  }
}
 
Example #4
Source File: RaftServerImpl.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
private void preAppendEntriesAsync(RaftPeerId leaderId, RaftGroupId leaderGroupId, long leaderTerm,
    TermIndex previous, long leaderCommit, boolean initializing, LogEntryProto... entries) throws IOException {
  CodeInjectionForTesting.execute(APPEND_ENTRIES, getId(),
      leaderId, leaderTerm, previous, leaderCommit, initializing, entries);

  assertLifeCycleState(LifeCycle.States.STARTING_OR_RUNNING);
  if (!startComplete.get()) {
    throw new ServerNotReadyException(getMemberId() + ": The server role is not yet initialized.");
  }
  assertGroup(leaderId, leaderGroupId);

  try {
    validateEntries(leaderTerm, previous, entries);
  } catch (IllegalArgumentException e) {
    throw new IOException(e);
  }
}
 
Example #5
Source File: RaftLogCache.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Override
public TermIndex next() {
  LogRecord record;
  if (currentSegment == null ||
      (record = currentSegment.getLogRecord(nextIndex)) == null) {
    throw new NoSuchElementException();
  }
  if (++nextIndex > currentSegment.getEndIndex()) {
    if (currentSegment != openSegment) {
      segmentIndex++;
      currentSegment = segmentIndex == closedSegments.size() ?
          openSegment : closedSegments.get(segmentIndex);
    }
  }
  return record.getTermIndex();
}
 
Example #6
Source File: OzoneManagerStateMachine.java    From hadoop-ozone with Apache License 2.0 6 votes vote down vote up
/**
 * Unpause the StateMachine, re-initialize the DoubleBuffer and update the
 * lastAppliedIndex. This should be done after uploading new state to the
 * StateMachine.
 */
public void unpause(long newLastAppliedSnaphsotIndex,
    long newLastAppliedSnapShotTermIndex) {
  getLifeCycle().startAndTransition(() -> {
    this.ozoneManagerDoubleBuffer =
        new OzoneManagerDoubleBuffer.Builder()
            .setOmMetadataManager(ozoneManager.getMetadataManager())
            .setOzoneManagerRatisSnapShot(this::updateLastAppliedIndex)
            .enableRatis(true)
            .enableTracing(isTracingEnabled)
            .build();
    handler.updateDoubleBuffer(ozoneManagerDoubleBuffer);
    this.setLastAppliedTermIndex(TermIndex.newTermIndex(
        newLastAppliedSnapShotTermIndex, newLastAppliedSnaphsotIndex));
  });
}
 
Example #7
Source File: RaftTestUtil.java    From ratis with Apache License 2.0 6 votes vote down vote up
static void checkLogEntries(RaftLog log, SimpleMessage[] expectedMessages,
    Predicate<LogEntryProto> predicate) {
  TermIndex[] termIndices = log.getEntries(0, Long.MAX_VALUE);
  for (int i = 0; i < termIndices.length; i++) {
    for (int j = 0; j < expectedMessages.length; j++) {
      final LogEntryProto e;
      try {
        e = log.get(termIndices[i].getIndex());
        if (Arrays.equals(expectedMessages[j].getContent().toByteArray(),
            e.getStateMachineLogEntry().getLogData().toByteArray())) {
          Assert.assertTrue(predicate.test(e));
        }
      } catch (IOException exception) {
        exception.printStackTrace();
      }
    }
  }
}
 
Example #8
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 #9
Source File: SegmentedRaftLogCache.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Override
public TermIndex next() {
  LogRecord record;
  if (currentSegment == null ||
      (record = currentSegment.getLogRecord(nextIndex)) == null) {
    throw new NoSuchElementException();
  }
  if (++nextIndex > currentSegment.getEndIndex()) {
    if (currentSegment != openSegment) {
      segmentIndex++;
      currentSegment = segmentIndex == closedSegments.size() ?
          openSegment : closedSegments.get(segmentIndex);
    }
  }
  return record.getTermIndex();
}
 
Example #10
Source File: TestLogSegment.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
static void checkLogSegment(LogSegment segment, long start, long end,
    boolean isOpen, long totalSize, long term) throws Exception {
  Assert.assertEquals(start, segment.getStartIndex());
  Assert.assertEquals(end, segment.getEndIndex());
  Assert.assertEquals(isOpen, segment.isOpen());
  Assert.assertEquals(totalSize, segment.getTotalSize());

  long offset = SegmentedRaftLogFormat.getHeaderLength();
  for (long i = start; i <= end; i++) {
    LogSegment.LogRecord record = segment.getLogRecord(i);
    final TermIndex ti = record.getTermIndex();
    Assert.assertEquals(i, ti.getIndex());
    Assert.assertEquals(term, ti.getTerm());
    Assert.assertEquals(offset, record.getOffset());

    LogEntryProto entry = segment.getEntryFromCache(ti);
    if (entry == null) {
      entry = segment.loadCache(record);
    }
    offset += getEntrySize(entry);
  }
}
 
Example #11
Source File: RaftLogCache.java    From ratis with Apache License 2.0 6 votes vote down vote up
/**
 * @param startIndex inclusive
 * @param endIndex exclusive
 */
TermIndex[] getTermIndices(final long startIndex, final long endIndex) {
  if (startIndex < 0 || startIndex < getStartIndex()) {
    throw new IndexOutOfBoundsException("startIndex = " + startIndex
        + ", log cache starts from index " + getStartIndex());
  }
  if (startIndex > endIndex) {
    throw new IndexOutOfBoundsException("startIndex(" + startIndex
        + ") > endIndex(" + endIndex + ")");
  }
  final long realEnd = Math.min(getEndIndex() + 1, endIndex);
  if (startIndex >= realEnd) {
    return TermIndex.EMPTY_TERMINDEX_ARRAY;
  }
  return closedSegments.getTermIndex(startIndex, realEnd, openSegment);
}
 
Example #12
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 #13
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 #14
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 #15
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 #16
Source File: ServerProtoUtils.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("checkstyle:parameternumber")
static InstallSnapshotRequestProto toInstallSnapshotRequestProto(
    RaftGroupMemberId requestorId, RaftPeerId replyId, String requestId, int requestIndex,
    long term, TermIndex lastTermIndex, List<FileChunkProto> chunks,
    long totalSize, boolean done) {
  final InstallSnapshotRequestProto.SnapshotChunkProto.Builder snapshotChunkProto =
      InstallSnapshotRequestProto.SnapshotChunkProto.newBuilder()
          .setRequestId(requestId)
          .setRequestIndex(requestIndex)
          .setTermIndex(toTermIndexProto(lastTermIndex))
          .addAllFileChunks(chunks)
          .setTotalSize(totalSize)
          .setDone(done);
  return InstallSnapshotRequestProto.newBuilder()
      .setServerRequest(toRaftRpcRequestProtoBuilder(requestorId, replyId))
      // .setRaftConfiguration()  TODO: save and pass RaftConfiguration
      .setLeaderTerm(term)
      .setSnapshotChunk(snapshotChunkProto)
      .build();
}
 
Example #17
Source File: RaftLog.java    From ratis with Apache License 2.0 6 votes vote down vote up
/**
 * Update the last committed index.
 * @param majorityIndex the index that has achieved majority.
 * @param currentTerm the current term.
 * @return true if update is applied; otherwise, return false, i.e. no update required.
 */
public boolean updateLastCommitted(long majorityIndex, long currentTerm) {
  try(AutoCloseableLock writeLock = writeLock()) {
    final long oldCommittedIndex = getLastCommittedIndex();
    if (oldCommittedIndex < majorityIndex) {
      // Only update last committed index for current term. See ยง5.4.2 in
      // paper for details.
      final TermIndex entry = getTermIndex(majorityIndex);
      if (entry != null && entry.getTerm() == currentTerm) {
        final long newCommitIndex = Math.min(majorityIndex, getLatestFlushedIndex());
        if (newCommitIndex > oldCommittedIndex) {
          commitIndex.updateIncreasingly(newCommitIndex, traceIndexChange);
        }
        return true;
      }
    }
  }
  return false;
}
 
Example #18
Source File: RaftLogCache.java    From ratis with Apache License 2.0 6 votes vote down vote up
TermIndex[] getTermIndex(long startIndex, long realEnd, LogSegment openSegment) {
  final TermIndex[] entries = new TermIndex[Math.toIntExact(realEnd - startIndex)];
  final int searchIndex;
  long index = startIndex;

  try(AutoCloseableLock readLock = readLock()) {
    searchIndex = Collections.binarySearch(segments, startIndex);
    if (searchIndex >= 0) {
      for(int i = searchIndex; i < segments.size() && index < realEnd; i++) {
        final LogSegment s = segments.get(i);
        final int numberFromSegment = Math.toIntExact(Math.min(realEnd - index, s.getEndIndex() - index + 1));
        getFromSegment(s, index, entries, Math.toIntExact(index - startIndex), numberFromSegment);
        index += numberFromSegment;
      }
    }
  }

  // openSegment is read outside the lock.
  if (searchIndex < 0) {
    getFromSegment(openSegment, startIndex, entries, 0, entries.length);
  } else if (index < realEnd) {
    getFromSegment(openSegment, index, entries,
        Math.toIntExact(index - startIndex), Math.toIntExact(realEnd - index));
  }
  return entries;
}
 
Example #19
Source File: RaftServerImpl.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Override
public CompletableFuture<AppendEntriesReplyProto> appendEntriesAsync(AppendEntriesRequestProto r)
    throws IOException {
  // TODO avoid converting list to array
  final RaftRpcRequestProto request = r.getServerRequest();
  final LogEntryProto[] entries = r.getEntriesList()
      .toArray(new LogEntryProto[r.getEntriesCount()]);
  final TermIndex previous = r.hasPreviousLog() ?
      ServerProtoUtils.toTermIndex(r.getPreviousLog()) : null;
  final RaftPeerId requestorId = RaftPeerId.valueOf(request.getRequestorId());

  preAppendEntriesAsync(requestorId, ProtoUtils.toRaftGroupId(request.getRaftGroupId()), r.getLeaderTerm(),
      previous, r.getLeaderCommit(), r.getInitializing(), entries);
  try {
    return appendEntriesAsync(requestorId, r.getLeaderTerm(), previous, r.getLeaderCommit(),
        request.getCallId(), r.getInitializing(), r.getCommitInfosList(), entries);
  } catch(Throwable t) {
    LOG.error(getId() + ": Failed appendEntriesAsync " + r, t);
    throw t;
  }
}
 
Example #20
Source File: MemoryRaftLog.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
@Override
public TermIndex[] getEntries(long startIndex, long endIndex) {
  checkLogState();
  try(AutoCloseableLock readLock = readLock()) {
    if (startIndex >= entries.size()) {
      return null;
    }
    final int from = Math.toIntExact(startIndex);
    final int to = Math.toIntExact(Math.min(entries.size(), endIndex));
    TermIndex[] ti = new TermIndex[to - from];
    for (int i = 0; i < ti.length; i++) {
      ti[i] = entries.getTermIndex(i);
    }
    return ti;
  }
}
 
Example #21
Source File: MemoryRaftLog.java    From ratis with Apache License 2.0 6 votes vote down vote up
@Override
public TermIndex[] getEntries(long startIndex, long endIndex) {
  checkLogState();
  try(AutoCloseableLock readLock = readLock()) {
    if (startIndex >= entries.size()) {
      return null;
    }
    final int from = Math.toIntExact(startIndex);
    final int to = Math.toIntExact(Math.min(entries.size(), endIndex));
    TermIndex[] ti = new TermIndex[to - from];
    for (int i = 0; i < ti.length; i++) {
      ti[i] = entries.getTermIndex(i);
    }
    return ti;
  }
}
 
Example #22
Source File: TestSegmentedRaftLog.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
private void checkEntries(RaftLog raftLog, List<LogEntryProto> expected,
    int offset, int size) throws IOException {
  if (size > 0) {
    for (int i = offset; i < size + offset; i++) {
      LogEntryProto entry = raftLog.get(expected.get(i).getIndex());
      Assert.assertEquals(expected.get(i), entry);
    }
    TermIndex[] termIndices = raftLog.getEntries(
        expected.get(offset).getIndex(),
        expected.get(offset + size - 1).getIndex() + 1);
    LogEntryProto[] entriesFromLog = Arrays.stream(termIndices)
        .map(ti -> {
          try {
            return raftLog.get(ti.getIndex());
          } catch (IOException e) {
            throw new RuntimeException(e);
          }
        })
        .toArray(LogEntryProto[]::new);
    LogEntryProto[] expectedArray = expected.subList(offset, offset + size)
        .stream().toArray(LogEntryProto[]::new);
    Assert.assertArrayEquals(expectedArray, entriesFromLog);
  }
}
 
Example #23
Source File: BaseStateMachine.java    From incubator-ratis with Apache License 2.0 6 votes vote down vote up
protected boolean updateLastAppliedTermIndex(long term, long index) {
  final TermIndex newTI = TermIndex.newTermIndex(term, index);
  final TermIndex oldTI = lastAppliedTermIndex.getAndSet(newTI);
  if (!newTI.equals(oldTI)) {
    LOG.trace("{}: update lastAppliedTermIndex from {} to {}", getId(), oldTI, newTI);
    if (oldTI != null) {
      Preconditions.assertTrue(newTI.compareTo(oldTI) >= 0,
          () -> getId() + ": Failed updateLastAppliedTermIndex: newTI = "
              + newTI + " < oldTI = " + oldTI);
    }
    return true;
  }

  synchronized (transactionFutures) {
    for(long i; !transactionFutures.isEmpty() && (i = transactionFutures.firstKey()) <= index; ) {
      transactionFutures.remove(i).complete(null);
    }
  }
  return false;
}
 
Example #24
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 #25
Source File: MemoryRaftLog.java    From ratis with Apache License 2.0 5 votes vote down vote up
@Override
public TermIndex getLastEntryTermIndex() {
  checkLogState();
  try(AutoCloseableLock readLock = readLock()) {
    return entries.getTermIndex(entries.size() - 1);
  }
}
 
Example #26
Source File: MemoryRaftLog.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
@Override
public TermIndex getTermIndex(long index) {
  checkLogState();
  try(AutoCloseableLock readLock = readLock()) {
    return entries.getTermIndex(Math.toIntExact(index));
  }
}
 
Example #27
Source File: SimpleStateMachineStorage.java    From incubator-ratis with Apache License 2.0 5 votes vote down vote up
public static TermIndex getTermIndexFromSnapshotFile(File file) {
  final String name = file.getName();
  final Matcher m = SNAPSHOT_REGEX.matcher(name);
  if (!m.matches()) {
    throw new IllegalArgumentException("File \"" + file
        + "\" does not match snapshot file name pattern \""
        + SNAPSHOT_REGEX + "\"");
  }
  final long term = Long.parseLong(m.group(1));
  final long index = Long.parseLong(m.group(2));
  return TermIndex.newTermIndex(term, index);
}
 
Example #28
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;
}
 
Example #29
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 #30
Source File: RaftLogCache.java    From ratis with Apache License 2.0 5 votes vote down vote up
private static void getFromSegment(LogSegment segment, long startIndex,
    TermIndex[] entries, int offset, int size) {
  long endIndex = segment.getEndIndex();
  endIndex = Math.min(endIndex, startIndex + size - 1);
  int index = offset;
  for (long i = startIndex; i <= endIndex; i++) {
    LogRecord r = segment.getLogRecord(i);
    entries[index++] = r == null ? null : r.getTermIndex();
  }
}