<commit_before>/*
 * Copyright 2016-present Facebook, Inc.
 *
 * 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.facebook.buck.cli;

import com.facebook.buck.distributed.BuildJobStateSerializer;
import com.facebook.buck.distributed.DistBuildConfig;
import com.facebook.buck.distributed.DistBuildMode;
import com.facebook.buck.distributed.DistBuildRunEvent;
import com.facebook.buck.distributed.DistBuildService;
import com.facebook.buck.distributed.DistBuildState;
import com.facebook.buck.distributed.FileContentsProvider;
import com.facebook.buck.distributed.FileMaterializationStatsTracker;
import com.facebook.buck.distributed.build_slave.BuildSlaveTimingStatsTracker;
import com.facebook.buck.distributed.build_slave.BuildSlaveTimingStatsTracker.SlaveEvents;
import com.facebook.buck.distributed.build_slave.CoordinatorBuildRuleEventsPublisher;
import com.facebook.buck.distributed.build_slave.DistBuildSlaveExecutor;
import com.facebook.buck.distributed.build_slave.HealthCheckStatsTracker;
import com.facebook.buck.distributed.build_slave.MinionBuildProgressTracker;
import com.facebook.buck.distributed.build_slave.NoOpMinionBuildProgressTracker;
import com.facebook.buck.distributed.thrift.BuildJobState;
import com.facebook.buck.distributed.thrift.BuildSlaveRunId;
import com.facebook.buck.distributed.thrift.StampedeId;
import com.facebook.buck.event.BuckEventListener;
import com.facebook.buck.event.ConsoleEvent;
import com.facebook.buck.event.listener.DistBuildSlaveEventBusListener;
import com.facebook.buck.event.listener.NoOpCoordinatorBuildRuleEventsPublisher;
import com.facebook.buck.io.filesystem.ProjectFilesystem;
import com.facebook.buck.log.Logger;
import com.facebook.buck.rules.RuleKey;
import com.facebook.buck.rules.keys.DefaultRuleKeyCache;
import com.facebook.buck.rules.keys.EventPostingRuleKeyCacheScope;
import com.facebook.buck.rules.keys.RuleKeyCacheScope;
import com.facebook.buck.rules.keys.TrackedRuleKeyCache;
import com.facebook.buck.step.ExecutorPool;
import com.facebook.buck.util.Console;
import com.facebook.buck.util.ExitCode;
import com.facebook.buck.util.HumanReadableException;
import com.facebook.buck.util.cache.InstrumentingCacheStatsTracker;
import com.facebook.buck.util.concurrent.ConcurrencyLimit;
import com.facebook.buck.util.timing.DefaultClock;
import com.facebook.buck.util.types.Pair;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ListeningExecutorService;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ScheduledExecutorService;
import javax.annotation.Nullable;
import org.kohsuke.args4j.Option;

public class DistBuildRunCommand extends AbstractDistBuildCommand {
  private static final Logger LOG = Logger.get(DistBuildRunCommand.class);

  public static final String BUILD_STATE_FILE_ARG_NAME = "--build-state-file";
  public static final String BUILD_STATE_FILE_ARG_USAGE = "File containing the BuildStateJob data.";

  @Nullable
  @Option(name = BUILD_STATE_FILE_ARG_NAME, usage = BUILD_STATE_FILE_ARG_USAGE)
  private String buildStateFile;

  @Option(
    name = "--coordinator-port",
    usage = "Port of the remote build coordinator. (only used in MINION mode)."
  )
  private int coordinatorPort = -1;

  @Option(
    name = "--coordinator-address",
    usage = "Address of the remote build coordinator. (only used in MINION mode)."
  )
  private String coordinatorAddress = "localhost";

  @Nullable
  @Option(name = "--build-mode", usage = "The mode in which the distributed build is going to run.")
  private DistBuildMode distBuildMode = DistBuildMode.REMOTE_BUILD;

  @Nullable
  @Option(
    name = "--global-cache-dir",
    usage = "Full path to an existing directory that will contain a global cache across builds."
  )
  private Path globalCacheDir;

  private static final String RUN_ID_ARG_NAME = "--buildslave-run-id";

  @Nullable
  @Option(name = RUN_ID_ARG_NAME, usage = "Stampede RunId for this instance of BuildSlave.")
  private String buildSlaveRunId;

  @Nullable private DistBuildSlaveEventBusListener slaveEventListener;

  private CoordinatorBuildRuleEventsPublisher coordinatorBuildRuleEventsPublisher =
      new NoOpCoordinatorBuildRuleEventsPublisher();

  private MinionBuildProgressTracker minionBuildProgressTracker =
      new NoOpMinionBuildProgressTracker();

  private final FileMaterializationStatsTracker fileMaterializationStatsTracker =
      new FileMaterializationStatsTracker();

  private final HealthCheckStatsTracker healthCheckStatsTracker = new HealthCheckStatsTracker();

  private final BuildSlaveTimingStatsTracker timeStatsTracker = new BuildSlaveTimingStatsTracker();

  @Override
  public boolean isReadOnly() {
    return false;
  }

  @Override
  public String getShortDescription() {
    return "runs a distributed build in the current machine (experimental)";
  }

  @Override
  public ExitCode runWithoutHelp(CommandRunnerParams params)
      throws IOException, InterruptedException {
    Optional<StampedeId> stampedeId = getStampedeIdOptional();
    if (stampedeId.isPresent()) {
      params.getBuckEventBus().post(new DistBuildRunEvent(stampedeId.get(), getBuildSlaveRunId()));
    }

    LOG.info("Starting DistBuildRunCommand.");

    timeStatsTracker.startTimer(SlaveEvents.TOTAL_RUNTIME);
    timeStatsTracker.startTimer(SlaveEvents.DIST_BUILD_PREPARATION_TIME);
    Console console = params.getConsole();
    try (DistBuildService service = DistBuildFactory.newDistBuildService(params)) {
      if (slaveEventListener != null) {
        slaveEventListener.setDistBuildService(service);
      }

      try {
        timeStatsTracker.startTimer(SlaveEvents.DIST_BUILD_STATE_FETCH_TIME);
        Pair<BuildJobState, String> jobStateAndBuildName =
            getBuildJobStateAndBuildName(params.getCell().getFilesystem(), console, service);
        timeStatsTracker.stopTimer(SlaveEvents.DIST_BUILD_STATE_FETCH_TIME);

        BuildJobState jobState = jobStateAndBuildName.getFirst();
        String buildName = jobStateAndBuildName.getSecond();

        console
            .getStdOut()
            .println(
                String.format(
                    "BuildJob depends on a total of [%d] input deps.",
                    jobState.getFileHashesSize()));

        // Load up the remote build state from the client. Client-side .buckconfig is overlayed
        // with Stampede build slave .buckconfig.
        timeStatsTracker.startTimer(SlaveEvents.DIST_BUILD_STATE_LOADING_TIME);
        DistBuildState state =
            DistBuildState.load(
                params.getBuckConfig(),
                jobState,
                params.getCell(),
                params.getEnvironment(),
                params.getProcessExecutor(),
                params.getExecutableFinder(),
                params.getPluginManager(),
                params.getProjectFilesystemFactory());
        timeStatsTracker.stopTimer(SlaveEvents.DIST_BUILD_STATE_LOADING_TIME);

        ConcurrencyLimit concurrencyLimit =
            getConcurrencyLimit(state.getRootCell().getBuckConfig());

        try (CommandThreadManager pool =
                new CommandThreadManager(getClass().getName(), concurrencyLimit);
            RuleKeyCacheScope<RuleKey> ruleKeyCacheScope =
                new EventPostingRuleKeyCacheScope<>(
                    params.getBuckEventBus(),
                    new TrackedRuleKeyCache<>(
                        new DefaultRuleKeyCache<>(), new InstrumentingCacheStatsTracker()))) {
          // Note that we cannot use the same pool of build threads for file materialization<commit_msg>One minute timeout for CommandThreadManager in minions/coordinator<commit_after>/*
 * Copyright 2016-present Facebook, Inc.
 *
 * 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.facebook.buck.cli;

import com.facebook.buck.distributed.BuildJobStateSerializer;
import com.facebook.buck.distributed.DistBuildConfig;
import com.facebook.buck.distributed.DistBuildMode;
import com.facebook.buck.distributed.DistBuildRunEvent;
import com.facebook.buck.distributed.DistBuildService;
import com.facebook.buck.distributed.DistBuildState;
import com.facebook.buck.distributed.FileContentsProvider;
import com.facebook.buck.distributed.FileMaterializationStatsTracker;
import com.facebook.buck.distributed.build_slave.BuildSlaveTimingStatsTracker;
import com.facebook.buck.distributed.build_slave.BuildSlaveTimingStatsTracker.SlaveEvents;
import com.facebook.buck.distributed.build_slave.CoordinatorBuildRuleEventsPublisher;
import com.facebook.buck.distributed.build_slave.DistBuildSlaveExecutor;
import com.facebook.buck.distributed.build_slave.HealthCheckStatsTracker;
import com.facebook.buck.distributed.build_slave.MinionBuildProgressTracker;
import com.facebook.buck.distributed.build_slave.NoOpMinionBuildProgressTracker;
import com.facebook.buck.distributed.thrift.BuildJobState;
import com.facebook.buck.distributed.thrift.BuildSlaveRunId;
import com.facebook.buck.distributed.thrift.StampedeId;
import com.facebook.buck.event.BuckEventListener;
import com.facebook.buck.event.ConsoleEvent;
import com.facebook.buck.event.listener.DistBuildSlaveEventBusListener;
import com.facebook.buck.event.listener.NoOpCoordinatorBuildRuleEventsPublisher;
import com.facebook.buck.io.filesystem.ProjectFilesystem;
import com.facebook.buck.log.Logger;
import com.facebook.buck.rules.RuleKey;
import com.facebook.buck.rules.keys.DefaultRuleKeyCache;
import com.facebook.buck.rules.keys.EventPostingRuleKeyCacheScope;
import com.facebook.buck.rules.keys.RuleKeyCacheScope;
import com.facebook.buck.rules.keys.TrackedRuleKeyCache;
import com.facebook.buck.step.ExecutorPool;
import com.facebook.buck.util.Console;
import com.facebook.buck.util.ExitCode;
import com.facebook.buck.util.HumanReadableException;
import com.facebook.buck.util.cache.InstrumentingCacheStatsTracker;
import com.facebook.buck.util.concurrent.ConcurrencyLimit;
import com.facebook.buck.util.timing.DefaultClock;
import com.facebook.buck.util.types.Pair;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import com.google.common.util.concurrent.ListeningExecutorService;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import org.kohsuke.args4j.Option;

public class DistBuildRunCommand extends AbstractDistBuildCommand {
  private static final Logger LOG = Logger.get(DistBuildRunCommand.class);

  public static final String BUILD_STATE_FILE_ARG_NAME = "--build-state-file";
  public static final String BUILD_STATE_FILE_ARG_USAGE = "File containing the BuildStateJob data.";
  public static final int SHUTDOWN_TIMEOUT_MINUTES = 1;

  @Nullable
  @Option(name = BUILD_STATE_FILE_ARG_NAME, usage = BUILD_STATE_FILE_ARG_USAGE)
  private String buildStateFile;

  @Option(
    name = "--coordinator-port",
    usage = "Port of the remote build coordinator. (only used in MINION mode)."
  )
  private int coordinatorPort = -1;

  @Option(
    name = "--coordinator-address",
    usage = "Address of the remote build coordinator. (only used in MINION mode)."
  )
  private String coordinatorAddress = "localhost";

  @Nullable
  @Option(name = "--build-mode", usage = "The mode in which the distributed build is going to run.")
  private DistBuildMode distBuildMode = DistBuildMode.REMOTE_BUILD;

  @Nullable
  @Option(
    name = "--global-cache-dir",
    usage = "Full path to an existing directory that will contain a global cache across builds."
  )
  private Path globalCacheDir;

  private static final String RUN_ID_ARG_NAME = "--buildslave-run-id";

  @Nullable
  @Option(name = RUN_ID_ARG_NAME, usage = "Stampede RunId for this instance of BuildSlave.")
  private String buildSlaveRunId;

  @Nullable private DistBuildSlaveEventBusListener slaveEventListener;

  private CoordinatorBuildRuleEventsPublisher coordinatorBuildRuleEventsPublisher =
      new NoOpCoordinatorBuildRuleEventsPublisher();

  private MinionBuildProgressTracker minionBuildProgressTracker =
      new NoOpMinionBuildProgressTracker();

  private final FileMaterializationStatsTracker fileMaterializationStatsTracker =
      new FileMaterializationStatsTracker();

  private final HealthCheckStatsTracker healthCheckStatsTracker = new HealthCheckStatsTracker();

  private final BuildSlaveTimingStatsTracker timeStatsTracker = new BuildSlaveTimingStatsTracker();

  @Override
  public boolean isReadOnly() {
    return false;
  }

  @Override
  public String getShortDescription() {
    return "runs a distributed build in the current machine (experimental)";
  }

  @Override
  public ExitCode runWithoutHelp(CommandRunnerParams params)
      throws IOException, InterruptedException {
    Optional<StampedeId> stampedeId = getStampedeIdOptional();
    if (stampedeId.isPresent()) {
      params.getBuckEventBus().post(new DistBuildRunEvent(stampedeId.get(), getBuildSlaveRunId()));
    }

    LOG.info("Starting DistBuildRunCommand.");

    timeStatsTracker.startTimer(SlaveEvents.TOTAL_RUNTIME);
    timeStatsTracker.startTimer(SlaveEvents.DIST_BUILD_PREPARATION_TIME);
    Console console = params.getConsole();
    try (DistBuildService service = DistBuildFactory.newDistBuildService(params)) {
      if (slaveEventListener != null) {
        slaveEventListener.setDistBuildService(service);
      }

      try {
        timeStatsTracker.startTimer(SlaveEvents.DIST_BUILD_STATE_FETCH_TIME);
        Pair<BuildJobState, String> jobStateAndBuildName =
            getBuildJobStateAndBuildName(params.getCell().getFilesystem(), console, service);
        timeStatsTracker.stopTimer(SlaveEvents.DIST_BUILD_STATE_FETCH_TIME);

        BuildJobState jobState = jobStateAndBuildName.getFirst();
        String buildName = jobStateAndBuildName.getSecond();

        console
            .getStdOut()
            .println(
                String.format(
                    "BuildJob depends on a total of [%d] input deps.",
                    jobState.getFileHashesSize()));

        // Load up the remote build state from the client. Client-side .buckconfig is overlayed
        // with Stampede build slave .buckconfig.
        timeStatsTracker.startTimer(SlaveEvents.DIST_BUILD_STATE_LOADING_TIME);
        DistBuildState state =
            DistBuildState.load(
                params.getBuckConfig(),
                jobState,
                params.getCell(),
                params.getEnvironment(),
                params.getProcessExecutor(),
                params.getExecutableFinder(),
                params.getPluginManager(),
                params.getProjectFilesystemFactory());
        timeStatsTracker.stopTimer(SlaveEvents.DIST_BUILD_STATE_LOADING_TIME);

        ConcurrencyLimit concurrencyLimit =
            getConcurrencyLimit(state.getRootCell().getBuckConfig());

        try (CommandThreadManager pool =
                new CommandThreadManager(
                    getClass().getName(),
                    concurrencyLimit,
                    SHUTDOWN_TIMEOUT_MINUTES,
                    TimeUnit.MINUTES);
            RuleKeyCacheScope<RuleKey> ruleKeyCacheScope =
                new EventPostingRuleKeyCacheScope<>(
                    params.getBuckEventBus(),
                    new TrackedRuleKeyCache<>(
                        new DefaultRuleKeyCache<>(), new InstrumentingCacheStatsTracker()))) {
          // Note that we cannot use the same pool of build threads for file materialization
{'fraction_non_alphanumeric': 0.08290831455343375, 'fraction_numerical': 0.0012454038666824814, 'mean_word_length': 4.169527896995708, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3308400', 'n_tokens_mistral': 4683, 'n_tokens_neox': 4404, 'n_words': 1006}