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