Search in sources :

Example 26 with ExecutionException

use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.

the class CassandraAdmin method createNamespace.

@Override
public void createNamespace(String namespace, Map<String, String> options) throws ExecutionException {
    CreateKeyspace query = SchemaBuilder.createKeyspace(quoteIfNecessary(namespace));
    String replicationFactor = options.getOrDefault(REPLICATION_FACTOR, "1");
    ReplicationStrategy replicationStrategy = options.containsKey(REPLICATION_STRATEGY) ? ReplicationStrategy.fromString(options.get(REPLICATION_STRATEGY)) : ReplicationStrategy.SIMPLE_STRATEGY;
    Map<String, Object> replicationOptions = new LinkedHashMap<>();
    if (replicationStrategy == ReplicationStrategy.SIMPLE_STRATEGY) {
        replicationOptions.put("class", ReplicationStrategy.SIMPLE_STRATEGY.toString());
        replicationOptions.put("replication_factor", replicationFactor);
    } else if (replicationStrategy == ReplicationStrategy.NETWORK_TOPOLOGY_STRATEGY) {
        replicationOptions.put("class", ReplicationStrategy.NETWORK_TOPOLOGY_STRATEGY.toString());
        replicationOptions.put("dc1", replicationFactor);
    }
    try {
        clusterManager.getSession().execute(query.with().replication(replicationOptions).getQueryString());
    } catch (RuntimeException e) {
        throw new ExecutionException(String.format("creating the keyspace %s failed", namespace), e);
    }
}
Also used : CreateKeyspace(com.datastax.driver.core.schemabuilder.CreateKeyspace) ExecutionException(com.scalar.db.exception.storage.ExecutionException) LinkedHashMap(java.util.LinkedHashMap)

Example 27 with ExecutionException

use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.

the class CassandraAdmin method dropIndex.

@Override
public void dropIndex(String namespace, String table, String columnName) throws ExecutionException {
    String indexName = getIndexName(table, columnName);
    SchemaStatement dropIndex = SchemaBuilder.dropIndex(quoteIfNecessary(namespace), indexName);
    try {
        clusterManager.getSession().execute(dropIndex.getQueryString());
    } catch (RuntimeException e) {
        throw new ExecutionException(String.format("dropping the secondary index for %s.%s.%s failed", namespace, table, columnName), e);
    }
}
Also used : SchemaStatement(com.datastax.driver.core.schemabuilder.SchemaStatement) ExecutionException(com.scalar.db.exception.storage.ExecutionException)

Example 28 with ExecutionException

use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.

the class CassandraAdmin method createIndex.

@Override
public void createIndex(String namespace, String table, String columnName, Map<String, String> options) throws ExecutionException {
    String indexName = getIndexName(table, columnName);
    SchemaStatement createIndex = SchemaBuilder.createIndex(indexName).onTable(quoteIfNecessary(namespace), quoteIfNecessary(table)).andColumn(quoteIfNecessary(columnName));
    try {
        clusterManager.getSession().execute(createIndex.getQueryString());
    } catch (RuntimeException e) {
        throw new ExecutionException(String.format("creating the secondary index for %s.%s.%s failed", namespace, table, columnName), e);
    }
}
Also used : SchemaStatement(com.datastax.driver.core.schemabuilder.SchemaStatement) ExecutionException(com.scalar.db.exception.storage.ExecutionException)

Example 29 with ExecutionException

use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.

the class MutateStatementHandler method handle.

/**
 * Executes the specified {@link Mutation} {@link Operation}
 *
 * @param operation {@link Mutation} operation
 * @return a {@code ResultSet}
 * @throws RetriableExecutionException if the execution failed, but it can be retriable
 * @throws ReadRepairableExecutionException if the execution partially failed, which can be
 *     repaired by a following read
 */
