@Override
 protected void doGet(HttpServletRequest req, HttpServletResponse resp)
     throws ServletException, IOException {
   try {
     System.out.println("********************************* : debug");
     resp.getOutputStream().print(broker.getDebugInfo());
     resp.getOutputStream().flush();
     resp.getOutputStream().close();
   } catch (final Exception e) {
     resp.getOutputStream().print(e.getMessage());
     resp.getOutputStream().flush();
     resp.getOutputStream().close();
     LOGGER.error("Caught exception while processing GET request", e);
     brokerMetrics.addMeteredValue(null, BrokerMeter.UNCAUGHT_GET_EXCEPTIONS, 1);
   }
 }
Beispiel #2
0
    private void runInternal() {
      if (_isCancelled.get()) {
        LOGGER.error(
            "Request {} to server {} cancelled even before request is sent !! Not sending request",
            _request.getRequestId(),
            _server);
        _requestDispatchLatch.countDown();
        return;
      }

      NettyClientConnection conn = null;
      boolean gotConnection = false;
      boolean error = true;
      try {
        KeyedFuture<ServerInstance, NettyClientConnection> c = _connPool.checkoutObject(_server);

        byte[] serializedRequest = _request.getRequestForService(_server, _segmentIds);
        long timeRemaining = _timeoutMS - (System.currentTimeMillis() - _startTime);
        int ntries = 0;
        // Try a maximum of pool size objects.
        while (true) {
          if (timeRemaining <= 0) {
            c.cancel(true);
            throw new TimeoutException(
                "Timed out trying to connect to "
                    + _server
                    + "(timeout="
                    + _timeoutMS
                    + "ms,ntries="
                    + ntries
                    + ")");
          }
          conn = c.getOne(timeRemaining, TimeUnit.MILLISECONDS);
          // conn may be null if we cannot get any connection from the pool. This condition can
          // happen either
          // due to a timeout (server host is switched off, or cable disconnected), or due to
          // immediate connection refusal
          // (host is up, but server JVM is not running, or not up yet). It will also get a null
          // when the AsyncPoolImpl.create()
          // is not able to create a connection, and this one was a waiting request.
          // In either case, there is no point in retrying  this request
          if (conn != null && conn.validate()) {
            gotConnection = true;
            break;
          }
          // If we get a null error map, then it is most likely a case of "Connection Refused" from
          // remote.
          // The connect errors are obtained from two different objects -- 'conn' and 'c'.
          // We pick the error from 'c' here, if we find it. Unfortunately there is not a way (now)
          // to pass the
          // error from 'c' to 'conn' (need to do it via AsyncPoolImpl)
          Map<ServerInstance, Throwable> errorMap = c.getError();
          String errStr = "";
          if (errorMap != null && errorMap.containsKey(_server)) {
            errStr = errorMap.get(_server).getMessage();
          }
          LOGGER.warn("Destroying invalid conn {}:{}", conn, errStr);
          if (conn != null) {
            _connPool.destroyObject(_server, conn);
          }
          if (++ntries == MAX_CONN_RETRIES - 1) {
            throw new RuntimeException(
                "Could not connect to "
                    + _server
                    + " after "
                    + ntries
                    + "attempts(timeRemaining="
                    + timeRemaining
                    + "ms)");
          }
          c = _connPool.checkoutObject(_server);
          timeRemaining = _timeoutMS - (System.currentTimeMillis() - _startTime);
        }
        ByteBuf req = Unpooled.wrappedBuffer(serializedRequest);
        _responseFuture = conn.sendRequest(req, _request.getRequestId(), timeRemaining);
        _isSent.set(true);
        LOGGER.debug("Response Future is : {}", _responseFuture);
        error = false;
      } catch (TimeoutException e1) {
        LOGGER.error(
            "Timed out waiting for connection for server ({})({})(gotConnection={}). Setting error future",
            _server,
            _request.getRequestId(),
            gotConnection,
            e1);
        _responseFuture =
            new ResponseFuture(_server, e1, "Error Future for request " + _request.getRequestId());
      } catch (Exception e) {
        LOGGER.error(
            "Got exception sending request ({})(gotConnection={}). Setting error future",
            _request.getRequestId(),
            gotConnection,
            e);
        _responseFuture =
            new ResponseFuture(_server, e, "Error Future for request " + _request.getRequestId());
      } finally {
        _requestDispatchLatch.countDown();
        BrokerRequest brokerRequest = (BrokerRequest) _request.getBrokerRequest();
        if (error) {
          if (gotConnection) {
            _brokerMetrics.addMeteredValue(
                brokerRequest, BrokerMeter.REQUEST_DROPPED_DUE_TO_SEND_ERROR, 1);
          } else {
            _brokerMetrics.addMeteredValue(
                brokerRequest, BrokerMeter.REQUEST_DROPPED_DUE_TO_CONNECTION_ERROR, 1);
          }
        }
      }
    }
