Search in sources :

Example 26 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class DefaultRegionKVService method handleKeyLockRequest.

@Override
public void handleKeyLockRequest(final KeyLockRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
    final KeyLockResponse response = new KeyLockResponse();
    response.setRegionId(getRegionId());
    response.setRegionEpoch(getRegionEpoch());
    try {
        KVParameterRequires.requireSameEpoch(request, getRegionEpoch());
        final byte[] key = KVParameterRequires.requireNonNull(request.getKey(), "lock.key");
        final byte[] fencingKey = this.regionEngine.getRegion().getStartKey();
        final DistributedLock.Acquirer acquirer = KVParameterRequires.requireNonNull(request.getAcquirer(), "lock.acquirer");
        KVParameterRequires.requireNonNull(acquirer.getId(), "lock.id");
        KVParameterRequires.requirePositive(acquirer.getLeaseMillis(), "lock.leaseMillis");
        this.rawKVStore.tryLockWith(key, fencingKey, request.isKeepLease(), acquirer, new BaseKVStoreClosure() {

            @Override
            public void run(final Status status) {
                if (status.isOk()) {
                    response.setValue((DistributedLock.Owner) 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) DistributedLock(com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock) KeyLockResponse(com.alipay.sofa.jraft.rhea.cmd.store.KeyLockResponse) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)

Example 27 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class DefaultRegionKVService method handleScanRequest.

@Override
public void handleScanRequest(final ScanRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
    final ScanResponse response = new ScanResponse();
    response.setRegionId(getRegionId());
    response.setRegionEpoch(getRegionEpoch());
    try {
        KVParameterRequires.requireSameEpoch(request, getRegionEpoch());
        final BaseKVStoreClosure kvStoreClosure = new BaseKVStoreClosure() {

            @SuppressWarnings("unchecked")
            @Override
            public void run(final Status status) {
                if (status.isOk()) {
                    response.setValue((List<KVEntry>) getData());
                } else {
                    setFailure(request, response, status, getError());
                }
                closure.sendResponse(response);
            }
        };
        if (request.isReverse()) {
            this.rawKVStore.reverseScan(request.getStartKey(), request.getEndKey(), request.getLimit(), request.isReadOnlySafe(), request.isReturnValue(), kvStoreClosure);
        } else {
            this.rawKVStore.scan(request.getStartKey(), request.getEndKey(), request.getLimit(), request.isReadOnlySafe(), request.isReturnValue(), kvStoreClosure);
        }
    } 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) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) ScanResponse(com.alipay.sofa.jraft.rhea.cmd.store.ScanResponse)

Example 28 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class DefaultRegionKVService method handleBatchDeleteRequest.

@Override
public void handleBatchDeleteRequest(final BatchDeleteRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
    final BatchDeleteResponse response = new BatchDeleteResponse();
    response.setRegionId(getRegionId());
    response.setRegionEpoch(getRegionEpoch());
    try {
        KVParameterRequires.requireSameEpoch(request, getRegionEpoch());
        final List<byte[]> keys = KVParameterRequires.requireNonEmpty(request.getKeys(), "delete.keys");
        this.rawKVStore.delete(keys, 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) BatchDeleteResponse(com.alipay.sofa.jraft.rhea.cmd.store.BatchDeleteResponse)

Example 29 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class DefaultRegionKVService method handleMultiGetRequest.

@Override
public void handleMultiGetRequest(final MultiGetRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
    final MultiGetResponse response = new MultiGetResponse();
    response.setRegionId(getRegionId());
    response.setRegionEpoch(getRegionEpoch());
    try {
        KVParameterRequires.requireSameEpoch(request, getRegionEpoch());
        final List<byte[]> keys = KVParameterRequires.requireNonEmpty(request.getKeys(), "multiGet.keys");
        this.rawKVStore.multiGet(keys, request.isReadOnlySafe(), new BaseKVStoreClosure() {

            @SuppressWarnings("unchecked")
            @Override
            public void run(final Status status) {
                if (status.isOk()) {
                    response.setValue((Map<ByteArray, byte[]>) 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) MultiGetResponse(com.alipay.sofa.jraft.rhea.cmd.store.MultiGetResponse) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) Map(java.util.Map)

Example 30 with Status

use of com.alipay.sofa.jraft.Status in project sofa-jraft by sofastack.

the class DefaultRegionKVService method handleRangeSplitRequest.

@Override
public void handleRangeSplitRequest(final RangeSplitRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
    final RangeSplitResponse response = new RangeSplitResponse();
    response.setRegionId(getRegionId());
    response.setRegionEpoch(getRegionEpoch());
    try {
        // do not need to check the region epoch
        final Long newRegionId = KVParameterRequires.requireNonNull(request.getNewRegionId(), "rangeSplit.newRegionId");
        this.regionEngine.getStoreEngine().applySplit(request.getRegionId(), newRegionId, 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) RangeSplitResponse(com.alipay.sofa.jraft.rhea.cmd.store.RangeSplitResponse) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)

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