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);
}
}
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);
}
}
}
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);
}
}
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());
}
}
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();
}
Aggregations