Search in sources :

Example 1 with GrpcTransaction

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

the class DistributedTransactionServiceWithConsensusCommitWithExtraWriteIntegrationTest method commit_WriteSkewOnNonExistingRecordsWithSerializableWithExtraWrite_OneShouldCommitTheOtherShouldThrowCommitConflictException.

private void commit_WriteSkewOnNonExistingRecordsWithSerializableWithExtraWrite_OneShouldCommitTheOtherShouldThrowCommitConflictException(String table1, String table2) throws TransactionException {
    // Arrange
    // no records
    // Act
    GrpcTransaction transaction1 = manager.start();
    GrpcTransaction transaction2 = manager.start();
    Get get1_1 = prepareGet(0, 1, table2);
    Optional<Result> result1 = transaction1.get(get1_1);
    Get get1_2 = prepareGet(0, 0, table1);
    transaction1.get(get1_2);
    int current1 = 0;
    Get get2_1 = prepareGet(0, 0, table1);
    Optional<Result> result2 = transaction2.get(get2_1);
    Get get2_2 = prepareGet(0, 1, table2);
    transaction2.get(get2_2);
    int current2 = 0;
    Put put1 = preparePut(0, 0, table1).withValue(BALANCE, current1 + 1);
    transaction1.put(put1);
    Put put2 = preparePut(0, 1, table2).withValue(BALANCE, current2 + 1);
    transaction2.put(put2);
    Throwable thrown1 = catchThrowable(transaction1::commit);
    Throwable thrown2 = catchThrowable(transaction2::commit);
    // Assert
    assertThat(result1.isPresent()).isFalse();
    assertThat(result2.isPresent()).isFalse();
    GrpcTransaction transaction = manager.start();
    result1 = transaction.get(get1_1);
    result2 = transaction.get(get2_1);
    transaction.commit();
    assertThat(result1.isPresent()).isFalse();
    assertThat(result2).isPresent();
    assertThat(getBalance(result2.get())).isEqualTo(1);
    assertThat(thrown1).doesNotThrowAnyException();
    assertThat(thrown2).isInstanceOf(CommitConflictException.class);
}
Also used : DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet(com.scalar.db.server.DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet) Get(com.scalar.db.api.Get) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) DistributedTransactionServiceWithConsensusCommitIntegrationTest.preparePut(com.scalar.db.server.DistributedTransactionServiceWithConsensusCommitIntegrationTest.preparePut) Put(com.scalar.db.api.Put) Result(com.scalar.db.api.Result)

Example 2 with GrpcTransaction

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

the class DistributedTransactionServiceWithJdbcTransactionIntegrationTest method commit_DeleteGivenForExistingAfterRead_ShouldDeleteRecord.

@Test
public void commit_DeleteGivenForExistingAfterRead_ShouldDeleteRecord() throws TransactionException {
    // Arrange
    populateRecords();
    Get get = prepareGet(0, 0, NAMESPACE, TABLE);
    Delete delete = prepareDelete(0, 0, NAMESPACE, TABLE);
    GrpcTransaction transaction = manager.start();
    // Act
    Optional<Result> result = transaction.get(get);
    transaction.delete(delete);
    transaction.commit();
    // Assert
    assertThat(result.isPresent()).isTrue();
    GrpcTransaction another = manager.start();
    Optional<Result> result1 = another.get(get);
    another.commit();
    assertThat(result1.isPresent()).isFalse();
}
Also used : Delete(com.scalar.db.api.Delete) Get(com.scalar.db.api.Get) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 3 with GrpcTransaction

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

the class DistributedTransactionServiceWithJdbcTransactionIntegrationTest method get_GetGivenForNonExisting_ShouldReturnEmpty.

@Test
public void get_GetGivenForNonExisting_ShouldReturnEmpty() throws TransactionException {
    // Arrange
    populateRecords();
    GrpcTransaction 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) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 4 with GrpcTransaction

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

the class DistributedTransactionServiceWithJdbcTransactionIntegrationTest method populateRecords.

private void populateRecords() 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 (CrudException e) {
            throw new RuntimeException(e);
        }
    }));
    transaction.commit();
}
Also used : IntStream(java.util.stream.IntStream) IntValue(com.scalar.db.io.IntValue) 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) TableMetadata(com.scalar.db.api.TableMetadata) CrudException(com.scalar.db.exception.transaction.CrudException) ArrayList(java.util.ArrayList) Result(com.scalar.db.api.Result) GrpcTransactionManager(com.scalar.db.transaction.rpc.GrpcTransactionManager) DataType(com.scalar.db.io.DataType) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Before(org.junit.Before) GrpcConfig(com.scalar.db.storage.rpc.GrpcConfig) Key(com.scalar.db.io.Key) AfterClass(org.junit.AfterClass) 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) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Key(com.scalar.db.io.Key) Put(com.scalar.db.api.Put) CrudException(com.scalar.db.exception.transaction.CrudException)

Example 5 with GrpcTransaction

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

the class DistributedTransactionServiceWithJdbcTransactionIntegrationTest method putAndCommit_GetsAndPutsGiven_ShouldCommitProperly.

@Test
public void putAndCommit_GetsAndPutsGiven_ShouldCommitProperly() throws TransactionException {
    // Arrange
    populateRecords();
    List<Get> gets = prepareGets(NAMESPACE, TABLE);
    int amount = 100;
    int fromBalance = INITIAL_BALANCE - amount;
    int toBalance = INITIAL_BALANCE + amount;
    int from = 0;
    int to = NUM_TYPES;
    // Act
    prepareTransfer(from, to, amount).commit();
    // Assert
    GrpcTransaction another = null;
    try {
        another = manager.start();
        Optional<Result> fromResult = another.get(gets.get(from));
        assertThat(fromResult).isPresent();
        assertThat(getBalance(fromResult.get())).isEqualTo(fromBalance);
        Optional<Result> toResult = another.get(gets.get(to));
        assertThat(toResult).isPresent();
        assertThat(getBalance(toResult.get())).isEqualTo(toBalance);
    } finally {
        if (another != null) {
            another.commit();
        }
    }
}
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