Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 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 | |
nharmata | 1bd4aaf | 2017-10-31 11:23:04 -0400 | [diff] [blame] | 16 | import static com.google.common.collect.ImmutableSet.toImmutableSet; |
Googler | 73b7b2e | 2017-03-23 17:20:58 +0000 | [diff] [blame] | 17 | import static com.google.common.util.concurrent.MoreExecutors.directExecutor; |
shreyax | 432b253 | 2019-01-23 11:16:07 -0800 | [diff] [blame] | 18 | import static com.google.devtools.build.lib.pkgcache.FilteringPolicies.NO_FILTER; |
Googler | 73b7b2e | 2017-03-23 17:20:58 +0000 | [diff] [blame] | 19 | |
Nathan Harmata | 46e5e41 | 2016-06-29 16:18:10 +0000 | [diff] [blame] | 20 | import com.google.common.base.Ascii; |
Janak Ramakrishnan | 3685873 | 2015-06-17 16:45:47 +0000 | [diff] [blame] | 21 | import com.google.common.base.Function; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 22 | import com.google.common.base.Preconditions; |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 23 | import com.google.common.base.Predicate; |
Janak Ramakrishnan | b735d2f | 2015-06-16 17:46:36 +0000 | [diff] [blame] | 24 | import com.google.common.base.Predicates; |
Nathan Harmata | 79e4965 | 2016-12-16 22:00:48 +0000 | [diff] [blame] | 25 | import com.google.common.base.Throwables; |
Janak Ramakrishnan | f6f0fcc | 2015-06-19 20:24:52 +0000 | [diff] [blame] | 26 | import com.google.common.collect.ArrayListMultimap; |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 27 | import com.google.common.collect.Collections2; |
Janak Ramakrishnan | cda5b66 | 2015-06-18 23:46:36 +0000 | [diff] [blame] | 28 | import com.google.common.collect.ImmutableList; |
Janak Ramakrishnan | 3685873 | 2015-06-17 16:45:47 +0000 | [diff] [blame] | 29 | import com.google.common.collect.ImmutableMap; |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 30 | import com.google.common.collect.ImmutableMultimap; |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 31 | import com.google.common.collect.ImmutableSet; |
| 32 | import com.google.common.collect.Iterables; |
Googler | d191162 | 2016-06-27 15:43:11 +0000 | [diff] [blame] | 33 | import com.google.common.collect.Maps; |
Janak Ramakrishnan | 3685873 | 2015-06-17 16:45:47 +0000 | [diff] [blame] | 34 | import com.google.common.collect.Multimap; |
Miguel Alcon Pinto | 933c13a | 2015-09-16 18:37:45 +0000 | [diff] [blame] | 35 | import com.google.common.collect.Sets; |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 36 | import com.google.common.util.concurrent.AsyncFunction; |
| 37 | import com.google.common.util.concurrent.Futures; |
| 38 | import com.google.common.util.concurrent.ListenableFuture; |
| 39 | import com.google.common.util.concurrent.ListeningExecutorService; |
| 40 | import com.google.common.util.concurrent.MoreExecutors; |
| 41 | import com.google.common.util.concurrent.ThreadFactoryBuilder; |
shahan | 602cc85 | 2018-06-06 20:09:57 -0700 | [diff] [blame] | 42 | import com.google.devtools.build.lib.actions.FileStateValue; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 43 | import com.google.devtools.build.lib.cmdline.Label; |
dannark | be3cefc | 2018-12-13 11:52:45 -0800 | [diff] [blame] | 44 | import com.google.devtools.build.lib.cmdline.LabelConstants; |
Kristina Chodorow | 73fa203 | 2015-08-28 17:57:46 +0000 | [diff] [blame] | 45 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 46 | import com.google.devtools.build.lib.cmdline.TargetParsingException; |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 47 | import com.google.devtools.build.lib.cmdline.TargetPattern; |
philwo | 3bcb9f6 | 2017-09-06 12:52:21 +0200 | [diff] [blame] | 48 | import com.google.devtools.build.lib.collect.compacthashset.CompactHashSet; |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 49 | import com.google.devtools.build.lib.concurrent.BlockingStack; |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 50 | import com.google.devtools.build.lib.concurrent.MultisetSemaphore; |
Mark Schaller | 20c7501 | 2016-06-21 22:51:14 +0000 | [diff] [blame] | 51 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadSafe; |
Mark Schaller | 4f48f1b | 2017-03-10 20:38:43 +0000 | [diff] [blame] | 52 | import com.google.devtools.build.lib.events.DelegatingEventHandler; |
Janak Ramakrishnan | 3533ab5 | 2016-01-14 18:14:09 +0000 | [diff] [blame] | 53 | import com.google.devtools.build.lib.events.Event; |
Mark Schaller | 4f48f1b | 2017-03-10 20:38:43 +0000 | [diff] [blame] | 54 | import com.google.devtools.build.lib.events.EventKind; |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 55 | import com.google.devtools.build.lib.events.ExtendedEventHandler; |
Janak Ramakrishnan | 0a4c6e4 | 2015-09-17 00:37:58 +0000 | [diff] [blame] | 56 | import com.google.devtools.build.lib.packages.BuildFileContainsErrorsException; |
Dmitry Lomov | 6073eb6 | 2016-01-21 21:26:32 +0000 | [diff] [blame] | 57 | import com.google.devtools.build.lib.packages.DependencyFilter; |
Janak Ramakrishnan | 112840b | 2016-12-29 21:49:56 +0000 | [diff] [blame] | 58 | import com.google.devtools.build.lib.packages.NoSuchPackageException; |
Janak Ramakrishnan | 3685873 | 2015-06-17 16:45:47 +0000 | [diff] [blame] | 59 | import com.google.devtools.build.lib.packages.NoSuchTargetException; |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 60 | import com.google.devtools.build.lib.packages.NoSuchThingException; |
| 61 | import com.google.devtools.build.lib.packages.Package; |
| 62 | import com.google.devtools.build.lib.packages.Rule; |
| 63 | import com.google.devtools.build.lib.packages.Target; |
ulfjack | e83775d | 2019-05-14 08:58:46 -0700 | [diff] [blame] | 64 | import com.google.devtools.build.lib.pkgcache.FilteringPolicies; |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 65 | import com.google.devtools.build.lib.pkgcache.PathPackageLocator; |
Nathan Harmata | 483ded9 | 2016-01-13 19:42:45 +0000 | [diff] [blame] | 66 | import com.google.devtools.build.lib.profiler.AutoProfiler; |
Janak Ramakrishnan | 643063d | 2015-06-25 16:21:49 +0000 | [diff] [blame] | 67 | import com.google.devtools.build.lib.query2.engine.AllRdepsFunction; |
Miguel Alcon Pinto | 42984f3 | 2015-11-06 19:05:13 +0000 | [diff] [blame] | 68 | import com.google.devtools.build.lib.query2.engine.Callback; |
Nathan Harmata | e9826b4 | 2017-03-07 18:05:21 +0000 | [diff] [blame] | 69 | import com.google.devtools.build.lib.query2.engine.KeyExtractor; |
| 70 | import com.google.devtools.build.lib.query2.engine.MinDepthUniquifier; |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 71 | import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback; |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 72 | import com.google.devtools.build.lib.query2.engine.QueryEvalResult; |
| 73 | import com.google.devtools.build.lib.query2.engine.QueryException; |
| 74 | import com.google.devtools.build.lib.query2.engine.QueryExpression; |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 75 | import com.google.devtools.build.lib.query2.engine.QueryExpressionContext; |
Nathan Harmata | ed93560 | 2016-03-02 01:16:14 +0000 | [diff] [blame] | 76 | import com.google.devtools.build.lib.query2.engine.QueryExpressionMapper; |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 77 | import com.google.devtools.build.lib.query2.engine.QueryUtil.MinDepthUniquifierImpl; |
nharmata | bf2e2d8 | 2017-06-21 23:12:51 +0200 | [diff] [blame] | 78 | import com.google.devtools.build.lib.query2.engine.QueryUtil.MutableKeyExtractorBackedMapImpl; |
nharmata | c49e874 | 2018-09-27 11:27:41 -0700 | [diff] [blame] | 79 | import com.google.devtools.build.lib.query2.engine.QueryUtil.NonExceptionalUniquifier; |
nharmata | bf2e2d8 | 2017-06-21 23:12:51 +0200 | [diff] [blame] | 80 | import com.google.devtools.build.lib.query2.engine.QueryUtil.ThreadSafeMutableKeyExtractorBackedSetImpl; |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 81 | import com.google.devtools.build.lib.query2.engine.QueryUtil.UniquifierImpl; |
Googler | d191162 | 2016-06-27 15:43:11 +0000 | [diff] [blame] | 82 | import com.google.devtools.build.lib.query2.engine.StreamableQueryEnvironment; |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 83 | import com.google.devtools.build.lib.query2.engine.ThreadSafeOutputFormatterCallback; |
Miguel Alcon Pinto | 42984f3 | 2015-11-06 19:05:13 +0000 | [diff] [blame] | 84 | import com.google.devtools.build.lib.query2.engine.Uniquifier; |
gregce | a91495f | 2019-05-07 07:27:39 -0700 | [diff] [blame] | 85 | import com.google.devtools.build.lib.query2.query.BlazeTargetAccessor; |
Nathan Harmata | 4d89d11 | 2016-04-17 02:46:15 +0000 | [diff] [blame] | 86 | import com.google.devtools.build.lib.skyframe.BlacklistedPackagePrefixesValue; |
Nathan Harmata | c686fd6 | 2016-04-26 17:41:20 +0000 | [diff] [blame] | 87 | import com.google.devtools.build.lib.skyframe.ContainingPackageLookupFunction; |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 88 | import com.google.devtools.build.lib.skyframe.GraphBackedRecursivePackageProvider; |
Janak Ramakrishnan | d802d5b | 2015-08-20 21:05:46 +0000 | [diff] [blame] | 89 | import com.google.devtools.build.lib.skyframe.PackageLookupValue; |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 90 | import com.google.devtools.build.lib.skyframe.PackageValue; |
Janak Ramakrishnan | 958ef82 | 2016-01-07 16:21:39 +0000 | [diff] [blame] | 91 | import com.google.devtools.build.lib.skyframe.PrepareDepsOfPatternsFunction; |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 92 | import com.google.devtools.build.lib.skyframe.RecursivePackageProviderBackedTargetPatternResolver; |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 93 | import com.google.devtools.build.lib.skyframe.TargetPatternValue; |
Mark Schaller | d7311e0 | 2015-07-07 16:36:09 +0000 | [diff] [blame] | 94 | import com.google.devtools.build.lib.skyframe.TargetPatternValue.TargetPatternKey; |
Mark Schaller | 8ff5b3c | 2015-07-29 17:32:11 +0000 | [diff] [blame] | 95 | import com.google.devtools.build.lib.skyframe.TransitiveTraversalValue; |
juliexxia | cc86475 | 2018-06-01 09:03:48 -0700 | [diff] [blame] | 96 | import com.google.devtools.build.lib.skyframe.TraversalInfoRootPackageExtractor; |
Googler | 407d393 | 2019-05-16 15:13:59 -0700 | [diff] [blame] | 97 | import com.google.devtools.build.lib.supplier.InterruptibleSupplier; |
| 98 | import com.google.devtools.build.lib.supplier.MemoizingInterruptibleSupplier; |
John Cater | e5fb5f0 | 2017-12-04 07:53:19 -0800 | [diff] [blame] | 99 | import com.google.devtools.build.lib.vfs.Path; |
Janak Ramakrishnan | d802d5b | 2015-08-20 21:05:46 +0000 | [diff] [blame] | 100 | import com.google.devtools.build.lib.vfs.PathFragment; |
| 101 | import com.google.devtools.build.lib.vfs.RootedPath; |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 102 | import com.google.devtools.build.skyframe.EvaluationContext; |
Mark Schaller | d7311e0 | 2015-07-07 16:36:09 +0000 | [diff] [blame] | 103 | import com.google.devtools.build.skyframe.EvaluationResult; |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 104 | import com.google.devtools.build.skyframe.SkyFunctionName; |
| 105 | import com.google.devtools.build.skyframe.SkyKey; |
Janak Ramakrishnan | 3685873 | 2015-06-17 16:45:47 +0000 | [diff] [blame] | 106 | import com.google.devtools.build.skyframe.SkyValue; |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 107 | import com.google.devtools.build.skyframe.WalkableGraph; |
| 108 | import com.google.devtools.build.skyframe.WalkableGraph.WalkableGraphFactory; |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 109 | import java.io.IOException; |
Miguel Alcon Pinto | 47ea948 | 2015-11-18 16:05:17 +0000 | [diff] [blame] | 110 | import java.util.ArrayList; |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 111 | import java.util.Collection; |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 112 | import java.util.HashSet; |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 113 | import java.util.List; |
| 114 | import java.util.Map; |
| 115 | import java.util.Set; |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 116 | import java.util.concurrent.Callable; |
| 117 | import java.util.concurrent.RejectedExecutionException; |
| 118 | import java.util.concurrent.ThreadPoolExecutor; |
Nathan Harmata | 71616b1 | 2016-09-28 20:20:48 +0000 | [diff] [blame] | 119 | import java.util.concurrent.TimeUnit; |
nharmata | 641db6d | 2018-08-16 14:31:57 -0700 | [diff] [blame] | 120 | import java.util.function.BiConsumer; |
Nathan Harmata | 79e4965 | 2016-12-16 22:00:48 +0000 | [diff] [blame] | 121 | import java.util.logging.Level; |
Eric Fellheimer | a39f8a9 | 2015-07-28 19:11:23 +0000 | [diff] [blame] | 122 | import java.util.logging.Logger; |
Janak Ramakrishnan | b5a541a | 2015-06-19 20:55:01 +0000 | [diff] [blame] | 123 | |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 124 | /** |
| 125 | * {@link AbstractBlazeQueryEnvironment} that introspects the Skyframe graph to find forward and |
Googler | d191162 | 2016-06-27 15:43:11 +0000 | [diff] [blame] | 126 | * reverse edges. Results obtained by calling {@link #evaluateQuery} are not guaranteed to be in any |
| 127 | * particular order. As well, this class eagerly loads the full transitive closure of targets, even |
| 128 | * if the full closure isn't needed. |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 129 | * |
| 130 | * <p>This class has concurrent implementations of the |
| 131 | * {@link QueryTaskFuture}/{@link QueryTaskCallable} helper methods. The combination of this and the |
| 132 | * asynchronous evaluation model yields parallel query evaluation. |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 133 | */ |
Googler | d191162 | 2016-06-27 15:43:11 +0000 | [diff] [blame] | 134 | public class SkyQueryEnvironment extends AbstractBlazeQueryEnvironment<Target> |
| 135 | implements StreamableQueryEnvironment<Target> { |
Janak Ramakrishnan | ae3a20a | 2016-01-08 16:24:29 +0000 | [diff] [blame] | 136 | // 10k is likely a good balance between using batch efficiently and not blowing up memory. |
| 137 | // TODO(janakr): Unify with RecursivePackageProviderBackedTargetPatternResolver's constant. |
nharmata | 1bd4aaf | 2017-10-31 11:23:04 -0400 | [diff] [blame] | 138 | protected static final int BATCH_CALLBACK_SIZE = 10000; |
gregce | a91495f | 2019-05-07 07:27:39 -0700 | [diff] [blame] | 139 | public static final int DEFAULT_THREAD_COUNT = Runtime.getRuntime().availableProcessors(); |
Nathan Harmata | 116c2f6 | 2016-06-28 15:58:20 +0000 | [diff] [blame] | 140 | private static final int MAX_QUERY_EXPRESSION_LOG_CHARS = 1000; |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 141 | private static final Logger logger = Logger.getLogger(SkyQueryEnvironment.class.getName()); |
Mark Schaller | d7311e0 | 2015-07-07 16:36:09 +0000 | [diff] [blame] | 142 | |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 143 | private final BlazeTargetAccessor accessor = new BlazeTargetAccessor(this); |
Janak Ramakrishnan | df69432 | 2016-11-08 18:28:12 +0000 | [diff] [blame] | 144 | protected final int loadingPhaseThreads; |
| 145 | protected final WalkableGraphFactory graphFactory; |
Janak Ramakrishnan | e77b6d2 | 2016-11-08 18:44:55 +0000 | [diff] [blame] | 146 | protected final ImmutableList<String> universeScope; |
Mark Schaller | 4f48f1b | 2017-03-10 20:38:43 +0000 | [diff] [blame] | 147 | protected boolean blockUniverseEvaluationErrors; |
| 148 | protected ExtendedEventHandler universeEvalEventHandler; |
| 149 | |
Nathan Harmata | 6454347 | 2016-06-30 18:33:40 +0000 | [diff] [blame] | 150 | protected final String parserPrefix; |
Janak Ramakrishnan | df69432 | 2016-11-08 18:28:12 +0000 | [diff] [blame] | 151 | protected final PathPackageLocator pkgPath; |
nharmata | fac7c25 | 2018-09-12 15:31:23 -0700 | [diff] [blame] | 152 | protected final int queryEvaluationParallelismLevel; |
Mark Schaller | 6cebed6 | 2016-06-27 18:05:39 +0000 | [diff] [blame] | 153 | |
| 154 | // The following fields are set in the #beforeEvaluateQuery method. |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 155 | private MultisetSemaphore<PackageIdentifier> packageSemaphore; |
Mark Schaller | 6cebed6 | 2016-06-27 18:05:39 +0000 | [diff] [blame] | 156 | protected WalkableGraph graph; |
Googler | ee34392 | 2019-03-05 08:51:12 -0800 | [diff] [blame] | 157 | protected InterruptibleSupplier<ImmutableSet<PathFragment>> blacklistPatternsSupplier; |
| 158 | protected GraphBackedRecursivePackageProvider graphBackedRecursivePackageProvider; |
| 159 | protected ListeningExecutorService executor; |
Mark Schaller | d9d390a | 2016-06-21 22:01:28 +0000 | [diff] [blame] | 160 | private RecursivePackageProviderBackedTargetPatternResolver resolver; |
Googler | 5e65c98 | 2017-08-17 05:21:54 +0200 | [diff] [blame] | 161 | protected final SkyKey universeKey; |
Janak Ramakrishnan | 63edbd3 | 2016-11-11 18:56:25 +0000 | [diff] [blame] | 162 | private final ImmutableList<TargetPatternKey> universeTargetPatternKeys; |
Miguel Alcon Pinto | b45e262 | 2015-08-21 18:31:23 +0000 | [diff] [blame] | 163 | |
Mark Schaller | 4b801f2 | 2016-06-21 22:26:12 +0000 | [diff] [blame] | 164 | public SkyQueryEnvironment( |
| 165 | boolean keepGoing, |
| 166 | int loadingPhaseThreads, |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 167 | ExtendedEventHandler eventHandler, |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 168 | Set<Setting> settings, |
Mark Schaller | 20c7501 | 2016-06-21 22:51:14 +0000 | [diff] [blame] | 169 | Iterable<QueryFunction> extraFunctions, |
| 170 | String parserPrefix, |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 171 | WalkableGraphFactory graphFactory, |
Mark Schaller | 20c7501 | 2016-06-21 22:51:14 +0000 | [diff] [blame] | 172 | List<String> universeScope, |
Mark Schaller | 4f48f1b | 2017-03-10 20:38:43 +0000 | [diff] [blame] | 173 | PathPackageLocator pkgPath, |
| 174 | boolean blockUniverseEvaluationErrors) { |
Nathan Harmata | 2e2b459 | 2016-09-21 17:17:33 +0000 | [diff] [blame] | 175 | this( |
| 176 | keepGoing, |
| 177 | loadingPhaseThreads, |
| 178 | // SkyQueryEnvironment operates on a prepopulated Skyframe graph. Therefore, query |
| 179 | // evaluation is completely CPU-bound. |
| 180 | /*queryEvaluationParallelismLevel=*/ DEFAULT_THREAD_COUNT, |
| 181 | eventHandler, |
| 182 | settings, |
| 183 | extraFunctions, |
Nathan Harmata | 2e2b459 | 2016-09-21 17:17:33 +0000 | [diff] [blame] | 184 | parserPrefix, |
| 185 | graphFactory, |
| 186 | universeScope, |
Mark Schaller | 4f48f1b | 2017-03-10 20:38:43 +0000 | [diff] [blame] | 187 | pkgPath, |
| 188 | blockUniverseEvaluationErrors); |
Nathan Harmata | 2e2b459 | 2016-09-21 17:17:33 +0000 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | protected SkyQueryEnvironment( |
| 192 | boolean keepGoing, |
| 193 | int loadingPhaseThreads, |
| 194 | int queryEvaluationParallelismLevel, |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 195 | ExtendedEventHandler eventHandler, |
Nathan Harmata | 2e2b459 | 2016-09-21 17:17:33 +0000 | [diff] [blame] | 196 | Set<Setting> settings, |
| 197 | Iterable<QueryFunction> extraFunctions, |
Nathan Harmata | 2e2b459 | 2016-09-21 17:17:33 +0000 | [diff] [blame] | 198 | String parserPrefix, |
| 199 | WalkableGraphFactory graphFactory, |
| 200 | List<String> universeScope, |
Mark Schaller | 4f48f1b | 2017-03-10 20:38:43 +0000 | [diff] [blame] | 201 | PathPackageLocator pkgPath, |
| 202 | boolean blockUniverseEvaluationErrors) { |
Mark Schaller | 4b801f2 | 2016-06-21 22:26:12 +0000 | [diff] [blame] | 203 | super( |
| 204 | keepGoing, |
| 205 | /*strictScope=*/ true, |
| 206 | /*labelFilter=*/ Rule.ALL_LABELS, |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 207 | eventHandler, |
| 208 | settings, |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 209 | extraFunctions); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 210 | this.loadingPhaseThreads = loadingPhaseThreads; |
| 211 | this.graphFactory = graphFactory; |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 212 | this.pkgPath = pkgPath; |
Janak Ramakrishnan | e77b6d2 | 2016-11-08 18:44:55 +0000 | [diff] [blame] | 213 | this.universeScope = ImmutableList.copyOf(Preconditions.checkNotNull(universeScope)); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 214 | this.parserPrefix = parserPrefix; |
Nathan Harmata | 71616b1 | 2016-09-28 20:20:48 +0000 | [diff] [blame] | 215 | Preconditions.checkState( |
| 216 | !universeScope.isEmpty(), "No queries can be performed with an empty universe"); |
| 217 | this.queryEvaluationParallelismLevel = queryEvaluationParallelismLevel; |
Janak Ramakrishnan | e77b6d2 | 2016-11-08 18:44:55 +0000 | [diff] [blame] | 218 | this.universeKey = graphFactory.getUniverseKey(universeScope, parserPrefix); |
Mark Schaller | 4f48f1b | 2017-03-10 20:38:43 +0000 | [diff] [blame] | 219 | this.blockUniverseEvaluationErrors = blockUniverseEvaluationErrors; |
| 220 | this.universeEvalEventHandler = |
| 221 | this.blockUniverseEvaluationErrors |
| 222 | ? new ErrorBlockingForwardingEventHandler(this.eventHandler) |
| 223 | : this.eventHandler; |
| 224 | this.universeTargetPatternKeys = |
Janak Ramakrishnan | 63edbd3 | 2016-11-11 18:56:25 +0000 | [diff] [blame] | 225 | PrepareDepsOfPatternsFunction.getTargetPatternKeys( |
| 226 | PrepareDepsOfPatternsFunction.getSkyKeys(universeKey, eventHandler)); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 227 | } |
| 228 | |
mschaller | fe88387 | 2017-07-17 22:40:11 +0200 | [diff] [blame] | 229 | @Override |
| 230 | public void close() { |
| 231 | if (executor != null) { |
| 232 | executor.shutdownNow(); |
| 233 | executor = null; |
| 234 | } |
| 235 | } |
| 236 | |
Googler | 5e65c98 | 2017-08-17 05:21:54 +0200 | [diff] [blame] | 237 | /** Gets roots of graph which contains all nodes needed to evaluate {@code expr}. */ |
| 238 | protected Set<SkyKey> getGraphRootsFromExpression(QueryExpression expr) |
| 239 | throws QueryException, InterruptedException { |
| 240 | return ImmutableSet.of(universeKey); |
| 241 | } |
| 242 | |
nharmata | 6147ebf | 2018-10-09 08:43:49 -0700 | [diff] [blame] | 243 | protected void beforeEvaluateQuery(QueryExpression expr) |
Googler | 5e65c98 | 2017-08-17 05:21:54 +0200 | [diff] [blame] | 244 | throws QueryException, InterruptedException { |
| 245 | Set<SkyKey> roots = getGraphRootsFromExpression(expr); |
Googler | 5e65c98 | 2017-08-17 05:21:54 +0200 | [diff] [blame] | 246 | |
Googler | 5375520 | 2018-02-13 11:37:30 -0800 | [diff] [blame] | 247 | EvaluationResult<SkyValue> result; |
| 248 | try (AutoProfiler p = AutoProfiler.logged("evaluation and walkable graph", logger)) { |
Googler | 1002867 | 2018-10-25 12:14:34 -0700 | [diff] [blame] | 249 | EvaluationContext evaluationContext = |
| 250 | EvaluationContext.newBuilder() |
| 251 | .setNumThreads(loadingPhaseThreads) |
| 252 | .setEventHander(universeEvalEventHandler) |
| 253 | .build(); |
Googler | deb9959 | 2018-10-30 12:23:58 -0700 | [diff] [blame] | 254 | result = graphFactory.prepareAndGet(roots, configureEvaluationContext(evaluationContext)); |
Googler | 5375520 | 2018-02-13 11:37:30 -0800 | [diff] [blame] | 255 | } |
Googler | 57f228a | 2017-12-21 08:10:15 -0800 | [diff] [blame] | 256 | |
Googler | 5375520 | 2018-02-13 11:37:30 -0800 | [diff] [blame] | 257 | if (graph == null || graph != result.getWalkableGraph()) { |
Googler | 61c48b5 | 2017-10-30 17:33:04 -0400 | [diff] [blame] | 258 | checkEvaluationResult(roots, result); |
Janak Ramakrishnan | 3207f93 | 2016-11-16 23:16:53 +0000 | [diff] [blame] | 259 | packageSemaphore = makeFreshPackageMultisetSemaphore(); |
| 260 | graph = result.getWalkableGraph(); |
Googler | 407d393 | 2019-05-16 15:13:59 -0700 | [diff] [blame] | 261 | blacklistPatternsSupplier = MemoizingInterruptibleSupplier.of(new BlacklistSupplier(graph)); |
Janak Ramakrishnan | 3207f93 | 2016-11-16 23:16:53 +0000 | [diff] [blame] | 262 | graphBackedRecursivePackageProvider = |
juliexxia | cc86475 | 2018-06-01 09:03:48 -0700 | [diff] [blame] | 263 | new GraphBackedRecursivePackageProvider( |
| 264 | graph, universeTargetPatternKeys, pkgPath, new TraversalInfoRootPackageExtractor()); |
Eric Fellheimer | a39f8a9 | 2015-07-28 19:11:23 +0000 | [diff] [blame] | 265 | } |
Googler | 57f228a | 2017-12-21 08:10:15 -0800 | [diff] [blame] | 266 | |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 267 | if (executor == null) { |
| 268 | executor = MoreExecutors.listeningDecorator( |
| 269 | new ThreadPoolExecutor( |
| 270 | /*corePoolSize=*/ queryEvaluationParallelismLevel, |
| 271 | /*maximumPoolSize=*/ queryEvaluationParallelismLevel, |
| 272 | /*keepAliveTime=*/ 1, |
| 273 | /*units=*/ TimeUnit.SECONDS, |
| 274 | /*workQueue=*/ new BlockingStack<Runnable>(), |
| 275 | new ThreadFactoryBuilder().setNameFormat("QueryEnvironment %d").build())); |
Janak Ramakrishnan | 3207f93 | 2016-11-16 23:16:53 +0000 | [diff] [blame] | 276 | } |
Mark Schaller | 09f42d5 | 2017-01-20 21:57:32 +0000 | [diff] [blame] | 277 | resolver = |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 278 | new RecursivePackageProviderBackedTargetPatternResolver( |
Mark Schaller | 09f42d5 | 2017-01-20 21:57:32 +0000 | [diff] [blame] | 279 | graphBackedRecursivePackageProvider, |
| 280 | eventHandler, |
ulfjack | e83775d | 2019-05-14 08:58:46 -0700 | [diff] [blame] | 281 | FilteringPolicies.NO_FILTER, |
Mark Schaller | 09f42d5 | 2017-01-20 21:57:32 +0000 | [diff] [blame] | 282 | packageSemaphore); |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Googler | deb9959 | 2018-10-30 12:23:58 -0700 | [diff] [blame] | 285 | /** |
| 286 | * Configures the default {@link EvaluationContext} to change the behavior of how evaluations in |
| 287 | * {@link WalkableGraphFactory#prepareAndGet} work. |
| 288 | */ |
| 289 | protected EvaluationContext configureEvaluationContext(EvaluationContext evaluationContext) { |
| 290 | return evaluationContext; |
| 291 | } |
| 292 | |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 293 | protected MultisetSemaphore<PackageIdentifier> makeFreshPackageMultisetSemaphore() { |
| 294 | return MultisetSemaphore.unbounded(); |
| 295 | } |
| 296 | |
| 297 | @ThreadSafe |
| 298 | public MultisetSemaphore<PackageIdentifier> getPackageMultisetSemaphore() { |
| 299 | return packageSemaphore; |
Mark Schaller | 6cebed6 | 2016-06-27 18:05:39 +0000 | [diff] [blame] | 300 | } |
Janak Ramakrishnan | 958ef82 | 2016-01-07 16:21:39 +0000 | [diff] [blame] | 301 | |
Googler | 61c48b5 | 2017-10-30 17:33:04 -0400 | [diff] [blame] | 302 | protected void checkEvaluationResult(Set<SkyKey> roots, EvaluationResult<SkyValue> result) |
| 303 | throws QueryException { |
| 304 | // If the only root is the universe key, we expect to see either a single successfully evaluated |
| 305 | // value or a cycle in the result. |
| 306 | if (roots.size() == 1 && Iterables.getOnlyElement(roots).equals(universeKey)) { |
| 307 | Collection<SkyValue> values = result.values(); |
| 308 | if (!values.isEmpty()) { |
| 309 | Preconditions.checkState( |
| 310 | values.size() == 1, |
| 311 | "Universe query \"%s\" returned multiple values unexpectedly (%s values in result)", |
| 312 | universeScope, |
| 313 | values.size()); |
| 314 | Preconditions.checkNotNull(result.get(universeKey), result); |
| 315 | } else { |
| 316 | // No values in the result, so there must be an error. We expect the error to be a cycle. |
| 317 | boolean foundCycle = !Iterables.isEmpty(result.getError().getCycleInfo()); |
| 318 | Preconditions.checkState( |
| 319 | foundCycle, |
| 320 | "Universe query \"%s\" failed with non-cycle error: %s", |
| 321 | universeScope, |
| 322 | result.getError()); |
| 323 | } |
Mark Schaller | d7311e0 | 2015-07-07 16:36:09 +0000 | [diff] [blame] | 324 | } |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 325 | } |
| 326 | |
| 327 | @Override |
Nathan Harmata | 2643c8e | 2016-07-01 23:19:23 +0000 | [diff] [blame] | 328 | public final QueryExpression transformParsedQuery(QueryExpression queryExpression) { |
nharmata | 327b911 | 2018-01-31 10:48:31 -0800 | [diff] [blame] | 329 | QueryExpressionMapper<Void> mapper = getQueryExpressionMapper(); |
adgar | d1e1cb7 | 2019-05-14 08:32:51 -0700 | [diff] [blame] | 330 | QueryExpression transformedQueryExpression; |
| 331 | try (AutoProfiler p = |
| 332 | AutoProfiler.logged( |
| 333 | "transforming query", logger, /*minTimeForLoggingInMilliseconds=*/ 50)) { |
| 334 | transformedQueryExpression = queryExpression.accept(mapper); |
| 335 | } |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 336 | logger.info( |
| 337 | String.format( |
| 338 | "transformed query [%s] to [%s]", |
| 339 | Ascii.truncate( |
| 340 | queryExpression.toString(), MAX_QUERY_EXPRESSION_LOG_CHARS, "[truncated]"), |
| 341 | Ascii.truncate( |
| 342 | transformedQueryExpression.toString(), |
| 343 | MAX_QUERY_EXPRESSION_LOG_CHARS, |
| 344 | "[truncated]"))); |
Nathan Harmata | ed93560 | 2016-03-02 01:16:14 +0000 | [diff] [blame] | 345 | return transformedQueryExpression; |
| 346 | } |
| 347 | |
nharmata | 327b911 | 2018-01-31 10:48:31 -0800 | [diff] [blame] | 348 | protected QueryExpressionMapper<Void> getQueryExpressionMapper() { |
Nathan Harmata | 6454347 | 2016-06-30 18:33:40 +0000 | [diff] [blame] | 349 | if (universeScope.size() != 1) { |
Nathan Harmata | 54a16f0 | 2016-12-13 17:49:27 +0000 | [diff] [blame] | 350 | return QueryExpressionMapper.identity(); |
Nathan Harmata | 6454347 | 2016-06-30 18:33:40 +0000 | [diff] [blame] | 351 | } |
| 352 | TargetPattern.Parser targetPatternParser = new TargetPattern.Parser(parserPrefix); |
| 353 | String universeScopePattern = Iterables.getOnlyElement(universeScope); |
Nathan Harmata | 54a16f0 | 2016-12-13 17:49:27 +0000 | [diff] [blame] | 354 | return new RdepsToAllRdepsQueryExpressionMapper(targetPatternParser, universeScopePattern); |
Nathan Harmata | 6454347 | 2016-06-30 18:33:40 +0000 | [diff] [blame] | 355 | } |
| 356 | |
Nathan Harmata | ed93560 | 2016-03-02 01:16:14 +0000 | [diff] [blame] | 357 | @Override |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 358 | protected void evalTopLevelInternal( |
| 359 | QueryExpression expr, OutputFormatterCallback<Target> callback) |
| 360 | throws QueryException, InterruptedException { |
Nathan Harmata | 79e4965 | 2016-12-16 22:00:48 +0000 | [diff] [blame] | 361 | Throwable throwableToThrow = null; |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 362 | try { |
| 363 | super.evalTopLevelInternal(expr, callback); |
Nathan Harmata | 79e4965 | 2016-12-16 22:00:48 +0000 | [diff] [blame] | 364 | } catch (Throwable throwable) { |
| 365 | throwableToThrow = throwable; |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 366 | } finally { |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 367 | if (throwableToThrow != null) { |
lberki | 97abb52 | 2017-09-04 18:51:57 +0200 | [diff] [blame] | 368 | logger.log( |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 369 | Level.INFO, |
| 370 | "About to shutdown query threadpool because of throwable", |
| 371 | throwableToThrow); |
mschaller | 6d155d7 | 2018-08-28 15:12:34 -0700 | [diff] [blame] | 372 | ListeningExecutorService obsoleteExecutor = executor; |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 373 | // Signal that executor must be recreated on the next invocation. |
| 374 | executor = null; |
mschaller | 6d155d7 | 2018-08-28 15:12:34 -0700 | [diff] [blame] | 375 | |
| 376 | // If evaluation failed abruptly (e.g. was interrupted), attempt to terminate all remaining |
| 377 | // tasks and then wait for them all to finish. We don't want to leave any dangling threads |
| 378 | // running tasks. |
| 379 | obsoleteExecutor.shutdownNow(); |
| 380 | boolean interrupted = false; |
| 381 | boolean executorTerminated = false; |
| 382 | try { |
| 383 | while (!executorTerminated) { |
| 384 | try { |
| 385 | executorTerminated = |
| 386 | obsoleteExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS); |
| 387 | } catch (InterruptedException e) { |
| 388 | interrupted = true; |
| 389 | handleInterruptedShutdown(); |
| 390 | } |
| 391 | } |
| 392 | } finally { |
| 393 | if (interrupted) { |
| 394 | Thread.currentThread().interrupt(); |
| 395 | } |
| 396 | } |
| 397 | |
Nathan Harmata | 79e4965 | 2016-12-16 22:00:48 +0000 | [diff] [blame] | 398 | Throwables.propagateIfPossible( |
| 399 | throwableToThrow, QueryException.class, InterruptedException.class); |
Janak Ramakrishnan | 3207f93 | 2016-11-16 23:16:53 +0000 | [diff] [blame] | 400 | } |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 401 | } |
| 402 | } |
| 403 | |
mschaller | 6d155d7 | 2018-08-28 15:12:34 -0700 | [diff] [blame] | 404 | /** |
| 405 | * Subclasses may implement special handling when the query threadpool shutdown process is |
| 406 | * interrupted. This isn't likely to happen unless there's a bug in the lifecycle management of |
| 407 | * query tasks. |
| 408 | */ |
| 409 | protected void handleInterruptedShutdown() {} |
| 410 | |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 411 | @Override |
| 412 | public QueryEvalResult evaluateQuery( |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 413 | QueryExpression expr, ThreadSafeOutputFormatterCallback<Target> callback) |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 414 | throws QueryException, InterruptedException, IOException { |
Googler | 5e65c98 | 2017-08-17 05:21:54 +0200 | [diff] [blame] | 415 | beforeEvaluateQuery(expr); |
Mark Schaller | 4d8baf8 | 2016-06-21 18:56:04 +0000 | [diff] [blame] | 416 | |
| 417 | // SkyQueryEnvironment batches callback invocations using a BatchStreamedCallback, created here |
| 418 | // so that there's one per top-level evaluateQuery call. The batch size is large enough that |
| 419 | // per-call costs of calling the original callback are amortized over a good number of targets, |
| 420 | // and small enough that holding a batch of targets in memory doesn't risk an OOM error. |
| 421 | // |
| 422 | // This flushes the batched callback prior to constructing the QueryEvalResult in the unlikely |
| 423 | // case of a race between the original callback and the eventHandler. |
nharmata | fac7c25 | 2018-09-12 15:31:23 -0700 | [diff] [blame] | 424 | BatchStreamedCallback batchCallback = new BatchStreamedCallback( |
| 425 | callback, |
| 426 | BATCH_CALLBACK_SIZE, |
nharmata | c49e874 | 2018-09-27 11:27:41 -0700 | [diff] [blame] | 427 | createUniquifierForOuterBatchStreamedCallback(expr)); |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 428 | return super.evaluateQuery(expr, batchCallback); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 429 | } |
| 430 | |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 431 | private Map<SkyKey, Collection<Target>> targetifyValues( |
| 432 | Map<SkyKey, ? extends Iterable<SkyKey>> input) throws InterruptedException { |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 433 | return targetifyValues( |
| 434 | input, |
| 435 | makePackageKeyToTargetKeyMap(ImmutableSet.copyOf(Iterables.concat(input.values())))); |
| 436 | } |
| 437 | |
| 438 | private Map<SkyKey, Collection<Target>> targetifyValues( |
| 439 | Map<SkyKey, ? extends Iterable<SkyKey>> input, |
| 440 | Multimap<SkyKey, SkyKey> packageKeyToTargetKeyMap) throws InterruptedException { |
Nathan Harmata | be59799 | 2016-10-10 15:59:00 +0000 | [diff] [blame] | 441 | ImmutableMap.Builder<SkyKey, Collection<Target>> result = ImmutableMap.builder(); |
Googler | d191162 | 2016-06-27 15:43:11 +0000 | [diff] [blame] | 442 | |
Janak Ramakrishnan | e933d5e | 2016-01-08 16:43:54 +0000 | [diff] [blame] | 443 | Map<SkyKey, Target> allTargets = |
nharmata | 7c56dce | 2018-08-16 10:50:26 -0700 | [diff] [blame] | 444 | getTargetKeyToTargetMapForPackageKeyToTargetKeyMap(packageKeyToTargetKeyMap); |
Janak Ramakrishnan | 3685873 | 2015-06-17 16:45:47 +0000 | [diff] [blame] | 445 | |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 446 | for (Map.Entry<SkyKey, ? extends Iterable<SkyKey>> entry : input.entrySet()) { |
Nathan Harmata | be59799 | 2016-10-10 15:59:00 +0000 | [diff] [blame] | 447 | Iterable<SkyKey> skyKeys = entry.getValue(); |
Miguel Alcon Pinto | 933c13a | 2015-09-16 18:37:45 +0000 | [diff] [blame] | 448 | Set<Target> targets = CompactHashSet.createWithExpectedSize(Iterables.size(skyKeys)); |
| 449 | for (SkyKey key : skyKeys) { |
| 450 | Target target = allTargets.get(key); |
| 451 | if (target != null) { |
| 452 | targets.add(target); |
| 453 | } |
| 454 | } |
Nathan Harmata | be59799 | 2016-10-10 15:59:00 +0000 | [diff] [blame] | 455 | result.put(entry.getKey(), targets); |
Janak Ramakrishnan | 3685873 | 2015-06-17 16:45:47 +0000 | [diff] [blame] | 456 | } |
| 457 | return result.build(); |
| 458 | } |
| 459 | |
Nathan Harmata | be59799 | 2016-10-10 15:59:00 +0000 | [diff] [blame] | 460 | private Map<SkyKey, Collection<Target>> getRawReverseDeps( |
| 461 | Iterable<SkyKey> transitiveTraversalKeys) throws InterruptedException { |
| 462 | return targetifyValues(graph.getReverseDeps(transitiveTraversalKeys)); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 463 | } |
| 464 | |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 465 | private Set<Label> getAllowedDeps(Rule rule) throws InterruptedException { |
Miguel Alcon Pinto | 4ffe28d | 2015-08-19 14:29:02 +0000 | [diff] [blame] | 466 | Set<Label> allowedLabels = new HashSet<>(rule.getTransitions(dependencyFilter).values()); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 467 | allowedLabels.addAll(rule.getVisibility().getDependencyLabels()); |
Marian Lobur | fdd788e | 2015-03-25 09:36:28 +0000 | [diff] [blame] | 468 | // We should add deps from aspects, otherwise they are going to be filtered out. |
| 469 | allowedLabels.addAll(rule.getAspectLabelsSuperset(dependencyFilter)); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 470 | return allowedLabels; |
| 471 | } |
| 472 | |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 473 | private Collection<Target> filterFwdDeps(Target target, Collection<Target> rawFwdDeps) |
| 474 | throws InterruptedException { |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 475 | if (!(target instanceof Rule)) { |
Janak Ramakrishnan | 3685873 | 2015-06-17 16:45:47 +0000 | [diff] [blame] | 476 | return rawFwdDeps; |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 477 | } |
| 478 | final Set<Label> allowedLabels = getAllowedDeps((Rule) target); |
Janak Ramakrishnan | 3685873 | 2015-06-17 16:45:47 +0000 | [diff] [blame] | 479 | return Collections2.filter(rawFwdDeps, |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 480 | new Predicate<Target>() { |
| 481 | @Override |
| 482 | public boolean apply(Target target) { |
| 483 | return allowedLabels.contains(target.getLabel()); |
| 484 | } |
| 485 | }); |
| 486 | } |
| 487 | |
nharmata | bf2e2d8 | 2017-06-21 23:12:51 +0200 | [diff] [blame] | 488 | @Override |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 489 | public ThreadSafeMutableSet<Target> getFwdDeps( |
| 490 | Iterable<Target> targets, QueryExpressionContext<Target> context) |
nharmata | bf2e2d8 | 2017-06-21 23:12:51 +0200 | [diff] [blame] | 491 | throws InterruptedException { |
nharmata | 523b575 | 2017-08-10 21:00:52 +0200 | [diff] [blame] | 492 | Map<SkyKey, Target> targetsByKey = Maps.newHashMapWithExpectedSize(Iterables.size(targets)); |
nharmata | bf2e2d8 | 2017-06-21 23:12:51 +0200 | [diff] [blame] | 493 | for (Target target : targets) { |
| 494 | targetsByKey.put(TARGET_TO_SKY_KEY.apply(target), target); |
| 495 | } |
| 496 | Map<SkyKey, Collection<Target>> directDeps = targetifyValues( |
| 497 | graph.getDirectDeps(targetsByKey.keySet())); |
| 498 | if (targetsByKey.keySet().size() != directDeps.keySet().size()) { |
| 499 | Iterable<Label> missingTargets = Iterables.transform( |
| 500 | Sets.difference(targetsByKey.keySet(), directDeps.keySet()), |
| 501 | SKYKEY_TO_LABEL); |
Janak Ramakrishnan | 3533ab5 | 2016-01-14 18:14:09 +0000 | [diff] [blame] | 502 | eventHandler.handle(Event.warn("Targets were missing from graph: " + missingTargets)); |
| 503 | } |
nharmata | bf2e2d8 | 2017-06-21 23:12:51 +0200 | [diff] [blame] | 504 | ThreadSafeMutableSet<Target> result = createThreadSafeMutableSet(); |
| 505 | for (Map.Entry<SkyKey, Collection<Target>> entry : directDeps.entrySet()) { |
| 506 | result.addAll(filterFwdDeps(targetsByKey.get(entry.getKey()), entry.getValue())); |
Janak Ramakrishnan | 73dd230 | 2015-06-16 17:04:25 +0000 | [diff] [blame] | 507 | } |
| 508 | return result; |
| 509 | } |
| 510 | |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 511 | /** |
| 512 | * Returns deps in the form of {@link SkyKey}s. |
| 513 | * |
nharmata | b6bf51d | 2018-07-27 13:49:45 -0700 | [diff] [blame] | 514 | * <p>The implementation of this method does not filter out deps due to disallowed edges, |
| 515 | * therefore callers are responsible for doing the right thing themselves. |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 516 | */ |
nharmata | b6bf51d | 2018-07-27 13:49:45 -0700 | [diff] [blame] | 517 | public Multimap<SkyKey, SkyKey> getUnfilteredDirectDepsOfSkyKeys(Iterable<SkyKey> keys) |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 518 | throws InterruptedException { |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 519 | ImmutableMultimap.Builder<SkyKey, SkyKey> builder = ImmutableMultimap.builder(); |
| 520 | graph.getDirectDeps(keys).forEach(builder::putAll); |
| 521 | return builder.build(); |
| 522 | } |
| 523 | |
Janak Ramakrishnan | 3685873 | 2015-06-17 16:45:47 +0000 | [diff] [blame] | 524 | @Override |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 525 | public Collection<Target> getReverseDeps( |
| 526 | Iterable<Target> targets, QueryExpressionContext<Target> context) |
| 527 | throws InterruptedException { |
Nathan Harmata | be59799 | 2016-10-10 15:59:00 +0000 | [diff] [blame] | 528 | return getReverseDepsOfTransitiveTraversalKeys(Iterables.transform(targets, TARGET_TO_SKY_KEY)); |
| 529 | } |
Miguel Alcon Pinto | b45e262 | 2015-08-21 18:31:23 +0000 | [diff] [blame] | 530 | |
janakr | dc8b2e9a | 2017-08-18 22:52:37 +0200 | [diff] [blame] | 531 | private Collection<Target> getReverseDepsOfTransitiveTraversalKeys( |
Nathan Harmata | be59799 | 2016-10-10 15:59:00 +0000 | [diff] [blame] | 532 | Iterable<SkyKey> transitiveTraversalKeys) throws InterruptedException { |
| 533 | Map<SkyKey, Collection<Target>> rawReverseDeps = getRawReverseDeps(transitiveTraversalKeys); |
Googler | d191162 | 2016-06-27 15:43:11 +0000 | [diff] [blame] | 534 | return processRawReverseDeps(rawReverseDeps); |
| 535 | } |
| 536 | |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 537 | /** Targetify SkyKeys of reverse deps and filter out targets whose deps are not allowed. */ |
| 538 | Collection<Target> filterRawReverseDepsOfTransitiveTraversalKeys( |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 539 | Map<SkyKey, ? extends Iterable<SkyKey>> rawReverseDeps, |
| 540 | Multimap<SkyKey, SkyKey> packageKeyToTargetKeyMap) throws InterruptedException { |
| 541 | return processRawReverseDeps(targetifyValues(rawReverseDeps, packageKeyToTargetKeyMap)); |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Nathan Harmata | be59799 | 2016-10-10 15:59:00 +0000 | [diff] [blame] | 544 | private Collection<Target> processRawReverseDeps(Map<SkyKey, Collection<Target>> rawReverseDeps) |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 545 | throws InterruptedException { |
Googler | d191162 | 2016-06-27 15:43:11 +0000 | [diff] [blame] | 546 | Set<Target> result = CompactHashSet.create(); |
Eric Fellheimer | cfd61cf | 2016-08-10 21:36:02 +0000 | [diff] [blame] | 547 | CompactHashSet<Target> visited = |
| 548 | CompactHashSet.createWithExpectedSize(totalSizeOfCollections(rawReverseDeps.values())); |
Miguel Alcon Pinto | b45e262 | 2015-08-21 18:31:23 +0000 | [diff] [blame] | 549 | |
| 550 | Set<Label> keys = CompactHashSet.create(Collections2.transform(rawReverseDeps.keySet(), |
Nathan Harmata | be59799 | 2016-10-10 15:59:00 +0000 | [diff] [blame] | 551 | SKYKEY_TO_LABEL)); |
Miguel Alcon Pinto | b45e262 | 2015-08-21 18:31:23 +0000 | [diff] [blame] | 552 | for (Collection<Target> parentCollection : rawReverseDeps.values()) { |
| 553 | for (Target parent : parentCollection) { |
| 554 | if (visited.add(parent)) { |
Dmitry Lomov | 6073eb6 | 2016-01-21 21:26:32 +0000 | [diff] [blame] | 555 | if (parent instanceof Rule && dependencyFilter != DependencyFilter.ALL_DEPS) { |
Miguel Alcon Pinto | b45e262 | 2015-08-21 18:31:23 +0000 | [diff] [blame] | 556 | for (Label label : getAllowedDeps((Rule) parent)) { |
| 557 | if (keys.contains(label)) { |
| 558 | result.add(parent); |
| 559 | } |
| 560 | } |
| 561 | } else { |
| 562 | result.add(parent); |
| 563 | } |
| 564 | } |
| 565 | } |
Janak Ramakrishnan | 73dd230 | 2015-06-16 17:04:25 +0000 | [diff] [blame] | 566 | } |
| 567 | return result; |
| 568 | } |
| 569 | |
Eric Fellheimer | cfd61cf | 2016-08-10 21:36:02 +0000 | [diff] [blame] | 570 | private static <T> int totalSizeOfCollections(Iterable<Collection<T>> nestedCollections) { |
| 571 | int totalSize = 0; |
| 572 | for (Collection<T> collection : nestedCollections) { |
| 573 | totalSize += collection.size(); |
| 574 | } |
| 575 | return totalSize; |
| 576 | } |
| 577 | |
Janak Ramakrishnan | 73dd230 | 2015-06-16 17:04:25 +0000 | [diff] [blame] | 578 | @Override |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 579 | public ThreadSafeMutableSet<Target> getTransitiveClosure( |
| 580 | ThreadSafeMutableSet<Target> targets, QueryExpressionContext<Target> context) |
nharmata | bf2e2d8 | 2017-06-21 23:12:51 +0200 | [diff] [blame] | 581 | throws InterruptedException { |
janakr | dc8b2e9a | 2017-08-18 22:52:37 +0200 | [diff] [blame] | 582 | return SkyQueryUtils.getTransitiveClosure( |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 583 | targets, targets1 -> getFwdDeps(targets1, context), createThreadSafeMutableSet()); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 584 | } |
| 585 | |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 586 | @Override |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 587 | public ImmutableList<Target> getNodesOnPath( |
| 588 | Target from, Target to, QueryExpressionContext<Target> context) throws InterruptedException { |
| 589 | return SkyQueryUtils.getNodesOnPath( |
| 590 | from, to, targets -> getFwdDeps(targets, context), Target::getLabel); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 591 | } |
| 592 | |
Googler | b39486c | 2019-01-03 08:58:16 -0800 | [diff] [blame] | 593 | protected final <R> ListenableFuture<R> safeSubmit(Callable<R> callable) { |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 594 | try { |
| 595 | return executor.submit(callable); |
| 596 | } catch (RejectedExecutionException e) { |
| 597 | return Futures.immediateCancelledFuture(); |
Nathan Harmata | f37750a | 2016-09-07 14:58:14 +0000 | [diff] [blame] | 598 | } |
Miguel Alcon Pinto | 42984f3 | 2015-11-06 19:05:13 +0000 | [diff] [blame] | 599 | } |
| 600 | |
nharmata | 2ef8de6 | 2019-04-23 18:30:55 -0700 | [diff] [blame] | 601 | @SuppressWarnings("unchecked") |
| 602 | private <R> ListenableFuture<R> safeSubmitAsync(QueryTaskAsyncCallable<R> callable) { |
Googler | db6e13b | 2017-10-19 17:26:59 +0200 | [diff] [blame] | 603 | try { |
nharmata | 2ef8de6 | 2019-04-23 18:30:55 -0700 | [diff] [blame] | 604 | return Futures.submitAsync(() -> (ListenableFuture<R>) callable.call(), executor); |
Googler | db6e13b | 2017-10-19 17:26:59 +0200 | [diff] [blame] | 605 | } catch (RejectedExecutionException e) { |
| 606 | return Futures.immediateCancelledFuture(); |
| 607 | } |
| 608 | } |
| 609 | |
Nathan Harmata | f37750a | 2016-09-07 14:58:14 +0000 | [diff] [blame] | 610 | @ThreadSafe |
Janak Ramakrishnan | e933d5e | 2016-01-08 16:43:54 +0000 | [diff] [blame] | 611 | @Override |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 612 | public QueryTaskFuture<Void> eval( |
| 613 | final QueryExpression expr, |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 614 | final QueryExpressionContext<Target> context, |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 615 | final Callback<Target> callback) { |
| 616 | // TODO(bazel-team): As in here, use concurrency for the async #eval of other QueryEnvironment |
| 617 | // implementations. |
nharmata | 2ef8de6 | 2019-04-23 18:30:55 -0700 | [diff] [blame] | 618 | return executeAsync(() -> expr.eval(SkyQueryEnvironment.this, context, callback)); |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | @Override |
nharmata | 2ef8de6 | 2019-04-23 18:30:55 -0700 | [diff] [blame] | 622 | public <R> QueryTaskFuture<R> execute(QueryTaskCallable<R> callable) { |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 623 | return QueryTaskFutureImpl.ofDelegate(safeSubmit(callable)); |
| 624 | } |
| 625 | |
| 626 | @Override |
nharmata | 2ef8de6 | 2019-04-23 18:30:55 -0700 | [diff] [blame] | 627 | public <R> QueryTaskFuture<R> executeAsync(QueryTaskAsyncCallable<R> callable) { |
| 628 | return QueryTaskFutureImpl.ofDelegate(safeSubmitAsync(callable)); |
| 629 | } |
| 630 | |
| 631 | @Override |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 632 | public <T1, T2> QueryTaskFuture<T2> transformAsync( |
| 633 | QueryTaskFuture<T1> future, |
| 634 | final Function<T1, QueryTaskFuture<T2>> function) { |
| 635 | return QueryTaskFutureImpl.ofDelegate( |
| 636 | Futures.transformAsync( |
| 637 | (QueryTaskFutureImpl<T1>) future, |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 638 | input -> (QueryTaskFutureImpl<T2>) function.apply(input), |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 639 | executor)); |
| 640 | } |
| 641 | |
| 642 | @Override |
| 643 | public <R> QueryTaskFuture<R> whenAllSucceedCall( |
| 644 | Iterable<? extends QueryTaskFuture<?>> futures, QueryTaskCallable<R> callable) { |
| 645 | return QueryTaskFutureImpl.ofDelegate( |
| 646 | Futures.whenAllSucceed(cast(futures)).call(callable, executor)); |
| 647 | } |
| 648 | |
| 649 | @ThreadSafe |
| 650 | @Override |
nharmata | bf2e2d8 | 2017-06-21 23:12:51 +0200 | [diff] [blame] | 651 | public ThreadSafeMutableSet<Target> createThreadSafeMutableSet() { |
| 652 | return new ThreadSafeMutableKeyExtractorBackedSetImpl<>( |
nharmata | fac7c25 | 2018-09-12 15:31:23 -0700 | [diff] [blame] | 653 | TargetKeyExtractor.INSTANCE, Target.class, queryEvaluationParallelismLevel); |
nharmata | bf2e2d8 | 2017-06-21 23:12:51 +0200 | [diff] [blame] | 654 | } |
| 655 | |
| 656 | @Override |
| 657 | public <V> MutableMap<Target, V> createMutableMap() { |
shreyax | 6871cf0 | 2018-07-02 09:16:18 -0700 | [diff] [blame] | 658 | return new MutableKeyExtractorBackedMapImpl<>(TargetKeyExtractor.INSTANCE); |
nharmata | bf2e2d8 | 2017-06-21 23:12:51 +0200 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | @ThreadSafe |
nharmata | c49e874 | 2018-09-27 11:27:41 -0700 | [diff] [blame] | 662 | protected NonExceptionalUniquifier<Target> createUniquifierForOuterBatchStreamedCallback( |
| 663 | QueryExpression expr) { |
| 664 | return createUniquifier(); |
| 665 | } |
| 666 | |
| 667 | @ThreadSafe |
nharmata | bf2e2d8 | 2017-06-21 23:12:51 +0200 | [diff] [blame] | 668 | @Override |
nharmata | c49e874 | 2018-09-27 11:27:41 -0700 | [diff] [blame] | 669 | public NonExceptionalUniquifier<Target> createUniquifier() { |
nharmata | b9f0802 | 2019-04-23 20:36:10 -0700 | [diff] [blame] | 670 | return new UniquifierImpl<>(TargetKeyExtractor.INSTANCE, queryEvaluationParallelismLevel); |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | @ThreadSafe |
Nathan Harmata | e9826b4 | 2017-03-07 18:05:21 +0000 | [diff] [blame] | 674 | @Override |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 675 | public MinDepthUniquifier<Target> createMinDepthUniquifier() { |
nharmata | fac7c25 | 2018-09-12 15:31:23 -0700 | [diff] [blame] | 676 | return new MinDepthUniquifierImpl<>( |
| 677 | TargetKeyExtractor.INSTANCE, queryEvaluationParallelismLevel); |
Nathan Harmata | e9826b4 | 2017-03-07 18:05:21 +0000 | [diff] [blame] | 678 | } |
| 679 | |
| 680 | @ThreadSafe |
nharmata | fac7c25 | 2018-09-12 15:31:23 -0700 | [diff] [blame] | 681 | public MinDepthUniquifier<SkyKey> createMinDepthSkyKeyUniquifier() { |
| 682 | return new MinDepthUniquifierImpl<>( |
| 683 | SkyKeyKeyExtractor.INSTANCE, queryEvaluationParallelismLevel); |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | @ThreadSafe |
shreyax | 2643d4b | 2018-05-25 11:12:11 -0700 | [diff] [blame] | 687 | public Uniquifier<SkyKey> createSkyKeyUniquifier() { |
nharmata | b9f0802 | 2019-04-23 20:36:10 -0700 | [diff] [blame] | 688 | return new UniquifierImpl<>(SkyKeyKeyExtractor.INSTANCE, queryEvaluationParallelismLevel); |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 689 | } |
| 690 | |
nharmata | de0c535 | 2017-07-25 17:39:09 +0200 | [diff] [blame] | 691 | private ImmutableSet<PathFragment> getBlacklistedExcludes(TargetPatternKey targetPatternKey) |
| 692 | throws InterruptedException { |
| 693 | return targetPatternKey.getAllBlacklistedSubdirectoriesToExclude(blacklistPatternsSupplier); |
Nathan Harmata | 5bd26b2 | 2016-11-14 19:55:50 +0000 | [diff] [blame] | 694 | } |
| 695 | |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 696 | @ThreadSafe |
Miguel Alcon Pinto | 42984f3 | 2015-11-06 19:05:13 +0000 | [diff] [blame] | 697 | @Override |
nharmata | 50f7249 | 2017-08-11 21:31:03 +0200 | [diff] [blame] | 698 | public Collection<Target> getSiblingTargetsInPackage(Target target) { |
| 699 | return target.getPackage().getTargets().values(); |
| 700 | } |
| 701 | |
| 702 | @ThreadSafe |
| 703 | @Override |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 704 | public QueryTaskFuture<Void> getTargetsMatchingPattern( |
nharmata | de0c535 | 2017-07-25 17:39:09 +0200 | [diff] [blame] | 705 | QueryExpression owner, String pattern, Callback<Target> callback) { |
| 706 | TargetPatternKey targetPatternKey; |
Mark Schaller | 77612fd | 2016-06-21 21:04:45 +0000 | [diff] [blame] | 707 | try { |
ulfjack | e83775d | 2019-05-14 08:58:46 -0700 | [diff] [blame] | 708 | targetPatternKey = TargetPatternValue.key(pattern, FilteringPolicies.NO_FILTER, parserPrefix); |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 709 | } catch (TargetParsingException tpe) { |
| 710 | try { |
| 711 | reportBuildFileError(owner, tpe.getMessage()); |
| 712 | } catch (QueryException qe) { |
| 713 | return immediateFailedFuture(qe); |
| 714 | } |
| 715 | return immediateSuccessfulFuture(null); |
nharmata | de0c535 | 2017-07-25 17:39:09 +0200 | [diff] [blame] | 716 | } |
| 717 | return evalTargetPatternKey(owner, targetPatternKey, callback); |
| 718 | } |
| 719 | |
| 720 | @ThreadSafe |
| 721 | public QueryTaskFuture<Void> evalTargetPatternKey( |
| 722 | QueryExpression owner, TargetPatternKey targetPatternKey, Callback<Target> callback) { |
| 723 | ImmutableSet<PathFragment> blacklistedSubdirectoriesToExclude; |
| 724 | try { |
| 725 | blacklistedSubdirectoriesToExclude = getBlacklistedExcludes(targetPatternKey); |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 726 | } catch (InterruptedException ie) { |
| 727 | return immediateCancelledFuture(); |
Nathan Harmata | 5bd26b2 | 2016-11-14 19:55:50 +0000 | [diff] [blame] | 728 | } |
nharmata | de0c535 | 2017-07-25 17:39:09 +0200 | [diff] [blame] | 729 | TargetPattern patternToEval = targetPatternKey.getParsedPattern(); |
| 730 | ImmutableSet<PathFragment> additionalSubdirectoriesToExclude = |
| 731 | targetPatternKey.getExcludedSubdirectories(); |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 732 | AsyncFunction<TargetParsingException, Void> reportBuildFileErrorAsyncFunction = |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 733 | exn -> { |
| 734 | reportBuildFileError(owner, exn.getMessage()); |
| 735 | return Futures.immediateFuture(null); |
| 736 | }; |
shreyax | 432b253 | 2019-01-23 11:16:07 -0800 | [diff] [blame] | 737 | Callback<Target> filteredCallback = callback; |
| 738 | if (!targetPatternKey.getPolicy().equals(NO_FILTER)) { |
| 739 | filteredCallback = |
| 740 | targets -> |
| 741 | callback.process( |
| 742 | Iterables.filter( |
| 743 | targets, |
| 744 | target -> |
| 745 | targetPatternKey.getPolicy().shouldRetain(target, /*explicit=*/ false))); |
| 746 | } |
| 747 | ListenableFuture<Void> evalFuture = |
| 748 | patternToEval.evalAsync( |
| 749 | resolver, |
| 750 | blacklistedSubdirectoriesToExclude, |
| 751 | additionalSubdirectoriesToExclude, |
| 752 | filteredCallback, |
| 753 | QueryException.class, |
| 754 | executor); |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 755 | return QueryTaskFutureImpl.ofDelegate( |
| 756 | Futures.catchingAsync( |
| 757 | evalFuture, |
| 758 | TargetParsingException.class, |
Googler | 73b7b2e | 2017-03-23 17:20:58 +0000 | [diff] [blame] | 759 | reportBuildFileErrorAsyncFunction, |
| 760 | directExecutor())); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 761 | } |
| 762 | |
Nathan Harmata | f37750a | 2016-09-07 14:58:14 +0000 | [diff] [blame] | 763 | @ThreadSafe |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 764 | @Override |
nharmata | bf2e2d8 | 2017-06-21 23:12:51 +0200 | [diff] [blame] | 765 | public ThreadSafeMutableSet<Target> getBuildFiles( |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 766 | QueryExpression caller, |
| 767 | ThreadSafeMutableSet<Target> nodes, |
| 768 | boolean buildFiles, |
| 769 | boolean loads, |
| 770 | QueryExpressionContext<Target> context) |
shreyax | dac4d51 | 2018-05-02 16:26:13 -0700 | [diff] [blame] | 771 | throws QueryException, InterruptedException { |
nharmata | bf2e2d8 | 2017-06-21 23:12:51 +0200 | [diff] [blame] | 772 | ThreadSafeMutableSet<Target> dependentFiles = createThreadSafeMutableSet(); |
| 773 | Set<PackageIdentifier> seenPackages = new HashSet<>(); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 774 | // Keep track of seen labels, to avoid adding a fake subinclude label that also exists as a |
| 775 | // real target. |
| 776 | Set<Label> seenLabels = new HashSet<>(); |
| 777 | |
| 778 | // Adds all the package definition files (BUILD files and build |
| 779 | // extensions) for package "pkg", to "buildfiles". |
| 780 | for (Target x : nodes) { |
| 781 | Package pkg = x.getPackage(); |
nharmata | bf2e2d8 | 2017-06-21 23:12:51 +0200 | [diff] [blame] | 782 | if (seenPackages.add(pkg.getPackageIdentifier())) { |
Han-Wen Nienhuys | c13c002 | 2015-12-15 19:08:38 +0000 | [diff] [blame] | 783 | if (buildFiles) { |
| 784 | addIfUniqueLabel(pkg.getBuildFile(), seenLabels, dependentFiles); |
| 785 | } |
| 786 | |
| 787 | List<Label> extensions = new ArrayList<>(); |
Han-Wen Nienhuys | c13c002 | 2015-12-15 19:08:38 +0000 | [diff] [blame] | 788 | if (loads) { |
| 789 | extensions.addAll(pkg.getSkylarkFileDependencies()); |
| 790 | } |
| 791 | |
nharmata | 51cb8ff | 2018-03-26 10:40:50 -0700 | [diff] [blame] | 792 | for (Label extension : extensions) { |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 793 | |
nharmata | 51cb8ff | 2018-03-26 10:40:50 -0700 | [diff] [blame] | 794 | Target loadTarget = getLoadTarget(extension, pkg); |
| 795 | addIfUniqueLabel(loadTarget, seenLabels, dependentFiles); |
John Cater | e5fb5f0 | 2017-12-04 07:53:19 -0800 | [diff] [blame] | 796 | |
nharmata | 51cb8ff | 2018-03-26 10:40:50 -0700 | [diff] [blame] | 797 | // Also add the BUILD file of the extension. |
Han-Wen Nienhuys | c13c002 | 2015-12-15 19:08:38 +0000 | [diff] [blame] | 798 | if (buildFiles) { |
shreyax | d7829b1 | 2018-04-05 09:30:46 -0700 | [diff] [blame] | 799 | Label buildFileLabel = getBuildFileLabel(loadTarget.getLabel().getPackageIdentifier()); |
John Cater | e5fb5f0 | 2017-12-04 07:53:19 -0800 | [diff] [blame] | 800 | addIfUniqueLabel(new FakeLoadTarget(buildFileLabel, pkg), seenLabels, dependentFiles); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 801 | } |
| 802 | } |
| 803 | } |
| 804 | } |
| 805 | return dependentFiles; |
| 806 | } |
| 807 | |
shreyax | d7829b1 | 2018-04-05 09:30:46 -0700 | [diff] [blame] | 808 | protected Label getBuildFileLabel(PackageIdentifier packageIdentifier) throws QueryException { |
| 809 | // TODO(bazel-team): Try avoid filesystem access here. |
| 810 | Path buildFileForLoad = null; |
| 811 | try { |
| 812 | buildFileForLoad = pkgPath.getPackageBuildFile(packageIdentifier); |
| 813 | } catch (NoSuchPackageException e) { |
| 814 | throw new QueryException(packageIdentifier + " does not exist in graph"); |
| 815 | } |
| 816 | return Label.createUnvalidated(packageIdentifier, buildFileForLoad.getBaseName()); |
| 817 | } |
| 818 | |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 819 | private static void addIfUniqueLabel(Target node, Set<Label> labels, Set<Target> nodes) { |
| 820 | if (labels.add(node.getLabel())) { |
| 821 | nodes.add(node); |
| 822 | } |
| 823 | } |
| 824 | |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 825 | protected int getVisitBatchSizeForParallelVisitation() { |
| 826 | return ParallelSkyQueryUtils.VISIT_BATCH_SIZE; |
| 827 | } |
| 828 | |
nharmata | 51cb8ff | 2018-03-26 10:40:50 -0700 | [diff] [blame] | 829 | private Target getLoadTarget(Label label, Package pkg) { |
juliexxia | 93a3763 | 2017-10-26 00:55:59 +0200 | [diff] [blame] | 830 | return new FakeLoadTarget(label, pkg); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 831 | } |
| 832 | |
nharmata | b9f0802 | 2019-04-23 20:36:10 -0700 | [diff] [blame] | 833 | int getQueryEvaluationParallelismLevel() { |
| 834 | return queryEvaluationParallelismLevel; |
| 835 | } |
| 836 | |
Nathan Harmata | f37750a | 2016-09-07 14:58:14 +0000 | [diff] [blame] | 837 | @ThreadSafe |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 838 | @Override |
| 839 | public TargetAccessor<Target> getAccessor() { |
| 840 | return accessor; |
| 841 | } |
| 842 | |
Nathan Harmata | f37750a | 2016-09-07 14:58:14 +0000 | [diff] [blame] | 843 | @ThreadSafe |
John Cater | e5fb5f0 | 2017-12-04 07:53:19 -0800 | [diff] [blame] | 844 | private Package getPackage(PackageIdentifier packageIdentifier) |
| 845 | throws InterruptedException, QueryException, NoSuchPackageException { |
| 846 | SkyKey packageKey = PackageValue.key(packageIdentifier); |
shreyax | de6e9a8 | 2019-02-19 10:53:17 -0800 | [diff] [blame] | 847 | PackageValue packageValue = (PackageValue) graph.getValue(packageKey); |
| 848 | if (packageValue != null) { |
| 849 | Package pkg = packageValue.getPackage(); |
| 850 | if (pkg.containsErrors()) { |
John Cater | e5fb5f0 | 2017-12-04 07:53:19 -0800 | [diff] [blame] | 851 | throw new BuildFileContainsErrorsException(packageIdentifier); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 852 | } |
shreyax | de6e9a8 | 2019-02-19 10:53:17 -0800 | [diff] [blame] | 853 | return pkg; |
| 854 | } else { |
| 855 | NoSuchPackageException exception = (NoSuchPackageException) graph.getException(packageKey); |
| 856 | if (exception != null) { |
| 857 | throw exception; |
| 858 | } |
| 859 | if (graph.isCycle(packageKey)) { |
| 860 | throw new NoSuchPackageException(packageIdentifier, "Package depends on a cycle"); |
| 861 | } else { |
| 862 | throw new QueryException(packageKey + " does not exist in graph"); |
| 863 | } |
| 864 | } |
John Cater | e5fb5f0 | 2017-12-04 07:53:19 -0800 | [diff] [blame] | 865 | } |
| 866 | |
| 867 | @ThreadSafe |
| 868 | @Override |
| 869 | public Target getTarget(Label label) |
| 870 | throws TargetNotFoundException, QueryException, InterruptedException { |
| 871 | try { |
| 872 | Package pkg = getPackage(label.getPackageIdentifier()); |
| 873 | return pkg.getTarget(label.getName()); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 874 | } catch (NoSuchThingException e) { |
| 875 | throw new TargetNotFoundException(e); |
| 876 | } |
| 877 | } |
| 878 | |
nharmata | 2ef8de6 | 2019-04-23 18:30:55 -0700 | [diff] [blame] | 879 | @Override |
| 880 | public Map<Label, Target> getTargets(Iterable<Label> labels) throws InterruptedException { |
| 881 | Multimap<PackageIdentifier, Label> packageIdToLabelMap = ArrayListMultimap.create(); |
| 882 | labels.forEach(label -> packageIdToLabelMap.put(label.getPackageIdentifier(), label)); |
| 883 | Map<PackageIdentifier, Package> packageIdToPackageMap = |
| 884 | bulkGetPackages(packageIdToLabelMap.keySet()); |
| 885 | ImmutableMap.Builder<Label, Target> resultBuilder = ImmutableMap.builder(); |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 886 | packageSemaphore.acquireAll(packageIdToLabelMap.keySet()); |
| 887 | try { |
| 888 | for (PackageIdentifier pkgId : packageIdToLabelMap.keySet()) { |
| 889 | Package pkg = packageIdToPackageMap.get(pkgId); |
| 890 | if (pkg == null) { |
nharmata | 2ef8de6 | 2019-04-23 18:30:55 -0700 | [diff] [blame] | 891 | continue; |
| 892 | } |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 893 | for (Label label : packageIdToLabelMap.get(pkgId)) { |
| 894 | Target target; |
| 895 | try { |
| 896 | target = pkg.getTarget(label.getName()); |
| 897 | } catch (NoSuchTargetException e) { |
| 898 | continue; |
| 899 | } |
| 900 | resultBuilder.put(label, target); |
| 901 | } |
nharmata | 2ef8de6 | 2019-04-23 18:30:55 -0700 | [diff] [blame] | 902 | } |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 903 | return resultBuilder.build(); |
| 904 | } finally { |
| 905 | packageSemaphore.releaseAll(packageIdToLabelMap.keySet()); |
nharmata | 2ef8de6 | 2019-04-23 18:30:55 -0700 | [diff] [blame] | 906 | } |
nharmata | 2ef8de6 | 2019-04-23 18:30:55 -0700 | [diff] [blame] | 907 | } |
| 908 | |
Nathan Harmata | f37750a | 2016-09-07 14:58:14 +0000 | [diff] [blame] | 909 | @ThreadSafe |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 910 | public Map<PackageIdentifier, Package> bulkGetPackages(Iterable<PackageIdentifier> pkgIds) |
| 911 | throws InterruptedException { |
Nathan Harmata | 6454347 | 2016-06-30 18:33:40 +0000 | [diff] [blame] | 912 | Set<SkyKey> pkgKeys = ImmutableSet.copyOf(PackageValue.keys(pkgIds)); |
| 913 | ImmutableMap.Builder<PackageIdentifier, Package> pkgResults = ImmutableMap.builder(); |
| 914 | Map<SkyKey, SkyValue> packages = graph.getSuccessfulValues(pkgKeys); |
| 915 | for (Map.Entry<SkyKey, SkyValue> pkgEntry : packages.entrySet()) { |
| 916 | PackageIdentifier pkgId = (PackageIdentifier) pkgEntry.getKey().argument(); |
| 917 | PackageValue pkgValue = (PackageValue) pkgEntry.getValue(); |
| 918 | pkgResults.put(pkgId, Preconditions.checkNotNull(pkgValue.getPackage(), pkgId)); |
| 919 | } |
| 920 | return pkgResults.build(); |
| 921 | } |
| 922 | |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 923 | @Override |
nharmata | bf2e2d8 | 2017-06-21 23:12:51 +0200 | [diff] [blame] | 924 | public void buildTransitiveClosure( |
| 925 | QueryExpression caller, |
| 926 | ThreadSafeMutableSet<Target> targets, |
| 927 | int maxDepth) throws QueryException, InterruptedException { |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 928 | // Everything has already been loaded, so here we just check for errors so that we can |
| 929 | // pre-emptively throw/report if needed. |
Mark Schaller | c031a00 | 2015-10-21 18:17:32 +0000 | [diff] [blame] | 930 | Iterable<SkyKey> transitiveTraversalKeys = makeTransitiveTraversalKeys(targets); |
| 931 | ImmutableList.Builder<String> errorMessagesBuilder = ImmutableList.builder(); |
| 932 | |
| 933 | // First, look for errors in the successfully evaluated TransitiveTraversalValues. They may |
| 934 | // have encountered errors that they were able to recover from. |
jcater | 94b8702 | 2018-05-02 09:08:52 -0700 | [diff] [blame] | 935 | Set<Map.Entry<SkyKey, SkyValue>> successfulEntries = |
Mark Schaller | c031a00 | 2015-10-21 18:17:32 +0000 | [diff] [blame] | 936 | graph.getSuccessfulValues(transitiveTraversalKeys).entrySet(); |
jcater | 96d3c91 | 2018-04-20 04:04:06 -0700 | [diff] [blame] | 937 | ImmutableSet.Builder<SkyKey> successfulKeysBuilder = ImmutableSet.builder(); |
jcater | 94b8702 | 2018-05-02 09:08:52 -0700 | [diff] [blame] | 938 | for (Map.Entry<SkyKey, SkyValue> successfulEntry : successfulEntries) { |
Mark Schaller | c031a00 | 2015-10-21 18:17:32 +0000 | [diff] [blame] | 939 | successfulKeysBuilder.add(successfulEntry.getKey()); |
| 940 | TransitiveTraversalValue value = (TransitiveTraversalValue) successfulEntry.getValue(); |
janakr | f091f9c | 2019-03-25 13:42:18 -0700 | [diff] [blame] | 941 | String errorMessage = value.getErrorMessage(); |
| 942 | if (errorMessage != null) { |
| 943 | errorMessagesBuilder.add(errorMessage); |
Mark Schaller | c031a00 | 2015-10-21 18:17:32 +0000 | [diff] [blame] | 944 | } |
| 945 | } |
| 946 | ImmutableSet<SkyKey> successfulKeys = successfulKeysBuilder.build(); |
| 947 | |
| 948 | // Next, look for errors from the unsuccessfully evaluated TransitiveTraversal skyfunctions. |
| 949 | Iterable<SkyKey> unsuccessfulKeys = |
| 950 | Iterables.filter(transitiveTraversalKeys, Predicates.not(Predicates.in(successfulKeys))); |
jcater | 94b8702 | 2018-05-02 09:08:52 -0700 | [diff] [blame] | 951 | Set<Map.Entry<SkyKey, Exception>> errorEntries = |
Mark Schaller | c031a00 | 2015-10-21 18:17:32 +0000 | [diff] [blame] | 952 | graph.getMissingAndExceptions(unsuccessfulKeys).entrySet(); |
| 953 | for (Map.Entry<SkyKey, Exception> entry : errorEntries) { |
Janak Ramakrishnan | f6f0fcc | 2015-06-19 20:24:52 +0000 | [diff] [blame] | 954 | if (entry.getValue() == null) { |
Janak Ramakrishnan | 3533ab5 | 2016-01-14 18:14:09 +0000 | [diff] [blame] | 955 | // Targets may be in the graph because they are not in the universe or depend on cycles. |
| 956 | eventHandler.handle(Event.warn(entry.getKey().argument() + " does not exist in graph")); |
| 957 | } else { |
| 958 | errorMessagesBuilder.add(entry.getValue().getMessage()); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 959 | } |
Mark Schaller | c031a00 | 2015-10-21 18:17:32 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | // Lastly, report all found errors. |
| 963 | ImmutableList<String> errorMessages = errorMessagesBuilder.build(); |
| 964 | for (String errorMessage : errorMessages) { |
| 965 | reportBuildFileError(caller, errorMessage); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 966 | } |
| 967 | } |
| 968 | |
Nathan Harmata | 3fae366 | 2015-04-22 20:10:48 +0000 | [diff] [blame] | 969 | @Override |
Janak Ramakrishnan | ee6208b | 2016-01-07 20:17:41 +0000 | [diff] [blame] | 970 | protected void preloadOrThrow(QueryExpression caller, Collection<String> patterns) |
Janak Ramakrishnan | cbe2634 | 2015-08-17 18:57:57 +0000 | [diff] [blame] | 971 | throws QueryException, TargetParsingException { |
Mark Schaller | 77612fd | 2016-06-21 21:04:45 +0000 | [diff] [blame] | 972 | // SkyQueryEnvironment directly evaluates target patterns in #getTarget and similar methods |
Mark Schaller | 6cebed6 | 2016-06-27 18:05:39 +0000 | [diff] [blame] | 973 | // using its graph, which is prepopulated using the universeScope (see #beforeEvaluateQuery), |
| 974 | // so no preloading of target patterns is necessary. |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 975 | } |
| 976 | |
shreyax | 2643d4b | 2018-05-25 11:12:11 -0700 | [diff] [blame] | 977 | public ExtendedEventHandler getEventHandler() { |
| 978 | return eventHandler; |
| 979 | } |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 980 | |
shreyax | 2643d4b | 2018-05-25 11:12:11 -0700 | [diff] [blame] | 981 | public static final Predicate<SkyKey> IS_TTV = |
| 982 | SkyFunctionName.functionIs(Label.TRANSITIVE_TRAVERSAL); |
| 983 | |
| 984 | public static final Function<SkyKey, Label> SKYKEY_TO_LABEL = |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 985 | skyKey -> IS_TTV.apply(skyKey) ? (Label) skyKey.argument() : null; |
| 986 | |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 987 | static final Function<SkyKey, PackageIdentifier> PACKAGE_SKYKEY_TO_PACKAGE_IDENTIFIER = |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 988 | skyKey -> (PackageIdentifier) skyKey.argument(); |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 989 | |
nharmata | 7c56dce | 2018-08-16 10:50:26 -0700 | [diff] [blame] | 990 | public static Multimap<SkyKey, SkyKey> makePackageKeyToTargetKeyMap(Iterable<SkyKey> keys) { |
Janak Ramakrishnan | e933d5e | 2016-01-08 16:43:54 +0000 | [diff] [blame] | 991 | Multimap<SkyKey, SkyKey> packageKeyToTargetKeyMap = ArrayListMultimap.create(); |
| 992 | for (SkyKey key : keys) { |
| 993 | Label label = SKYKEY_TO_LABEL.apply(key); |
Janak Ramakrishnan | b5a541a | 2015-06-19 20:55:01 +0000 | [diff] [blame] | 994 | if (label == null) { |
Janak Ramakrishnan | 3685873 | 2015-06-17 16:45:47 +0000 | [diff] [blame] | 995 | continue; |
| 996 | } |
Mark Schaller | 4b801f2 | 2016-06-21 22:26:12 +0000 | [diff] [blame] | 997 | packageKeyToTargetKeyMap.put(PackageValue.key(label.getPackageIdentifier()), key); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 998 | } |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 999 | return packageKeyToTargetKeyMap; |
| 1000 | } |
| 1001 | |
nharmata | 7c56dce | 2018-08-16 10:50:26 -0700 | [diff] [blame] | 1002 | public static Set<PackageIdentifier> getPkgIdsNeededForTargetification( |
| 1003 | Multimap<SkyKey, SkyKey> packageKeyToTargetKeyMap) { |
| 1004 | return packageKeyToTargetKeyMap |
| 1005 | .keySet() |
| 1006 | .stream() |
| 1007 | .map(SkyQueryEnvironment.PACKAGE_SKYKEY_TO_PACKAGE_IDENTIFIER) |
| 1008 | .collect(toImmutableSet()); |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | @ThreadSafe |
nharmata | 7c56dce | 2018-08-16 10:50:26 -0700 | [diff] [blame] | 1012 | public Map<SkyKey, Target> getTargetKeyToTargetMapForPackageKeyToTargetKeyMap( |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 1013 | Multimap<SkyKey, SkyKey> packageKeyToTargetKeyMap) throws InterruptedException { |
nharmata | 641db6d | 2018-08-16 14:31:57 -0700 | [diff] [blame] | 1014 | ImmutableMap.Builder<SkyKey, Target> resultBuilder = ImmutableMap.builder(); |
| 1015 | getTargetsForPackageKeyToTargetKeyMapHelper(packageKeyToTargetKeyMap, resultBuilder::put); |
| 1016 | return resultBuilder.build(); |
| 1017 | } |
| 1018 | |
| 1019 | @ThreadSafe |
| 1020 | public Multimap<PackageIdentifier, Target> getPkgIdToTargetMultimapForPackageKeyToTargetKeyMap( |
| 1021 | Multimap<SkyKey, SkyKey> packageKeyToTargetKeyMap) throws InterruptedException { |
| 1022 | Multimap<PackageIdentifier, Target> result = ArrayListMultimap.create(); |
| 1023 | getTargetsForPackageKeyToTargetKeyMapHelper( |
| 1024 | packageKeyToTargetKeyMap, |
| 1025 | (k, t) -> result.put(t.getLabel().getPackageIdentifier(), t)); |
| 1026 | return result; |
| 1027 | } |
| 1028 | |
| 1029 | private void getTargetsForPackageKeyToTargetKeyMapHelper( |
| 1030 | Multimap<SkyKey, SkyKey> packageKeyToTargetKeyMap, |
| 1031 | BiConsumer<SkyKey, Target> targetKeyAndTargetConsumer) throws InterruptedException { |
Googler | 2b50388 | 2016-11-28 21:54:43 +0000 | [diff] [blame] | 1032 | Set<SkyKey> processedTargets = new HashSet<>(); |
Miguel Alcon Pinto | 4582087 | 2015-09-11 19:57:47 +0000 | [diff] [blame] | 1033 | Map<SkyKey, SkyValue> packageMap = graph.getSuccessfulValues(packageKeyToTargetKeyMap.keySet()); |
Janak Ramakrishnan | 3685873 | 2015-06-17 16:45:47 +0000 | [diff] [blame] | 1034 | for (Map.Entry<SkyKey, SkyValue> entry : packageMap.entrySet()) { |
nharmata | 641db6d | 2018-08-16 14:31:57 -0700 | [diff] [blame] | 1035 | Package pkg = ((PackageValue) entry.getValue()).getPackage(); |
Janak Ramakrishnan | e933d5e | 2016-01-08 16:43:54 +0000 | [diff] [blame] | 1036 | for (SkyKey targetKey : packageKeyToTargetKeyMap.get(entry.getKey())) { |
Googler | 2b50388 | 2016-11-28 21:54:43 +0000 | [diff] [blame] | 1037 | if (processedTargets.add(targetKey)) { |
| 1038 | try { |
nharmata | 641db6d | 2018-08-16 14:31:57 -0700 | [diff] [blame] | 1039 | Target target = pkg.getTarget(SKYKEY_TO_LABEL.apply(targetKey).getName()); |
| 1040 | targetKeyAndTargetConsumer.accept(targetKey, target); |
Googler | 2b50388 | 2016-11-28 21:54:43 +0000 | [diff] [blame] | 1041 | } catch (NoSuchTargetException e) { |
| 1042 | // Skip missing target. |
| 1043 | } |
Janak Ramakrishnan | 3685873 | 2015-06-17 16:45:47 +0000 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | } |
Janak Ramakrishnan | 3685873 | 2015-06-17 16:45:47 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
Nathan Harmata | f44211c | 2016-10-10 16:31:18 +0000 | [diff] [blame] | 1049 | static final Function<Target, SkyKey> TARGET_TO_SKY_KEY = |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 1050 | target -> TransitiveTraversalValue.key(target.getLabel()); |
Janak Ramakrishnan | a40e7b7 | 2015-08-20 20:06:16 +0000 | [diff] [blame] | 1051 | |
Nathan Harmata | 3990080 | 2016-09-20 21:06:59 +0000 | [diff] [blame] | 1052 | /** A strict (i.e. non-lazy) variant of {@link #makeTransitiveTraversalKeys}. */ |
| 1053 | public static Iterable<SkyKey> makeTransitiveTraversalKeysStrict(Iterable<Target> targets) { |
| 1054 | return ImmutableList.copyOf(makeTransitiveTraversalKeys(targets)); |
| 1055 | } |
| 1056 | |
Mark Schaller | c031a00 | 2015-10-21 18:17:32 +0000 | [diff] [blame] | 1057 | private static Iterable<SkyKey> makeTransitiveTraversalKeys(Iterable<Target> targets) { |
Janak Ramakrishnan | a40e7b7 | 2015-08-20 20:06:16 +0000 | [diff] [blame] | 1058 | return Iterables.transform(targets, TARGET_TO_SKY_KEY); |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 1059 | } |
| 1060 | |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 1061 | @Override |
| 1062 | public Target getOrCreate(Target target) { |
| 1063 | return target; |
| 1064 | } |
Janak Ramakrishnan | 643063d | 2015-06-25 16:21:49 +0000 | [diff] [blame] | 1065 | |
Janak Ramakrishnan | d802d5b | 2015-08-20 21:05:46 +0000 | [diff] [blame] | 1066 | /** |
Nathan Harmata | c686fd6 | 2016-04-26 17:41:20 +0000 | [diff] [blame] | 1067 | * Returns package lookup keys for looking up the package root for which there may be a relevant |
nharmata | fa9b01e | 2017-11-27 08:16:38 -0800 | [diff] [blame] | 1068 | * (from the perspective of {@link #getRBuildFiles}) {@link FileStateValue} node in the graph for |
Nathan Harmata | c686fd6 | 2016-04-26 17:41:20 +0000 | [diff] [blame] | 1069 | * {@code originalFileFragment}, which is assumed to be a file path. |
| 1070 | * |
nharmata | fa9b01e | 2017-11-27 08:16:38 -0800 | [diff] [blame] | 1071 | * <p>This is a helper function for {@link #getFileStateKeysForFileFragments}. |
Nathan Harmata | c686fd6 | 2016-04-26 17:41:20 +0000 | [diff] [blame] | 1072 | */ |
| 1073 | private static Iterable<SkyKey> getPkgLookupKeysForFile(PathFragment originalFileFragment, |
| 1074 | PathFragment currentPathFragment) { |
| 1075 | if (originalFileFragment.equals(currentPathFragment) |
dannark | be3cefc | 2018-12-13 11:52:45 -0800 | [diff] [blame] | 1076 | && originalFileFragment.equals(LabelConstants.WORKSPACE_FILE_NAME)) { |
mschaller | 391c4cf | 2018-05-16 12:17:14 -0700 | [diff] [blame] | 1077 | // TODO(mschaller): this should not be checked at runtime. These are constants! |
Nathan Harmata | c686fd6 | 2016-04-26 17:41:20 +0000 | [diff] [blame] | 1078 | Preconditions.checkState( |
dannark | be3cefc | 2018-12-13 11:52:45 -0800 | [diff] [blame] | 1079 | LabelConstants.WORKSPACE_FILE_NAME |
| 1080 | .getParentDirectory() |
| 1081 | .equals(PathFragment.EMPTY_FRAGMENT), |
| 1082 | LabelConstants.WORKSPACE_FILE_NAME); |
Nathan Harmata | c686fd6 | 2016-04-26 17:41:20 +0000 | [diff] [blame] | 1083 | return ImmutableList.of( |
dannark | be3cefc | 2018-12-13 11:52:45 -0800 | [diff] [blame] | 1084 | PackageLookupValue.key(LabelConstants.EXTERNAL_PACKAGE_IDENTIFIER), |
Nathan Harmata | c686fd6 | 2016-04-26 17:41:20 +0000 | [diff] [blame] | 1085 | PackageLookupValue.key(PackageIdentifier.createInMainRepo(PathFragment.EMPTY_FRAGMENT))); |
| 1086 | } |
| 1087 | PathFragment parentPathFragment = currentPathFragment.getParentDirectory(); |
| 1088 | return parentPathFragment == null |
mschaller | 391c4cf | 2018-05-16 12:17:14 -0700 | [diff] [blame] | 1089 | ? ImmutableList.of() |
| 1090 | : ImmutableList.of( |
| 1091 | PackageLookupValue.key(PackageIdentifier.createInMainRepo(parentPathFragment))); |
Nathan Harmata | c686fd6 | 2016-04-26 17:41:20 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
| 1094 | /** |
nharmata | fa9b01e | 2017-11-27 08:16:38 -0800 | [diff] [blame] | 1095 | * Returns FileStateValue keys for which there may be relevant (from the perspective of {@link |
Googler | b39486c | 2019-01-03 08:58:16 -0800 | [diff] [blame] | 1096 | * #getRBuildFiles}) FileStateValues in the graph corresponding to the given {@code |
| 1097 | * pathFragments}, which are assumed to be file paths. |
Nathan Harmata | c686fd6 | 2016-04-26 17:41:20 +0000 | [diff] [blame] | 1098 | * |
| 1099 | * <p>To do this, we emulate the {@link ContainingPackageLookupFunction} logic: for each given |
| 1100 | * file path, we look for the nearest ancestor directory (starting with its parent directory), if |
| 1101 | * any, that has a package. The {@link PackageLookupValue} for this package tells us the package |
nharmata | fa9b01e | 2017-11-27 08:16:38 -0800 | [diff] [blame] | 1102 | * root that we should use for the {@link RootedPath} for the {@link FileStateValue} key. |
Googler | d191162 | 2016-06-27 15:43:11 +0000 | [diff] [blame] | 1103 | * |
| 1104 | * <p>Note that there may not be nodes in the graph corresponding to the returned SkyKeys. |
Janak Ramakrishnan | d802d5b | 2015-08-20 21:05:46 +0000 | [diff] [blame] | 1105 | */ |
Googler | b39486c | 2019-01-03 08:58:16 -0800 | [diff] [blame] | 1106 | protected Collection<SkyKey> getFileStateKeysForFileFragments( |
| 1107 | Iterable<PathFragment> pathFragments) throws InterruptedException { |
Janak Ramakrishnan | d802d5b | 2015-08-20 21:05:46 +0000 | [diff] [blame] | 1108 | Set<SkyKey> result = new HashSet<>(); |
| 1109 | Multimap<PathFragment, PathFragment> currentToOriginal = ArrayListMultimap.create(); |
| 1110 | for (PathFragment pathFragment : pathFragments) { |
| 1111 | currentToOriginal.put(pathFragment, pathFragment); |
| 1112 | } |
| 1113 | while (!currentToOriginal.isEmpty()) { |
Nathan Harmata | c686fd6 | 2016-04-26 17:41:20 +0000 | [diff] [blame] | 1114 | Multimap<SkyKey, PathFragment> packageLookupKeysToOriginal = ArrayListMultimap.create(); |
| 1115 | Multimap<SkyKey, PathFragment> packageLookupKeysToCurrent = ArrayListMultimap.create(); |
jcater | 94b8702 | 2018-05-02 09:08:52 -0700 | [diff] [blame] | 1116 | for (Map.Entry<PathFragment, PathFragment> entry : currentToOriginal.entries()) { |
Nathan Harmata | c686fd6 | 2016-04-26 17:41:20 +0000 | [diff] [blame] | 1117 | PathFragment current = entry.getKey(); |
| 1118 | PathFragment original = entry.getValue(); |
| 1119 | for (SkyKey packageLookupKey : getPkgLookupKeysForFile(original, current)) { |
| 1120 | packageLookupKeysToOriginal.put(packageLookupKey, original); |
| 1121 | packageLookupKeysToCurrent.put(packageLookupKey, current); |
| 1122 | } |
Janak Ramakrishnan | d802d5b | 2015-08-20 21:05:46 +0000 | [diff] [blame] | 1123 | } |
Nathan Harmata | c686fd6 | 2016-04-26 17:41:20 +0000 | [diff] [blame] | 1124 | Map<SkyKey, SkyValue> lookupValues = |
| 1125 | graph.getSuccessfulValues(packageLookupKeysToOriginal.keySet()); |
Janak Ramakrishnan | d802d5b | 2015-08-20 21:05:46 +0000 | [diff] [blame] | 1126 | for (Map.Entry<SkyKey, SkyValue> entry : lookupValues.entrySet()) { |
Nathan Harmata | c686fd6 | 2016-04-26 17:41:20 +0000 | [diff] [blame] | 1127 | SkyKey packageLookupKey = entry.getKey(); |
Janak Ramakrishnan | d802d5b | 2015-08-20 21:05:46 +0000 | [diff] [blame] | 1128 | PackageLookupValue packageLookupValue = (PackageLookupValue) entry.getValue(); |
Janak Ramakrishnan | d802d5b | 2015-08-20 21:05:46 +0000 | [diff] [blame] | 1129 | if (packageLookupValue.packageExists()) { |
Nathan Harmata | c686fd6 | 2016-04-26 17:41:20 +0000 | [diff] [blame] | 1130 | Collection<PathFragment> originalFiles = |
| 1131 | packageLookupKeysToOriginal.get(packageLookupKey); |
Janak Ramakrishnan | d802d5b | 2015-08-20 21:05:46 +0000 | [diff] [blame] | 1132 | Preconditions.checkState(!originalFiles.isEmpty(), entry); |
| 1133 | for (PathFragment fileName : originalFiles) { |
nharmata | fa9b01e | 2017-11-27 08:16:38 -0800 | [diff] [blame] | 1134 | result.add(FileStateValue.key( |
| 1135 | RootedPath.toRootedPath(packageLookupValue.getRoot(), fileName))); |
Janak Ramakrishnan | d802d5b | 2015-08-20 21:05:46 +0000 | [diff] [blame] | 1136 | } |
Nathan Harmata | c686fd6 | 2016-04-26 17:41:20 +0000 | [diff] [blame] | 1137 | for (PathFragment current : packageLookupKeysToCurrent.get(packageLookupKey)) { |
| 1138 | currentToOriginal.removeAll(current); |
| 1139 | } |
Janak Ramakrishnan | d802d5b | 2015-08-20 21:05:46 +0000 | [diff] [blame] | 1140 | } |
Janak Ramakrishnan | d802d5b | 2015-08-20 21:05:46 +0000 | [diff] [blame] | 1141 | } |
| 1142 | Multimap<PathFragment, PathFragment> newCurrentToOriginal = ArrayListMultimap.create(); |
| 1143 | for (PathFragment pathFragment : currentToOriginal.keySet()) { |
| 1144 | PathFragment parent = pathFragment.getParentDirectory(); |
| 1145 | if (parent != null) { |
| 1146 | newCurrentToOriginal.putAll(parent, currentToOriginal.get(pathFragment)); |
| 1147 | } |
| 1148 | } |
| 1149 | currentToOriginal = newCurrentToOriginal; |
| 1150 | } |
| 1151 | return result; |
| 1152 | } |
nharmata | 1bd4aaf | 2017-10-31 11:23:04 -0400 | [diff] [blame] | 1153 | |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 1154 | protected Iterable<Target> getBuildFileTargetsForPackageKeys( |
| 1155 | Set<PackageIdentifier> pkgIds, QueryExpressionContext<Target> context) |
nharmata | 1bd4aaf | 2017-10-31 11:23:04 -0400 | [diff] [blame] | 1156 | throws QueryException, InterruptedException { |
nharmata | 1bd4aaf | 2017-10-31 11:23:04 -0400 | [diff] [blame] | 1157 | packageSemaphore.acquireAll(pkgIds); |
| 1158 | try { |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 1159 | return Iterables.transform( |
| 1160 | graph.getSuccessfulValues(PackageValue.keys(pkgIds)).values(), |
| 1161 | skyValue -> ((PackageValue) skyValue).getPackage().getBuildFile()); |
nharmata | 1bd4aaf | 2017-10-31 11:23:04 -0400 | [diff] [blame] | 1162 | } finally { |
| 1163 | packageSemaphore.releaseAll(pkgIds); |
| 1164 | } |
| 1165 | } |
| 1166 | |
mschaller | 096b707 | 2018-05-01 14:49:22 -0700 | [diff] [blame] | 1167 | /** |
nharmata | 20efb2a | 2018-09-28 12:54:12 -0700 | [diff] [blame] | 1168 | * Calculates the set of packages whose evaluation transitively depends on (e.g. via 'load' |
| 1169 | * statements) the contents of the specified paths. The emitted {@link Target}s are BUILD file |
| 1170 | * targets. |
mschaller | 096b707 | 2018-05-01 14:49:22 -0700 | [diff] [blame] | 1171 | */ |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 1172 | @ThreadSafe |
mschaller | 096b707 | 2018-05-01 14:49:22 -0700 | [diff] [blame] | 1173 | QueryTaskFuture<Void> getRBuildFiles( |
nharmata | 20efb2a | 2018-09-28 12:54:12 -0700 | [diff] [blame] | 1174 | Collection<PathFragment> fileIdentifiers, |
| 1175 | QueryExpressionContext<Target> context, |
| 1176 | Callback<Target> callback) { |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 1177 | return QueryTaskFutureImpl.ofDelegate( |
| 1178 | safeSubmit( |
| 1179 | () -> { |
| 1180 | ParallelSkyQueryUtils.getRBuildFilesParallel( |
nharmata | 20efb2a | 2018-09-28 12:54:12 -0700 | [diff] [blame] | 1181 | SkyQueryEnvironment.this, fileIdentifiers, context, callback); |
laurentlb | 3d2a68c | 2017-06-30 00:32:04 +0200 | [diff] [blame] | 1182 | return null; |
| 1183 | })); |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 1184 | } |
| 1185 | |
Janak Ramakrishnan | 643063d | 2015-06-25 16:21:49 +0000 | [diff] [blame] | 1186 | @Override |
| 1187 | public Iterable<QueryFunction> getFunctions() { |
| 1188 | return ImmutableList.<QueryFunction>builder() |
Janak Ramakrishnan | d802d5b | 2015-08-20 21:05:46 +0000 | [diff] [blame] | 1189 | .addAll(super.getFunctions()) |
| 1190 | .add(new AllRdepsFunction()) |
| 1191 | .add(new RBuildFilesFunction()) |
| 1192 | .build(); |
Janak Ramakrishnan | 643063d | 2015-06-25 16:21:49 +0000 | [diff] [blame] | 1193 | } |
Miguel Alcon Pinto | 47ea948 | 2015-11-18 16:05:17 +0000 | [diff] [blame] | 1194 | |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 1195 | private static class BlacklistSupplier |
| 1196 | implements InterruptibleSupplier<ImmutableSet<PathFragment>> { |
Mark Schaller | 6cebed6 | 2016-06-27 18:05:39 +0000 | [diff] [blame] | 1197 | private final WalkableGraph graph; |
| 1198 | |
Googler | 5e65c98 | 2017-08-17 05:21:54 +0200 | [diff] [blame] | 1199 | private BlacklistSupplier(WalkableGraph graph) { |
Mark Schaller | 6cebed6 | 2016-06-27 18:05:39 +0000 | [diff] [blame] | 1200 | this.graph = graph; |
| 1201 | } |
| 1202 | |
| 1203 | @Override |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 1204 | public ImmutableSet<PathFragment> get() throws InterruptedException { |
Mark Schaller | 6cebed6 | 2016-06-27 18:05:39 +0000 | [diff] [blame] | 1205 | return ((BlacklistedPackagePrefixesValue) |
| 1206 | graph.getValue(BlacklistedPackagePrefixesValue.key())) |
| 1207 | .getPatterns(); |
| 1208 | } |
| 1209 | } |
| 1210 | |
Nathan Harmata | e9826b4 | 2017-03-07 18:05:21 +0000 | [diff] [blame] | 1211 | private static class SkyKeyKeyExtractor implements KeyExtractor<SkyKey, SkyKey> { |
| 1212 | private static final SkyKeyKeyExtractor INSTANCE = new SkyKeyKeyExtractor(); |
| 1213 | |
| 1214 | private SkyKeyKeyExtractor() { |
Nathan Harmata | 29bc3fb | 2016-09-09 16:11:12 +0000 | [diff] [blame] | 1215 | } |
Mark Schaller | 20c7501 | 2016-06-21 22:51:14 +0000 | [diff] [blame] | 1216 | |
| 1217 | @Override |
Nathan Harmata | e9826b4 | 2017-03-07 18:05:21 +0000 | [diff] [blame] | 1218 | public SkyKey extractKey(SkyKey element) { |
Nathan Harmata | 29bc3fb | 2016-09-09 16:11:12 +0000 | [diff] [blame] | 1219 | return element; |
Mark Schaller | 20c7501 | 2016-06-21 22:51:14 +0000 | [diff] [blame] | 1220 | } |
| 1221 | } |
| 1222 | |
Mark Schaller | 4d8baf8 | 2016-06-21 18:56:04 +0000 | [diff] [blame] | 1223 | /** |
| 1224 | * Wraps a {@link Callback} and guarantees that all calls to the original will have at least |
| 1225 | * {@code batchThreshold} {@link Target}s, except for the final such call. |
| 1226 | * |
| 1227 | * <p>Retains fewer than {@code batchThreshold} {@link Target}s at a time. |
| 1228 | * |
| 1229 | * <p>After this object's {@link #process} has been called for the last time, {#link |
| 1230 | * #processLastPending} must be called to "flush" any remaining {@link Target}s through to the |
| 1231 | * original. |
Mark Schaller | 20c7501 | 2016-06-21 22:51:14 +0000 | [diff] [blame] | 1232 | * |
Googler | b3610d5 | 2016-10-24 19:18:36 +0000 | [diff] [blame] | 1233 | * <p>This callback may be called from multiple threads concurrently. At most one thread will call |
| 1234 | * the wrapped {@code callback} concurrently. |
Mark Schaller | 4d8baf8 | 2016-06-21 18:56:04 +0000 | [diff] [blame] | 1235 | */ |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 1236 | // TODO(nharmata): For queries with less than {@code batchThreshold} results, this batching |
| 1237 | // strategy probably hurts performance since we can only start formatting results once the entire |
| 1238 | // query is finished. |
nharmata | 1bd4aaf | 2017-10-31 11:23:04 -0400 | [diff] [blame] | 1239 | // TODO(nharmata): This batching strategy is also potentially harmful from a memory perspective |
| 1240 | // since when the Targets being output are backed by Package instances, we're delaying GC of the |
| 1241 | // Package instances until the output batch size is met. |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 1242 | private static class BatchStreamedCallback extends ThreadSafeOutputFormatterCallback<Target> |
| 1243 | implements Callback<Target> { |
Miguel Alcon Pinto | 47ea948 | 2015-11-18 16:05:17 +0000 | [diff] [blame] | 1244 | |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 1245 | // TODO(nharmata): Now that we know the wrapped callback is ThreadSafe, there's no correctness |
| 1246 | // concern that requires the prohibition of concurrent uses of the callback; the only concern is |
| 1247 | // memory. We should have a threshold for when to invoke the callback with a batch, and also a |
Mark Schaller | 4f48f1b | 2017-03-10 20:38:43 +0000 | [diff] [blame] | 1248 | // separate, larger, bound on the number of targets being processed at the same time. |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 1249 | private final ThreadSafeOutputFormatterCallback<Target> callback; |
nharmata | c49e874 | 2018-09-27 11:27:41 -0700 | [diff] [blame] | 1250 | private final NonExceptionalUniquifier<Target> uniquifier; |
Mark Schaller | 20c7501 | 2016-06-21 22:51:14 +0000 | [diff] [blame] | 1251 | private final Object pendingLock = new Object(); |
Miguel Alcon Pinto | 47ea948 | 2015-11-18 16:05:17 +0000 | [diff] [blame] | 1252 | private List<Target> pending = new ArrayList<>(); |
| 1253 | private int batchThreshold; |
| 1254 | |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 1255 | private BatchStreamedCallback( |
| 1256 | ThreadSafeOutputFormatterCallback<Target> callback, |
nharmata | fac7c25 | 2018-09-12 15:31:23 -0700 | [diff] [blame] | 1257 | int batchThreshold, |
nharmata | c49e874 | 2018-09-27 11:27:41 -0700 | [diff] [blame] | 1258 | NonExceptionalUniquifier<Target> uniquifier) { |
Miguel Alcon Pinto | 47ea948 | 2015-11-18 16:05:17 +0000 | [diff] [blame] | 1259 | this.callback = callback; |
| 1260 | this.batchThreshold = batchThreshold; |
nharmata | fac7c25 | 2018-09-12 15:31:23 -0700 | [diff] [blame] | 1261 | this.uniquifier = uniquifier; |
Miguel Alcon Pinto | 47ea948 | 2015-11-18 16:05:17 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | @Override |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 1265 | public void start() throws IOException { |
| 1266 | callback.start(); |
| 1267 | } |
| 1268 | |
| 1269 | @Override |
| 1270 | public void processOutput(Iterable<Target> partialResult) |
| 1271 | throws IOException, InterruptedException { |
Mark Schaller | 20c7501 | 2016-06-21 22:51:14 +0000 | [diff] [blame] | 1272 | ImmutableList<Target> uniquifiedTargets = uniquifier.unique(partialResult); |
nharmata | 2e8d43d | 2019-01-15 10:21:14 -0800 | [diff] [blame] | 1273 | Iterable<Target> toProcess = null; |
Mark Schaller | 20c7501 | 2016-06-21 22:51:14 +0000 | [diff] [blame] | 1274 | synchronized (pendingLock) { |
| 1275 | Preconditions.checkNotNull(pending, "Reuse of the callback is not allowed"); |
| 1276 | pending.addAll(uniquifiedTargets); |
| 1277 | if (pending.size() >= batchThreshold) { |
nharmata | 2e8d43d | 2019-01-15 10:21:14 -0800 | [diff] [blame] | 1278 | toProcess = pending; |
Mark Schaller | 20c7501 | 2016-06-21 22:51:14 +0000 | [diff] [blame] | 1279 | pending = new ArrayList<>(); |
| 1280 | } |
Miguel Alcon Pinto | 47ea948 | 2015-11-18 16:05:17 +0000 | [diff] [blame] | 1281 | } |
nharmata | 2e8d43d | 2019-01-15 10:21:14 -0800 | [diff] [blame] | 1282 | if (toProcess != null) { |
| 1283 | callback.processOutput(toProcess); |
| 1284 | } |
Miguel Alcon Pinto | 47ea948 | 2015-11-18 16:05:17 +0000 | [diff] [blame] | 1285 | } |
| 1286 | |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 1287 | @Override |
Nathan Harmata | a2565aa | 2016-11-17 22:37:33 +0000 | [diff] [blame] | 1288 | public void close(boolean failFast) throws IOException, InterruptedException { |
| 1289 | if (!failFast) { |
| 1290 | processLastPending(); |
| 1291 | } |
| 1292 | callback.close(failFast); |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | private void processLastPending() throws IOException, InterruptedException { |
Mark Schaller | 20c7501 | 2016-06-21 22:51:14 +0000 | [diff] [blame] | 1296 | synchronized (pendingLock) { |
| 1297 | if (!pending.isEmpty()) { |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 1298 | callback.processOutput(pending); |
Mark Schaller | 20c7501 | 2016-06-21 22:51:14 +0000 | [diff] [blame] | 1299 | pending = null; |
| 1300 | } |
Miguel Alcon Pinto | 47ea948 | 2015-11-18 16:05:17 +0000 | [diff] [blame] | 1301 | } |
| 1302 | } |
| 1303 | } |
Googler | d191162 | 2016-06-27 15:43:11 +0000 | [diff] [blame] | 1304 | |
Nathan Harmata | f37750a | 2016-09-07 14:58:14 +0000 | [diff] [blame] | 1305 | @ThreadSafe |
Googler | d191162 | 2016-06-27 15:43:11 +0000 | [diff] [blame] | 1306 | @Override |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 1307 | public QueryTaskFuture<Void> getAllRdepsUnboundedParallel( |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 1308 | QueryExpression expression, |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 1309 | QueryExpressionContext<Target> context, |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 1310 | Callback<Target> callback) { |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 1311 | return ParallelSkyQueryUtils.getAllRdepsUnboundedParallel(this, expression, context, callback); |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 1312 | } |
| 1313 | |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 1314 | @ThreadSafe |
Googler | 96f95cc | 2017-09-02 00:54:18 +0200 | [diff] [blame] | 1315 | @Override |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 1316 | public QueryTaskFuture<Void> getAllRdepsBoundedParallel( |
Googler | 96f95cc | 2017-09-02 00:54:18 +0200 | [diff] [blame] | 1317 | QueryExpression expression, |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 1318 | int depth, |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 1319 | QueryExpressionContext<Target> context, |
Googler | 96f95cc | 2017-09-02 00:54:18 +0200 | [diff] [blame] | 1320 | Callback<Target> callback) { |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 1321 | return ParallelSkyQueryUtils.getAllRdepsBoundedParallel( |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 1322 | this, expression, depth, context, callback); |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 1323 | } |
| 1324 | |
nharmata | b6bf51d | 2018-07-27 13:49:45 -0700 | [diff] [blame] | 1325 | protected QueryTaskFuture<Predicate<SkyKey>> getUnfilteredUniverseDTCSkyKeyPredicateFuture( |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 1326 | QueryExpression universe, QueryExpressionContext<Target> context) { |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 1327 | return ParallelSkyQueryUtils.getDTCSkyKeyPredicateFuture( |
| 1328 | this, |
| 1329 | universe, |
| 1330 | context, |
| 1331 | BATCH_CALLBACK_SIZE, |
nharmata | fac7c25 | 2018-09-12 15:31:23 -0700 | [diff] [blame] | 1332 | queryEvaluationParallelismLevel); |
Googler | 96f95cc | 2017-09-02 00:54:18 +0200 | [diff] [blame] | 1333 | } |
| 1334 | |
Nathan Harmata | 593dc52 | 2016-09-28 23:35:46 +0000 | [diff] [blame] | 1335 | @ThreadSafe |
| 1336 | @Override |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 1337 | public QueryTaskFuture<Void> getRdepsUnboundedParallel( |
Googler | d191162 | 2016-06-27 15:43:11 +0000 | [diff] [blame] | 1338 | QueryExpression expression, |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 1339 | QueryExpression universe, |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 1340 | QueryExpressionContext<Target> context, |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 1341 | Callback<Target> callback) { |
| 1342 | return transformAsync( |
nharmata | 10911f1 | 2018-08-08 07:58:21 -0700 | [diff] [blame] | 1343 | // Even if we need to do edge filtering, it's fine to construct the rdeps universe via an |
| 1344 | // unfiltered DTC visitation; the subsequent rdeps visitation will perform the edge |
| 1345 | // filtering. |
nharmata | b6bf51d | 2018-07-27 13:49:45 -0700 | [diff] [blame] | 1346 | getUnfilteredUniverseDTCSkyKeyPredicateFuture(universe, context), |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 1347 | unfilteredUniversePredicate -> |
| 1348 | ParallelSkyQueryUtils.getRdepsInUniverseUnboundedParallel( |
| 1349 | this, expression, unfilteredUniversePredicate, context, callback)); |
Googler | d191162 | 2016-06-27 15:43:11 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
shreyax | 2643d4b | 2018-05-25 11:12:11 -0700 | [diff] [blame] | 1352 | @Override |
| 1353 | public QueryTaskFuture<Void> getDepsUnboundedParallel( |
| 1354 | QueryExpression expression, |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 1355 | QueryExpressionContext<Target> context, |
nharmata | f59022b | 2018-07-30 10:37:48 -0700 | [diff] [blame] | 1356 | Callback<Target> callback) { |
shreyax | 2643d4b | 2018-05-25 11:12:11 -0700 | [diff] [blame] | 1357 | return ParallelSkyQueryUtils.getDepsUnboundedParallel( |
| 1358 | SkyQueryEnvironment.this, |
| 1359 | expression, |
| 1360 | context, |
| 1361 | callback, |
nharmata | f59022b | 2018-07-30 10:37:48 -0700 | [diff] [blame] | 1362 | /*depsNeedFiltering=*/ !dependencyFilter.equals(DependencyFilter.ALL_DEPS)); |
shreyax | 2643d4b | 2018-05-25 11:12:11 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 1365 | @ThreadSafe |
| 1366 | @Override |
| 1367 | public QueryTaskFuture<Void> getRdepsBoundedParallel( |
Googler | d191162 | 2016-06-27 15:43:11 +0000 | [diff] [blame] | 1368 | QueryExpression expression, |
Nathan Harmata | bc47f40 | 2016-07-13 16:22:30 +0000 | [diff] [blame] | 1369 | int depth, |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 1370 | QueryExpression universe, |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 1371 | QueryExpressionContext<Target> context, |
nharmata | 398e6dab | 2018-04-12 15:31:26 -0700 | [diff] [blame] | 1372 | Callback<Target> callback) { |
| 1373 | return transformAsync( |
nharmata | 10911f1 | 2018-08-08 07:58:21 -0700 | [diff] [blame] | 1374 | // Even if we need to do edge filtering, it's fine to construct the rdeps universe via an |
| 1375 | // unfiltered DTC visitation; the subsequent rdeps visitation will perform the edge |
| 1376 | // filtering. |
nharmata | b6bf51d | 2018-07-27 13:49:45 -0700 | [diff] [blame] | 1377 | getUnfilteredUniverseDTCSkyKeyPredicateFuture(universe, context), |
shreyax | 76e4e42 | 2019-06-13 12:15:45 -0700 | [diff] [blame] | 1378 | universePredicate -> |
| 1379 | ParallelSkyQueryUtils.getRdepsInUniverseBoundedParallel( |
| 1380 | this, expression, depth, universePredicate, context, callback)); |
Googler | d191162 | 2016-06-27 15:43:11 +0000 | [diff] [blame] | 1381 | } |
Mark Schaller | 4f48f1b | 2017-03-10 20:38:43 +0000 | [diff] [blame] | 1382 | |
| 1383 | /** |
| 1384 | * Query evaluation behavior is specified with respect to errors it emits. (Or at least it should |
| 1385 | * be. Tools rely on it.) Notably, errors that occur during evaluation of a query's universe must |
| 1386 | * not be emitted during query command evaluation. Consider the case of a simple single target |
| 1387 | * query when {@code //...} is the universe: errors in far flung parts of the workspace should not |
| 1388 | * be emitted when that query command is evaluated. |
| 1389 | * |
| 1390 | * <p>Non-error message events are not specified. For instance, it's useful (and expected by some |
| 1391 | * unit tests that should know better) for query commands to emit {@link EventKind#PROGRESS} |
| 1392 | * events during package loading. |
| 1393 | * |
| 1394 | * <p>Therefore, this class is used to forward only non-{@link EventKind#ERROR} events during |
| 1395 | * universe loading to the {@link SkyQueryEnvironment}'s {@link ExtendedEventHandler}. |
| 1396 | */ |
| 1397 | protected static class ErrorBlockingForwardingEventHandler extends DelegatingEventHandler { |
| 1398 | |
| 1399 | public ErrorBlockingForwardingEventHandler(ExtendedEventHandler delegate) { |
| 1400 | super(delegate); |
| 1401 | } |
| 1402 | |
| 1403 | @Override |
| 1404 | public void handle(Event e) { |
| 1405 | if (!e.getKind().equals(EventKind.ERROR)) { |
| 1406 | super.handle(e); |
| 1407 | } |
| 1408 | } |
| 1409 | } |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 1410 | } |