Beispiel #3
0
  public void buildNetwork() throws ConfigurationException {
    // build transport
    Configuration transportConfigs = _config.subset(TRANSPORT_CONFIG_PREFIX);
    TransportClientConf conf = new TransportClientConf();
    conf.init(transportConfigs);

    _registry = new MetricsRegistry();
    MetricsHelper.initializeMetrics(_config.subset(METRICS_CONFIG_PREFIX));
    MetricsHelper.registerMetricsRegistry(_registry);
    _brokerMetrics = new BrokerMetrics(_registry);
    _brokerMetrics.initializeGlobalMeters();
    _state.set(State.INIT);
    _eventLoopGroup = new NioEventLoopGroup();
    /**
     * Some of the client metrics uses histogram which is doing synchronous operation. These are
     * fixed overhead per request/response. TODO: Measure the overhead of this.
     */
    final NettyClientMetrics clientMetrics = new NettyClientMetrics(_registry, "client_");

    // Setup Netty Connection Pool
    _resourceManager =
        new PooledNettyClientResourceManager(
            _eventLoopGroup, new HashedWheelTimer(), clientMetrics);
    _poolTimeoutExecutor = new ScheduledThreadPoolExecutor(50);
    // _requestSenderPool = MoreExecutors.sameThreadExecutor();
    final ConnectionPoolConfig cfg = conf.getConnPool();

    _requestSenderPool = Executors.newCachedThreadPool();

    ConnectionPoolConfig connPoolCfg = conf.getConnPool();

    _connPool =
        new KeyedPoolImpl<ServerInstance, NettyClientConnection>(
            connPoolCfg.getMinConnectionsPerServer(),
            connPoolCfg.getMaxConnectionsPerServer(),
            connPoolCfg.getIdleTimeoutMs(),
            connPoolCfg.getMaxBacklogPerServer(),
            _resourceManager,
            _poolTimeoutExecutor,
            _requestSenderPool,
            _registry);
    // MoreExecutors.sameThreadExecutor(), _registry);
    _resourceManager.setPool(_connPool);

    // Setup Routing Table
    if (conf.getRoutingMode() == RoutingMode.CONFIG) {
      final CfgBasedRouting rt = new CfgBasedRouting();
      rt.init(conf.getCfgBasedRouting());
      _routingTable = rt;
    } else {
      // Helix based routing is already initialized.
    }

    // Setup ScatterGather
    _scatterGather = new ScatterGatherImpl(_connPool, _requestSenderPool);

    // Setup Broker Request Handler
    long brokerTimeOut = DEFAULT_BROKER_TIME_OUT;
    if (_config.containsKey(BROKER_TIME_OUT_CONFIG)) {
      try {
        brokerTimeOut = _config.getLong(BROKER_TIME_OUT_CONFIG);
      } catch (Exception e) {
        LOGGER.warn(
            "Caught exception while reading broker timeout from config, using default value", e);
      }
    }
    LOGGER.info("Broker timeout is - " + brokerTimeOut + " ms");

    _requestHandler =
        new BrokerRequestHandler(
            _routingTable,
            _timeBoundaryService,
            _scatterGather,
            new DefaultReduceService(),
            _brokerMetrics,
            brokerTimeOut);

    // TODO: Start Broker Server : Code goes here. Broker Server part should use request handler to
    // submit requests

    LOGGER.info("Network initialized !!");
  }