use of com.linkedin.databus2.core.container.request.DatabusRequest in project databus by linkedin.
the class ClientStateRequestProcessor method findV2Registration.
/**
* Helper method to locate a databus V2 registration by its registration id. This method
* can locate both top-level (registered by one of _dbusClient.registerXXX()) and
* individual-partition (child) registration that are aggregated inside a top-level
* MultiPartition registration.
*
* Please note that this can traverse the registration tree which is 1 level deep. In
* other words, it will not work when we have MultiPartition registrations aggregated
* inside another MultiPartition registrations.
*
* @param regId
* Registration Id to be located
* @param request
* Databus Request corresponding to the REST call.
* @return
* @throws RequestProcessingException
* when the registration is not found.
*/
private DatabusRegistration findV2Registration(DatabusRequest request, String prefix) throws RequestProcessingException {
String category = request.getParams().getProperty(DatabusRequest.PATH_PARAM_NAME);
String registrationIdStr = category.substring(prefix.length());
RegistrationId regId = new RegistrationId(registrationIdStr);
Collection<DatabusRegistration> regs = _client.getAllRegistrations();
if (null != regs) {
for (DatabusRegistration r : regs) {
if (regId.equals(r.getRegistrationId())) {
return r;
}
/**
* Important Note: There is an important implementation difference on which
* registrations are stored in the global registration data-structure maintained
* by the client (DatabusHttp[V3]ClientImpls) between V2 and V3.
*
* 1. In the case of V2, only top-level registrations are stored in the global
* data-structure (DatabusHttpClientImpl.regList 2. In the case of V3, all
* registrations are stored in the global data-structure.
*
* In the case of V3, this is needed so that all registrations can act on the
* relay external view change. This can be refactored in the future by moving the
* relay-external view change to registration impl ( reduce the complexity in
* ClientImpl ). The V2 implementation did not have this logic and was following a
* more intuitive structure of preserving the hierarchy. The below code handles
* the discrepancy for V2.
*/
if (r instanceof DatabusMultiPartitionRegistration) {
Map<DbusPartitionInfo, DatabusRegistration> childRegs = ((DatabusMultiPartitionRegistration) r).getPartitionRegs();
for (Entry<DbusPartitionInfo, DatabusRegistration> e : childRegs.entrySet()) {
if (regId.equals(e.getValue().getRegistrationId())) {
return e.getValue();
}
}
}
}
}
throw new RequestProcessingException("Unable to find registration (" + regId + ") ");
}
use of com.linkedin.databus2.core.container.request.DatabusRequest in project databus by linkedin.
the class ClientStatsRequestProcessor method processOutboundHttpClientStats.
private void processOutboundHttpClientStats(DatabusRequest request) throws IOException, RequestProcessingException {
String category = request.getParams().getProperty(DatabusRequest.PATH_PARAM_NAME);
String client = category.substring(INBOUND_HTTP_RELAYS_PREFIX.length());
DbusHttpTotalStats clientStats = _client.getHttpStatsCollector().getPeerStats(client);
if (null == clientStats) {
throw new InvalidRequestParamValueException(request.getName(), INBOUND_HTTP_RELAYS_PREFIX, client);
}
writeJsonObjectToResponse(clientStats, request);
if (request.getRequestType() == HttpMethod.PUT || request.getRequestType() == HttpMethod.POST) {
enableOrResetStatsMBean(clientStats, request);
}
}
use of com.linkedin.databus2.core.container.request.DatabusRequest in project databus by linkedin.
the class DatabusRequestExecutionHandler method messageReceived.
@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
if (e.getMessage() instanceof HttpRequest) {
_httpRequest = (HttpRequest) e.getMessage();
ctx.sendUpstream(e);
}
if (e.getMessage() instanceof DatabusRequest) {
_dbusRequest = (DatabusRequest) e.getMessage();
// If there is a physical partition stashed away, then restore it into the request now.
if (ctx.getAttachment() != null && ctx.getAttachment() instanceof PhysicalPartition) {
_dbusRequest.setCursorPartition((PhysicalPartition) (ctx.getAttachment()));
}
/*NettyStats nettyStats = _configManager.getNettyStats();
boolean nettyStatsEnabled = nettyStats.isEnabled();
CallCompletion callCompletion = nettyStatsEnabled ?
nettyStats.getRequestHandler_writeResponse().startCall() :
null;
CallCompletion processRequestCompletion = null;*/
try {
if (LOG.isDebugEnabled()) {
LOG.debug("Creating response for command [" + _dbusRequest.getId() + "] " + _dbusRequest.getName());
}
// Decide whether to close the connection or not.
boolean keepAlive = isKeepAlive(_httpRequest);
HttpResponse response = generateEmptyResponse();
if (LOG.isDebugEnabled()) {
//We are debugging -- let's add some more info to the response
response.addHeader(DatabusHttpHeaders.DATABUS_REQUEST_ID_HEADER, Long.toString(_dbusRequest.getId()));
}
// Write the response.
ChunkedBodyWritableByteChannel responseChannel = null;
try {
responseChannel = new ChunkedBodyWritableByteChannel(e.getChannel(), response);
_dbusRequest.setResponseContent(responseChannel);
if (LOG.isDebugEnabled()) {
LOG.debug("About to run command [" + _dbusRequest.getId() + "] " + _dbusRequest.getName());
}
//FIXME DDS-305: Rework the netty stats collector to use event-based stats aggregation
/*if (nettyStatsEnabled)
{
processRequestCompletion = nettyStats.getRequestHandler_processRequest().startCall();
}*/
Future<DatabusRequest> responseFuture = _processorRegistry.run(_dbusRequest);
ServerContainer.RuntimeConfig config = _dbusRequest.getConfig();
int timeoutMs = config.getRequestProcessingBudgetMs();
boolean done = responseFuture.isDone();
while (!done) {
try {
responseFuture.get(timeoutMs, TimeUnit.MILLISECONDS);
done = true;
ctx.setAttachment(_dbusRequest.getCursorPartition());
} catch (InterruptedException ie) {
done = responseFuture.isDone();
} catch (Exception ex) {
done = true;
_dbusRequest.setError(ex);
// On any error, clear any context saved. We will start afresh in a new request.
ctx.setAttachment(null);
//FIXME DDS-305: Rework the netty stats collector to use event-based stats aggregation
/*if (null != processRequestCompletion)
{
processRequestCompletion.endCallWithError(ex);
processRequestCompletion = null;
}*/
}
}
} finally {
if (null != responseChannel) {
if (LOG.isDebugEnabled()) {
//Add some more debugging info
long curTimeMs = System.currentTimeMillis();
responseChannel.addMetadata(DatabusHttpHeaders.DATABUS_REQUEST_LATENCY_HEADER, Long.toString(curTimeMs - _dbusRequest.getCreateTimestampMs()));
}
responseChannel.close();
}
if (null != _dbusRequest.getResponseThrowable()) {
ContainerStatisticsCollector statsCollector = _serverContainer.getContainerStatsCollector();
if (null != statsCollector) {
statsCollector.registerContainerError(_dbusRequest.getResponseThrowable());
}
}
}
if (LOG.isDebugEnabled()) {
LOG.debug("Done runing command [" + _dbusRequest.getId() + "] " + _dbusRequest.getName());
}
// Close the non-keep-alive or hard-failed connection after the write operation is done.
if (!keepAlive || null == responseChannel) {
e.getChannel().close();
}
//FIXME DDS-305: Rework the netty stats collector to use event-based stats aggregation
/*if (null != callCompletion)
{
callCompletion.endCall();
}*/
} catch (RuntimeException ex) {
LOG.error("HttpRequestHandler.writeResponse error", ex);
//FIXME DDS-305: Rework the netty stats collector to use event-based stats aggregation
/*if (null != callCompletion)
{
callCompletion.endCallWithError(ex);
}*/
ContainerStatisticsCollector statsCollector = _serverContainer.getContainerStatsCollector();
if (null != statsCollector)
statsCollector.registerContainerError(ex);
}
} else {
//Pass on everything else
ctx.sendUpstream(e);
}
}
use of com.linkedin.databus2.core.container.request.DatabusRequest in project databus by linkedin.
the class UnknownCommandProcessor method run.
public Future<DatabusRequest> run(DatabusRequest request) {
RequestProcessor processor = _processors.get(request.getName());
if (null == processor) {
processor = UNKOWN_COMMAND_PROCESSOR;
}
request.setProcessor(processor);
ExecutorService procExecutor = processor.getExecutorService();
if (null != procExecutor) {
return procExecutor.submit(request);
} else {
request.call();
return request;
}
}
use of com.linkedin.databus2.core.container.request.DatabusRequest in project databus by linkedin.
the class ContainerOperationProcessor method process.
@Override
public DatabusRequest process(DatabusRequest request) throws IOException, RequestProcessingException {
String action = request.getParams().getProperty(DatabusRequest.PATH_PARAM_NAME, "");
if (action.equals("shutdown")) {
String pid = Utils.getPid();
String response = "{\"Container\":\"set-shutdown\",\"pid\":\"" + pid + "\"}";
request.getResponseContent().write(ByteBuffer.wrap(response.getBytes(Charset.defaultCharset())));
Thread runThread = new Thread(new Runnable() {
@Override
public void run() {
_serverContainer.shutdown();
}
});
runThread.start();
} else if (action.equals("pause")) {
_serverContainer.pause();
request.getResponseContent().write(ByteBuffer.wrap("{\"Container\":\"set-pause\"}".getBytes(Charset.defaultCharset())));
} else if (action.equals("resume")) {
_serverContainer.resume();
request.getResponseContent().write(ByteBuffer.wrap("{\"Container\":\"set-resume\"}".getBytes(Charset.defaultCharset())));
} else if (action.equals("getpid")) {
String pid = Utils.getPid();
String response = "{\"pid\":\"" + pid + "\"}";
request.getResponseContent().write(ByteBuffer.wrap(response.getBytes(Charset.defaultCharset())));
} else {
throw new InvalidRequestParamValueException(COMMAND_NAME, "request path", action);
}
return request;
}
Aggregations