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