Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [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.skyframe; |
| 15 | |
| 16 | import com.google.common.annotations.VisibleForTesting; |
| 17 | import com.google.common.base.Function; |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 18 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | import com.google.common.base.Predicates; |
| 20 | import com.google.common.collect.ImmutableList; |
| 21 | import com.google.common.collect.Iterables; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 22 | import java.util.HashSet; |
| 23 | import java.util.Objects; |
| 24 | import java.util.Set; |
| 25 | |
| 26 | /** |
| 27 | * Data for a single cycle in the graph, together with the path to the cycle. For any value, the |
| 28 | * head of path to the cycle should be the value itself, or, if the value is actually in the cycle, |
| 29 | * the cycle should start with the value. |
| 30 | */ |
Michajlo Matijkiw | 4e29c83 | 2015-09-30 22:23:25 +0000 | [diff] [blame] | 31 | public class CycleInfo { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 32 | private final ImmutableList<SkyKey> cycle; |
| 33 | private final ImmutableList<SkyKey> pathToCycle; |
| 34 | |
| 35 | @VisibleForTesting |
| 36 | public CycleInfo(Iterable<SkyKey> cycle) { |
| 37 | this(ImmutableList.<SkyKey>of(), cycle); |
| 38 | } |
| 39 | |
Michajlo Matijkiw | aa05828 | 2015-09-28 22:13:27 +0000 | [diff] [blame] | 40 | public CycleInfo(Iterable<SkyKey> pathToCycle, Iterable<SkyKey> cycle) { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 41 | this.pathToCycle = ImmutableList.copyOf(pathToCycle); |
| 42 | this.cycle = ImmutableList.copyOf(cycle); |
| 43 | } |
| 44 | |
| 45 | // If a cycle is already known, but we are processing a value in the middle of the cycle, we need |
| 46 | // to shift the cycle so that the value is at the head. |
| 47 | private CycleInfo(Iterable<SkyKey> cycle, int cycleStart) { |
| 48 | Preconditions.checkState(cycleStart >= 0, cycleStart); |
| 49 | ImmutableList.Builder<SkyKey> cycleTail = ImmutableList.builder(); |
| 50 | ImmutableList.Builder<SkyKey> cycleHead = ImmutableList.builder(); |
| 51 | int index = 0; |
| 52 | for (SkyKey key : cycle) { |
| 53 | if (index >= cycleStart) { |
| 54 | cycleHead.add(key); |
| 55 | } else { |
| 56 | cycleTail.add(key); |
| 57 | } |
| 58 | index++; |
| 59 | } |
| 60 | Preconditions.checkState(cycleStart < index, "%s >= %s ??", cycleStart, index); |
| 61 | this.cycle = cycleHead.addAll(cycleTail.build()).build(); |
| 62 | this.pathToCycle = ImmutableList.of(); |
| 63 | } |
| 64 | |
| 65 | public ImmutableList<SkyKey> getCycle() { |
| 66 | return cycle; |
| 67 | } |
| 68 | |
| 69 | public ImmutableList<SkyKey> getPathToCycle() { |
| 70 | return pathToCycle; |
| 71 | } |
| 72 | |
| 73 | // Given a cycle and a value, if the value is part of the cycle, shift the cycle. Otherwise, |
| 74 | // prepend the value to the head of pathToCycle. |
| 75 | private static CycleInfo normalizeCycle(final SkyKey value, CycleInfo cycle) { |
| 76 | int index = cycle.cycle.indexOf(value); |
| 77 | if (index > -1) { |
| 78 | if (!cycle.pathToCycle.isEmpty()) { |
| 79 | // The head value we are considering is already part of a cycle, but we have reached it by a |
| 80 | // roundabout way. Since we should have reached it directly as well, filter this roundabout |
| 81 | // way out. Example (c has a dependence on top): |
| 82 | // top |
| 83 | // / ^ |
| 84 | // a | |
| 85 | // / \ / |
| 86 | // b-> c |
| 87 | // In the traversal, we start at top, visit a, then c, then top. This yields the |
| 88 | // cycle {top,a,c}. Then we visit b, getting (b, {top,a,c}). Then we construct the full |
| 89 | // error for a. The error should just be the cycle {top,a,c}, but we have an extra copy of |
| 90 | // it via the path through b. |
| 91 | return null; |
| 92 | } |
| 93 | return new CycleInfo(cycle.cycle, index); |
| 94 | } |
| 95 | return new CycleInfo(Iterables.concat(ImmutableList.of(value), cycle.pathToCycle), |
| 96 | cycle.cycle); |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Normalize multiple cycles. This includes removing multiple paths to the same cycle, so that |
| 101 | * a value does not depend on the same cycle multiple ways through the same child value. Note that |
| 102 | * a value can still depend on the same cycle multiple ways, it's just that each way must be |
| 103 | * through a different child value (a path with a different first element). |
| 104 | */ |
| 105 | static Iterable<CycleInfo> prepareCycles(final SkyKey value, Iterable<CycleInfo> cycles) { |
| 106 | final Set<ImmutableList<SkyKey>> alreadyDoneCycles = new HashSet<>(); |
| 107 | return Iterables.filter(Iterables.transform(cycles, |
| 108 | new Function<CycleInfo, CycleInfo>() { |
| 109 | @Override |
| 110 | public CycleInfo apply(CycleInfo input) { |
| 111 | CycleInfo normalized = normalizeCycle(value, input); |
| 112 | if (normalized != null && alreadyDoneCycles.add(normalized.cycle)) { |
| 113 | return normalized; |
| 114 | } |
| 115 | return null; |
| 116 | } |
| 117 | }), Predicates.notNull()); |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public int hashCode() { |
| 122 | return Objects.hash(cycle, pathToCycle); |
| 123 | } |
| 124 | |
| 125 | @Override |
| 126 | public boolean equals(Object that) { |
| 127 | if (this == that) { |
| 128 | return true; |
| 129 | } |
| 130 | if (!(that instanceof CycleInfo)) { |
| 131 | return false; |
| 132 | } |
| 133 | |
| 134 | CycleInfo thatCycle = (CycleInfo) that; |
| 135 | return thatCycle.cycle.equals(this.cycle) && thatCycle.pathToCycle.equals(this.pathToCycle); |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | public String toString() { |
| 140 | return Iterables.toString(pathToCycle) + " -> " + Iterables.toString(cycle); |
| 141 | } |
| 142 | } |