mschaller | d8a121e | 2020-02-28 16:41:11 -0800 | [diff] [blame] | 1 | // Copyright 2020 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 | |
| 15 | package com.google.devtools.build.lib.util; |
| 16 | |
mschaller | bb4d4cc | 2020-03-30 10:54:13 -0700 | [diff] [blame] | 17 | import static com.google.common.base.Preconditions.checkArgument; |
mschaller | 854bb90 | 2020-03-03 10:56:14 -0800 | [diff] [blame] | 18 | import static com.google.common.base.Preconditions.checkNotNull; |
| 19 | |
mschaller | bb4d4cc | 2020-03-30 10:54:13 -0700 | [diff] [blame] | 20 | import com.google.devtools.build.lib.server.FailureDetails; |
mschaller | d8a121e | 2020-02-28 16:41:11 -0800 | [diff] [blame] | 21 | import com.google.devtools.build.lib.server.FailureDetails.FailureDetail; |
mschaller | bb4d4cc | 2020-03-30 10:54:13 -0700 | [diff] [blame] | 22 | import com.google.protobuf.Descriptors.EnumValueDescriptor; |
| 23 | import com.google.protobuf.Descriptors.FieldDescriptor; |
| 24 | import com.google.protobuf.MessageOrBuilder; |
janakr | 5d66721 | 2020-04-03 10:45:41 -0700 | [diff] [blame] | 25 | import java.util.Comparator; |
mschaller | bb4d4cc | 2020-03-30 10:54:13 -0700 | [diff] [blame] | 26 | import java.util.Map; |
janakr | 86e9538 | 2020-04-10 09:00:47 -0700 | [diff] [blame] | 27 | import java.util.Objects; |
mschaller | d8a121e | 2020-02-28 16:41:11 -0800 | [diff] [blame] | 28 | import javax.annotation.Nullable; |
| 29 | |
mschaller | 859c9ac | 2020-09-25 16:09:19 -0700 | [diff] [blame] | 30 | /** An {@link ExitCode} that has a {@link FailureDetail} unless it's {@link ExitCode#SUCCESS}. */ |
mschaller | d8a121e | 2020-02-28 16:41:11 -0800 | [diff] [blame] | 31 | public class DetailedExitCode { |
| 32 | private final ExitCode exitCode; |
| 33 | @Nullable private final FailureDetail failureDetail; |
| 34 | |
| 35 | private DetailedExitCode(ExitCode exitCode, @Nullable FailureDetail failureDetail) { |
| 36 | this.exitCode = exitCode; |
| 37 | this.failureDetail = failureDetail; |
| 38 | } |
| 39 | |
| 40 | public ExitCode getExitCode() { |
| 41 | return exitCode; |
| 42 | } |
| 43 | |
mschaller | bb4d4cc | 2020-03-30 10:54:13 -0700 | [diff] [blame] | 44 | /** Returns the registered {@link ExitCode} associated with a {@link FailureDetail} message. */ |
janakr | 40f2a12 | 2020-10-06 08:53:15 -0700 | [diff] [blame] | 45 | public static ExitCode getExitCode(FailureDetail failureDetail) { |
mschaller | bb4d4cc | 2020-03-30 10:54:13 -0700 | [diff] [blame] | 46 | // TODO(mschaller): Consider specializing for unregistered exit codes here, if absolutely |
| 47 | // necessary. |
| 48 | int numericExitCode = getNumericExitCode(failureDetail); |
| 49 | return checkNotNull( |
| 50 | ExitCode.forCode(numericExitCode), "No ExitCode for numericExitCode %s", numericExitCode); |
| 51 | } |
| 52 | |
mschaller | d8a121e | 2020-02-28 16:41:11 -0800 | [diff] [blame] | 53 | @Nullable |
| 54 | public FailureDetail getFailureDetail() { |
| 55 | return failureDetail; |
| 56 | } |
| 57 | |
mschaller | 854bb90 | 2020-03-03 10:56:14 -0800 | [diff] [blame] | 58 | public boolean isSuccess() { |
| 59 | return exitCode.equals(ExitCode.SUCCESS); |
| 60 | } |
| 61 | |
mschaller | 07cbdce | 2020-05-12 22:45:26 -0700 | [diff] [blame] | 62 | /** Returns a {@link DetailedExitCode} specifying success (i.e. exit code 0). */ |
| 63 | public static DetailedExitCode success() { |
| 64 | return new DetailedExitCode(ExitCode.SUCCESS, null); |
| 65 | } |
| 66 | |
mschaller | 854bb90 | 2020-03-03 10:56:14 -0800 | [diff] [blame] | 67 | /** |
mschaller | 854bb90 | 2020-03-03 10:56:14 -0800 | [diff] [blame] | 68 | * Returns a {@link DetailedExitCode} combining the provided {@link FailureDetail} and {@link |
| 69 | * ExitCode}. |
| 70 | * |
| 71 | * <p>This method exists in order to allow for the introduction of new {@link |
| 72 | * FailureDetail)-handling code infrastructure without requiring any simultaneous change in exit |
| 73 | * code behavior. |
| 74 | * |
mschaller | 859c9ac | 2020-09-25 16:09:19 -0700 | [diff] [blame] | 75 | * <p>Unless contrained by backwards-compatibility needs, callers should use {@link |
| 76 | * #of(FailureDetail)} instead. |
mschaller | 854bb90 | 2020-03-03 10:56:14 -0800 | [diff] [blame] | 77 | */ |
mschaller | 854bb90 | 2020-03-03 10:56:14 -0800 | [diff] [blame] | 78 | public static DetailedExitCode of(ExitCode exitCode, FailureDetail failureDetail) { |
| 79 | return new DetailedExitCode(checkNotNull(exitCode), checkNotNull(failureDetail)); |
mschaller | d8a121e | 2020-02-28 16:41:11 -0800 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Returns a {@link DetailedExitCode} whose {@link ExitCode} is chosen referencing {@link |
| 84 | * FailureDetail}'s metadata. |
| 85 | */ |
| 86 | public static DetailedExitCode of(FailureDetail failureDetail) { |
mschaller | 859c9ac | 2020-09-25 16:09:19 -0700 | [diff] [blame] | 87 | return new DetailedExitCode(getExitCode(failureDetail), checkNotNull(failureDetail)); |
mschaller | d8a121e | 2020-02-28 16:41:11 -0800 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | @Override |
janakr | 86e9538 | 2020-04-10 09:00:47 -0700 | [diff] [blame] | 91 | public int hashCode() { |
| 92 | return Objects.hash(exitCode, failureDetail); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public boolean equals(Object obj) { |
| 97 | if (obj == this) { |
| 98 | return true; |
| 99 | } |
| 100 | if (!(obj instanceof DetailedExitCode)) { |
| 101 | return false; |
| 102 | } |
| 103 | DetailedExitCode that = (DetailedExitCode) obj; |
| 104 | return this.exitCode.equals(that.exitCode) |
| 105 | && Objects.equals(this.failureDetail, that.failureDetail); |
| 106 | } |
| 107 | |
| 108 | @Override |
mschaller | d8a121e | 2020-02-28 16:41:11 -0800 | [diff] [blame] | 109 | public String toString() { |
| 110 | return String.format( |
| 111 | "DetailedExitCode{exitCode=%s, failureDetail=%s}", exitCode, failureDetail); |
| 112 | } |
mschaller | bb4d4cc | 2020-03-30 10:54:13 -0700 | [diff] [blame] | 113 | |
| 114 | /** Returns the numeric exit code associated with a {@link FailureDetail} message. */ |
Googler | 2b7c205 | 2022-01-21 06:00:02 -0800 | [diff] [blame] | 115 | public static int getNumericExitCode(FailureDetail failureDetail) { |
mschaller | bb4d4cc | 2020-03-30 10:54:13 -0700 | [diff] [blame] | 116 | MessageOrBuilder categoryMsg = getCategorySubmessage(failureDetail); |
| 117 | EnumValueDescriptor subcategoryDescriptor = |
| 118 | getSubcategoryDescriptor(failureDetail, categoryMsg); |
| 119 | return getNumericExitCode(subcategoryDescriptor); |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Returns the numeric exit code associated with a {@link FailureDetail} submessage's subcategory |
| 124 | * enum value. |
| 125 | */ |
janakr | 40f2a12 | 2020-10-06 08:53:15 -0700 | [diff] [blame] | 126 | public static int getNumericExitCode(EnumValueDescriptor subcategoryDescriptor) { |
mschaller | bb4d4cc | 2020-03-30 10:54:13 -0700 | [diff] [blame] | 127 | checkArgument( |
| 128 | subcategoryDescriptor.getOptions().hasExtension(FailureDetails.metadata), |
| 129 | "Enum value %s has no FailureDetails.metadata", |
| 130 | subcategoryDescriptor); |
| 131 | return subcategoryDescriptor.getOptions().getExtension(FailureDetails.metadata).getExitCode(); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Returns the category submessage, i.e. the message in {@link FailureDetail}'s oneof. Throws if |
| 136 | * none of those fields are set. |
| 137 | */ |
| 138 | private static MessageOrBuilder getCategorySubmessage(FailureDetail failureDetail) { |
| 139 | MessageOrBuilder categoryMsg = null; |
| 140 | for (Map.Entry<FieldDescriptor, Object> entry : failureDetail.getAllFields().entrySet()) { |
| 141 | FieldDescriptor fieldDescriptor = entry.getKey(); |
| 142 | if (isCategoryField(fieldDescriptor)) { |
| 143 | categoryMsg = (MessageOrBuilder) entry.getValue(); |
| 144 | break; |
| 145 | } |
| 146 | } |
| 147 | return checkNotNull( |
| 148 | categoryMsg, "FailureDetail missing category submessage: %s", failureDetail); |
| 149 | } |
| 150 | |
| 151 | /** |
| 152 | * Returns whether the {@link FieldDescriptor} describes a field in {@link FailureDetail}'s oneof. |
| 153 | * |
| 154 | * <p>Uses the field number criteria described in failure_details.proto. |
| 155 | */ |
| 156 | private static boolean isCategoryField(FieldDescriptor fieldDescriptor) { |
| 157 | int fieldNum = fieldDescriptor.getNumber(); |
| 158 | return 100 < fieldNum && fieldNum <= 10_000; |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Returns the enum value descriptor for the enum field with field number 1 in the {@link |
| 163 | * FailureDetail}'s category submessage. |
| 164 | */ |
| 165 | private static EnumValueDescriptor getSubcategoryDescriptor( |
| 166 | FailureDetail failureDetail, MessageOrBuilder categoryMsg) { |
| 167 | FieldDescriptor fieldNumberOne = categoryMsg.getDescriptorForType().findFieldByNumber(1); |
| 168 | checkNotNull( |
| 169 | fieldNumberOne, "FailureDetail category submessage has no field #1: %s", failureDetail); |
| 170 | Object fieldNumberOneVal = categoryMsg.getField(fieldNumberOne); |
| 171 | checkArgument( |
| 172 | fieldNumberOneVal instanceof EnumValueDescriptor, |
| 173 | "FailureDetail category submessage has non-enum field #1: %s", |
| 174 | failureDetail); |
| 175 | return (EnumValueDescriptor) fieldNumberOneVal; |
| 176 | } |
janakr | 5d66721 | 2020-04-03 10:45:41 -0700 | [diff] [blame] | 177 | |
| 178 | /** |
| 179 | * A comparator to determine the reporting priority of {@link DetailedExitCode}. |
| 180 | * |
| 181 | * <p>Priority: infrastructure exit codes > non-infrastructure exit codes > null exit codes, with |
| 182 | * exit codes that contain failure details taking priority within each class. |
| 183 | */ |
| 184 | public static class DetailedExitCodeComparator implements Comparator<DetailedExitCode> { |
| 185 | public static final DetailedExitCodeComparator INSTANCE = new DetailedExitCodeComparator(); |
| 186 | |
| 187 | private DetailedExitCodeComparator() {} |
| 188 | |
| 189 | @Nullable |
| 190 | public static DetailedExitCode chooseMoreImportantWithFirstIfTie( |
| 191 | @Nullable DetailedExitCode first, @Nullable DetailedExitCode second) { |
| 192 | return INSTANCE.compare(first, second) >= 0 ? first : second; |
| 193 | } |
| 194 | |
| 195 | @Override |
| 196 | public int compare(DetailedExitCode c1, DetailedExitCode c2) { |
| 197 | // returns POSITIVE result when the priority of c1 is HIGHER than the priority of c2 |
| 198 | return getPriority(c1) - getPriority(c2); |
| 199 | } |
| 200 | |
| 201 | private static int getPriority(DetailedExitCode code) { |
| 202 | if (code == null) { |
| 203 | return 0; |
| 204 | } else { |
| 205 | int codeClass = code.getExitCode().isInfrastructureFailure() ? 4 : 2; |
| 206 | return codeClass + (code.getFailureDetail() != null ? 1 : 0); |
| 207 | } |
| 208 | } |
| 209 | } |
mschaller | d8a121e | 2020-02-28 16:41:11 -0800 | [diff] [blame] | 210 | } |