Search in sources :

Example 16 with Closure

use of com.alipay.sofa.jraft.Closure in project nacos by alibaba.

the class NacosStateMachine method onApply.

@Override
public void onApply(Iterator iter) {
    int index = 0;
    int applied = 0;
    Message message;
    NacosClosure closure = null;
    try {
        while (iter.hasNext()) {
            Status status = Status.OK();
            try {
                if (iter.done() != null) {
                    closure = (NacosClosure) iter.done();
                    message = closure.getMessage();
                } else {
                    final ByteBuffer data = iter.getData();
                    message = ProtoMessageUtil.parse(data.array());
                    if (message instanceof ReadRequest) {
                        // 'iter.done() == null' means current node is follower, ignore read operation
                        applied++;
                        index++;
                        iter.next();
                        continue;
                    }
                }
                LoggerUtils.printIfDebugEnabled(Loggers.RAFT, "receive log : {}", message);
                if (message instanceof WriteRequest) {
                    Response response = processor.onApply((WriteRequest) message);
                    postProcessor(response, closure);
                }
                if (message instanceof ReadRequest) {
                    Response response = processor.onRequest((ReadRequest) message);
                    postProcessor(response, closure);
                }
            } catch (Throwable e) {
                index++;
                status.setError(RaftError.UNKNOWN, e.toString());
                Optional.ofNullable(closure).ifPresent(closure1 -> closure1.setThrowable(e));
                throw e;
            } finally {
                Optional.ofNullable(closure).ifPresent(closure1 -> closure1.run(status));
            }
            applied++;
            index++;
            iter.next();
        }
    } catch (Throwable t) {
        Loggers.RAFT.error("processor : {}, stateMachine meet critical error: {}.", processor, t);
        iter.setErrorAndRollback(index - applied, new Status(RaftError.ESTATEMACHINE, "StateMachine meet critical error: %s.", ExceptionUtil.getStackTrace(t)));
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Response(com.alibaba.nacos.consistency.entity.Response) NotifyCenter(com.alibaba.nacos.common.notify.NotifyCenter) Arrays(java.util.Arrays) LoggerUtils(com.alibaba.nacos.common.utils.LoggerUtils) SnapshotOperation(com.alibaba.nacos.consistency.snapshot.SnapshotOperation) StateMachineAdapter(com.alipay.sofa.jraft.core.StateMachineAdapter) RequestProcessor4CP(com.alibaba.nacos.consistency.cp.RequestProcessor4CP) LeaderChangeContext(com.alipay.sofa.jraft.entity.LeaderChangeContext) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) Loggers(com.alibaba.nacos.core.utils.Loggers) ArrayList(java.util.ArrayList) LocalFileMeta(com.alibaba.nacos.consistency.snapshot.LocalFileMeta) LocalFileMetaOutter(com.alipay.sofa.jraft.entity.LocalFileMetaOutter) Closure(com.alipay.sofa.jraft.Closure) Map(java.util.Map) BiConsumer(java.util.function.BiConsumer) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest) RaftError(com.alipay.sofa.jraft.error.RaftError) Iterator(com.alipay.sofa.jraft.Iterator) JRaftUtils(com.alibaba.nacos.core.distributed.raft.utils.JRaftUtils) JacksonUtils(com.alibaba.nacos.common.utils.JacksonUtils) Writer(com.alibaba.nacos.consistency.snapshot.Writer) ExceptionUtil(com.alibaba.nacos.common.utils.ExceptionUtil) RouteTable(com.alipay.sofa.jraft.RouteTable) Configuration(com.alipay.sofa.jraft.conf.Configuration) ProtoMessageUtil(com.alibaba.nacos.consistency.ProtoMessageUtil) Collection(java.util.Collection) RequestProcessor(com.alibaba.nacos.consistency.RequestProcessor) Status(com.alipay.sofa.jraft.Status) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) Response(com.alibaba.nacos.consistency.entity.Response) Objects(java.util.Objects) List(java.util.List) Node(com.alipay.sofa.jraft.Node) Reader(com.alibaba.nacos.consistency.snapshot.Reader) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) Message(com.google.protobuf.Message) Optional(java.util.Optional) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) Collections(java.util.Collections) RaftException(com.alipay.sofa.jraft.error.RaftException) Message(com.google.protobuf.Message) WriteRequest(com.alibaba.nacos.consistency.entity.WriteRequest) ByteBuffer(java.nio.ByteBuffer) ReadRequest(com.alibaba.nacos.consistency.entity.ReadRequest)

