Search in sources :

Example 31 with GrpcTransaction

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method getState_forFailedTransaction_ShouldReturnAbortedState.

@Test
public void getState_forFailedTransaction_ShouldReturnAbortedState() throws TransactionException {
    // Arrange
    GrpcTransaction transaction1 = manager.start();
    transaction1.get(prepareGet(0, 0, TABLE_1));
    transaction1.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
    GrpcTransaction transaction2 = manager.start();
    transaction2.get(prepareGet(0, 0, TABLE_1));
    transaction2.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
    transaction2.commit();
    assertThatCode(transaction1::commit).isInstanceOf(CommitException.class);
    // Act
    TransactionState state = manager.getState(transaction1.getId());
    // Assert
    assertThat(state).isEqualTo(TransactionState.ABORTED);
}
Also used : TransactionState(com.scalar.db.api.TransactionState) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Test(org.junit.Test)

Example 32 with GrpcTransaction

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method putAndCommit_DeleteGivenInBetweenTransactions_ShouldProduceSerializableResults.

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

Example 33 with GrpcTransaction

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method scan_OverlappingPutGivenBefore_ShouldThrowIllegalArgumentException.

@Test
public void scan_OverlappingPutGivenBefore_ShouldThrowIllegalArgumentException() throws TransactionException {
    // Arrange
    GrpcTransaction transaction = manager.start();
    transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
    // Act
    Scan scan = prepareScan(0, 0, 0, TABLE_1);
    Throwable thrown = catchThrowable(() -> transaction.scan(scan));
    transaction.abort();
    // Assert
    assertThat(thrown).isInstanceOf(IllegalArgumentException.class);
}
Also used : Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Scan(com.scalar.db.api.Scan) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Test(org.junit.Test)

Example 34 with GrpcTransaction

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method populateRecords.

private void populateRecords(String table) throws TransactionException {
    GrpcTransaction transaction = manager.start();
    IntStream.range(0, NUM_ACCOUNTS).forEach(i -> IntStream.range(0, NUM_TYPES).forEach(j -> {
        Key partitionKey = new Key(ACCOUNT_ID, i);
        Key clusteringKey = new Key(ACCOUNT_TYPE, j);
        Put put = new Put(partitionKey, clusteringKey).forNamespace(NAMESPACE).forTable(table).withValue(BALANCE, INITIAL_BALANCE);
        try {
            transaction.put(put);
        } catch (TransactionException e) {
            throw new RuntimeException(e);
        }
    }));
    transaction.commit();
}
Also used : IntStream(java.util.stream.IntStream) IntValue(com.scalar.db.io.IntValue) Arrays(java.util.Arrays) BeforeClass(org.junit.BeforeClass) Consistency(com.scalar.db.api.Consistency) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ExecutionException(com.scalar.db.exception.storage.ExecutionException) Value(com.scalar.db.io.Value) StorageFactory(com.scalar.db.service.StorageFactory) DistributedStorageAdmin(com.scalar.db.api.DistributedStorageAdmin) ConsensusCommitConfig(com.scalar.db.transaction.consensuscommit.ConsensusCommitConfig) TableMetadata(com.scalar.db.api.TableMetadata) ArrayList(java.util.ArrayList) Result(com.scalar.db.api.Result) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) GrpcTransactionManager(com.scalar.db.transaction.rpc.GrpcTransactionManager) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) CommitException(com.scalar.db.exception.transaction.CommitException) DataType(com.scalar.db.io.DataType) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) CommitConflictException(com.scalar.db.exception.transaction.CommitConflictException) Before(org.junit.Before) GrpcConfig(com.scalar.db.storage.rpc.GrpcConfig) Key(com.scalar.db.io.Key) AfterClass(org.junit.AfterClass) TransactionState(com.scalar.db.api.TransactionState) IOException(java.io.IOException) Test(org.junit.Test) Put(com.scalar.db.api.Put) Delete(com.scalar.db.api.Delete) Get(com.scalar.db.api.Get) List(java.util.List) Scan(com.scalar.db.api.Scan) Optional(java.util.Optional) TransactionException(com.scalar.db.exception.transaction.TransactionException) ConsensusCommitAdmin(com.scalar.db.transaction.consensuscommit.ConsensusCommitAdmin) Assertions.assertThatCode(org.assertj.core.api.Assertions.assertThatCode) TransactionException(com.scalar.db.exception.transaction.TransactionException) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put)

Example 35 with GrpcTransaction

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method get_PutCalledBefore_ShouldGet.

@Test
public void get_PutCalledBefore_ShouldGet() throws TransactionException {
    // Arrange
    GrpcTransaction 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::commit).doesNotThrowAnyException();
    // Assert
    assertThat(result).isPresent();
    assertThat(getBalance(result.get())).isEqualTo(1);
}
Also used : Get(com.scalar.db.api.Get) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Aggregations

GrpcTransaction (com.scalar.db.transaction.rpc.GrpcTransaction)52 Result (com.scalar.db.api.Result)41 Test (org.junit.Test)37 Get (com.scalar.db.api.Get)34 Put (com.scalar.db.api.Put)19 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)13 Scan (com.scalar.db.api.Scan)9 Delete (com.scalar.db.api.Delete)8 DistributedTransactionServiceWithConsensusCommitIntegrationTest.preparePut (com.scalar.db.server.DistributedTransactionServiceWithConsensusCommitIntegrationTest.preparePut)7 TransactionState (com.scalar.db.api.TransactionState)4 IntValue (com.scalar.db.io.IntValue)4 DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet (com.scalar.db.server.DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet)4 Consistency (com.scalar.db.api.Consistency)2 DistributedStorageAdmin (com.scalar.db.api.DistributedStorageAdmin)2 TableMetadata (com.scalar.db.api.TableMetadata)2 ExecutionException (com.scalar.db.exception.storage.ExecutionException)2 TransactionException (com.scalar.db.exception.transaction.TransactionException)2 DataType (com.scalar.db.io.DataType)2 Key (com.scalar.db.io.Key)2 Value (com.scalar.db.io.Value)2