com.alipay.sofa.jraft.Iterator Java Examples

The following examples show how to use com.alipay.sofa.jraft.Iterator. 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: MockStateMachine.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Override
public void onApply(final Iterator iter) {
    while (iter.hasNext()) {
        this.lock.lock();
        try {
            if (iter.getIndex() <= this.lastAppliedIndex.get()) {
                //prevent duplication
                continue;
            }
            this.lastAppliedIndex.set(iter.getIndex());
            this.logs.add(iter.getData().slice());
            if (iter.done() != null) {
                iter.done().run(Status.OK());
            }
        } finally {
            this.lock.unlock();
        }
        this.appliedIndex = iter.getIndex();
        iter.next();
    }
}
 
Example #2
Source File: FSMCallerTest.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #3
Source File: MockStateMachine.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Override
public void onApply(final Iterator iter) {
    while (iter.hasNext()) {
        this.lock.lock();
        try {
            if (iter.getIndex() <= this.lastAppliedIndex.get()) {
                //prevent duplication
                continue;
            }
            this.lastAppliedIndex.set(iter.getIndex());
            this.logs.add(iter.getData().slice());
            if (iter.done() != null) {
                iter.done().run(Status.OK());
            }
        } finally {
            this.lock.unlock();
        }
        this.appliedIndex = iter.getIndex();
        iter.next();
    }
}
 
Example #4
Source File: PriorityElectionOnlyStateMachine.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public void onApply(final Iterator it) {
    // election only, do nothing
    while (it.hasNext()) {
        LOG.info("On apply with term: {} and index: {}. ", it.getTerm(), it.getIndex());
        it.next();
    }
}
 
Example #5
Source File: ElectionOnlyStateMachine.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public void onApply(final Iterator it) {
    // election only, do nothing
    while (it.hasNext()) {
        LOG.info("On apply with term: {} and index: {}. ", it.getTerm(), it.getIndex());
        it.next();
    }
}
 
Example #6
Source File: ServiceStateMachine.java    From sofa-registry with Apache License 2.0 5 votes vote down vote up
@Override
public void onApply(Iterator iter) {
    while (iter.hasNext()) {
        Closure done = iter.done();
        ByteBuffer data = iter.getData();
        ProcessRequest request;
        LeaderTaskClosure closure = null;

        if (done != null) {
            closure = (LeaderTaskClosure) done;
            request = closure.getRequest();
        } else {

            Hessian2Input input = new Hessian2Input(new ByteArrayInputStream(data.array()));
            SerializerFactory serializerFactory = new SerializerFactory();
            input.setSerializerFactory(serializerFactory);
            try {
                request = (ProcessRequest) input.readObject();
                input.close();
            } catch (IOException e) {
                throw new RuntimeException(
                    "IOException occurred when Hessian serializer decode!", e);
            }
        }

        ProcessResponse response = Processor.getInstance().process(request);

        if (closure != null) {
            closure.setResponse(response);
            closure.run(Status.OK());
        }
        iter.next();
    }
}
 
Example #7
Source File: KVStoreStateMachine.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public void onApply(final Iterator it) {
    int index = 0;
    int applied = 0;
    try {
        KVStateOutputList kvStates = KVStateOutputList.newInstance();
        while (it.hasNext()) {
            KVOperation kvOp;
            final KVClosureAdapter done = (KVClosureAdapter) it.done();
            if (done != null) {
                kvOp = done.getOperation();
            } else {
                final ByteBuffer buf = it.getData();
                try {
                    if (buf.hasArray()) {
                        kvOp = this.serializer.readObject(buf.array(), KVOperation.class);
                    } else {
                        kvOp = this.serializer.readObject(buf, KVOperation.class);
                    }
                } catch (final Throwable t) {
                    ++index;
                    throw new StoreCodecException("Decode operation error", t);
                }
            }
            final KVState first = kvStates.getFirstElement();
            if (first != null && !first.isSameOp(kvOp)) {
                applied += batchApplyAndRecycle(first.getOpByte(), kvStates);
                kvStates = KVStateOutputList.newInstance();
            }
            kvStates.add(KVState.of(kvOp, done));
            ++index;
            it.next();
        }
        if (!kvStates.isEmpty()) {
            final KVState first = kvStates.getFirstElement();
            assert first != null;
            applied += batchApplyAndRecycle(first.getOpByte(), kvStates);
        }
    } catch (final Throwable t) {
        LOG.error("StateMachine meet critical error: {}.", StackTraceUtil.stackTrace(t));
        it.setErrorAndRollback(index - applied, new Status(RaftError.ESTATEMACHINE,
            "StateMachine meet critical error: %s.", t.getMessage()));
    } finally {
        // metrics: qps
        this.applyMeter.mark(applied);
    }
}
 
