Search in sources :

Example 56 with Get

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

the class DistributedTransactionServiceWithConsensusCommitWithExtraReadIntegrationTest method commit_WriteSkewOnExistingRecordsWithSerializableWithExtraRead_OneShouldCommitTheOtherShouldThrowCommitConflictException.

private void commit_WriteSkewOnExistingRecordsWithSerializableWithExtraRead_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 57 with Get

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

the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method put_DeleteCalledBefore_ShouldThrowIllegalArgumentException.

@Test
public void put_DeleteCalledBefore_ShouldThrowIllegalArgumentException() 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();
    Get get = prepareGet(0, 0, TABLE_1);
    transaction1.get(get);
    transaction1.delete(prepareDelete(0, 0, TABLE_1));
    Throwable thrown = catchThrowable(() -> transaction1.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 2)));
    transaction1.rollback();
    // Assert
    assertThat(thrown).isInstanceOf(IllegalArgumentException.class);
}
Also used : DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet(com.scalar.db.server.DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet) Get(com.scalar.db.api.Get) GrpcTwoPhaseCommitTransaction(com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) Test(org.junit.Test)

Example 58 with Get

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method get_GetGivenForCommittedRecord_ShouldReturnRecord.

@Test
public void get_GetGivenForCommittedRecord_ShouldReturnRecord() throws TransactionException {
    // Arrange
    populateRecords(TABLE_1);
    GrpcTransaction transaction = manager.start();
    Get get = prepareGet(0, 0, TABLE_1);
    // 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 59 with Get

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method commit_NonConflictingPutsGivenForExisting_ShouldCommitBoth.

private void commit_NonConflictingPutsGivenForExisting_ShouldCommitBoth(String table1, String table2) throws TransactionException {
    // Arrange
    boolean differentTables = !table1.equals(table2);
    int amount1 = 100;
    int amount2 = 200;
    int from = 0;
    int to = NUM_TYPES;
    int anotherFrom = NUM_TYPES * 2;
    int anotherTo = NUM_TYPES * 3;
    populateRecords(table1);
    if (differentTables) {
        populateRecords(table2);
    }
    // Act
    GrpcTransaction transaction = prepareTransfer(from, table1, to, table2, amount1);
    assertThatCode(() -> prepareTransfer(anotherFrom, table2, anotherTo, table1, amount2).commit()).doesNotThrowAnyException();
    assertThatCode(transaction::commit).doesNotThrowAnyException();
    // Assert
    List<Get> gets1 = prepareGets(table1);
    List<Get> gets2 = prepareGets(table2);
    GrpcTransaction another = manager.start();
    Optional<Result> fromResult = another.get(gets1.get(from));
    assertThat(fromResult).isPresent();
    assertThat(getBalance(fromResult.get())).isEqualTo(INITIAL_BALANCE - amount1);
    Optional<Result> toResult = another.get(gets2.get(to));
    assertThat(toResult).isPresent();
    assertThat(getBalance(toResult.get())).isEqualTo(INITIAL_BALANCE + amount1);
    Optional<Result> anotherFromResult = another.get(gets2.get(anotherFrom));
    assertThat(anotherFromResult).isPresent();
    assertThat(getBalance(anotherFromResult.get())).isEqualTo(INITIAL_BALANCE - amount2);
    Optional<Result> anotherToResult = another.get(gets1.get(anotherTo));
    assertThat(anotherToResult).isPresent();
    assertThat(getBalance(anotherToResult.get())).isEqualTo(INITIAL_BALANCE + amount2);
    another.commit();
}
Also used : Get(com.scalar.db.api.Get) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Result(com.scalar.db.api.Result)

Example 60 with Get

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method putAndCommit_PutGivenForNonExisting_ShouldCreateRecord.

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

Aggregations

Get (com.scalar.db.api.Get)286 Test (org.junit.jupiter.api.Test)130 Result (com.scalar.db.api.Result)129 Key (com.scalar.db.io.Key)126 Test (org.junit.Test)88 Put (com.scalar.db.api.Put)86 GrpcTransaction (com.scalar.db.transaction.rpc.GrpcTransaction)32 Delete (com.scalar.db.api.Delete)28 IntValue (com.scalar.db.io.IntValue)28 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)21 TextValue (com.scalar.db.io.TextValue)16 ConditionalExpression (com.scalar.db.api.ConditionalExpression)13 PartitionKey (com.azure.cosmos.models.PartitionKey)12 TableMetadata (com.scalar.db.api.TableMetadata)10 BooleanValue (com.scalar.db.io.BooleanValue)10 PutIf (com.scalar.db.api.PutIf)8 BigIntValue (com.scalar.db.io.BigIntValue)7 HashMap (java.util.HashMap)7 BlobValue (com.scalar.db.io.BlobValue)6 DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet (com.scalar.db.server.DistributedTransactionServiceWithConsensusCommitIntegrationTest.prepareGet)6