Search in sources :

Example 16 with GrpcTwoPhaseCommitTransaction

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method populate.

private void populate(String table) throws TransactionException {
    GrpcTwoPhaseCommitTransaction transaction = manager.start();
    for (int i = 0; i < NUM_ACCOUNTS; i++) {
        for (int j = 0; j < NUM_TYPES; j++) {
            transaction.put(preparePut(i, j, table).withValue(BALANCE, INITIAL_BALANCE));
        }
    }
    transaction.prepare();
    transaction.commit();
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction)

Example 17 with GrpcTwoPhaseCommitTransaction

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method scan_ScanGivenForCommittedRecord_ShouldReturnRecord.

@Test
public void scan_ScanGivenForCommittedRecord_ShouldReturnRecord() throws TransactionException {
    // Arrange
    populate(TABLE_1);
    GrpcTwoPhaseCommitTransaction transaction = manager.start();
    // Act
    List<Result> results = transaction.scan(prepareScan(0, 0, 0, TABLE_1));
    transaction.prepare();
    transaction.commit();
    // Assert
    assertThat(results.size()).isEqualTo(1);
    assertThat(getAccountId(results.get(0))).isEqualTo(0);
    assertThat(getAccountType(results.get(0))).isEqualTo(0);
    assertThat(getBalance(results.get(0))).isEqualTo(INITIAL_BALANCE);
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 18 with GrpcTwoPhaseCommitTransaction

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method scan_ScanGivenForNonExisting_ShouldReturnEmpty.

@Test
public void scan_ScanGivenForNonExisting_ShouldReturnEmpty() throws TransactionException {
    // Arrange
    populate(TABLE_1);
    GrpcTwoPhaseCommitTransaction transaction = manager.start();
    // Act
    List<Result> results = transaction.scan(prepareScan(0, 4, 4, TABLE_1));
    transaction.prepare();
    transaction.commit();
    // Assert
    assertThat(results.size()).isEqualTo(0);
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 19 with GrpcTwoPhaseCommitTransaction

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method putAndCommit_GetsAndPutsForSameKeyButDifferentTablesGiven_ShouldCommitBoth.

@Test
public void putAndCommit_GetsAndPutsForSameKeyButDifferentTablesGiven_ShouldCommitBoth() throws TransactionException {
    // Arrange
    int expected = INITIAL_BALANCE;
    int fromId = 0;
    int fromType = 0;
    int toId = 1;
    int toType = 0;
    int anotherFromId = fromId;
    int anotherFromType = fromType;
    int anotherToId = toId;
    int anotherToType = toType;
    GrpcTwoPhaseCommitTransaction fromTx = manager.start();
    GrpcTwoPhaseCommitTransaction toTx = manager.join(fromTx.getId());
    fromTx.put(preparePut(fromId, fromType, TABLE_1).withValue(BALANCE, expected));
    toTx.put(preparePut(toId, toType, TABLE_2).withValue(BALANCE, expected));
    // Act Assert
    assertThatCode(() -> {
        GrpcTwoPhaseCommitTransaction anotherFromTx = manager.start();
        GrpcTwoPhaseCommitTransaction anotherToTx = manager.join(anotherFromTx.getId());
        anotherFromTx.put(preparePut(anotherFromId, anotherFromType, TABLE_2).withValue(BALANCE, expected));
        anotherToTx.put(preparePut(anotherToId, anotherToType, TABLE_1).withValue(BALANCE, expected));
        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(expected);
    result = another.get(prepareGet(toId, toType, TABLE_2));
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(expected);
    result = another.get(prepareGet(anotherFromId, anotherFromType, TABLE_2));
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(expected);
    result = another.get(prepareGet(anotherToId, anotherToType, TABLE_1));
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(expected);
    another.prepare();
    another.commit();
}
Also used : GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 20 with GrpcTwoPhaseCommitTransaction

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method get_DeleteCalledBefore_ShouldReturnEmpty.

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

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