Skip to content

Commit 5b000a3

Browse files
code4wtbeiwei30
authored andcommitted
Simplify the code logic of the method AbstractClusterInvoker#reselect. (#2826)
* Simplify the code logic of the method AbstractClusterInvoker#reselect. * Minor modification for code style.
1 parent ea71adb commit 5b000a3

File tree

2 files changed

+36
-36
lines changed

2 files changed

+36
-36
lines changed

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/AbstractClusterInvoker.java

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.apache.dubbo.common.logger.LoggerFactory;
2525
import org.apache.dubbo.common.utils.CollectionUtils;
2626
import org.apache.dubbo.common.utils.NetUtils;
27+
import org.apache.dubbo.common.utils.StringUtils;
2728
import org.apache.dubbo.rpc.Invocation;
2829
import org.apache.dubbo.rpc.Invoker;
2930
import org.apache.dubbo.rpc.Result;
@@ -44,8 +45,8 @@
4445
*/
4546
public abstract class AbstractClusterInvoker<T> implements Invoker<T> {
4647

47-
private static final Logger logger = LoggerFactory
48-
.getLogger(AbstractClusterInvoker.class);
48+
private static final Logger logger = LoggerFactory.getLogger(AbstractClusterInvoker.class);
49+
4950
protected final Directory<T> directory;
5051

5152
protected final boolean availablecheck;
@@ -110,13 +111,16 @@ public void destroy() {
110111
* @return the invoker which will final to do invoke.
111112
* @throws RpcException
112113
*/
113-
protected Invoker<T> select(LoadBalance loadbalance, Invocation invocation, List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
114-
if (invokers == null || invokers.isEmpty()) {
114+
protected Invoker<T> select(LoadBalance loadbalance, Invocation invocation,
115+
List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
116+
117+
if (CollectionUtils.isEmpty(invokers)) {
115118
return null;
116119
}
117-
String methodName = invocation == null ? "" : invocation.getMethodName();
120+
String methodName = invocation == null ? StringUtils.EMPTY : invocation.getMethodName();
118121

119-
boolean sticky = invokers.get(0).getUrl().getMethodParameter(methodName, Constants.CLUSTER_STICKY_KEY, Constants.DEFAULT_CLUSTER_STICKY);
122+
boolean sticky = invokers.get(0).getUrl()
123+
.getMethodParameter(methodName, Constants.CLUSTER_STICKY_KEY, Constants.DEFAULT_CLUSTER_STICKY);
120124
{
121125
//ignore overloaded method
122126
if (stickyInvoker != null && !invokers.contains(stickyInvoker)) {
@@ -137,8 +141,10 @@ protected Invoker<T> select(LoadBalance loadbalance, Invocation invocation, List
137141
return invoker;
138142
}
139143

140-
private Invoker<T> doSelect(LoadBalance loadbalance, Invocation invocation, List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
141-
if (invokers == null || invokers.isEmpty()) {
144+
private Invoker<T> doSelect(LoadBalance loadbalance, Invocation invocation,
145+
List<Invoker<T>> invokers, List<Invoker<T>> selected) throws RpcException {
146+
147+
if (CollectionUtils.isEmpty(invokers)) {
142148
return null;
143149
}
144150
if (invokers.size() == 1) {
@@ -158,7 +164,7 @@ private Invoker<T> doSelect(LoadBalance loadbalance, Invocation invocation, List
158164
int index = invokers.indexOf(invoker);
159165
try {
160166
//Avoid collision
161-
invoker = index < invokers.size() - 1 ? invokers.get(index + 1) : invokers.get(0);
167+
invoker = invokers.get((index + 1) % invokers.size());
162168
} catch (Exception e) {
163169
logger.warn(e.getMessage() + " may because invokers list dynamic change, ignore.", e);
164170
}
@@ -171,7 +177,8 @@ private Invoker<T> doSelect(LoadBalance loadbalance, Invocation invocation, List
171177
}
172178

173179
/**
174-
* Reselect, use invokers not in `selected` first, if all invokers are in `selected`, just pick an available one using loadbalance policy.
180+
* Reselect, use invokers not in `selected` first, if all invokers are in `selected`,
181+
* just pick an available one using loadbalance policy.
175182
*
176183
* @param loadbalance
177184
* @param invocation
@@ -181,34 +188,27 @@ private Invoker<T> doSelect(LoadBalance loadbalance, Invocation invocation, List
181188
* @throws RpcException
182189
*/
183190
private Invoker<T> reselect(LoadBalance loadbalance, Invocation invocation,
184-
List<Invoker<T>> invokers, List<Invoker<T>> selected, boolean availablecheck)
185-
throws RpcException {
191+
List<Invoker<T>> invokers, List<Invoker<T>> selected, boolean availablecheck) throws RpcException {
186192

187193
//Allocating one in advance, this list is certain to be used.
188-
List<Invoker<T>> reselectInvokers = new ArrayList<Invoker<T>>(invokers.size() > 1 ? (invokers.size() - 1) : invokers.size());
194+
List<Invoker<T>> reselectInvokers = new ArrayList<>(
195+
invokers.size() > 1 ? (invokers.size() - 1) : invokers.size());
189196

190-
//First, try picking a invoker not in `selected`.
191-
if (availablecheck) { // invoker.isAvailable() should be checked
192-
for (Invoker<T> invoker : invokers) {
193-
if (invoker.isAvailable()) {
194-
if (selected == null || !selected.contains(invoker)) {
195-
reselectInvokers.add(invoker);
196-
}
197-
}
198-
}
199-
if (!reselectInvokers.isEmpty()) {
200-
return loadbalance.select(reselectInvokers, getUrl(), invocation);
197+
// First, try picking a invoker not in `selected`.
198+
for (Invoker<T> invoker : invokers) {
199+
if (availablecheck && !invoker.isAvailable()) {
200+
continue;
201201
}
202-
} else { // do not check invoker.isAvailable()
203-
for (Invoker<T> invoker : invokers) {
204-
if (selected == null || !selected.contains(invoker)) {
205-
reselectInvokers.add(invoker);
206-
}
207-
}
208-
if (!reselectInvokers.isEmpty()) {
209-
return loadbalance.select(reselectInvokers, getUrl(), invocation);
202+
203+
if (selected == null || !selected.contains(invoker)) {
204+
reselectInvokers.add(invoker);
210205
}
211206
}
207+
208+
if (!reselectInvokers.isEmpty()) {
209+
return loadbalance.select(reselectInvokers, getUrl(), invocation);
210+
}
211+
212212
// Just pick an available invoker using loadbalance policy
213213
{
214214
if (selected != null) {
@@ -257,7 +257,7 @@ public String toString() {
257257
}
258258

259259
protected void checkInvokers(List<Invoker<T>> invokers, Invocation invocation) {
260-
if (invokers == null || invokers.isEmpty()) {
260+
if (CollectionUtils.isEmpty(invokers)) {
261261
throw new RpcException("Failed to invoke the method "
262262
+ invocation.getMethodName() + " in the service " + getInterface().getName()
263263
+ ". No provider available for the service " + directory.getUrl().getServiceKey()

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/support/FailbackClusterInvoker.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public FailbackClusterInvoker(Directory<T> directory) {
6565
super(directory);
6666
}
6767

68-
private void addFailed(Invocation invocation, AbstractClusterInvoker<?> router) {
68+
private void addFailed(Invocation invocation, AbstractClusterInvoker<?> invoker) {
6969
if (retryFuture == null) {
7070
synchronized (this) {
7171
if (retryFuture == null) {
@@ -84,11 +84,11 @@ public void run() {
8484
}
8585
}
8686
}
87-
failed.put(invocation, router);
87+
failed.put(invocation, invoker);
8888
}
8989

9090
void retryFailed() {
91-
if (failed.size() == 0) {
91+
if (failed.isEmpty()) {
9292
return;
9393
}
9494
for (Map.Entry<Invocation, AbstractClusterInvoker<?>> entry : new HashMap<>(failed).entrySet()) {

0 commit comments

Comments
 (0)