Search in sources :

Example 6 with SqlQuery

use of org.apache.druid.sql.http.SqlQuery in project druid by apache.

the class ITSqlCancelTest method testCancelInvalidQuery.

@Test
public void testCancelInvalidQuery() throws Exception {
    final Future<StatusResponseHolder> queryResponseFuture = sqlClient.queryAsync(sqlHelper.getQueryURL(config.getRouterUrl()), new SqlQuery(QUERY, null, false, false, false, ImmutableMap.of(BaseQuery.SQL_QUERY_ID, "validId"), null));
    // Wait until the sqlLifecycle is authorized and registered
    Thread.sleep(1000);
    final HttpResponseStatus responseStatus = sqlClient.cancelQuery(sqlHelper.getCancelUrl(config.getRouterUrl(), "invalidId"), 1000);
    if (!responseStatus.equals(HttpResponseStatus.NOT_FOUND)) {
        throw new RE("Expected http response [%s], actual response [%s]", HttpResponseStatus.NOT_FOUND, responseStatus);
    }
    final StatusResponseHolder queryResponse = queryResponseFuture.get(30, TimeUnit.SECONDS);
    if (!queryResponse.getStatus().equals(HttpResponseStatus.OK)) {
        throw new ISE("Cancel request failed with status[%s] and content[%s]", queryResponse.getStatus(), queryResponse.getContent());
    }
}
Also used : SqlQuery(org.apache.druid.sql.http.SqlQuery) RE(org.apache.druid.java.util.common.RE) HttpResponseStatus(org.jboss.netty.handler.codec.http.HttpResponseStatus) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) ISE(org.apache.druid.java.util.common.ISE) Test(org.testng.annotations.Test)

Example 7 with SqlQuery

use of org.apache.druid.sql.http.SqlQuery in project druid by druid-io.

the class ITSqlCancelTest method testCancelInvalidQuery.

@Test
public void testCancelInvalidQuery() throws Exception {
    final Future<StatusResponseHolder> queryResponseFuture = sqlClient.queryAsync(sqlHelper.getQueryURL(config.getRouterUrl()), new SqlQuery(QUERY, null, false, false, false, ImmutableMap.of(BaseQuery.SQL_QUERY_ID, "validId"), null));
    // Wait until the sqlLifecycle is authorized and registered
    Thread.sleep(1000);
    final HttpResponseStatus responseStatus = sqlClient.cancelQuery(sqlHelper.getCancelUrl(config.getRouterUrl(), "invalidId"), 1000);
    if (!responseStatus.equals(HttpResponseStatus.NOT_FOUND)) {
        throw new RE("Expected http response [%s], actual response [%s]", HttpResponseStatus.NOT_FOUND, responseStatus);
    }
    final StatusResponseHolder queryResponse = queryResponseFuture.get(30, TimeUnit.SECONDS);
    if (!queryResponse.getStatus().equals(HttpResponseStatus.OK)) {
        throw new ISE("Cancel request failed with status[%s] and content[%s]", queryResponse.getStatus(), queryResponse.getContent());
    }
}
Also used : SqlQuery(org.apache.druid.sql.http.SqlQuery) RE(org.apache.druid.java.util.common.RE) HttpResponseStatus(org.jboss.netty.handler.codec.http.HttpResponseStatus) StatusResponseHolder(org.apache.druid.java.util.http.client.response.StatusResponseHolder) ISE(org.apache.druid.java.util.common.ISE) Test(org.testng.annotations.Test)

Example 8 with SqlQuery

use of org.apache.druid.sql.http.SqlQuery in project druid by druid-io.

the class AsyncQueryForwardingServlet method sendProxyRequest.

