Search in sources :

Example 6 with TransactionState

use of com.scalar.db.api.TransactionState in project scalardb by scalar-labs.

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method abort_forOngoingTransaction_ShouldAbortCorrectly.

@Test
public void abort_forOngoingTransaction_ShouldAbortCorrectly() throws TransactionException {
    // Arrange
    GrpcTwoPhaseCommitTransaction transaction = manager.start();
    transaction.get(prepareGet(0, 0, TABLE_1));
    transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
    // Act
    manager.abort(transaction.getId());
    transaction.prepare();
    assertThatCode(transaction::commit).isInstanceOf(CommitException.class);
    transaction.rollback();
    // Assert
    TransactionState state = manager.getState(transaction.getId());
    assertThat(state).isEqualTo(TransactionState.ABORTED);
}
Also used : TransactionState(com.scalar.db.api.TransactionState) GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Test(org.junit.Test)

Example 7 with TransactionState

use of com.scalar.db.api.TransactionState in project scalardb by scalar-labs.

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method getState_forFailedTransaction_ShouldReturnAbortedState.

@Test
public void getState_forFailedTransaction_ShouldReturnAbortedState() throws TransactionException {
    // Arrange
    GrpcTwoPhaseCommitTransaction transaction1 = manager.start();
    transaction1.get(prepareGet(0, 0, TABLE_1));
    transaction1.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
    GrpcTwoPhaseCommitTransaction transaction2 = manager.start();
    transaction2.get(prepareGet(0, 0, TABLE_1));
    transaction2.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
    transaction2.prepare();
    transaction2.commit();
    assertThatCode(transaction1::prepare).isInstanceOf(PreparationException.class);
    transaction1.rollback();
    // Act
    TransactionState state = manager.getState(transaction1.getId());
    // Assert
    assertThat(state).isEqualTo(TransactionState.ABORTED);
}
Also used : TransactionState(com.scalar.db.api.TransactionState) GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Test(org.junit.Test)

Example 8 with TransactionState

use of com.scalar.db.api.TransactionState in project scalardb by scalar-labs.

the class TwoPhaseCommitTransactionService method abort.

@Override
public void abort(AbortRequest request, StreamObserver<AbortResponse> responseObserver) {
    execute(() -> {
        TransactionState state = manager.abort(request.getTransactionId());
        responseObserver.onNext(AbortResponse.newBuilder().setState(ProtoUtils.toTransactionState(state)).build());
        responseObserver.onCompleted();
    }, responseObserver, "abort");
}
Also used : TransactionState(com.scalar.db.api.TransactionState)

Example 9 with TransactionState

use of com.scalar.db.api.TransactionState in project scalardb by scalar-labs.

the class TwoPhaseConsensusCommitManagerTest method check_StateReturned_ReturnTheState.

@Test
public void check_StateReturned_ReturnTheState() throws CoordinatorException {
    // Arrange
    TransactionState expected = TransactionState.COMMITTED;
    when(coordinator.getState(ANY_TX_ID)).thenReturn(Optional.of(new State(ANY_TX_ID, expected)));
    // Act
    TransactionState actual = manager.getState(ANY_TX_ID);
    // Assert
    assertThat(actual).isEqualTo(expected);
}
Also used : TransactionState(com.scalar.db.api.TransactionState) TransactionState(com.scalar.db.api.TransactionState) State(com.scalar.db.transaction.consensuscommit.Coordinator.State) Test(org.junit.jupiter.api.Test)

Example 10 with TransactionState

use of com.scalar.db.api.TransactionState in project scalardb by scalar-labs.

the class DistributedTransactionService method getState.

@Override
public void getState(GetTransactionStateRequest request, StreamObserver<GetTransactionStateResponse> responseObserver) {
    execute(() -> {
        TransactionState state = manager.getState(request.getTransactionId());
        responseObserver.onNext(GetTransactionStateResponse.newBuilder().setState(ProtoUtils.toTransactionState(state)).build());
        responseObserver.onCompleted();
    }, responseObserver, "get_state");
}
Also used : TransactionState(com.scalar.db.api.TransactionState)

Aggregations

TransactionState (com.scalar.db.api.TransactionState)37 Test (org.junit.jupiter.api.Test)16 Test (org.junit.Test)12 Put (com.scalar.db.api.Put)4 Key (com.scalar.db.io.Key)4 State (com.scalar.db.transaction.consensuscommit.Coordinator.State)4 GrpcTransaction (com.scalar.db.transaction.rpc.GrpcTransaction)3 GrpcTwoPhaseCommitTransaction (com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction)3 IntValue (com.scalar.db.io.IntValue)2 CommitException (com.scalar.db.exception.transaction.CommitException)1