Search in sources :

Example 6 with RaftException

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

the class FSMCallerImpl method init.

@Override
public boolean init(final FSMCallerOptions opts) {
    this.logManager = opts.getLogManager();
    this.fsm = opts.getFsm();
    this.closureQueue = opts.getClosureQueue();
    this.afterShutdown = opts.getAfterShutdown();
    this.node = opts.getNode();
    this.nodeMetrics = this.node.getNodeMetrics();
    this.lastAppliedIndex.set(opts.getBootstrapId().getIndex());
    notifyLastAppliedIndexUpdated(this.lastAppliedIndex.get());
    this.lastAppliedTerm = opts.getBootstrapId().getTerm();
    this.disruptor = // 
    DisruptorBuilder.<ApplyTask>newInstance().setEventFactory(// 
    new ApplyTaskFactory()).setRingBufferSize(// 
    opts.getDisruptorBufferSize()).setThreadFactory(// 
    new NamedThreadFactory("JRaft-FSMCaller-Disruptor-", true)).setProducerType(// 
    ProducerType.MULTI).setWaitStrategy(// 
    new BlockingWaitStrategy()).build();
    this.disruptor.handleEventsWith(new ApplyTaskHandler());
    this.disruptor.setDefaultExceptionHandler(new LogExceptionHandler<Object>(getClass().getSimpleName()));
    this.taskQueue = this.disruptor.start();
    if (this.nodeMetrics.getMetricRegistry() != null) {
        this.nodeMetrics.getMetricRegistry().register("jraft-fsm-caller-disruptor", new DisruptorMetricSet(this.taskQueue));
    }
    this.error = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_NONE);
    LOG.info("Starts FSMCaller successfully.");
    return true;
}
Also used : RaftException(com.alipay.sofa.jraft.error.RaftException) BlockingWaitStrategy(com.lmax.disruptor.BlockingWaitStrategy) NamedThreadFactory(com.alipay.sofa.jraft.util.NamedThreadFactory) DisruptorMetricSet(com.alipay.sofa.jraft.util.DisruptorMetricSet)

Example 7 with RaftException

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

the class FSMCallerImpl method doSnapshotLoad.

private void doSnapshotLoad(final LoadSnapshotClosure done) {
    Requires.requireNonNull(done, "LoadSnapshotClosure is null");
    final SnapshotReader reader = done.start();
    if (reader == null) {
        done.run(new Status(RaftError.EINVAL, "open SnapshotReader failed"));
        return;
    }
    final RaftOutter.SnapshotMeta meta = reader.load();
    if (meta == null) {
        done.run(new Status(RaftError.EINVAL, "SnapshotReader load meta failed"));
        if (reader.getRaftError() == RaftError.EIO) {
            final RaftException err = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_SNAPSHOT, RaftError.EIO, "Fail to load snapshot meta");
            setError(err);
        }
        return;
    }
    final LogId lastAppliedId = new LogId(this.lastAppliedIndex.get(), this.lastAppliedTerm);
    final LogId snapshotId = new LogId(meta.getLastIncludedIndex(), meta.getLastIncludedTerm());
    if (lastAppliedId.compareTo(snapshotId) > 0) {
        done.run(new Status(RaftError.ESTALE, "Loading a stale snapshot last_applied_index=%d last_applied_term=%d snapshot_index=%d snapshot_term=%d", lastAppliedId.getIndex(), lastAppliedId.getTerm(), snapshotId.getIndex(), snapshotId.getTerm()));
        return;
    }
    if (!this.fsm.onSnapshotLoad(reader)) {
        done.run(new Status(-1, "StateMachine onSnapshotLoad failed"));
        final RaftException e = new RaftException(EnumOutter.ErrorType.ERROR_TYPE_STATE_MACHINE, RaftError.ESTATEMACHINE, "StateMachine onSnapshotLoad failed");
        setError(e);
        return;
    }
    if (meta.getOldPeersCount() == 0) {
        // Joint stage is not supposed to be noticeable by end users.
        final Configuration conf = new Configuration();
        for (int i = 0, size = meta.getPeersCount(); i < size; i++) {
            final PeerId peer = new PeerId();
            Requires.requireTrue(peer.parse(meta.getPeers(i)), "Parse peer failed");
            conf.addPeer(peer);
        }
        this.fsm.onConfigurationCommitted(conf);
    }
    this.lastAppliedIndex.set(meta.getLastIncludedIndex());
    this.lastAppliedTerm = meta.getLastIncludedTerm();
    done.run(Status.OK());
}
Also used : Status(com.alipay.sofa.jraft.Status) RaftException(com.alipay.sofa.jraft.error.RaftException) Configuration(com.alipay.sofa.jraft.conf.Configuration) RaftOutter(com.alipay.sofa.jraft.entity.RaftOutter) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) LogId(com.alipay.sofa.jraft.entity.LogId) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Aggregations

RaftException (com.alipay.sofa.jraft.error.RaftException)7 Status (com.alipay.sofa.jraft.Status)6 PeerId (com.alipay.sofa.jraft.entity.PeerId)2 RaftOutter (com.alipay.sofa.jraft.entity.RaftOutter)2 Test (org.junit.Test)2 Node (com.alipay.sofa.jraft.Node)1 Configuration (com.alipay.sofa.jraft.conf.Configuration)1 LogId (com.alipay.sofa.jraft.entity.LogId)1 InstallSnapshotRequest (com.alipay.sofa.jraft.rpc.RpcRequests.InstallSnapshotRequest)1 InstallSnapshotResponse (com.alipay.sofa.jraft.rpc.RpcRequests.InstallSnapshotResponse)1 SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)1 DisruptorMetricSet (com.alipay.sofa.jraft.util.DisruptorMetricSet)1 Endpoint (com.alipay.sofa.jraft.util.Endpoint)1 NamedThreadFactory (com.alipay.sofa.jraft.util.NamedThreadFactory)1 ByteString (com.google.protobuf.ByteString)1 Message (com.google.protobuf.Message)1 BlockingWaitStrategy (com.lmax.disruptor.BlockingWaitStrategy)1