Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 1 | // Copyright 2016 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 | package com.google.devtools.build.lib.query2; |
| 15 | |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 16 | import static com.google.common.collect.ImmutableSet.toImmutableSet; |
| 17 | |
Googler | 2b50388 | 2016-11-28 21:54:43 +0000 | [diff] [blame] | 18 | import com.google.common.annotations.VisibleForTesting; |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 19 | import com.google.common.base.Preconditions; |
Nathan Harmata | f44211c | 2016-10-10 16:31:18 +0000 | [diff] [blame] | 20 | import com.google.common.collect.ArrayListMultimap; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 21 | import com.google.common.collect.ImmutableList; |
| 22 | import com.google.common.collect.Iterables; |
Googler | 13dc56a | 2016-12-07 15:49:20 +0000 | [diff] [blame] | 23 | import com.google.common.collect.ListMultimap; |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 24 | import com.google.common.collect.Maps; |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 25 | import com.google.common.collect.Multimap; |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 26 | import com.google.common.collect.Streams; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.cmdline.Label; |
Nathan Harmata | f44211c | 2016-10-10 16:31:18 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.collect.CompactHashSet; |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 30 | import com.google.devtools.build.lib.concurrent.MultisetSemaphore; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 31 | import com.google.devtools.build.lib.packages.Target; |
| 32 | import com.google.devtools.build.lib.query2.engine.Callback; |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.query2.engine.QueryEnvironment.QueryTaskFuture; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.query2.engine.QueryException; |
| 35 | import com.google.devtools.build.lib.query2.engine.QueryExpression; |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.query2.engine.Uniquifier; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 37 | import com.google.devtools.build.lib.query2.engine.VariableContext; |
| 38 | import com.google.devtools.build.lib.skyframe.PackageValue; |
| 39 | import com.google.devtools.build.lib.skyframe.SkyFunctions; |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 40 | import com.google.devtools.build.lib.util.Pair; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 41 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 42 | import com.google.devtools.build.skyframe.SkyKey; |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 43 | import java.util.ArrayList; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 44 | import java.util.Collection; |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 45 | import java.util.LinkedList; |
| 46 | import java.util.Map; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 47 | import java.util.Set; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 48 | |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 49 | /** |
| 50 | * Parallel implementations of various functionality in {@link SkyQueryEnvironment}. |
| 51 | * |
| 52 | * <p>Special attention is given to memory usage. Naive parallel implementations of query |
| 53 | * functionality would lead to memory blowup. Instead of dealing with {@link Target}s, we try to |
| 54 | * deal with {@link SkyKey}s as much as possible to reduce the number of {@link Package}s forcibly |
| 55 | * in memory at any given time. |
| 56 | */ |
| 57 | // TODO(bazel-team): Be more deliberate about bounding memory usage here. |
| 58 | class ParallelSkyQueryUtils { |
Googler | 2b50388 | 2016-11-28 21:54:43 +0000 | [diff] [blame] | 59 | |
| 60 | /** The maximum number of keys to visit at once. */ |
| 61 | @VisibleForTesting static final int VISIT_BATCH_SIZE = 10000; |
| 62 | |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 63 | private ParallelSkyQueryUtils() { |
| 64 | } |
| 65 | |
| 66 | /** |
| 67 | * Specialized parallel variant of {@link SkyQueryEnvironment#getAllRdeps} that is appropriate |
| 68 | * when there is no depth-bound. |
| 69 | */ |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 70 | static QueryTaskFuture<Void> getAllRdepsUnboundedParallel( |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 71 | SkyQueryEnvironment env, |
| 72 | QueryExpression expression, |
| 73 | VariableContext<Target> context, |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 74 | Callback<Target> callback, |
| 75 | MultisetSemaphore<PackageIdentifier> packageSemaphore) { |
| 76 | return env.eval( |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 77 | expression, |
| 78 | context, |
Googler | 7184b6f | 2017-05-16 05:25:49 +0200 | [diff] [blame] | 79 | ParallelVisitor.createParallelVisitorCallback( |
Googler | 2b50388 | 2016-11-28 21:54:43 +0000 | [diff] [blame] | 80 | new AllRdepsUnboundedVisitor.Factory(env, callback, packageSemaphore))); |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | /** Specialized parallel variant of {@link SkyQueryEnvironment#getRBuildFiles}. */ |
| 84 | static void getRBuildFilesParallel( |
| 85 | SkyQueryEnvironment env, |
| 86 | Collection<PathFragment> fileIdentifiers, |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 87 | Callback<Target> callback, |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 88 | MultisetSemaphore<PackageIdentifier> packageSemaphore) |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 89 | throws QueryException, InterruptedException { |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 90 | Uniquifier<SkyKey> keyUniquifier = env.createSkyKeyUniquifier(); |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 91 | RBuildFilesVisitor visitor = |
Googler | 2b50388 | 2016-11-28 21:54:43 +0000 | [diff] [blame] | 92 | new RBuildFilesVisitor(env, keyUniquifier, callback, packageSemaphore); |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 93 | visitor.visitAndWaitForCompletion(env.getSkyKeysForFileFragments(fileIdentifiers)); |
| 94 | } |
| 95 | |
| 96 | /** A helper class that computes 'rbuildfiles(<blah>)' via BFS. */ |
Googler | 7184b6f | 2017-05-16 05:25:49 +0200 | [diff] [blame] | 97 | private static class RBuildFilesVisitor extends ParallelVisitor<SkyKey> { |
| 98 | private final SkyQueryEnvironment env; |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 99 | private final MultisetSemaphore<PackageIdentifier> packageSemaphore; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 100 | |
| 101 | private RBuildFilesVisitor( |
| 102 | SkyQueryEnvironment env, |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 103 | Uniquifier<SkyKey> uniquifier, |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 104 | Callback<Target> callback, |
| 105 | MultisetSemaphore<PackageIdentifier> packageSemaphore) { |
Googler | 7184b6f | 2017-05-16 05:25:49 +0200 | [diff] [blame] | 106 | super(uniquifier, callback, VISIT_BATCH_SIZE); |
| 107 | this.env = env; |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 108 | this.packageSemaphore = packageSemaphore; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | @Override |
| 112 | protected Visit getVisitResult(Iterable<SkyKey> values) throws InterruptedException { |
| 113 | Collection<Iterable<SkyKey>> reverseDeps = env.graph.getReverseDeps(values).values(); |
| 114 | Set<SkyKey> keysToUseForResult = CompactHashSet.create(); |
| 115 | Set<SkyKey> keysToVisitNext = CompactHashSet.create(); |
| 116 | for (SkyKey rdep : Iterables.concat(reverseDeps)) { |
| 117 | if (rdep.functionName().equals(SkyFunctions.PACKAGE)) { |
| 118 | keysToUseForResult.add(rdep); |
| 119 | // Every package has a dep on the external package, so we need to include those edges too. |
| 120 | if (rdep.equals(PackageValue.key(Label.EXTERNAL_PACKAGE_IDENTIFIER))) { |
| 121 | keysToVisitNext.add(rdep); |
| 122 | } |
| 123 | } else if (!rdep.functionName().equals(SkyFunctions.PACKAGE_LOOKUP)) { |
| 124 | // Packages may depend on the existence of subpackages, but these edges aren't relevant to |
| 125 | // rbuildfiles. |
| 126 | keysToVisitNext.add(rdep); |
| 127 | } |
| 128 | } |
| 129 | return new Visit(keysToUseForResult, keysToVisitNext); |
| 130 | } |
| 131 | |
| 132 | @Override |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 133 | protected void processResultantTargets( |
| 134 | Iterable<SkyKey> keysToUseForResult, Callback<Target> callback) |
| 135 | throws QueryException, InterruptedException { |
| 136 | Set<PackageIdentifier> pkgIdsNeededForResult = |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 137 | Streams.stream(keysToUseForResult) |
| 138 | .map(SkyQueryEnvironment.PACKAGE_SKYKEY_TO_PACKAGE_IDENTIFIER) |
| 139 | .collect(toImmutableSet()); |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 140 | packageSemaphore.acquireAll(pkgIdsNeededForResult); |
| 141 | try { |
| 142 | callback.process(SkyQueryEnvironment.getBuildFilesForPackageValues( |
| 143 | env.graph.getSuccessfulValues(keysToUseForResult).values())); |
| 144 | } finally { |
| 145 | packageSemaphore.releaseAll(pkgIdsNeededForResult); |
| 146 | } |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 147 | } |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 148 | |
| 149 | @Override |
| 150 | protected Iterable<SkyKey> preprocessInitialVisit(Iterable<SkyKey> keys) { |
| 151 | return keys; |
| 152 | } |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 155 | /** |
| 156 | * A helper class that computes 'allrdeps(<blah>)' via BFS. |
| 157 | * |
| 158 | * <p>The visitor uses a pair of <node, reverse dep> to keep track the nodes to visit and avoid |
| 159 | * dealing with targetification of reverse deps until they are needed. The node itself is needed |
| 160 | * to filter out disallowed deps later. Compared against the approach using a single SkyKey, it |
| 161 | * consumes 16 more bytes in a 64-bit environment for each edge. However it defers the need to |
| 162 | * load all the packages which have at least a target as a rdep of the current batch, thus greatly |
| 163 | * reduces the risk of OOMs. The additional memory usage should not be a large concern here, as |
| 164 | * even with 10M edges, the memory overhead is around 160M, and the memory can be reclaimed by |
| 165 | * regular GC. |
| 166 | */ |
Googler | 7184b6f | 2017-05-16 05:25:49 +0200 | [diff] [blame] | 167 | private static class AllRdepsUnboundedVisitor extends ParallelVisitor<Pair<SkyKey, SkyKey>> { |
| 168 | private final SkyQueryEnvironment env; |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 169 | private final MultisetSemaphore<PackageIdentifier> packageSemaphore; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 170 | |
| 171 | private AllRdepsUnboundedVisitor( |
| 172 | SkyQueryEnvironment env, |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 173 | Uniquifier<Pair<SkyKey, SkyKey>> uniquifier, |
| 174 | Callback<Target> callback, |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 175 | MultisetSemaphore<PackageIdentifier> packageSemaphore) { |
Googler | 7184b6f | 2017-05-16 05:25:49 +0200 | [diff] [blame] | 176 | super(uniquifier, callback, VISIT_BATCH_SIZE); |
| 177 | this.env = env; |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 178 | this.packageSemaphore = packageSemaphore; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | /** |
| 182 | * A {@link Factory} for {@link AllRdepsUnboundedVisitor} instances, each of which will be used |
| 183 | * to perform visitation of the reverse transitive closure of the {@link Target}s passed in a |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 184 | * single {@link Callback#process} call. Note that all the created instances share the same |
| 185 | * {@link Uniquifier} so that we don't visit the same Skyframe node more than once. |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 186 | */ |
Googler | 7184b6f | 2017-05-16 05:25:49 +0200 | [diff] [blame] | 187 | private static class Factory implements ParallelVisitor.Factory { |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 188 | private final SkyQueryEnvironment env; |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 189 | private final Uniquifier<Pair<SkyKey, SkyKey>> uniquifier; |
| 190 | private final Callback<Target> callback; |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 191 | private final MultisetSemaphore<PackageIdentifier> packageSemaphore; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 192 | |
| 193 | private Factory( |
| 194 | SkyQueryEnvironment env, |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 195 | Callback<Target> callback, |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 196 | MultisetSemaphore<PackageIdentifier> packageSemaphore) { |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 197 | this.env = env; |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 198 | this.uniquifier = env.createReverseDepSkyKeyUniquifier(); |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 199 | this.callback = callback; |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 200 | this.packageSemaphore = packageSemaphore; |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | @Override |
Googler | 7184b6f | 2017-05-16 05:25:49 +0200 | [diff] [blame] | 204 | public ParallelVisitor<Pair<SkyKey, SkyKey>> create() { |
Googler | 2b50388 | 2016-11-28 21:54:43 +0000 | [diff] [blame] | 205 | return new AllRdepsUnboundedVisitor(env, uniquifier, callback, packageSemaphore); |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
| 209 | @Override |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 210 | protected Visit getVisitResult(Iterable<Pair<SkyKey, SkyKey>> keys) |
| 211 | throws InterruptedException { |
| 212 | Collection<SkyKey> filteredKeys = new ArrayList<>(); |
Nathan Harmata | be59799 | 2016-10-10 15:59:00 +0000 | [diff] [blame] | 213 | |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 214 | // Build a raw reverse dep map from pairs of SkyKeys to filter out the disallowed deps. |
| 215 | Map<SkyKey, Collection<SkyKey>> reverseDepsMap = Maps.newHashMap(); |
| 216 | for (Pair<SkyKey, SkyKey> reverseDepPair : keys) { |
| 217 | // First-level nodes do not have a parent node (they may have one in Skyframe but we do not |
| 218 | // need to retrieve them. |
| 219 | if (reverseDepPair.first == null) { |
| 220 | filteredKeys.add(Preconditions.checkNotNull(reverseDepPair.second)); |
| 221 | continue; |
| 222 | } |
| 223 | |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 224 | reverseDepsMap.computeIfAbsent(reverseDepPair.first, k -> new LinkedList<SkyKey>()); |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 225 | |
| 226 | reverseDepsMap.get(reverseDepPair.first).add(reverseDepPair.second); |
Nathan Harmata | f44211c | 2016-10-10 16:31:18 +0000 | [diff] [blame] | 227 | } |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 228 | |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 229 | Multimap<SkyKey, SkyKey> packageKeyToTargetKeyMap = |
| 230 | env.makePackageKeyToTargetKeyMap(Iterables.concat(reverseDepsMap.values())); |
| 231 | Set<PackageIdentifier> pkgIdsNeededForTargetification = |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 232 | packageKeyToTargetKeyMap |
| 233 | .keySet() |
| 234 | .stream() |
| 235 | .map(SkyQueryEnvironment.PACKAGE_SKYKEY_TO_PACKAGE_IDENTIFIER) |
| 236 | .collect(toImmutableSet()); |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 237 | packageSemaphore.acquireAll(pkgIdsNeededForTargetification); |
| 238 | |
| 239 | try { |
| 240 | // Filter out disallowed deps. We cannot defer the targetification any further as we do not |
| 241 | // want to retrieve the rdeps of unwanted nodes (targets). |
| 242 | if (!reverseDepsMap.isEmpty()) { |
| 243 | Collection<Target> filteredTargets = |
| 244 | env.filterRawReverseDepsOfTransitiveTraversalKeys( |
| 245 | reverseDepsMap, packageKeyToTargetKeyMap); |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 246 | filteredTargets |
| 247 | .stream() |
| 248 | .map(SkyQueryEnvironment.TARGET_TO_SKY_KEY) |
| 249 | .forEachOrdered(filteredKeys::add); |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 250 | } |
| 251 | } finally { |
| 252 | packageSemaphore.releaseAll(pkgIdsNeededForTargetification); |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | // Retrieve the reverse deps as SkyKeys and defer the targetification and filtering to next |
| 256 | // recursive visitation. |
| 257 | Map<SkyKey, Iterable<SkyKey>> unfilteredReverseDeps = env.graph.getReverseDeps(filteredKeys); |
| 258 | |
Googler | 2b50388 | 2016-11-28 21:54:43 +0000 | [diff] [blame] | 259 | ImmutableList.Builder<Pair<SkyKey, SkyKey>> builder = ImmutableList.builder(); |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 260 | for (Map.Entry<SkyKey, Iterable<SkyKey>> rdeps : unfilteredReverseDeps.entrySet()) { |
| 261 | for (SkyKey rdep : rdeps.getValue()) { |
| 262 | Label label = SkyQueryEnvironment.SKYKEY_TO_LABEL.apply(rdep); |
| 263 | if (label != null) { |
Googler | 2b50388 | 2016-11-28 21:54:43 +0000 | [diff] [blame] | 264 | builder.add(Pair.of(rdeps.getKey(), rdep)); |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 265 | } |
| 266 | } |
| 267 | } |
| 268 | |
Googler | 2b50388 | 2016-11-28 21:54:43 +0000 | [diff] [blame] | 269 | return new Visit(/*keysToUseForResult=*/ filteredKeys, /*keysToVisit=*/ builder.build()); |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | @Override |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 273 | protected void processResultantTargets( |
| 274 | Iterable<SkyKey> keysToUseForResult, Callback<Target> callback) |
| 275 | throws QueryException, InterruptedException { |
| 276 | Multimap<SkyKey, SkyKey> packageKeyToTargetKeyMap = |
| 277 | env.makePackageKeyToTargetKeyMap(keysToUseForResult); |
| 278 | Set<PackageIdentifier> pkgIdsNeededForResult = |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 279 | packageKeyToTargetKeyMap |
| 280 | .keySet() |
| 281 | .stream() |
| 282 | .map(SkyQueryEnvironment.PACKAGE_SKYKEY_TO_PACKAGE_IDENTIFIER) |
| 283 | .collect(toImmutableSet()); |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 284 | packageSemaphore.acquireAll(pkgIdsNeededForResult); |
| 285 | try { |
| 286 | callback.process( |
| 287 | env.makeTargetsFromPackageKeyToTargetKeyMap(packageKeyToTargetKeyMap).values()); |
| 288 | } finally { |
| 289 | packageSemaphore.releaseAll(pkgIdsNeededForResult); |
| 290 | } |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 291 | } |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 292 | |
| 293 | @Override |
| 294 | protected Iterable<Pair<SkyKey, SkyKey>> preprocessInitialVisit(Iterable<SkyKey> keys) { |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 295 | return Iterables.transform(keys, key -> Pair.of(null, key)); |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 296 | } |
Googler | 2b50388 | 2016-11-28 21:54:43 +0000 | [diff] [blame] | 297 | |
| 298 | @Override |
| 299 | protected Iterable<Task> getVisitTasks(Collection<Pair<SkyKey, SkyKey>> pendingKeysToVisit) { |
| 300 | // Group pending visits by package. |
Googler | 13dc56a | 2016-12-07 15:49:20 +0000 | [diff] [blame] | 301 | ListMultimap<PackageIdentifier, Pair<SkyKey, SkyKey>> visitsByPackage = |
Googler | 2b50388 | 2016-11-28 21:54:43 +0000 | [diff] [blame] | 302 | ArrayListMultimap.create(); |
| 303 | for (Pair<SkyKey, SkyKey> visit : pendingKeysToVisit) { |
| 304 | Label label = SkyQueryEnvironment.SKYKEY_TO_LABEL.apply(visit.second); |
| 305 | if (label != null) { |
| 306 | visitsByPackage.put(label.getPackageIdentifier(), visit); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | ImmutableList.Builder<Task> builder = ImmutableList.builder(); |
| 311 | |
| 312 | // A couple notes here: |
| 313 | // (i) ArrayListMultimap#values returns the values grouped by key, which is exactly what we |
| 314 | // want. |
| 315 | // (ii) ArrayListMultimap#values returns a Collection view, so we make a copy to avoid |
| 316 | // accidentally retaining the entire ArrayListMultimap object. |
| 317 | for (Iterable<Pair<SkyKey, SkyKey>> keysToVisitBatch : |
| 318 | Iterables.partition(ImmutableList.copyOf(visitsByPackage.values()), VISIT_BATCH_SIZE)) { |
| 319 | builder.add(new VisitTask(keysToVisitBatch)); |
| 320 | } |
| 321 | |
| 322 | return builder.build(); |
| 323 | } |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 324 | } |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 325 | } |
| 326 | |