コード例 #1
0
ファイル: Monitor.java プロジェクト: paxtonhare/xqsync
  public void checkThrottle() {
    // optional throttling
    if (!config.isThrottled()) {
      return;
    }

    long sleepMillis;
    double throttledEventsPerSecond = config.getThrottledEventsPerSecond();
    boolean isEvents = (throttledEventsPerSecond > 0);
    int throttledBytesPerSecond = isEvents ? 0 : config.getThrottledBytesPerSecond();
    logger.fine(
        "throttling "
            + (isEvents
                // events
                ? (timer.getEventsPerSecond() + " tps to " + throttledEventsPerSecond + " tps")
                // bytes
                : (timer.getBytesPerSecond() + " B/sec to " + throttledBytesPerSecond + " B/sec")));
    // call the methods every time
    while ((throttledEventsPerSecond > 0 && (throttledEventsPerSecond < timer.getEventsPerSecond()))
        || (throttledBytesPerSecond > 0 && (throttledBytesPerSecond < timer.getBytesPerSecond()))) {
      if (isEvents) {
        sleepMillis =
            (long)
                Math.ceil(
                    Timer.MILLISECONDS_PER_SECOND
                        * ((timer.getEventCount() / throttledEventsPerSecond)
                            - timer.getDurationSeconds()));
      } else {
        sleepMillis =
            (long)
                Math.ceil(
                    Timer.MILLISECONDS_PER_SECOND
                        * ((timer.getBytes() / throttledBytesPerSecond)
                            - timer.getDurationSeconds()));
      }
      sleepMillis = Math.max(sleepMillis, 1);
      logger.finer("sleeping " + sleepMillis);
      try {
        Thread.sleep(sleepMillis);
      } catch (InterruptedException e) {
        // reset interrupt status and continue
        Thread.interrupted();
        logger.logException("interrupted", e);
      }
    }
    logger.fine(
        "throttled to "
            + (isEvents
                ? (timer.getEventsPerSecond() + " tps")
                : (timer.getBytesPerSecond() + " B/sec")));
  }
コード例 #2
0
ファイル: Monitor.java プロジェクト: paxtonhare/xqsync
  /** @throws ExecutionException */
  protected void monitor() throws ExecutionException {
    int displayMillis = DISPLAY_MILLIS;
    int futureMillis = FUTURE_MILLIS;
    int sleepMillis = SLEEP_MILLIS;
    Future<TimedEvent[]> future = null;
    /* Initialize lastFutureMillis so that we do not get
     * warnings on slow queue startup.
     */
    long currentMillis = System.currentTimeMillis();
    long lastDisplayMillis = 0;
    long lastFutureMillis = currentMillis;
    TimedEvent[] lastEvent = null;

    logger.finest(
        "looping every "
            + sleepMillis
            + ", core="
            + pool.getCorePoolSize()
            + ", active="
            + pool.getActiveCount()
            + ", tasks="
            + taskCount);

    timer = new Timer();

    // run until all futures have been checked
    do {
      // try to avoid thread starvation
      yield();

      // check completed tasks
      // sometimes this goes so fast that we never leave the loop,
      // so progress is never displayed... so limit the number of loops.
      do {
        try {
          future = completionService.poll(SLEEP_MILLIS, TimeUnit.MILLISECONDS);
          if (null != future) {
            // record result, or throw exception
            lastFutureMillis = System.currentTimeMillis();
            try {
              lastEvent = future.get();
              if (null == lastEvent) {
                throw new FatalException("unexpected null event");
              }
              for (int i = 0; i < lastEvent.length; i++) {
                // discard events to reduce memory utilization
                if (null != lastEvent[i]) {
                  timer.add(lastEvent[i], false);
                }
              }
            } catch (ExecutionException e) {
              if (fatalErrors) {
                throw e;
              }
              Throwable cause = e.getCause();
              if (null != cause && cause instanceof FatalException) {
                throw (FatalException) cause;
              }
              logger.logException("non-fatal", e);
              timer.incrementEventCount(false);
            }
          }
        } catch (InterruptedException e) {
          // reset interrupt status and continue
          Thread.interrupted();
          logger.logException("interrupted in poll() or get()", e);
          continue;
        }

        currentMillis = System.currentTimeMillis();
        if (currentMillis - lastDisplayMillis > displayMillis) {
          lastDisplayMillis = currentMillis;
          logger.finer(
              "thread count: core="
                  + pool.getCorePoolSize()
                  + ", active="
                  + pool.getActiveCount()
                  + ", tasks="
                  + taskCount);
          if (null != lastEvent) {
            logger.info(
                ""
                    + timer.getEventCount()
                    + "/"
                    + taskCount
                    + ", "
                    + timer.getProgressMessage(false)
                    + ", "
                    + lastEvent[0].getDescription());

            if (config.doPrintCurrRate()) {
              String currMsg = timer.getCurrProgressMessage();
              if (currMsg != null) logger.info(currMsg);
            }
          }
        }

      } while (null != future);

      logger.finer(
          "running = "
              + running
              + ", terminated = "
              + pool.isTerminated()
              + ", last future = "
              + lastFutureMillis);
      // currentMillis has already been set recently
      if (currentMillis - lastFutureMillis > futureMillis) {
        logger.warning("no futures received in over " + futureMillis + " ms");
        break;
      }
    } while (running && !pool.isTerminated());
    // NB - caller will set running to false to ensure exit
  }