blob: 3ac4e3d7eb660dce01db3d2b8a8d3109c5143852 [file] [log] [blame]
Janak Ramakrishnana973b612016-06-20 18:58:12 +00001// 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.
14package com.google.devtools.build.skyframe;
15
tomlua155b532017-11-08 20:12:47 +010016import com.google.common.base.Preconditions;
Janak Ramakrishnana973b612016-06-20 18:58:12 +000017import com.google.common.collect.Iterables;
18import com.google.common.collect.Maps;
Klaus Aehlig777b30d2017-02-24 16:30:15 +000019import com.google.devtools.build.lib.events.ExtendedEventHandler;
Nathan Harmata3b47b1f2016-07-26 18:41:41 +000020import com.google.devtools.build.skyframe.QueryableGraph.Reason;
Janak Ramakrishnana973b612016-06-20 18:58:12 +000021import java.util.Map;
Janak Ramakrishnana973b612016-06-20 18:58:12 +000022
Janak Ramakrishnana973b612016-06-20 18:58:12 +000023/**
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 */
28public class QueryableGraphBackedSkyFunctionEnvironment extends AbstractSkyFunctionEnvironment {
29 private final QueryableGraph queryableGraph;
Klaus Aehlig777b30d2017-02-24 16:30:15 +000030 private final ExtendedEventHandler eventHandler;
Janak Ramakrishnana973b612016-06-20 18:58:12 +000031
32 public QueryableGraphBackedSkyFunctionEnvironment(
Klaus Aehlig777b30d2017-02-24 16:30:15 +000033 QueryableGraph queryableGraph, ExtendedEventHandler eventHandler) {
Janak Ramakrishnana973b612016-06-20 18:58:12 +000034 this.queryableGraph = queryableGraph;
35 this.eventHandler = eventHandler;
36 }
37
janakrbddc5142018-08-08 13:10:36 -070038 private ValueOrUntypedException toUntypedValue(NodeEntry nodeEntry) throws InterruptedException {
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000039 if (nodeEntry == null || !nodeEntry.isDone()) {
janakrbddc5142018-08-08 13:10:36 -070040 valuesMissing = true;
shreyaxc10ad742018-04-10 12:44:40 -070041 return ValueOrUntypedException.ofNull();
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000042 }
43 SkyValue maybeWrappedValue = nodeEntry.getValueMaybeWithMetadata();
44 SkyValue justValue = ValueWithMetadata.justValue(maybeWrappedValue);
45 if (justValue != null) {
shreyaxc10ad742018-04-10 12:44:40 -070046 return ValueOrUntypedException.ofValueUntyped(justValue);
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000047 }
janakrbddc5142018-08-08 13:10:36 -070048 errorMightHaveBeenFound = true;
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000049 ErrorInfo errorInfo =
50 Preconditions.checkNotNull(ValueWithMetadata.getMaybeErrorInfo(maybeWrappedValue));
51 Exception exception = errorInfo.getException();
Janak Ramakrishnana973b612016-06-20 18:58:12 +000052
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000053 if (exception != null) {
54 // Give SkyFunction#compute a chance to handle this exception.
shreyaxc10ad742018-04-10 12:44:40 -070055 return ValueOrUntypedException.ofExn(exception);
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000056 }
57 // In a cycle.
58 Preconditions.checkState(
59 !Iterables.isEmpty(errorInfo.getCycleInfo()), "%s %s", errorInfo, maybeWrappedValue);
shreyaxc10ad742018-04-10 12:44:40 -070060 return ValueOrUntypedException.ofNull();
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000061 }
Janak Ramakrishnana973b612016-06-20 18:58:12 +000062
63 @Override
Janak Ramakrishnan731c2712016-08-23 17:30:52 +000064 protected Map<SkyKey, ValueOrUntypedException> getValueOrUntypedExceptions(
ulfjackbe83f132017-07-18 18:12:09 +020065 Iterable<? extends SkyKey> depKeys) throws InterruptedException {
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000066 Map<SkyKey, ? extends NodeEntry> resultMap =
67 queryableGraph.getBatch(null, Reason.DEP_REQUESTED, depKeys);
Janak Ramakrishnan731c2712016-08-23 17:30:52 +000068 // 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 Ramakrishnan3c0adb22016-08-15 21:54:55 +000072 for (SkyKey dep : depKeys) {
73 result.put(dep, toUntypedValue(resultMap.get(dep)));
Janak Ramakrishnana973b612016-06-20 18:58:12 +000074 }
Janak Ramakrishnan3c0adb22016-08-15 21:54:55 +000075 return result;
Janak Ramakrishnana973b612016-06-20 18:58:12 +000076 }
77
78 @Override
Klaus Aehlig777b30d2017-02-24 16:30:15 +000079 public ExtendedEventHandler getListener() {
Janak Ramakrishnana973b612016-06-20 18:58:12 +000080 return eventHandler;
81 }
82
83 @Override
84 public boolean inErrorBubblingForTesting() {
85 return false;
86 }
87}