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);
}
}
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);
}
}
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);
}
}
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);
}
}
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();
}
Aggregations