Exemplo n.º 1
0
  private boolean shouldReschedule(SingularityRequest newRequest, SingularityRequest oldRequest) {
    if (newRequest.getInstancesSafe() != oldRequest.getInstancesSafe()) {
      return true;
    }
    if (newRequest.isScheduled() && oldRequest.isScheduled()) {
      if (!newRequest.getQuartzScheduleSafe().equals(oldRequest.getQuartzScheduleSafe())) {
        return true;
      }
    }

    return false;
  }
  private int getNumMissingInstances(
      List<SingularityTaskId> matchingTaskIds,
      SingularityRequest request,
      SingularityPendingRequest pendingRequest) {
    if (request.isOneOff() && pendingRequest.getPendingType() == PendingType.ONEOFF) {
      return 1;
    }

    final int numInstances = request.getInstancesSafe();

    return numInstances - matchingTaskIds.size();
  }
  private int scheduleTasks(
      SingularitySchedulerStateCache stateCache,
      SingularityRequest request,
      RequestState state,
      SingularityDeployStatistics deployStatistics,
      SingularityPendingRequest pendingRequest,
      List<SingularityTaskId> matchingTaskIds) {
    deleteScheduledTasks(stateCache.getScheduledTasks(), pendingRequest);

    final int numMissingInstances =
        getNumMissingInstances(matchingTaskIds, request, pendingRequest);

    LOG.debug(
        "Missing {} instances of request {} (matching tasks: {}), pending request: {}",
        numMissingInstances,
        request.getId(),
        matchingTaskIds,
        pendingRequest);

    if (numMissingInstances > 0) {
      final List<SingularityPendingTask> scheduledTasks =
          getScheduledTaskIds(
              numMissingInstances,
              matchingTaskIds,
              request,
              state,
              deployStatistics,
              pendingRequest.getDeployId(),
              pendingRequest);

      if (!scheduledTasks.isEmpty()) {
        LOG.trace("Scheduling tasks: {}", scheduledTasks);

        for (SingularityPendingTask scheduledTask : scheduledTasks) {
          taskManager.savePendingTask(scheduledTask);
        }
      } else {
        LOG.info(
            "No new scheduled tasks found for {}, setting state to {}",
            request.getId(),
            RequestState.FINISHED);
        requestManager.finish(request, System.currentTimeMillis());
      }
    } else if (numMissingInstances < 0) {
      final long now = System.currentTimeMillis();

      Collections.sort(
          matchingTaskIds,
          Collections.reverseOrder(
              SingularityTaskId.INSTANCE_NO_COMPARATOR)); // clean the highest numbers

      for (int i = 0; i < Math.abs(numMissingInstances); i++) {
        final SingularityTaskId toCleanup = matchingTaskIds.get(i);

        LOG.info(
            "Cleaning up task {} due to new request {} - scaling down to {} instances",
            toCleanup.getId(),
            request.getId(),
            request.getInstancesSafe());

        taskManager.createTaskCleanup(
            new SingularityTaskCleanup(
                pendingRequest.getUser(),
                TaskCleanupType.SCALING_DOWN,
                now,
                toCleanup,
                Optional.<String>absent()));
      }
    }

    return numMissingInstances;
  }