Search in sources :

Example 16 with Status

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);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) BaseResponse(com.alipay.sofa.jraft.rhea.cmd.store.BaseResponse) InvokeContext(com.alipay.sofa.jraft.rpc.InvokeContext) InvokeCallback(com.alipay.sofa.jraft.rpc.InvokeCallback)

Example 17 with Status

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;
}
Also used : Status(com.alipay.sofa.jraft.Status)

Example 18 with Status

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);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) GetAndPutResponse(com.alipay.sofa.jraft.rhea.cmd.store.GetAndPutResponse) CompareAndPutResponse(com.alipay.sofa.jraft.rhea.cmd.store.CompareAndPutResponse) BatchPutResponse(com.alipay.sofa.jraft.rhea.cmd.store.BatchPutResponse) PutResponse(com.alipay.sofa.jraft.rhea.cmd.store.PutResponse)

Example 19 with Status

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);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) BatchPutResponse(com.alipay.sofa.jraft.rhea.cmd.store.BatchPutResponse) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)

Example 20 with Status

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);
}
Also used : Status(com.alipay.sofa.jraft.Status) Task(com.alipay.sofa.jraft.entity.Task) KVClosureAdapter(com.alipay.sofa.jraft.rhea.storage.KVClosureAdapter) KVOperation(com.alipay.sofa.jraft.rhea.storage.KVOperation) Region(com.alipay.sofa.jraft.rhea.metadata.Region)

Aggregations

Status (com.alipay.sofa.jraft.Status)213 Test (org.junit.Test)63 PeerId (com.alipay.sofa.jraft.entity.PeerId)55 CountDownLatch (java.util.concurrent.CountDownLatch)36 BaseKVStoreClosure (com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)33 Configuration (com.alipay.sofa.jraft.conf.Configuration)25 Message (com.google.protobuf.Message)24 ReadIndexClosure (com.alipay.sofa.jraft.closure.ReadIndexClosure)22 ArrayList (java.util.ArrayList)22 Node (com.alipay.sofa.jraft.Node)21 Closure (com.alipay.sofa.jraft.Closure)17 Task (com.alipay.sofa.jraft.entity.Task)17 ByteBuffer (java.nio.ByteBuffer)16 Endpoint (com.alipay.sofa.jraft.util.Endpoint)15 List (java.util.List)15 RaftException (com.alipay.sofa.jraft.error.RaftException)14 LogEntry (com.alipay.sofa.jraft.entity.LogEntry)12 LogId (com.alipay.sofa.jraft.entity.LogId)12 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)12 JRaftException (com.alipay.sofa.jraft.error.JRaftException)11