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 | |
tomlu | a155b53 | 2017-11-08 20:12:47 +0100 | [diff] [blame] | 16 | import com.google.common.base.Preconditions; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 17 | import com.google.common.collect.ImmutableList; |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 18 | import com.google.devtools.build.lib.events.ExtendedEventHandler; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | |
| 20 | /** |
| 21 | * An utility for custom reporting of errors from cycles in the the Skyframe graph. This class is |
| 22 | * stateful in order to differentiate between new cycles and cycles that have already been |
| 23 | * reported (do not reuse the instances or cache the results as it could end up printing |
| 24 | * inconsistent information or leak memory). It treats two cycles as the same if they contain the |
| 25 | * same {@link SkyKey}s in the same order, but perhaps with different starting points. See |
| 26 | * {@link CycleDeduper} for more information. |
| 27 | */ |
| 28 | public class CyclesReporter { |
| 29 | |
| 30 | /** |
| 31 | * Interface for reporting custom information about a single cycle. |
| 32 | */ |
| 33 | public interface SingleCycleReporter { |
| 34 | |
| 35 | /** |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 36 | * Reports the given cycle and returns {@code true}, or return {@code false} if this {@link |
| 37 | * SingleCycleReporter} doesn't know how to report the cycle. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 38 | * |
| 39 | * @param topLevelKey the top level key that transitively depended on the cycle |
| 40 | * @param cycleInfo the cycle |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 41 | * @param alreadyReported whether the cycle has already been reported to the {@link |
| 42 | * CyclesReporter}. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 43 | * @param eventHandler the eventHandler to which to report the error |
| 44 | */ |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 45 | boolean maybeReportCycle( |
| 46 | SkyKey topLevelKey, |
| 47 | CycleInfo cycleInfo, |
| 48 | boolean alreadyReported, |
| 49 | ExtendedEventHandler eventHandler); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 50 | } |
| 51 | |
| 52 | private final ImmutableList<SingleCycleReporter> cycleReporters; |
| 53 | private final CycleDeduper<SkyKey> cycleDeduper = new CycleDeduper<>(); |
| 54 | |
| 55 | /** |
| 56 | * Constructs a {@link CyclesReporter} that delegates to the given {@link SingleCycleReporter}s, |
| 57 | * in the given order, to report custom information about cycles. |
| 58 | */ |
| 59 | public CyclesReporter(SingleCycleReporter... cycleReporters) { |
| 60 | this.cycleReporters = ImmutableList.copyOf(cycleReporters); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Reports the given cycles, differentiating between cycles that have already been reported. |
| 65 | * |
| 66 | * @param cycles The {@code Iterable} of cycles. |
| 67 | * @param topLevelKey This key represents the top level value key that returned cycle errors. |
| 68 | * @param eventHandler the eventHandler to which to report the error |
| 69 | */ |
Klaus Aehlig | 777b30d | 2017-02-24 16:30:15 +0000 | [diff] [blame] | 70 | public void reportCycles( |
| 71 | Iterable<CycleInfo> cycles, SkyKey topLevelKey, ExtendedEventHandler eventHandler) { |
janakr | 2098474 | 2019-01-28 10:36:35 -0800 | [diff] [blame] | 72 | Preconditions.checkNotNull(eventHandler, "topLevelKey: %s, Cycles %s", topLevelKey, cycles); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 73 | for (CycleInfo cycleInfo : cycles) { |
janakr | 2098474 | 2019-01-28 10:36:35 -0800 | [diff] [blame] | 74 | maybeReportCycle(cycleInfo, topLevelKey, eventHandler); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 75 | } |
| 76 | } |
| 77 | |
janakr | 2098474 | 2019-01-28 10:36:35 -0800 | [diff] [blame] | 78 | private void maybeReportCycle( |
| 79 | CycleInfo cycleInfo, SkyKey topLevelKey, ExtendedEventHandler eventHandler) { |
| 80 | boolean alreadyReported = !cycleDeduper.seen(cycleInfo.getCycle()); |
| 81 | for (SingleCycleReporter cycleReporter : cycleReporters) { |
| 82 | if (cycleReporter.maybeReportCycle(topLevelKey, cycleInfo, alreadyReported, eventHandler)) { |
| 83 | return; |
| 84 | } |
| 85 | } |
| 86 | throw new IllegalStateException( |
| 87 | printArbitraryCycle(topLevelKey, cycleInfo, alreadyReported) + "\n" + cycleReporters); |
| 88 | } |
| 89 | |
| 90 | private static String printArbitraryCycle( |
| 91 | SkyKey topLevelKey, CycleInfo cycleInfo, boolean alreadyReported) { |
| 92 | StringBuilder cycleMessage = |
| 93 | new StringBuilder() |
| 94 | .append("topLevelKey: ") |
| 95 | .append(topLevelKey) |
| 96 | .append("\n") |
| 97 | .append("alreadyReported: ") |
| 98 | .append(alreadyReported) |
| 99 | .append("\n") |
| 100 | .append("path to cycle:\n"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 101 | for (SkyKey skyKey : cycleInfo.getPathToCycle()) { |
janakr | 2098474 | 2019-01-28 10:36:35 -0800 | [diff] [blame] | 102 | cycleMessage.append(skyKey).append("\n"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 103 | } |
| 104 | cycleMessage.append("cycle:\n"); |
| 105 | for (SkyKey skyKey : cycleInfo.getCycle()) { |
janakr | 2098474 | 2019-01-28 10:36:35 -0800 | [diff] [blame] | 106 | cycleMessage.append(skyKey).append("\n"); |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 107 | } |
| 108 | return cycleMessage.toString(); |
| 109 | } |
| 110 | } |