Skip to content

Commit 4ad4373

Browse files
committed
1 parent 59abd04 commit 4ad4373

File tree

2 files changed

+27
-10
lines changed

2 files changed

+27
-10
lines changed

dubbo-cluster/src/main/java/org/apache/dubbo/rpc/cluster/RouterChain.java

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,20 @@
1717
package org.apache.dubbo.rpc.cluster;
1818

1919
import org.apache.dubbo.common.URL;
20+
import org.apache.dubbo.common.Version;
2021
import org.apache.dubbo.common.extension.ExtensionLoader;
22+
import org.apache.dubbo.common.logger.Logger;
23+
import org.apache.dubbo.common.logger.LoggerFactory;
2124
import org.apache.dubbo.common.threadlocal.NamedInternalThreadFactory;
2225
import org.apache.dubbo.common.threadpool.ThreadPool;
2326
import org.apache.dubbo.common.threadpool.support.fixed.FixedThreadPool;
2427
import org.apache.dubbo.common.utils.BitList;
2528
import org.apache.dubbo.common.utils.CollectionUtils;
29+
import org.apache.dubbo.common.utils.NetUtils;
2630
import org.apache.dubbo.rpc.Invocation;
2731
import org.apache.dubbo.rpc.Invoker;
32+
import org.apache.dubbo.rpc.RpcException;
33+
import org.apache.dubbo.rpc.cluster.directory.StaticDirectory;
2834
import org.apache.dubbo.rpc.cluster.router.state.AddrCache;
2935
import org.apache.dubbo.rpc.cluster.router.state.RouterCache;
3036
import org.apache.dubbo.rpc.cluster.router.state.StateRouter;
@@ -75,10 +81,12 @@ public class RouterChain<T> {
7581
0L, TimeUnit.MILLISECONDS,
7682
new LinkedBlockingQueue<Runnable>(1024), new NamedInternalThreadFactory("dubbo-state-router-loop-",true), new ThreadPoolExecutor.AbortPolicy());
7783

78-
private final static ExecutorService poolRouterThreadPool = new ThreadPoolExecutor(1, 1,
84+
private final static ExecutorService poolRouterThreadPool = new ThreadPoolExecutor(1, 10,
7985
0L, TimeUnit.MILLISECONDS,
8086
new LinkedBlockingQueue<Runnable>(1024), new NamedInternalThreadFactory("dubbo-state-router-pool-",true), new ThreadPoolExecutor.AbortPolicy());
8187

88+
private static final Logger logger = LoggerFactory.getLogger(StaticDirectory.class);
89+
8290
public static <T> RouterChain<T> buildChain(URL url) {
8391
return new RouterChain<>(url);
8492
}
@@ -176,9 +184,14 @@ private void sort() {
176184
public List<Invoker<T>> route(URL url, Invocation invocation) {
177185

178186
AddrCache cache = this.cache.get();
179-
//if (cache == null) {
180-
// buildCache();
181-
//}
187+
if (cache == null) {
188+
throw new RpcException(RpcException.ROUTER_CACHE_NOT_BUILD, "Failed to invoke the method "
189+
+ invocation.getMethodName() + " in the service " + url.getServiceInterface()
190+
+ ". address cache not build "
191+
+ " on the consumer " + NetUtils.getLocalHost()
192+
+ " using the dubbo version " + Version.getVersion()
193+
+ ".");
194+
}
182195
BitList<Invoker<T>> finalBitListInvokers = new BitList<Invoker<T>>(invokers, false);
183196
for (StateRouter stateRouter : stateRouters) {
184197
if (stateRouter.isEnable()) {
@@ -225,11 +238,11 @@ private void buildCache() {
225238
public void run() {
226239
RouterCache routerCache = null;
227240
try {
228-
routerCache = poolRouter(stateRouter, origin, new ArrayList<>(copyInvokers));
241+
routerCache = poolRouter(stateRouter, origin, copyInvokers);
229242
//file cache
230243
newCache.getCache().put(stateRouter.getName(), routerCache);
231-
} catch (Exception e) {
232-
e.printStackTrace();
244+
} catch (Throwable t) {
245+
logger.error("Failed to pool router: " + stateRouter.getUrl() + ", cause: " + t.getMessage(), t);
233246
} finally {
234247
cdl.countDown();
235248
}
@@ -248,13 +261,16 @@ public void run() {
248261

249262
private RouterCache poolRouter(StateRouter router, AddrCache orign, List<Invoker<T>> invokers) {
250263
String routerName = router.getName();
251-
264+
RouterCache routerCache = null;
252265
if (isCacheMiss(orign, routerName) || router.shouldRePool()) {
253266
return router.pool(invokers);
254-
255267
} else {
256-
return orign.getCache().get(routerName);
268+
routerCache = orign.getCache().get(routerName);
269+
}
270+
if (routerCache == null) {
271+
return new RouterCache();
257272
}
273+
return routerCache;
258274
}
259275

260276
private boolean isCacheMiss(AddrCache cache, String routerName) {

dubbo-rpc/dubbo-rpc-api/src/main/java/org/apache/dubbo/rpc/RpcException.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
public static final int NO_INVOKER_AVAILABLE_AFTER_FILTER = 6;
3838
public static final int LIMIT_EXCEEDED_EXCEPTION = 7;
3939
public static final int TIMEOUT_TERMINATE = 8;
40+
public static final int ROUTER_CACHE_NOT_BUILD = 9;
4041
private static final long serialVersionUID = 7815426752583648734L;
4142
/**
4243
* RpcException cannot be extended, use error code for exception type to keep compatibility

0 commit comments

Comments
 (0)