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);
}
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);
}
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());
}
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");
}
}
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;
}
Aggregations