Search in sources :

Example 1 with TableNotFoundException

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

the class TransactionRunnersTest method testSingleExceptionCheckedPropagation.

@Test
public void testSingleExceptionCheckedPropagation() {
    try {
        TransactionRunners.run(MOCK, (TxRunnable) context -> {
            throw new TableNotFoundException(new StructuredTableId("id"));
        }, TableNotFoundException.class);
        Assert.fail("runnable should have thrown an exception");
    } catch (TableNotFoundException e) {
    // expected
    }
    try {
        TransactionRunners.run(MOCK, (TxCallable<Object>) context -> {
            throw new TableNotFoundException(new StructuredTableId("id"));
        }, TableNotFoundException.class);
        Assert.fail("runnable should have thrown an exception");
    } catch (TableNotFoundException 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) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) StructuredTableId(io.cdap.cdap.spi.data.table.StructuredTableId) Test(org.junit.Test)

Example 2 with TableNotFoundException

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

the class OAuthStore method writeProvider.

/**
 * Create/update an OAuth provider.
 *
 * @param oauthProvider {@link OAuthProvider} to write
 * @throws OAuthStoreException if the write fails
 */
public void writeProvider(OAuthProvider oauthProvider) throws OAuthStoreException {
    try {
        TransactionRunners.run(transactionRunner, context -> {
            StructuredTable table = context.getTable(TABLE_ID);
            table.upsert(getRow(oauthProvider));
        }, TableNotFoundException.class, InvalidFieldException.class);
    } catch (TableNotFoundException e) {
        throw new OAuthStoreException("OAuth provider table not found", e);
    } catch (InvalidFieldException e) {
        throw new OAuthStoreException("OAuth provider object fields do not match table", e);
    }
    String namespace = NamespaceId.SYSTEM.getNamespace();
    try {
        secureStoreManager.put(namespace, getClientCredsKey(oauthProvider.getName()), GSON.toJson(oauthProvider.getClientCredentials()), "OAuth client creds", Collections.emptyMap());
    } catch (IOException e) {
        throw new OAuthStoreException("Failed to write to OAuth provider secure storage", e);
    } catch (Exception e) {
        throw new OAuthStoreException("Namespace \"" + namespace + "\" does not exist", e);
    }
}
Also used : TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) IOException(java.io.IOException) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException)

Example 3 with TableNotFoundException

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

the class OAuthStore method getProvider.

/**
 * Get an OAuth provider.
 *
 * @param name name of {@link OAuthProvider} to read
 * @throws OAuthStoreException if the read fails
 */
public Optional<OAuthProvider> getProvider(String name) throws OAuthStoreException {
    OAuthClientCredentials clientCreds;
    try {
        String clientCredsJson = new String(secureStore.get(NamespaceId.SYSTEM.getNamespace(), getClientCredsKey(name)).get(), StandardCharsets.UTF_8);
        clientCreds = GSON.fromJson(clientCredsJson, OAuthClientCredentials.class);
    } catch (IOException e) {
        throw new OAuthStoreException("Failed to read from OAuth provider secure storage", e);
    } catch (Exception e) {
        return Optional.empty();
    }
    try {
        return TransactionRunners.run(transactionRunner, context -> {
            StructuredTable table = context.getTable(TABLE_ID);
            Optional<StructuredRow> row = table.read(getKey(name));
            return row.map(structuredRow -> fromRow(structuredRow, clientCreds));
        }, TableNotFoundException.class, InvalidFieldException.class);
    } catch (TableNotFoundException e) {
        throw new OAuthStoreException("OAuth provider table not found", e);
    } catch (InvalidFieldException e) {
        throw new OAuthStoreException("OAuth provider object fields do not match table", e);
    }
}
Also used : TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) StructuredRow(io.cdap.cdap.spi.data.StructuredRow) IOException(java.io.IOException) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) InvalidFieldException(io.cdap.cdap.spi.data.InvalidFieldException) JsonSyntaxException(com.google.gson.JsonSyntaxException) IOException(java.io.IOException) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException)

Example 4 with TableNotFoundException

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

the class SpannerStructuredTableAdmin method loadSchema.

private StructuredTableSchema loadSchema(StructuredTableId tableId) throws TableNotFoundException {
    // Query the information_schema to reconstruct the StructuredTableSchema
    // See https://cloud.google.com/spanner/docs/information-schema for details
    Statement schemaStatement = Statement.newBuilder("SELECT C.column_name, C.spanner_type, I.index_type, I.ordinal_position FROM information_schema.columns C " + "LEFT JOIN information_schema.index_columns I " + "ON C.column_name = I.column_name AND C.table_name = I.table_name " + "WHERE C.table_name = @table_name ORDER BY C.ordinal_position").bind("table_name").to(tableId.getName()).build();
    List<FieldType> fields = new ArrayList<>();
    List<String> primaryKeys = new ArrayList<>();
    List<String> indexes = new ArrayList<>();
    try (ReadOnlyTransaction tx = databaseClient.readOnlyTransaction()) {
        try (ResultSet resultSet = tx.executeQuery(schemaStatement)) {
            while (resultSet.next()) {
                Struct row = resultSet.getCurrentRowAsStruct();
                String columnName = row.getString("column_name");
                String indexType = row.isNull("index_type") ? null : row.getString("index_type");
                // If a field is not a primary nor an index, the ordinal_position will be NULL in the index_columns table.
                boolean isIndex = !row.isNull("ordinal_position");
                fields.add(new FieldType(columnName, fromSpannerType(row.getString("spanner_type"))));
                if ("PRIMARY_KEY".equalsIgnoreCase(indexType)) {
                    primaryKeys.add(columnName);
                } else if ("INDEX".equalsIgnoreCase(indexType) && isIndex) {
                    indexes.add(columnName);
                }
            }
        }
    }
    if (fields.isEmpty()) {
        throw new TableNotFoundException(tableId);
    }
    return new StructuredTableSchema(tableId, fields, primaryKeys, indexes);
}
Also used : StructuredTableSchema(io.cdap.cdap.spi.data.table.StructuredTableSchema) TableNotFoundException(io.cdap.cdap.spi.data.TableNotFoundException) Statement(com.google.cloud.spanner.Statement) ArrayList(java.util.ArrayList) ReadOnlyTransaction(com.google.cloud.spanner.ReadOnlyTransaction) ResultSet(com.google.cloud.spanner.ResultSet) FieldType(io.cdap.cdap.spi.data.table.field.FieldType) Struct(com.google.cloud.spanner.Struct)

Aggregations

TableNotFoundException (io.cdap.cdap.spi.data.TableNotFoundException)4 IOException (java.io.IOException)3 JsonSyntaxException (com.google.gson.JsonSyntaxException)2 InvalidFieldException (io.cdap.cdap.spi.data.InvalidFieldException)2 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)2 ReadOnlyTransaction (com.google.cloud.spanner.ReadOnlyTransaction)1 ResultSet (com.google.cloud.spanner.ResultSet)1 Statement (com.google.cloud.spanner.Statement)1 Struct (com.google.cloud.spanner.Struct)1 StructuredRow (io.cdap.cdap.spi.data.StructuredRow)1 TableAlreadyExistsException (io.cdap.cdap.spi.data.TableAlreadyExistsException)1 StructuredTableId (io.cdap.cdap.spi.data.table.StructuredTableId)1 StructuredTableSchema (io.cdap.cdap.spi.data.table.StructuredTableSchema)1 FieldType (io.cdap.cdap.spi.data.table.field.FieldType)1 ArrayList (java.util.ArrayList)1 Assert (org.junit.Assert)1 Test (org.junit.Test)1