Example 17 with Closure

use of com.alipay.sofa.jraft.Closure 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);
}
Also used : Status(com.alipay.sofa.jraft.Status) LocalFileMetaOutter(com.alipay.sofa.jraft.entity.LocalFileMetaOutter) Closure(com.alipay.sofa.jraft.Closure) ArrayList(java.util.ArrayList) Reader(com.alibaba.nacos.consistency.snapshot.Reader) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) RaftException(com.alipay.sofa.jraft.error.RaftException) SnapshotOperation(com.alibaba.nacos.consistency.snapshot.SnapshotOperation) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) ConsistencyException(com.alibaba.nacos.consistency.exception.ConsistencyException) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) LocalFileMeta(com.alibaba.nacos.consistency.snapshot.LocalFileMeta) HashMap(java.util.HashMap) Map(java.util.Map) Writer(com.alibaba.nacos.consistency.snapshot.Writer) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) BiConsumer(java.util.function.BiConsumer)

Example 18 with Closure

use of com.alipay.sofa.jraft.Closure in project sofa-jraft by sofastack.

the class AddLearnersRequestProcessorTest method verify.

@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());
}
Also used : Closure(com.alipay.sofa.jraft.Closure) LearnersOpResponse(com.alipay.sofa.jraft.rpc.CliRequests.LearnersOpResponse) AddLearnersRequest(com.alipay.sofa.jraft.rpc.CliRequests.AddLearnersRequest) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 19 with Closure

use of com.alipay.sofa.jraft.Closure in project sofa-jraft by sofastack.

the class ChangePeersRequestProcessorTest method verify.

@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());
}
Also used : ChangePeersRequest(com.alipay.sofa.jraft.rpc.CliRequests.ChangePeersRequest) Closure(com.alipay.sofa.jraft.Closure) ChangePeersResponse(com.alipay.sofa.jraft.rpc.CliRequests.ChangePeersResponse)

Example 20 with Closure

use of com.alipay.sofa.jraft.Closure in project sofa-jraft by sofastack.

the class ResetLearnersRequestProcessorTest method verify.

@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());
}
Also used : Closure(com.alipay.sofa.jraft.Closure) LearnersOpResponse(com.alipay.sofa.jraft.rpc.CliRequests.LearnersOpResponse) ResetLearnersRequest(com.alipay.sofa.jraft.rpc.CliRequests.ResetLearnersRequest) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

Closure (com.alipay.sofa.jraft.Closure)28 Status (com.alipay.sofa.jraft.Status)16 PeerId (com.alipay.sofa.jraft.entity.PeerId)11 List (java.util.List)9 Test (org.junit.Test)9 CountDownLatch (java.util.concurrent.CountDownLatch)8 ArrayList (java.util.ArrayList)7 Configuration (com.alipay.sofa.jraft.conf.Configuration)6 Map (java.util.Map)6 RaftError (com.alipay.sofa.jraft.error.RaftError)5 TimeUnit (java.util.concurrent.TimeUnit)5 LocalFileMeta (com.alipay.sofa.jraft.entity.LocalFileMetaOutter.LocalFileMeta)4 Node (com.alipay.sofa.jraft.Node)3 LeaderChangeContext (com.alipay.sofa.jraft.entity.LeaderChangeContext)3 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)3 LogId (com.alipay.sofa.jraft.entity.LogId)3 NodeId (com.alipay.sofa.jraft.entity.NodeId)3 RaftException (com.alipay.sofa.jraft.error.RaftException)3 NodeOptions (com.alipay.sofa.jraft.option.NodeOptions)3 KeyValueTool.makeKey (com.alipay.sofa.jraft.rhea.KeyValueTool.makeKey)3