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 }
@@ -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 ) {
0 commit comments