aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/actions/BlazeExecutor.java
blob: 216eb5f81bf044bba20e2ad39c5ff26f5d3e6937 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
// Copyright 2014 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.actions;

import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.common.eventbus.EventBus;
import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.events.EventHandler;
import com.google.devtools.build.lib.events.EventKind;
import com.google.devtools.build.lib.events.Reporter;
import com.google.devtools.build.lib.profiler.Profiler;
import com.google.devtools.build.lib.profiler.ProfilerTask;
import com.google.devtools.build.lib.util.Clock;
import com.google.devtools.build.lib.vfs.Path;
import com.google.devtools.common.options.OptionsClassProvider;

import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

/**
 * The Executor class provides a dynamic abstraction of the various actual primitive system
 * operations that might be performed during a build step.
 *
 * <p>Constructions of this class might perform distributed execution, "virtual" execution for
 * testing purposes, or just print out the sequence of commands that would be executed, like Make's
 * "-n" option.
 */
@ThreadSafe
public final class BlazeExecutor implements Executor {

  private final Path outputPath;
  private final boolean verboseFailures;
  private final boolean showSubcommands;
  private final Path execRoot;
  private final Reporter reporter;
  private final EventBus eventBus;
  private final Clock clock;
  private final OptionsClassProvider options;
  private AtomicBoolean inExecutionPhase;

  private final Map<String, SpawnActionContext> spawnActionContextMap;
  private final Map<Class<? extends ActionContext>, ActionContext> contextMap =
      new HashMap<>();

  /**
   * Constructs an Executor, bound to a specified output base path, and which
   * will use the specified reporter to announce SUBCOMMAND events,
   * the given event bus to delegate events and the given output streams
   * for streaming output. The list of
   * strategy implementation classes is used to construct instances of the
   * strategies mapped by their declared abstract type. This list is uniquified
   * before using. Each strategy instance is created with a reference to this
   * Executor as well as the given options object.
   * <p>
   * Don't forget to call startBuildRequest() and stopBuildRequest() for each
   * request, and shutdown() when you're done with this executor.
   */
  public BlazeExecutor(Path execRoot,
      Path outputPath,
      Reporter reporter,
      EventBus eventBus,
      Clock clock,
      OptionsClassProvider options,
      boolean verboseFailures,
      boolean showSubcommands,
      List<ActionContext> contextImplementations,
      Map<String, SpawnActionContext> spawnActionContextMap,
      Iterable<ActionContextProvider> contextProviders)
      throws ExecutorInitException {
    this.outputPath = outputPath;
    this.verboseFailures = verboseFailures;
    this.showSubcommands = showSubcommands;
    this.execRoot = execRoot;
    this.reporter = reporter;
    this.eventBus = eventBus;
    this.clock = clock;
    this.options = options;
    this.inExecutionPhase = new AtomicBoolean(false);

    // We need to keep only the last occurrences of the entries in contextImplementations
    // (so we respect insertion order but also instantiate them only once).
    LinkedHashSet<ActionContext> allContexts = new LinkedHashSet<>();
    allContexts.addAll(contextImplementations);
    allContexts.addAll(spawnActionContextMap.values());
    this.spawnActionContextMap = ImmutableMap.copyOf(spawnActionContextMap);

    for (ActionContext context : contextImplementations) {
      ExecutionStrategy annotation = context.getClass().getAnnotation(ExecutionStrategy.class);
      if (annotation != null) {
        contextMap.put(annotation.contextType(), context);
      }
    }

    for (ActionContextProvider factory : contextProviders) {
      factory.executorCreated(allContexts);
    }
  }

  @Override
  public Path getExecRoot() {
    return execRoot;
  }

  @Override
  public EventHandler getEventHandler() {
    return reporter;
  }

  @Override
  public EventBus getEventBus() {
    return eventBus;
  }

  @Override
  public Clock getClock() {
    return clock;
  }

  @Override
  public boolean reportsSubcommands() {
    return showSubcommands;
  }

  /**
   * Report a subcommand event to this Executor's Reporter and, if action
   * logging is enabled, post it on its EventBus.
   */
  @Override
  public void reportSubcommand(String reason, String message) {
    reporter.handle(new Event(EventKind.SUBCOMMAND, null, "# " + reason + "\n" + message));
  }

  /**
   * This method is called before the start of the execution phase of each
   * build request.
   */
  public void executionPhaseStarting() {
    Preconditions.checkState(!inExecutionPhase.getAndSet(true));
    Profiler.instance().startTask(ProfilerTask.INFO, "Initializing executors");
    Profiler.instance().completeTask(ProfilerTask.INFO);
  }

  /**
   * This method is called after the end of the execution phase of each build
   * request (even if there was an interrupt).
   */
  public void executionPhaseEnding() {
    if (!inExecutionPhase.get()) {
      return;
    }

    Profiler.instance().startTask(ProfilerTask.INFO, "Shutting down executors");
    Profiler.instance().completeTask(ProfilerTask.INFO);
    inExecutionPhase.set(false);
  }

  public static void shutdownHelperPool(EventHandler reporter, ExecutorService pool,
      String name) {
    pool.shutdownNow();

    boolean interrupted = false;
    while (true) {
      try {
        if (!pool.awaitTermination(10, TimeUnit.SECONDS)) {
          reporter.handle(Event.warn(name + " threadpool shutdown took greater than ten seconds"));
        }
        break;
      } catch (InterruptedException e) {
        interrupted = true;
      }
    }

    if (interrupted) {
      Thread.currentThread().interrupt();
    }
  }

  @Override
  public <T extends ActionContext> T getContext(Class<? extends T> type) {
    Preconditions.checkArgument(type != SpawnActionContext.class, 
        "should use getSpawnActionContext instead");
    return type.cast(contextMap.get(type));
  }

  /**
   * Returns the {@link SpawnActionContext} to use for the given mnemonic. If no execution mode is
   * set, then it returns the default strategy for spawn actions.
   */
  @Override
  public SpawnActionContext getSpawnActionContext(String mnemonic) {
     SpawnActionContext context = spawnActionContextMap.get(mnemonic);
     return context == null ? spawnActionContextMap.get("") : context;
   }

  /** Returns true iff the --verbose_failures option was enabled. */
  @Override
  public boolean getVerboseFailures() {
    return verboseFailures;
  }

  /** Returns the options associated with the execution. */
  @Override
  public OptionsClassProvider getOptions() {
    return options;
  }

  public Path getOutputPath() {
    return outputPath;
  }
}