From dcb0724ea00d4b4ed76c1eb9a25e2b1353dc581b Mon Sep 17 00:00:00 2001 From: Balazs Vatai <111979739+bvatai-br@users.noreply.github.com> Date: Thu, 28 Nov 2024 10:19:55 +0100 Subject: [PATCH] Debug action logs (#2) --- .../build/lib/remote/RemoteAction.java | 37 +++++++++++++++++++ .../lib/remote/RemoteExecutionService.java | 2 +- .../build/lib/remote/RemoteSpawnCache.java | 2 + .../lib/remote/merkletree/MerkleTree.java | 9 +++-- 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/google/devtools/build/lib/remote/RemoteAction.java b/src/main/java/com/google/devtools/build/lib/remote/RemoteAction.java index e8230f56933194..77f399620cd1c5 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/RemoteAction.java +++ b/src/main/java/com/google/devtools/build/lib/remote/RemoteAction.java @@ -28,6 +28,8 @@ import com.google.devtools.build.lib.vfs.PathFragment; import java.util.SortedMap; import javax.annotation.Nullable; +import java.io.FileWriter; +import java.io.IOException; /** * A value class representing an action which can be executed remotely. @@ -42,6 +44,7 @@ public class RemoteAction { private final RemoteActionExecutionContext remoteActionExecutionContext; private final RemotePathResolver remotePathResolver; @Nullable private final MerkleTree merkleTree; + @Nullable private final MerkleTree merkleTreeForDebug; private final long inputBytes; private final long inputFiles; private final Digest commandHash; @@ -65,6 +68,7 @@ public class RemoteAction { this.remoteActionExecutionContext = remoteActionExecutionContext; this.remotePathResolver = remotePathResolver; this.merkleTree = remoteDiscardMerkleTrees ? null : merkleTree; + this.merkleTreeForDebug = merkleTree; this.inputBytes = merkleTree.getInputBytes(); this.inputFiles = merkleTree.getInputFiles(); this.commandHash = commandHash; @@ -73,6 +77,39 @@ public class RemoteAction { this.actionKey = actionKey; } + private void writeDirectoryToFile(FileWriter writer, String root, MerkleTree t) throws IOException { + writer.write("[Root proto of " + root + "]\n"); + if (t != null) { + writer.write(t.rootProto.toString()); + for (String dirName : t.directories.keySet()) { + MerkleTree dir = t.directories.get(dirName); + writeDirectoryToFile(writer, root + "/" + dirName, dir); + } + } else { + writer.write("[merkleTree is null]\n"); + } + } + + public void writeToFile() { + String jsonPath = "/tmp/bazeldebug/" + actionKey.getDigest().getHash() + ".txt"; + try (FileWriter writer = new FileWriter(jsonPath)) { + writer.write("[Action]\n"); + writer.write(action.toString()); + writer.write("\n\n"); + + writer.write("[Command]\n"); + writer.write(command.toString()); + writer.write("\n\n"); + + writer.write("[InputRoot]\n"); + writeDirectoryToFile(writer, ".", merkleTreeForDebug); + + System.out.println(jsonPath + " written."); + } catch (IOException e) { + e.printStackTrace(); + } + } + public RemoteActionExecutionContext getRemoteActionExecutionContext() { return remoteActionExecutionContext; } diff --git a/src/main/java/com/google/devtools/build/lib/remote/RemoteExecutionService.java b/src/main/java/com/google/devtools/build/lib/remote/RemoteExecutionService.java index 0da922fb789787..aa2f64d06c7aa5 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/RemoteExecutionService.java +++ b/src/main/java/com/google/devtools/build/lib/remote/RemoteExecutionService.java @@ -137,6 +137,7 @@ import io.reactivex.rxjava3.schedulers.Schedulers; import java.io.FileNotFoundException; import java.io.IOException; +import java.io.File; import java.time.Instant; import java.util.ArrayList; import java.util.Collection; @@ -670,7 +671,6 @@ public RemoteAction buildRemoteAction(Spawn spawn, SpawnExecutionContext context buildSalt(spawn, spawnScrubber)); ActionKey actionKey = digestUtil.computeActionKey(action); - RequestMetadata metadata = TracingMetadataUtils.buildMetadata( buildRequestId, commandId, actionKey.getDigest().getHash(), spawn.getResourceOwner()); diff --git a/src/main/java/com/google/devtools/build/lib/remote/RemoteSpawnCache.java b/src/main/java/com/google/devtools/build/lib/remote/RemoteSpawnCache.java index 06c97c5390db38..efc1123851fcd1 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/RemoteSpawnCache.java +++ b/src/main/java/com/google/devtools/build/lib/remote/RemoteSpawnCache.java @@ -249,6 +249,8 @@ public void store(SpawnResult result) throws ExecException, InterruptedException return; } + action.writeToFile(); + if (options.experimentalGuardAgainstConcurrentChanges) { try (SilentCloseable c = prof.profile("checkForConcurrentModifications")) { checkForConcurrentModifications(); diff --git a/src/main/java/com/google/devtools/build/lib/remote/merkletree/MerkleTree.java b/src/main/java/com/google/devtools/build/lib/remote/merkletree/MerkleTree.java index 7cace2695d63b5..142fcbd135a961 100644 --- a/src/main/java/com/google/devtools/build/lib/remote/merkletree/MerkleTree.java +++ b/src/main/java/com/google/devtools/build/lib/remote/merkletree/MerkleTree.java @@ -96,11 +96,11 @@ private interface MerkleTreeDirectoryVisitor { private Map digestDirectoryMap; private Map digestFileMap; - @Nullable private final Directory rootProto; - private final Digest rootDigest; - private final SortedSet files; + @Nullable public final Directory rootProto; + public final Digest rootDigest; + public final SortedSet files; private final SortedSet symlinks; - private final SortedMap directories; + public final SortedMap directories; private final long inputFiles; private final long inputBytes; @@ -123,6 +123,7 @@ private MerkleTree( this.inputBytes = inputBytes; } + // TODO /** Returns the digest of the Merkle tree's root. */ @Nullable public Directory getRootProto() {