Search in sources :

Example 6 with BooleanCommand

use of com.alipay.sofa.jraft.test.atomic.command.BooleanCommand in project sofa-jraft by sofastack.

the class AtomicStateMachine method onApply.

@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();
    }
}
Also used : Closure(com.alipay.sofa.jraft.Closure) BooleanCommand(com.alipay.sofa.jraft.test.atomic.command.BooleanCommand) IncrementAndGetCommand(com.alipay.sofa.jraft.test.atomic.command.IncrementAndGetCommand) ByteBuffer(java.nio.ByteBuffer) IncrementAndGetCommand(com.alipay.sofa.jraft.test.atomic.command.IncrementAndGetCommand) GetCommand(com.alipay.sofa.jraft.test.atomic.command.GetCommand) CompareAndSetCommand(com.alipay.sofa.jraft.test.atomic.command.CompareAndSetCommand) AtomicLong(java.util.concurrent.atomic.AtomicLong) BaseRequestCommand(com.alipay.sofa.jraft.test.atomic.command.BaseRequestCommand) ValueCommand(com.alipay.sofa.jraft.test.atomic.command.ValueCommand) CompareAndSetCommand(com.alipay.sofa.jraft.test.atomic.command.CompareAndSetCommand) SetCommand(com.alipay.sofa.jraft.test.atomic.command.SetCommand)

Example 7 with BooleanCommand

use of com.alipay.sofa.jraft.test.atomic.command.BooleanCommand in project sofa-jraft by sofastack.

the class BaseAsyncUserProcessor method createTask.

private Task createTask(RpcContext asyncCtx, T request, CommandType cmdType) {
    final LeaderTaskClosure closure = new LeaderTaskClosure();
    closure.setCmd(request);
    closure.setCmdType(cmdType);
    closure.setDone(status -> {
        if (status.isOk()) {
            asyncCtx.sendResponse(closure.getResponse());
        } else {
            asyncCtx.sendResponse(new BooleanCommand(false, status.getErrorMsg()));
        }
    });
    final byte[] cmdBytes = CommandCodec.encodeCommand(request);
    final ByteBuffer data = ByteBuffer.allocate(cmdBytes.length + 1);
    data.put(cmdType.toByte());
    data.put(cmdBytes);
    data.flip();
    return new Task(data, closure);
}
Also used : Task(com.alipay.sofa.jraft.entity.Task) BooleanCommand(com.alipay.sofa.jraft.test.atomic.command.BooleanCommand) LeaderTaskClosure(com.alipay.sofa.jraft.test.atomic.server.LeaderTaskClosure) ByteBuffer(java.nio.ByteBuffer)

Example 8 with BooleanCommand

use of com.alipay.sofa.jraft.test.atomic.command.BooleanCommand in project sofa-jraft by sofastack.

the class AtomicClient method get.

public long get(PeerId peer, String key, boolean readFromQuorum, boolean readByStateMachine) throws KeyNotFoundException, InterruptedException {
    try {
        final GetCommand request = new GetCommand(key);
        request.setReadFromQuorum(readFromQuorum);
        request.setReadByStateMachine(readByStateMachine);
        final Object response = this.rpcClient.invokeSync(peer.getEndpoint(), request, cliOptions.getRpcDefaultTimeout());
        final BooleanCommand cmd = (BooleanCommand) response;
        if (cmd.isSuccess()) {
            return ((ValueCommand) cmd).getVlaue();
        } else {
            if (cmd.getErrorMsg().equals("key not found")) {
                throw new KeyNotFoundException();
            } else {
                throw new IllegalStateException("Server error:" + cmd.getErrorMsg());
            }
        }
    } catch (final Throwable t) {
        t.printStackTrace();
        throw new IllegalStateException("Remoting error:" + t.getMessage());
    }
}
Also used : IncrementAndGetCommand(com.alipay.sofa.jraft.test.atomic.command.IncrementAndGetCommand) GetCommand(com.alipay.sofa.jraft.test.atomic.command.GetCommand) BooleanCommand(com.alipay.sofa.jraft.test.atomic.command.BooleanCommand) ValueCommand(com.alipay.sofa.jraft.test.atomic.command.ValueCommand) KeyNotFoundException(com.alipay.sofa.jraft.test.atomic.KeyNotFoundException)

Example 9 with BooleanCommand

use of com.alipay.sofa.jraft.test.atomic.command.BooleanCommand in project sofa-jraft by sofastack.

the class AtomicClient method compareAndSet.

public boolean compareAndSet(PeerId peer, String key, long expect, long newVal) throws InterruptedException {
    try {
        final CompareAndSetCommand request = new CompareAndSetCommand();
        request.setKey(key);
        request.setNewValue(newVal);
        request.setExpect(expect);
        final Object response = this.rpcClient.invokeSync(peer.getEndpoint(), request, cliOptions.getRpcDefaultTimeout());
        final BooleanCommand cmd = (BooleanCommand) response;
        return cmd.isSuccess();
    } catch (final Throwable t) {
        throw new IllegalStateException("Remoting error:" + t.getMessage());
    }
}
Also used : CompareAndSetCommand(com.alipay.sofa.jraft.test.atomic.command.CompareAndSetCommand) BooleanCommand(com.alipay.sofa.jraft.test.atomic.command.BooleanCommand)

Aggregations

BooleanCommand (com.alipay.sofa.jraft.test.atomic.command.BooleanCommand)9 ValueCommand (com.alipay.sofa.jraft.test.atomic.command.ValueCommand)4 CompareAndSetCommand (com.alipay.sofa.jraft.test.atomic.command.CompareAndSetCommand)3 IncrementAndGetCommand (com.alipay.sofa.jraft.test.atomic.command.IncrementAndGetCommand)3 PeerId (com.alipay.sofa.jraft.entity.PeerId)2 KeyNotFoundException (com.alipay.sofa.jraft.test.atomic.KeyNotFoundException)2 GetCommand (com.alipay.sofa.jraft.test.atomic.command.GetCommand)2 SetCommand (com.alipay.sofa.jraft.test.atomic.command.SetCommand)2 ByteBuffer (java.nio.ByteBuffer)2 Closure (com.alipay.sofa.jraft.Closure)1 Status (com.alipay.sofa.jraft.Status)1 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)1 Task (com.alipay.sofa.jraft.entity.Task)1 CliOptions (com.alipay.sofa.jraft.option.CliOptions)1 BaseRequestCommand (com.alipay.sofa.jraft.test.atomic.command.BaseRequestCommand)1 GetSlotsCommand (com.alipay.sofa.jraft.test.atomic.command.GetSlotsCommand)1 SlotsResponseCommand (com.alipay.sofa.jraft.test.atomic.command.SlotsResponseCommand)1 LeaderTaskClosure (com.alipay.sofa.jraft.test.atomic.server.LeaderTaskClosure)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1