com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter Java Examples

The following examples show how to use com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractKVStoreSnapshotFile.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Override
public void save(final SnapshotWriter writer, final Region region, final Closure done,
                 final ExecutorService executor) {
    final String writerPath = writer.getPath();
    final String snapshotPath = Paths.get(writerPath, SNAPSHOT_DIR).toString();
    try {
        doSnapshotSave(snapshotPath, region, executor).whenComplete((metaBuilder, throwable) -> {
            if (throwable == null) {
                executor.execute(() -> compressSnapshot(writer, metaBuilder, done));
            } else {
                LOG.error("Fail to save snapshot, path={}, file list={}, {}.", writerPath, writer.listFiles(),
                        StackTraceUtil.stackTrace(throwable));
                done.run(new Status(RaftError.EIO, "Fail to save snapshot at %s, error is %s", writerPath,
                        throwable.getMessage()));
            }
        });
    } catch (final Throwable t) {
        LOG.error("Fail to save snapshot, path={}, file list={}, {}.", writerPath, writer.listFiles(),
                StackTraceUtil.stackTrace(t));
        done.run(new Status(RaftError.EIO, "Fail to save snapshot at %s, error is %s", writerPath,
                t.getMessage()));
    }
}
 
Example #2
Source File: AbstractKVStoreSnapshotFile.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
protected void compressSnapshot(final SnapshotWriter writer, final LocalFileMeta.Builder metaBuilder,
                                final Closure done) {
    final String writerPath = writer.getPath();
    final String outputFile = Paths.get(writerPath, SNAPSHOT_ARCHIVE).toString();
    try {
        final Checksum checksum = new CRC64();
        ZipUtil.compress(writerPath, SNAPSHOT_DIR, outputFile, checksum);
        metaBuilder.setChecksum(Long.toHexString(checksum.getValue()));
        if (writer.addFile(SNAPSHOT_ARCHIVE, metaBuilder.build())) {
            done.run(Status.OK());
        } else {
            done.run(new Status(RaftError.EIO, "Fail to add snapshot file: %s", writerPath));
        }
    } catch (final Throwable t) {
        LOG.error("Fail to compress snapshot, path={}, file list={}, {}.", writerPath, writer.listFiles(),
            StackTraceUtil.stackTrace(t));
        done.run(new Status(RaftError.EIO, "Fail to compress snapshot at %s, error is %s", writerPath, t
            .getMessage()));
    }
}
 
Example #3
Source File: CounterStateMachine.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Override
public void onSnapshotSave(final SnapshotWriter writer, final Closure done) {
  final long currVal = this.value.get();
  Utils.runInThread(() -> {
    final CounterSnapshotFile snapshot = new CounterSnapshotFile(writer.getPath() + File.separator + "data");
    if (snapshot.save(currVal)) {
      if (writer.addFile("data")) {
        done.run(Status.OK());
      } else {
        done.run(new Status(RaftError.EIO, "Fail to add file to writer"));
      }
    } else {
      done.run(new Status(RaftError.EIO, "Fail to save counter snapshot %s", snapshot.getPath()));
    }
  });
}
 
Example #4
Source File: AtomicStateMachine.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Override
public void onSnapshotSave(final SnapshotWriter writer, final Closure done) {
  final Map<String, Long> values = new HashMap<>();
  for (final Map.Entry<String, AtomicLong> entry : this.counters.entrySet()) {
    values.put(entry.getKey(), entry.getValue().get());
  }
  Utils.runInThread(() -> {
    final AtomicSnapshotFile snapshot = new AtomicSnapshotFile(writer.getPath() + File.separator + "data");
    if (snapshot.save(values)) {
      if (writer.addFile("data")) {
        done.run(Status.OK());
      } else {
        done.run(new Status(RaftError.EIO, "Fail to add file to writer"));
      }
    } else {
      done.run(new Status(RaftError.EIO, "Fail to save counter snapshot %s", snapshot.getPath()));
    }
  });
}
 
Example #5
Source File: FSMCallerTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #6
Source File: FSMCallerTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example #7
Source File: LocalSnapshotStorageTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #8
Source File: LocalSnapshotStorage.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
public SnapshotWriter create(final boolean fromEmpty) {
    LocalSnapshotWriter writer = null;
    // noinspection ConstantConditions
    do {
        final String snapshotPath = this.path + File.separator + TEMP_PATH;
        // delete temp
        // TODO: Notify watcher before deleting
        if (new File(snapshotPath).exists() && fromEmpty) {
            if (!destroySnapshot(snapshotPath)) {
                break;
            }
        }
        writer = new LocalSnapshotWriter(snapshotPath, this, this.raftOptions);
        if (!writer.init(null)) {
            LOG.error("Fail to init snapshot writer.");
            writer = null;
            break;
        }
    } while (false);
    return writer;
}
 
