Skip to content

Commit 4244caa

Browse files
committed
1 parent ee573b5 commit 4244caa

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
}
@@ -171,9 +179,14 @@ private void sort() {
171179
public List<Invoker<T>> route(URL url, Invocation invocation) {
172180

173181
AddrCache cache = this.cache.get();
174-
//if (cache == null) {
175-
// buildCache();
176-
//}
182+
if (cache == null) {
183+
throw new RpcException(RpcException.ROUTER_CACHE_NOT_BUILD, "Failed to invoke the method "
184+
+ invocation.getMethodName() + " in the service " + url.getServiceInterface()
185+
+ ". address cache not build "
186+
+ " on the consumer " + NetUtils.getLocalHost()
187+
+ " using the dubbo version " + Version.getVersion()
188+
+ ".");
189+
}
177190
BitList<Invoker<T>> finalBitListInvokers = new BitList<Invoker<T>>(invokers, false);
178191
for (StateRouter stateRouter : stateRouters) {
179192
if (stateRouter.isEnable()) {
@@ -220,11 +233,11 @@ private void buildCache() {
220233
public void run() {
221234
RouterCache routerCache = null;
222235
try {
223-
routerCache = poolRouter(stateRouter, origin, new ArrayList<>(copyInvokers));
236+
routerCache = poolRouter(stateRouter, origin, copyInvokers);
224237
//file cache
225238
newCache.getCache().put(stateRouter.getName(), routerCache);
226-
} catch (Exception e) {
227-
e.printStackTrace();
239+
} catch (Throwable t) {
240+
logger.error("Failed to pool router: " + stateRouter.getUrl() + ", cause: " + t.getMessage(), t);
228241
} finally {
229242
cdl.countDown();
230243
}
@@ -243,13 +256,16 @@ public void run() {
243256

244257
private RouterCache poolRouter(StateRouter router, AddrCache orign, List<Invoker<T>> invokers) {
245258
String routerName = router.getName();
246-
259+
RouterCache routerCache = null;
247260
if (isCacheMiss(orign, routerName) || router.shouldRePool()) {
248261
return router.pool(invokers);
249-
250262
} else {
251-
return orign.getCache().get(routerName);
263+
routerCache = orign.getCache().get(routerName);
264+
}
265+
if (routerCache == null) {
266+
return new RouterCache();
252267
}
268+
return routerCache;
253269
}
254270

255271
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)