Search in sources :

Example 1 with Iterator

use of com.alipay.sofa.jraft.Iterator 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 2 with Iterator

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

the class NodeTest method testRollbackStateMachineWithReadIndex_Issue317.

/**
 * Test rollback stateMachine with readIndex for issue 317:
 * https://github.com/sofastack/sofa-jraft/issues/317
 */
@Test
public void testRollbackStateMachineWithReadIndex_Issue317() throws Exception {
    final Endpoint addr = new Endpoint(TestUtils.getMyIp(), TestUtils.INIT_PORT);
    final PeerId peer = new PeerId(addr, 0);
    NodeManager.getInstance().addAddress(addr);
    final NodeOptions nodeOptions = createNodeOptionsWithSharedTimer();
    final CountDownLatch applyCompleteLatch = new CountDownLatch(1);
    final CountDownLatch applyLatch = new CountDownLatch(1);
    final CountDownLatch readIndexLatch = new CountDownLatch(1);
    final AtomicInteger currentValue = new AtomicInteger(-1);
    final String errorMsg = this.testName.getMethodName();
    final StateMachine fsm = new StateMachineAdapter() {

        @Override
        public void onApply(final Iterator iter) {
            // Notify that the #onApply is preparing to go.
            readIndexLatch.countDown();
            // Wait for submitting a read-index request
            try {
                applyLatch.await();
            } catch (InterruptedException e) {
                fail();
            }
            int i = 0;
            while (iter.hasNext()) {
                byte[] data = iter.next().array();
                int v = Bits.getInt(data, 0);
                assertEquals(i++, v);
                currentValue.set(v);
            }
            if (i > 0) {
                // rollback
                currentValue.set(i - 1);
                iter.setErrorAndRollback(1, new Status(-1, errorMsg));
                applyCompleteLatch.countDown();
            }
        }
    };
    nodeOptions.setFsm(fsm);
    nodeOptions.setLogUri(this.dataPath + File.separator + "log");
    nodeOptions.setRaftMetaUri(this.dataPath + File.separator + "meta");
    nodeOptions.setSnapshotUri(this.dataPath + File.separator + "snapshot");
    nodeOptions.setInitialConf(new Configuration(Collections.singletonList(peer)));
    final Node node = new NodeImpl("unittest", peer);
    assertTrue(node.init(nodeOptions));
    assertEquals(1, node.listPeers().size());
    assertTrue(node.listPeers().contains(peer));
    while (!node.isLeader()) {
        ;
    }
    int n = 5;
    {
        // apply tasks
        for (int i = 0; i < n; i++) {
            byte[] b = new byte[4];
            Bits.putInt(b, 0, i);
            node.apply(new Task(ByteBuffer.wrap(b), null));
        }
    }
    final AtomicInteger readIndexSuccesses = new AtomicInteger(0);
    {
        // Submit a read-index, wait for #onApply
        readIndexLatch.await();
        final CountDownLatch latch = new CountDownLatch(1);
        node.readIndex(null, new ReadIndexClosure() {

            @Override
            public void run(final Status status, final long index, final byte[] reqCtx) {
                try {
                    if (status.isOk()) {
                        readIndexSuccesses.incrementAndGet();
                    } else {
                        assertTrue("Unexpected status: " + status, status.getErrorMsg().contains(errorMsg) || status.getRaftError() == RaftError.ETIMEDOUT || status.getErrorMsg().contains("Invalid state for readIndex: STATE_ERROR"));
                    }
                } finally {
                    latch.countDown();
                }
            }
        });
        // We have already submit a read-index request,
        // notify #onApply can go right now
        applyLatch.countDown();
        // The state machine is in error state, the node should step down.
        while (node.isLeader()) {
            Thread.sleep(10);
        }
        latch.await();
        applyCompleteLatch.await();
    }
    // No read-index request succeed.
    assertEquals(0, readIndexSuccesses.get());
    assertTrue(n - 1 >= currentValue.get());
    node.shutdown();
    node.join();
}
Also used : Status(com.alipay.sofa.jraft.Status) Task(com.alipay.sofa.jraft.entity.Task) Configuration(com.alipay.sofa.jraft.conf.Configuration) StateMachine(com.alipay.sofa.jraft.StateMachine) Node(com.alipay.sofa.jraft.Node) NodeOptions(com.alipay.sofa.jraft.option.NodeOptions) CountDownLatch(java.util.concurrent.CountDownLatch) Endpoint(com.alipay.sofa.jraft.util.Endpoint) Endpoint(com.alipay.sofa.jraft.util.Endpoint) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Iterator(com.alipay.sofa.jraft.Iterator) PeerId(com.alipay.sofa.jraft.entity.PeerId) Test(org.junit.Test)

Example 3 with Iterator

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

the class FSMCallerTest method testOnCommitted.

