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));
}
}
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();
}
}
Aggregations