Search in sources :

Example 6 with TermIndex

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;
}
Also used : TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 7 with TermIndex

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();
}
Also used : AutoCloseableLock(org.apache.ratis.util.AutoCloseableLock) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 8 with TermIndex

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());
    }
}
Also used : TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 9 with TermIndex

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;
}
Also used : SMLogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.SMLogEntryProto) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) MD5Hash(org.apache.ratis.io.MD5Hash) IOException(java.io.IOException) File(java.io.File) LogOutputStream(org.apache.ratis.server.storage.LogOutputStream) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Example 10 with TermIndex

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));
        }
    }
}
Also used : SingleFileSnapshotInfo(org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo) FileInfo(org.apache.ratis.server.storage.FileInfo) FileOutputStream(java.io.FileOutputStream) MD5Hash(org.apache.ratis.io.MD5Hash) IOException(java.io.IOException) TermIndex(org.apache.ratis.server.protocol.TermIndex)

Aggregations

TermIndex (org.apache.ratis.server.protocol.TermIndex)25 IOException (java.io.IOException)8 File (java.io.File)7 AutoCloseableLock (org.apache.ratis.util.AutoCloseableLock)6 SnapshotInfo (org.apache.ratis.statemachine.SnapshotInfo)5 BaseTest (org.apache.ratis.BaseTest)4 LogEntryProto (org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto)4 Test (org.junit.Test)4 Timer (com.codahale.metrics.Timer)3 FileNotFoundException (java.io.FileNotFoundException)3 CompletableFuture (java.util.concurrent.CompletableFuture)3 CompletionException (java.util.concurrent.CompletionException)3 MD5Hash (org.apache.ratis.io.MD5Hash)3 RaftServerImpl (org.apache.ratis.server.impl.RaftServerImpl)3 FileInfo (org.apache.ratis.server.storage.FileInfo)3 SingleFileSnapshotInfo (org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo)3 AbortedException (alluxio.exception.status.AbortedException)2 AlluxioStatusException (alluxio.exception.status.AlluxioStatusException)2 NotFoundException (alluxio.exception.status.NotFoundException)2 FileOutputStream (java.io.FileOutputStream)2