Search in sources :

Example 46 with Put

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method putAndCommit_GetsAndPutsForSameKeyButDifferentTablesGiven_ShouldCommitBoth.

@Test
public void putAndCommit_GetsAndPutsForSameKeyButDifferentTablesGiven_ShouldCommitBoth() throws TransactionException {
    // Arrange
    int expected = INITIAL_BALANCE;
    List<Put> puts1 = preparePuts(TABLE_1);
    List<Put> puts2 = preparePuts(TABLE_2);
    int from = 0;
    int to = NUM_TYPES;
    int anotherFrom = from;
    int anotherTo = to;
    puts1.get(from).withValue(BALANCE, expected);
    puts1.get(to).withValue(BALANCE, expected);
    // Act Assert
    GrpcTransaction transaction = manager.start();
    transaction.put(puts1.get(from));
    transaction.put(puts1.get(to));
    GrpcTransaction conflictingTransaction = manager.start();
    puts2.get(from).withValue(BALANCE, expected);
    puts2.get(to).withValue(BALANCE, expected);
    assertThatCode(() -> {
        conflictingTransaction.put(puts2.get(anotherFrom));
        conflictingTransaction.put(puts2.get(anotherTo));
        conflictingTransaction.commit();
    }).doesNotThrowAnyException();
    assertThatCode(transaction::commit).doesNotThrowAnyException();
    // Assert
    List<Get> gets1 = prepareGets(TABLE_1);
    List<Get> gets2 = prepareGets(TABLE_2);
    GrpcTransaction another = manager.start();
    Optional<Result> fromResult = another.get(gets1.get(from));
    assertThat(fromResult).isPresent();
    assertThat(getBalance(fromResult.get())).isEqualTo(expected);
    Optional<Result> toResult = another.get(gets1.get(to));
    assertThat(toResult).isPresent();
    assertThat(getBalance(toResult.get())).isEqualTo(expected);
    Optional<Result> anotherFromResult = another.get(gets2.get(anotherFrom));
    assertThat(anotherFromResult).isPresent();
    assertThat(getBalance(anotherFromResult.get())).isEqualTo(expected);
    Optional<Result> anotherToResult = another.get(gets2.get(anotherTo));
    assertThat(anotherToResult).isPresent();
    assertThat(getBalance(anotherToResult.get())).isEqualTo(expected);
    another.commit();
}
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)

Example 47 with Put

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method commit_WriteSkewOnExistingRecordsWithSnapshot_ShouldProduceNonSerializableResult.

private void commit_WriteSkewOnExistingRecordsWithSnapshot_ShouldProduceNonSerializableResult(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();
    transaction2.commit();
    // Assert
    transaction = manager.start();
    result1 = transaction.get(get1_1);
    result2 = transaction.get(get2_1);
    transaction.commit();
    // the results can not be produced by executing the transactions serially
    assertThat(result1).isPresent();
    assertThat(getBalance(result1.get())).isEqualTo(2);
    assertThat(result2).isPresent();
    assertThat(getBalance(result2.get())).isEqualTo(2);
}
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)

Example 48 with Put

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method putAndCommit_PutGivenForExistingAndNeverRead_ShouldThrowCommitException.

@Test
public void putAndCommit_PutGivenForExistingAndNeverRead_ShouldThrowCommitException() throws TransactionException {
    // Arrange
    populateRecords(TABLE_1);
    List<Put> puts = preparePuts(TABLE_1);
    puts.get(0).withValue(BALANCE, 1100);
    GrpcTransaction transaction = manager.start();
    // Act Assert
    transaction.put(puts.get(0));
    assertThatThrownBy(transaction::commit).isInstanceOf(CommitException.class);
}
Also used : Put(com.scalar.db.api.Put) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Test(org.junit.Test)

Example 49 with Put

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

the class DistributedTransactionServiceWithConsensusCommitWithExtraReadIntegrationTest method commit_WriteSkewWithScanOnNonExistingRecordsWithSerializableWithExtraRead_ShouldThrowCommitConflictException.

@Test
public void commit_WriteSkewWithScanOnNonExistingRecordsWithSerializableWithExtraRead_ShouldThrowCommitConflictException() throws TransactionException {
    // Arrange
    // no records
    // Act
    GrpcTransaction transaction1 = manager.start();
    GrpcTransaction transaction2 = manager.start();
    List<Result> results1 = transaction1.scan(prepareScan(0, 0, 1, TABLE_1));
    int count1 = results1.size();
    List<Result> results2 = transaction2.scan(prepareScan(0, 0, 1, TABLE_1));
    int count2 = results2.size();
    Put put1 = preparePut(0, 0, TABLE_1).withValue(BALANCE, count1 + 1);
    transaction1.put(put1);
    Put put2 = preparePut(0, 1, TABLE_1).withValue(BALANCE, count2 + 1);
    transaction2.put(put2);
    Throwable thrown1 = catchThrowable(transaction1::commit);
    Throwable thrown2 = catchThrowable(transaction2::commit);
    // Assert
    assertThat(results1).isEmpty();
    assertThat(results2).isEmpty();
    GrpcTransaction transaction = manager.start();
    Optional<Result> result1 = transaction.get(prepareGet(0, 0, TABLE_1));
    Optional<Result> result2 = transaction.get(prepareGet(0, 1, TABLE_1));
    transaction.commit();
    assertThat(result1.isPresent()).isTrue();
    assertThat(getBalance(result1.get())).isEqualTo(1);
    assertThat(result2.isPresent()).isFalse();
    assertThat(thrown1).doesNotThrowAnyException();
    assertThat(thrown2).isInstanceOf(CommitConflictException.class);
}
Also used : 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) Test(org.junit.Test)

Example 50 with Put

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

the class DistributedTransactionServiceWithConsensusCommitWithExtraReadIntegrationTest method commit_WriteSkewOnNonExistingRecordsWithSerializableWithExtraRead_OneShouldCommitTheOtherShouldThrowCommitConflictException.

private void commit_WriteSkewOnNonExistingRecordsWithSerializableWithExtraRead_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()).isTrue();
    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)

Aggregations

Put (com.scalar.db.api.Put)374 Key (com.scalar.db.io.Key)216 Test (org.junit.jupiter.api.Test)209 Result (com.scalar.db.api.Result)108 Get (com.scalar.db.api.Get)93 Test (org.junit.Test)67 Delete (com.scalar.db.api.Delete)64 IntValue (com.scalar.db.io.IntValue)63 TextValue (com.scalar.db.io.TextValue)48 Scan (com.scalar.db.api.Scan)44 Value (com.scalar.db.io.Value)37 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)35 BooleanValue (com.scalar.db.io.BooleanValue)33 ConditionalExpression (com.scalar.db.api.ConditionalExpression)30 PutIfNotExists (com.scalar.db.api.PutIfNotExists)29 PutIf (com.scalar.db.api.PutIf)28 DoubleValue (com.scalar.db.io.DoubleValue)26 GrpcTransaction (com.scalar.db.transaction.rpc.GrpcTransaction)19 ExecutionException (com.scalar.db.exception.storage.ExecutionException)17 Mutation (com.scalar.db.api.Mutation)16