Package com.twitter.hbc.core.event

Examples of com.twitter.hbc.core.event.Event


      }
      rateTracker.start();
      while (!isDone()) {
        String host = hosts.nextHost();
        if (host == null) {
          setExitStatus(new Event(EventType.STOPPED_BY_ERROR, "No hosts available"));
          break;
        }

        double rate = rateTracker.getCurrentRateSeconds();
        if (!Double.isNaN(rate)) {
          endpoint.setBackfillCount(reconnectionManager.estimateBackfill(rate));
        }

        HttpUriRequest request = HttpConstants.constructRequest(host, endpoint, auth);
        if (request != null) {
          String postContent = null;
          if (endpoint.getHttpMethod().equalsIgnoreCase(HttpConstants.HTTP_POST)) {
            postContent = endpoint.getPostParamString();
          }
          auth.signRequest(request, postContent);
          Connection conn = new Connection(client, processor);
          StatusLine status = establishConnection(conn, request);
          if (handleConnectionResult(status)) {
            rateTracker.resume();
            processConnectionData(conn);
            rateTracker.pause();
          }
          logger.info("{} Done processing, preparing to close connection", name);
          conn.close();
        } else {
          addEvent(
            new Event(
              EventType.CONNECTION_ERROR,
              String.format("Error creating request: %s, %s, %s", endpoint.getHttpMethod(), host, endpoint.getURI())
            )
          );
        }
      }
    } catch (Throwable e) {
      logger.warn(name + " Uncaught exception", e);
      Exception laundered = (e instanceof Exception) ? (Exception) e : new RuntimeException(e);
      setExitStatus(new Event(EventType.STOPPED_BY_ERROR, laundered));
    } finally {
      rateTracker.stop();
      logger.info("{} Shutting down httpclient connection manager", name);
      client.getConnectionManager().shutdown();
      isRunning.countDown();
View Full Code Here


      addEvent(new ConnectionEvent(EventType.CONNECTION_ATTEMPT, request));
      status = conn.connect(request);
    } catch (UnknownHostException e) {
      // banking on some httpHosts.nextHost() being legitimate, or else this connection will fail.
      logger.warn("{} Unknown host - {}", name, request.getURI().getHost());
      addEvent(new Event(EventType.CONNECTION_ERROR, e));
    } catch (IOException e) {
      logger.warn("{} IOException caught when establishing connection to {}", name, request.getURI());
      addEvent(new Event(EventType.CONNECTION_ERROR, e));
      reconnectionManager.handleLinearBackoff();
    } catch (Exception e) {
      logger.error(String.format("%s Unknown exception while establishing connection to %s", name, request.getURI()), e);
      setExitStatus(new Event(EventType.STOPPED_BY_ERROR, e));
    }
    return status;
  }
View Full Code Here

  @VisibleForTesting
  boolean handleConnectionResult(@Nullable StatusLine statusLine) {
    statsReporter.incrNumConnects();
    if (statusLine == null) {
      logger.warn("{} failed to establish connection properly", name);
      addEvent(new Event(EventType.CONNECTION_ERROR, "Failed to establish connection properly"));
      return false;
    }
    int statusCode = statusLine.getStatusCode();
    if (statusCode == HttpConstants.Codes.SUCCESS) {
      logger.debug("{} Connection successfully established", name);
      statsReporter.incrNum200s();
      connectionEstablished.set(true);
      addEvent(new HttpResponseEvent(EventType.CONNECTED, statusLine));
      reconnectionManager.resetCounts();
      return true;
    }

    logger.warn(name + " Error connecting w/ status code - {}, reason - {}", statusCode, statusLine.getReasonPhrase());
    statsReporter.incrNumConnectionFailures();
    addEvent(new HttpResponseEvent(EventType.HTTP_ERROR, statusLine));
    if (HttpConstants.FATAL_CODES.contains(statusCode)) {
      setExitStatus(new Event(EventType.STOPPED_BY_ERROR, "Fatal error code: " + statusCode));
    } else if (statusCode < 500 && statusCode >= 400) {
      statsReporter.incrNum400s();
      // we will retry these a set number of times, then fail
      if (reconnectionManager.shouldReconnectOn400s()) {
        logger.debug("{} Reconnecting on {}", name, statusCode);
        reconnectionManager.handleExponentialBackoff();
      } else {
        logger.debug("{} Reconnecting retries exhausted for {}", name, statusCode);
        setExitStatus(new Event(EventType.STOPPED_BY_ERROR, "Retries exhausted"));
      }
    } else if (statusCode >= 500) {
      statsReporter.incrNum500s();
      reconnectionManager.handleExponentialBackoff();
    } else {
      setExitStatus(new Event(EventType.STOPPED_BY_ERROR, statusLine.getReasonPhrase()));
    }
    return false;
  }
View Full Code Here

  }

  private void processConnectionData(Connection conn) {
    logger.info("{} Processing connection data", name);
    try {
      addEvent(new Event(EventType.PROCESSING, "Processing messages"));
      while(!isDone() && !reconnect.getAndSet(false)) {
        if (conn.processResponse()) {
          statsReporter.incrNumMessages();
        } else {
          statsReporter.incrNumMessagesDropped();
        }
        rateTracker.eventObserved();
      }
    } catch (RuntimeException e) {
      logger.warn(name + " Unknown error processing connection: ", e);
      statsReporter.incrNumDisconnects();
      addEvent(new Event(EventType.DISCONNECTED, e));
    } catch (IOException ex) {
      // connection issue? whatever. let's try connecting again
      // we can't really diagnosis the actual disconnection reason without parsing (looking at disconnect message)
      // but we can make a good guess at when we're stalling. TODO
      logger.info("{} Disconnected during processing - will reconnect", name);
      statsReporter.incrNumDisconnects();
      addEvent(new Event(EventType.DISCONNECTED, ex));
    } catch (InterruptedException interrupt) {
      // interrupted while trying to append message to queue. exit
      logger.info("{} Thread interrupted during processing, exiting", name);
      statsReporter.incrNumDisconnects();
      setExitStatus(new Event(EventType.STOPPED_BY_ERROR, interrupt));
    } catch (Exception e) {
      // Unexpected exception thrown, killing everything
      logger.warn(name + " Unexpected exception during processing", e);
      statsReporter.incrNumDisconnects();
      setExitStatus(new Event(EventType.STOPPED_BY_ERROR, e));
    }
  }
View Full Code Here

   * Waits for the loop to end
   **/
  public void stop(int waitMillis) throws InterruptedException {
    try {
      if (!isDone()) {
        setExitStatus(new Event(EventType.STOPPED_BY_USER, String.format("Stopped by user: waiting for %d ms", waitMillis)));
      }
      if (!waitForFinish(waitMillis)) {
        logger.warn("{} Client thread failed to finish in {} millis", name, waitMillis);
      }
    } finally {
View Full Code Here

TOP

Related Classes of com.twitter.hbc.core.event.Event

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.