Search in sources :

Example 6 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)

Example 7 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(PrepareAndExecuteRequest request) {
    try (final Context ignore = prepareAndExecuteTimer.start()) {
        final Meta.StatementHandle sh = new Meta.StatementHandle(request.connectionId, request.statementId, null);
        try {
            final Meta.ExecuteResult executeResult = meta.prepareAndExecute(sh, request.sql, request.maxRowCount, request.maxRowsInFirstFrame, new Meta.PrepareCallback() {

                @Override
                public Object getMonitor() {
                    return LocalService.class;
                }

                @Override
                public void clear() {
                }

                @Override
                public void assign(Meta.Signature signature, Meta.Frame firstFrame, long updateCount) {
                }

                @Override
                public void execute() {
                }
            });
            final List<ResultSetResponse> results = new ArrayList<>();
            for (Meta.MetaResultSet metaResultSet : executeResult.resultSets) {
                results.add(toResponse(metaResultSet));
            }
            return new ExecuteResponse(results, false, serverLevelRpcMetadata);
        } catch (NoSuchStatementException e) {
            // The Statement doesn't exist anymore, bubble up this information
            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 8 with Context

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

the class JsonHandler method encode.

/**
   * Serializes the provided object as JSON.
   *
   * @param response The object to serialize.
   * @return A JSON string.
   */
@Override
String encode(Response response) throws IOException {
    try (final Context ctx = serializationTimer.start()) {
        final StringWriter w = new StringWriter();
        MAPPER.writeValue(w, response);
        return w.toString();
    }
}
Also used : Context(org.apache.calcite.avatica.metrics.Timer.Context) StringWriter(java.io.StringWriter)

Example 9 with Context

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

the class AvaticaJsonHandler method handle.

public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
    try (final Context ctx = requestTimer.start()) {
        if (!isUserPermitted(serverConfig, request, response)) {
            LOG.debug("HTTP request from {} is unauthenticated and authentication is required", request.getRemoteAddr());
            return;
        }
        response.setContentType("application/json;charset=utf-8");
        response.setStatus(HttpServletResponse.SC_OK);
        if (request.getMethod().equals("POST")) {
            // First look for a request in the header, then look in the body.
            // The latter allows very large requests without hitting HTTP 413.
            String rawRequest = request.getHeader("request");
            if (rawRequest == null) {
                // Avoid a new buffer creation for every HTTP request
                final UnsynchronizedBuffer buffer = threadLocalBuffer.get();
                try (ServletInputStream inputStream = request.getInputStream()) {
                    rawRequest = AvaticaUtils.readFully(inputStream, buffer);
                } finally {
                    // Reset the offset into the buffer after we're done
                    buffer.reset();
                }
            }
            final String jsonRequest = new String(rawRequest.getBytes("ISO-8859-1"), "UTF-8");
            LOG.trace("request: {}", jsonRequest);
            HandlerResponse<String> jsonResponse;
            try {
                if (null != serverConfig && serverConfig.supportsImpersonation()) {
                    String remoteUser = serverConfig.getRemoteUserExtractor().extract(request);
                    jsonResponse = serverConfig.doAsRemoteUser(remoteUser, request.getRemoteAddr(), new Callable<HandlerResponse<String>>() {

                        @Override
                        public HandlerResponse<String> call() {
                            return jsonHandler.apply(jsonRequest);
                        }
                    });
                } else {
                    jsonResponse = jsonHandler.apply(jsonRequest);
                }
            } catch (RemoteUserExtractionException e) {
                LOG.debug("Failed to extract remote user from request", e);
                jsonResponse = jsonHandler.unauthenticatedErrorResponse(e);
            } catch (RemoteUserDisallowedException e) {
                LOG.debug("Remote user is not authorized", e);
                jsonResponse = jsonHandler.unauthorizedErrorResponse(e);
            } catch (Exception e) {
                LOG.debug("Error invoking request from {}", baseRequest.getRemoteAddr(), e);
                jsonResponse = jsonHandler.convertToErrorResponse(e);
            }
            LOG.trace("response: {}", jsonResponse);
            baseRequest.setHandled(true);
            // Set the status code and write out the response.
            response.setStatus(jsonResponse.getStatusCode());
            response.getWriter().println(jsonResponse.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)

Aggregations

Context (org.apache.calcite.avatica.metrics.Timer.Context)9 Meta (org.apache.calcite.avatica.Meta)4 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 Timer (org.apache.calcite.avatica.metrics.Timer)2 UnsynchronizedBuffer (org.apache.calcite.avatica.util.UnsynchronizedBuffer)2 Test (org.junit.Test)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