Search in sources :

Example 1 with ClientException

use of com.facebook.presto.client.ClientException in project presto by prestodb.

the class PrestoStatement method internalExecute.

final boolean internalExecute(String sql) throws SQLException {
    clearCurrentResults();
    checkOpen();
    StatementClient client = null;
    PrestoResultSet resultSet = null;
    boolean intercepted = false;
    try {
        WarningsManager warningsManager = new WarningsManager();
        currentWarningsManager.set(Optional.of(warningsManager));
        int statementDepth = this.statementDepth.incrementAndGet();
        boolean shouldIntercept = !connection().getQueryInterceptorInstances().isEmpty() && statementDepth == 1;
        if (shouldIntercept) {
            Optional<PrestoResultSet> newResultSet = connection().invokeQueryInterceptorsPre(sql, this);
            if (newResultSet.isPresent()) {
                resultSet = newResultSet.get();
            }
        }
        // Check if no resultSet is returned from an interceptor
        if (resultSet != null) {
            currentResult.set(resultSet);
            intercepted = true;
        } else {
            client = connection().startQuery(sql, getStatementSessionProperties());
            if (client.isFinished()) {
                QueryStatusInfo finalStatusInfo = client.finalStatusInfo();
                if (finalStatusInfo.getError() != null) {
                    throw resultsException(finalStatusInfo);
                }
            }
            executingClient.set(client);
            resultSet = new PrestoResultSet(this, client, maxRows.get(), progressConsumer, warningsManager);
            for (Map.Entry<String, SelectedRole> entry : client.getSetRoles().entrySet()) {
                connection.get().setRole(entry.getKey(), entry.getValue());
            }
        }
        // check if this is a query
        if (intercepted || client.currentStatusInfo().getUpdateType() == null) {
            currentResult.set(resultSet);
            if (shouldIntercept) {
                resultSet = connection().invokeQueryInterceptorsPost(sql, this, resultSet);
                verifyNotNull(resultSet, "invokeQueryInterceptorsPost should never return a null ResultSet");
                currentResult.set(resultSet);
            }
            return true;
        }
        // this is an update, not a query
        while (resultSet.next()) {
        // ignore rows
        }
        connection().updateSession(client);
        Long updateCount = client.finalStatusInfo().getUpdateCount();
        currentUpdateCount.set((updateCount != null) ? updateCount : 0);
        currentUpdateType.set(client.finalStatusInfo().getUpdateType());
        warningsManager.addWarnings(client.finalStatusInfo().getWarnings());
        return false;
    } catch (ClientException e) {
        throw new SQLException(e.getMessage(), e);
    } catch (RuntimeException e) {
        throw new SQLException("Error executing query", e);
    } finally {
        this.statementDepth.decrementAndGet();
        executingClient.set(null);
        if (currentResult.get() == null) {
            if (resultSet != null) {
                resultSet.close();
            }
            if (client != null) {
                client.close();
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) SelectedRole(com.facebook.presto.spi.security.SelectedRole) StatementClient(com.facebook.presto.client.StatementClient) QueryStatusInfo(com.facebook.presto.client.QueryStatusInfo) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClientException(com.facebook.presto.client.ClientException) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map)

Example 2 with ClientException

use of com.facebook.presto.client.ClientException in project presto by prestodb.

the class QueryExecutor method getServerInfo.

public ServerInfo getServerInfo(URI server) {
    HttpUrl url = HttpUrl.get(server);
    if (url == null) {
        throw new ClientException("Invalid server URL: " + server);
    }
    url = url.newBuilder().encodedPath("/v1/info").build();
    Request request = new Request.Builder().url(url).build();
    JsonResponse<ServerInfo> response = JsonResponse.execute(SERVER_INFO_CODEC, httpClient, request);
    if (!response.hasValue()) {
        throw new RuntimeException(format("Request to %s failed: %s [Error: %s]", server, response, response.getResponseBody()));
    }
    return response.getValue();
}
Also used : ServerInfo(com.facebook.presto.client.ServerInfo) Request(okhttp3.Request) ClientException(com.facebook.presto.client.ClientException) HttpUrl(okhttp3.HttpUrl)

Example 3 with ClientException

use of com.facebook.presto.client.ClientException in project presto by prestodb.

the class PrestoDriverUri method setupClient.

public void setupClient(OkHttpClient.Builder builder) throws SQLException {
    try {
        setupCookieJar(builder);
        setupSocksProxy(builder, SOCKS_PROXY.getValue(properties));
        setupHttpProxy(builder, HTTP_PROXY.getValue(properties));
        // add user specified protocols to okhttp3 client if specified
        getProtocols().ifPresent(builder::protocols);
        // TODO: fix Tempto to allow empty passwords
        String password = PASSWORD.getValue(properties).orElse("");
        if (!password.isEmpty() && !password.equals("***empty***")) {
            if (!useSecureConnection) {
                throw new SQLException("Authentication using username/password requires SSL to be enabled");
            }
            builder.addInterceptor(basicAuth(getUser(), password));
        }
        if (useSecureConnection) {
            setupSsl(builder, SSL_KEY_STORE_PATH.getValue(properties), SSL_KEY_STORE_PASSWORD.getValue(properties), SSL_TRUST_STORE_PATH.getValue(properties), SSL_TRUST_STORE_PASSWORD.getValue(properties));
        }
        if (KERBEROS_REMOTE_SERVICE_NAME.getValue(properties).isPresent()) {
            if (!useSecureConnection) {
                throw new SQLException("Authentication using Kerberos requires SSL to be enabled");
            }
            setupKerberos(builder, KERBEROS_REMOTE_SERVICE_NAME.getRequiredValue(properties), KERBEROS_USE_CANONICAL_HOSTNAME.getRequiredValue(properties), KERBEROS_PRINCIPAL.getValue(properties), KERBEROS_CONFIG_PATH.getValue(properties), KERBEROS_KEYTAB_PATH.getValue(properties), Optional.ofNullable(KERBEROS_CREDENTIAL_CACHE_PATH.getValue(properties).orElseGet(() -> defaultCredentialCachePath().map(File::new).orElse(null))));
        }
        Map<String, String> extraCredentials = EXTRA_CREDENTIALS.getValue(properties).orElse(ImmutableMap.of());
        Optional.ofNullable(extraCredentials.get(GCS_CREDENTIALS_PATH_KEY)).ifPresent(credentialPath -> OkHttpUtil.setupGCSOauth(builder, credentialPath, Optional.ofNullable(extraCredentials.get(GCS_OAUTH_SCOPES_KEY))));
        if (ACCESS_TOKEN.getValue(properties).isPresent()) {
            if (!useSecureConnection) {
                throw new SQLException("Authentication using an access token requires SSL to be enabled");
            }
            builder.addInterceptor(tokenAuth(ACCESS_TOKEN.getValue(properties).get()));
        }
    } catch (ClientException e) {
        throw new SQLException(e.getMessage(), e);
    } catch (RuntimeException e) {
        throw new SQLException("Error setting up connection", e);
    }
}
Also used : SQLException(java.sql.SQLException) ClientException(com.facebook.presto.client.ClientException) File(java.io.File)

Aggregations

ClientException (com.facebook.presto.client.ClientException)3 SQLException (java.sql.SQLException)2 QueryStatusInfo (com.facebook.presto.client.QueryStatusInfo)1 ServerInfo (com.facebook.presto.client.ServerInfo)1 StatementClient (com.facebook.presto.client.StatementClient)1 SelectedRole (com.facebook.presto.spi.security.SelectedRole)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 File (java.io.File)1 Map (java.util.Map)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1 HttpUrl (okhttp3.HttpUrl)1 Request (okhttp3.Request)1