Search in sources :

Example 6 with SnapshotReader

use of com.alipay.sofa.jraft.storage.snapshot.SnapshotReader in project sofa-jraft by sofastack.

the class ReplicatorTest method testInstallSnapshot.

@Test
public void testInstallSnapshot() {
    final Replicator r = getReplicator();
    this.id.unlock();
    final Future<Message> rpcInFly = r.getRpcInFly();
    assertNotNull(rpcInFly);
    final SnapshotReader reader = Mockito.mock(SnapshotReader.class);
    Mockito.when(this.snapshotStorage.open()).thenReturn(reader);
    final String uri = "remote://localhost:8081/99";
    Mockito.when(reader.generateURIForCopy()).thenReturn(uri);
    final RaftOutter.SnapshotMeta meta = // 
    RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(// 
    11).setLastIncludedTerm(// 
    1).build();
    Mockito.when(reader.load()).thenReturn(meta);
    assertEquals(0, r.statInfo.lastLogIncluded);
    assertEquals(0, r.statInfo.lastTermIncluded);
    final RpcRequests.InstallSnapshotRequest.Builder rb = RpcRequests.InstallSnapshotRequest.newBuilder();
    rb.setTerm(this.opts.getTerm());
    rb.setGroupId(this.opts.getGroupId());
    rb.setServerId(this.opts.getServerId().toString());
    rb.setPeerId(this.opts.getPeerId().toString());
    rb.setMeta(meta);
    rb.setUri(uri);
    Mockito.when(this.rpcService.installSnapshot(Matchers.eq(this.opts.getPeerId().getEndpoint()), eq(rb.build()), Mockito.any())).thenReturn(new FutureImpl<>());
    r.installSnapshot();
    assertNotNull(r.getRpcInFly());
    assertNotSame(r.getRpcInFly(), rpcInFly);
    Assert.assertEquals(Replicator.RunningState.INSTALLING_SNAPSHOT, r.statInfo.runningState);
    assertEquals(11, r.statInfo.lastLogIncluded);
    assertEquals(1, r.statInfo.lastTermIncluded);
}
Also used : Message(com.google.protobuf.Message) RaftOutter(com.alipay.sofa.jraft.entity.RaftOutter) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 7 with SnapshotReader

use of com.alipay.sofa.jraft.storage.snapshot.SnapshotReader in project sofa-jraft by sofastack.

the class FSMCallerTest method testOnSnapshotLoadStale.

@Test
public void testOnSnapshotLoadStale() throws Exception {
    final SnapshotReader reader = Mockito.mock(SnapshotReader.class);
    final SnapshotMeta meta = SnapshotMeta.newBuilder().setLastIncludedIndex(5).setLastIncludedTerm(1).build();
    Mockito.when(reader.load()).thenReturn(meta);
    final CountDownLatch latch = new CountDownLatch(1);
    this.fsmCaller.onSnapshotLoad(new LoadSnapshotClosure() {

        @Override
        public void run(final Status status) {
            assertFalse(status.isOk());
            assertEquals(RaftError.ESTALE, status.getRaftError());
            latch.countDown();
        }

        @Override
        public SnapshotReader start() {
            return reader;
        }
    });
    latch.await();
    assertEquals(this.fsmCaller.getLastAppliedIndex(), 10);
}
Also used : LoadSnapshotClosure(com.alipay.sofa.jraft.closure.LoadSnapshotClosure) Status(com.alipay.sofa.jraft.Status) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) SnapshotMeta(com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 8 with SnapshotReader

use of com.alipay.sofa.jraft.storage.snapshot.SnapshotReader in project sofa-jraft by sofastack.

the class FSMCallerTest method testOnSnapshotLoad.

@Test
public void testOnSnapshotLoad() throws Exception {
    final SnapshotReader reader = Mockito.mock(SnapshotReader.class);
    final SnapshotMeta meta = SnapshotMeta.newBuilder().setLastIncludedIndex(12).setLastIncludedTerm(1).build();
    Mockito.when(reader.load()).thenReturn(meta);
    Mockito.when(this.fsm.onSnapshotLoad(reader)).thenReturn(true);
    final CountDownLatch latch = new CountDownLatch(1);
    this.fsmCaller.onSnapshotLoad(new LoadSnapshotClosure() {

        @Override
        public void run(final Status status) {
            assertTrue(status.isOk());
            latch.countDown();
        }

        @Override
        public SnapshotReader start() {
            return reader;
        }
    });
    latch.await();
    assertEquals(this.fsmCaller.getLastAppliedIndex(), 12);
    Mockito.verify(this.fsm).onConfigurationCommitted(Mockito.any());
}
Also used : LoadSnapshotClosure(com.alipay.sofa.jraft.closure.LoadSnapshotClosure) Status(com.alipay.sofa.jraft.Status) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) SnapshotMeta(com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 9 with SnapshotReader

use of com.alipay.sofa.jraft.storage.snapshot.SnapshotReader in project sofa-jraft by sofastack.

the class LocalSnapshotCopier method filter.

private void filter() throws IOException {
    this.writer = (LocalSnapshotWriter) this.storage.create(!this.filterBeforeCopyRemote);
    if (this.writer == null) {
        setError(RaftError.EIO, "Fail to create snapshot writer");
        return;
    }
    if (this.filterBeforeCopyRemote) {
        final SnapshotReader reader = this.storage.open();
        if (!filterBeforeCopy(this.writer, reader)) {
            LOG.warn("Fail to filter writer before copying, destroy and create a new writer.");
            this.writer.setError(-1, "Fail to filter");
            Utils.closeQuietly(this.writer);
            this.writer = (LocalSnapshotWriter) this.storage.create(true);
        }
        if (reader != null) {
            Utils.closeQuietly(reader);
        }
        if (this.writer == null) {
            setError(RaftError.EIO, "Fail to create snapshot writer");
            return;
        }
    }
    this.writer.saveMeta(this.remoteSnapshot.getMetaTable().getMeta());
    if (!this.writer.sync()) {
        LOG.error("Fail to sync snapshot writer path={}", this.writer.getPath());
        setError(RaftError.EIO, "Fail to sync snapshot writer");
    }
}
Also used : SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)

