@@ -1266,58 +1266,50 @@ protected List<DomainRouterVO> findOrDeployVirtualRouterInGuestNetwork(Network g
12661266 boolean isRedundant , Map <Param , Object > params ) throws ConcurrentOperationException ,
12671267 InsufficientCapacityException , ResourceUnavailableException {
12681268
1269- assert guestNetwork .getState () == Network .State .Implemented || guestNetwork .getState () == Network .State .Setup ||
1270- guestNetwork .getState () == Network .State .Implementing : "Network is not yet fully implemented: "
1271- + guestNetwork ;
1272- assert guestNetwork .getTrafficType () == TrafficType .Guest ;
1273-
1274-
1275-
1276- //1) Get deployment plan and find out the list of routers
1277- boolean isPodBased = (dest .getDataCenter ().getNetworkType () == NetworkType .Basic ||
1278- _networkMgr .areServicesSupportedInNetwork (guestNetwork .getId (), Service .SecurityGroup ))
1279- && guestNetwork .getTrafficType () == TrafficType .Guest ;
1280-
1281- Pair <DeploymentPlan , List <DomainRouterVO >> planAndRouters = getDeploymentPlanAndRouters (isPodBased , dest , guestNetwork .getId ());
1282- DeploymentPlan plan = planAndRouters .first ();
1283- List <DomainRouterVO > routers = planAndRouters .second ();
12841269
1285- //2) Figure out required routers count
1286- int routerCount = 1 ;
1287- if (isRedundant ) {
1288- routerCount = 2 ;
1289- }
1290-
1291- /* If it is the single router network, then keep it untouched */
1292- for (DomainRouterVO router : routers ) {
1293- if (!router .getIsRedundantRouter () || isPodBased ) {
1294- routerCount = 1 ;
1295- break ;
1296- }
1297- }
1298-
1299- /* If old network is redundant but new is single router, then routers.size() = 2 but routerCount = 1 */
1300- if (routers .size () >= routerCount ) {
1301- return routers ;
1302- }
1303-
1304- if (routers .size () >= 5 ) {
1305- s_logger .error ("Too much redundant routers!" );
1306- }
1307-
1308- Network network = _networkDao .acquireInLockTable (guestNetwork .getId ());
1309- if (network == null ) {
1270+ List <DomainRouterVO > routers = new ArrayList <DomainRouterVO >();
1271+ Network lock = _networkDao .acquireInLockTable (guestNetwork .getId (), _networkMgr .getNetworkLockTimeout ());
1272+ if (lock == null ) {
13101273 throw new ConcurrentOperationException ("Unable to lock network " + guestNetwork .getId ());
13111274 }
13121275
13131276 if (s_logger .isDebugEnabled ()) {
1314- s_logger .debug ("Lock is acquired for network id " + network .getId () + " as a part of router startup in " + dest );
1277+ s_logger .debug ("Lock is acquired for network id " + lock .getId () + " as a part of router startup in " + dest );
13151278 }
1316-
1279+
13171280 try {
1318- //Check if providers are supported in the physical networks
1281+
1282+ assert guestNetwork .getState () == Network .State .Implemented || guestNetwork .getState () == Network .State .Setup ||
1283+ guestNetwork .getState () == Network .State .Implementing : "Network is not yet fully implemented: "
1284+ + guestNetwork ;
1285+ assert guestNetwork .getTrafficType () == TrafficType .Guest ;
1286+
1287+ // 1) Get deployment plan and find out the list of routers
1288+ boolean isPodBased = (dest .getDataCenter ().getNetworkType () == NetworkType .Basic ||
1289+ _networkMgr .areServicesSupportedInNetwork (guestNetwork .getId (), Service .SecurityGroup ))
1290+ && guestNetwork .getTrafficType () == TrafficType .Guest ;
1291+
1292+ Pair <DeploymentPlan , List <DomainRouterVO >> planAndRouters = getDeploymentPlanAndRouters (isPodBased , dest , guestNetwork .getId ());
1293+ routers = planAndRouters .second ();
1294+
1295+ // 2) Figure out required routers count
1296+ int routerCount = 1 ;
1297+ if (isRedundant ) {
1298+ routerCount = 2 ;
1299+ }
1300+
1301+ /* If old network is redundant but new is single router, then routers.size() = 2 but routerCount = 1 */
1302+ if (routers .size () >= routerCount ) {
1303+ return routers ;
1304+ }
1305+
1306+ if (routers .size () >= 5 ) {
1307+ s_logger .error ("Too much redundant routers!" );
1308+ }
1309+
1310+ // Check if providers are supported in the physical networks
13191311 VirtualRouterProviderType type = VirtualRouterProviderType .VirtualRouter ;
1320- Long physicalNetworkId = _networkMgr .getPhysicalNetworkId (network );
1312+ Long physicalNetworkId = _networkMgr .getPhysicalNetworkId (guestNetwork );
13211313 PhysicalNetworkServiceProvider provider = _physicalProviderDao .findByServiceProvider (physicalNetworkId , type .toString ());
13221314 if (provider == null ) {
13231315 throw new CloudRuntimeException ("Cannot find service provider " + type .toString () + " in physical network " + physicalNetworkId );
@@ -1353,21 +1345,22 @@ protected List<DomainRouterVO> findOrDeployVirtualRouterInGuestNetwork(Network g
13531345
13541346 //3) deploy virtual router(s)
13551347 int count = routerCount - routers .size ();
1348+ DeploymentPlan plan = planAndRouters .first ();
13561349 for (int i = 0 ; i < count ; i ++) {
13571350 List <Pair <NetworkVO , NicProfile >> networks = createRouterNetworks (owner , isRedundant , plan , guestNetwork ,
13581351 new Pair <Boolean , PublicIp >(publicNetwork , sourceNatIp ));
1352+ //don't start the router as we are holding the network lock that needs to be released at the end of router allocation
13591353 DomainRouterVO router = deployRouter (owner , dest , plan , params , isRedundant , vrProvider , offeringId ,
1360- null , networks );
1361-
1362- _routerDao .addRouterToGuestNetwork (router , network );
1363-
1354+ null , networks , false );
1355+
1356+ _routerDao .addRouterToGuestNetwork (router , guestNetwork );
13641357 routers .add (router );
13651358 }
13661359 } finally {
1367- if (network != null ) {
1368- _networkDao .releaseFromLockTable (network .getId ());
1360+ if (lock != null ) {
1361+ _networkDao .releaseFromLockTable (lock .getId ());
13691362 if (s_logger .isDebugEnabled ()) {
1370- s_logger .debug ("Lock is released for network id " + network .getId () + " as a part of router startup in " + dest );
1363+ s_logger .debug ("Lock is released for network id " + lock .getId () + " as a part of router startup in " + dest );
13711364 }
13721365 }
13731366 }
@@ -1376,8 +1369,8 @@ protected List<DomainRouterVO> findOrDeployVirtualRouterInGuestNetwork(Network g
13761369
13771370 protected DomainRouterVO deployRouter (Account owner , DeployDestination dest , DeploymentPlan plan , Map <Param , Object > params ,
13781371 boolean isRedundant , VirtualRouterProvider vrProvider , long svcOffId ,
1379- Long vpcId , List <Pair <NetworkVO , NicProfile >> networks ) throws ConcurrentOperationException ,
1380- InsufficientAddressCapacityException , InsufficientServerCapacityException , InsufficientCapacityException ,
1372+ Long vpcId , List <Pair <NetworkVO , NicProfile >> networks , boolean startRouter ) throws ConcurrentOperationException ,
1373+ InsufficientAddressCapacityException , InsufficientServerCapacityException , InsufficientCapacityException ,
13811374 StorageUnavailableException , ResourceUnavailableException {
13821375
13831376 long id = _routerDao .getNextInSequence (Long .class , "id" );
@@ -1388,33 +1381,9 @@ protected DomainRouterVO deployRouter(Account owner, DeployDestination dest, Dep
13881381 ServiceOfferingVO routerOffering = _serviceOfferingDao .findById (svcOffId );
13891382
13901383 // Router is the network element, we don't know the hypervisor type yet.
1391- //Try to allocate the domR twice using diff hypervisors, and when failed both times, throw the exception up
1392- List <HypervisorType > supportedHypervisors = new ArrayList <HypervisorType >();
1393- HypervisorType defaults = _resourceMgr .getDefaultHypervisor (dest .getDataCenter ().getId ());
1394- if (defaults != HypervisorType .None ) {
1395- supportedHypervisors .add (defaults );
1396- }
1397-
1398- if (dest .getCluster () != null ) {
1399- if (dest .getCluster ().getHypervisorType () == HypervisorType .Ovm ) {
1400- supportedHypervisors .add (getClusterToStartDomainRouterForOvm (dest .getCluster ().getPodId ()));
1401- } else {
1402- supportedHypervisors .add (dest .getCluster ().getHypervisorType ());
1403- }
1404- } else {
1405- supportedHypervisors = _resourceMgr .getSupportedHypervisorTypes (dest .getDataCenter ().getId (), true ,
1406- plan .getPodId ());
1407- }
1408-
1409- if (supportedHypervisors .isEmpty ()) {
1410- if (plan .getPodId () != null ) {
1411- throw new InsufficientServerCapacityException ("Unable to create virtual router, " +
1412- "there are no clusters in the pod " , Pod .class , plan .getPodId ());
1413- }
1414- throw new InsufficientServerCapacityException ("Unable to create virtual router, " +
1415- "there are no clusters in the zone " , DataCenter .class , dest .getDataCenter ().getId ());
1416- }
1417-
1384+ // Try to allocate the domR twice using diff hypervisors, and when failed both times, throw the exception up
1385+ List <HypervisorType > supportedHypervisors = getSupportedHypervisors (dest , plan );
1386+
14181387 int allocateRetry = 0 ;
14191388 int startRetry = 0 ;
14201389 DomainRouterVO router = null ;
@@ -1443,37 +1412,70 @@ protected DomainRouterVO deployRouter(Account owner, DeployDestination dest, Dep
14431412 router = _itMgr .allocate (router , template , routerOffering , networks , plan , null , owner );
14441413 } catch (InsufficientCapacityException ex ) {
14451414 if (allocateRetry < 2 && iter .hasNext ()) {
1446- s_logger .debug ("Failed to allocate the domR with hypervisor type " + hType + ", retrying one more time" );
1415+ s_logger .debug ("Failed to allocate the VR with hypervisor type " + hType + ", retrying one more time" );
14471416 continue ;
14481417 } else {
14491418 throw ex ;
14501419 }
14511420 } finally {
14521421 allocateRetry ++;
14531422 }
1454-
1455- try {
1456- router = startVirtualRouter (router , _accountMgr .getSystemUser (), _accountMgr .getSystemAccount (), params );
1457- break ;
1458- } catch (InsufficientCapacityException ex ) {
1459- if (startRetry < 2 && iter .hasNext ()) {
1460- s_logger .debug ("Failed to start the domR " + router + " with hypervisor type " + hType + ", " +
1461- "destroying it and recreating one more time" );
1462- //destroy the router
1463- destroyRouter (router .getId ());
1464- continue ;
1465- } else {
1466- throw ex ;
1423+
1424+
1425+ if (startRouter ) {
1426+ try {
1427+ router = startVirtualRouter (router , _accountMgr .getSystemUser (), _accountMgr .getSystemAccount (), params );
1428+ break ;
1429+ } catch (InsufficientCapacityException ex ) {
1430+ if (startRetry < 2 && iter .hasNext ()) {
1431+ s_logger .debug ("Failed to start the VR " + router + " with hypervisor type " + hType + ", " +
1432+ "destroying it and recreating one more time" );
1433+ // destroy the router
1434+ destroyRouter (router .getId ());
1435+ continue ;
1436+ } else {
1437+ throw ex ;
1438+ }
1439+ } finally {
1440+ startRetry ++;
14671441 }
1468- } finally {
1469- startRetry ++;
14701442 }
14711443 }
14721444
14731445 return router ;
14741446 }
14751447
1476- protected List <Pair <NetworkVO , NicProfile >> createRouterNetworks (Account owner , boolean isRedundant ,
1448+
1449+ protected List <HypervisorType > getSupportedHypervisors (DeployDestination dest , DeploymentPlan plan ) throws InsufficientServerCapacityException {
1450+ List <HypervisorType > supportedHypervisors = new ArrayList <HypervisorType >();
1451+ HypervisorType defaults = _resourceMgr .getDefaultHypervisor (dest .getDataCenter ().getId ());
1452+ if (defaults != HypervisorType .None ) {
1453+ supportedHypervisors .add (defaults );
1454+ }
1455+
1456+ if (dest .getCluster () != null ) {
1457+ if (dest .getCluster ().getHypervisorType () == HypervisorType .Ovm ) {
1458+ supportedHypervisors .add (getClusterToStartDomainRouterForOvm (dest .getCluster ().getPodId ()));
1459+ } else {
1460+ supportedHypervisors .add (dest .getCluster ().getHypervisorType ());
1461+ }
1462+ } else {
1463+ supportedHypervisors = _resourceMgr .getSupportedHypervisorTypes (dest .getDataCenter ().getId (), true ,
1464+ plan .getPodId ());
1465+ }
1466+
1467+ if (supportedHypervisors .isEmpty ()) {
1468+ if (plan .getPodId () != null ) {
1469+ throw new InsufficientServerCapacityException ("Unable to create virtual router, " +
1470+ "there are no clusters in the pod " , Pod .class , plan .getPodId ());
1471+ }
1472+ throw new InsufficientServerCapacityException ("Unable to create virtual router, " +
1473+ "there are no clusters in the zone " , DataCenter .class , dest .getDataCenter ().getId ());
1474+ }
1475+ return supportedHypervisors ;
1476+ }
1477+
1478+ protected List <Pair <NetworkVO , NicProfile >> createRouterNetworks (Account owner , boolean isRedundant ,
14771479 DeploymentPlan plan , Network guestNetwork , Pair <Boolean , PublicIp > publicNetwork ) throws ConcurrentOperationException ,
14781480 InsufficientAddressCapacityException {
14791481
@@ -2340,7 +2342,8 @@ private DomainRouterVO start(DomainRouterVO router, User user, Account caller, M
23402342 ConcurrentOperationException , ResourceUnavailableException {
23412343 s_logger .debug ("Starting router " + router );
23422344 if (_itMgr .start (router , params , user , caller , planToDeploy ) != null ) {
2343- // We don't want the failure of VPN Connection affect the status of router, so we try to make connection only after router start successfully
2345+ // We don't want the failure of VPN Connection affect the status of router, so we try to make connection
2346+ // only after router start successfully
23442347 Long vpcId = router .getVpcId ();
23452348 if (vpcId != null ) {
23462349 _s2sVpnMgr .reconnectDisconnectedVpnByVpc (vpcId );
0 commit comments