Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +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 | |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 16 | import com.google.common.base.Predicate; |
| 17 | import com.google.common.collect.ImmutableList; |
Miguel Alcon Pinto | b651026 | 2015-12-10 17:50:15 +0000 | [diff] [blame] | 18 | import com.google.common.collect.Iterables; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 19 | import com.google.common.collect.Sets; |
Lukacs Berki | 6e91eb9 | 2015-09-21 09:12:37 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.lib.cmdline.Label; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 21 | import com.google.devtools.build.lib.cmdline.TargetParsingException; |
| 22 | import com.google.devtools.build.lib.events.ErrorSensingEventHandler; |
| 23 | import com.google.devtools.build.lib.events.Event; |
| 24 | import com.google.devtools.build.lib.events.EventHandler; |
Dmitry Lomov | 6073eb6 | 2016-01-21 21:26:32 +0000 | [diff] [blame] | 25 | import com.google.devtools.build.lib.packages.DependencyFilter; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 26 | import com.google.devtools.build.lib.packages.Target; |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 27 | import com.google.devtools.build.lib.profiler.AutoProfiler; |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 28 | import com.google.devtools.build.lib.query2.engine.OutputFormatterCallback; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 29 | import com.google.devtools.build.lib.query2.engine.QueryEnvironment; |
| 30 | import com.google.devtools.build.lib.query2.engine.QueryEvalResult; |
| 31 | import com.google.devtools.build.lib.query2.engine.QueryException; |
| 32 | import com.google.devtools.build.lib.query2.engine.QueryExpression; |
Nathan Harmata | f37750a | 2016-09-07 14:58:14 +0000 | [diff] [blame] | 33 | import com.google.devtools.build.lib.query2.engine.QueryExpressionEvalListener; |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 34 | import com.google.devtools.build.lib.query2.engine.QueryUtil; |
Janak Ramakrishnan | 0dbc1f9 | 2016-01-07 19:12:06 +0000 | [diff] [blame] | 35 | import com.google.devtools.build.lib.query2.engine.QueryUtil.AggregateAllCallback; |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 36 | import com.google.devtools.build.lib.query2.engine.ThreadSafeCallback; |
Nathan Harmata | bc47f40 | 2016-07-13 16:22:30 +0000 | [diff] [blame] | 37 | import com.google.devtools.build.lib.query2.engine.VariableContext; |
Mark Schaller | 6df8179 | 2015-12-10 18:47:47 +0000 | [diff] [blame] | 38 | import com.google.devtools.build.lib.util.Preconditions; |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 39 | import java.io.IOException; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 40 | import java.util.Collection; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 41 | import java.util.LinkedHashSet; |
| 42 | import java.util.List; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 43 | import java.util.Set; |
Miguel Alcon Pinto | b651026 | 2015-12-10 17:50:15 +0000 | [diff] [blame] | 44 | import java.util.concurrent.atomic.AtomicBoolean; |
Eric Fellheimer | a39f8a9 | 2015-07-28 19:11:23 +0000 | [diff] [blame] | 45 | import java.util.logging.Logger; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 46 | |
| 47 | /** |
Mark Schaller | 6cebed6 | 2016-06-27 18:05:39 +0000 | [diff] [blame] | 48 | * {@link QueryEnvironment} that can evaluate queries to produce a result, and implements as much of |
| 49 | * QueryEnvironment as possible while remaining mostly agnostic as to the objects being stored. |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 50 | */ |
Mark Schaller | 6cebed6 | 2016-06-27 18:05:39 +0000 | [diff] [blame] | 51 | public abstract class AbstractBlazeQueryEnvironment<T> |
Nathan Harmata | 71616b1 | 2016-09-28 20:20:48 +0000 | [diff] [blame] | 52 | implements QueryEnvironment<T> { |
Janak Ramakrishnan | df69432 | 2016-11-08 18:28:12 +0000 | [diff] [blame^] | 53 | protected ErrorSensingEventHandler eventHandler; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 54 | protected final boolean keepGoing; |
| 55 | protected final boolean strictScope; |
| 56 | |
Dmitry Lomov | 6073eb6 | 2016-01-21 21:26:32 +0000 | [diff] [blame] | 57 | protected final DependencyFilter dependencyFilter; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 58 | private final Predicate<Label> labelFilter; |
| 59 | |
Janak Ramakrishnan | df69432 | 2016-11-08 18:28:12 +0000 | [diff] [blame^] | 60 | protected final Set<Setting> settings; |
| 61 | protected final List<QueryFunction> extraFunctions; |
Nathan Harmata | f37750a | 2016-09-07 14:58:14 +0000 | [diff] [blame] | 62 | private final QueryExpressionEvalListener<T> evalListener; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 63 | |
Nathan Harmata | f0cc5b8 | 2016-03-18 14:56:28 +0000 | [diff] [blame] | 64 | private static final Logger LOG = Logger.getLogger(AbstractBlazeQueryEnvironment.class.getName()); |
Eric Fellheimer | a39f8a9 | 2015-07-28 19:11:23 +0000 | [diff] [blame] | 65 | |
Janak Ramakrishnan | e72d522 | 2015-02-26 17:09:18 +0000 | [diff] [blame] | 66 | protected AbstractBlazeQueryEnvironment(boolean keepGoing, |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 67 | boolean strictScope, |
| 68 | Predicate<Label> labelFilter, |
| 69 | EventHandler eventHandler, |
| 70 | Set<Setting> settings, |
Nathan Harmata | f37750a | 2016-09-07 14:58:14 +0000 | [diff] [blame] | 71 | Iterable<QueryFunction> extraFunctions, |
| 72 | QueryExpressionEvalListener<T> evalListener) { |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 73 | this.eventHandler = new ErrorSensingEventHandler(eventHandler); |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 74 | this.keepGoing = keepGoing; |
| 75 | this.strictScope = strictScope; |
| 76 | this.dependencyFilter = constructDependencyFilter(settings); |
| 77 | this.labelFilter = labelFilter; |
| 78 | this.settings = Sets.immutableEnumSet(settings); |
| 79 | this.extraFunctions = ImmutableList.copyOf(extraFunctions); |
Nathan Harmata | f37750a | 2016-09-07 14:58:14 +0000 | [diff] [blame] | 80 | this.evalListener = evalListener; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Dmitry Lomov | cb14c5e | 2016-01-21 22:04:01 +0000 | [diff] [blame] | 83 | private static DependencyFilter constructDependencyFilter( |
| 84 | Set<Setting> settings) { |
Dmitry Lomov | 6073eb6 | 2016-01-21 21:26:32 +0000 | [diff] [blame] | 85 | DependencyFilter specifiedFilter = |
| 86 | settings.contains(Setting.NO_HOST_DEPS) |
| 87 | ? DependencyFilter.NO_HOST_DEPS |
| 88 | : DependencyFilter.ALL_DEPS; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 89 | if (settings.contains(Setting.NO_IMPLICIT_DEPS)) { |
Dmitry Lomov | 6073eb6 | 2016-01-21 21:26:32 +0000 | [diff] [blame] | 90 | specifiedFilter = DependencyFilter.and(specifiedFilter, DependencyFilter.NO_IMPLICIT_DEPS); |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 91 | } |
| 92 | if (settings.contains(Setting.NO_NODEP_DEPS)) { |
Dmitry Lomov | 6073eb6 | 2016-01-21 21:26:32 +0000 | [diff] [blame] | 93 | specifiedFilter = DependencyFilter.and(specifiedFilter, DependencyFilter.NO_NODEP_ATTRIBUTES); |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 94 | } |
| 95 | return specifiedFilter; |
| 96 | } |
| 97 | |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 98 | /** |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 99 | * Used by {@link #evaluateQuery} to evaluate the given {@code expr}. The caller, |
| 100 | * {@link #evaluateQuery}, not {@link #evalTopLevelInternal}, is responsible for managing |
| 101 | * {@code callback}. |
| 102 | */ |
| 103 | protected void evalTopLevelInternal(QueryExpression expr, OutputFormatterCallback<T> callback) |
| 104 | throws QueryException, InterruptedException { |
| 105 | eval(expr, VariableContext.<T>empty(), callback); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Evaluate the specified query expression in this environment, streaming results to the given |
| 110 | * {@code callback}. {@code callback.start()} will be called before query evaluation and |
| 111 | * {@code callback.close()} will be unconditionally called at the end of query evaluation |
| 112 | * (i.e. regardless of whether it was successful). |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 113 | * |
| 114 | * @return a {@link QueryEvalResult} object that contains the resulting set of targets and a bit |
Mark Schaller | 895b3d2 | 2015-03-23 14:27:26 +0000 | [diff] [blame] | 115 | * to indicate whether errors occurred during evaluation; note that the |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 116 | * success status can only be false if {@code --keep_going} was in effect |
| 117 | * @throws QueryException if the evaluation failed and {@code --nokeep_going} was in |
| 118 | * effect |
| 119 | */ |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 120 | public QueryEvalResult evaluateQuery( |
| 121 | QueryExpression expr, |
| 122 | final OutputFormatterCallback<T> callback) |
| 123 | throws QueryException, InterruptedException, IOException { |
| 124 | EmptinessSensingCallback<T> emptySensingCallback = createEmptinessSensingCallback(callback); |
Miguel Alcon Pinto | b651026 | 2015-12-10 17:50:15 +0000 | [diff] [blame] | 125 | try (final AutoProfiler p = AutoProfiler.logged("evaluating query", LOG)) { |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 126 | // In the --nokeep_going case, errors are reported in the order in which the patterns are |
| 127 | // specified; using a linked hash set here makes sure that the left-most error is reported. |
| 128 | Set<String> targetPatternSet = new LinkedHashSet<>(); |
| 129 | expr.collectTargetPatterns(targetPatternSet); |
| 130 | try { |
Janak Ramakrishnan | ee6208b | 2016-01-07 20:17:41 +0000 | [diff] [blame] | 131 | preloadOrThrow(expr, targetPatternSet); |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 132 | } catch (TargetParsingException e) { |
| 133 | // Unfortunately, by evaluating the patterns in parallel, we lose some location information. |
| 134 | throw new QueryException(expr, e.getMessage()); |
| 135 | } |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 136 | IOException ioExn = null; |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 137 | try { |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 138 | callback.start(); |
| 139 | evalTopLevelInternal(expr, emptySensingCallback); |
Nathan Harmata | d480301 | 2015-09-08 20:03:22 +0000 | [diff] [blame] | 140 | } catch (QueryException e) { |
| 141 | throw new QueryException(e, expr); |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 142 | } catch (InterruptedException e) { |
| 143 | throw e; |
| 144 | } finally { |
| 145 | try { |
| 146 | callback.close(); |
| 147 | } catch (IOException e) { |
| 148 | // Only throw this IOException if we weren't about to throw a different exception. |
| 149 | ioExn = e; |
| 150 | } |
| 151 | } |
| 152 | if (ioExn != null) { |
| 153 | throw ioExn; |
Eric Fellheimer | a39f8a9 | 2015-07-28 19:11:23 +0000 | [diff] [blame] | 154 | } |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | if (eventHandler.hasErrors()) { |
| 158 | if (!keepGoing) { |
| 159 | // This case represents loading-phase errors reported during evaluation |
| 160 | // of target patterns that don't cause evaluation to fail per se. |
| 161 | throw new QueryException("Evaluation of query \"" + expr |
| 162 | + "\" failed due to BUILD file errors"); |
| 163 | } else { |
| 164 | eventHandler.handle(Event.warn("--keep_going specified, ignoring errors. " |
| 165 | + "Results may be inaccurate")); |
| 166 | } |
| 167 | } |
| 168 | |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 169 | return new QueryEvalResult(!eventHandler.hasErrors(), emptySensingCallback.isEmpty()); |
| 170 | } |
| 171 | |
| 172 | private static <T> EmptinessSensingCallback<T> createEmptinessSensingCallback( |
| 173 | OutputFormatterCallback<T> callback) { |
| 174 | return (callback instanceof ThreadSafeCallback) |
| 175 | ? new ThreadSafeEmptinessSensingCallback<>(callback) |
| 176 | : new EmptinessSensingCallback<>(callback); |
| 177 | } |
| 178 | |
| 179 | private static class EmptinessSensingCallback<T> extends OutputFormatterCallback<T> { |
| 180 | private final OutputFormatterCallback<T> callback; |
| 181 | private final AtomicBoolean empty = new AtomicBoolean(true); |
| 182 | |
| 183 | private EmptinessSensingCallback(OutputFormatterCallback<T> callback) { |
| 184 | this.callback = callback; |
| 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public void start() throws IOException { |
| 189 | callback.start(); |
| 190 | } |
| 191 | |
| 192 | @Override |
| 193 | public void processOutput(Iterable<T> partialResult) |
| 194 | throws IOException, InterruptedException { |
| 195 | empty.compareAndSet(true, Iterables.isEmpty(partialResult)); |
| 196 | callback.processOutput(partialResult); |
| 197 | } |
| 198 | |
| 199 | @Override |
| 200 | public void close() throws InterruptedException, IOException { |
| 201 | callback.close(); |
| 202 | } |
| 203 | |
| 204 | boolean isEmpty() { |
| 205 | return empty.get(); |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | private static class ThreadSafeEmptinessSensingCallback<T> |
| 210 | extends EmptinessSensingCallback<T> implements ThreadSafeCallback<T> { |
| 211 | private ThreadSafeEmptinessSensingCallback(OutputFormatterCallback<T> callback) { |
| 212 | super(callback); |
| 213 | Preconditions.checkState(callback instanceof ThreadSafeCallback); |
| 214 | } |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 215 | } |
| 216 | |
Nathan Harmata | 2643c8e | 2016-07-01 23:19:23 +0000 | [diff] [blame] | 217 | public QueryExpression transformParsedQuery(QueryExpression queryExpression) { |
Nathan Harmata | ed93560 | 2016-03-02 01:16:14 +0000 | [diff] [blame] | 218 | return queryExpression; |
| 219 | } |
| 220 | |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 221 | public QueryEvalResult evaluateQuery(String query, OutputFormatterCallback<T> callback) |
| 222 | throws QueryException, InterruptedException, IOException { |
| 223 | return evaluateQuery( |
| 224 | QueryExpression.parse(query, this), callback); |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | @Override |
| 228 | public void reportBuildFileError(QueryExpression caller, String message) throws QueryException { |
| 229 | if (!keepGoing) { |
| 230 | throw new QueryException(caller, message); |
| 231 | } else { |
| 232 | // Keep consistent with evaluateQuery() above. |
| 233 | eventHandler.handle(Event.error("Evaluation of query \"" + caller + "\" failed: " + message)); |
| 234 | } |
| 235 | } |
| 236 | |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 237 | public abstract Target getTarget(Label label) |
| 238 | throws TargetNotFoundException, QueryException, InterruptedException; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 239 | |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 240 | protected boolean validateScope(Label label, boolean strict) throws QueryException { |
| 241 | if (!labelFilter.apply(label)) { |
| 242 | String error = String.format("target '%s' is not within the scope of the query", label); |
| 243 | if (strict) { |
| 244 | throw new QueryException(error); |
| 245 | } else { |
| 246 | eventHandler.handle(Event.warn(error + ". Skipping")); |
| 247 | return false; |
| 248 | } |
| 249 | } |
| 250 | return true; |
| 251 | } |
| 252 | |
| 253 | public Set<T> evalTargetPattern(QueryExpression caller, String pattern) |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 254 | throws QueryException, InterruptedException { |
Janak Ramakrishnan | ee6208b | 2016-01-07 20:17:41 +0000 | [diff] [blame] | 255 | try { |
| 256 | preloadOrThrow(caller, ImmutableList.of(pattern)); |
| 257 | } catch (TargetParsingException e) { |
| 258 | // Will skip the target and keep going if -k is specified. |
| 259 | reportBuildFileError(caller, e.getMessage()); |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 260 | } |
Nathan Harmata | 5bb9cc9 | 2016-09-30 21:28:30 +0000 | [diff] [blame] | 261 | AggregateAllCallback<T> aggregatingCallback = QueryUtil.newAggregateAllCallback(); |
Janak Ramakrishnan | 0dbc1f9 | 2016-01-07 19:12:06 +0000 | [diff] [blame] | 262 | getTargetsMatchingPattern(caller, pattern, aggregatingCallback); |
| 263 | return aggregatingCallback.getResult(); |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 264 | } |
| 265 | |
Janak Ramakrishnan | ee6208b | 2016-01-07 20:17:41 +0000 | [diff] [blame] | 266 | /** |
Janak Ramakrishnan | 71cdea4 | 2016-08-16 15:14:41 +0000 | [diff] [blame] | 267 | * Perform any work that should be done ahead of time to resolve the target patterns in the query. |
| 268 | * Implementations may choose to cache the results of resolving the patterns, cache intermediate |
| 269 | * work, or not cache and resolve patterns on the fly. |
Janak Ramakrishnan | ee6208b | 2016-01-07 20:17:41 +0000 | [diff] [blame] | 270 | */ |
| 271 | protected abstract void preloadOrThrow(QueryExpression caller, Collection<String> patterns) |
Janak Ramakrishnan | 71cdea4 | 2016-08-16 15:14:41 +0000 | [diff] [blame] | 272 | throws QueryException, TargetParsingException, InterruptedException; |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 273 | |
| 274 | @Override |
| 275 | public boolean isSettingEnabled(Setting setting) { |
| 276 | return settings.contains(Preconditions.checkNotNull(setting)); |
| 277 | } |
| 278 | |
| 279 | @Override |
| 280 | public Iterable<QueryFunction> getFunctions() { |
| 281 | ImmutableList.Builder<QueryFunction> builder = ImmutableList.builder(); |
| 282 | builder.addAll(DEFAULT_QUERY_FUNCTIONS); |
| 283 | builder.addAll(extraFunctions); |
| 284 | return builder.build(); |
| 285 | } |
Nathan Harmata | f37750a | 2016-09-07 14:58:14 +0000 | [diff] [blame] | 286 | |
| 287 | @Override |
| 288 | public QueryExpressionEvalListener<T> getEvalListener() { |
| 289 | return evalListener; |
| 290 | } |
Janak Ramakrishnan | a46125c | 2015-02-11 16:51:37 +0000 | [diff] [blame] | 291 | } |