Search in sources :

Example 1 with Context

use of org.apache.calcite.avatica.metrics.Timer.Context in project calcite-avatica by apache.

the class LocalService method apply.

public PrepareResponse apply(PrepareRequest request) {
    try (final Context ignore = prepareTimer.start()) {
        final Meta.ConnectionHandle ch = new Meta.ConnectionHandle(request.connectionId);
        final Meta.StatementHandle h = meta.prepare(ch, request.sql, request.maxRowCount);
        return new PrepareResponse(h, serverLevelRpcMetadata);
    }
}
Also used : Context(org.apache.calcite.avatica.metrics.Timer.Context) Meta(org.apache.calcite.avatica.Meta)

Example 2 with Context

use of org.apache.calcite.avatica.metrics.Timer.Context in project calcite-avatica by apache.

the class LocalService method apply.

public ExecuteResponse apply(ExecuteRequest request) {
    try (final Context ignore = executeTimer.start()) {
        try {
            final Meta.ExecuteResult executeResult = meta.execute(request.statementHandle, request.parameterValues, AvaticaUtils.toSaturatedInt(request.maxRowCount));
            final List<ResultSetResponse> results = new ArrayList<>(executeResult.resultSets.size());
            for (Meta.MetaResultSet metaResultSet : executeResult.resultSets) {
                results.add(toResponse(metaResultSet));
            }
            return new ExecuteResponse(results, false, serverLevelRpcMetadata);
        } catch (NoSuchStatementException e) {
            return new ExecuteResponse(null, true, serverLevelRpcMetadata);
        }
    }
}
Also used : Context(org.apache.calcite.avatica.metrics.Timer.Context) Meta(org.apache.calcite.avatica.Meta) ArrayList(java.util.ArrayList) NoSuchStatementException(org.apache.calcite.avatica.NoSuchStatementException)

Example 3 with Context

use of org.apache.calcite.avatica.metrics.Timer.Context in project calcite-avatica by apache.

the class LocalService method apply.

public ConnectionSyncResponse apply(ConnectionSyncRequest request) {
    try (final Context ignore = connectionSyncTimer.start()) {
        final Meta.ConnectionHandle ch = new Meta.ConnectionHandle(request.connectionId);
        final Meta.ConnectionProperties connProps = meta.connectionSync(ch, request.connProps);
        return new ConnectionSyncResponse(connProps, serverLevelRpcMetadata);
    }
}
Also used : Context(org.apache.calcite.avatica.metrics.Timer.Context) Meta(org.apache.calcite.avatica.Meta)

Example 4 with Context

use of org.apache.calcite.avatica.metrics.Timer.Context in project calcite-avatica by apache.

the class AvaticaProtobufHandler method handle.

public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    try (final Context ctx = this.requestTimer.start()) {
        if (!request.getMethod().equals("POST")) {
            response.setStatus(HttpServletResponse.SC_BAD_REQUEST);
            response.getOutputStream().write("This server expects only POST calls.".getBytes(StandardCharsets.UTF_8));
            baseRequest.setHandled(true);
            return;
        }
        // Check if the user is OK to proceed.
        if (!isUserPermitted(serverConfig, baseRequest, request, response)) {
            LOG.debug("HTTP request from {} is unauthenticated and authentication is required", request.getRemoteAddr());
            return;
        }
        final byte[] requestBytes;
        // Avoid a new buffer creation for every HTTP request
        final UnsynchronizedBuffer buffer = threadLocalBuffer.get();
        try (ServletInputStream inputStream = request.getInputStream()) {
            requestBytes = AvaticaUtils.readFullyToBytes(inputStream, buffer);
        } finally {
            buffer.reset();
        }
        response.setContentType("application/octet-stream;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        HandlerResponse<byte[]> handlerResponse;
        try {
            if (null != serverConfig && serverConfig.supportsImpersonation()) {
                // If we can't extract a user, need to throw 401 in that case.
                String remoteUser = serverConfig.getRemoteUserExtractor().extract(request);
                // Invoke the ProtobufHandler inside as doAs for the remote user.
                // The doAsRemoteUser call may disallow a user, need to throw 403 in that case.
                handlerResponse = serverConfig.doAsRemoteUser(remoteUser, request.getRemoteAddr(), new Callable<HandlerResponse<byte[]>>() {

                    @Override
                    public HandlerResponse<byte[]> call() {
                        return pbHandler.apply(requestBytes);
                    }
                });
            } else {
                handlerResponse = pbHandler.apply(requestBytes);
            }
        } catch (RemoteUserExtractionException e) {
            LOG.debug("Failed to extract remote user from request", e);
            handlerResponse = pbHandler.unauthenticatedErrorResponse(e);
        } catch (RemoteUserDisallowedException e) {
            LOG.debug("Remote user is not authorized", e);
            handlerResponse = pbHandler.unauthorizedErrorResponse(e);
        } catch (Exception e) {
            LOG.debug("Error invoking request from {}", baseRequest.getRemoteAddr(), e);
            // Catch at the highest level of exceptions
            handlerResponse = pbHandler.convertToErrorResponse(e);
        }
        baseRequest.setHandled(true);
        response.setStatus(handlerResponse.getStatusCode());
        response.getOutputStream().write(handlerResponse.getResponse());
    }
}
Also used : Context(org.apache.calcite.avatica.metrics.Timer.Context) UnsynchronizedBuffer(org.apache.calcite.avatica.util.UnsynchronizedBuffer) ServletInputStream(javax.servlet.ServletInputStream) Callable(java.util.concurrent.Callable) ServletException(javax.servlet.ServletException) IOException(java.io.IOException)

Example 5 with Context

use of org.apache.calcite.avatica.metrics.Timer.Context in project calcite-avatica by apache.

the class DropwizardMetricsSystemTest method testTimer.

@Test
public void testTimer() {
    final String name = "timer";
    final com.codahale.metrics.Timer mockTimer = mock(com.codahale.metrics.Timer.class);
    final com.codahale.metrics.Timer.Context mockContext = mock(com.codahale.metrics.Timer.Context.class);
    when(mockRegistry.timer(name)).thenReturn(mockTimer);
    when(mockTimer.time()).thenReturn(mockContext);
    Timer timer = metrics.getTimer(name);
    Context context = timer.start();
    context.close();
    verify(mockTimer).time();
    verify(mockContext).stop();
}
Also used : Context(org.apache.calcite.avatica.metrics.Timer.Context) Timer(org.apache.calcite.avatica.metrics.Timer) Test(org.junit.Test)

Aggregations

Context (org.apache.calcite.avatica.metrics.Timer.Context)10 Meta (org.apache.calcite.avatica.Meta)4 Timer (org.apache.calcite.avatica.metrics.Timer)3 Test (org.junit.Test)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Callable (java.util.concurrent.Callable)2 ServletException (javax.servlet.ServletException)2 ServletInputStream (javax.servlet.ServletInputStream)2 NoSuchStatementException (org.apache.calcite.avatica.NoSuchStatementException)2 UnsynchronizedBuffer (org.apache.calcite.avatica.util.UnsynchronizedBuffer)2 StringWriter (java.io.StringWriter)1 Counter (org.apache.calcite.avatica.metrics.Counter)1 Histogram (org.apache.calcite.avatica.metrics.Histogram)1 Meter (org.apache.calcite.avatica.metrics.Meter)1 MetricsSystem (org.apache.calcite.avatica.metrics.MetricsSystem)1