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