Search in sources :

Example 6 with CrudException

use of com.scalar.db.exception.transaction.CrudException in project scalardb by scalar-labs.

the class GrpcTransactionOnBidirectionalStream method scan.

public List<Result> scan(Scan scan) throws CrudException {
    throwIfTransactionFinished();
    ResponseOrError responseOrError = sendRequest(TransactionRequest.newBuilder().setScanRequest(ScanRequest.newBuilder().setScan(ProtoUtils.toScan(scan))).build());
    throwIfErrorForCrud(responseOrError);
    TableMetadata tableMetadata = getTableMetadata(scan);
    return responseOrError.getResponse().getScanResponse().getResultList().stream().map(r -> ProtoUtils.toResult(r, tableMetadata)).collect(Collectors.toList());
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) DistributedTransactionStub(com.scalar.db.rpc.DistributedTransactionGrpc.DistributedTransactionStub) ExecutionException(com.scalar.db.exception.storage.ExecutionException) AbortRequest(com.scalar.db.rpc.TransactionRequest.AbortRequest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CrudConflictException(com.scalar.db.exception.transaction.CrudConflictException) CommitRequest(com.scalar.db.rpc.TransactionRequest.CommitRequest) StartRequest(com.scalar.db.rpc.TransactionRequest.StartRequest) TableMetadata(com.scalar.db.api.TableMetadata) CrudException(com.scalar.db.exception.transaction.CrudException) Result(com.scalar.db.api.Result) ScalarDbUtils(com.scalar.db.util.ScalarDbUtils) Code(io.grpc.Status.Code) ClientResponseObserver(io.grpc.stub.ClientResponseObserver) GetRequest(com.scalar.db.rpc.TransactionRequest.GetRequest) TransactionResponse(com.scalar.db.rpc.TransactionResponse) Mutation(com.scalar.db.api.Mutation) CommitException(com.scalar.db.exception.transaction.CommitException) MutateRequest(com.scalar.db.rpc.TransactionRequest.MutateRequest) CommitConflictException(com.scalar.db.exception.transaction.CommitConflictException) Nullable(javax.annotation.Nullable) ServiceTemporaryUnavailableException(com.scalar.db.util.retry.ServiceTemporaryUnavailableException) GrpcConfig(com.scalar.db.storage.rpc.GrpcConfig) TableMetadataManager(com.scalar.db.common.TableMetadataManager) Uninterruptibles(com.google.common.util.concurrent.Uninterruptibles) TransactionRequest(com.scalar.db.rpc.TransactionRequest) GetResponse(com.scalar.db.rpc.TransactionResponse.GetResponse) Operation(com.scalar.db.api.Operation) BlockingQueue(java.util.concurrent.BlockingQueue) ClientCallStreamObserver(io.grpc.stub.ClientCallStreamObserver) Get(com.scalar.db.api.Get) ProtoUtils(com.scalar.db.util.ProtoUtils) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Collectors(java.util.stream.Collectors) StatusRuntimeException(io.grpc.StatusRuntimeException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Scan(com.scalar.db.api.Scan) UnknownTransactionStatusException(com.scalar.db.exception.transaction.UnknownTransactionStatusException) AbortException(com.scalar.db.exception.transaction.AbortException) Optional(java.util.Optional) TransactionException(com.scalar.db.exception.transaction.TransactionException) ScanRequest(com.scalar.db.rpc.TransactionRequest.ScanRequest) NotThreadSafe(javax.annotation.concurrent.NotThreadSafe)

Example 7 with CrudException

use of com.scalar.db.exception.transaction.CrudException in project scalardb by scalar-labs.

the class GrpcTransactionOnBidirectionalStream method throwIfErrorForCrud.

private void throwIfErrorForCrud(ResponseOrError responseOrError) throws CrudException {
    if (responseOrError.isError()) {
        finished.set(true);
        Throwable error = responseOrError.getError();
        if (error instanceof Error) {
            throw (Error) error;
        }
        throw new CrudException("failed to execute crud", error);
    }
    TransactionResponse response = responseOrError.getResponse();
    if (response.hasError()) {
        TransactionResponse.Error error = response.getError();
        switch(error.getErrorCode()) {
            case INVALID_ARGUMENT:
                throw new IllegalArgumentException(error.getMessage());
            case CONFLICT:
                throw new CrudConflictException(error.getMessage());
            default:
                throw new CrudException(error.getMessage());
        }
    }
}
Also used : TransactionResponse(com.scalar.db.rpc.TransactionResponse) CrudConflictException(com.scalar.db.exception.transaction.CrudConflictException) CrudException(com.scalar.db.exception.transaction.CrudException)

Example 8 with CrudException

use of com.scalar.db.exception.transaction.CrudException in project scalardb by scalar-labs.

the class GrpcTwoPhaseCommitTransactionOnBidirectionalStream method throwIfErrorForCrud.

private void throwIfErrorForCrud(ResponseOrError responseOrError) throws CrudException {
    if (responseOrError.isError()) {
        finished.set(true);
        Throwable error = responseOrError.getError();
        if (error instanceof Error) {
            throw (Error) error;
        }
        throw new CrudException("failed to execute crud", error);
    }
    TwoPhaseCommitTransactionResponse response = responseOrError.getResponse();
    if (response.hasError()) {
        TwoPhaseCommitTransactionResponse.Error error = response.getError();
        switch(error.getErrorCode()) {
            case INVALID_ARGUMENT:
                throw new IllegalArgumentException(error.getMessage());
            case CONFLICT:
                throw new CrudConflictException(error.getMessage());
            default:
                throw new CrudException(error.getMessage());
        }
    }
}
Also used : TwoPhaseCommitTransactionResponse(com.scalar.db.rpc.TwoPhaseCommitTransactionResponse) CrudConflictException(com.scalar.db.exception.transaction.CrudConflictException) CrudException(com.scalar.db.exception.transaction.CrudException)

Example 9 with CrudException

use of com.scalar.db.exception.transaction.CrudException in project scalardb by scalar-labs.

the class CrudHandler method getFromStorage.

private Optional<TransactionResult> getFromStorage(Get get) throws CrudException {
    try {
        // get only after image columns
        get.clearProjections();
        LinkedHashSet<String> afterImageColumnNames = tableMetadataManager.getTransactionalTableMetadata(get).getAfterImageColumnNames();
        get.withProjections(afterImageColumnNames);
        get.withConsistency(Consistency.LINEARIZABLE);
        return storage.get(get).map(TransactionResult::new);
    } catch (ExecutionException e) {
        throw new CrudException("get failed.", e);
    }
}
Also used : ExecutionException(com.scalar.db.exception.storage.ExecutionException) CrudException(com.scalar.db.exception.transaction.CrudException)

Example 10 with CrudException

use of com.scalar.db.exception.transaction.CrudException in project scalardb by scalar-labs.

the class CrudHandler method getFromStorage.

private Scanner getFromStorage(Scan scan) throws CrudException {
    try {
        // get only after image columns
        scan.clearProjections();
        LinkedHashSet<String> afterImageColumnNames = tableMetadataManager.getTransactionalTableMetadata(scan).getAfterImageColumnNames();
        scan.withProjections(afterImageColumnNames);
        scan.withConsistency(Consistency.LINEARIZABLE);
        return storage.scan(scan);
    } catch (ExecutionException e) {
        throw new CrudException("scan failed.", e);
    }
}
Also used : ExecutionException(com.scalar.db.exception.storage.ExecutionException) CrudException(com.scalar.db.exception.transaction.CrudException)

Aggregations

CrudException (com.scalar.db.exception.transaction.CrudException)14 ExecutionException (com.scalar.db.exception.storage.ExecutionException)12 List (java.util.List)7 Optional (java.util.Optional)7 Get (com.scalar.db.api.Get)6 TransactionException (com.scalar.db.exception.transaction.TransactionException)6 Result (com.scalar.db.api.Result)5 Scan (com.scalar.db.api.Scan)5 TableMetadata (com.scalar.db.api.TableMetadata)5 DataType (com.scalar.db.io.DataType)5 Key (com.scalar.db.io.Key)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 CommitException (com.scalar.db.exception.transaction.CommitException)4 CrudConflictException (com.scalar.db.exception.transaction.CrudConflictException)4 IntValue (com.scalar.db.io.IntValue)4 Value (com.scalar.db.io.Value)4 ArrayList (java.util.ArrayList)4 IntStream (java.util.stream.IntStream)4 Delete (com.scalar.db.api.Delete)3 Test (org.junit.jupiter.api.Test)3