Example #8
Source File: AtomicStateMachine.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public void onApply(final Iterator iter) {
    while (iter.hasNext()) {
        final Closure done = iter.done();
        CommandType cmdType;
        final ByteBuffer data = iter.getData();
        Object cmd = null;
        LeaderTaskClosure closure = null;
        if (done != null) {
            closure = (LeaderTaskClosure) done;
            cmdType = closure.getCmdType();
            cmd = closure.getCmd();
        } else {
            final byte b = data.get();
            final byte[] cmdBytes = new byte[data.remaining()];
            data.get(cmdBytes);
            cmdType = CommandType.parseByte(b);
            switch (cmdType) {
                case GET:
                    cmd = CommandCodec.decodeCommand(cmdBytes, GetCommand.class);
                    break;
                case SET:
                    cmd = CommandCodec.decodeCommand(cmdBytes, SetCommand.class);
                    break;
                case CAS:
                    cmd = CommandCodec.decodeCommand(cmdBytes, CompareAndSetCommand.class);
                    break;
                case INC:
                    cmd = CommandCodec.decodeCommand(cmdBytes, IncrementAndGetCommand.class);
                    break;
            }
        }
        final String key = ((BaseRequestCommand) cmd).getKey();
        final AtomicLong counter = getCounter(key, true);
        Object response = null;
        switch (cmdType) {
            case GET:
                response = new ValueCommand(counter.get());
                break;
            case SET:
                final SetCommand setCmd = (SetCommand) cmd;
                counter.set(setCmd.getValue());
                response = new BooleanCommand(true);
                break;
            case CAS:
                final CompareAndSetCommand casCmd = (CompareAndSetCommand) cmd;
                response = new BooleanCommand(counter.compareAndSet(casCmd.getExpect(), casCmd.getNewValue()));
                break;
            case INC:
                final IncrementAndGetCommand incCmd = (IncrementAndGetCommand) cmd;
                final long ret = counter.addAndGet(incCmd.getDetal());
                response = new ValueCommand(ret);
                break;
        }
        if (closure != null) {
            closure.setResponse(response);
            closure.run(Status.OK());
        }
        iter.next();
    }
}
 
Example #9
Source File: CounterStateMachine.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public void onApply(final Iterator iter) {
    while (iter.hasNext()) {
        long current = 0;
        CounterOperation counterOperation = null;

        CounterClosure closure = null;
        if (iter.done() != null) {
            // This task is applied by this node, get value from closure to avoid additional parsing.
            closure = (CounterClosure) iter.done();
            counterOperation = closure.getCounterOperation();
        } else {
            // Have to parse FetchAddRequest from this user log.
            final ByteBuffer data = iter.getData();
            try {
                counterOperation = SerializerManager.getSerializer(SerializerManager.Hessian2).deserialize(
                    data.array(), CounterOperation.class.getName());
            } catch (final CodecException e) {
                LOG.error("Fail to decode IncrementAndGetRequest", e);
            }
        }
        if (counterOperation != null) {
            switch (counterOperation.getOp()) {
                case GET:
                    current = this.value.get();
                    LOG.info("Get value={} at logIndex={}", current, iter.getIndex());
                    break;
                case INCREMENT:
                    final long delta = counterOperation.getDelta();
                    final long prev = this.value.get();
                    current = this.value.addAndGet(delta);
                    LOG.info("Added value={} by delta={} at logIndex={}", prev, delta, iter.getIndex());
                    break;
            }

            if (closure != null) {
                closure.success(current);
                closure.run(Status.OK());
            }
        }
        iter.next();
    }
}
 