@Override
protected void sendProxyRequest(HttpServletRequest clientRequest, HttpServletResponse proxyResponse, Request proxyRequest) {
    proxyRequest.timeout(httpClientConfig.getReadTimeout().getMillis(), TimeUnit.MILLISECONDS);
    proxyRequest.idleTimeout(httpClientConfig.getReadTimeout().getMillis(), TimeUnit.MILLISECONDS);
    byte[] avaticaQuery = (byte[]) clientRequest.getAttribute(AVATICA_QUERY_ATTRIBUTE);
    if (avaticaQuery != null) {
        proxyRequest.content(new BytesContentProvider(avaticaQuery));
    }
    final Query query = (Query) clientRequest.getAttribute(QUERY_ATTRIBUTE);
    final SqlQuery sqlQuery = (SqlQuery) clientRequest.getAttribute(SQL_QUERY_ATTRIBUTE);
    if (query != null) {
        setProxyRequestContent(proxyRequest, clientRequest, query);
    } else if (sqlQuery != null) {
        setProxyRequestContent(proxyRequest, clientRequest, sqlQuery);
    }
    // Since we can't see the request object on the remote side, we can't check whether the remote side actually
    // performed an authorization check here, so always set this to true for the proxy servlet.
    // If the remote node failed to perform an authorization check, PreResponseAuthorizationCheckFilter
    // will log that on the remote node.
    clientRequest.setAttribute(AuthConfig.DRUID_AUTHORIZATION_CHECKED, true);
    // Check if there is an authentication result and use it to decorate the proxy request if needed.
    AuthenticationResult authenticationResult = (AuthenticationResult) clientRequest.getAttribute(AuthConfig.DRUID_AUTHENTICATION_RESULT);
    if (authenticationResult != null && authenticationResult.getAuthenticatedBy() != null) {
        Authenticator authenticator = authenticatorMapper.getAuthenticatorMap().get(authenticationResult.getAuthenticatedBy());
        if (authenticator != null) {
            authenticator.decorateProxyRequest(clientRequest, proxyResponse, proxyRequest);
        } else {
            LOG.error("Can not find Authenticator with Name [%s]", authenticationResult.getAuthenticatedBy());
        }
    }
    super.sendProxyRequest(clientRequest, proxyResponse, proxyRequest);
}
Also used : SqlQuery(org.apache.druid.sql.http.SqlQuery) Query(org.apache.druid.query.Query) SqlQuery(org.apache.druid.sql.http.SqlQuery) BytesContentProvider(org.eclipse.jetty.client.util.BytesContentProvider) Authenticator(org.apache.druid.server.security.Authenticator) AuthenticationResult(org.apache.druid.server.security.AuthenticationResult)

Example 9 with SqlQuery

use of org.apache.druid.sql.http.SqlQuery in project druid by apache.

the class AsyncQueryForwardingServletTest method testSqlQueryProxy.

@Test
public void testSqlQueryProxy() throws Exception {
    final SqlQuery query = new SqlQuery("SELECT * FROM foo", ResultFormat.ARRAY, false, false, false, null, null);
    final QueryHostFinder hostFinder = EasyMock.createMock(QueryHostFinder.class);
    EasyMock.expect(hostFinder.findServerSql(query)).andReturn(new TestServer("http", "1.2.3.4", 9999)).once();
    EasyMock.replay(hostFinder);
    Properties properties = new Properties();
    properties.setProperty("druid.router.sql.enable", "true");
    verifyServletCallsForQuery(query, true, hostFinder, properties);
}
Also used : SqlQuery(org.apache.druid.sql.http.SqlQuery) Properties(java.util.Properties) QueryHostFinder(org.apache.druid.server.router.QueryHostFinder) BaseJettyTest(org.apache.druid.server.initialization.BaseJettyTest) Test(org.junit.Test)

Example 10 with SqlQuery

use of org.apache.druid.sql.http.SqlQuery in project druid by apache.

