Search in sources :

Example 26 with GrpcTwoPhaseCommitTransaction

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method prepare_DeleteGivenWithoutRead_ShouldThrowPreparationException.

@Test
public void prepare_DeleteGivenWithoutRead_ShouldThrowPreparationException() throws TransactionException {
    // Arrange
    GrpcTwoPhaseCommitTransaction transaction = manager.start();
    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 27 with GrpcTwoPhaseCommitTransaction

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method get_PutCalledBefore_ShouldGet.

@Test
public void get_PutCalledBefore_ShouldGet() throws TransactionException {
    // Arrange
    GrpcTwoPhaseCommitTransaction transaction = manager.start();
    // Act
    transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
    Get get = prepareGet(0, 0, TABLE_1);
    Optional<Result> result = transaction.get(get);
    assertThatCode(() -> {
        transaction.prepare();
        transaction.commit();
    }).doesNotThrowAnyException();
    // Assert
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(1);
}
Also used : DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet(com.scalar.db.server.DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet) Get(com.scalar.db.api.Get) GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 28 with GrpcTwoPhaseCommitTransaction

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method commit_NonConflictingPutsGivenForExisting_ShouldCommitBoth.

@Test
public void commit_NonConflictingPutsGivenForExisting_ShouldCommitBoth() throws TransactionException {
    // Arrange
    int amount1 = 100;
    int amount2 = 200;
    int fromId = 0;
    int fromType = 0;
    int toId = 1;
    int toType = 0;
    int anotherFromId = 2;
    int anotherFromType = 0;
    int anotherToId = 3;
    int anotherToType = 0;
    populate(TABLE_1);
    populate(TABLE_2);
    GrpcTwoPhaseCommitTransaction fromTx = manager.start();
    GrpcTwoPhaseCommitTransaction toTx = manager.join(fromTx.getId());
    transfer(fromId, fromType, TABLE_1, fromTx, toId, toType, TABLE_2, toTx, amount1);
    // Act Assert
    assertThatCode(() -> {
        GrpcTwoPhaseCommitTransaction anotherFromTx = manager.start();
        GrpcTwoPhaseCommitTransaction anotherToTx = manager.join(anotherFromTx.getId());
        transfer(anotherFromId, anotherFromType, TABLE_2, anotherFromTx, anotherToId, anotherToType, TABLE_1, anotherToTx, amount2);
        anotherFromTx.prepare();
        anotherToTx.prepare();
        anotherFromTx.commit();
        anotherToTx.commit();
    }).doesNotThrowAnyException();
    assertThatCode(() -> {
        fromTx.prepare();
        toTx.prepare();
        fromTx.commit();
        toTx.commit();
    }).doesNotThrowAnyException();
    // Assert
    GrpcTwoPhaseCommitTransaction another = manager.start();
    Optional<Result> result = another.get(prepareGet(fromId, fromType, TABLE_1));
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE - amount1);
    result = another.get(prepareGet(toId, toType, TABLE_2));
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE + amount1);
    result = another.get(prepareGet(anotherFromId, anotherFromType, TABLE_2));
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE - amount2);
    result = another.get(prepareGet(anotherToId, anotherToType, TABLE_1));
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE + amount2);
    another.prepare();
    another.commit();
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 29 with GrpcTwoPhaseCommitTransaction

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method scan_OverlappingPutGivenBefore_ShouldThrowIllegalArgumentException.

@Test
public void scan_OverlappingPutGivenBefore_ShouldThrowIllegalArgumentException() throws TransactionException {
    // Arrange
    GrpcTwoPhaseCommitTransaction transaction = manager.start();
    transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
    // Act Assert
    assertThatThrownBy(() -> transaction.scan(prepareScan(0, 0, 0, TABLE_1))).isInstanceOf(IllegalArgumentException.class);
    transaction.rollback();
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Test(org.junit.Test)

Example 30 with GrpcTwoPhaseCommitTransaction

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method scan_DeleteCalledBefore_ShouldReturnEmpty.

@Test
public void scan_DeleteCalledBefore_ShouldReturnEmpty() throws TransactionException {
    // Arrange
    GrpcTwoPhaseCommitTransaction transaction = manager.start();
    transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
    transaction.prepare();
    transaction.commit();
    // Act Assert
    GrpcTwoPhaseCommitTransaction transaction1 = manager.start();
    List<Result> resultBefore = transaction1.scan(prepareScan(0, 0, 0, TABLE_1));
    transaction1.delete(prepareDelete(0, 0, TABLE_1));
    List<Result> resultAfter = transaction1.scan(prepareScan(0, 0, 0, TABLE_1));
    assertThatCode(() -> {
        transaction1.prepare();
        transaction1.commit();
    }).doesNotThrowAnyException();
    // Assert
    assertThat(resultBefore.size()).isEqualTo(1);
    assertThat(resultAfter.size()).isEqualTo(0);
}
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