use of com.scalar.db.transaction.rpc.GrpcTransaction in project scalardb by scalar-labs.
the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method getState_forFailedTransaction_ShouldReturnAbortedState.
@Test
public void getState_forFailedTransaction_ShouldReturnAbortedState() throws TransactionException {
// Arrange
GrpcTransaction transaction1 = manager.start();
transaction1.get(prepareGet(0, 0, TABLE_1));
transaction1.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
GrpcTransaction transaction2 = manager.start();
transaction2.get(prepareGet(0, 0, TABLE_1));
transaction2.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
transaction2.commit();
assertThatCode(transaction1::commit).isInstanceOf(CommitException.class);
// Act
TransactionState state = manager.getState(transaction1.getId());
// Assert
assertThat(state).isEqualTo(TransactionState.ABORTED);
}
use of com.scalar.db.transaction.rpc.GrpcTransaction in project scalardb by scalar-labs.
the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method putAndCommit_DeleteGivenInBetweenTransactions_ShouldProduceSerializableResults.
@Test
public void putAndCommit_DeleteGivenInBetweenTransactions_ShouldProduceSerializableResults() throws TransactionException {
// Arrange
GrpcTransaction transaction = manager.start();
transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 2));
transaction.commit();
// Act
GrpcTransaction transaction1 = manager.start();
Optional<Result> result1 = transaction1.get(prepareGet(0, 0, TABLE_1));
int balance1 = 0;
if (result1.isPresent()) {
balance1 = getBalance(result1.get());
}
transaction1.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, balance1 + 1));
GrpcTransaction transaction2 = manager.start();
transaction2.get(prepareGet(0, 0, TABLE_1));
transaction2.delete(prepareDelete(0, 0, TABLE_1));
transaction2.commit();
// the same transaction processing as transaction1
GrpcTransaction transaction3 = manager.start();
Optional<Result> result3 = transaction3.get(prepareGet(0, 0, TABLE_1));
int balance3 = 0;
if (result3.isPresent()) {
balance3 = getBalance(result3.get());
}
transaction3.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, balance3 + 1));
transaction3.commit();
Throwable thrown = catchThrowable(transaction1::commit);
// Assert
assertThat(thrown).isInstanceOf(CommitConflictException.class);
transaction = manager.start();
Optional<Result> result = transaction.get(prepareGet(0, 0, TABLE_1));
transaction.commit();
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(1);
}
use of com.scalar.db.transaction.rpc.GrpcTransaction in project scalardb by scalar-labs.
the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method scan_OverlappingPutGivenBefore_ShouldThrowIllegalArgumentException.
@Test
public void scan_OverlappingPutGivenBefore_ShouldThrowIllegalArgumentException() throws TransactionException {
// Arrange
GrpcTransaction transaction = manager.start();
transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
// Act
Scan scan = prepareScan(0, 0, 0, TABLE_1);
Throwable thrown = catchThrowable(() -> transaction.scan(scan));
transaction.abort();
// Assert
assertThat(thrown).isInstanceOf(IllegalArgumentException.class);
}
use of com.scalar.db.transaction.rpc.GrpcTransaction in project scalardb by scalar-labs.
the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method populateRecords.
private void populateRecords(String table) 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 (TransactionException e) {
throw new RuntimeException(e);
}
}));
transaction.commit();
}
use of com.scalar.db.transaction.rpc.GrpcTransaction in project scalardb by scalar-labs.
the class DistributedTransactionServiceWithConsensusCommitIntegrationTest method get_PutCalledBefore_ShouldGet.
@Test
public void get_PutCalledBefore_ShouldGet() throws TransactionException {
// Arrange
GrpcTransaction transaction = manager.start();
// Act
transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
Get get = prepareGet(0, 0, TABLE_1);
Optional<Result> result = transaction.get(get);
assertThatCode(transaction::commit).doesNotThrowAnyException();
// Assert
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(1);
}
Aggregations