Search in sources :

Example 1 with ChunkedBodyWritableByteChannel

use of com.linkedin.databus2.core.container.netty.ChunkedBodyWritableByteChannel 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);
    }
}
Also used : HttpRequest(org.jboss.netty.handler.codec.http.HttpRequest) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse) DatabusRequest(com.linkedin.databus2.core.container.request.DatabusRequest) ContainerStatisticsCollector(com.linkedin.databus2.core.container.monitoring.mbean.ContainerStatisticsCollector) PhysicalPartition(com.linkedin.databus.core.data_model.PhysicalPartition)

Example 2 with ChunkedBodyWritableByteChannel

use of com.linkedin.databus2.core.container.netty.ChunkedBodyWritableByteChannel in project databus by linkedin.

the class DummyHttpRequestHandler method messageReceived.

@Override
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
    //HttpRequest request = (HttpRequest) e.getMessage();
    HttpResponse response = new DefaultHttpResponse(HTTP_1_1, OK);
    response.setHeader(CONTENT_TYPE, "text/plain; charset=UTF-8");
    ChunkedBodyWritableByteChannel writeChannel = new ChunkedBodyWritableByteChannel(e.getChannel(), response);
    for (String key : _headers.keySet()) {
        writeChannel.setMetadata(key, _headers.get(key));
    }
    for (byte[] chunk : _responseChunks) {
        writeChannel.write(ByteBuffer.wrap(chunk));
    }
    writeChannel.setResponseCode(_responseCode);
    for (String key : _footers.keySet()) {
        writeChannel.setMetadata(key, _footers.get(key));
    }
    writeChannel.close();
}
Also used : DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) ChunkedBodyWritableByteChannel(com.linkedin.databus2.core.container.netty.ChunkedBodyWritableByteChannel) DefaultHttpResponse(org.jboss.netty.handler.codec.http.DefaultHttpResponse) HttpResponse(org.jboss.netty.handler.codec.http.HttpResponse)

Aggregations

DefaultHttpResponse (org.jboss.netty.handler.codec.http.DefaultHttpResponse)2 HttpResponse (org.jboss.netty.handler.codec.http.HttpResponse)2 PhysicalPartition (com.linkedin.databus.core.data_model.PhysicalPartition)1 ContainerStatisticsCollector (com.linkedin.databus2.core.container.monitoring.mbean.ContainerStatisticsCollector)1 ChunkedBodyWritableByteChannel (com.linkedin.databus2.core.container.netty.ChunkedBodyWritableByteChannel)1 DatabusRequest (com.linkedin.databus2.core.container.request.DatabusRequest)1 HttpRequest (org.jboss.netty.handler.codec.http.HttpRequest)1