Java Code Examples for com.alipay.sofa.jraft.Iterator#hasNext()

The following examples show how to use com.alipay.sofa.jraft.Iterator#hasNext() . 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: 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 3
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 4
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 5
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 6
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 7
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();
    }
}