Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[7.1.0] Publish the new execution log format to the build event protocol. #21417

Merged
merged 1 commit into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.eventbus.Subscribe;
import com.google.common.primitives.Booleans;
import com.google.devtools.build.lib.buildtool.BuildRequest;
import com.google.devtools.build.lib.buildtool.buildevent.BuildCompleteEvent;
import com.google.devtools.build.lib.events.Event;
import com.google.devtools.build.lib.exec.CompactSpawnLogContext;
import com.google.devtools.build.lib.exec.ExecutionOptions;
Expand All @@ -40,10 +42,16 @@

/** Module providing on-demand spawn logging. */
public final class SpawnLogModule extends BlazeModule {

@Nullable private SpawnLogContext spawnLogContext;
@Nullable private Path outputPath;

@Nullable private AbruptExitException abruptExit = null;

private void clear() {
spawnLogContext = null;
outputPath = null;
abruptExit = null;
}

private void initOutputs(CommandEnvironment env) throws IOException {
Expand Down Expand Up @@ -88,20 +96,21 @@ private void initOutputs(CommandEnvironment env) throws IOException {
Path outputBase = env.getOutputBase();

if (executionOptions.executionLogCompactFile != null) {
outputPath = workingDirectory.getRelative(executionOptions.executionLogCompactFile);

try {
spawnLogContext =
new CompactSpawnLogContext(
workingDirectory.getRelative(executionOptions.executionLogCompactFile),
env.getExecRoot().asFragment(),
env.getOptions().getOptions(RemoteOptions.class),
env.getRuntime().getFileSystem().getDigestFunction(),
env.getXattrProvider());
spawnLogContext =
new CompactSpawnLogContext(
outputPath,
env.getExecRoot().asFragment(),
env.getOptions().getOptions(RemoteOptions.class),
env.getRuntime().getFileSystem().getDigestFunction(),
env.getXattrProvider());
} catch (InterruptedException e) {
env.getReporter()
.handle(Event.error("Error while setting up the execution log: " + e.getMessage()));
}
} else {
Path outputPath = null;
Encoding encoding = null;

if (executionOptions.executionLogBinaryFile != null) {
Expand Down Expand Up @@ -159,19 +168,27 @@ public void executorInit(CommandEnvironment env, BuildRequest request, ExecutorB
}
}

@Override
public void afterCommand() throws AbruptExitException {
@Subscribe
public void buildComplete(BuildCompleteEvent event) {
// The log must be finalized in buildComplete() instead of afterCommand(), because it's our
// last chance to publish it to the build event protocol.

if (spawnLogContext == null) {
// No logging requested.
clear();
return;
}

try {
spawnLogContext.close();
event.getResult().getBuildToolLogCollection().addLocalFile("execution.log", outputPath);
} catch (IOException e) {
String message = e.getMessage() == null ? "Error writing execution log" : e.getMessage();
throw new AbruptExitException(
createDetailedExitCode(message, Code.EXECUTION_LOG_WRITE_FAILURE), e);
abruptExit =
new AbruptExitException(
createDetailedExitCode(
String.format("Error writing execution log: %s", e.getMessage()),
Code.EXECUTION_LOG_WRITE_FAILURE),
e);
} finally {
clear();
}
Expand All @@ -184,4 +201,11 @@ private static DetailedExitCode createDetailedExitCode(String message, Code deta
.setExecution(Execution.newBuilder().setCode(detailedCode))
.build());
}

@Override
public void afterCommand() throws AbruptExitException {
if (abruptExit != null) {
throw abruptExit;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ private void logInvocation() throws IOException, InterruptedException {
.setHashFunctionName(digestHashFunction.toString())));
}

@Override
public boolean shouldPublish() {
// The compact log is small enough to be uploaded to a remote store.
return true;
}

@Override
public void logSpawn(
Spawn spawn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,12 @@ private MessageOutputStream<SpawnExec> getConvertedOutputStream(Path path) throw
String.format("invalid execution log encoding: %s", encoding));
}

@Override
public boolean shouldPublish() {
// The expanded log tends to be too large to be uploaded to a remote store.
return false;
}

@Override
public void logSpawn(
Spawn spawn,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ public abstract void logSpawn(
/** Finishes writing the log and performs any required post-processing. */
public abstract void close() throws IOException;

/** Whether the log should be published to the build event protocol. */
public abstract boolean shouldPublish();

/** Computes the environment variables. */
protected ImmutableList<EnvironmentVariable> getEnvironmentVariables(Spawn spawn) {
ImmutableMap<String, String> environment = spawn.getEnvironment();
Expand Down
Loading