1717package org .apache .dubbo .rpc .cluster ;
1818
1919import org .apache .dubbo .common .URL ;
20+ import org .apache .dubbo .common .Version ;
2021import org .apache .dubbo .common .extension .ExtensionLoader ;
22+ import org .apache .dubbo .common .logger .Logger ;
23+ import org .apache .dubbo .common .logger .LoggerFactory ;
2124import org .apache .dubbo .common .threadlocal .NamedInternalThreadFactory ;
2225import org .apache .dubbo .common .threadpool .ThreadPool ;
2326import org .apache .dubbo .common .threadpool .support .fixed .FixedThreadPool ;
2427import org .apache .dubbo .common .utils .BitList ;
2528import org .apache .dubbo .common .utils .CollectionUtils ;
29+ import org .apache .dubbo .common .utils .NetUtils ;
2630import org .apache .dubbo .rpc .Invocation ;
2731import org .apache .dubbo .rpc .Invoker ;
32+ import org .apache .dubbo .rpc .RpcException ;
33+ import org .apache .dubbo .rpc .cluster .directory .StaticDirectory ;
2834import org .apache .dubbo .rpc .cluster .router .state .AddrCache ;
2935import org .apache .dubbo .rpc .cluster .router .state .RouterCache ;
3036import 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 ) {
0 commit comments