use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.
the class DistributedStorageSingleClusteringKeyScanIntegrationTestBase method prepareRecords.
private List<Value<?>> prepareRecords(DataType clusteringKeyType, Order clusteringOrder) throws ExecutionException {
RANDOM.setSeed(seed);
List<Value<?>> ret = new ArrayList<>();
List<Put> puts = new ArrayList<>();
if (clusteringKeyType == DataType.BOOLEAN) {
TestUtils.booleanValues(CLUSTERING_KEY).forEach(clusteringKeyValue -> {
ret.add(clusteringKeyValue);
puts.add(preparePut(clusteringKeyType, clusteringOrder, clusteringKeyValue));
});
} else {
Set<Value<?>> valueSet = new HashSet<>();
// Add min and max clustering key values
Arrays.asList(getMinValue(CLUSTERING_KEY, clusteringKeyType), getMaxValue(CLUSTERING_KEY, clusteringKeyType)).forEach(clusteringKeyValue -> {
valueSet.add(clusteringKeyValue);
ret.add(clusteringKeyValue);
puts.add(preparePut(clusteringKeyType, clusteringOrder, clusteringKeyValue));
});
IntStream.range(0, CLUSTERING_KEY_NUM - 2).forEach(i -> {
Value<?> clusteringKeyValue;
while (true) {
clusteringKeyValue = getRandomValue(RANDOM, CLUSTERING_KEY, clusteringKeyType);
// reject duplication
if (!valueSet.contains(clusteringKeyValue)) {
valueSet.add(clusteringKeyValue);
break;
}
}
ret.add(clusteringKeyValue);
puts.add(preparePut(clusteringKeyType, clusteringOrder, clusteringKeyValue));
});
}
try {
List<Put> buffer = new ArrayList<>();
for (Put put : puts) {
buffer.add(put);
if (buffer.size() == 20) {
storage.mutate(buffer);
buffer.clear();
}
}
if (!buffer.isEmpty()) {
storage.mutate(buffer);
}
} catch (ExecutionException e) {
throw new ExecutionException("put data to database failed", e);
}
ret.sort(clusteringOrder == Order.ASC ? com.google.common.collect.Ordering.natural() : com.google.common.collect.Ordering.natural().reverse());
return ret;
}
use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.
the class CommitHandlerTest method commit_ExceptionThrownInPrepareRecords_ShouldAbortAndRollbackRecords.
@Test
public void commit_ExceptionThrownInPrepareRecords_ShouldAbortAndRollbackRecords() throws ExecutionException, CoordinatorException {
// Arrange
Snapshot snapshot = prepareSnapshotWithDifferentPartitionPut();
ExecutionException toThrow = mock(ExecutionException.class);
doThrow(toThrow).when(storage).mutate(anyList());
doNothing().when(coordinator).putState(any(Coordinator.State.class));
doNothing().when(handler).rollbackRecords(any(Snapshot.class));
// Act
assertThatThrownBy(() -> handler.commit(snapshot)).isInstanceOf(CommitException.class).hasCause(toThrow);
// Assert
verify(coordinator).putState(new Coordinator.State(ANY_ID, TransactionState.ABORTED));
verify(coordinator, never()).putState(new Coordinator.State(ANY_ID, TransactionState.COMMITTED));
verify(handler).rollbackRecords(snapshot);
}
use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.
the class CommitHandlerTest method commit_ExceptionThrownInPrepareRecordsAndFailedInCoordinatorAbortThenNothingReturnedInGetState_ShouldThrowUnknown.
@Test
public void commit_ExceptionThrownInPrepareRecordsAndFailedInCoordinatorAbortThenNothingReturnedInGetState_ShouldThrowUnknown() throws ExecutionException, CoordinatorException {
// Arrange
Snapshot snapshot = prepareSnapshotWithDifferentPartitionPut();
ExecutionException toThrow1 = mock(ExecutionException.class);
CoordinatorException toThrow2 = mock(CoordinatorException.class);
doThrow(toThrow1).when(storage).mutate(anyList());
doThrow(toThrow2).when(coordinator).putState(new Coordinator.State(ANY_ID, TransactionState.ABORTED));
// assume that it will call Coordinator.getState() if Coordinator.putState() failed
doReturn(Optional.empty()).when(coordinator).getState(ANY_ID);
// Act
assertThatThrownBy(() -> handler.commit(snapshot)).isInstanceOf(UnknownTransactionStatusException.class);
// Assert
verify(coordinator).putState(new Coordinator.State(ANY_ID, TransactionState.ABORTED));
verify(coordinator, never()).putState(new Coordinator.State(ANY_ID, TransactionState.COMMITTED));
verify(coordinator).getState(ANY_ID);
verify(handler, never()).rollbackRecords(snapshot);
}
use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.
the class CommitHandlerTest method commit_ExceptionThrownInCommitRecords_ShouldBeIgnored.
@Test
public void commit_ExceptionThrownInCommitRecords_ShouldBeIgnored() throws ExecutionException, CommitException, UnknownTransactionStatusException, CoordinatorException {
// Arrange
Snapshot snapshot = prepareSnapshotWithDifferentPartitionPut();
ExecutionException toThrow = mock(ExecutionException.class);
doNothing().doNothing().doThrow(toThrow).when(storage).mutate(anyList());
doNothing().when(coordinator).putState(new Coordinator.State(ANY_ID, TransactionState.COMMITTED));
// Act
handler.commit(snapshot);
// Assert
verify(storage, times(3)).mutate(anyList());
verify(coordinator).putState(new Coordinator.State(ANY_ID, TransactionState.COMMITTED));
verify(coordinator, never()).putState(new Coordinator.State(ANY_ID, TransactionState.ABORTED));
verify(handler, never()).rollbackRecords(snapshot);
}
use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.
the class CoordinatorTest method getState_TransactionIdGivenAndExceptionThrownInGet_ShouldThrowCoordinatorException.
@Test
public void getState_TransactionIdGivenAndExceptionThrownInGet_ShouldThrowCoordinatorException() throws ExecutionException {
// Arrange
String id = ANY_ID_1;
ExecutionException toThrow = mock(ExecutionException.class);
when(storage.get(any(Get.class))).thenThrow(toThrow);
// Act Assert
assertThatThrownBy(() -> coordinator.getState(id)).isInstanceOf(CoordinatorException.class);
}
Aggregations