use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method getState_forSuccessfulTransaction_ShouldReturnCommittedState.
@Test
public void getState_forSuccessfulTransaction_ShouldReturnCommittedState() throws TransactionException {
// Arrange
GrpcTwoPhaseCommitTransaction transaction = manager.start();
transaction.get(prepareGet(0, 0, TABLE_1));
transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
transaction.prepare();
transaction.commit();
// Act
TransactionState state = manager.getState(transaction.getId());
// Assert
assertThat(state).isEqualTo(TransactionState.COMMITTED);
}
use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method getAndScan_CommitHappenedInBetween_ShouldReadRepeatably.
@Test
public void getAndScan_CommitHappenedInBetween_ShouldReadRepeatably() throws TransactionException {
// Arrange
GrpcTwoPhaseCommitTransaction transaction = manager.start();
transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
transaction.prepare();
transaction.commit();
GrpcTwoPhaseCommitTransaction transaction1 = manager.start();
Optional<Result> result1 = transaction1.get(prepareGet(0, 0, TABLE_1));
GrpcTwoPhaseCommitTransaction transaction2 = manager.start();
transaction2.get(prepareGet(0, 0, TABLE_1));
transaction2.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 2));
transaction2.prepare();
transaction2.commit();
// Act
Result result2 = transaction1.scan(prepareScan(0, 0, 0, TABLE_1)).get(0);
Optional<Result> result3 = transaction1.get(prepareGet(0, 0, TABLE_1));
transaction1.prepare();
transaction1.commit();
// Assert
assertThat(result1).isPresent();
assertThat(result1.get()).isEqualTo(result2);
assertThat(result1).isEqualTo(result3);
}
use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method putAndCommit_DeleteGivenInBetweenTransactions_ShouldProduceSerializableResults.
@Test
public void putAndCommit_DeleteGivenInBetweenTransactions_ShouldProduceSerializableResults() throws TransactionException {
// Arrange
GrpcTwoPhaseCommitTransaction transaction = manager.start();
transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 2));
transaction.prepare();
transaction.commit();
// Act
GrpcTwoPhaseCommitTransaction transaction1 = manager.start();
Optional<Result> result = transaction1.get(prepareGet(0, 0, TABLE_1));
assertThat(result).isPresent();
int balance1 = getBalance(result.get());
transaction1.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, balance1 + 1));
GrpcTwoPhaseCommitTransaction transaction2 = manager.start();
transaction2.get(prepareGet(0, 0, TABLE_1));
transaction2.delete(prepareDelete(0, 0, TABLE_1));
transaction2.prepare();
transaction2.commit();
// the same transaction processing as transaction1
GrpcTwoPhaseCommitTransaction transaction3 = manager.start();
result = transaction3.get(prepareGet(0, 0, TABLE_1));
assertThat(result).isNotPresent();
int balance3 = 0;
transaction3.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, balance3 + 1));
transaction3.prepare();
transaction3.commit();
assertThatThrownBy(transaction1::prepare).isInstanceOf(PreparationException.class);
transaction1.rollback();
// Assert
transaction = manager.start();
result = transaction.get(prepareGet(0, 0, TABLE_1));
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(1);
transaction.prepare();
transaction.commit();
}
use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method commit_NonConflictingDeletesGivenForExisting_ShouldCommitBoth.
@Test
public void commit_NonConflictingDeletesGivenForExisting_ShouldCommitBoth() throws TransactionException {
// Arrange
int account1Id = 0;
int account1Type = 0;
int account2Id = 1;
int account2Type = 0;
int account3Id = 2;
int account3Type = 0;
int account4Id = 3;
int account4Type = 0;
populate(TABLE_1);
populate(TABLE_2);
GrpcTwoPhaseCommitTransaction tx1 = manager.start();
GrpcTwoPhaseCommitTransaction tx2 = manager.join(tx1.getId());
deletes(account1Id, account1Type, TABLE_1, tx1, account2Id, account2Type, TABLE_2, tx2);
// Act Assert
assertThatCode(() -> {
GrpcTwoPhaseCommitTransaction tx3 = manager.start();
GrpcTwoPhaseCommitTransaction tx4 = manager.join(tx3.getId());
deletes(account3Id, account3Type, TABLE_2, tx3, account4Id, account4Type, TABLE_1, tx4);
tx3.prepare();
tx4.prepare();
tx3.commit();
tx4.commit();
}).doesNotThrowAnyException();
assertThatCode(() -> {
tx1.prepare();
tx2.prepare();
tx1.commit();
tx2.commit();
}).doesNotThrowAnyException();
// Assert
GrpcTwoPhaseCommitTransaction another = manager.start();
Optional<Result> result = another.get(prepareGet(account1Id, account1Type, TABLE_1));
assertThat(result).isNotPresent();
result = another.get(prepareGet(account2Id, account2Type, TABLE_2));
assertThat(result).isNotPresent();
result = another.get(prepareGet(account3Id, account3Type, TABLE_2));
assertThat(result).isNotPresent();
result = another.get(prepareGet(account4Id, account4Type, TABLE_1));
assertThat(result).isNotPresent();
another.prepare();
another.commit();
}
use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitWithExtraReadIntegrationTest method commit_WriteSkewWithScanOnNonExistingRecordsWithSerializableWithExtraRead_ShouldThrowValidationException.
@Test
public void commit_WriteSkewWithScanOnNonExistingRecordsWithSerializableWithExtraRead_ShouldThrowValidationException() 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));
assertThatCode(() -> {
transaction1.prepare();
transaction1.validate();
transaction1.commit();
}).doesNotThrowAnyException();
transaction2.prepare();
assertThatThrownBy(transaction2::validate).isInstanceOf(ValidationException.class);
transaction2.rollback();
// Assert
GrpcTwoPhaseCommitTransaction transaction = manager.start();
Optional<Result> result = transaction.get(prepareGet(0, 0, TABLE_1));
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(count1 + 1);
result = transaction.get(prepareGet(0, 1, TABLE_1));
assertThat(result).isNotPresent();
transaction.prepare();
transaction.validate();
transaction.commit();
}
Aggregations