use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method get_GetGivenForNonExisting_ShouldReturnEmpty.
@Test
public void get_GetGivenForNonExisting_ShouldReturnEmpty() throws TransactionException {
// Arrange
populate(TABLE_1);
GrpcTwoPhaseCommitTransaction transaction = manager.start();
// Act
Optional<Result> result = transaction.get(prepareGet(0, 4, TABLE_1));
transaction.prepare();
transaction.commit();
// Assert
assertThat(result.isPresent()).isFalse();
}
use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method commit_ConflictingPutAndDeleteGivenForExisting_ShouldCommitPutAndAbortDelete.
@Test
public void commit_ConflictingPutAndDeleteGivenForExisting_ShouldCommitPutAndAbortDelete() throws TransactionException {
// Arrange
int amount = 200;
int fromId = 0;
int fromType = 0;
int toId = 1;
int toType = 0;
int anotherFromId = toId;
int anotherFromType = toType;
int anotherToId = 2;
int anotherToType = 0;
populate(TABLE_1);
populate(TABLE_2);
GrpcTwoPhaseCommitTransaction fromTx = manager.start();
GrpcTwoPhaseCommitTransaction toTx = manager.join(fromTx.getId());
fromTx.get(prepareGet(fromId, fromType, TABLE_1));
fromTx.delete(prepareDelete(fromId, fromType, TABLE_1));
toTx.get(prepareGet(toId, toType, TABLE_2));
toTx.delete(prepareDelete(toId, toType, TABLE_2));
// Act Assert
assertThatCode(() -> {
GrpcTwoPhaseCommitTransaction anotherFromTx = manager.start();
GrpcTwoPhaseCommitTransaction anotherToTx = manager.join(anotherFromTx.getId());
transfer(anotherFromId, anotherFromType, TABLE_2, anotherFromTx, anotherToId, anotherToType, TABLE_1, anotherToTx, amount);
anotherFromTx.prepare();
anotherToTx.prepare();
anotherFromTx.commit();
anotherToTx.commit();
}).doesNotThrowAnyException();
assertThatThrownBy(() -> {
fromTx.prepare();
toTx.prepare();
}).isInstanceOf(PreparationException.class);
fromTx.rollback();
toTx.rollback();
// Assert
GrpcTwoPhaseCommitTransaction another = manager.start();
Optional<Result> result = another.get(prepareGet(fromId, fromType, TABLE_1));
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE);
result = another.get(prepareGet(anotherFromId, anotherFromType, TABLE_2));
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE - amount);
result = another.get(prepareGet(anotherToId, anotherToType, TABLE_1));
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE + amount);
another.prepare();
another.commit();
}
use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method put_DeleteCalledBefore_ShouldThrowIllegalArgumentException.
@Test
public void put_DeleteCalledBefore_ShouldThrowIllegalArgumentException() throws TransactionException {
// Arrange
GrpcTwoPhaseCommitTransaction transaction = manager.start();
transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1));
transaction.prepare();
transaction.commit();
// Act
GrpcTwoPhaseCommitTransaction transaction1 = manager.start();
Get get = prepareGet(0, 0, TABLE_1);
transaction1.get(get);
transaction1.delete(prepareDelete(0, 0, TABLE_1));
Throwable thrown = catchThrowable(() -> transaction1.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 2)));
transaction1.rollback();
// Assert
assertThat(thrown).isInstanceOf(IllegalArgumentException.class);
}
use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method putAndCommit_PutGivenForExistingAndNeverRead_ShouldThrowPreparationException.
@Test
public void putAndCommit_PutGivenForExistingAndNeverRead_ShouldThrowPreparationException() throws TransactionException {
// Arrange
populate(TABLE_1);
GrpcTwoPhaseCommitTransaction transaction = manager.start();
transaction.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, 1100));
// Act Assert
assertThatThrownBy(transaction::prepare).isInstanceOf(PreparationException.class);
transaction.rollback();
// Assert
GrpcTwoPhaseCommitTransaction another = manager.start();
Optional<Result> result = another.get(prepareGet(0, 0, TABLE_1));
another.prepare();
another.commit();
assertThat(result).isPresent();
assertThat(getAccountId(result.get())).isEqualTo(0);
assertThat(getAccountType(result.get())).isEqualTo(0);
// a rolled back value
assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE);
}
use of com.scalar.db.transaction.rpc.GrpcTwoPhaseCommitTransaction in project scalardb by scalar-labs.
the class TwoPhaseCommitTransactionServiceWithTwoPhaseConsensusCommitIntegrationTest method commit_ConflictingPutsGivenForExisting_ShouldCommitOneAndAbortTheOther.
@Test
public void commit_ConflictingPutsGivenForExisting_ShouldCommitOneAndAbortTheOther() throws TransactionException {
// Arrange
int amount1 = 100;
int amount2 = 200;
int fromId = 0;
int fromType = 0;
int toId = 1;
int toType = 0;
int anotherFromId = toId;
int anotherFromType = toType;
int anotherToId = 2;
int anotherToType = 0;
populate(TABLE_1);
populate(TABLE_2);
GrpcTwoPhaseCommitTransaction fromTx = manager.start();
GrpcTwoPhaseCommitTransaction toTx = manager.join(fromTx.getId());
transfer(fromId, fromType, TABLE_1, fromTx, toId, toType, TABLE_2, toTx, amount1);
// Act Assert
assertThatCode(() -> {
GrpcTwoPhaseCommitTransaction anotherFromTx = manager.start();
GrpcTwoPhaseCommitTransaction anotherToTx = manager.join(anotherFromTx.getId());
transfer(anotherFromId, anotherFromType, TABLE_2, anotherFromTx, anotherToId, anotherToType, TABLE_1, anotherToTx, amount2);
anotherFromTx.prepare();
anotherToTx.prepare();
anotherFromTx.commit();
anotherToTx.commit();
}).doesNotThrowAnyException();
assertThatThrownBy(() -> {
fromTx.prepare();
toTx.prepare();
}).isInstanceOf(PreparationException.class);
fromTx.rollback();
toTx.rollback();
// Assert
GrpcTwoPhaseCommitTransaction another = manager.start();
Optional<Result> result = another.get(prepareGet(fromId, fromType, TABLE_1));
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE);
result = another.get(prepareGet(anotherFromId, anotherFromType, TABLE_2));
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE - amount2);
result = another.get(prepareGet(anotherToId, anotherToType, TABLE_1));
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(INITIAL_BALANCE + amount2);
another.prepare();
another.commit();
}
Aggregations