Search in sources :

Example 61 with ExecutionException

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;
}
Also used : Value(com.scalar.db.io.Value) ArrayList(java.util.ArrayList) ExecutionException(com.scalar.db.exception.storage.ExecutionException) HashSet(java.util.HashSet)

Example 62 with ExecutionException

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);
}
Also used : CommitException(com.scalar.db.exception.transaction.CommitException) TransactionState(com.scalar.db.api.TransactionState) ExecutionException(com.scalar.db.exception.storage.ExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) Test(org.junit.jupiter.api.Test)

Example 63 with ExecutionException

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);
}
Also used : ExecutionException(com.scalar.db.exception.storage.ExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) Test(org.junit.jupiter.api.Test)

Example 64 with ExecutionException

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);
}
Also used : ExecutionException(com.scalar.db.exception.storage.ExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) Test(org.junit.jupiter.api.Test)

Example 65 with ExecutionException

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);
}
Also used : Get(com.scalar.db.api.Get) ExecutionException(com.scalar.db.exception.storage.ExecutionException) Test(org.junit.jupiter.api.Test)

Aggregations

ExecutionException (com.scalar.db.exception.storage.ExecutionException)78 DynamoDbException (software.amazon.awssdk.services.dynamodb.model.DynamoDbException)23 RetriableExecutionException (com.scalar.db.exception.storage.RetriableExecutionException)18 ObjectNotFoundException (software.amazon.awssdk.services.applicationautoscaling.model.ObjectNotFoundException)18 ResourceNotFoundException (software.amazon.awssdk.services.dynamodb.model.ResourceNotFoundException)18 SQLException (java.sql.SQLException)14 ArrayList (java.util.ArrayList)14 Connection (java.sql.Connection)12 Test (org.junit.jupiter.api.Test)11 HashSet (java.util.HashSet)10 NoMutationException (com.scalar.db.exception.storage.NoMutationException)9 TableMetadata (com.scalar.db.api.TableMetadata)8 Value (com.scalar.db.io.Value)8 Put (com.scalar.db.api.Put)7 TransactionState (com.scalar.db.api.TransactionState)6 CrudException (com.scalar.db.exception.transaction.CrudException)6 HashMap (java.util.HashMap)6 AttributeValue (software.amazon.awssdk.services.dynamodb.model.AttributeValue)5 CosmosDatabase (com.azure.cosmos.CosmosDatabase)4 Get (com.scalar.db.api.Get)4