/**
   * Triggers a query to retrieve the "child" ImageCopyService instances in FAILED or CANCELLED
   * state.
   *
   * @param current
   */
  private void checkFailedOrCancelledCount(final State current) {
    Operation.CompletionHandler handler =
        new Operation.CompletionHandler() {
          @Override
          public void handle(Operation completedOp, Throwable failure) {
            if (failure != null) {
              // The query failed to execute. This most likely means that the
              // host is in a bad state and if we re-issue the query it is likely
              // to fail again. Terminate and fail the task early and delegate any
              // retry logic to the caller.
              failTask(failure);
              return;
            }

            QueryTask rsp = completedOp.getBody(QueryTask.class);

            State s = buildPatch(current.taskInfo.stage, current.taskInfo.subStage, null);
            ServiceUtils.logInfo(
                ImageReplicatorService.this, "Failed %s", Utils.toJson(rsp.results.documentLinks));
            s.failedOrCanceledCopies = rsp.results.documentLinks.size();

            sendSelfPatch(s);
          }
        };

    QueryTask.QuerySpecification spec =
        QueryTaskUtils.buildChildServiceTaskStatusQuerySpec(
            this.getSelfLink(),
            ImageCopyService.State.class,
            TaskState.TaskStage.FAILED,
            TaskState.TaskStage.CANCELLED);

    this.sendQuery(spec, handler);
  }