Skip to content

Commit cdcc026

Browse files
committed
fix(dubbo): keep provider identity through Dubbo bookkeeping and count inflight in a consumer filter (#8132)
1 parent aab453f commit cdcc026

10 files changed

Lines changed: 542 additions & 202 deletions

File tree

‎shenyu-common/src/main/java/org/apache/shenyu/common/constant/Constants.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,14 @@ public interface Constants {
591591
*/
592592
String DUBBO_LOAD_BALANCE = "dubboLoadBalance";
593593

594+
/**
595+
* Marks the upstream the gray load balance selected for the current dubbo attempt,
596+
* so the consumer-side inflight filter can keep the provider's outstanding count
597+
* current without wrapping the invoker (a wrapper would break Dubbo's
598+
* already-tried bookkeeping, see #8132).
599+
*/
600+
String DUBBO_INFLIGHT_UPSTREAM_URL = "shenyuDubboInflightUpstreamUrl";
601+
594602
/**
595603
* dubbo serialization protobuf.
596604
*/

‎shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/ApacheDubboGrayLoadBalance.java‎

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,28 +87,37 @@ public <T> Invoker<T> select(final List<Invoker<T>> invokers, final URL url, fin
8787
// list. The upstream it named is not carrying the request, so it is not counted against it.
8888
return dubboSelect(invokers, url, invocation);
8989
}
90-
return withInflight(dubboSelect(invokerGrays, url, invocation), upstream, dubboRuleHandle.getLoadBalance());
90+
return markInflightTarget(invocation, upstream, dubboRuleHandle.getLoadBalance(), dubboSelect(invokerGrays, url, invocation));
9191
}
9292
return dubboSelect(invokers, url, invocation);
9393
}
94-
94+
9595
/**
9696
* Keep the gray upstream's outstanding request count current for least active selection.
9797
*
98-
* <p>Only the least active algorithm reads the count, and only the invoker returned here can observe
99-
* the start and the completion of the RPC, so every other algorithm keeps the plain invoker.
98+
* <p>Only the least active algorithm reads the count. The count itself is maintained by
99+
* {@link DubboGrayInflightFilter}, which runs after the cluster committed to a provider:
100+
* this method only marks the upstream chosen for the current attempt on a local attachment.
101+
* The invoker handed to Dubbo stays the bare directory invoker — a wrapper here would enter
102+
* Dubbo's already-tried list, where raw candidates can never compare equal to it and a failed
103+
* provider would be retried even when another one is available (#8132).
100104
*
101-
* @param selected the invoker chosen for this call
105+
* @param invocation the current invocation
102106
* @param upstream the gray upstream the invoker was chosen for
103107
* @param loadBalance the algorithm configured on the rule
108+
* @param selected the invoker chosen for this call
104109
* @param <T> the service type
105-
* @return the invoker to hand back to Dubbo
110+
* @return the invoker to hand back to Dubbo, unchanged
106111
*/
107-
private static <T> Invoker<T> withInflight(final Invoker<T> selected, final Upstream upstream, final String loadBalance) {
108-
if (Objects.isNull(selected) || !LoadBalanceEnum.LEAST_ACTIVITY.getName().equals(loadBalance)) {
112+
private static <T> Invoker<T> markInflightTarget(final Invocation invocation, final Upstream upstream,
113+
final String loadBalance, final Invoker<T> selected) {
114+
if (Objects.isNull(selected) || !LoadBalanceEnum.LEAST_ACTIVITY.getName().equals(loadBalance)
115+
|| StringUtils.isBlank(upstream.getUrl())) {
116+
// an upstream that is not addressable by url cannot be re-identified by the filter
109117
return selected;
110118
}
111-
return new InflightInvoker<>(selected, upstream);
119+
invocation.setObjectAttachment(Constants.DUBBO_INFLIGHT_UPSTREAM_URL, upstream.getUrl());
120+
return selected;
112121
}
113122

114123
private <T> Invoker<T> dubboSelect(final List<Invoker<T>> invokers, final URL url, final Invocation invocation) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
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+
}

‎shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-rpc/shenyu-plugin-dubbo/shenyu-plugin-apache-dubbo/src/main/java/org/apache/shenyu/plugin/apache/dubbo/proxy/InflightInvoker.java‎

Lines changed: 0 additions & 123 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one or more
2+
# contributor license agreements. See the NOTICE file distributed with
3+
# this work for additional information regarding copyright ownership.
4+
# The ASF licenses this file to You under the Apache License, Version 2.0
5+
# (the "License"); you may not use this file except in compliance with
6+
# the License. You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
16+
shenyuGrayInflight=org.apache.shenyu.plugin.apache.dubbo.proxy.DubboGrayInflightFilter

0 commit comments

Comments
 (0)