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