use of com.scalar.db.api.Result in project scalardb by scalar-labs.
the class TwoPhaseConsensusCommitIntegrationTest method commit_WriteSkewOnNonExistingRecordsWithSerializableWithExtraWriteAndCommitStatusFailed_ShouldRollbackProperly.
@Test
public void commit_WriteSkewOnNonExistingRecordsWithSerializableWithExtraWriteAndCommitStatusFailed_ShouldRollbackProperly() throws TransactionException, CoordinatorException {
// Arrange
State state = new State(ANY_ID_1, TransactionState.ABORTED);
coordinator.putState(state);
Isolation isolation = Isolation.SERIALIZABLE;
SerializableStrategy strategy = SerializableStrategy.EXTRA_WRITE;
// Act
TwoPhaseConsensusCommit txSub1 = manager.start(ANY_ID_1, isolation, strategy);
TwoPhaseConsensusCommit txSub2 = manager.join(txSub1.getId(), isolation, strategy);
Optional<Result> result = txSub2.get(prepareGet(1, 0, TABLE_2));
assertThat(result).isNotPresent();
result = txSub1.get(prepareGet(0, 0, TABLE_1));
assertThat(result).isNotPresent();
int current1 = 0;
txSub1.put(preparePut(0, 0, TABLE_1).withValue(BALANCE, current1 + 1));
assertThatThrownBy(() -> {
txSub1.prepare();
txSub2.prepare();
txSub1.commit();
txSub2.commit();
}).isInstanceOf(CommitException.class);
txSub1.rollback();
txSub2.rollback();
// Assert
TwoPhaseConsensusCommit transaction = manager.start();
result = transaction.get(prepareGet(0, 0, TABLE_1));
assertThat(result).isNotPresent();
result = transaction.get(prepareGet(1, 0, TABLE_2));
assertThat(result).isNotPresent();
}
use of com.scalar.db.api.Result in project scalardb by scalar-labs.
the class JdbcTransactionIntegrationTest method get_GetGivenForNonExisting_ShouldReturnEmpty.
@Test
public void get_GetGivenForNonExisting_ShouldReturnEmpty() throws TransactionException {
// Arrange
populateRecords();
JdbcTransaction transaction = manager.start();
Get get = prepareGet(0, 4, NAMESPACE, TABLE);
// Act
Optional<Result> result = transaction.get(get);
transaction.commit();
// Assert
assertThat(result.isPresent()).isFalse();
}
use of com.scalar.db.api.Result in project scalardb by scalar-labs.
the class ConsensusCommitIntegrationTestBase method putAndCommit_DeleteGivenInBetweenTransactions_ShouldProduceSerializableResults.
@Test
public void putAndCommit_DeleteGivenInBetweenTransactions_ShouldProduceSerializableResults() throws CrudException, CommitException, UnknownTransactionStatusException {
// Arrange
ConsensusCommit transaction = manager.start();
transaction.put(preparePut(0, 0, namespace1, TABLE_1).withValue(BALANCE, 2));
transaction.commit();
// Act
ConsensusCommit transaction1 = manager.start();
Optional<Result> result1 = transaction1.get(prepareGet(0, 0, namespace1, TABLE_1));
int balance1 = 0;
if (result1.isPresent()) {
balance1 = getBalance(result1.get());
}
transaction1.put(preparePut(0, 0, namespace1, TABLE_1).withValue(BALANCE, balance1 + 1));
ConsensusCommit transaction2 = manager.start();
transaction2.get(prepareGet(0, 0, namespace1, TABLE_1));
transaction2.delete(prepareDelete(0, 0, namespace1, TABLE_1));
transaction2.commit();
// the same transaction processing as transaction1
ConsensusCommit transaction3 = manager.start();
Optional<Result> result3 = transaction3.get(prepareGet(0, 0, namespace1, TABLE_1));
int balance3 = 0;
if (result3.isPresent()) {
balance3 = getBalance(result3.get());
}
transaction3.put(preparePut(0, 0, namespace1, TABLE_1).withValue(BALANCE, balance3 + 1));
transaction3.commit();
Throwable thrown = catchThrowable(transaction1::commit);
// Assert
assertThat(thrown).isInstanceOf(CommitConflictException.class).hasCauseInstanceOf(NoMutationException.class);
transaction = manager.start();
Optional<Result> result = transaction.get(prepareGet(0, 0, namespace1, TABLE_1));
assertThat(result).isPresent();
assertThat(getBalance(result.get())).isEqualTo(1);
}
use of com.scalar.db.api.Result in project scalardb by scalar-labs.
the class ConsensusCommitIntegrationTestBase method prepareTransfer.
private ConsensusCommit prepareTransfer(int fromId, String fromNamespace, String fromTable, int toId, String toNamespace, String toTable, int amount) throws CrudException {
boolean differentTables = toNamespace.equals(fromNamespace) || !toTable.equals(fromTable);
ConsensusCommit transaction = manager.start();
List<Get> fromGets = prepareGets(fromNamespace, fromTable);
List<Get> toGets = differentTables ? prepareGets(toNamespace, toTable) : fromGets;
Optional<Result> fromResult = transaction.get(fromGets.get(fromId));
assertThat(fromResult).isPresent();
IntValue fromBalance = new IntValue(BALANCE, getBalance(fromResult.get()) - amount);
Optional<Result> toResult = transaction.get(toGets.get(toId));
assertThat(toResult).isPresent();
IntValue toBalance = new IntValue(BALANCE, getBalance(toResult.get()) + amount);
List<Put> fromPuts = preparePuts(fromNamespace, fromTable);
List<Put> toPuts = differentTables ? preparePuts(toNamespace, toTable) : fromPuts;
fromPuts.get(fromId).withValue(fromBalance);
toPuts.get(toId).withValue(toBalance);
transaction.put(fromPuts.get(fromId));
transaction.put(toPuts.get(toId));
return transaction;
}
use of com.scalar.db.api.Result in project scalardb by scalar-labs.
the class ConsensusCommitIntegrationTestBase method get_CalledTwice_ShouldReturnFromSnapshotInSecondTime.
@Test
public void get_CalledTwice_ShouldReturnFromSnapshotInSecondTime() throws CrudException, ExecutionException, CommitException, UnknownTransactionStatusException {
// Arrange
populateRecords(namespace1, TABLE_1);
ConsensusCommit transaction = manager.start();
Get get = prepareGet(0, 0, namespace1, TABLE_1);
// Act
Optional<Result> result1 = transaction.get(get);
Optional<Result> result2 = transaction.get(get);
// Assert
verify(storage).get(any(Get.class));
assertThat(result1).isEqualTo(result2);
}
Aggregations