Example #9
Source File: DBStateMachine.java    From KitDB with Apache License 2.0 5 votes vote down vote up
@Override
public void onSnapshotSave(final SnapshotWriter writer, final Closure done) {
    try {
        String fileName = this.db.backupDB(writer.getPath(), spname);
        if (writer.addFile(spname + DB.BACK_FILE_SUFFIX)) {
            done.run(Status.OK());
        } else {
            done.run(new Status(RaftError.EIO, "Fail to add file to writer"));
        }
    } catch (Exception e) {
        done.run(new Status(RaftError.EIO, "Fail to save counter snapshot %s", writer.getPath()));
    }

}
 
Example #10
Source File: MockStateMachine.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public void onSnapshotSave(final SnapshotWriter writer, final Closure done) {
    this.saveSnapshotTimes++;
    final String path = writer.getPath() + File.separator + "data";
    final File file = new File(path);
    try (FileOutputStream fout = new FileOutputStream(file);
            BufferedOutputStream out = new BufferedOutputStream(fout)) {
        this.lock.lock();
        try {
            for (final ByteBuffer buf : this.logs) {
                final byte[] bs = new byte[4];
                Bits.putInt(bs, 0, buf.remaining());
                out.write(bs);
                out.write(buf.array());
            }
            this.snapshotIndex = this.appliedIndex;
        } finally {
            this.lock.unlock();
        }
        System.out.println("Node<" + this.address + "> saved snapshot into " + file);
        writer.addFile("data");
        done.run(Status.OK());
    } catch (final IOException e) {
        e.printStackTrace();
        done.run(new Status(RaftError.EIO, "Fail to save snapshot"));
    }
}
 
Example #11
Source File: MockStateMachine.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public void onSnapshotSave(final SnapshotWriter writer, final Closure done) {
    this.saveSnapshotTimes++;
    final String path = writer.getPath() + File.separator + "data";
    final File file = new File(path);
    try (FileOutputStream fout = new FileOutputStream(file);
            BufferedOutputStream out = new BufferedOutputStream(fout)) {
        this.lock.lock();
        try {
            for (final ByteBuffer buf : this.logs) {
                final byte[] bs = new byte[4];
                Bits.putInt(bs, 0, buf.remaining());
                out.write(bs);
                out.write(buf.array());
            }
            this.snapshotIndex = this.appliedIndex;
        } finally {
            this.lock.unlock();
        }
        System.out.println("Node<" + this.address + "> saved snapshot into " + file);
        writer.addFile("data");
        done.run(Status.OK());
    } catch (final IOException e) {
        e.printStackTrace();
        done.run(new Status(RaftError.EIO, "Fail to save snapshot"));
    }
}
 
Example #12
Source File: LocalSnapshotStorage.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public SnapshotWriter create() {
    return create(true);
}
 
Example #13
Source File: StateMachineAdapter.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public void onSnapshotSave(final SnapshotWriter writer, final Closure done) {
    error("onSnapshotSave");
    runClosure(done, "onSnapshotSave");
}
 
Example #14
Source File: KVStoreStateMachine.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public void onSnapshotSave(final SnapshotWriter writer, final Closure done) {
    this.storeSnapshotFile.save(writer, this.region.copy(), done, this.storeEngine.getSnapshotExecutor());
}
 
Example #15
Source File: MetaStateMachine.java    From distkv with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
@Override
public void onSnapshotSave(final SnapshotWriter writer, final Closure done) {
  // TODO(kairbon): Add snapshot
}
 
Example #16
Source File: SnapshotStorage.java    From sofa-jraft with Apache License 2.0 2 votes vote down vote up
/**
 * Create a snapshot writer.
 */
SnapshotWriter create();
 
Example #17
Source File: SaveSnapshotClosure.java    From sofa-jraft with Apache License 2.0 2 votes vote down vote up
/**
 * Starts to save snapshot, returns the writer.
 *
 * @param meta metadata of snapshot.
 * @return returns snapshot writer.
 */
SnapshotWriter start(final SnapshotMeta meta);
 
Example #18
Source File: StateMachine.java    From sofa-jraft with Apache License 2.0 2 votes vote down vote up
/**
 * User defined snapshot generate function, this method will block StateMachine#onApply(Iterator).
 * user can make snapshot async when fsm can be cow(copy-on-write).
 * call done.run(status) when snapshot finished.
 * Default: Save nothing and returns error.
 *
 * @param writer snapshot writer
 * @param done   callback
 */
void onSnapshotSave(final SnapshotWriter writer, final Closure done);
 
Example #19
Source File: KVStoreSnapshotFile.java    From sofa-jraft with Apache License 2.0 2 votes vote down vote up
/**
 * Save a snapshot for the specified region.
 *
 * @param writer   snapshot writer
 * @param region   the region to save snapshot
 * @param done     callback
 * @param executor the executor to compress snapshot
 */
void save(final SnapshotWriter writer, final Region region, final Closure done, final ExecutorService executor);