@Test
public void testOnCommitted() throws Exception {
    final LogEntry log = new LogEntry(EntryType.ENTRY_TYPE_DATA);
    log.getId().setIndex(11);
    log.getId().setTerm(1);
    Mockito.when(this.logManager.getTerm(11)).thenReturn(1L);
    Mockito.when(this.logManager.getEntry(11)).thenReturn(log);
    final ArgumentCaptor<Iterator> itArg = ArgumentCaptor.forClass(Iterator.class);
    assertTrue(this.fsmCaller.onCommitted(11));
    this.fsmCaller.flush();
    assertEquals(this.fsmCaller.getLastAppliedIndex(), 11);
    Mockito.verify(this.fsm).onApply(itArg.capture());
    final Iterator it = itArg.getValue();
    assertFalse(it.hasNext());
    assertEquals(it.getIndex(), 12);
    Mockito.verify(this.logManager).setAppliedId(new LogId(11, 1));
    assertTrue(this.fsmCaller.getError().getStatus().isOk());
}
Also used : Iterator(com.alipay.sofa.jraft.Iterator) LogId(com.alipay.sofa.jraft.entity.LogId) LogEntry(com.alipay.sofa.jraft.entity.LogEntry) Test(org.junit.Test)

Example 4 with Iterator

use of com.alipay.sofa.jraft.Iterator in project mmqtt by MrHKing.

the class MmqStateMachine method onApply.

@Override
public void onApply(Iterator iter) {
    int index = 0;
    int applied = 0;
    Message message;
    MmqClosure closure = null;
    try {
        while (iter.hasNext()) {
            Status status = Status.OK();
            try {
                if (iter.done() != null) {
                    closure = (MmqClosure) iter.done();
                    message = closure.getMessage();
                } else {
                    final ByteBuffer data = iter.getData();
                    message = ProtoMessageUtil.parse(data.array());
                }
                Loggers.RAFT.info("receive log : {}", message);
                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 : Response(org.monkey.mmq.core.entity.Response) Response(org.monkey.mmq.core.entity.Response) java.util(java.util) StateMachineAdapter(com.alipay.sofa.jraft.core.StateMachineAdapter) LocalFileMeta(org.monkey.mmq.core.consistency.snapshot.LocalFileMeta) LeaderChangeContext(com.alipay.sofa.jraft.entity.LeaderChangeContext) SnapshotOperation(org.monkey.mmq.core.consistency.snapshot.SnapshotOperation) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BooleanUtils(org.apache.commons.lang3.BooleanUtils) Reader(org.monkey.mmq.core.consistency.snapshot.Reader) RequestProcessor4CP(org.monkey.mmq.core.consistency.cp.RequestProcessor4CP) ByteBuffer(java.nio.ByteBuffer) JRaftUtils(org.monkey.mmq.core.distributed.raft.utils.JRaftUtils) LocalFileMetaOutter(com.alipay.sofa.jraft.entity.LocalFileMetaOutter) com.alipay.sofa.jraft(com.alipay.sofa.jraft) BiConsumer(java.util.function.BiConsumer) Writer(org.monkey.mmq.core.consistency.snapshot.Writer) RaftError(com.alipay.sofa.jraft.error.RaftError) WriteRequest(org.monkey.mmq.core.entity.WriteRequest) Iterator(com.alipay.sofa.jraft.Iterator) org.monkey.mmq.core.utils(org.monkey.mmq.core.utils) NotifyCenter(org.monkey.mmq.core.notify.NotifyCenter) Configuration(com.alipay.sofa.jraft.conf.Configuration) ReadRequest(org.monkey.mmq.core.entity.ReadRequest) Objects(java.util.Objects) ConsistencyException(org.monkey.mmq.core.distributed.raft.exception.ConsistencyException) SnapshotWriter(com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter) Message(com.google.protobuf.Message) RequestProcessor(org.monkey.mmq.core.consistency.RequestProcessor) SnapshotReader(com.alipay.sofa.jraft.storage.snapshot.SnapshotReader) RaftException(com.alipay.sofa.jraft.error.RaftException) Message(com.google.protobuf.Message) WriteRequest(org.monkey.mmq.core.entity.WriteRequest) ByteBuffer(java.nio.ByteBuffer) ReadRequest(org.monkey.mmq.core.entity.ReadRequest)

Aggregations

Iterator (com.alipay.sofa.jraft.Iterator)4 Configuration (com.alipay.sofa.jraft.conf.Configuration)3 Node (com.alipay.sofa.jraft.Node)2 Status (com.alipay.sofa.jraft.Status)2 StateMachineAdapter (com.alipay.sofa.jraft.core.StateMachineAdapter)2 LeaderChangeContext (com.alipay.sofa.jraft.entity.LeaderChangeContext)2 LocalFileMetaOutter (com.alipay.sofa.jraft.entity.LocalFileMetaOutter)2 RaftError (com.alipay.sofa.jraft.error.RaftError)2 RaftException (com.alipay.sofa.jraft.error.RaftException)2 SnapshotReader (com.alipay.sofa.jraft.storage.snapshot.SnapshotReader)2 SnapshotWriter (com.alipay.sofa.jraft.storage.snapshot.SnapshotWriter)2 Message (com.google.protobuf.Message)2 ByteBuffer (java.nio.ByteBuffer)2 Objects (java.util.Objects)2 Test (org.junit.Test)2 NotifyCenter (com.alibaba.nacos.common.notify.NotifyCenter)1 ExceptionUtil (com.alibaba.nacos.common.utils.ExceptionUtil)1 JacksonUtils (com.alibaba.nacos.common.utils.JacksonUtils)1 LoggerUtils (com.alibaba.nacos.common.utils.LoggerUtils)1 ProtoMessageUtil (com.alibaba.nacos.consistency.ProtoMessageUtil)1