2424import org .apache .dubbo .common .logger .LoggerFactory ;
2525import org .apache .dubbo .common .utils .CollectionUtils ;
2626import org .apache .dubbo .common .utils .NetUtils ;
27+ import org .apache .dubbo .common .utils .StringUtils ;
2728import org .apache .dubbo .rpc .Invocation ;
2829import org .apache .dubbo .rpc .Invoker ;
2930import org .apache .dubbo .rpc .Result ;
4445 */
4546public 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 ()
0 commit comments