Search in sources :

Example 41 with GrpcTransaction

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method commit_DeleteGivenForNonExisting_ShouldThrowCommitException.

@Test
public void commit_DeleteGivenForNonExisting_ShouldThrowCommitException() throws TransactionException {
    // Arrange
    Get get = prepareGet(0, 0, TABLE_1);
    Delete delete = prepareDelete(0, 0, TABLE_1);
    GrpcTransaction transaction = manager.start();
    // Act Assert
    transaction.get(get);
    transaction.delete(delete);
    assertThatCode(transaction::commit).isInstanceOf(CommitException.class);
}
Also used : Delete(com.scalar.db.api.Delete) Get(com.scalar.db.api.Get) GrpcTransaction(com.scalar.db.transaction.rpc.GrpcTransaction) Test(org.junit.Test)

Example 42 with GrpcTransaction

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method commit_ConflictingPutsGivenForNonExisting_ShouldCommitOneAndAbortTheOther.

private void commit_ConflictingPutsGivenForNonExisting_ShouldCommitOneAndAbortTheOther(String table1, String table2) throws TransactionException {
    // Arrange
    boolean differentTables = !table1.equals(table2);
    int expected = INITIAL_BALANCE;
    List<Put> puts1 = preparePuts(table1);
    List<Put> puts2 = differentTables ? preparePuts(table2) : puts1;
    int from = 0;
    int to = NUM_TYPES;
    int anotherFrom = to;
    int anotherTo = NUM_TYPES * 2;
    puts1.get(from).withValue(BALANCE, expected);
    puts2.get(to).withValue(BALANCE, expected);
    // Act Assert
    GrpcTransaction transaction = manager.start();
    transaction.put(puts1.get(from));
    transaction.put(puts2.get(to));
    GrpcTransaction conflictingTransaction = manager.start();
    puts1.get(anotherTo).withValue(BALANCE, expected);
    assertThatCode(() -> {
        conflictingTransaction.put(puts2.get(anotherFrom));
        conflictingTransaction.put(puts1.get(anotherTo));
        conflictingTransaction.commit();
    }).doesNotThrowAnyException();
    assertThatThrownBy(transaction::commit).isInstanceOf(CommitException.class);
    // Assert
    List<Get> gets1 = prepareGets(table1);
    List<Get> gets2 = differentTables ? prepareGets(table2) : gets1;
    GrpcTransaction another = manager.start();
    assertThat(another.get(gets1.get(from)).isPresent()).isFalse();
    Optional<Result> toResult = another.get(gets2.get(to));
    assertThat(toResult).isPresent();
    assertThat(getBalance(toResult.get())).isEqualTo(expected);
    Optional<Result> anotherToResult = another.get(gets1.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)

Example 43 with GrpcTransaction

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

the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method commit_ConflictingPutsGivenForExisting_ShouldCommitOneAndAbortTheOther.

private void commit_ConflictingPutsGivenForExisting_ShouldCommitOneAndAbortTheOther(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 = to;
    int anotherTo = NUM_TYPES * 2;
    populateRecords(table1);
    if (differentTables) {
        populateRecords(table2);
    }
    // Act
    GrpcTransaction transaction = prepareTransfer(from, table1, to, table2, amount1);
    assertThatCode(() -> prepareTransfer(anotherFrom, table2, anotherTo, table1, amount2).commit()).doesNotThrowAnyException();
    assertThatThrownBy(transaction::commit).isInstanceOf(CommitException.class);
    // 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);
    Optional<Result> toResult = another.get(gets2.get(to));
    assertThat(toResult).isPresent();
    assertThat(getBalance(toResult.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 44 with GrpcTransaction

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

the class DistributedTransactionServiceWithConsensusCommitWithExtraReadIntegrationTest method commit_WriteSkewWithScanOnExistingRecordsWithSerializableWithExtraRead_ShouldThrowCommitConflictException.

@Test
public void commit_WriteSkewWithScanOnExistingRecordsWithSerializableWithExtraRead_ShouldThrowCommitConflictException() throws TransactionException {
    // Arrange
    List<Put> puts = Arrays.asList(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1), preparePut(0, 1, TABLE_1).withValue(BALANCE, 1));
    GrpcTransaction transaction = manager.start();
    transaction.put(puts);
    transaction.commit();
    // 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
    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();
    assertThat(getBalance(result1.get())).isEqualTo(3);
    assertThat(result2).isPresent();
    assertThat(getBalance(result2.get())).isEqualTo(1);
    assertThat(thrown1).doesNotThrowAnyException();
    assertThat(thrown2).isInstanceOf(CommitConflictException.class);
}
Also used : 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) Test(org.junit.Test)

Example 45 with GrpcTransaction

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

the class DistributedTransactionServiceWithConsensusCommitWithExtraWriteIntegrationTest method commit_WriteSkewWithScanOnNonExistingRecordsWithSerializableWithExtraWrite_ShouldThrowCommitConflictException.

@Test
public void commit_WriteSkewWithScanOnNonExistingRecordsWithSerializableWithExtraWrite_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()).isFalse();
    assertThat(result2.isPresent()).isFalse();
    assertThat(thrown1).isInstanceOf(CommitConflictException.class);
    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)

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