@Override
  public CommandProcessorResponse run() throws CommandProcessorException {
    executionIndex = 0;
    int maxExecutions = 1 + coreDriver.getConf().getIntVar(ConfVars.HIVE_QUERY_MAX_REEXECUTION_COUNT);

    while (true) {
      executionIndex++;

      for (IReExecutionPlugin p : plugins) {
        p.beforeExecute(executionIndex, explainReOptimization);
      }
      coreDriver.getContext().setExecutionIndex(executionIndex);
      LOG.info("Execution #{} of query", executionIndex);
      CommandProcessorResponse cpr = null;
      CommandProcessorException cpe = null;
      try {
        cpr = coreDriver.run();
      } catch (CommandProcessorException e) {
        cpe = e;
      }

      PlanMapper oldPlanMapper = coreDriver.getPlanMapper();
      boolean success = cpr != null;
      plugins.forEach(p -> p.afterExecute(oldPlanMapper, success));

      boolean shouldReExecute = explainReOptimization && executionIndex==1;
      shouldReExecute |= cpr == null && plugins.stream().anyMatch(p -> p.shouldReExecute(executionIndex));

      LOG.info("Re-execution decision is made according to: executionIndex: {}, maxExecutions: {}, shouldReExecute: {}",
          executionIndex, maxExecutions, shouldReExecute);
      if (executionIndex >= maxExecutions || !shouldReExecute) {
        if (cpr != null) {
          return cpr;
        } else {
          throw cpe;
        }
      }
      LOG.info("Preparing to re-execute query");
      plugins.forEach(IReExecutionPlugin::prepareToReExecute);

      try {
        coreDriver.compileAndRespond(currentQuery);
      } catch (CommandProcessorException e) {
        LOG.error("Recompilation of the query failed; this is unexpected.");
        // FIXME: somehow place pointers that re-execution compilation have failed; the query have been successfully compiled before?
        throw e;
      }

      PlanMapper newPlanMapper = coreDriver.getPlanMapper();
      if (!explainReOptimization &&
          !plugins.stream().anyMatch(p -> p.shouldReExecuteAfterCompile(executionIndex, oldPlanMapper, newPlanMapper))) {
        LOG.info("re-running the query would probably not yield better results; returning with last error");
        // FIXME: retain old error; or create a new one?
        return cpr;
      }
    }
  }
Enables to use re-execution logics.

Covers the IDriver interface, handles query re-execution; and asks clear questions from the underlying re-execution plugins.
ql/src/java/org/apache/hadoop/hive/ql/reexec/ReExecDriver.java