Search in sources :

Example 1 with ClientException

use of io.prestosql.client.ClientException in project hetu-core by openlookeng.

the class PrestoStatement method internalExecute.

final boolean internalExecute(String sql) throws SQLException {
    clearCurrentResults();
    checkOpen();
    StatementClient client = null;
    PrestoResultSet resultSet = null;
    try {
        client = connection().startQuery(sql, getStatementSessionProperties());
        if (client.isFinished()) {
            QueryStatusInfo finalStatusInfo = client.finalStatusInfo();
            if (finalStatusInfo.getError() != null) {
                throw resultsException(finalStatusInfo);
            }
        }
        executingClient.set(client);
        WarningsManager warningsManager = new WarningsManager();
        currentWarningsManager.set(Optional.of(warningsManager));
        resultSet = new PrestoResultSet(client, maxRows.get(), progressConsumer, warningsManager);
        // check if this is a query
        if (client.currentStatusInfo().getUpdateType() == null) {
            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 {
        executingClient.set(null);
        if (currentResult.get() == null) {
            if (resultSet != null) {
                resultSet.close();
            }
            if (client != null) {
                client.close();
            }
        }
    }
}
Also used : SQLException(java.sql.SQLException) StatementClient(io.prestosql.client.StatementClient) AtomicLong(java.util.concurrent.atomic.AtomicLong) ClientException(io.prestosql.client.ClientException) QueryStatusInfo(io.prestosql.client.QueryStatusInfo)

Example 2 with ClientException

use of io.prestosql.client.ClientException in project hetu-core by openlookeng.

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(io.prestosql.client.ServerInfo) Request(okhttp3.Request) ClientException(io.prestosql.client.ClientException) HttpUrl(okhttp3.HttpUrl)

Example 3 with ClientException

use of io.prestosql.client.ClientException in project hetu-core by openlookeng.

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));
        // 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_SERVICE_PRINCIPAL_PATTERN.getRequiredValue(properties), 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))));
        }
        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(io.prestosql.client.ClientException) File(java.io.File)

Example 4 with ClientException

use of io.prestosql.client.ClientException in project hetu-core by openlookeng.

the class HttpUtil method buildQueryRequest.

