Java Code Examples for com.alipay.sofa.jraft.Closure#run()

The following examples show how to use com.alipay.sofa.jraft.Closure#run() . 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: 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 4
Source File: FSMCallerImpl.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
private boolean passByStatus(final Closure done) {
    final Status status = this.error.getStatus();
    if (!status.isOk()) {
        if (done != null) {
            done.run(new Status(RaftError.EINVAL, "FSMCaller is in bad status=`%s`", status));
            return false;
        }
    }
    return true;
}
 
Example 5
Source File: AddPeerRequestProcessorTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public void verify(String interest, Node node, ArgumentCaptor<Closure> doneArg) {
    assertEquals(interest, AddPeerRequest.class.getName());
    Mockito.verify(node).addPeer(eq(new PeerId("test", 8181)), doneArg.capture());
    Closure done = doneArg.getValue();
    assertNotNull(done);
    done.run(Status.OK());
    assertNotNull(this.asyncContext.getResponseObject());
    assertEquals("[localhost:8081, localhost:8082, localhost:8083]", this.asyncContext.as(AddPeerResponse.class)
        .getOldPeersList().toString());
    assertEquals("[localhost:8081, localhost:8082, localhost:8083, test:8181]",
        this.asyncContext.as(AddPeerResponse.class).getNewPeersList().toString());
}
 
Example 6
Source File: AddLearnersRequestProcessorTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public void verify(final String interest, final Node node, final ArgumentCaptor<Closure> doneArg) {
    assertEquals(interest, AddLearnersRequest.class.getName());
    Mockito.verify(node).addLearners(
        eq(Arrays.asList(new PeerId("learner", 8082), new PeerId("test", 8182), new PeerId("test", 8183))),
        doneArg.capture());
    Closure done = doneArg.getValue();
    assertNotNull(done);
    done.run(Status.OK());
    assertNotNull(this.asyncContext.getResponseObject());
    assertEquals("[learner:8081, learner:8082, learner:8083]", this.asyncContext.as(LearnersOpResponse.class)
        .getOldLearnersList().toString());
    assertEquals("[learner:8081, learner:8082, learner:8083, test:8182, test:8183]",
        this.asyncContext.as(LearnersOpResponse.class).getNewLearnersList().toString());
}
 
Example 7
Source File: ChangePeersRequestProcessorTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public void verify(String interest, Node node, ArgumentCaptor<Closure> doneArg) {
    assertEquals(interest, ChangePeersRequest.class.getName());
    Mockito.verify(node).changePeers(eq(JRaftUtils.getConfiguration("localhost:8084,localhost:8085")),
        doneArg.capture());
    Closure done = doneArg.getValue();
    assertNotNull(done);
    done.run(Status.OK());
    assertNotNull(this.asyncContext.getResponseObject());
    assertEquals("[localhost:8081, localhost:8082, localhost:8083]", this.asyncContext
        .as(ChangePeersResponse.class).getOldPeersList().toString());
    assertEquals("[localhost:8084, localhost:8085]", this.asyncContext.as(ChangePeersResponse.class)
        .getNewPeersList().toString());
}
 
Example 8
Source File: RemovePeerRequestProcessorTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public void verify(String interest, Node node, ArgumentCaptor<Closure> doneArg) {
    assertEquals(interest, RemovePeerRequest.class.getName());
    Mockito.verify(node).removePeer(eq(new PeerId("localhost", 8082)), doneArg.capture());
    Closure done = doneArg.getValue();
    assertNotNull(done);
    done.run(Status.OK());
    assertNotNull(this.asyncContext.getResponseObject());
    assertEquals("[localhost:8081, localhost:8082, localhost:8083]", this.asyncContext.as(RemovePeerResponse.class)
        .getOldPeersList().toString());
    assertEquals("[localhost:8081, localhost:8083]", this.asyncContext.as(RemovePeerResponse.class)
        .getNewPeersList().toString());
}
 
Example 9
Source File: RemoveLearnersRequestProcessorTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public void verify(final String interest, final Node node, final ArgumentCaptor<Closure> doneArg) {
    assertEquals(interest, RemoveLearnersRequest.class.getName());
    Mockito.verify(node).removeLearners(eq(Arrays.asList(new PeerId("learner", 8082), new PeerId("test", 8183))),
        doneArg.capture());
    Closure done = doneArg.getValue();
    assertNotNull(done);
    done.run(Status.OK());
    assertNotNull(this.asyncContext.getResponseObject());
    assertEquals("[learner:8081, learner:8082, learner:8083]", this.asyncContext.as(LearnersOpResponse.class)
        .getOldLearnersList().toString());
    assertEquals("[learner:8081, learner:8083]", this.asyncContext.as(LearnersOpResponse.class)
        .getNewLearnersList().toString());
}
 
Example 10
Source File: ResetLearnersRequestProcessorTest.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public void verify(final String interest, final Node node, final ArgumentCaptor<Closure> doneArg) {
    assertEquals(interest, ResetLearnersRequest.class.getName());
    Mockito.verify(node).resetLearners(
        eq(Arrays.asList(new PeerId("learner", 8082), new PeerId("test", 8182), new PeerId("test", 8183))),
        doneArg.capture());
    Closure done = doneArg.getValue();
    assertNotNull(done);
    done.run(Status.OK());
    assertNotNull(this.asyncContext.getResponseObject());
    assertEquals("[learner:8081, learner:8082, learner:8083]", this.asyncContext.as(LearnersOpResponse.class)
        .getOldLearnersList().toString());
    assertEquals("[learner:8082, test:8182, test:8183]", this.asyncContext.as(LearnersOpResponse.class)
        .getNewLearnersList().toString());
}
 
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: 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 13
Source File: StateMachineAdapter.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("SameParameterValue")
private void runClosure(final Closure done, final String methodName) {
    done.run(new Status(-1, "%s doesn't implement %s", getClassName(), methodName));
}