use of io.cdap.cdap.spi.data.TableAlreadyExistsException in project cdap by cdapio.
the class SpannerStructuredTableAdmin method create.
@Override
public void create(StructuredTableSpecification spec) throws IOException, TableAlreadyExistsException {
List<String> statements = new ArrayList<>();
statements.add(getCreateTableStatement(spec));
for (String idxColumn : spec.getIndexes()) {
String createIndex = String.format("CREATE INDEX %s ON %s (%s)", escapeName(getIndexName(spec.getTableId(), idxColumn)), escapeName(spec.getTableId().getName()), escapeName(idxColumn));
// Need to store all the non-primary keys and non index fields so that it can be queried
Set<String> storingFields = spec.getFieldTypes().stream().map(FieldType::getName).collect(Collectors.toSet());
storingFields.remove(idxColumn);
spec.getPrimaryKeys().forEach(storingFields::remove);
if (!storingFields.isEmpty()) {
createIndex += " STORING (" + String.join(",", storingFields) + ")";
}
statements.add(createIndex);
}
try {
Uninterruptibles.getUninterruptibly(adminClient.updateDatabaseDdl(databaseId.getInstanceId().getInstance(), databaseId.getDatabase(), statements, null));
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof SpannerException && ((SpannerException) cause).getErrorCode() == ErrorCode.FAILED_PRECONDITION) {
throw new TableAlreadyExistsException(spec.getTableId());
}
throw new IOException("Failed to create table in Spanner", cause);
}
}
use of io.cdap.cdap.spi.data.TableAlreadyExistsException in project cdap by cdapio.
the class TransactionRunnersTest method testTwoExceptionCheckedPropagation.
@Test
public void testTwoExceptionCheckedPropagation() throws Exception {
try {
TransactionRunners.run(MOCK, (TxRunnable) context -> {
throw new TableAlreadyExistsException(new StructuredTableId("id"));
}, TableNotFoundException.class, TableAlreadyExistsException.class);
Assert.fail("runnable should have thrown an exception");
} catch (TableAlreadyExistsException e) {
// expected
}
try {
TransactionRunners.run(MOCK, (TxCallable<Object>) context -> {
throw new TableAlreadyExistsException(new StructuredTableId("id"));
}, TableNotFoundException.class, TableAlreadyExistsException.class);
Assert.fail("runnable should have thrown an exception");
} catch (TableAlreadyExistsException e) {
// expected
}
}
use of io.cdap.cdap.spi.data.TableAlreadyExistsException in project cdap by caskdata.
the class SpannerStructuredTableAdmin method create.
@Override
public void create(StructuredTableSpecification spec) throws IOException, TableAlreadyExistsException {
List<String> statements = new ArrayList<>();
statements.add(getCreateTableStatement(spec));
for (String idxColumn : spec.getIndexes()) {
String createIndex = String.format("CREATE INDEX %s ON %s (%s)", escapeName(getIndexName(spec.getTableId(), idxColumn)), escapeName(spec.getTableId().getName()), escapeName(idxColumn));
// Need to store all the non-primary keys and non index fields so that it can be queried
Set<String> storingFields = spec.getFieldTypes().stream().map(FieldType::getName).collect(Collectors.toSet());
storingFields.remove(idxColumn);
spec.getPrimaryKeys().forEach(storingFields::remove);
if (!storingFields.isEmpty()) {
createIndex += " STORING (" + String.join(",", storingFields) + ")";
}
statements.add(createIndex);
}
try {
Uninterruptibles.getUninterruptibly(adminClient.updateDatabaseDdl(databaseId.getInstanceId().getInstance(), databaseId.getDatabase(), statements, null));
} catch (ExecutionException e) {
Throwable cause = e.getCause();
if (cause instanceof SpannerException && ((SpannerException) cause).getErrorCode() == ErrorCode.FAILED_PRECONDITION) {
throw new TableAlreadyExistsException(spec.getTableId());
}
throw new IOException("Failed to create table in Spanner", cause);
}
}
use of io.cdap.cdap.spi.data.TableAlreadyExistsException in project cdap by caskdata.
the class PostgreSqlStructuredTableAdmin method create.
@Override
public void create(StructuredTableSpecification spec) throws IOException, TableAlreadyExistsException {
StructuredTableId tableId = spec.getTableId();
try (Connection connection = dataSource.getConnection()) {
// table existence, index existence and then register for the table.
if (registry.getSpecification(spec.getTableId()) != null) {
throw new TableAlreadyExistsException(spec.getTableId());
}
try (Statement statement = connection.createStatement()) {
if (!tableExistsInternal(connection, tableId)) {
// Create table
LOG.info("Creating table {}", spec);
statement.execute(getCreateStatement(spec));
}
// Create indexes
for (String indexStatement : getCreateIndexStatements(tableId, getNonExistIndexes(connection, spec))) {
LOG.debug("Create index statement: {}", indexStatement);
statement.execute(indexStatement);
}
registry.registerSpecification(spec);
}
} catch (SQLException e) {
throw new IOException(String.format("Error creating table %s", tableId), e);
}
}
use of io.cdap.cdap.spi.data.TableAlreadyExistsException in project cdap by caskdata.
the class SqlStructuredTableRegistry method createRegistryTable.
private void createRegistryTable() throws IOException {
// Use a no-op registry to create PostgresSqlStructuredTableAdmin.
// During a table creation, except for registerSpecification and getSpecification, no other methods on the
// registry will be called by PostgresSqlStructuredTableAdmin. Since we always check existence before we create
// the registry table, we can safely return null for the getSpecification method.
StructuredTableRegistry noOpRegistry = new StructuredTableRegistry() {
final UnsupportedOperationException exception = new UnsupportedOperationException("Not expected to be called during creation of registry!");
@Override
public void registerSpecification(StructuredTableSpecification specification) {
// Do nothing
}
@Nullable
@Override
public StructuredTableSpecification getSpecification(StructuredTableId tableId) {
return null;
}
@Override
public void removeSpecification(StructuredTableId tableId) {
throw exception;
}
@Override
public boolean isEmpty() {
throw exception;
}
};
try {
// Create the table if needed
PostgreSqlStructuredTableAdmin admin = new PostgreSqlStructuredTableAdmin(noOpRegistry, dataSource);
if (!admin.tableExists(REGISTRY)) {
LOG.info("Creating SQL table {}", REGISTRY);
admin.create(SPEC);
}
} catch (TableAlreadyExistsException e) {
// Looks like the table was created concurrently by some other process
LOG.debug(String.format("Got exception when trying to create table %s", REGISTRY), e);
}
}
Aggregations