Janak Ramakrishnan | a973b61 | 2016-06-20 18:58:12 +0000 | [diff] [blame] | 1 | // Copyright 2016 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.skyframe; |
| 15 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 16 | import com.google.common.base.Preconditions; |
Janak Ramakrishnan | a973b61 | 2016-06-20 18:58:12 +0000 | [diff] [blame] | 17 | import com.google.common.collect.Iterables; |
| 18 | import com.google.common.collect.Maps; |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 19 | import com.google.devtools.build.lib.events.ExtendedEventHandler; |
Nathan Harmata | 3b47b1f | 2016-07-26 18:41:41 +0000 | [diff] [blame] | 20 | import com.google.devtools.build.skyframe.QueryableGraph.Reason; |
Janak Ramakrishnan | a973b61 | 2016-06-20 18:58:12 +0000 | [diff] [blame] | 21 | import java.util.Map; |
Janak Ramakrishnan | a973b61 | 2016-06-20 18:58:12 +0000 | [diff] [blame] | 22 | |
Janak Ramakrishnan | a973b61 | 2016-06-20 18:58:12 +0000 | [diff] [blame] | 23 | /** |
| 24 | * A {@link SkyFunction.Environment} backed by a {@link QueryableGraph}. For use when a single |
| 25 | * SkyFunction needs recomputation, and its dependencies do not need to be evaluated. Any missing |
| 26 | * dependencies will be ignored. |
| 27 | */ |
| 28 | public class QueryableGraphBackedSkyFunctionEnvironment extends AbstractSkyFunctionEnvironment { |
| 29 | private final QueryableGraph queryableGraph; |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 30 | private final ExtendedEventHandler eventHandler; |
Janak Ramakrishnan | a973b61 | 2016-06-20 18:58:12 +0000 | [diff] [blame] | 31 | |
| 32 | public QueryableGraphBackedSkyFunctionEnvironment( |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 33 | QueryableGraph queryableGraph, ExtendedEventHandler eventHandler) { |
Janak Ramakrishnan | a973b61 | 2016-06-20 18:58:12 +0000 | [diff] [blame] | 34 | this.queryableGraph = queryableGraph; |
| 35 | this.eventHandler = eventHandler; |
| 36 | } |
| 37 | |
janakr | bddc514 | 2018-08-08 13:10:36 -0700 | [diff] [blame] | 38 | private ValueOrUntypedException toUntypedValue(NodeEntry nodeEntry) throws InterruptedException { |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 39 | if (nodeEntry == null || !nodeEntry.isDone()) { |
janakr | bddc514 | 2018-08-08 13:10:36 -0700 | [diff] [blame] | 40 | valuesMissing = true; |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 41 | return ValueOrUntypedException.ofNull(); |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 42 | } |
| 43 | SkyValue maybeWrappedValue = nodeEntry.getValueMaybeWithMetadata(); |
| 44 | SkyValue justValue = ValueWithMetadata.justValue(maybeWrappedValue); |
| 45 | if (justValue != null) { |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 46 | return ValueOrUntypedException.ofValueUntyped(justValue); |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 47 | } |
janakr | bddc514 | 2018-08-08 13:10:36 -0700 | [diff] [blame] | 48 | errorMightHaveBeenFound = true; |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 49 | ErrorInfo errorInfo = |
| 50 | Preconditions.checkNotNull(ValueWithMetadata.getMaybeErrorInfo(maybeWrappedValue)); |
| 51 | Exception exception = errorInfo.getException(); |
Janak Ramakrishnan | a973b61 | 2016-06-20 18:58:12 +0000 | [diff] [blame] | 52 | |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 53 | if (exception != null) { |
| 54 | // Give SkyFunction#compute a chance to handle this exception. |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 55 | return ValueOrUntypedException.ofExn(exception); |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 56 | } |
| 57 | // In a cycle. |
| 58 | Preconditions.checkState( |
| 59 | !Iterables.isEmpty(errorInfo.getCycleInfo()), "%s %s", errorInfo, maybeWrappedValue); |
shreyax | c10ad74 | 2018-04-10 12:44:40 -0700 | [diff] [blame] | 60 | return ValueOrUntypedException.ofNull(); |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 61 | } |
Janak Ramakrishnan | a973b61 | 2016-06-20 18:58:12 +0000 | [diff] [blame] | 62 | |
| 63 | @Override |
Janak Ramakrishnan | 731c271 | 2016-08-23 17:30:52 +0000 | [diff] [blame] | 64 | protected Map<SkyKey, ValueOrUntypedException> getValueOrUntypedExceptions( |
ulfjack | be83f13 | 2017-07-18 18:12:09 +0200 | [diff] [blame] | 65 | Iterable<? extends SkyKey> depKeys) throws InterruptedException { |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 66 | Map<SkyKey, ? extends NodeEntry> resultMap = |
| 67 | queryableGraph.getBatch(null, Reason.DEP_REQUESTED, depKeys); |
Janak Ramakrishnan | 731c271 | 2016-08-23 17:30:52 +0000 | [diff] [blame] | 68 | // resultMap will be smaller than what we actually return if some of depKeys were not found in |
| 69 | // the graph. Pad to a minimum of 16 to avoid excessive resizing. |
| 70 | Map<SkyKey, ValueOrUntypedException> result = |
| 71 | Maps.newHashMapWithExpectedSize(Math.max(16, resultMap.size())); |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 72 | for (SkyKey dep : depKeys) { |
| 73 | result.put(dep, toUntypedValue(resultMap.get(dep))); |
Janak Ramakrishnan | a973b61 | 2016-06-20 18:58:12 +0000 | [diff] [blame] | 74 | } |
Janak Ramakrishnan | 3c0adb2 | 2016-08-15 21:54:55 +0000 | [diff] [blame] | 75 | return result; |
Janak Ramakrishnan | a973b61 | 2016-06-20 18:58:12 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @Override |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 79 | public ExtendedEventHandler getListener() { |
Janak Ramakrishnan | a973b61 | 2016-06-20 18:58:12 +0000 | [diff] [blame] | 80 | return eventHandler; |
| 81 | } |
| 82 | |
| 83 | @Override |
| 84 | public boolean inErrorBubblingForTesting() { |
| 85 | return false; |
| 86 | } |
| 87 | } |