Search in sources :

Example 66 with Result

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

the class TwoPhaseConsensusCommitIntegrationTest method commit_WriteSkewOnNonExistingRecordsWithSerializableWithExtraWriteAndCommitStatusFailed_ShouldRollbackProperly.

@Test
public void commit_WriteSkewOnNonExistingRecordsWithSerializableWithExtraWriteAndCommitStatusFailed_ShouldRollbackProperly() throws TransactionException, CoordinatorException {
    // Arrange
    State state = new State(ANY_ID_1, TransactionState.ABORTED);
    coordinator.putState(state);
    Isolation isolation = Isolation.SERIALIZABLE;
    SerializableStrategy strategy = SerializableStrategy.EXTRA_WRITE;
    // Act
    TwoPhaseConsensusCommit txSub1 = manager.start(ANY_ID_1, isolation, strategy);
    TwoPhaseConsensusCommit txSub2 = manager.join(txSub1.getId(), isolation, strategy);
    Optional<Result> result = txSub2.get(prepareGet(1, 0, TABLE_2));
    assertThat(result).isNotPresent();
    result = txSub1.get(prepareGet(0, 0, TABLE_1));
    assertThat(result).isNotPresent();
    int current1 = 0;
    txSub1.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, current1 + 1));
    assertThatThrownBy(() -> {
        txSub1.prepare();
        txSub2.prepare();
        txSub1.commit();
        txSub2.commit();
    }).isInstanceOf(CommitException.class);
    txSub1.rollback();
    txSub2.rollback();
    // Assert
    TwoPhaseConsensusCommit transaction = manager.start();
    result = transaction.get(prepareGet(0, 0, TABLE_1));
    assertThat(result).isNotPresent();
    result = transaction.get(prepareGet(1, 0, TABLE_2));
    assertThat(result).isNotPresent();
}
Also used : State(com.scalar.db.transaction.consensuscommit.Coordinator.State) TransactionState(com.scalar.db.api.TransactionState) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 67 with Result

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

the class JdbcTransactionIntegrationTest method get_GetGivenForNonExisting_ShouldReturnEmpty.

@Test
public void get_GetGivenForNonExisting_ShouldReturnEmpty() throws TransactionException {
    // Arrange
    populateRecords();
    JdbcTransaction transaction = manager.start();
    Get get = prepareGet(0, 4, NAMESPACE, TABLE);
    // Act
    Optional<Result> result = transaction.get(get);
    transaction.commit();
    // Assert
    assertThat(result.isPresent()).isFalse();
}
Also used : Get(com.scalar.db.api.Get) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 68 with Result

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

the class ConsensusCommitIntegrationTestBase method putAndCommit_DeleteGivenInBetweenTransactions_ShouldProduceSerializableResults.

@Test
public void putAndCommit_DeleteGivenInBetweenTransactions_ShouldProduceSerializableResults() throws CrudException, CommitException, UnknownTransactionStatusException {
    // Arrange
    ConsensusCommit transaction = manager.start();
    transaction.put(preparePut(0, 0, namespace1, TABLE_1).withValue(BALANCE, 2));
    transaction.commit();
    // Act
    ConsensusCommit transaction1 = manager.start();
    Optional<Result> result1 = transaction1.get(prepareGet(0, 0, namespace1, TABLE_1));
    int balance1 = 0;
    if (result1.isPresent()) {
        balance1 = getBalance(result1.get());
    }
    transaction1.put(preparePut(0, 0, namespace1, TABLE_1).withValue(BALANCE, balance1 + 1));
    ConsensusCommit transaction2 = manager.start();
    transaction2.get(prepareGet(0, 0, namespace1, TABLE_1));
    transaction2.delete(prepareDelete(0, 0, namespace1, TABLE_1));
    transaction2.commit();
    // the same transaction processing as transaction1
    ConsensusCommit transaction3 = manager.start();
    Optional<Result> result3 = transaction3.get(prepareGet(0, 0, namespace1, TABLE_1));
    int balance3 = 0;
    if (result3.isPresent()) {
        balance3 = getBalance(result3.get());
    }
    transaction3.put(preparePut(0, 0, namespace1, TABLE_1).withValue(BALANCE, balance3 + 1));
    transaction3.commit();
    Throwable thrown = catchThrowable(transaction1::commit);
    // Assert
    assertThat(thrown).isInstanceOf(CommitConflictException.class).hasCauseInstanceOf(NoMutationException.class);
    transaction = manager.start();
    Optional<Result> result = transaction.get(prepareGet(0, 0, namespace1, TABLE_1));
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(1);
}
Also used : CommitConflictException(com.scalar.db.exception.transaction.CommitConflictException) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 69 with Result

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