the class AsyncQueryForwardingServlet method service.

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    final boolean isSmile = SmileMediaTypes.APPLICATION_JACKSON_SMILE.equals(request.getContentType()) || APPLICATION_SMILE.equals(request.getContentType());
    final ObjectMapper objectMapper = isSmile ? smileMapper : jsonMapper;
    request.setAttribute(OBJECTMAPPER_ATTRIBUTE, objectMapper);
    final String requestURI = request.getRequestURI();
    final String method = request.getMethod();
    final Server targetServer;
    // The Router does not have the ability to look inside SQL queries and route them intelligently, so just treat
    // them as a generic request.
    final boolean isNativeQueryEndpoint = requestURI.startsWith("/druid/v2") && !requestURI.startsWith("/druid/v2/sql");
    final boolean isSqlQueryEndpoint = requestURI.startsWith("/druid/v2/sql");
    final boolean isAvaticaJson = requestURI.startsWith("/druid/v2/sql/avatica");
    final boolean isAvaticaPb = requestURI.startsWith("/druid/v2/sql/avatica-protobuf");
    if (isAvaticaPb) {
        byte[] requestBytes = IOUtils.toByteArray(request.getInputStream());
        Service.Request protobufRequest = this.protobufTranslation.parseRequest(requestBytes);
        String connectionId = getAvaticaProtobufConnectionId(protobufRequest);
        targetServer = hostFinder.findServerAvatica(connectionId);
        request.setAttribute(AVATICA_QUERY_ATTRIBUTE, requestBytes);
        LOG.debug("Forwarding protobuf JDBC connection [%s] to broker [%s]", connectionId, targetServer);
    } else if (isAvaticaJson) {
        Map<String, Object> requestMap = objectMapper.readValue(request.getInputStream(), JacksonUtils.TYPE_REFERENCE_MAP_STRING_OBJECT);
        String connectionId = getAvaticaConnectionId(requestMap);
        targetServer = hostFinder.findServerAvatica(connectionId);
        byte[] requestBytes = objectMapper.writeValueAsBytes(requestMap);
        request.setAttribute(AVATICA_QUERY_ATTRIBUTE, requestBytes);
        LOG.debug("Forwarding JDBC connection [%s] to broker [%s]", connectionId, targetServer.getHost());
    } else if (HttpMethod.DELETE.is(method)) {
        // query cancellation request
        targetServer = hostFinder.pickDefaultServer();
        broadcastQueryCancelRequest(request, targetServer);
        LOG.debug("Broadcasting cancellation request to all brokers");
    } else if (isNativeQueryEndpoint && HttpMethod.POST.is(method)) {
        // query request
        try {
            Query inputQuery = objectMapper.readValue(request.getInputStream(), Query.class);
            if (inputQuery != null) {
                targetServer = hostFinder.pickServer(inputQuery);
                if (inputQuery.getId() == null) {
                    inputQuery = inputQuery.withId(UUID.randomUUID().toString());
                }
                LOG.debug("Forwarding JSON query [%s] to broker [%s]", inputQuery.getId(), targetServer.getHost());
            } else {
                targetServer = hostFinder.pickDefaultServer();
                LOG.debug("Forwarding JSON request to broker [%s]", targetServer.getHost());
            }
            request.setAttribute(QUERY_ATTRIBUTE, inputQuery);
        } catch (IOException e) {
            handleQueryParseException(request, response, objectMapper, e, true);
            return;
        } catch (Exception e) {
            handleException(response, objectMapper, e);
            return;
        }
    } else if (routeSqlByStrategy && isSqlQueryEndpoint && HttpMethod.POST.is(method)) {
        try {
            SqlQuery inputSqlQuery = objectMapper.readValue(request.getInputStream(), SqlQuery.class);
            request.setAttribute(SQL_QUERY_ATTRIBUTE, inputSqlQuery);
            targetServer = hostFinder.findServerSql(inputSqlQuery);
            LOG.debug("Forwarding SQL query to broker [%s]", targetServer.getHost());
        } catch (IOException e) {
            handleQueryParseException(request, response, objectMapper, e, false);
            return;
        } catch (Exception e) {
            handleException(response, objectMapper, e);
            return;
        }
    } else {
        targetServer = hostFinder.pickDefaultServer();
        LOG.debug("Forwarding query to broker [%s]", targetServer.getHost());
    }
    request.setAttribute(HOST_ATTRIBUTE, targetServer.getHost());
    request.setAttribute(SCHEME_ATTRIBUTE, targetServer.getScheme());
    doService(request, response);
}
Also used : SqlQuery(org.apache.druid.sql.http.SqlQuery) Server(org.apache.druid.client.selector.Server) Query(org.apache.druid.query.Query) SqlQuery(org.apache.druid.sql.http.SqlQuery) Service(org.apache.calcite.avatica.remote.Service) IOException(java.io.IOException) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ServletException(javax.servlet.ServletException) QueryInterruptedException(org.apache.druid.query.QueryInterruptedException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException)

Aggregations

SqlQuery (org.apache.druid.sql.http.SqlQuery)12 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)4 ISE (org.apache.druid.java.util.common.ISE)4 RE (org.apache.druid.java.util.common.RE)4 StatusResponseHolder (org.apache.druid.java.util.http.client.response.StatusResponseHolder)4 Query (org.apache.druid.query.Query)4 HttpResponseStatus (org.jboss.netty.handler.codec.http.HttpResponseStatus)4 Test (org.junit.Test)4 Test (org.testng.annotations.Test)4 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 Properties (java.util.Properties)2 Future (java.util.concurrent.Future)2 ServletException (javax.servlet.ServletException)2 Service (org.apache.calcite.avatica.remote.Service)2 Server (org.apache.druid.client.selector.Server)2 QueryException (org.apache.druid.query.QueryException)2