2727import java .util .Iterator ;
2828import java .util .List ;
2929import java .util .Map ;
30+ import java .util .Queue ;
3031import java .util .Set ;
3132import java .util .TimeZone ;
33+ import java .util .concurrent .BlockingQueue ;
34+ import java .util .concurrent .ConcurrentLinkedQueue ;
35+ import java .util .concurrent .ExecutorService ;
3236import java .util .concurrent .Executors ;
37+ import java .util .concurrent .LinkedBlockingQueue ;
3338import java .util .concurrent .ScheduledExecutorService ;
3439import java .util .concurrent .TimeUnit ;
3540
@@ -336,6 +341,7 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian
336341
337342 int _routerStatsInterval = 300 ;
338343 int _routerCheckInterval = 30 ;
344+ int _rvrStatusUpdatePoolSize = 10 ;
339345 protected ServiceOfferingVO _offering ;
340346 private String _dnsBasicZoneUpdates = "all" ;
341347 private Set <String > _guestOSNeedGatewayOnNonDefaultNetwork = new HashSet <String >();
@@ -350,8 +356,11 @@ public class VirtualNetworkApplianceManagerImpl implements VirtualNetworkApplian
350356 ScheduledExecutorService _executor ;
351357 ScheduledExecutorService _checkExecutor ;
352358 ScheduledExecutorService _networkStatsUpdateExecutor ;
359+ ExecutorService _rvrStatusUpdateExecutor ;
353360
354361 Account _systemAcct ;
362+
363+ BlockingQueue <Long > _vrUpdateQueue = null ;
355364
356365 @ Override
357366 public boolean sendSshKeysToHost (Long hostId , String pubKey , String prvKey ) {
@@ -579,7 +588,7 @@ public boolean configure(final String name, final Map<String, Object> params) th
579588 _executor = Executors .newScheduledThreadPool (1 , new NamedThreadFactory ("RouterMonitor" ));
580589 _checkExecutor = Executors .newScheduledThreadPool (1 , new NamedThreadFactory ("RouterStatusMonitor" ));
581590 _networkStatsUpdateExecutor = Executors .newScheduledThreadPool (1 , new NamedThreadFactory ("NetworkStatsUpdater" ));
582-
591+
583592 final ComponentLocator locator = ComponentLocator .getCurrentLocator ();
584593
585594 final Map <String , String > configs = _configDao .getConfiguration ("AgentManager" , params );
@@ -606,7 +615,18 @@ public boolean configure(final String name, final Map<String, Object> params) th
606615
607616 value = configs .get ("router.check.interval" );
608617 _routerCheckInterval = NumbersUtil .parseInt (value , 30 );
609-
618+
619+ value = configs .get ("router.check.poolsize" );
620+ _rvrStatusUpdatePoolSize = NumbersUtil .parseInt (value , 10 );
621+
622+ /*
623+ * We assume that one thread can handle 20 requests in 1 minute in normal situation, so here we give the queue size up to 50 minutes.
624+ * It's mostly for buffer, since each time CheckRouterTask running, it would add all the redundant networks in the queue immediately
625+ */
626+ _vrUpdateQueue = new LinkedBlockingQueue <Long >(_rvrStatusUpdatePoolSize * 1000 );
627+
628+ _rvrStatusUpdateExecutor = Executors .newFixedThreadPool (_rvrStatusUpdatePoolSize , new NamedThreadFactory ("RedundantRouterStatusMonitor" ));
629+
610630 _instance = configs .get ("instance.name" );
611631 if (_instance == null ) {
612632 _instance = "DEFAULT" ;
@@ -704,6 +724,9 @@ public boolean start() {
704724
705725 if (_routerCheckInterval > 0 ) {
706726 _checkExecutor .scheduleAtFixedRate (new CheckRouterTask (), _routerCheckInterval , _routerCheckInterval , TimeUnit .SECONDS );
727+ for (int i = 0 ; i < _rvrStatusUpdatePoolSize ; i ++) {
728+ _rvrStatusUpdateExecutor .execute (new RvRStatusUpdateTask ());
729+ }
707730 } else {
708731 s_logger .debug ("router.check.interval - " + _routerCheckInterval + " so not scheduling the redundant router checking thread" );
709732 }
@@ -1039,14 +1062,11 @@ protected void updateRoutersRedundantState(List<DomainRouterVO> routers) {
10391062 if (host == null || host .getStatus () != Status .Up ) {
10401063 router .setRedundantState (RedundantState .UNKNOWN );
10411064 updated = true ;
1042- } else if (host .getManagementServerId () != ManagementServerNode .getManagementServerId ()) {
1043- /* Only cover hosts managed by this management server */
1044- continue ;
10451065 } else if (privateIP != null ) {
10461066 final CheckRouterCommand command = new CheckRouterCommand ();
10471067 command .setAccessDetail (NetworkElementCommand .ROUTER_IP , getRouterControlIp (router .getId ()));
10481068 command .setAccessDetail (NetworkElementCommand .ROUTER_NAME , router .getInstanceName ());
1049- command .setWait (60 );
1069+ command .setWait (30 );
10501070 final Answer origAnswer = _agentMgr .easySend (router .getHostId (), command );
10511071 CheckRouterAnswer answer = null ;
10521072 if (origAnswer instanceof CheckRouterAnswer ) {
@@ -1128,11 +1148,11 @@ private int getRealPriority(DomainRouterVO router) {
11281148 return priority ;
11291149 }
11301150
1131- protected class CheckRouterTask implements Runnable {
1151+ protected class RvRStatusUpdateTask implements Runnable {
11321152
1133- public CheckRouterTask () {
1153+ public RvRStatusUpdateTask () {
11341154 }
1135-
1155+
11361156 /*
11371157 * In order to make fail-over works well at any time, we have to ensure:
11381158 * 1. Backup router's priority = Master's priority - DELTA + 1
@@ -1195,9 +1215,9 @@ private void checkDuplicateMaster(List <DomainRouterVO> routers) {
11951215 String title = "More than one redundant virtual router is in MASTER state! Router " + router .getHostName () + " and router " + dupRouter .getHostName ();
11961216 String context = "Virtual router (name: " + router .getHostName () + ", id: " + router .getId () + " and router (name: "
11971217 + dupRouter .getHostName () + ", id: " + router .getId () + ") are both in MASTER state! If the problem persist, restart both of routers. " ;
1198-
11991218 _alertMgr .sendAlert (AlertManager .ALERT_TYPE_DOMAIN_ROUTER , router .getDataCenterIdToDeployIn (), router .getPodIdToDeployIn (), title , context );
12001219 _alertMgr .sendAlert (AlertManager .ALERT_TYPE_DOMAIN_ROUTER , dupRouter .getDataCenterIdToDeployIn (), dupRouter .getPodIdToDeployIn (), title , context );
1220+ s_logger .warn (context );
12011221 } else {
12021222 networkRouterMaps .put (routerGuestNtwkId , router );
12031223 }
@@ -1206,19 +1226,61 @@ private void checkDuplicateMaster(List <DomainRouterVO> routers) {
12061226 }
12071227 }
12081228
1229+ @ Override
1230+ public void run () {
1231+ while (true ) {
1232+ try {
1233+ Long networkId = _vrUpdateQueue .take ();
1234+ List <DomainRouterVO > routers = _routerDao .listByNetworkAndRole (networkId , Role .VIRTUAL_ROUTER );
1235+
1236+ if (routers .size () != 2 ) {
1237+ continue ;
1238+ }
1239+ /*
1240+ * We update the router pair which the lower id router owned by this mgmt server, in order
1241+ * to prevent duplicate update of router status from cluster mgmt servers
1242+ */
1243+ DomainRouterVO router = routers .get (0 );
1244+ if (routers .get (1 ).getId () < router .getId ()) {
1245+ router = routers .get (1 );
1246+ }
1247+ HostVO host = _hostDao .findById (router .getHostId ());
1248+ if (host == null || host .getManagementServerId () == null ||
1249+ host .getManagementServerId () != ManagementServerNode .getManagementServerId ()) {
1250+ continue ;
1251+ }
1252+ updateRoutersRedundantState (routers );
1253+ checkDuplicateMaster (routers );
1254+ checkSanity (routers );
1255+ } catch (Exception ex ) {
1256+ s_logger .error ("Fail to complete the RvRStatusUpdateTask! " , ex );
1257+ }
1258+ }
1259+ }
1260+
1261+ }
1262+
1263+ protected class CheckRouterTask implements Runnable {
1264+
1265+ public CheckRouterTask () {
1266+ }
1267+
12091268 @ Override
12101269 public void run () {
12111270 try {
12121271 final List <DomainRouterVO > routers = _routerDao .listIsolatedByHostId (null );
1213- s_logger .debug ("Found " + routers .size () + " routers. " );
1272+ s_logger .debug ("Found " + routers .size () + " routers to update status . " );
12141273
1215- updateRoutersRedundantState (routers );
12161274 updateSite2SiteVpnConnectionState (routers );
12171275
1218- /* FIXME assumed the a pair of redundant routers managed by same mgmt server,
1219- * then the update above can get the latest status */
1220- checkDuplicateMaster (routers );
1221- checkSanity (routers );
1276+ final List <NetworkVO > networks = _networkDao .listRedundantNetworks ();
1277+ s_logger .debug ("Found " + networks .size () + " networks to update RvR status. " );
1278+ for (NetworkVO network : networks ) {
1279+ if (!_vrUpdateQueue .offer (network .getId (), 500 , TimeUnit .MILLISECONDS )) {
1280+ s_logger .warn ("Cannot insert into virtual router update queue! Adjustment of router.check.interval and router.check.poolsize maybe needed." );
1281+ break ;
1282+ }
1283+ }
12221284 } catch (Exception ex ) {
12231285 s_logger .error ("Fail to complete the CheckRouterTask! " , ex );
12241286 }
0 commit comments