the class ConsensusCommitIntegrationTestBase method prepareTransfer.

private ConsensusCommit prepareTransfer(int fromId, String fromNamespace, String fromTable, int toId, String toNamespace, String toTable, int amount) throws CrudException {
    boolean differentTables = toNamespace.equals(fromNamespace) || !toTable.equals(fromTable);
    ConsensusCommit transaction = manager.start();
    List<Get> fromGets = prepareGets(fromNamespace, fromTable);
    List<Get> toGets = differentTables ? prepareGets(toNamespace, toTable) : fromGets;
    Optional<Result> fromResult = transaction.get(fromGets.get(fromId));
    assertThat(fromResult).isPresent();
    IntValue fromBalance = new IntValue(BALANCE, getBalance(fromResult.get()) - amount);
    Optional<Result> toResult = transaction.get(toGets.get(toId));
    assertThat(toResult).isPresent();
    IntValue toBalance = new IntValue(BALANCE, getBalance(toResult.get()) + amount);
    List<Put> fromPuts = preparePuts(fromNamespace, fromTable);
    List<Put> toPuts = differentTables ? preparePuts(toNamespace, toTable) : fromPuts;
    fromPuts.get(fromId).withValue(fromBalance);
    toPuts.get(toId).withValue(toBalance);
    transaction.put(fromPuts.get(fromId));
    transaction.put(toPuts.get(toId));
    return transaction;
}
Also used : Get(com.scalar.db.api.Get) IntValue(com.scalar.db.io.IntValue) Put(com.scalar.db.api.Put) Result(com.scalar.db.api.Result)

Example 70 with Result

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

the class ConsensusCommitIntegrationTestBase method get_CalledTwice_ShouldReturnFromSnapshotInSecondTime.

@Test
public void get_CalledTwice_ShouldReturnFromSnapshotInSecondTime() throws CrudException, ExecutionException, CommitException, UnknownTransactionStatusException {
    // Arrange
    populateRecords(namespace1, TABLE_1);
    ConsensusCommit transaction = manager.start();
    Get get = prepareGet(0, 0, namespace1, TABLE_1);
    // Act
    Optional<Result> result1 = transaction.get(get);
    Optional<Result> result2 = transaction.get(get);
    // Assert
    verify(storage).get(any(Get.class));
    assertThat(result1).isEqualTo(result2);
}
Also used : Get(com.scalar.db.api.Get) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Aggregations

Result (com.scalar.db.api.Result)324 Test (org.junit.Test)158 Get (com.scalar.db.api.Get)132 Scan (com.scalar.db.api.Scan)106 Put (com.scalar.db.api.Put)101 Key (com.scalar.db.io.Key)85 Test (org.junit.jupiter.api.Test)83 GrpcTransaction (com.scalar.db.transaction.rpc.GrpcTransaction)39 IntValue (com.scalar.db.io.IntValue)36 GrpcTwoPhaseCommitTransaction (com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction)32 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)29 Delete (com.scalar.db.api.Delete)28 TextValue (com.scalar.db.io.TextValue)24 Value (com.scalar.db.io.Value)19 BigIntValue (com.scalar.db.io.BigIntValue)16 Scanner (com.scalar.db.api.Scanner)15 ArrayList (java.util.ArrayList)14 BooleanValue (com.scalar.db.io.BooleanValue)11 ConditionalExpression (com.scalar.db.api.ConditionalExpression)10 ScanAll (com.scalar.db.api.ScanAll)9