Ejemplo n.º 1
0
 private void updateState(long elapsed) {
   // Update the job actors
   List<GridEvent> events = getNextSlice(elapsed);
   if (!events.isEmpty()) {
     log.debug("Applying {} events", events.size());
     for (GridEvent event : events) {
       applyEvent(event);
     }
   }
 }
Ejemplo n.º 2
0
  private List<GridEvent> getNextSlice(long elapsed) {

    List<GridEvent> slice = new ArrayList<GridEvent>();
    if (elapsed <= 0) return slice;

    this.totalElapsed += elapsed;

    log.trace("getNextSlice, prevElapsed={}, totalElapsed={}", prevElapsed, totalElapsed);

    if (prevElapsed >= totalElapsed) {
      log.warn(
          "No slice possible with (prevElapsed={}, totalElapsed={})", prevElapsed, totalElapsed);
      return slice;
    }

    long start = prevElapsed;
    long end = totalElapsed;

    SortedMap<Long, List<Event>> eventSlice = timeline.getEvents(start, end);

    if (!eventSlice.isEmpty()) {
      // We only move the start of the window up when we find an event. This done because the
      // database might
      // have gaps if the incoming events cannot be processed in real-time. In that case, we don't
      // want to
      // miss any events if they come late.
      this.prevElapsed = totalElapsed;

      log.trace("Timeline has {} offset buckets", timeline.getNumOffsets());
      log.info(
          "Requested slice where {}<=t<{} and got " + eventSlice.size() + " buckets", start, end);
    }

    if (!eventSlice.isEmpty()) {
      for (Long offset : eventSlice.keySet()) {
        log.trace("Got offset bucket {}", offset);
        if (offset >= totalElapsed) {
          log.warn(
              "Timeline returned grid events outside the requested frame: {}>{}",
              offset,
              totalElapsed);
          break;
        }
        List<Event> events = eventSlice.get(offset);
        synchronized (events) {
          if (events.isEmpty()) {
            log.trace("Got empty offset bucket for offset {}", offset);
          }
          for (Event event : events) {
            if (event instanceof GridEvent) {
              GridEvent gridEvent = (GridEvent) event;
              log.trace("Got grid event {}", gridEvent);
              slice.add(gridEvent);
            } else if (event instanceof SnapshotEvent) {
              SnapshotEvent gridEvent = (SnapshotEvent) event;
              log.trace("Got snapshot event {}", gridEvent);
            } else {
              log.trace("Got unrecognized event {}", event);
            }
          }
        }
      }
    }

    return slice;
  }