Search in sources :

Example 1 with BooleanCommand

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

the class AtomicRangeGroup method redirect.

/**
 * Redirect request to new leader
 * @return
 */
public BooleanCommand redirect() {
    final BooleanCommand response = new BooleanCommand();
    response.setSuccess(false);
    response.setErrorMsg("Not leader");
    if (node != null) {
        final PeerId leader = node.getLeaderId();
        if (leader != null) {
            response.setRedirect(leader.toString());
        }
    }
    return response;
}
Also used : BooleanCommand(com.alipay.sofa.jraft.test.atomic.command.BooleanCommand) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 2 with BooleanCommand

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

the class AtomicClient method start.

public void start() throws InterruptedException, TimeoutException {
    cliOptions = new CliOptions();
    this.cliClientService.init(cliOptions);
    this.rpcClient = this.cliClientService.getRpcClient();
    if (conf != null) {
        final Set<PeerId> peers = conf.getPeerSet();
        for (final PeerId peer : peers) {
            try {
                final BooleanCommand cmd = (BooleanCommand) this.rpcClient.invokeSync(peer.getEndpoint(), new GetSlotsCommand(), cliOptions.getRpcDefaultTimeout());
                if (cmd instanceof SlotsResponseCommand) {
                    groups = ((SlotsResponseCommand) cmd).getMap();
                    break;
                } else {
                    LOG.warn("Fail to get slots from peer {}, error: {}", peer, cmd.getErrorMsg());
                }
            } catch (final Throwable t) {
                LOG.warn("Fail to get slots from peer {}, error: {}", peer, t.getMessage());
            // continue;
            }
        }
        if (groups == null || groups.isEmpty()) {
            throw new IllegalArgumentException("Can't get slots from any peers");
        } else {
            LOG.info("All groups  is {}", groups);
        }
        for (final String groupId : groups.values()) {
            RouteTable.getInstance().updateConfiguration(groupId, conf);
            refreshLeader(groupId);
            refreshConf(groupId);
        }
    }
}
Also used : GetSlotsCommand(com.alipay.sofa.jraft.test.atomic.command.GetSlotsCommand) BooleanCommand(com.alipay.sofa.jraft.test.atomic.command.BooleanCommand) SlotsResponseCommand(com.alipay.sofa.jraft.test.atomic.command.SlotsResponseCommand) CliOptions(com.alipay.sofa.jraft.option.CliOptions) PeerId(com.alipay.sofa.jraft.entity.PeerId)

Example 3 with BooleanCommand

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

the class AtomicClient method addAndGet.

public long addAndGet(PeerId peer, String key, long delta) throws InterruptedException {
    try {
        final IncrementAndGetCommand request = new IncrementAndGetCommand();
        request.setKey(key);
        request.setDetal(delta);
        final Object response = this.rpcClient.invokeSync(peer.getEndpoint(), request, cliOptions.getRpcDefaultTimeout());
        final BooleanCommand cmd = (BooleanCommand) response;
        if (cmd.isSuccess()) {
            return ((ValueCommand) cmd).getVlaue();
        } else {
            throw new IllegalStateException("Server error:" + cmd.getErrorMsg());
        }
    } catch (final Throwable t) {
        throw new IllegalStateException("Remoting error:" + t.getMessage());
    }
}
Also used : BooleanCommand(com.alipay.sofa.jraft.test.atomic.command.BooleanCommand) ValueCommand(com.alipay.sofa.jraft.test.atomic.command.ValueCommand) IncrementAndGetCommand(com.alipay.sofa.jraft.test.atomic.command.IncrementAndGetCommand)

Example 4 with BooleanCommand

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

the class AtomicClient method set.

public boolean set(PeerId peer, String key, long value) throws InterruptedException {
    try {
        final SetCommand request = new SetCommand();
        request.setKey(key);
        request.setValue(value);
        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 : BooleanCommand(com.alipay.sofa.jraft.test.atomic.command.BooleanCommand) CompareAndSetCommand(com.alipay.sofa.jraft.test.atomic.command.CompareAndSetCommand) SetCommand(com.alipay.sofa.jraft.test.atomic.command.SetCommand)

Example 5 with BooleanCommand

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

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