Search in sources :

Example 1 with TableAlreadyExistsException

use of io.cdap.cdap.spi.data.TableAlreadyExistsException in project cdap by caskdata.

the class SqlStructuredTableRegistry method registerSpecification.

@Override
public void registerSpecification(StructuredTableSpecification specification) throws TableAlreadyExistsException {
    initIfNeeded();
    TransactionRunners.run(transactionRunner, context -> {
        StructuredTable registry = context.getTable(REGISTRY);
        StructuredTableId tableId = specification.getTableId();
        Optional<StructuredRow> optional = registry.read(Collections.singleton(Fields.stringField(TABLE_NAME_FIELD, tableId.getName())));
        if (optional.isPresent()) {
            throw new TableAlreadyExistsException(tableId);
        }
        LOG.debug("Registering table specification {}", specification);
        registry.upsert(Arrays.asList(Fields.stringField(TABLE_NAME_FIELD, tableId.getName()), Fields.stringField(TABLE_SPEC_FIELD, GSON.toJson(specification))));
    }, TableAlreadyExistsException.class);
}
Also used : TableAlreadyExistsException(io.cdap.cdap.spi.data.TableAlreadyExistsException) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) StructuredRow(io.cdap.cdap.spi.data.StructuredRow)

Example 2 with TableAlreadyExistsException

use of io.cdap.cdap.spi.data.TableAlreadyExistsException in project cdap by caskdata.

the class NoSqlStructuredTableAdmin method create.

@Override
public void create(StructuredTableSpecification spec) throws IOException, TableAlreadyExistsException {
    if (exists(spec.getTableId())) {
        throw new TableAlreadyExistsException(spec.getTableId());
    }
    LOG.info("Creating table {} in namespace {}", spec, NamespaceId.SYSTEM);
    DatasetAdmin indexTableAdmin = indexTableDefinition.getAdmin(SYSTEM_CONTEXT, indexTableSpec, null);
    if (!indexTableAdmin.exists()) {
        LOG.info("Creating dataset indexed table {} in namespace {}", indexTableSpec.getName(), NamespaceId.SYSTEM);
        indexTableAdmin.create();
    }
    registry.registerSpecification(spec);
}
Also used : TableAlreadyExistsException(io.cdap.cdap.spi.data.TableAlreadyExistsException) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin)

Example 3 with TableAlreadyExistsException

use of io.cdap.cdap.spi.data.TableAlreadyExistsException in project cdap by caskdata.

the class NoSqlStructuredTableRegistry method registerSpecification.

@Override
public void registerSpecification(StructuredTableSpecification specification) throws TableAlreadyExistsException {
    LOG.debug("Registering table specification {}", specification);
    StructuredTableId tableId = specification.getTableId();
    MetricsTable table = getRegistryTable();
    try {
        byte[] rowKeyBytes = getRowKeyBytes(tableId);
        byte[] serialized = table.get(rowKeyBytes, SCHEMA_COL_BYTES);
        if (serialized != null) {
            throw new TableAlreadyExistsException(tableId);
        }
        serialized = Bytes.toBytes(GSON.toJson(specification));
        if (!table.swap(rowKeyBytes, SCHEMA_COL_BYTES, null, serialized)) {
            throw new TableAlreadyExistsException(tableId);
        }
    } finally {
        closeRegistryTable(table);
    }
}
Also used : TableAlreadyExistsException(io.cdap.cdap.spi.data.TableAlreadyExistsException) MetricsTable(io.cdap.cdap.data2.dataset2.lib.table.MetricsTable) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId)

Example 4 with TableAlreadyExistsException

use of io.cdap.cdap.spi.data.TableAlreadyExistsException in project cdap by caskdata.

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 5 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)

Aggregations

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