// Copyright 2020 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.exec;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.flogger.GoogleLogger;
import com.google.devtools.build.lib.actions.ActionContext;
import com.google.devtools.build.lib.actions.ActionExecutionContext;
import com.google.devtools.build.lib.actions.ExecException;
import com.google.devtools.build.lib.actions.Spawn;
import com.google.devtools.build.lib.actions.SpawnContinuation;
import com.google.devtools.build.lib.actions.SpawnResult;
import com.google.devtools.build.lib.actions.SpawnStrategy;
import com.google.devtools.build.lib.actions.UserExecException;
import com.google.devtools.build.lib.server.FailureDetails;
import com.google.devtools.build.lib.server.FailureDetails.FailureDetail;
import com.google.devtools.build.lib.server.FailureDetails.Spawn.Code;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * Resolver that looks up the right strategy for a spawn during {@link #exec} (via a {@link
 * SpawnStrategyRegistry}) and uses it to execute the spawn.
 */
public final class SpawnStrategyResolver implements ActionContext {
  private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();

  /**
   * Executes the given spawn with the {@linkplain SpawnStrategyRegistry highest priority strategy}
   * that can be found for it.
   *
   * @param actionExecutionContext context in which to execute the spawn
   * @return result(s) from the spawn's execution
   */
  public ImmutableList<SpawnResult> exec(Spawn spawn, ActionExecutionContext actionExecutionContext)
      throws ExecException, InterruptedException {
    return resolveOne(spawn, actionExecutionContext).exec(spawn, actionExecutionContext);
  }

  /**
   * Queues execution of the given spawn with the {@linkplain SpawnStrategyRegistry highest priority
   * strategy} that can be found for it.
   *
   * @param actionExecutionContext context in which to execute the spawn
   * @return handle to the spawn's pending execution (or failure thereof)
   */
  public SpawnContinuation beginExecution(
      Spawn spawn, ActionExecutionContext actionExecutionContext) throws InterruptedException {
    SpawnStrategy resolvedStrategy;
    try {
      resolvedStrategy = resolveOne(spawn, actionExecutionContext);
    } catch (ExecException e) {
      return SpawnContinuation.failedWithExecException(e);
    }
    return resolvedStrategy.beginExecution(spawn, actionExecutionContext);
  }

  private SpawnStrategy resolveOne(Spawn spawn, ActionExecutionContext actionExecutionContext)
      throws UserExecException {
    List<? extends SpawnStrategy> strategies = resolve(spawn, actionExecutionContext);

    // Because the strategies are ordered by preference, we can execute the spawn with the best
    // possible one by simply filtering out the ones that can't execute it and then picking the
    // first one from the remaining strategies in the list.
    return strategies.get(0);
  }

  /**
   * Returns the list of {@link SpawnStrategy}s that should be used to execute the given spawn.
   *
   * @param spawn spawn for which the correct {@link SpawnStrategy} should be determined
   */
  @VisibleForTesting
  public List<? extends SpawnStrategy> resolve(
      Spawn spawn, ActionExecutionContext actionExecutionContext) throws UserExecException {
    List<? extends SpawnStrategy> strategies =
        actionExecutionContext
            .getContext(SpawnStrategyRegistry.class)
            .getStrategies(spawn, actionExecutionContext.getEventHandler());

    List<? extends SpawnStrategy> execableStrategies =
        strategies.stream()
            .filter(spawnActionContext -> spawnActionContext.canExec(spawn, actionExecutionContext))
            .collect(Collectors.toList());

    if (execableStrategies.isEmpty()) {
      // Legacy implicit fallbacks should be a last-ditch option after all other strategies are
      // found non-executable.
      List<? extends SpawnStrategy> fallbackStrategies =
          strategies.stream()
              .filter(
                  spawnActionContext ->
                      spawnActionContext.canExecWithLegacyFallback(spawn, actionExecutionContext))
              .collect(Collectors.toList());

      if (fallbackStrategies.isEmpty()) {
        String message =
            String.format(
                "%s spawn cannot be executed with any of the available strategies: %s. Your"
                    + " --spawn_strategy, --genrule_strategy and/or --strategy flags are probably"
                    + " too strict. Visit https://github.com/bazelbuild/bazel/issues/7480 for"
                    + " advice",
                spawn.getMnemonic(), strategies);
        throw new UserExecException(
            FailureDetail.newBuilder()
                .setMessage(message)
                .setSpawn(FailureDetails.Spawn.newBuilder().setCode(Code.NO_USABLE_STRATEGY_FOUND))
                .build());
      } else {
        // Extra logging to debug b/194373457
        logger.atInfo().atMostEvery(1, TimeUnit.SECONDS).log(
            "Spawn %s resolved with fallback to strategies %s",
            spawn.getResourceOwner().describe(), fallbackStrategies);
      }
      return fallbackStrategies;
    } else {
      // Extra logging to debug b/194373457
      logger.atInfo().atMostEvery(1, TimeUnit.SECONDS).log(
          "Spawn %s resolved to strategies %s",
          spawn.getResourceOwner().describe(), execableStrategies);
    }

    return execableStrategies;
  }
}

@@ -761,55 +761,8 @@
 st;%0A
-import com.google.common.flogger.GoogleLogger;%0A
 impo
@@ -1476,46 +1476,8 @@
 st;%0A
-import java.util.concurrent.TimeUnit;%0A
 impo
@@ -1742,88 +1742,8 @@
 t %7B%0A
-  private static final GoogleLogger logger = GoogleLogger.forEnclosingClass();%0A%0A
   /*
@@ -5473,530 +5473,40 @@
    %7D
- else %7B%0A        // Extra logging to debug b/194373457%0A        logger.atInfo().atMostEvery(1, TimeUnit.SECONDS).log(%0A            %22Spawn %25s resolved with fallback to strategies %25s%22,%0A            spawn.getResourceOwner().describe(), fallbackStrategies);%0A      %7D%0A      return fallbackStrategies;%0A    %7D else %7B%0A      // Extra logging to debug b/194373457%0A      logger.atInfo().atMostEvery(1, TimeUnit.SECONDS).log(%0A          %22Spawn %25s resolved to strategies %25s%22,%0A          spawn.getResourceOwner().describe(), execableStrategies)
+%0A      return fallbackStrategies
 ;%0A  

Remove spammy logging after bug has been resolved.