use of com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter in project sofa-jraft by sofastack.
the class LocalSnapshotStorageTest method testCreateOpen.
@Test
public void testCreateOpen() throws Exception {
SnapshotWriter writer = this.snapshotStorage.create();
assertNotNull(writer);
RaftOutter.SnapshotMeta wroteMeta = RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(this.lastSnapshotIndex + 1).setLastIncludedTerm(1).build();
((LocalSnapshotWriter) writer).saveMeta(wroteMeta);
writer.addFile("data");
assertEquals(1, this.snapshotStorage.getRefs(this.lastSnapshotIndex).get());
writer.close();
// release old
assertEquals(0, this.snapshotStorage.getRefs(this.lastSnapshotIndex).get());
// ref new
assertEquals(1, this.snapshotStorage.getRefs(this.lastSnapshotIndex + 1).get());
SnapshotReader reader = this.snapshotStorage.open();
assertNotNull(reader);
assertTrue(reader.listFiles().contains("data"));
RaftOutter.SnapshotMeta readMeta = reader.load();
assertEquals(wroteMeta, readMeta);
assertEquals(2, this.snapshotStorage.getRefs(this.lastSnapshotIndex + 1).get());
reader.close();
assertEquals(1, this.snapshotStorage.getRefs(this.lastSnapshotIndex + 1).get());
}
use of com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter in project sofa-jraft by sofastack.
the class FSMCallerTest method testOnSnapshotSaveEmptyConf.
@Test
public void testOnSnapshotSaveEmptyConf() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
this.fsmCaller.onSnapshotSave(new SaveSnapshotClosure() {
@Override
public void run(final Status status) {
assertFalse(status.isOk());
assertEquals("Empty conf entry for lastAppliedIndex=10", status.getErrorMsg());
latch.countDown();
}
@Override
public SnapshotWriter start(final SnapshotMeta meta) {
// TODO Auto-generated method stub
return null;
}
});
latch.await();
}
use of com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter in project nacos by alibaba.
the class NacosStateMachine method adapterToJRaftSnapshot.
private void adapterToJRaftSnapshot(Collection<SnapshotOperation> userOperates) {
List<JSnapshotOperation> tmp = new ArrayList<>();
for (SnapshotOperation item : userOperates) {
if (item == null) {
Loggers.RAFT.error("Existing SnapshotOperation for null");
continue;
}
tmp.add(new JSnapshotOperation() {
@Override
public void onSnapshotSave(SnapshotWriter writer, Closure done) {
final Writer wCtx = new Writer(writer.getPath());
// Do a layer of proxy operation to shield different Raft
// components from implementing snapshots
final BiConsumer<Boolean, Throwable> callFinally = (result, t) -> {
boolean[] results = new boolean[wCtx.listFiles().size()];
int[] index = new int[] { 0 };
wCtx.listFiles().forEach((file, meta) -> {
try {
results[index[0]++] = writer.addFile(file, buildMetadata(meta));
} catch (Exception e) {
throw new ConsistencyException(e);
}
});
final Status status = result && !Arrays.asList(results).stream().anyMatch(Boolean.FALSE::equals) ? Status.OK() : new Status(RaftError.EIO, "Fail to compress snapshot at %s, error is %s", writer.getPath(), t == null ? "" : t.getMessage());
done.run(status);
};
item.onSnapshotSave(wCtx, callFinally);
}
@Override
public boolean onSnapshotLoad(SnapshotReader reader) {
final Map<String, LocalFileMeta> metaMap = new HashMap<>(reader.listFiles().size());
for (String fileName : reader.listFiles()) {
final LocalFileMetaOutter.LocalFileMeta meta = (LocalFileMetaOutter.LocalFileMeta) reader.getFileMeta(fileName);
byte[] bytes = meta.getUserMeta().toByteArray();
final LocalFileMeta fileMeta;
if (bytes == null || bytes.length == 0) {
fileMeta = new LocalFileMeta();
} else {
fileMeta = JacksonUtils.toObj(bytes, LocalFileMeta.class);
}
metaMap.put(fileName, fileMeta);
}
final Reader rCtx = new Reader(reader.getPath(), metaMap);
return item.onSnapshotLoad(rCtx);
}
@Override
public String info() {
return item.toString();
}
});
}
this.operations = Collections.unmodifiableList(tmp);
}
use of com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter in project sofa-jraft by sofastack.
the class FSMCallerTest method testOnSnapshotSave.
@Test
public void testOnSnapshotSave() throws Exception {
final SnapshotWriter writer = Mockito.mock(SnapshotWriter.class);
Mockito.when(this.logManager.getConfiguration(10)).thenReturn(TestUtils.getConfEntry("localhost:8081,localhost:8082,localhost:8083", "localhost:8081"));
final SaveSnapshotClosure done = new SaveSnapshotClosure() {
@Override
public void run(final Status status) {
}
@Override
public SnapshotWriter start(final SnapshotMeta meta) {
assertEquals(10, meta.getLastIncludedIndex());
return writer;
}
};
this.fsmCaller.onSnapshotSave(done);
this.fsmCaller.flush();
Mockito.verify(this.fsm).onSnapshotSave(writer, done);
}
use of com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter in project sofa-jraft by sofastack.
the class FSMCallerImpl method doSnapshotSave.
private void doSnapshotSave(final SaveSnapshotClosure done) {
Requires.requireNonNull(done, "SaveSnapshotClosure is null");
final long lastAppliedIndex = this.lastAppliedIndex.get();
final RaftOutter.SnapshotMeta.Builder metaBuilder = //
RaftOutter.SnapshotMeta.newBuilder().setLastIncludedIndex(//
lastAppliedIndex).setLastIncludedTerm(this.lastAppliedTerm);
final ConfigurationEntry confEntry = this.logManager.getConfiguration(lastAppliedIndex);
if (confEntry == null || confEntry.isEmpty()) {
LOG.error("Empty conf entry for lastAppliedIndex={}", lastAppliedIndex);
Utils.runClosureInThread(done, new Status(RaftError.EINVAL, "Empty conf entry for lastAppliedIndex=%s", lastAppliedIndex));
return;
}
for (final PeerId peer : confEntry.getConf()) {
metaBuilder.addPeers(peer.toString());
}
for (final PeerId peer : confEntry.getConf().getLearners()) {
metaBuilder.addLearners(peer.toString());
}
if (confEntry.getOldConf() != null) {
for (final PeerId peer : confEntry.getOldConf()) {
metaBuilder.addOldPeers(peer.toString());
}
for (final PeerId peer : confEntry.getOldConf().getLearners()) {
metaBuilder.addOldLearners(peer.toString());
}
}
final SnapshotWriter writer = done.start(metaBuilder.build());
if (writer == null) {
done.run(new Status(RaftError.EINVAL, "snapshot_storage create SnapshotWriter failed"));
return;
}
this.fsm.onSnapshotSave(writer, done);
}
Aggregations