Search in sources :

Example 1 with KeyNotFoundException

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

the class AtomicRangeGroup method readFromQuorum.

public void readFromQuorum(final String key, RpcContext asyncContext) {
    final byte[] reqContext = new byte[4];
    Bits.putInt(reqContext, 0, requestId.incrementAndGet());
    this.node.readIndex(reqContext, new ReadIndexClosure() {

        @Override
        public void run(Status status, long index, byte[] reqCtx) {
            if (status.isOk()) {
                try {
                    asyncContext.sendResponse(new ValueCommand(fsm.getValue(key)));
                } catch (final KeyNotFoundException e) {
                    asyncContext.sendResponse(GetCommandProcessor.createKeyNotFoundResponse());
                }
            } else {
                asyncContext.sendResponse(new BooleanCommand(false, status.getErrorMsg()));
            }
        }
    });
}
Also used : Status(com.alipay.sofa.jraft.Status) ReadIndexClosure(com.alipay.sofa.jraft.closure.ReadIndexClosure) 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 2 with KeyNotFoundException

use of com.alipay.sofa.jraft.test.atomic.KeyNotFoundException 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)

Aggregations

KeyNotFoundException (com.alipay.sofa.jraft.test.atomic.KeyNotFoundException)2 BooleanCommand (com.alipay.sofa.jraft.test.atomic.command.BooleanCommand)2 ValueCommand (com.alipay.sofa.jraft.test.atomic.command.ValueCommand)2 Status (com.alipay.sofa.jraft.Status)1 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)1 GetCommand (com.alipay.sofa.jraft.test.atomic.command.GetCommand)1 IncrementAndGetCommand (com.alipay.sofa.jraft.test.atomic.command.IncrementAndGetCommand)1