public static Request buildQueryRequest(String clientId, DataCenterClientSession session, String queryId, String query) {
    HttpUrl url = HttpUrl.get(session.getServer());
    if (url == null) {
        throw new ClientException("Invalid server URL: " + session.getServer());
    }
    url = url.newBuilder().encodedPath(ROOT_URL + queryId).build();
    DataCenterRequest request = new DataCenterRequest(queryId, clientId, query, session.getMaxAnticipatedDelay(), DataCenterResponseType.HTTP_PULL);
    Request.Builder builder = prepareRequest(url, session).post(RequestBody.create(MEDIA_TYPE_JSON, DATA_CENTER_REQUEST_CODEC.toJsonBytes(request)));
    if (session.getSource() != null) {
        builder.addHeader(PRESTO_SOURCE, session.getSource());
    }
    session.getTraceToken().ifPresent(token -> builder.addHeader(PRESTO_TRACE_TOKEN, token));
    if (session.getClientTags() != null && !session.getClientTags().isEmpty()) {
        builder.addHeader(PRESTO_CLIENT_TAGS, Joiner.on(",").join(session.getClientTags()));
    }
    if (session.getClientInfo() != null) {
        builder.addHeader(PRESTO_CLIENT_INFO, session.getClientInfo());
    }
    if (session.getCatalog() != null) {
        builder.addHeader(PRESTO_CATALOG, session.getCatalog());
    }
    if (session.getSchema() != null) {
        builder.addHeader(PRESTO_SCHEMA, session.getSchema());
    }
    if (session.getPath() != null) {
        builder.addHeader(PRESTO_PATH, session.getPath());
    }
    builder.addHeader(PRESTO_TIME_ZONE, session.getTimeZone().getId());
    if (session.getLocale() != null) {
        builder.addHeader(PRESTO_LANGUAGE, session.getLocale().toLanguageTag());
    }
    Map<String, String> property = session.getProperties();
    for (Map.Entry<String, String> entry : property.entrySet()) {
        builder.addHeader(PRESTO_SESSION, entry.getKey() + "=" + urlEncode(entry.getValue()));
    }
    Map<String, String> resourceEstimates = session.getResourceEstimates();
    for (Map.Entry<String, String> entry : resourceEstimates.entrySet()) {
        builder.addHeader(PRESTO_RESOURCE_ESTIMATE, entry.getKey() + "=" + urlEncode(entry.getValue()));
    }
    Map<String, ClientSelectedRole> roles = session.getRoles();
    for (Map.Entry<String, ClientSelectedRole> entry : roles.entrySet()) {
        builder.addHeader(PrestoHeaders.PRESTO_ROLE, entry.getKey() + '=' + urlEncode(entry.getValue().toString()));
    }
    Map<String, String> extraCredentials = session.getExtraCredentials();
    for (Map.Entry<String, String> entry : extraCredentials.entrySet()) {
        builder.addHeader(PRESTO_EXTRA_CREDENTIAL, entry.getKey() + "=" + urlEncode(entry.getValue()));
    }
    Map<String, String> statements = session.getPreparedStatements();
    for (Map.Entry<String, String> entry : statements.entrySet()) {
        builder.addHeader(PRESTO_PREPARED_STATEMENT, urlEncode(entry.getKey()) + "=" + urlEncode(entry.getValue()));
    }
    builder.addHeader(PRESTO_TRANSACTION_ID, session.getTransactionId() == null ? "NONE" : session.getTransactionId());
    builder.addHeader(PRESTO_CLIENT_CAPABILITIES, CLIENT_CAPABILITIES);
    return builder.build();
}
Also used : DataCenterRequest(io.prestosql.client.DataCenterRequest) ClientSelectedRole(io.prestosql.client.ClientSelectedRole) Request(okhttp3.Request) CrossRegionDynamicFilterRequest(io.prestosql.client.CrossRegionDynamicFilterRequest) DataCenterRequest(io.prestosql.client.DataCenterRequest) ClientException(io.prestosql.client.ClientException) Map(java.util.Map) HttpUrl(okhttp3.HttpUrl)

Example 5 with ClientException

use of io.prestosql.client.ClientException in project hetu-core by openlookeng.

the class HttpUtil method buildDynamicFilterRequest.

public static Request buildDynamicFilterRequest(String clientId, DataCenterClientSession session, String queryId, Map<String, byte[]> dynamicFilters) {
    HttpUrl url = HttpUrl.get(session.getServer());
    if (url == null) {
        throw new ClientException("Invalid server URL: " + session.getServer());
    }
    url = url.newBuilder().encodedPath(DYNAMIC_FILTER_URL + queryId).build();
    CrossRegionDynamicFilterRequest request = new CrossRegionDynamicFilterRequest(queryId, clientId, dynamicFilters);
    Request.Builder builder = prepareRequest(url, session).post(RequestBody.create(MEDIA_TYPE_JSON, CRDF_REQUEST_CODEC.toJsonBytes(request)));
    return builder.build();
}
Also used : CrossRegionDynamicFilterRequest(io.prestosql.client.CrossRegionDynamicFilterRequest) Request(okhttp3.Request) CrossRegionDynamicFilterRequest(io.prestosql.client.CrossRegionDynamicFilterRequest) DataCenterRequest(io.prestosql.client.DataCenterRequest) ClientException(io.prestosql.client.ClientException) HttpUrl(okhttp3.HttpUrl)

Aggregations

ClientException (io.prestosql.client.ClientException)5 HttpUrl (okhttp3.HttpUrl)3 Request (okhttp3.Request)3 CrossRegionDynamicFilterRequest (io.prestosql.client.CrossRegionDynamicFilterRequest)2 DataCenterRequest (io.prestosql.client.DataCenterRequest)2 SQLException (java.sql.SQLException)2 ClientSelectedRole (io.prestosql.client.ClientSelectedRole)1 QueryStatusInfo (io.prestosql.client.QueryStatusInfo)1 ServerInfo (io.prestosql.client.ServerInfo)1 StatementClient (io.prestosql.client.StatementClient)1 File (java.io.File)1 Map (java.util.Map)1 AtomicLong (java.util.concurrent.atomic.AtomicLong)1