Search in sources :

Example 1 with CodecException

use of com.alipay.remoting.exception.CodecException in project sofa-jraft by sofastack.

the class CounterServiceImpl method applyOperation.

private void applyOperation(final CounterOperation op, final CounterClosure closure) {
    if (!isLeader()) {
        handlerNotLeaderError(closure);
        return;
    }
    try {
        closure.setCounterOperation(op);
        final Task task = new Task();
        task.setData(ByteBuffer.wrap(SerializerManager.getSerializer(SerializerManager.Hessian2).serialize(op)));
        task.setDone(closure);
        this.counterServer.getNode().apply(task);
    } catch (CodecException e) {
        String errorMsg = "Fail to encode CounterOperation";
        LOG.error(errorMsg, e);
        closure.failure(errorMsg, StringUtils.EMPTY);
        closure.run(new Status(RaftError.EINTERNAL, errorMsg));
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) Task(com.alipay.sofa.jraft.entity.Task) CodecException(com.alipay.remoting.exception.CodecException)

Example 2 with CodecException

use of com.alipay.remoting.exception.CodecException in project sofa-jraft by sofastack.

the class CounterStateMachine method onApply.

@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();
    }
}
Also used : CodecException(com.alipay.remoting.exception.CodecException) ByteBuffer(java.nio.ByteBuffer)

Aggregations

CodecException (com.alipay.remoting.exception.CodecException)2 Status (com.alipay.sofa.jraft.Status)1 Task (com.alipay.sofa.jraft.entity.Task)1 ByteBuffer (java.nio.ByteBuffer)1