use of org.apache.ratis.server.protocol.TermIndex in project incubator-ratis by apache.
the class BaseStateMachine method updateLastAppliedTermIndex.
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.debug("{}: 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;
}
use of org.apache.ratis.server.protocol.TermIndex in project incubator-ratis by apache.
the class ArithmeticStateMachine method takeSnapshot.
@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();
}
use of org.apache.ratis.server.protocol.TermIndex in project incubator-ratis by apache.
the class TestRaftLogCache method testIterator.
private void testIterator(long startIndex) throws IOException {
Iterator<TermIndex> iterator = cache.iterator(startIndex);
TermIndex prev = null;
while (iterator.hasNext()) {
TermIndex termIndex = iterator.next();
Assert.assertEquals(cache.getLogRecord(termIndex.getIndex()).getTermIndex(), termIndex);
if (prev != null) {
Assert.assertEquals(prev.getIndex() + 1, termIndex.getIndex());
}
prev = termIndex;
}
if (startIndex <= cache.getEndIndex()) {
Assert.assertNotNull(prev);
Assert.assertEquals(cache.getEndIndex(), prev.getIndex());
}
}
use of org.apache.ratis.server.protocol.TermIndex in project incubator-ratis by apache.
the class SimpleStateMachine4Testing method takeSnapshot.
@Override
public long takeSnapshot() {
final TermIndex termIndex = getLastAppliedTermIndex();
if (termIndex.getTerm() <= 0 || termIndex.getIndex() <= 0) {
return RaftServerConstants.INVALID_LOG_INDEX;
}
final long endIndex = termIndex.getIndex();
// TODO: snapshot should be written to a tmp file, then renamed
File snapshotFile = storage.getSnapshotFile(termIndex.getTerm(), termIndex.getIndex());
LOG.debug("Taking a snapshot with t:{}, i:{}, file:{}", termIndex.getTerm(), termIndex.getIndex(), snapshotFile);
try (LogOutputStream out = new LogOutputStream(snapshotFile, false, segmentMaxSize, preallocatedSize, bufferSize)) {
for (final LogEntryProto entry : list) {
if (entry.getIndex() > endIndex) {
break;
} else {
out.write(entry);
}
}
out.flush();
} catch (IOException e) {
LOG.warn("Failed to take snapshot", e);
}
try {
final MD5Hash digest = MD5FileUtil.computeMd5ForFile(snapshotFile);
MD5FileUtil.saveMD5File(snapshotFile, digest);
} catch (IOException e) {
LOG.warn("Hit IOException when computing MD5 for snapshot file " + snapshotFile, e);
}
try {
this.storage.loadLatestSnapshot();
} catch (IOException e) {
LOG.warn("Hit IOException when loading latest snapshot for snapshot file " + snapshotFile, e);
}
// TODO: purge log segments
return endIndex;
}
use of org.apache.ratis.server.protocol.TermIndex in project alluxio by Alluxio.
the class SnapshotDownloader method onNextInternal.
private void onNextInternal(R response) throws IOException {
TermIndex termIndex = TermIndex.valueOf(mDataGetter.apply(response).getSnapshotTerm(), mDataGetter.apply(response).getSnapshotIndex());
if (mTermIndex == null) {
LOG.info("Downloading new snapshot {} from {}", termIndex, mSource);
mTermIndex = termIndex;
// start a new file
mTempFile = RaftJournalUtils.createTempSnapshotFile(mStorage);
mTempFile.deleteOnExit();
mStream.onNext(mMessageBuilder.apply(0L));
} else {
if (!termIndex.equals(mTermIndex)) {
throw new IOException(String.format("Mismatched term index when downloading the snapshot. expected: %s actual: %s", mTermIndex, termIndex));
}
if (!mDataGetter.apply(response).hasChunk()) {
throw new IOException(String.format("A chunk for file %s is missing from the response %s.", mTempFile, response));
}
// write the chunk
if (mOutputStream == null) {
LOG.info("Start writing to temporary file {}", mTempFile.getPath());
mOutputStream = new FileOutputStream(mTempFile);
}
long position = mOutputStream.getChannel().position();
if (position != mDataGetter.apply(response).getOffset()) {
throw new IOException(String.format("Mismatched offset in file %d, expect %d, bytes written %d", position, mDataGetter.apply(response).getOffset(), mBytesWritten));
}
mOutputStream.write(mDataGetter.apply(response).getChunk().toByteArray());
mBytesWritten += mDataGetter.apply(response).getChunk().size();
LOG.debug("Written {} bytes to snapshot file {}", mBytesWritten, mTempFile.getPath());
if (mDataGetter.apply(response).getEof()) {
LOG.debug("Completed writing to temporary file {} with size {}", mTempFile.getPath(), mOutputStream.getChannel().position());
mOutputStream.close();
mOutputStream = null;
final MD5Hash digest = MD5FileUtil.computeMd5ForFile(mTempFile);
mSnapshotToInstall = new SingleFileSnapshotInfo(new FileInfo(mTempFile.toPath(), digest), mTermIndex.getTerm(), mTermIndex.getIndex());
mFuture.complete(mTermIndex);
LOG.info("Finished copying snapshot to local file {}.", mTempFile);
mStream.onCompleted();
} else {
mStream.onNext(mMessageBuilder.apply(mBytesWritten));
}
}
}
Aggregations