use of com.alibaba.nacos.consistency.snapshot.Writer in project nacos by alibaba.
the class NamingSnapshotOperationTest method testNamingSnapshot.
@Test
public void testNamingSnapshot() throws InterruptedException {
AtomicBoolean result = new AtomicBoolean(false);
NamingSnapshotOperation operation = new NamingSnapshotOperation(storage, lock);
final Writer writer = new Writer(snapshotDir);
final CountDownLatch latch = new CountDownLatch(1);
operation.onSnapshotSave(writer, (isOk, throwable) -> {
result.set(isOk && throwable == null);
latch.countDown();
});
latch.await(10, TimeUnit.SECONDS);
Assert.assertTrue(isSnapshoted);
Assert.assertTrue(result.get());
final Reader reader = new Reader(snapshotDir, writer.listFiles());
boolean res = operation.onSnapshotLoad(reader);
Assert.assertTrue(res);
}
use of com.alibaba.nacos.consistency.snapshot.Writer 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);
}
Aggregations