Search in sources :

Example 6 with TableAlreadyExistsException

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);
    }
}
Also used : TableAlreadyExistsException(io.cdap.cdap.spi.data.TableAlreadyExistsException) ArrayList(java.util.ArrayList) SpannerException(com.google.cloud.spanner.SpannerException) IOException(java.io.IOException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 7 with TableAlreadyExistsException

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
    }
}
Also used : StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) TableAlreadyExistsException(io.cdap.cdap.spi.data.TableAlreadyExistsException) Test(org.junit.Test) IOException(java.io.IOException) Assert(org.junit.Assert) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) TableAlreadyExistsException(io.cdap.cdap.spi.data.TableAlreadyExistsException) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) Test(org.junit.Test)

Example 8 with TableAlreadyExistsException

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);
    }
}
Also used : TableAlreadyExistsException(io.cdap.cdap.spi.data.TableAlreadyExistsException) ArrayList(java.util.ArrayList) SpannerException(com.google.cloud.spanner.SpannerException) IOException(java.io.IOException) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with TableAlreadyExistsException

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);
    }
}
Also used : TableAlreadyExistsException(io.cdap.cdap.spi.data.TableAlreadyExistsException) SQLException(java.sql.SQLException) Statement(java.sql.Statement) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) Connection(java.sql.Connection) IOException(java.io.IOException)

Example 10 with TableAlreadyExistsException

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);
    }
}
Also used : TableAlreadyExistsException(io.cdap.cdap.spi.data.TableAlreadyExistsException) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) StructuredTableSpecification(io.cdap.cdap.spi.data.table.StructuredTableSpecification) StructuredTableRegistry(io.cdap.cdap.spi.data.common.StructuredTableRegistry)

Aggregations

TableAlreadyExistsException (io.cdap.cdap.spi.data.TableAlreadyExistsException)14 StructuredTableId (io.cdap.cdap.spi.data.table.StructuredTableId)10 IOException (java.io.IOException)6 SpannerException (com.google.cloud.spanner.SpannerException)2 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)2 DatasetAdmin (io.cdap.cdap.api.dataset.DatasetAdmin)2 MetricsTable (io.cdap.cdap.data2.dataset2.lib.table.MetricsTable)2 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)2 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)2 TableNotFoundException (io.cdap.cdap.spi.data.TableNotFoundException)2 StructuredTableRegistry (io.cdap.cdap.spi.data.common.StructuredTableRegistry)2 StructuredTableSpecification (io.cdap.cdap.spi.data.table.StructuredTableSpecification)2 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 ArrayList (java.util.ArrayList)2 ExecutionException (java.util.concurrent.ExecutionException)2 Assert (org.junit.Assert)2 Test (org.junit.Test)2