Example #10
Source File: TestServiceStateMachine.java    From sofa-registry with Apache License 2.0 4 votes vote down vote up
@Test
public void testApply() {
    ServiceStateMachine serviceStateMachine = ServiceStateMachine.getInstance();

    Processor processor = Processor.getInstance();

    processor.addWorker(TestServiceStateMachine.class.getSimpleName(),
        TestServiceStateMachine.class, new TestServiceStateMachine());

    AtomicInteger count = new AtomicInteger();

    LeaderTaskClosure leaderTaskClosure = new LeaderTaskClosure();
    ProcessRequest processRequest = new ProcessRequest();
    processRequest
        .setMethodArgs(new Object[] { TestServiceStateMachine.class.getSimpleName() });
    processRequest.setMethodArgSigs(new String[] { "java.lang.String" });
    processRequest.setMethodName("testMethod");
    processRequest.setServiceName(TestServiceStateMachine.class.getSimpleName());
    leaderTaskClosure.setRequest(processRequest);

    serviceStateMachine.onApply(new Iterator() {

        @Override
        public boolean hasNext() {
            if (count.get() > 0)
                return false;
            return true;
        }

        @Override
        public ByteBuffer next() {
            count.getAndIncrement();
            return null;
        }

        @Override
        public ByteBuffer getData() {
            return ByteBuffer.allocate(10);
        }

        @Override
        public long getIndex() {
            return 0;
        }

        @Override
        public long getTerm() {
            return 0;
        }

        @Override
        public Closure done() {
            return leaderTaskClosure;
        }

        @Override
        public void setErrorAndRollback(long ntail, Status st) {

        }
    });

    Assert.assertEquals(TestServiceStateMachine.class.getSimpleName(),
        ((ProcessResponse) leaderTaskClosure.getResponse()).getEntity());
}
 
Example #11
Source File: TestServiceStateMachine.java    From sofa-registry with Apache License 2.0 4 votes vote down vote up
@Test
public void testApply2() {
    ServiceStateMachine serviceStateMachine = ServiceStateMachine.getInstance();

    Processor processor = Processor.getInstance();

    processor.addWorker(TestServiceStateMachine.class.getSimpleName(),
        TestServiceStateMachine.class, new TestServiceStateMachine());

    AtomicInteger count = new AtomicInteger();

    LeaderTaskClosure leaderTaskClosure = new LeaderTaskClosure();
    ProcessRequest processRequest = new ProcessRequest();
    processRequest
        .setMethodArgs(new Object[] { TestServiceStateMachine.class.getSimpleName() });
    processRequest.setMethodArgSigs(new String[] { "java.lang.String" });
    processRequest.setMethodName("testMethod");
    processRequest.setServiceName(TestServiceStateMachine.class.getSimpleName());
    leaderTaskClosure.setRequest(processRequest);

    final ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
    Hessian2Output hessianOutput = new Hessian2Output(byteStream);
    SerializerFactory serializerFactory = new SerializerFactory();
    hessianOutput.setSerializerFactory(serializerFactory);
    try {
        hessianOutput.writeObject(processRequest);
        hessianOutput.close();
    } catch (IOException e) {
    }

    serviceStateMachine.onApply(new Iterator() {

        @Override
        public boolean hasNext() {
            if (count.get() > 0)
                return false;
            return true;
        }

        @Override
        public ByteBuffer next() {
            count.getAndIncrement();
            return null;
        }

        @Override
        public ByteBuffer getData() {
            byte[] cmdBytes = byteStream.toByteArray();

            ByteBuffer data = ByteBuffer.allocate(cmdBytes.length);
            data.put(cmdBytes);
            data.flip();
            return data;
        }

        @Override
        public long getIndex() {
            return 0;
        }

        @Override
        public long getTerm() {
            return 0;
        }

        @Override
        public Closure done() {
            return null;
        }

        @Override
        public void setErrorAndRollback(long ntail, Status st) {

        }
    });

}