|
45 | 45 | import io.grpc.internal.GrpcUtil;
|
46 | 46 | import io.grpc.xds.FaultConfig.FaultAbort;
|
47 | 47 | import io.grpc.xds.FaultConfig.FaultDelay;
|
48 |
| -import io.grpc.xds.Filter.ClientInterceptorBuilder; |
49 | 48 | import io.grpc.xds.ThreadSafeRandom.ThreadSafeRandomImpl;
|
50 | 49 | import java.util.Locale;
|
51 | 50 | import java.util.concurrent.Executor;
|
|
56 | 55 | import javax.annotation.Nullable;
|
57 | 56 |
|
58 | 57 | /** HttpFault filter implementation. */
|
59 |
| -final class FaultFilter implements Filter, ClientInterceptorBuilder { |
| 58 | +final class FaultFilter implements Filter { |
60 | 59 |
|
61 |
| - static final FaultFilter INSTANCE = |
| 60 | + private static final FaultFilter INSTANCE = |
62 | 61 | new FaultFilter(ThreadSafeRandomImpl.instance, new AtomicLong());
|
| 62 | + |
63 | 63 | @VisibleForTesting
|
64 | 64 | static final Metadata.Key<String> HEADER_DELAY_KEY =
|
65 | 65 | Metadata.Key.of("x-envoy-fault-delay-request", Metadata.ASCII_STRING_MARSHALLER);
|
@@ -87,96 +87,108 @@ final class FaultFilter implements Filter, ClientInterceptorBuilder {
|
87 | 87 | this.activeFaultCounter = activeFaultCounter;
|
88 | 88 | }
|
89 | 89 |
|
90 |
| - @Override |
91 |
| - public String[] typeUrls() { |
92 |
| - return new String[] { TYPE_URL }; |
93 |
| - } |
94 |
| - |
95 |
| - @Override |
96 |
| - public ConfigOrError<FaultConfig> parseFilterConfig(Message rawProtoMessage) { |
97 |
| - HTTPFault httpFaultProto; |
98 |
| - if (!(rawProtoMessage instanceof Any)) { |
99 |
| - return ConfigOrError.fromError("Invalid config type: " + rawProtoMessage.getClass()); |
| 90 | + static final class Provider implements Filter.Provider { |
| 91 | + @Override |
| 92 | + public String[] typeUrls() { |
| 93 | + return new String[]{TYPE_URL}; |
100 | 94 | }
|
101 |
| - Any anyMessage = (Any) rawProtoMessage; |
102 |
| - try { |
103 |
| - httpFaultProto = anyMessage.unpack(HTTPFault.class); |
104 |
| - } catch (InvalidProtocolBufferException e) { |
105 |
| - return ConfigOrError.fromError("Invalid proto: " + e); |
| 95 | + |
| 96 | + @Override |
| 97 | + public boolean isClientFilter() { |
| 98 | + return true; |
106 | 99 | }
|
107 |
| - return parseHttpFault(httpFaultProto); |
108 |
| - } |
109 | 100 |
|
110 |
| - private static ConfigOrError<FaultConfig> parseHttpFault(HTTPFault httpFault) { |
111 |
| - FaultDelay faultDelay = null; |
112 |
| - FaultAbort faultAbort = null; |
113 |
| - if (httpFault.hasDelay()) { |
114 |
| - faultDelay = parseFaultDelay(httpFault.getDelay()); |
| 101 | + @Override |
| 102 | + public FaultFilter newInstance() { |
| 103 | + return INSTANCE; |
115 | 104 | }
|
116 |
| - if (httpFault.hasAbort()) { |
117 |
| - ConfigOrError<FaultAbort> faultAbortOrError = parseFaultAbort(httpFault.getAbort()); |
118 |
| - if (faultAbortOrError.errorDetail != null) { |
119 |
| - return ConfigOrError.fromError( |
120 |
| - "HttpFault contains invalid FaultAbort: " + faultAbortOrError.errorDetail); |
| 105 | + |
| 106 | + @Override |
| 107 | + public ConfigOrError<FaultConfig> parseFilterConfig(Message rawProtoMessage) { |
| 108 | + HTTPFault httpFaultProto; |
| 109 | + if (!(rawProtoMessage instanceof Any)) { |
| 110 | + return ConfigOrError.fromError("Invalid config type: " + rawProtoMessage.getClass()); |
121 | 111 | }
|
122 |
| - faultAbort = faultAbortOrError.config; |
123 |
| - } |
124 |
| - Integer maxActiveFaults = null; |
125 |
| - if (httpFault.hasMaxActiveFaults()) { |
126 |
| - maxActiveFaults = httpFault.getMaxActiveFaults().getValue(); |
127 |
| - if (maxActiveFaults < 0) { |
128 |
| - maxActiveFaults = Integer.MAX_VALUE; |
| 112 | + Any anyMessage = (Any) rawProtoMessage; |
| 113 | + try { |
| 114 | + httpFaultProto = anyMessage.unpack(HTTPFault.class); |
| 115 | + } catch (InvalidProtocolBufferException e) { |
| 116 | + return ConfigOrError.fromError("Invalid proto: " + e); |
129 | 117 | }
|
| 118 | + return parseHttpFault(httpFaultProto); |
130 | 119 | }
|
131 |
| - return ConfigOrError.fromConfig(FaultConfig.create(faultDelay, faultAbort, maxActiveFaults)); |
132 |
| - } |
133 | 120 |
|
134 |
| - private static FaultDelay parseFaultDelay( |
135 |
| - io.envoyproxy.envoy.extensions.filters.common.fault.v3.FaultDelay faultDelay) { |
136 |
| - FaultConfig.FractionalPercent percent = parsePercent(faultDelay.getPercentage()); |
137 |
| - if (faultDelay.hasHeaderDelay()) { |
138 |
| - return FaultDelay.forHeader(percent); |
| 121 | + @Override |
| 122 | + public ConfigOrError<FaultConfig> parseFilterConfigOverride(Message rawProtoMessage) { |
| 123 | + return parseFilterConfig(rawProtoMessage); |
139 | 124 | }
|
140 |
| - return FaultDelay.forFixedDelay(Durations.toNanos(faultDelay.getFixedDelay()), percent); |
141 |
| - } |
142 | 125 |
|
143 |
| - @VisibleForTesting |
144 |
| - static ConfigOrError<FaultAbort> parseFaultAbort( |
145 |
| - io.envoyproxy.envoy.extensions.filters.http.fault.v3.FaultAbort faultAbort) { |
146 |
| - FaultConfig.FractionalPercent percent = parsePercent(faultAbort.getPercentage()); |
147 |
| - switch (faultAbort.getErrorTypeCase()) { |
148 |
| - case HEADER_ABORT: |
149 |
| - return ConfigOrError.fromConfig(FaultAbort.forHeader(percent)); |
150 |
| - case HTTP_STATUS: |
151 |
| - return ConfigOrError.fromConfig(FaultAbort.forStatus( |
152 |
| - GrpcUtil.httpStatusToGrpcStatus(faultAbort.getHttpStatus()), percent)); |
153 |
| - case GRPC_STATUS: |
154 |
| - return ConfigOrError.fromConfig(FaultAbort.forStatus( |
155 |
| - Status.fromCodeValue(faultAbort.getGrpcStatus()), percent)); |
156 |
| - case ERRORTYPE_NOT_SET: |
157 |
| - default: |
158 |
| - return ConfigOrError.fromError( |
159 |
| - "Unknown error type case: " + faultAbort.getErrorTypeCase()); |
| 126 | + private static ConfigOrError<FaultConfig> parseHttpFault(HTTPFault httpFault) { |
| 127 | + FaultDelay faultDelay = null; |
| 128 | + FaultAbort faultAbort = null; |
| 129 | + if (httpFault.hasDelay()) { |
| 130 | + faultDelay = parseFaultDelay(httpFault.getDelay()); |
| 131 | + } |
| 132 | + if (httpFault.hasAbort()) { |
| 133 | + ConfigOrError<FaultAbort> faultAbortOrError = parseFaultAbort(httpFault.getAbort()); |
| 134 | + if (faultAbortOrError.errorDetail != null) { |
| 135 | + return ConfigOrError.fromError( |
| 136 | + "HttpFault contains invalid FaultAbort: " + faultAbortOrError.errorDetail); |
| 137 | + } |
| 138 | + faultAbort = faultAbortOrError.config; |
| 139 | + } |
| 140 | + Integer maxActiveFaults = null; |
| 141 | + if (httpFault.hasMaxActiveFaults()) { |
| 142 | + maxActiveFaults = httpFault.getMaxActiveFaults().getValue(); |
| 143 | + if (maxActiveFaults < 0) { |
| 144 | + maxActiveFaults = Integer.MAX_VALUE; |
| 145 | + } |
| 146 | + } |
| 147 | + return ConfigOrError.fromConfig(FaultConfig.create(faultDelay, faultAbort, maxActiveFaults)); |
160 | 148 | }
|
161 |
| - } |
162 | 149 |
|
163 |
| - private static FaultConfig.FractionalPercent parsePercent(FractionalPercent proto) { |
164 |
| - switch (proto.getDenominator()) { |
165 |
| - case HUNDRED: |
166 |
| - return FaultConfig.FractionalPercent.perHundred(proto.getNumerator()); |
167 |
| - case TEN_THOUSAND: |
168 |
| - return FaultConfig.FractionalPercent.perTenThousand(proto.getNumerator()); |
169 |
| - case MILLION: |
170 |
| - return FaultConfig.FractionalPercent.perMillion(proto.getNumerator()); |
171 |
| - case UNRECOGNIZED: |
172 |
| - default: |
173 |
| - throw new IllegalArgumentException("Unknown denominator type: " + proto.getDenominator()); |
| 150 | + private static FaultDelay parseFaultDelay( |
| 151 | + io.envoyproxy.envoy.extensions.filters.common.fault.v3.FaultDelay faultDelay) { |
| 152 | + FaultConfig.FractionalPercent percent = parsePercent(faultDelay.getPercentage()); |
| 153 | + if (faultDelay.hasHeaderDelay()) { |
| 154 | + return FaultDelay.forHeader(percent); |
| 155 | + } |
| 156 | + return FaultDelay.forFixedDelay(Durations.toNanos(faultDelay.getFixedDelay()), percent); |
174 | 157 | }
|
175 |
| - } |
176 | 158 |
|
177 |
| - @Override |
178 |
| - public ConfigOrError<FaultConfig> parseFilterConfigOverride(Message rawProtoMessage) { |
179 |
| - return parseFilterConfig(rawProtoMessage); |
| 159 | + @VisibleForTesting |
| 160 | + static ConfigOrError<FaultAbort> parseFaultAbort( |
| 161 | + io.envoyproxy.envoy.extensions.filters.http.fault.v3.FaultAbort faultAbort) { |
| 162 | + FaultConfig.FractionalPercent percent = parsePercent(faultAbort.getPercentage()); |
| 163 | + switch (faultAbort.getErrorTypeCase()) { |
| 164 | + case HEADER_ABORT: |
| 165 | + return ConfigOrError.fromConfig(FaultAbort.forHeader(percent)); |
| 166 | + case HTTP_STATUS: |
| 167 | + return ConfigOrError.fromConfig(FaultAbort.forStatus( |
| 168 | + GrpcUtil.httpStatusToGrpcStatus(faultAbort.getHttpStatus()), percent)); |
| 169 | + case GRPC_STATUS: |
| 170 | + return ConfigOrError.fromConfig(FaultAbort.forStatus( |
| 171 | + Status.fromCodeValue(faultAbort.getGrpcStatus()), percent)); |
| 172 | + case ERRORTYPE_NOT_SET: |
| 173 | + default: |
| 174 | + return ConfigOrError.fromError( |
| 175 | + "Unknown error type case: " + faultAbort.getErrorTypeCase()); |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private static FaultConfig.FractionalPercent parsePercent(FractionalPercent proto) { |
| 180 | + switch (proto.getDenominator()) { |
| 181 | + case HUNDRED: |
| 182 | + return FaultConfig.FractionalPercent.perHundred(proto.getNumerator()); |
| 183 | + case TEN_THOUSAND: |
| 184 | + return FaultConfig.FractionalPercent.perTenThousand(proto.getNumerator()); |
| 185 | + case MILLION: |
| 186 | + return FaultConfig.FractionalPercent.perMillion(proto.getNumerator()); |
| 187 | + case UNRECOGNIZED: |
| 188 | + default: |
| 189 | + throw new IllegalArgumentException("Unknown denominator type: " + proto.getDenominator()); |
| 190 | + } |
| 191 | + } |
180 | 192 | }
|
181 | 193 |
|
182 | 194 | @Nullable
|
|
0 commit comments