Skip to content

Commit

Permalink
Move some more common logic into SpawnLogContext.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 591273969
Change-Id: I8d3dc6f167c256f330628af9b1cac7958f300270
  • Loading branch information
tjgq authored and copybara-github committed Dec 15, 2023
1 parent 76b4b6e commit cad0f29
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
package com.google.devtools.build.lib.exec;

import static com.google.devtools.build.lib.exec.SpawnLogContext.computeDigest;
import static com.google.devtools.build.lib.exec.SpawnLogContext.getEnvironmentVariables;
import static com.google.devtools.build.lib.exec.SpawnLogContext.getPlatform;
import static com.google.devtools.build.lib.exec.SpawnLogContext.getSpawnMetricsProto;

import build.bazel.remote.execution.v2.Platform;
import com.google.common.collect.ImmutableSet;
import com.google.common.flogger.GoogleLogger;
import com.google.devtools.build.lib.actions.ActionInput;
Expand All @@ -27,9 +28,9 @@
import com.google.devtools.build.lib.actions.SpawnResult;
import com.google.devtools.build.lib.actions.Spawns;
import com.google.devtools.build.lib.actions.cache.VirtualActionInput;
import com.google.devtools.build.lib.analysis.platform.PlatformUtils;
import com.google.devtools.build.lib.exec.Protos.Digest;
import com.google.devtools.build.lib.exec.Protos.File;
import com.google.devtools.build.lib.exec.Protos.Platform;
import com.google.devtools.build.lib.exec.Protos.SpawnExec;
import com.google.devtools.build.lib.profiler.Profiler;
import com.google.devtools.build.lib.profiler.SilentCloseable;
Expand All @@ -55,7 +56,6 @@
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.Consumer;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -145,13 +145,7 @@ public void logSpawn(
SortedMap<Path, ActionInput> existingOutputs = listExistingOutputs(spawn, fileSystem);
SpawnExec.Builder builder = SpawnExec.newBuilder();
builder.addAllCommandArgs(spawn.getArguments());

Map<String, String> env = spawn.getEnvironment();
// Sorting the environment pairs by variable name.
TreeSet<String> variables = new TreeSet<>(env.keySet());
for (String var : variables) {
builder.addEnvironmentVariablesBuilder().setName(var).setValue(env.get(var));
}
builder.addAllEnvironmentVariables(getEnvironmentVariables(spawn));

ImmutableSet<? extends ActionInput> toolFiles = spawn.getToolFiles().toSet();

Expand Down Expand Up @@ -225,9 +219,9 @@ public void logSpawn(
}
builder.setRemotable(Spawns.mayBeExecutedRemotely(spawn));

Platform execPlatform = PlatformUtils.getPlatformProto(spawn, remoteOptions);
if (execPlatform != null) {
builder.setPlatform(buildPlatform(execPlatform));
Platform platform = getPlatform(spawn, remoteOptions);
if (platform != null) {
builder.setPlatform(platform);
}
if (result.status() != SpawnResult.Status.SUCCESS) {
builder.setStatus(result.status().toString());
Expand Down Expand Up @@ -285,14 +279,6 @@ public void close() throws IOException {
}
}

private static Protos.Platform buildPlatform(Platform platform) {
Protos.Platform.Builder platformBuilder = Protos.Platform.newBuilder();
for (Platform.Property p : platform.getPropertiesList()) {
platformBuilder.addPropertiesBuilder().setName(p.getName()).setValue(p.getValue());
}
return platformBuilder.build();
}

private SortedMap<Path, ActionInput> listExistingOutputs(Spawn spawn, FileSystem fileSystem) {
TreeMap<Path, ActionInput> result = new TreeMap<>();
for (ActionInput output : spawn.getOutputFiles()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
package com.google.devtools.build.lib.exec;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.hash.HashCode;
import com.google.devtools.build.lib.actions.ActionContext;
import com.google.devtools.build.lib.actions.ActionInput;
Expand All @@ -23,8 +26,13 @@
import com.google.devtools.build.lib.actions.Spawn;
import com.google.devtools.build.lib.actions.SpawnMetrics;
import com.google.devtools.build.lib.actions.SpawnResult;
import com.google.devtools.build.lib.actions.UserExecException;
import com.google.devtools.build.lib.actions.cache.VirtualActionInput;
import com.google.devtools.build.lib.analysis.platform.PlatformUtils;
import com.google.devtools.build.lib.exec.Protos.Digest;
import com.google.devtools.build.lib.exec.Protos.EnvironmentVariable;
import com.google.devtools.build.lib.exec.Protos.Platform;
import com.google.devtools.build.lib.remote.options.RemoteOptions;
import com.google.devtools.build.lib.vfs.DigestHashFunction;
import com.google.devtools.build.lib.vfs.DigestUtils;
import com.google.devtools.build.lib.vfs.FileSystem;
Expand All @@ -34,6 +42,7 @@
import com.google.protobuf.util.Durations;
import java.io.IOException;
import java.time.Duration;
import java.util.Map;
import java.util.SortedMap;
import javax.annotation.Nullable;

Expand Down Expand Up @@ -64,6 +73,35 @@ void logSpawn(
/** Finishes writing the log and performs any required post-processing. */
void close() throws IOException;

/** Computes the environment variables. */
static ImmutableList<EnvironmentVariable> getEnvironmentVariables(Spawn spawn) {
ImmutableMap<String, String> environment = spawn.getEnvironment();
ImmutableList.Builder<EnvironmentVariable> builder =
ImmutableList.builderWithExpectedSize(environment.size());
for (Map.Entry<String, String> entry : ImmutableSortedMap.copyOf(environment).entrySet()) {
builder.add(
EnvironmentVariable.newBuilder()
.setName(entry.getKey())
.setValue(entry.getValue())
.build());
}
return builder.build();
}

/** Computes the execution platform. */
@Nullable
static Platform getPlatform(Spawn spawn, RemoteOptions remoteOptions) throws UserExecException {
var execPlatform = PlatformUtils.getPlatformProto(spawn, remoteOptions);
if (execPlatform == null) {
return null;
}
Platform.Builder builder = Platform.newBuilder();
for (var p : execPlatform.getPropertiesList()) {
builder.addPropertiesBuilder().setName(p.getName()).setValue(p.getValue());
}
return builder.build();
}

/**
* Computes the digest of an ActionInput or its path.
*
Expand Down

0 comments on commit cad0f29

Please sign in to comment.