Skip to content

Commit 5156558

Browse files
tjgqcopybara-github
authored andcommitted
Also use an AsynchronousTreeDeleter for multiplex workers.
I don't see a reason for singleplex and multiplex workers to differ in this regard. This makes it possible to clean up logic that previously needed to handle a null TreeDeleter. PiperOrigin-RevId: 662499579 Change-Id: I7138daad36c0f9f2db72588bfcc2947344c00a25
1 parent 1a61c84 commit 5156558

File tree

7 files changed

+38
-27
lines changed

7 files changed

+38
-27
lines changed

src/main/java/com/google/devtools/build/lib/sandbox/SandboxHelpers.java

Lines changed: 4 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -129,23 +129,13 @@ public static void moveOutputs(SandboxOutputs outputs, Path sourceRoot, Path tar
129129
* directories or files that are either not needed {@code inputs} or doesn't have the right
130130
* content or symlink target path are removed.
131131
*/
132-
public static void cleanExisting(
133-
Path root,
134-
SandboxInputs inputs,
135-
Set<PathFragment> inputsToCreate,
136-
Set<PathFragment> dirsToCreate,
137-
Path workDir)
138-
throws IOException, InterruptedException {
139-
cleanExisting(root, inputs, inputsToCreate, dirsToCreate, workDir, /* treeDeleter= */ null);
140-
}
141-
142132
public static void cleanExisting(
143133
Path root,
144134
SandboxInputs inputs,
145135
Set<PathFragment> inputsToCreate,
146136
Set<PathFragment> dirsToCreate,
147137
Path workDir,
148-
@Nullable TreeDeleter treeDeleter)
138+
TreeDeleter treeDeleter)
149139
throws IOException, InterruptedException {
150140
cleanExisting(
151141
root,
@@ -163,7 +153,7 @@ public static void cleanExisting(
163153
Set<PathFragment> inputsToCreate,
164154
Set<PathFragment> dirsToCreate,
165155
Path workDir,
166-
@Nullable TreeDeleter treeDeleter,
156+
TreeDeleter treeDeleter,
167157
@Nullable SandboxContents sandboxContents)
168158
throws IOException, InterruptedException {
169159
Path inaccessibleHelperDir = workDir.getRelative(INACCESSIBLE_HELPER_DIR);
@@ -216,7 +206,7 @@ private static void cleanRecursivelyWithInMemoryContents(
216206
Set<PathFragment> dirsToCreate,
217207
Path workDir,
218208
Set<PathFragment> prefixDirs,
219-
@Nullable TreeDeleter treeDeleter,
209+
TreeDeleter treeDeleter,
220210
SandboxContents stashContents)
221211
throws IOException, InterruptedException {
222212
Path execroot = workDir.getParentDirectory();
@@ -254,12 +244,7 @@ private static void cleanRecursivelyWithInMemoryContents(
254244
dirent.getValue());
255245
dirsToCreate.remove(pathRelativeToWorkDir);
256246
} else {
257-
if (treeDeleter == null) {
258-
// TODO(bazel-team): Use async tree deleter for workers too
259-
absPath.deleteTree();
260-
} else {
261-
treeDeleter.deleteTree(absPath);
262-
}
247+
treeDeleter.deleteTree(absPath);
263248
}
264249
}
265250
}

src/main/java/com/google/devtools/build/lib/sandbox/SandboxModule.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public final class SandboxModule extends BlazeModule {
9494
private final Set<SandboxFallbackSpawnRunner> spawnRunners = new HashSet<>();
9595

9696
/**
97-
* Handler to process expensive tree deletions outside of the critical path.
97+
* Handler to process expensive tree deletions, potentially outside of the critical path.
9898
*
9999
* <p>Sandboxing creates one separate tree for each action, and this tree is used to run the
100100
* action commands in. These trees are disjoint for all actions and have unique identifiers.

src/main/java/com/google/devtools/build/lib/worker/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,7 @@ java_library(
331331
":worker_protocol",
332332
"//src/main/java/com/google/devtools/build/lib/actions",
333333
"//src/main/java/com/google/devtools/build/lib/events",
334+
"//src/main/java/com/google/devtools/build/lib/exec:tree_deleter",
334335
"//src/main/java/com/google/devtools/build/lib/sandbox:cgroups_info",
335336
"//src/main/java/com/google/devtools/build/lib/sandbox:sandbox_helpers",
336337
"//src/main/java/com/google/devtools/build/lib/shell",

src/main/java/com/google/devtools/build/lib/worker/SandboxedWorkerProxy.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.google.common.collect.ImmutableSet;
1919
import com.google.common.collect.Iterables;
2020
import com.google.common.flogger.GoogleLogger;
21+
import com.google.devtools.build.lib.exec.TreeDeleter;
2122
import com.google.devtools.build.lib.sandbox.SandboxHelpers;
2223
import com.google.devtools.build.lib.sandbox.SandboxHelpers.SandboxInputs;
2324
import com.google.devtools.build.lib.sandbox.SandboxHelpers.SandboxOutputs;
@@ -42,12 +43,15 @@ public class SandboxedWorkerProxy extends WorkerProxy {
4243

4344
private final PathFragment sandboxName;
4445

46+
private final TreeDeleter treeDeleter;
47+
4548
SandboxedWorkerProxy(
4649
WorkerKey workerKey,
4750
int workerId,
4851
Path logFile,
4952
WorkerMultiplexer workerMultiplexer,
50-
Path workDir) {
53+
Path workDir,
54+
TreeDeleter treeDeleter) {
5155
super(workerKey, workerId, logFile, workerMultiplexer, workDir);
5256
sandboxName =
5357
PathFragment.create(
@@ -57,6 +61,7 @@ public class SandboxedWorkerProxy extends WorkerProxy {
5761
Integer.toString(workerId),
5862
workerKey.getExecRoot().getBaseName()));
5963
sandboxDir = this.workDir.getRelative(sandboxName);
64+
this.treeDeleter = treeDeleter;
6065
}
6166

6267
@Override
@@ -68,7 +73,7 @@ public boolean isSandboxed() {
6873
public void prepareExecution(
6974
SandboxInputs inputFiles, SandboxOutputs outputs, Set<PathFragment> workerFiles)
7075
throws IOException, InterruptedException {
71-
workerMultiplexer.createSandboxedProcess(workDir, workerFiles, inputFiles);
76+
workerMultiplexer.createSandboxedProcess(workDir, workerFiles, inputFiles, treeDeleter);
7277

7378
sandboxDir.createDirectoryAndParents();
7479
LinkedHashSet<PathFragment> dirsToCreate = new LinkedHashSet<>();
@@ -81,7 +86,12 @@ public void prepareExecution(
8186
Iterables.concat(inputFiles.getFiles().keySet(), inputFiles.getSymlinks().keySet()),
8287
outputs);
8388
SandboxHelpers.cleanExisting(
84-
sandboxDir.getParentDirectory(), inputFiles, inputsToCreate, dirsToCreate, sandboxDir);
89+
sandboxDir.getParentDirectory(),
90+
inputFiles,
91+
inputsToCreate,
92+
dirsToCreate,
93+
sandboxDir,
94+
treeDeleter);
8595
// Finally, create anything that is still missing. This is non-strict only for historical
8696
// reasons, we haven't seen what would break if we make it strict.
8797
SandboxHelpers.createDirectories(dirsToCreate, sandboxDir, /* strict=*/ false);

src/main/java/com/google/devtools/build/lib/worker/WorkerFactory.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,9 @@ public Worker create(WorkerKey key) throws IOException {
100100
if (key.isMultiplex()) {
101101
WorkerMultiplexer workerMultiplexer = WorkerMultiplexerManager.getInstance(key, logFile);
102102
Path workDir = getSandboxedWorkerPath(key);
103-
worker = new SandboxedWorkerProxy(key, workerId, logFile, workerMultiplexer, workDir);
103+
worker =
104+
new SandboxedWorkerProxy(
105+
key, workerId, logFile, workerMultiplexer, workDir, treeDeleter);
104106
} else {
105107
Path workDir = getSandboxedWorkerPath(key, workerId);
106108
worker =

src/main/java/com/google/devtools/build/lib/worker/WorkerMultiplexer.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.google.common.flogger.GoogleLogger;
2121
import com.google.devtools.build.lib.events.Event;
2222
import com.google.devtools.build.lib.events.EventHandler;
23+
import com.google.devtools.build.lib.exec.TreeDeleter;
2324
import com.google.devtools.build.lib.sandbox.SandboxHelpers;
2425
import com.google.devtools.build.lib.sandbox.SandboxHelpers.SandboxInputs;
2526
import com.google.devtools.build.lib.sandbox.SandboxHelpers.SandboxOutputs;
@@ -151,7 +152,10 @@ public WorkerProcessStatus getStatus() {
151152
* sets up the sandbox root dir with the required worker files.
152153
*/
153154
public synchronized void createSandboxedProcess(
154-
Path workDir, Set<PathFragment> workerFiles, SandboxInputs inputFiles)
155+
Path workDir,
156+
Set<PathFragment> workerFiles,
157+
SandboxInputs inputFiles,
158+
TreeDeleter treeDeleter)
155159
throws IOException, InterruptedException {
156160
// TODO: Make blaze clean remove the workdir.
157161
if (this.process == null) {
@@ -167,7 +171,12 @@ public synchronized void createSandboxedProcess(
167171
workerFiles,
168172
SandboxOutputs.getEmptyInstance());
169173
SandboxHelpers.cleanExisting(
170-
workDir.getParentDirectory(), inputFiles, inputsToCreate, dirsToCreate, workDir);
174+
workDir.getParentDirectory(),
175+
inputFiles,
176+
inputsToCreate,
177+
dirsToCreate,
178+
workDir,
179+
treeDeleter);
171180
SandboxHelpers.createDirectories(dirsToCreate, workDir, /* strict=*/ false);
172181
WorkerExecRoot.createInputs(inputsToCreate, inputFiles.limitedCopy(workerFiles), workDir);
173182
createProcess(workDir);

src/test/java/com/google/devtools/build/lib/sandbox/SandboxHelpersTest.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import com.google.devtools.build.lib.actions.cache.VirtualActionInput;
3333
import com.google.devtools.build.lib.actions.util.ActionsTestUtil;
3434
import com.google.devtools.build.lib.exec.BinTools;
35+
import com.google.devtools.build.lib.exec.TreeDeleter;
3536
import com.google.devtools.build.lib.exec.util.SpawnBuilder;
3637
import com.google.devtools.build.lib.sandbox.SandboxHelpers.SandboxInputs;
3738
import com.google.devtools.build.lib.sandbox.SandboxHelpers.SandboxOutputs;
@@ -67,6 +68,8 @@
6768
@RunWith(JUnit4.class)
6869
public class SandboxHelpersTest {
6970

71+
private final TreeDeleter treeDeleter = new SynchronousTreeDeleter();
72+
7073
private final Scratch scratch = new Scratch();
7174
private Path execRoot;
7275
@Nullable private ExecutorService executorToCleanup;
@@ -281,7 +284,8 @@ public void cleanExisting_updatesDirs() throws IOException, InterruptedException
281284
ImmutableMap.of(input1, inputTxt, input2, inputTxt, input3, inputTxt, input4, inputTxt),
282285
ImmutableMap.of(),
283286
ImmutableMap.of());
284-
SandboxHelpers.cleanExisting(rootDir, inputs2, inputsToCreate, dirsToCreate, execRoot);
287+
SandboxHelpers.cleanExisting(
288+
rootDir, inputs2, inputsToCreate, dirsToCreate, execRoot, treeDeleter);
285289
assertThat(dirsToCreate).containsExactly(inputDir2, inputDir3, outputDir);
286290
assertThat(execRoot.getRelative("existing/directory/with").exists()).isTrue();
287291
assertThat(execRoot.getRelative("partial").exists()).isTrue();

0 commit comments

Comments
 (0)