Example 10 with SnapshotReader

use of com.alipay.sofa.jraft.storage.snapshot.SnapshotReader in project sofa-jraft by sofastack.

the class LocalSnapshotStorage method open.

@Override
public SnapshotReader open() {
    long lsIndex = 0;
    this.lock.lock();
    try {
        if (this.lastSnapshotIndex != 0) {
            lsIndex = this.lastSnapshotIndex;
            ref(lsIndex);
        }
    } finally {
        this.lock.unlock();
    }
    if (lsIndex == 0) {
        LOG.warn("No data for snapshot reader {}.", this.path);
        return null;
    }
    final String snapshotPath = getSnapshotPath(lsIndex);
    final SnapshotReader reader = new LocalSnapshotReader(this, this.snapshotThrottle, this.addr, this.raftOptions, snapshotPath);
    if (!reader.init(null)) {
        LOG.error("Fail to init reader for path {}.", snapshotPath);
        unref(lsIndex);
        return null;
    }
    return reader;
}
Also used : SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)

Aggregations

SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)16 Test (org.junit.Test)10 Status (com.alipay.sofa.jraft.Status)6 Message (com.google.protobuf.Message)6 LoadSnapshotClosure (com.alipay.sofa.jraft.closure.LoadSnapshotClosure)5 RpcResponseClosure (com.alipay.sofa.jraft.rpc.RpcResponseClosure)5 FutureImpl (com.alipay.sofa.jraft.rpc.impl.FutureImpl)5 Endpoint (com.alipay.sofa.jraft.util.Endpoint)5 BaseStorageTest (com.alipay.sofa.jraft.storage.BaseStorageTest)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 RaftOutter (com.alipay.sofa.jraft.entity.RaftOutter)3 SnapshotMeta (com.alipay.sofa.jraft.entity.RaftOutter.SnapshotMeta)3 RaftException (com.alipay.sofa.jraft.error.RaftException)3 SnapshotWriter (com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter)3 ByteBuffer (java.nio.ByteBuffer)3 LocalFileMetaOutter (com.alipay.sofa.jraft.entity.LocalFileMetaOutter)2 RpcRequestClosure (com.alipay.sofa.jraft.rpc.RpcRequestClosure)2 LocalSnapshotReader (com.alipay.sofa.jraft.storage.snapshot.local.LocalSnapshotReader)2 BiConsumer (java.util.function.BiConsumer)2 ConsistencyException (com.alibaba.nacos.consistency.exception.ConsistencyException)1