Search in sources :

Example 31 with GrpcTwoPhaseCommitTransaction

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method putAndCommit_GetsAndPutsGiven_ShouldCommitProperly.

@Test
public void putAndCommit_GetsAndPutsGiven_ShouldCommitProperly() throws TransactionException {
    // Arrange
    populate(TABLE_1);
    populate(TABLE_2);
    int amount = 100;
    int fromBalance = INITIAL_BALANCE - amount;
    int toBalance = INITIAL_BALANCE + amount;
    int fromId = 0;
    int fromType = 0;
    int toId = 1;
    int toType = 0;
    GrpcTwoPhaseCommitTransaction fromTx = manager.start();
    GrpcTwoPhaseCommitTransaction toTx = manager.join(fromTx.getId());
    transfer(fromId, fromType, TABLE_1, fromTx, toId, toType, TABLE_2, toTx, amount);
    // Act Assert
    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(fromBalance);
    result = another.get(prepareGet(toId, toType, TABLE_2));
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(toBalance);
    another.prepare();
    another.commit();
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 32 with GrpcTwoPhaseCommitTransaction

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method delete_PutCalledBefore_ShouldDelete.

@Test
public void delete_PutCalledBefore_ShouldDelete() 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();
    Optional<Result> resultBefore = transaction1.get(prepareGet(0, 0, TABLE_1));
    transaction1.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 2));
    transaction1.delete(prepareDelete(0, 0, TABLE_1));
    assertThatCode(() -> {
        transaction1.prepare();
        transaction1.commit();
    }).doesNotThrowAnyException();
    // Assert
    GrpcTwoPhaseCommitTransaction transaction2 = manager.start();
    Optional<Result> resultAfter = transaction2.get(prepareGet(0, 0, TABLE_1));
    transaction2.prepare();
    transaction2.commit();
    assertThat(resultBefore.isPresent()).isTrue();
    assertThat(resultAfter.isPresent()).isFalse();
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 33 with GrpcTwoPhaseCommitTransaction

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method putAndCommit_PutGivenForExistingAfterRead_ShouldUpdateRecord.

@Test
public void putAndCommit_PutGivenForExistingAfterRead_ShouldUpdateRecord() throws TransactionException {
    // Arrange
    populate(TABLE_1);
    GrpcTwoPhaseCommitTransaction transaction = manager.start();
    // Act
    Optional<Result> result = transaction.get(prepareGet(0, 0, TABLE_1));
    assertThat(result.isPresent()).isTrue();
    int afterBalance = getBalance(result.get()) + 100;
    transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, afterBalance));
    transaction.prepare();
    transaction.commit();
    // Assert
    GrpcTwoPhaseCommitTransaction another = manager.start();
    result = another.get(prepareGet(0, 0, TABLE_1));
    another.prepare();
    another.commit();
    assertThat(result).isPresent();
    assertThat(getAccountId(result.get())).isEqualTo(0);
    assertThat(getAccountType(result.get())).isEqualTo(0);
    assertThat(getBalance(result.get())).isEqualTo(afterBalance);
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 34 with GrpcTwoPhaseCommitTransaction

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method commit_ConflictingDeletesGivenForExisting_ShouldCommitOneAndAbortTheOther.

@Test
public void commit_ConflictingDeletesGivenForExisting_ShouldCommitOneAndAbortTheOther() throws TransactionException {
    // Arrange
    int account1Id = 0;
    int account1Type = 0;
    int account2Id = 1;
    int account2Type = 0;
    int account3Id = 2;
    int account3Type = 0;
    populate(TABLE_1);
    populate(TABLE_2);
    GrpcTwoPhaseCommitTransaction tx1 = manager.start();
    GrpcTwoPhaseCommitTransaction tx2 = manager.join(tx1.getId());
    deletes(account1Id, account1Type, TABLE_1, tx1, account2Id, account2Type, TABLE_2, tx2);
    // Act Assert
    assertThatCode(() -> {
        GrpcTwoPhaseCommitTransaction tx3 = manager.start();
        GrpcTwoPhaseCommitTransaction tx4 = manager.join(tx3.getId());
        deletes(account2Id, account2Type, TABLE_2, tx3, account3Id, account3Type, TABLE_1, tx4);
        tx3.prepare();
        tx4.prepare();
        tx3.commit();
        tx4.commit();
    }).doesNotThrowAnyException();
    assertThatThrownBy(() -> {
        tx1.prepare();
        tx2.prepare();
    }).isInstanceOf(PreparationException.class);
    tx1.rollback();
    tx2.rollback();
    // Assert
    GrpcTwoPhaseCommitTransaction another = manager.start();
    Optional<Result> result = another.get(prepareGet(account1Id, account1Type, TABLE_1));
    assertThat(result).isPresent();
    result = another.get(prepareGet(account2Id, account2Type, TABLE_2));
    assertThat(result).isNotPresent();
    result = another.get(prepareGet(account3Id, account3Type, TABLE_1));
    assertThat(result).isNotPresent();
    another.prepare();
    another.commit();
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 35 with GrpcTwoPhaseCommitTransaction

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method putAndCommit_PutGivenForNonExisting_ShouldCreateRecord.

@Test
public void putAndCommit_PutGivenForNonExisting_ShouldCreateRecord() throws TransactionException {
    // Arrange
    int expected = INITIAL_BALANCE;
    GrpcTwoPhaseCommitTransaction transaction = manager.start();
    // Act
    transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, expected));
    transaction.prepare();
    transaction.commit();
    // Assert
    GrpcTwoPhaseCommitTransaction another = manager.start();
    Optional<Result> result = another.get(prepareGet(0, 0, TABLE_1));
    another.prepare();
    another.commit();
    assertThat(result).isPresent();
    assertThat(getAccountId(result.get())).isEqualTo(0);
    assertThat(getAccountType(result.get())).isEqualTo(0);
    assertThat(getBalance(result.get())).isEqualTo(expected);
}
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