|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.shenyu.plugin.apache.dubbo.proxy; |
| 19 | + |
| 20 | +import org.apache.commons.collections4.CollectionUtils; |
| 21 | +import org.apache.commons.lang3.StringUtils; |
| 22 | +import org.apache.dubbo.common.constants.CommonConstants; |
| 23 | +import org.apache.dubbo.common.extension.Activate; |
| 24 | +import org.apache.dubbo.rpc.Filter; |
| 25 | +import org.apache.dubbo.rpc.Invocation; |
| 26 | +import org.apache.dubbo.rpc.Invoker; |
| 27 | +import org.apache.dubbo.rpc.Result; |
| 28 | +import org.apache.dubbo.rpc.RpcException; |
| 29 | +import org.apache.shenyu.common.constant.Constants; |
| 30 | +import org.apache.shenyu.loadbalancer.cache.UpstreamCacheManager; |
| 31 | +import org.apache.shenyu.loadbalancer.entity.Upstream; |
| 32 | + |
| 33 | +import java.util.List; |
| 34 | +import java.util.Objects; |
| 35 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 36 | + |
| 37 | +/** |
| 38 | + * Keeps the selected upstream's outstanding request count current for the dubbo gray |
| 39 | + * least-active path, without wrapping the invoker Dubbo receives. |
| 40 | + * |
| 41 | + * <p>The gray load balance marks the upstream it selected for the current attempt |
| 42 | + * ({@code DUBBO_INFLIGHT_UPSTREAM_URL}); this consumer-side filter runs after the cluster |
| 43 | + * has committed to a provider, so counting here sees every actual attempt, including ones |
| 44 | + * produced by a retry reselection. Wrapping the invoker instead would put a foreign object |
| 45 | + * into Dubbo's already-tried list, where raw-directory candidates can never compare equal |
| 46 | + * to it and a failed provider would be retried even when another one is available (#8132). |
| 47 | + * |
| 48 | + * <p>The count is increased when the RPC starts, not when the invoker is selected, and it is |
| 49 | + * released exactly once per attempt, either when the returned result completes or when the |
| 50 | + * invocation throws before producing one. The marker attachment is removed before the call |
| 51 | + * continues towards the remote peer, so it never reaches the provider. |
| 52 | + */ |
| 53 | +@Activate(group = CommonConstants.CONSUMER) |
| 54 | +public class DubboGrayInflightFilter implements Filter { |
| 55 | + |
| 56 | + @Override |
| 57 | + public Result invoke(final Invoker<?> invoker, final Invocation invocation) throws RpcException { |
| 58 | + Object upstreamUrl = invocation.getObjectAttachment(Constants.DUBBO_INFLIGHT_UPSTREAM_URL); |
| 59 | + // the marker is local bookkeeping and must never travel to the provider |
| 60 | + invocation.getObjectAttachments().remove(Constants.DUBBO_INFLIGHT_UPSTREAM_URL); |
| 61 | + if (Objects.isNull(upstreamUrl)) { |
| 62 | + return invoker.invoke(invocation); |
| 63 | + } |
| 64 | + Upstream upstream = matchUpstream(invocation, String.valueOf(upstreamUrl)); |
| 65 | + if (Objects.isNull(upstream)) { |
| 66 | + return invoker.invoke(invocation); |
| 67 | + } |
| 68 | + upstream.getInflight().incrementAndGet(); |
| 69 | + AtomicBoolean released = new AtomicBoolean(); |
| 70 | + Result result; |
| 71 | + try { |
| 72 | + result = invoker.invoke(invocation); |
| 73 | + } catch (RuntimeException e) { |
| 74 | + // RpcException is a RuntimeException; anything thrown before a result exists still releases. |
| 75 | + release(released, upstream); |
| 76 | + throw e; |
| 77 | + } |
| 78 | + if (Objects.isNull(result)) { |
| 79 | + release(released, upstream); |
| 80 | + return null; |
| 81 | + } |
| 82 | + return result.whenCompleteWithContext((response, throwable) -> release(released, upstream)); |
| 83 | + } |
| 84 | + |
| 85 | + private Upstream matchUpstream(final Invocation invocation, final String upstreamUrl) { |
| 86 | + String selectorId = invocation.getAttachment(Constants.DUBBO_SELECTOR_ID); |
| 87 | + if (StringUtils.isBlank(selectorId)) { |
| 88 | + return null; |
| 89 | + } |
| 90 | + List<Upstream> upstreams = UpstreamCacheManager.getInstance().findUpstreamListBySelectorId(selectorId); |
| 91 | + if (CollectionUtils.isEmpty(upstreams)) { |
| 92 | + return null; |
| 93 | + } |
| 94 | + return upstreams.stream() |
| 95 | + .filter(upstream -> upstreamUrl.equals(upstream.getUrl())) |
| 96 | + .findFirst() |
| 97 | + .orElse(null); |
| 98 | + } |
| 99 | + |
| 100 | + private void release(final AtomicBoolean released, final Upstream upstream) { |
| 101 | + if (released.compareAndSet(false, true)) { |
| 102 | + upstream.getInflight().decrementAndGet(); |
| 103 | + } |
| 104 | + } |
| 105 | +} |
0 commit comments