Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +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.skyframe; |
| 15 | |
Googler | 16b2be1 | 2024-03-06 10:04:22 -0800 | [diff] [blame] | 16 | import static com.google.common.base.Throwables.throwIfInstanceOf; |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 17 | import static com.google.common.util.concurrent.Futures.immediateCancelledFuture; |
| 18 | import static com.google.common.util.concurrent.Futures.immediateFailedFuture; |
Googler | 3d37b4c | 2017-04-25 04:19:01 +0200 | [diff] [blame] | 19 | import static com.google.common.util.concurrent.MoreExecutors.directExecutor; |
Eric Fellheimer | 8c40daa | 2016-01-11 17:31:26 +0000 | [diff] [blame] | 20 | |
Eric Fellheimer | 6f8b7ce | 2016-01-29 00:15:44 +0000 | [diff] [blame] | 21 | import com.google.common.base.Throwables; |
Eric Fellheimer | 7b8dbeb | 2015-12-30 15:21:43 +0000 | [diff] [blame] | 22 | import com.google.common.collect.ImmutableMap; |
Mark Schaller | f6e32d6 | 2015-05-19 20:27:43 +0000 | [diff] [blame] | 23 | import com.google.common.collect.ImmutableSet; |
Eric Fellheimer | 7b8dbeb | 2015-12-30 15:21:43 +0000 | [diff] [blame] | 24 | import com.google.common.collect.Iterables; |
Eric Fellheimer | fb60143 | 2016-03-07 21:14:44 +0000 | [diff] [blame] | 25 | import com.google.common.collect.Sets; |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 26 | import com.google.common.util.concurrent.Futures; |
| 27 | import com.google.common.util.concurrent.ListenableFuture; |
| 28 | import com.google.common.util.concurrent.ListeningExecutorService; |
Nathan Harmata | 5bd26b2 | 2016-11-14 19:55:50 +0000 | [diff] [blame] | 29 | import com.google.common.util.concurrent.MoreExecutors; |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 30 | import com.google.common.util.concurrent.Uninterruptibles; |
janakr | be52530 | 2021-11-16 14:45:58 -0800 | [diff] [blame] | 31 | import com.google.devtools.build.lib.cmdline.BatchCallback; |
| 32 | import com.google.devtools.build.lib.cmdline.BatchCallback.SafeBatchCallback; |
Googler | 216ea78 | 2024-10-15 04:08:21 -0700 | [diff] [blame] | 33 | import com.google.devtools.build.lib.cmdline.IgnoredSubdirectories; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.cmdline.Label; |
Kristina Chodorow | 73fa203 | 2015-08-28 17:57:46 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.cmdline.PackageIdentifier; |
janakr | be52530 | 2021-11-16 14:45:58 -0800 | [diff] [blame] | 36 | import com.google.devtools.build.lib.cmdline.QueryExceptionMarkerInterface; |
Kristina Chodorow | ec5c07a | 2016-01-25 17:12:29 +0000 | [diff] [blame] | 37 | import com.google.devtools.build.lib.cmdline.RepositoryName; |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 38 | import com.google.devtools.build.lib.cmdline.ResolvedTargets; |
| 39 | import com.google.devtools.build.lib.cmdline.TargetParsingException; |
| 40 | import com.google.devtools.build.lib.cmdline.TargetPatternResolver; |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 41 | import com.google.devtools.build.lib.concurrent.MultisetSemaphore; |
Eric Fellheimer | 6f8b7ce | 2016-01-29 00:15:44 +0000 | [diff] [blame] | 42 | import com.google.devtools.build.lib.concurrent.ThreadSafety.ThreadCompatible; |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 43 | import com.google.devtools.build.lib.events.Event; |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 44 | import com.google.devtools.build.lib.events.ExtendedEventHandler; |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 45 | import com.google.devtools.build.lib.io.InconsistentFilesystemException; |
| 46 | import com.google.devtools.build.lib.io.ProcessPackageDirectoryException; |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 47 | import com.google.devtools.build.lib.packages.NoSuchPackageException; |
| 48 | import com.google.devtools.build.lib.packages.NoSuchThingException; |
| 49 | import com.google.devtools.build.lib.packages.Package; |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 50 | import com.google.devtools.build.lib.packages.Target; |
| 51 | import com.google.devtools.build.lib.pkgcache.FilteringPolicies; |
| 52 | import com.google.devtools.build.lib.pkgcache.FilteringPolicy; |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 53 | import com.google.devtools.build.lib.pkgcache.RecursivePackageProvider; |
| 54 | import com.google.devtools.build.lib.pkgcache.TargetPatternResolverUtil; |
juliexxia | de07246 | 2021-01-07 07:53:28 -0800 | [diff] [blame] | 55 | import com.google.devtools.build.lib.query2.engine.QueryException; |
Googler | 35b92d0 | 2020-08-03 07:42:11 -0700 | [diff] [blame] | 56 | import com.google.devtools.build.lib.server.FailureDetails.TargetPatterns; |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 57 | import com.google.devtools.build.lib.vfs.PathFragment; |
Janak Ramakrishnan | 5b5f22a | 2016-01-07 17:07:29 +0000 | [diff] [blame] | 58 | import java.util.ArrayList; |
ulfjack | 53a0ff1 | 2019-05-21 07:08:40 -0700 | [diff] [blame] | 59 | import java.util.Collection; |
Janak Ramakrishnan | 5b5f22a | 2016-01-07 17:07:29 +0000 | [diff] [blame] | 60 | import java.util.List; |
Eric Fellheimer | 7b8dbeb | 2015-12-30 15:21:43 +0000 | [diff] [blame] | 61 | import java.util.Map; |
Googler | 2a5a004 | 2024-10-16 07:11:44 -0700 | [diff] [blame] | 62 | import java.util.Optional; |
adgar | 0da823c | 2019-07-29 07:48:23 -0700 | [diff] [blame] | 63 | import java.util.concurrent.Callable; |
Mark Schaller | d9d390a | 2016-06-21 22:01:28 +0000 | [diff] [blame] | 64 | import java.util.concurrent.ExecutionException; |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 65 | import java.util.concurrent.Future; |
Googler | 2a5a004 | 2024-10-16 07:11:44 -0700 | [diff] [blame] | 66 | import java.util.concurrent.Semaphore; |
Googler | 5cc598c | 2022-07-06 03:29:45 -0700 | [diff] [blame] | 67 | import javax.annotation.Nullable; |
Eric Fellheimer | 7b8dbeb | 2015-12-30 15:21:43 +0000 | [diff] [blame] | 68 | |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 69 | /** A {@link TargetPatternResolver} backed by a {@link RecursivePackageProvider}. */ |
Eric Fellheimer | 6f8b7ce | 2016-01-29 00:15:44 +0000 | [diff] [blame] | 70 | @ThreadCompatible |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 71 | public final class RecursivePackageProviderBackedTargetPatternResolver |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 72 | extends TargetPatternResolver<Target> { |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 73 | |
Janak Ramakrishnan | 5b5f22a | 2016-01-07 17:07:29 +0000 | [diff] [blame] | 74 | // TODO(janakr): Move this to a more generic place and unify with SkyQueryEnvironment's value? |
adgar | 566b738 | 2019-08-23 08:49:45 -0700 | [diff] [blame] | 75 | static final int MAX_PACKAGES_BULK_GET = 1000; |
Janak Ramakrishnan | 5b5f22a | 2016-01-07 17:07:29 +0000 | [diff] [blame] | 76 | |
Googler | 77ba65e | 2017-06-26 19:55:09 +0200 | [diff] [blame] | 77 | protected final FilteringPolicy policy; |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 78 | private final RecursivePackageProvider recursivePackageProvider; |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 79 | private final ExtendedEventHandler eventHandler; |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 80 | private final MultisetSemaphore<PackageIdentifier> packageSemaphore; |
Googler | 2a5a004 | 2024-10-16 07:11:44 -0700 | [diff] [blame] | 81 | |
| 82 | @Nullable private final Semaphore getTargetsTaskSemaphore; |
nharmata | fbdd593 | 2021-05-20 12:07:09 -0700 | [diff] [blame] | 83 | private final PackageIdentifierBatchingCallback.Factory packageIdentifierBatchingCallbackFactory; |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 84 | |
| 85 | public RecursivePackageProviderBackedTargetPatternResolver( |
Mark Schaller | 8ca9131 | 2015-06-22 22:04:02 +0000 | [diff] [blame] | 86 | RecursivePackageProvider recursivePackageProvider, |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 87 | ExtendedEventHandler eventHandler, |
Eric Fellheimer | 6f8b7ce | 2016-01-29 00:15:44 +0000 | [diff] [blame] | 88 | FilteringPolicy policy, |
nharmata | fbdd593 | 2021-05-20 12:07:09 -0700 | [diff] [blame] | 89 | MultisetSemaphore<PackageIdentifier> packageSemaphore, |
Googler | 2a5a004 | 2024-10-16 07:11:44 -0700 | [diff] [blame] | 90 | Optional<Integer> maxConcurrentGetTargetsTasks, |
nharmata | fbdd593 | 2021-05-20 12:07:09 -0700 | [diff] [blame] | 91 | PackageIdentifierBatchingCallback.Factory packageIdentifierBatchingCallbackFactory) { |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 92 | this.recursivePackageProvider = recursivePackageProvider; |
| 93 | this.eventHandler = eventHandler; |
| 94 | this.policy = policy; |
Nathan Harmata | 41b5417 | 2016-11-10 18:54:09 +0000 | [diff] [blame] | 95 | this.packageSemaphore = packageSemaphore; |
Googler | 2a5a004 | 2024-10-16 07:11:44 -0700 | [diff] [blame] | 96 | this.getTargetsTaskSemaphore = |
| 97 | maxConcurrentGetTargetsTasks.isPresent() |
| 98 | ? new Semaphore(maxConcurrentGetTargetsTasks.get()) |
| 99 | : null; |
nharmata | fbdd593 | 2021-05-20 12:07:09 -0700 | [diff] [blame] | 100 | this.packageIdentifierBatchingCallbackFactory = packageIdentifierBatchingCallbackFactory; |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 101 | } |
| 102 | |
| 103 | @Override |
| 104 | public void warn(String msg) { |
| 105 | eventHandler.handle(Event.warn(msg)); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Gets a {@link Package} from the {@link RecursivePackageProvider}. May return a {@link Package} |
| 110 | * that has errors. |
| 111 | */ |
| 112 | private Package getPackage(PackageIdentifier pkgIdentifier) |
| 113 | throws NoSuchPackageException, InterruptedException { |
Janak Ramakrishnan | 0a4c6e4 | 2015-09-17 00:37:58 +0000 | [diff] [blame] | 114 | return recursivePackageProvider.getPackage(eventHandler, pkgIdentifier); |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 115 | } |
| 116 | |
Eric Fellheimer | 7b8dbeb | 2015-12-30 15:21:43 +0000 | [diff] [blame] | 117 | private Map<PackageIdentifier, Package> bulkGetPackages(Iterable<PackageIdentifier> pkgIds) |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 118 | throws NoSuchPackageException, InterruptedException { |
Mark Schaller | fb2d38b | 2017-03-10 23:01:45 +0000 | [diff] [blame] | 119 | return recursivePackageProvider.bulkGetPackages(pkgIds); |
Eric Fellheimer | 7b8dbeb | 2015-12-30 15:21:43 +0000 | [diff] [blame] | 120 | } |
| 121 | |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 122 | @Override |
Googler | 5cc598c | 2022-07-06 03:29:45 -0700 | [diff] [blame] | 123 | @Nullable |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 124 | public Target getTargetOrNull(Label label) |
| 125 | throws InterruptedException, InconsistentFilesystemException { |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 126 | try { |
Lukacs Berki | 10e3b2b | 2015-09-22 07:58:20 +0000 | [diff] [blame] | 127 | if (!isPackage(label.getPackageIdentifier())) { |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 128 | return null; |
| 129 | } |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 130 | return recursivePackageProvider.getTarget(eventHandler, label); |
Lukacs Berki | 960dc27 | 2015-09-24 07:48:36 +0000 | [diff] [blame] | 131 | } catch (NoSuchThingException e) { |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 132 | return null; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | @Override |
Lukacs Berki | 960dc27 | 2015-09-24 07:48:36 +0000 | [diff] [blame] | 137 | public ResolvedTargets<Target> getExplicitTarget(Label label) |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 138 | throws TargetParsingException, InterruptedException { |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 139 | try { |
| 140 | Target target = recursivePackageProvider.getTarget(eventHandler, label); |
| 141 | return policy.shouldRetain(target, true) |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 142 | ? ResolvedTargets.of(target) |
Googler | 54be05a | 2020-06-29 09:20:32 -0700 | [diff] [blame] | 143 | : ResolvedTargets.empty(); |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 144 | } catch (NoSuchThingException e) { |
Googler | 35b92d0 | 2020-08-03 07:42:11 -0700 | [diff] [blame] | 145 | throw new TargetParsingException(e.getMessage(), e, e.getDetailedExitCode()); |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | @Override |
ulfjack | 53a0ff1 | 2019-05-21 07:08:40 -0700 | [diff] [blame] | 150 | public Collection<Target> getTargetsInPackage( |
Lukacs Berki | 10e3b2b | 2015-09-22 07:58:20 +0000 | [diff] [blame] | 151 | String originalPattern, PackageIdentifier packageIdentifier, boolean rulesOnly) |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 152 | throws TargetParsingException, InterruptedException { |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 153 | FilteringPolicy actualPolicy = |
| 154 | rulesOnly ? FilteringPolicies.and(FilteringPolicies.RULES_ONLY, policy) : policy; |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 155 | try { |
Lukacs Berki | 10e3b2b | 2015-09-22 07:58:20 +0000 | [diff] [blame] | 156 | Package pkg = getPackage(packageIdentifier); |
Googler | 12c52a8 | 2023-10-23 10:16:20 -0700 | [diff] [blame] | 157 | Package.maybeAddPackageContainsErrorsEventToHandler(pkg, eventHandler); |
shreyax | 159a611 | 2018-06-12 15:34:09 -0700 | [diff] [blame] | 158 | return TargetPatternResolverUtil.resolvePackageTargets(pkg, actualPolicy); |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 159 | } catch (NoSuchThingException e) { |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 160 | String message = |
| 161 | TargetPatternResolverUtil.getParsingErrorMessage(e.getMessage(), originalPattern); |
Googler | 35b92d0 | 2020-08-03 07:42:11 -0700 | [diff] [blame] | 162 | throw new TargetParsingException(message, e, e.getDetailedExitCode()); |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
ulfjack | 53a0ff1 | 2019-05-21 07:08:40 -0700 | [diff] [blame] | 166 | private Map<PackageIdentifier, Collection<Target>> bulkGetTargetsInPackage( |
| 167 | String originalPattern, Iterable<PackageIdentifier> pkgIds, FilteringPolicy policy) |
| 168 | throws InterruptedException { |
Eric Fellheimer | 7b8dbeb | 2015-12-30 15:21:43 +0000 | [diff] [blame] | 169 | try { |
| 170 | Map<PackageIdentifier, Package> pkgs = bulkGetPackages(pkgIds); |
Eric Fellheimer | fb60143 | 2016-03-07 21:14:44 +0000 | [diff] [blame] | 171 | if (pkgs.size() != Iterables.size(pkgIds)) { |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 172 | throw new IllegalStateException( |
| 173 | "Bulk package retrieval missing results: " |
| 174 | + Sets.difference(ImmutableSet.copyOf(pkgIds), pkgs.keySet())); |
Eric Fellheimer | fb60143 | 2016-03-07 21:14:44 +0000 | [diff] [blame] | 175 | } |
ulfjack | 53a0ff1 | 2019-05-21 07:08:40 -0700 | [diff] [blame] | 176 | ImmutableMap.Builder<PackageIdentifier, Collection<Target>> result = ImmutableMap.builder(); |
Eric Fellheimer | 7b8dbeb | 2015-12-30 15:21:43 +0000 | [diff] [blame] | 177 | for (PackageIdentifier pkgId : pkgIds) { |
| 178 | Package pkg = pkgs.get(pkgId); |
Googler | 12c52a8 | 2023-10-23 10:16:20 -0700 | [diff] [blame] | 179 | Package.maybeAddPackageContainsErrorsEventToHandler(pkg, eventHandler); |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 180 | result.put(pkgId, TargetPatternResolverUtil.resolvePackageTargets(pkg, policy)); |
Eric Fellheimer | 7b8dbeb | 2015-12-30 15:21:43 +0000 | [diff] [blame] | 181 | } |
Googler | 3cd5f84 | 2022-01-31 17:07:10 -0800 | [diff] [blame] | 182 | return result.buildOrThrow(); |
Eric Fellheimer | 7b8dbeb | 2015-12-30 15:21:43 +0000 | [diff] [blame] | 183 | } catch (NoSuchThingException e) { |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 184 | String message = |
| 185 | TargetPatternResolverUtil.getParsingErrorMessage(e.getMessage(), originalPattern); |
Eric Fellheimer | fb60143 | 2016-03-07 21:14:44 +0000 | [diff] [blame] | 186 | throw new IllegalStateException( |
| 187 | "Mismatch: Expected given pkgIds to correspond to valid Packages. " + message, e); |
Eric Fellheimer | 7b8dbeb | 2015-12-30 15:21:43 +0000 | [diff] [blame] | 188 | } |
| 189 | } |
| 190 | |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 191 | @Override |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 192 | public boolean isPackage(PackageIdentifier packageIdentifier) |
| 193 | throws InterruptedException, InconsistentFilesystemException { |
Lukacs Berki | 10e3b2b | 2015-09-22 07:58:20 +0000 | [diff] [blame] | 194 | return recursivePackageProvider.isPackage(eventHandler, packageIdentifier); |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | @Override |
| 198 | public String getTargetKind(Target target) { |
| 199 | return target.getTargetKind(); |
| 200 | } |
| 201 | |
| 202 | @Override |
janakr | be52530 | 2021-11-16 14:45:58 -0800 | [diff] [blame] | 203 | public <E extends Exception & QueryExceptionMarkerInterface> void findTargetsBeneathDirectory( |
Janak Ramakrishnan | 5b5f22a | 2016-01-07 17:07:29 +0000 | [diff] [blame] | 204 | final RepositoryName repository, |
Eric Fellheimer | 6f8b7ce | 2016-01-29 00:15:44 +0000 | [diff] [blame] | 205 | final String originalPattern, |
Janak Ramakrishnan | 5b5f22a | 2016-01-07 17:07:29 +0000 | [diff] [blame] | 206 | String directory, |
| 207 | boolean rulesOnly, |
Googler | 216ea78 | 2024-10-15 04:08:21 -0700 | [diff] [blame] | 208 | IgnoredSubdirectories forbiddenSubdirectories, |
Janak Ramakrishnan | 3d9441b | 2016-01-13 17:38:29 +0000 | [diff] [blame] | 209 | ImmutableSet<PathFragment> excludedSubdirectories, |
Nathan Harmata | 5bd26b2 | 2016-11-14 19:55:50 +0000 | [diff] [blame] | 210 | BatchCallback<Target, E> callback, |
| 211 | Class<E> exceptionClass) |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 212 | throws TargetParsingException, E, InterruptedException, ProcessPackageDirectoryException { |
| 213 | ListenableFuture<Void> future; |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 214 | try { |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 215 | future = |
| 216 | findTargetsBeneathDirectoryAsyncImpl( |
Googler | 54be05a | 2020-06-29 09:20:32 -0700 | [diff] [blame] | 217 | repository, |
| 218 | originalPattern, |
| 219 | directory, |
| 220 | rulesOnly, |
aiuto | 1469a75 | 2021-06-17 17:46:15 -0700 | [diff] [blame] | 221 | forbiddenSubdirectories, |
Googler | 54be05a | 2020-06-29 09:20:32 -0700 | [diff] [blame] | 222 | excludedSubdirectories, |
| 223 | callback, |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 224 | MoreExecutors.newDirectExecutorService()); |
| 225 | } catch (QueryException e) { |
Googler | 16b2be1 | 2024-03-06 10:04:22 -0800 | [diff] [blame] | 226 | throwIfInstanceOf(e, exceptionClass); |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 227 | throw new IllegalStateException(e); |
| 228 | } catch (NoSuchPackageException e) { |
| 229 | // Can happen during a Skyframe no-keep-going evaluation. |
| 230 | throw new TargetParsingException( |
| 231 | "error loading package under directory '" + directory + "': " + e.getMessage(), |
| 232 | e, |
| 233 | e.getDetailedExitCode()); |
| 234 | } |
| 235 | if (!isSuccessful(future)) { |
| 236 | // Don't get the future if it finished successfully: all that will do is throw an |
| 237 | // interrupted exception if this thread was interrupted, but that's not helpful for a done |
| 238 | // future. |
| 239 | try { |
| 240 | future.get(); |
| 241 | } catch (ExecutionException e) { |
| 242 | Throwables.propagateIfPossible(e.getCause(), InterruptedException.class, exceptionClass); |
| 243 | throw new IllegalStateException(e.getCause()); |
| 244 | } |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 245 | } |
| 246 | } |
| 247 | |
| 248 | @Override |
janakr | be52530 | 2021-11-16 14:45:58 -0800 | [diff] [blame] | 249 | public <E extends Exception & QueryExceptionMarkerInterface> |
| 250 | ListenableFuture<Void> findTargetsBeneathDirectoryAsync( |
| 251 | RepositoryName repository, |
| 252 | String originalPattern, |
| 253 | String directory, |
| 254 | boolean rulesOnly, |
Googler | 216ea78 | 2024-10-15 04:08:21 -0700 | [diff] [blame] | 255 | IgnoredSubdirectories forbiddenSubdirectories, |
janakr | be52530 | 2021-11-16 14:45:58 -0800 | [diff] [blame] | 256 | ImmutableSet<PathFragment> excludedSubdirectories, |
| 257 | BatchCallback<Target, E> callback, |
| 258 | Class<E> exceptionClass, |
| 259 | ListeningExecutorService executor) { |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 260 | try { |
| 261 | return findTargetsBeneathDirectoryAsyncImpl( |
| 262 | repository, |
| 263 | originalPattern, |
| 264 | directory, |
| 265 | rulesOnly, |
| 266 | forbiddenSubdirectories, |
| 267 | excludedSubdirectories, |
| 268 | callback, |
| 269 | executor); |
| 270 | } catch (TargetParsingException e) { |
| 271 | return immediateFailedFuture(e); |
| 272 | } catch (InterruptedException e) { |
| 273 | return immediateCancelledFuture(); |
| 274 | } catch (ProcessPackageDirectoryException | NoSuchPackageException e) { |
| 275 | throw new IllegalStateException( |
| 276 | "Async find targets beneath directory isn't called from within Skyframe: traversing " |
| 277 | + directory |
| 278 | + " for " |
| 279 | + originalPattern, |
| 280 | e); |
| 281 | } catch (QueryException e) { |
| 282 | if (exceptionClass.isInstance(e)) { |
| 283 | return immediateFailedFuture(e); |
| 284 | } |
| 285 | throw new IllegalStateException(e); |
| 286 | } |
Nathan Harmata | 5bd26b2 | 2016-11-14 19:55:50 +0000 | [diff] [blame] | 287 | } |
| 288 | |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 289 | /** |
| 290 | * The returned future may throw {@link QueryException} (if {@code E} is {@link QueryException}) |
| 291 | * or {@link InterruptedException} on retrieval, but no other exceptions. |
| 292 | */ |
janakr | be52530 | 2021-11-16 14:45:58 -0800 | [diff] [blame] | 293 | private <E extends Exception & QueryExceptionMarkerInterface> |
| 294 | ListenableFuture<Void> findTargetsBeneathDirectoryAsyncImpl( |
| 295 | RepositoryName repository, |
| 296 | String pattern, |
| 297 | String directory, |
| 298 | boolean rulesOnly, |
Googler | 216ea78 | 2024-10-15 04:08:21 -0700 | [diff] [blame] | 299 | IgnoredSubdirectories forbiddenSubdirectories, |
janakr | be52530 | 2021-11-16 14:45:58 -0800 | [diff] [blame] | 300 | ImmutableSet<PathFragment> excludedSubdirectories, |
| 301 | BatchCallback<Target, E> callback, |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 302 | ListeningExecutorService executor) |
Googler | 216ea78 | 2024-10-15 04:08:21 -0700 | [diff] [blame] | 303 | throws TargetParsingException, |
| 304 | QueryException, |
| 305 | InterruptedException, |
| 306 | ProcessPackageDirectoryException, |
| 307 | NoSuchPackageException { |
adgar | 0da823c | 2019-07-29 07:48:23 -0700 | [diff] [blame] | 308 | FilteringPolicy actualPolicy = |
| 309 | rulesOnly ? FilteringPolicies.and(FilteringPolicies.RULES_ONLY, policy) : policy; |
adgar | 566b738 | 2019-08-23 08:49:45 -0700 | [diff] [blame] | 310 | |
adgar | 0da823c | 2019-07-29 07:48:23 -0700 | [diff] [blame] | 311 | ArrayList<ListenableFuture<Void>> futures = new ArrayList<>(); |
janakr | be52530 | 2021-11-16 14:45:58 -0800 | [diff] [blame] | 312 | SafeBatchCallback<PackageIdentifier> getPackageTargetsCallback = |
adgar | 0da823c | 2019-07-29 07:48:23 -0700 | [diff] [blame] | 313 | (pkgIdBatch) -> |
| 314 | futures.add( |
| 315 | executor.submit( |
| 316 | new GetTargetsInPackagesTask<>(pkgIdBatch, pattern, actualPolicy, callback))); |
adgar | 566b738 | 2019-08-23 08:49:45 -0700 | [diff] [blame] | 317 | |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 318 | PathFragment pathFragment = TargetPatternResolverUtil.getPathFragment(directory); |
adgar | 566b738 | 2019-08-23 08:49:45 -0700 | [diff] [blame] | 319 | try (PackageIdentifierBatchingCallback batchingCallback = |
nharmata | fbdd593 | 2021-05-20 12:07:09 -0700 | [diff] [blame] | 320 | packageIdentifierBatchingCallbackFactory.create( |
| 321 | getPackageTargetsCallback, MAX_PACKAGES_BULK_GET)) { |
adgar | 0da823c | 2019-07-29 07:48:23 -0700 | [diff] [blame] | 322 | recursivePackageProvider.streamPackagesUnderDirectory( |
adgar | 566b738 | 2019-08-23 08:49:45 -0700 | [diff] [blame] | 323 | batchingCallback, |
adgar | 0da823c | 2019-07-29 07:48:23 -0700 | [diff] [blame] | 324 | eventHandler, |
| 325 | repository, |
| 326 | pathFragment, |
aiuto | 1469a75 | 2021-06-17 17:46:15 -0700 | [diff] [blame] | 327 | forbiddenSubdirectories, |
adgar | 0da823c | 2019-07-29 07:48:23 -0700 | [diff] [blame] | 328 | excludedSubdirectories); |
Nathan Harmata | 7a5a236 | 2017-03-08 22:42:01 +0000 | [diff] [blame] | 329 | } |
adgar | 0da823c | 2019-07-29 07:48:23 -0700 | [diff] [blame] | 330 | if (futures.isEmpty()) { |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 331 | throw new TargetParsingException( |
| 332 | "no targets found beneath '" + pathFragment + "'", TargetPatterns.Code.TARGETS_MISSING); |
shreyax | 91e0bae | 2018-11-06 15:42:13 -0800 | [diff] [blame] | 333 | } |
shreyax | 91e0bae | 2018-11-06 15:42:13 -0800 | [diff] [blame] | 334 | return Futures.whenAllSucceed(futures).call(() -> null, directExecutor()); |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 335 | } |
Eric Fellheimer | 8c40daa | 2016-01-11 17:31:26 +0000 | [diff] [blame] | 336 | |
adgar | 0da823c | 2019-07-29 07:48:23 -0700 | [diff] [blame] | 337 | /** |
| 338 | * Task to get all matching targets in the given packages, filter them, and pass them to the |
| 339 | * target batch callback. |
| 340 | */ |
janakr | be52530 | 2021-11-16 14:45:58 -0800 | [diff] [blame] | 341 | private class GetTargetsInPackagesTask<E extends Exception & QueryExceptionMarkerInterface> |
| 342 | implements Callable<Void> { |
adgar | 0da823c | 2019-07-29 07:48:23 -0700 | [diff] [blame] | 343 | |
| 344 | private final Iterable<PackageIdentifier> packageIdentifiers; |
| 345 | private final String originalPattern; |
| 346 | private final FilteringPolicy actualPolicy; |
Googler | 54be05a | 2020-06-29 09:20:32 -0700 | [diff] [blame] | 347 | private final BatchCallback<Target, E> callback; |
adgar | 0da823c | 2019-07-29 07:48:23 -0700 | [diff] [blame] | 348 | |
| 349 | GetTargetsInPackagesTask( |
| 350 | Iterable<PackageIdentifier> packageIdentifiers, |
| 351 | String originalPattern, |
| 352 | FilteringPolicy actualPolicy, |
Googler | 54be05a | 2020-06-29 09:20:32 -0700 | [diff] [blame] | 353 | BatchCallback<Target, E> callback) { |
adgar | 0da823c | 2019-07-29 07:48:23 -0700 | [diff] [blame] | 354 | this.packageIdentifiers = packageIdentifiers; |
| 355 | this.originalPattern = originalPattern; |
| 356 | this.actualPolicy = actualPolicy; |
| 357 | this.callback = callback; |
| 358 | } |
| 359 | |
Googler | 2a5a004 | 2024-10-16 07:11:44 -0700 | [diff] [blame] | 360 | private void acquireTaskLock() throws InterruptedException { |
| 361 | if (getTargetsTaskSemaphore != null) { |
| 362 | getTargetsTaskSemaphore.acquire(); |
| 363 | } |
| 364 | } |
| 365 | |
| 366 | private void releaseTaskLock() { |
| 367 | if (getTargetsTaskSemaphore != null) { |
| 368 | getTargetsTaskSemaphore.release(); |
| 369 | } |
| 370 | } |
| 371 | |
adgar | 0da823c | 2019-07-29 07:48:23 -0700 | [diff] [blame] | 372 | @Override |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 373 | public Void call() throws E, InterruptedException { |
adgar | 0da823c | 2019-07-29 07:48:23 -0700 | [diff] [blame] | 374 | ImmutableSet<PackageIdentifier> pkgIdBatchSet = ImmutableSet.copyOf(packageIdentifiers); |
Googler | 2a5a004 | 2024-10-16 07:11:44 -0700 | [diff] [blame] | 375 | acquireTaskLock(); |
adgar | 0da823c | 2019-07-29 07:48:23 -0700 | [diff] [blame] | 376 | packageSemaphore.acquireAll(pkgIdBatchSet); |
| 377 | try { |
| 378 | Iterable<Collection<Target>> resolvedTargets = |
| 379 | RecursivePackageProviderBackedTargetPatternResolver.this |
| 380 | .bulkGetTargetsInPackage(originalPattern, packageIdentifiers, actualPolicy) |
| 381 | .values(); |
| 382 | List<Target> filteredTargets = new ArrayList<>(calculateSize(resolvedTargets)); |
| 383 | for (Collection<Target> targets : resolvedTargets) { |
| 384 | filteredTargets.addAll(targets); |
| 385 | } |
nharmata | 2c3929c | 2021-07-19 11:09:52 -0700 | [diff] [blame] | 386 | // TODO(b/121277360): Invoking the callback while holding onto the package |
nharmata | 30498d8 | 2020-09-10 06:46:53 -0700 | [diff] [blame] | 387 | // semaphore can lead to deadlocks. |
| 388 | // |
| 389 | // Also, if the semaphore has a small count, acquireAll can also lead to problems if we |
| 390 | // don't batch appropriately. Note: We default to an unbounded semaphore for SkyQuery. |
| 391 | // |
| 392 | // TODO(b/168142585): Make this code strictly correct in the situation where the semaphore |
| 393 | // is bounded. |
adgar | 0da823c | 2019-07-29 07:48:23 -0700 | [diff] [blame] | 394 | callback.process(filteredTargets); |
| 395 | } finally { |
| 396 | packageSemaphore.releaseAll(pkgIdBatchSet); |
Googler | 2a5a004 | 2024-10-16 07:11:44 -0700 | [diff] [blame] | 397 | releaseTaskLock(); |
adgar | 0da823c | 2019-07-29 07:48:23 -0700 | [diff] [blame] | 398 | } |
| 399 | return null; |
| 400 | } |
| 401 | } |
| 402 | |
ulfjack | 53a0ff1 | 2019-05-21 07:08:40 -0700 | [diff] [blame] | 403 | private static <T> int calculateSize(Iterable<Collection<T>> resolvedTargets) { |
Eric Fellheimer | 8c40daa | 2016-01-11 17:31:26 +0000 | [diff] [blame] | 404 | int size = 0; |
ulfjack | 53a0ff1 | 2019-05-21 07:08:40 -0700 | [diff] [blame] | 405 | for (Collection<T> targets : resolvedTargets) { |
| 406 | size += targets.size(); |
Eric Fellheimer | 8c40daa | 2016-01-11 17:31:26 +0000 | [diff] [blame] | 407 | } |
| 408 | return size; |
| 409 | } |
Mark Schaller | b889cf3 | 2015-03-17 20:55:30 +0000 | [diff] [blame] | 410 | |
janakr | b8bc284 | 2021-12-08 12:48:02 -0800 | [diff] [blame] | 411 | /** Inspired by not-yet-open-source futures code. */ |
| 412 | private static boolean isSuccessful(Future<?> future) { |
| 413 | if (future.isDone() && !future.isCancelled()) { |
| 414 | try { |
| 415 | Uninterruptibles.getUninterruptibly(future); |
| 416 | return true; |
| 417 | } catch (ExecutionException | RuntimeException e) { |
| 418 | // Fall through. |
| 419 | } |
| 420 | } |
| 421 | return false; |
| 422 | } |
| 423 | } |