aboutsummaryrefslogtreecommitdiffhomepage
path: root/src
diff options
context:
space:
mode:
authorGravatar Klaus Aehlig <aehlig@google.com>2016-08-12 08:56:30 +0000
committerGravatar Philipp Wollermann <philwo@google.com>2016-08-16 15:19:46 +0000
commit88a590a496149e40fe76e0a8d3f0814142af31e5 (patch)
treeccd87ff704d32939e4942c37609d4009cae2f18e /src
parent936c2c2c815b64525bc6d3c6ac8f049655589370 (diff)
Support setting the build id via the client environment
Normally, Bazel generates a new UUID for every build invocation. In some use cases, however, the invocation of Bazel is part of a larger build process or otherwise controlled by a different tool. In this case, it is useful to tell Bazel the identifier of the build to make it fit with other identifiers generated by the controlling process. To achieve this, the variable BAZEL_INTERNAL_INVOCATION_ID from the client environment is inspected and if it is set and its value is a syntactically correct UUID, this UUID will be used as the identifier for the build. It is in the responsibility of the caller to ensure the id is sufficiently unique if that environment variable is set. -- MOS_MIGRATED_REVID=130079446
Diffstat (limited to 'src')
-rw-r--r--src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java59
1 files changed, 51 insertions, 8 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java b/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java
index dfda7f1a4f..c3fec79170 100644
--- a/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java
+++ b/src/main/java/com/google/devtools/build/lib/runtime/CommandEnvironment.java
@@ -74,13 +74,15 @@ public final class CommandEnvironment {
private final BlazeWorkspace workspace;
private final BlazeDirectories directories;
- private final UUID commandId; // Unique identifier for the command being run
+ private UUID commandId; // Unique identifier for the command being run
private final Reporter reporter;
private final EventBus eventBus;
private final BlazeModule.ModuleEnvironment blazeModuleEnvironment;
private final Map<String, String> clientEnv = new HashMap<>();
private final TimestampGranularityMonitor timestampGranularityMonitor;
+ private String[] crashData;
+
private PathFragment relativeWorkingDirectory = PathFragment.EMPTY_FRAGMENT;
private long commandStartTime;
private OutputService outputService;
@@ -108,7 +110,7 @@ public final class CommandEnvironment {
this.runtime = runtime;
this.workspace = workspace;
this.directories = workspace.getDirectories();
- this.commandId = UUID.randomUUID();
+ this.commandId = null; // Will be set once we get the client environment
this.reporter = new Reporter();
this.eventBus = eventBus;
this.blazeModuleEnvironment = new BlazeModuleEnvironment();
@@ -170,6 +172,23 @@ public final class CommandEnvironment {
for (Map.Entry<String, String> entry : env) {
clientEnv.put(entry.getKey(), entry.getValue());
}
+ // Try to set the clientId from the client environment.
+ if (commandId == null) {
+ String uuidString = clientEnv.get("BAZEL_INTERNAL_INVOCATION_ID");
+ if (uuidString != null) {
+ try {
+ commandId = UUID.fromString(uuidString);
+ } catch (IllegalArgumentException e) {
+ // String was malformed, so we will resort to generating a random UUID
+ }
+ }
+ }
+ if (commandId == null) {
+ // We have been provided with the client environment, but it didn't contain
+ // the invocation id; hence generate our own.
+ commandId = UUID.randomUUID();
+ }
+ setCommandIdInCrashData();
}
public TimestampGranularityMonitor getTimestampGranularityMonitor() {
@@ -203,6 +222,13 @@ public final class CommandEnvironment {
* the build info.
*/
public UUID getCommandId() {
+ if (commandId == null) {
+ // The commandId should not be requested before the beforeCommand is executed, as the
+ // commandId might be set through the client environment. However, to simplify testing,
+ // we set the id value before we throw the exception.
+ commandId = UUID.randomUUID();
+ throw new IllegalArgumentException("Build Id requested before client environment provided");
+ }
return commandId;
}
@@ -288,14 +314,31 @@ public final class CommandEnvironment {
/**
* An array of String values useful if Blaze crashes.
- * For now, just returns the size of the action cache and the build id.
+ * For now, just returns the size of the action cache and the build id; the latter as
+ * soon as it is determined.
*/
public String[] getCrashData() {
- return new String[]{
+ if (crashData == null) {
+ String buildId;
+ if (commandId == null) {
+ buildId = " (build id not set yet)";
+ } else {
+ buildId = commandId + " (build id)";
+ }
+ crashData = new String[]{
getFileSizeString(CompactPersistentActionCache.cacheFile(workspace.getCacheDirectory()),
"action cache"),
- getCommandId() + " (build id)",
- };
+ buildId,
+ };
+ }
+ return crashData;
+ }
+
+ private void setCommandIdInCrashData() {
+ // Update the command id in the crash data, if it is already generated
+ if (crashData != null && crashData.length >= 2) {
+ crashData[1] = getCommandId() + " (build id)";
+ }
}
private static String getFileSizeString(Path path, String type) {
@@ -361,7 +404,7 @@ public final class CommandEnvironment {
skyframeExecutor.resetEvaluator();
}
skyframeExecutor.sync(reporter, packageCacheOptions, getOutputBase(),
- getWorkingDirectory(), defaultsPackageContents, commandId,
+ getWorkingDirectory(), defaultsPackageContents, getCommandId(),
timestampGranularityMonitor);
}
@@ -466,7 +509,7 @@ public final class CommandEnvironment {
}
eventBus.post(new CommandStartEvent(
- command.name(), commandId, getClientEnv(), workingDirectory, getDirectories(),
+ command.name(), getCommandId(), getClientEnv(), workingDirectory, getDirectories(),
waitTimeInMs + options.waitTime));
}