Search in sources :

Example 46 with GrpcTransaction

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

the class DistributedTransactionServiceWithConsensusCommitWithExtraWriteIntegrationTest method commit_WriteSkewOnExistingRecordsWithSerializableWithExtraWrite_OneShouldCommitTheOtherShouldThrowCommitConflictException.

private void commit_WriteSkewOnExistingRecordsWithSerializableWithExtraWrite_OneShouldCommitTheOtherShouldThrowCommitConflictException(String table1, String table2) throws TransactionException {
    // Arrange
    List<Put> puts = Arrays.asList(preparePut(0, 0, table1).withValue(BALANCE, 1), preparePut(0, 1, table2).withValue(BALANCE, 1));
    GrpcTransaction transaction = manager.start();
    transaction.put(puts);
    transaction.commit();
    // Act
    GrpcTransaction transaction1 = manager.start();
    GrpcTransaction transaction2 = manager.start();
    Get get1_1 = prepareGet(0, 1, table2);
    Optional<Result> result1 = transaction1.get(get1_1);
    assertThat(result1).isPresent();
    int current1 = getBalance(result1.get());
    Get get1_2 = prepareGet(0, 0, table1);
    transaction1.get(get1_2);
    Get get2_1 = prepareGet(0, 0, table1);
    Optional<Result> result2 = transaction2.get(get2_1);
    assertThat(result2).isPresent();
    int current2 = getBalance(result2.get());
    Get get2_2 = prepareGet(0, 1, table2);
    transaction2.get(get2_2);
    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);
    transaction1.commit();
    Throwable thrown = catchThrowable(transaction2::commit);
    // Assert
    transaction = manager.start();
    result1 = transaction.get(get1_1);
    result2 = transaction.get(get2_1);
    transaction.commit();
    assertThat(result1).isPresent();
    assertThat(getBalance(result1.get())).isEqualTo(1);
    assertThat(result2).isPresent();
    assertThat(getBalance(result2.get())).isEqualTo(2);
    assertThat(thrown).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) DistributedTransactionServiceWithConsensusCommitIntegrationTest.preparePut(com.scalar.db.server.DistributedTransactionServiceWithConsensusCommitIntegrationTest.preparePut) Put(com.scalar.db.api.Put) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Result(com.scalar.db.api.Result)

Example 47 with GrpcTransaction

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

the class DistributedTransactionServiceWithJdbcTransactionIntegrationTest method putAndCommit_PutGivenForExistingAfterRead_ShouldUpdateRecord.

@Test
public void putAndCommit_PutGivenForExistingAfterRead_ShouldUpdateRecord() throws TransactionException {
    // Arrange
    populateRecords();
    Get get = prepareGet(0, 0, NAMESPACE, TABLE);
    GrpcTransaction transaction = manager.start();
    // Act
    Optional<Result> result = transaction.get(get);
    assertThat(result).isPresent();
    int expected = getBalance(result.get()) + 100;
    Put put = preparePut(0, 0, NAMESPACE, TABLE).withValue(BALANCE, expected);
    transaction.put(put);
    transaction.commit();
    // Assert
    GrpcTransaction another = manager.start();
    Optional<Result> actual = another.get(get);
    another.commit();
    assertThat(actual).isPresent();
    assertThat(getBalance(actual.get())).isEqualTo(expected);
}
Also used : Get(com.scalar.db.api.Get) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Put(com.scalar.db.api.Put) Result(com.scalar.db.api.Result) Test(org.junit.Test)

Example 48 with GrpcTransaction

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

the class DistributedTransactionServiceWithJdbcTransactionIntegrationTest method prepareTransfer.

private GrpcTransaction prepareTransfer(int fromId, int toId, int amount) throws TransactionException {
    GrpcTransaction transaction = manager.start();
    List<Get> gets = prepareGets(NAMESPACE, TABLE);
    Optional<Result> result1 = transaction.get(gets.get(fromId));
    assertThat(result1).isPresent();
    IntValue fromBalance = new IntValue(BALANCE, getBalance(result1.get()) - amount);
    Optional<Result> result2 = transaction.get(gets.get(toId));
    assertThat(result2).isPresent();
    IntValue toBalance = new IntValue(BALANCE, getBalance(result2.get()) + amount);
    List<Put> puts = preparePuts(NAMESPACE, TABLE);
    puts.get(fromId).withValue(fromBalance);
    puts.get(toId).withValue(toBalance);
    transaction.put(puts.get(fromId));
    transaction.put(puts.get(toId));
    return transaction;
}
Also used : Get(com.scalar.db.api.Get) IntValue(com.scalar.db.io.IntValue) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Put(com.scalar.db.api.Put) Result(com.scalar.db.api.Result)

Example 49 with GrpcTransaction

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

the class DistributedTransactionServiceWithJdbcTransactionIntegrationTest method get_GetGivenForCommittedRecord_ShouldReturnRecord.

@Test
public void get_GetGivenForCommittedRecord_ShouldReturnRecord() throws TransactionException {
    // Arrange
    populateRecords();
    GrpcTransaction transaction = manager.start();
    Get get = prepareGet(0, 0, NAMESPACE, TABLE);
    // Act
    Optional<Result> result = transaction.get(get);
    transaction.commit();
    // Assert
    assertThat(result.isPresent()).isTrue();
}
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 50 with GrpcTransaction

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

the class DistributedTransactionServiceWithJdbcTransactionIntegrationTest method scan_ScanGivenForNonExisting_ShouldReturnEmpty.

@Test
public void scan_ScanGivenForNonExisting_ShouldReturnEmpty() throws TransactionException {
    // Arrange
    populateRecords();
    GrpcTransaction transaction = manager.start();
    Scan scan = prepareScan(0, 4, 4, NAMESPACE, TABLE);
    // Act
    List<Result> results = transaction.scan(scan);
    transaction.commit();
    // Assert
    assertThat(results.size()).isEqualTo(0);
}
Also used : Scan(com.scalar.db.api.Scan) 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