use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitWithExtraWriteIntegrationTest method commit_WriteSkewWithScanOnNonExistingRecordsWithSerializableWithExtraWrite_ShouldThrowPreparationException.
@Test
public void commit_WriteSkewWithScanOnNonExistingRecordsWithSerializableWithExtraWrite_ShouldThrowPreparationException() throws TransactionException {
// Arrange
// Act Assert
GrpcTwoPhaseCommitTransaction transaction1 = manager.start();
List<Result> results = transaction1.scan(prepareScan(0, 0, 1, TABLE_1));
assertThat(results).isEmpty();
int count1 = 0;
transaction1.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, count1 + 1));
GrpcTwoPhaseCommitTransaction transaction2 = manager.start();
results = transaction2.scan(prepareScan(0, 0, 1, TABLE_1));
assertThat(results).isEmpty();
int count2 = 0;
transaction2.put(preparePut(0, 1, TABLE_1).withValue(BALANCE, count2 + 1));
assertThatThrownBy(transaction1::prepare).isInstanceOf(PreparationException.class);
transaction1.rollback();
assertThatThrownBy(transaction2::prepare).isInstanceOf(PreparationException.class);
transaction2.rollback();
// Assert
GrpcTwoPhaseCommitTransaction transaction = manager.start();
Optional<Result> result = transaction.get(prepareGet(0, 0, TABLE_1));
assertThat(result).isNotPresent();
result = transaction.get(prepareGet(0, 1, TABLE_1));
assertThat(result).isNotPresent();
transaction.prepare();
transaction.commit();
}
use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitWithExtraWriteIntegrationTest method commit_WriteSkewOnNonExistingRecordsWithSerializableWithExtraWrite_OneShouldCommitTheOtherShouldThrowPreparationException.
@Test
public void commit_WriteSkewOnNonExistingRecordsWithSerializableWithExtraWrite_OneShouldCommitTheOtherShouldThrowPreparationException() throws TransactionException {
// Arrange
// Act Assert
GrpcTwoPhaseCommitTransaction tx1Sub1 = manager.start();
GrpcTwoPhaseCommitTransaction tx1Sub2 = manager.join(tx1Sub1.getId());
Optional<Result> result = tx1Sub2.get(prepareGet(1, 0, TABLE_2));
assertThat(result).isNotPresent();
int current1 = 0;
tx1Sub1.get(prepareGet(0, 0, TABLE_1));
tx1Sub1.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, current1 + 1));
GrpcTwoPhaseCommitTransaction tx2Sub1 = manager.start();
GrpcTwoPhaseCommitTransaction tx2Sub2 = manager.join(tx2Sub1.getId());
result = tx2Sub1.get(prepareGet(0, 0, TABLE_1));
assertThat(result).isNotPresent();
int current2 = 0;
tx2Sub2.get(prepareGet(1, 0, TABLE_2));
tx2Sub2.put(preparePut(1, 0, TABLE_2).withValue(BALANCE, current2 + 1));
tx1Sub1.prepare();
tx1Sub2.prepare();
tx1Sub1.commit();
tx1Sub2.commit();
assertThatThrownBy(() -> {
tx2Sub1.prepare();
tx2Sub2.prepare();
}).isInstanceOf(PreparationException.class);
tx2Sub1.rollback();
tx2Sub2.rollback();
// Assert
GrpcTwoPhaseCommitTransaction transaction = manager.start();
result = transaction.get(prepareGet(0, 0, TABLE_1));
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(current1 + 1);
result = transaction.get(prepareGet(1, 0, TABLE_2));
assertThat(result).isNotPresent();
transaction.prepare();
transaction.commit();
}
use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitWithExtraWriteIntegrationTest method commit_WriteSkewOnExistingRecordsWithSerializableWithExtraWrite_OneShouldCommitTheOtherShouldThrowPreparationException.
@Test
public void commit_WriteSkewOnExistingRecordsWithSerializableWithExtraWrite_OneShouldCommitTheOtherShouldThrowPreparationException() throws TransactionException {
// Arrange
GrpcTwoPhaseCommitTransaction transaction = manager.start();
transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
transaction.put(preparePut(1, 0, TABLE_2).withValue(BALANCE, 1));
transaction.prepare();
transaction.commit();
// Act Assert
GrpcTwoPhaseCommitTransaction tx1Sub1 = manager.start();
GrpcTwoPhaseCommitTransaction tx1Sub2 = manager.join(tx1Sub1.getId());
Optional<Result> result = tx1Sub2.get(prepareGet(1, 0, TABLE_2));
assertThat(result).isPresent();
int current1 = getBalance(result.get());
tx1Sub1.get(prepareGet(0, 0, TABLE_1));
tx1Sub1.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, current1 + 1));
GrpcTwoPhaseCommitTransaction tx2Sub1 = manager.start();
GrpcTwoPhaseCommitTransaction tx2Sub2 = manager.join(tx2Sub1.getId());
result = tx2Sub1.get(prepareGet(0, 0, TABLE_1));
assertThat(result).isPresent();
int current2 = getBalance(result.get());
tx2Sub2.get(prepareGet(1, 0, TABLE_2));
tx2Sub2.put(preparePut(1, 0, TABLE_2).withValue(BALANCE, current2 + 1));
tx1Sub1.prepare();
tx1Sub2.prepare();
tx1Sub1.commit();
tx1Sub2.commit();
assertThatThrownBy(() -> {
tx2Sub1.prepare();
tx2Sub2.prepare();
}).isInstanceOf(PreparationException.class);
tx2Sub1.rollback();
tx2Sub2.rollback();
// Assert
transaction = manager.start();
result = transaction.get(prepareGet(0, 0, TABLE_1));
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(current1 + 1);
result = transaction.get(prepareGet(1, 0, TABLE_2));
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(current2);
transaction.prepare();
transaction.commit();
}
use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method abort_forOngoingTransaction_ShouldAbortCorrectly.
@Test
public void abort_forOngoingTransaction_ShouldAbortCorrectly() throws TransactionException {
// Arrange
GrpcTwoPhaseCommitTransaction transaction = manager.start();
transaction.get(prepareGet(0, 0, TABLE_1));
transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
// Act
manager.abort(transaction.getId());
transaction.prepare();
assertThatCode(transaction::commit).isInstanceOf(CommitException.class);
transaction.rollback();
// Assert
TransactionState state = manager.getState(transaction.getId());
assertThat(state).isEqualTo(TransactionState.ABORTED);
}
use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method scan_NonOverlappingPutGivenBefore_ShouldScan.
@Test
public void scan_NonOverlappingPutGivenBefore_ShouldScan() throws TransactionException {
// Arrange
GrpcTwoPhaseCommitTransaction transaction = manager.start();
transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
// Act Assert
assertThatCode(() -> transaction.scan(prepareScan(0, 1, 1, TABLE_1))).doesNotThrowAnyException();
transaction.prepare();
transaction.commit();
}
Aggregations