blob: a05234858cb481562e7839a4099449de4366196a [file] [log] [blame]
jmmv16af94c2019-04-16 07:44:34 -07001// Copyright 2019 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package com.google.devtools.build.lib.sandbox;
16
17import static com.google.common.base.Preconditions.checkNotNull;
18import static com.google.common.base.Preconditions.checkState;
19
20import com.google.common.util.concurrent.ThreadFactoryBuilder;
21import com.google.devtools.build.lib.exec.TreeDeleter;
22import com.google.devtools.build.lib.vfs.Path;
23import java.io.IOException;
24import java.util.concurrent.LinkedBlockingQueue;
25import java.util.concurrent.ThreadFactory;
26import java.util.concurrent.ThreadPoolExecutor;
27import java.util.concurrent.TimeUnit;
28import java.util.logging.Logger;
29import javax.annotation.Nullable;
30
31/**
32 * Executes file system tree deletions asynchronously.
33 *
34 * <p>The number of threads used to process the backlog of tree deletions can be configured at any
35 * time via {@link #setThreads(int)}. While a build is running, this number should be low to not use
36 * precious resources that could otherwise be used for the build itself. But when the build is
37 * finished, this number should be raised to quickly go through any pending deletions.
38 */
39class AsynchronousTreeDeleter implements TreeDeleter {
40
41 private static final Logger logger = Logger.getLogger(TreeDeleter.class.getName());
42
43 /** Thread pool used to execute asynchronous tree deletions; null in synchronous mode. */
44 @Nullable private ThreadPoolExecutor service;
45
46 /** Constructs a new asynchronous tree deleter backed by just one thread. */
47 AsynchronousTreeDeleter() {
48 logger.info("Starting async tree deletion pool with 1 thread");
49
50 ThreadFactory threadFactory =
51 new ThreadFactoryBuilder()
52 .setNameFormat("tree-deleter")
53 .setDaemon(true)
54 .setPriority(Thread.MIN_PRIORITY)
55 .build();
56
57 service =
58 new ThreadPoolExecutor(
59 1, 1, 0L, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), threadFactory);
60 }
61
62 /**
63 * Resizes the thread pool to the given number of threads.
64 *
65 * <p>If the pool of active threads is larger than the requested number of threads, the resize
66 * will progressively happen as those active threads become inactive. If the requested size is
67 * zero, this will wait for all pending deletions to complete.
68 *
69 * @param threads desired number of threads, or 0 to go back to synchronous deletion
70 */
71 void setThreads(int threads) {
72 checkState(threads > 0, "Use SynchronousTreeDeleter if no async behavior is desired");
73 logger.info("Resizing async tree deletion pool to " + threads + " threads");
74 checkNotNull(service, "Cannot call setThreads after shutdown").setMaximumPoolSize(threads);
75 }
76
77 @Override
78 public void deleteTree(Path path) {
79 checkNotNull(service, "Cannot call deleteTree after shutdown")
80 .execute(
81 () -> {
82 try {
83 path.deleteTree();
84 } catch (IOException e) {
85 logger.warning("Failed to delete tree " + path + " asynchronously: " + e);
86 }
87 });
88 }
89
90 @Override
91 public void deleteTreesBelow(Path path) {
92 checkNotNull(service, "Cannot call deleteTree after shutdown")
93 .execute(
94 () -> {
95 try {
96 path.deleteTreesBelow();
97 } catch (IOException e) {
98 logger.warning("Failed to delete contents of " + path + " asynchronously: " + e);
99 }
100 });
101 }
102
103 @Override
104 public void shutdown() {
105 if (service != null) {
106 logger.info("Finishing " + service.getTaskCount() + " pending async tree deletions");
107 service.shutdown();
108 service = null;
109 }
110 }
111}