@Override
@Nonnull
public ResultSet handle(Operation operation) throws ExecutionException {
    try {
        ResultSet results = handleInternal(operation);
        Mutation mutation = (Mutation) operation;
        if (mutation.getCondition().isPresent() && !results.one().getBool(0)) {
            throw new NoMutationException("no mutation was applied.");
        }
        return results;
    } catch (WriteTimeoutException e) {
        LOGGER.warn("write timeout happened during mutate operation.", e);
        if (e.getWriteType() == WriteType.CAS) {
            // retry needs to be done if applications need to do the operation exactly
            throw new RetriableExecutionException("paxos phase in CAS operation failed.", e);
        } else if (e.getWriteType() == WriteType.SIMPLE) {
            Mutation mutation = (Mutation) operation;
            if (mutation.getCondition().isPresent()) {
                // learn phase needs to be repaired (by re-reading)
                throw new ReadRepairableExecutionException("learn phase in CAS operation failed.", e);
            } else {
                // retry needs to be done if applications need to do the operation exactly
                throw new RetriableExecutionException("simple write operation failed.", e);
            }
        } else {
            throw new ExecutionException("something wrong because it is neither CAS nor SIMPLE", e);
        }
    } catch (RuntimeException e) {
        LOGGER.warn(e.getMessage(), e);
        throw new RetriableExecutionException(e.getMessage(), e);
    }
}
Also used : WriteTimeoutException(com.datastax.driver.core.exceptions.WriteTimeoutException) ResultSet(com.datastax.driver.core.ResultSet) Mutation(com.scalar.db.api.Mutation) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) ExecutionException(com.scalar.db.exception.storage.ExecutionException) RetriableExecutionException(com.scalar.db.exception.storage.RetriableExecutionException) NoMutationException(com.scalar.db.exception.storage.NoMutationException) Nonnull(javax.annotation.Nonnull)

Example 30 with ExecutionException

use of com.scalar.db.exception.storage.ExecutionException in project scalardb by scalar-labs.

the class JdbcAdmin method getTableMetadata.

@Override
public TableMetadata getTableMetadata(String namespace, String table) throws ExecutionException {
    TableMetadata.Builder builder = TableMetadata.newBuilder();
    boolean tableExists = false;
    try (Connection connection = dataSource.getConnection();
        PreparedStatement preparedStatement = connection.prepareStatement(getSelectColumnsStatement())) {
        preparedStatement.setString(1, getFullTableName(namespace, table));
        try (ResultSet resultSet = preparedStatement.executeQuery()) {
            while (resultSet.next()) {
                tableExists = true;
                String columnName = resultSet.getString(METADATA_COL_COLUMN_NAME);
                DataType dataType = DataType.valueOf(resultSet.getString(METADATA_COL_DATA_TYPE));
                builder.addColumn(columnName, dataType);
                boolean indexed = resultSet.getBoolean(METADATA_COL_INDEXED);
                if (indexed) {
                    builder.addSecondaryIndex(columnName);
                }
                String keyType = resultSet.getString(METADATA_COL_KEY_TYPE);
                if (keyType == null) {
                    continue;
                }
                switch(KeyType.valueOf(keyType)) {
                    case PARTITION:
                        builder.addPartitionKey(columnName);
                        break;
                    case CLUSTERING:
                        Scan.Ordering.Order clusteringOrder = Scan.Ordering.Order.valueOf(resultSet.getString(METADATA_COL_CLUSTERING_ORDER));
                        builder.addClusteringKey(columnName, clusteringOrder);
                        break;
                    default:
                        throw new AssertionError("invalid key type: " + keyType);
                }
            }
        }
    } catch (SQLException e) {
        throw new ExecutionException("getting a table metadata failed", e);
    }
    if (!tableExists) {
        return null;
    }
    return builder.build();
}
Also used : TableMetadata(com.scalar.db.api.TableMetadata) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) Ordering(com.scalar.db.api.Scan.Ordering) DataType(com.scalar.db.io.DataType) Order(com.scalar.db.api.Scan.Ordering.Order) ExecutionException(com.scalar.db.exception.storage.ExecutionException)

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