use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class DefaultRheaKVRpcService method internalCallAsyncWithRpc.
private <V> void internalCallAsyncWithRpc(final Endpoint endpoint, final BaseRequest request, final FailoverClosure<V> closure) {
final InvokeContext invokeCtx = new InvokeContext();
invokeCtx.put(BoltRpcClient.BOLT_CTX, ExtSerializerSupports.getInvokeContext());
final InvokeCallback invokeCallback = new InvokeCallback() {
@Override
public void complete(final Object result, final Throwable err) {
if (err == null) {
final BaseResponse<?> response = (BaseResponse<?>) result;
if (response.isSuccess()) {
closure.setData(response.getValue());
closure.run(Status.OK());
} else {
closure.setError(response.getError());
closure.run(new Status(-1, "RPC failed with address: %s, response: %s", endpoint, response));
}
} else {
closure.failure(err);
}
}
@Override
public Executor executor() {
return rpcCallbackExecutor;
}
};
try {
this.rpcClient.invokeAsync(endpoint, request, invokeCtx, invokeCallback, this.rpcTimeoutMillis);
} catch (final Throwable t) {
closure.failure(t);
}
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class DefaultRheaKVStore method ensureOnValidEpoch.
private static boolean ensureOnValidEpoch(final Region region, final RegionEngine engine, final KVStoreClosure closure) {
if (isValidEpoch(region, engine)) {
return true;
}
// will retry on this error and status
closure.setError(Errors.INVALID_REGION_EPOCH);
closure.run(new Status(-1, "Invalid region epoch: %s", region));
return false;
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class DefaultRegionKVService method handlePutRequest.
@Override
public void handlePutRequest(final PutRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
final PutResponse response = new PutResponse();
response.setRegionId(getRegionId());
response.setRegionEpoch(getRegionEpoch());
try {
KVParameterRequires.requireSameEpoch(request, getRegionEpoch());
final byte[] key = KVParameterRequires.requireNonNull(request.getKey(), "put.key");
final byte[] value = KVParameterRequires.requireNonNull(request.getValue(), "put.value");
this.rawKVStore.put(key, value, new BaseKVStoreClosure() {
@Override
public void run(final Status status) {
if (status.isOk()) {
response.setValue((Boolean) getData());
} else {
setFailure(request, response, status, getError());
}
closure.sendResponse(response);
}
});
} catch (final Throwable t) {
LOG.error("Failed to handle: {}, {}.", request, StackTraceUtil.stackTrace(t));
response.setError(Errors.forException(t));
closure.sendResponse(response);
}
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class DefaultRegionKVService method handleBatchPutRequest.
@Override
public void handleBatchPutRequest(final BatchPutRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
final BatchPutResponse response = new BatchPutResponse();
response.setRegionId(getRegionId());
response.setRegionEpoch(getRegionEpoch());
try {
KVParameterRequires.requireSameEpoch(request, getRegionEpoch());
final List<KVEntry> kvEntries = KVParameterRequires.requireNonEmpty(request.getKvEntries(), "put.kvEntries");
this.rawKVStore.put(kvEntries, new BaseKVStoreClosure() {
@Override
public void run(final Status status) {
if (status.isOk()) {
response.setValue((Boolean) getData());
} else {
setFailure(request, response, status, getError());
}
closure.sendResponse(response);
}
});
} catch (final Throwable t) {
LOG.error("Failed to handle: {}, {}.", request, StackTraceUtil.stackTrace(t));
response.setError(Errors.forException(t));
closure.sendResponse(response);
}
}
use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.
the class StoreEngine method applySplit.
public void applySplit(final Long regionId, final Long newRegionId, final KVStoreClosure closure) {
Requires.requireNonNull(regionId, "regionId");
Requires.requireNonNull(newRegionId, "newRegionId");
if (this.regionEngineTable.containsKey(newRegionId)) {
closure.setError(Errors.CONFLICT_REGION_ID);
closure.run(new Status(-1, "Conflict region id %d", newRegionId));
return;
}
if (!this.splitting.compareAndSet(false, true)) {
closure.setError(Errors.SERVER_BUSY);
closure.run(new Status(-1, "Server is busy now"));
return;
}
final RegionEngine parentEngine = getRegionEngine(regionId);
if (parentEngine == null) {
closure.setError(Errors.NO_REGION_FOUND);
closure.run(new Status(-1, "RegionEngine[%s] not found", regionId));
this.splitting.set(false);
return;
}
if (!parentEngine.isLeader()) {
closure.setError(Errors.NOT_LEADER);
closure.run(new Status(-1, "RegionEngine[%s] not leader", regionId));
this.splitting.set(false);
return;
}
final Region parentRegion = parentEngine.getRegion();
final byte[] startKey = BytesUtil.nullToEmpty(parentRegion.getStartKey());
final byte[] endKey = parentRegion.getEndKey();
final long approximateKeys = this.rawKVStore.getApproximateKeysInRange(startKey, endKey);
final long leastKeysOnSplit = this.storeOpts.getLeastKeysOnSplit();
if (approximateKeys < leastKeysOnSplit) {
closure.setError(Errors.TOO_SMALL_TO_SPLIT);
closure.run(new Status(-1, "RegionEngine[%s]'s keys less than %d", regionId, leastKeysOnSplit));
this.splitting.set(false);
return;
}
final byte[] splitKey = this.rawKVStore.jumpOver(startKey, approximateKeys >> 1);
if (splitKey == null) {
closure.setError(Errors.STORAGE_ERROR);
closure.run(new Status(-1, "Fail to scan split key"));
this.splitting.set(false);
return;
}
final KVOperation op = KVOperation.createRangeSplit(splitKey, regionId, newRegionId);
final Task task = new Task();
task.setData(ByteBuffer.wrap(Serializers.getDefault().writeObject(op)));
task.setDone(new KVClosureAdapter(closure, op));
parentEngine.getNode().apply(task);
}
Aggregations