Search in sources :

Example 21 with GrpcTwoPhaseCommitTransaction

use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction 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 22 with GrpcTwoPhaseCommitTransaction

use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method prepare_DeleteGivenForNonExisting_ShouldThrowPreparationException.

@Test
public void prepare_DeleteGivenForNonExisting_ShouldThrowPreparationException() throws TransactionException {
    // Arrange
    GrpcTwoPhaseCommitTransaction transaction = manager.start();
    transaction.get(prepareGet(0, 0, TABLE_1));
    transaction.delete(prepareDelete(0, 0, TABLE_1));
    // Act
    Throwable throwable = catchThrowable(transaction::prepare);
    transaction.rollback();
    // Assert
    assertThat(throwable).isInstanceOf(PreparationException.class);
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Test(org.junit.Test)

Example 23 with GrpcTwoPhaseCommitTransaction

use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method deleteAndCommit_DeleteGivenInBetweenTransactions_ShouldProduceSerializableResults.

@Test
public void deleteAndCommit_DeleteGivenInBetweenTransactions_ShouldProduceSerializableResults() throws TransactionException {
    // Arrange
    GrpcTwoPhaseCommitTransaction transaction = manager.start();
    transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 2));
    transaction.prepare();
    transaction.commit();
    // Act
    GrpcTwoPhaseCommitTransaction transaction1 = manager.start();
    transaction1.get(prepareGet(0, 0, TABLE_1));
    transaction1.delete(prepareDelete(0, 0, TABLE_1));
    GrpcTwoPhaseCommitTransaction transaction2 = manager.start();
    transaction2.get(prepareGet(0, 0, TABLE_1));
    transaction2.delete(prepareDelete(0, 0, TABLE_1));
    transaction2.prepare();
    transaction2.commit();
    GrpcTwoPhaseCommitTransaction transaction3 = manager.start();
    Optional<Result> result = transaction3.get(prepareGet(0, 0, TABLE_1));
    assertThat(result).isNotPresent();
    int balance3 = 0;
    transaction3.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, balance3 + 1));
    transaction3.prepare();
    transaction3.commit();
    assertThatThrownBy(transaction1::prepare).isInstanceOf(PreparationException.class);
    transaction1.rollback();
    // Assert
    transaction = manager.start();
    result = transaction.get(prepareGet(0, 0, TABLE_1));
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(1);
    transaction.prepare();
    transaction.commit();
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 24 with GrpcTwoPhaseCommitTransaction

use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method get_CalledTwiceAndAnotherTransactionCommitsInBetween_ShouldReturnFromSnapshotInSecondTime.

@Test
public void get_CalledTwiceAndAnotherTransactionCommitsInBetween_ShouldReturnFromSnapshotInSecondTime() throws TransactionException {
    // Arrange
    GrpcTwoPhaseCommitTransaction transaction = manager.start();
    // Act
    Optional<Result> result1 = transaction.get(prepareGet(0, 0, TABLE_1));
    populate(TABLE_1);
    Optional<Result> result2 = transaction.get(prepareGet(0, 0, TABLE_1));
    transaction.prepare();
    transaction.commit();
    // Assert
    assertThat(result1).isEqualTo(result2);
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 25 with GrpcTwoPhaseCommitTransaction

use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method commit_DeleteGivenForExistingAfterRead_ShouldDeleteRecord.

@Test
public void commit_DeleteGivenForExistingAfterRead_ShouldDeleteRecord() throws TransactionException {
    // Arrange
    populate(TABLE_1);
    GrpcTwoPhaseCommitTransaction transaction = manager.start();
    // Act
    Optional<Result> result = transaction.get(prepareGet(0, 0, TABLE_1));
    transaction.delete(prepareDelete(0, 0, TABLE_1));
    transaction.prepare();
    transaction.commit();
    // Assert
    assertThat(result.isPresent()).isTrue();
    GrpcTwoPhaseCommitTransaction another = manager.start();
    result = another.get(prepareGet(0, 0, TABLE_1));
    another.prepare();
    another.commit();
    assertThat(result.isPresent()).isFalse();
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Aggregations

GrpcTwoPhaseCommitTransaction (com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction)41 Test (org.junit.Test)40 Result (com.scalar.db.api.Result)32 TransactionState (com.scalar.db.api.TransactionState)3 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)3 Get (com.scalar.db.api.Get)2 DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet (com.scalar.db.server.DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet)2