/** {@inheritDoc} */
  @Override
  protected Collection<E> dequeue0(int cnt) {
    WindowHolder tup = ref.get();

    AtomicInteger size = tup.size();
    Collection<T> evts = tup.collection();

    Collection<E> resCol = new ArrayList<>(cnt);

    while (true) {
      int curSize = size.get();

      if (curSize > 0) {
        if (size.compareAndSet(curSize, curSize - 1)) {
          E res = pollInternal(evts, tup.set());

          if (res != null) {
            resCol.add(res);

            if (resCol.size() >= cnt) return resCol;
          } else {
            size.incrementAndGet();

            return resCol;
          }
        }
      } else return resCol;
    }
  }
  /**
   * Poll evicted internal implementation.
   *
   * @return Evicted element.
   */
  @Nullable
  private E pollEvictedInternal() {
    WindowHolder tup = ref.get();

    AtomicInteger size = tup.size();

    while (true) {
      int curSize = size.get();

      if (curSize > maxSize) {
        if (size.compareAndSet(curSize, curSize - 1)) {
          E evt = pollInternal(tup.collection(), tup.set());

          if (evt != null) return evt;
          else {
            // No actual events in queue, it means that other thread is just adding event.
            // return null as it is a concurrent add call.
            size.incrementAndGet();

            return null;
          }
        }
      } else return null;
    }
  }
Exemple #3
0
  /**
   * Tests that failover don't pick local node if it has been excluded from topology.
   *
   * @throws Exception If failed.
   */
  @SuppressWarnings({"WaitNotInLoop", "UnconditionalWait", "unchecked"})
  public void testFailoverTopology() throws Exception {
    try {
      Grid grid1 = startGrid(1);
      Grid grid2 = startGrid(2);

      assert grid1 != null;
      assert grid2 != null;

      grid1.compute().localDeployTask(JobTask.class, JobTask.class.getClassLoader());

      try {
        GridComputeTaskFuture<String> fut;

        synchronized (mux) {
          fut = grid1.compute().execute(JobTask.class, null);

          mux.wait();
        }

        stopAndCancelGrid(2);

        String res = fut.get();

        info("Task result: " + res);
      } catch (GridException e) {
        info("Got unexpected grid exception: " + e);
      }

      info("Failed over: " + failCnt.get());

      assert failCnt.get() == 1
          : "Invalid fail over counter [expected=1, actual=" + failCnt.get() + ']';
    } finally {
      stopGrid(1);

      // Stopping stopped instance just in case.
      stopGrid(2);
    }
  }
  /** {@inheritDoc} */
  @Override
  public void onCollision(
      Collection<GridCollisionJobContext> waitJobs,
      Collection<GridCollisionJobContext> activeJobs) {
    assert waitJobs != null;
    assert activeJobs != null;

    int activeSize = F.size(activeJobs, RUNNING_JOBS);

    waitingCnt.set(waitJobs.size());
    runningCnt.set(activeSize);
    heldCnt.set(activeJobs.size() - activeSize);

    int waitSize = waitJobs.size();

    int activateCnt = parallelJobsNum - activeSize;

    if (activateCnt > 0 && !waitJobs.isEmpty()) {
      if (waitJobs.size() <= activateCnt) {
        for (GridCollisionJobContext waitJob : waitJobs) {
          waitJob.activate();

          waitSize--;
        }
      } else {
        List<GridCollisionJobContext> passiveList =
            new ArrayList<GridCollisionJobContext>(waitJobs);

        Collections.sort(
            passiveList,
            new Comparator<GridCollisionJobContext>() {
              /** {@inheritDoc} */
              @Override
              public int compare(GridCollisionJobContext o1, GridCollisionJobContext o2) {
                int p1 = getJobPriority(o1);
                int p2 = getJobPriority(o2);

                return p1 < p2 ? 1 : p1 == p2 ? 0 : -1;
              }
            });

        if (preventStarvation) bumpPriority(waitJobs, passiveList);

        for (int i = 0; i < activateCnt; i++) {
          passiveList.get(i).activate();

          waitSize--;
        }
      }
    }

    if (waitSize > waitJobsNum) {
      List<GridCollisionJobContext> waitList = new ArrayList<GridCollisionJobContext>(waitJobs);

      // Put jobs with highest priority first.
      Collections.sort(
          waitList,
          new Comparator<GridCollisionJobContext>() {
            /** {@inheritDoc} */
            @Override
            public int compare(GridCollisionJobContext o1, GridCollisionJobContext o2) {
              int p1 = getJobPriority(o1);
              int p2 = getJobPriority(o2);

              return p1 < p2 ? 1 : p1 == p2 ? 0 : -1;
            }
          });

      int skip = waitJobs.size() - waitSize;

      int i = 0;

      for (GridCollisionJobContext waitCtx : waitList) {
        if (++i >= skip) {
          waitCtx.cancel();

          if (--waitSize <= waitJobsNum) break;
        }
      }
    }
  }
 /** {@inheritDoc} */
 @Override
 public int getCurrentHeldJobsNumber() {
   return heldCnt.get();
 }
 /** {@inheritDoc} */
 @Override
 public int getCurrentRunningJobsNumber() {
   return runningCnt.get();
 }
 /** {@inheritDoc} */
 @Override
 public int getCurrentActiveJobsNumber() {
   return runningCnt.get() + heldCnt.get();
 }
 /** {@inheritDoc} */
 @Override
 public int getCurrentWaitJobsNumber() {
   return waitingCnt.get();
 }