@@ -116,8 +116,7 @@ public class RouterDeploymentDefinition {
116116 protected DeploymentPlan plan ;
117117 protected List <DomainRouterVO > routers = new ArrayList <>();
118118 protected ServiceOfferingVO offering ;
119-
120-
119+ protected Long tableLockId ;
121120
122121
123122 protected RouterDeploymentDefinition (final Network guestNetwork , final DeployDestination dest ,
@@ -220,6 +219,171 @@ public List<DomainRouterVO> deployVirtualRouter()
220219 protected void findOrDeployVirtualRouter ()
221220 throws ConcurrentOperationException , InsufficientCapacityException , ResourceUnavailableException {
222221
222+ try {
223+ this .lock ();
224+ this .checkPreconditions ();
225+ // dest has pod=null, for Basic Zone findOrDeployVRs for all Pods
226+ final List <DeployDestination > destinations = findDestinations ();
227+
228+ for (final DeployDestination destination : destinations ) {
229+ this .dest = destination ;
230+ this .planDeploymentRouters ();
231+ this .generateDeploymentPlan ();
232+ this .proceedEffectiveDeployment ();
233+ }
234+ } finally {
235+ this .unlock ();
236+ }
237+ }
238+
239+ protected void lock () {
240+ final Network lock = networkDao .acquireInLockTable (guestNetwork .getId (), NetworkOrchestrationService .NetworkLockTimeout .value ());
241+ if (lock == null ) {
242+ throw new ConcurrentOperationException ("Unable to lock network " + guestNetwork .getId ());
243+ }
244+ this .tableLockId = lock .getId ();
245+ }
246+
247+ protected void unlock () {
248+ if (this .tableLockId != null ) {
249+ networkDao .releaseFromLockTable (this .tableLockId );
250+ if (logger .isDebugEnabled ()) {
251+ logger .debug ("Lock is released for network id " + this .tableLockId
252+ + " as a part of router startup in " + dest );
253+ }
254+ }
255+ }
256+
257+ protected void checkPreconditions () throws ResourceUnavailableException {
258+ if (guestNetwork .getState () != Network .State .Implemented &&
259+ guestNetwork .getState () != Network .State .Setup &&
260+ guestNetwork .getState () != Network .State .Implementing ) {
261+ throw new ResourceUnavailableException ("Network is not yet fully implemented: " + guestNetwork ,
262+ Network .class , this .guestNetwork .getId ());
263+ }
264+
265+ if (guestNetwork .getTrafficType () != TrafficType .Guest ) {
266+ throw new ResourceUnavailableException ("Network is not type Guest as expected: " + guestNetwork ,
267+ Network .class , this .guestNetwork .getId ());
268+ }
269+ }
270+
271+ protected List <DeployDestination > findDestinations () {
272+ // dest has pod=null, for Basic Zone findOrDeployVRs for all Pods
273+ final List <DeployDestination > destinations = new ArrayList <DeployDestination >();
274+
275+ // for basic zone, if 'dest' has pod set to null then this is network restart scenario otherwise it is a vm deployment scenario
276+ if (this .isBasic () && dest .getPod () == null ) {
277+ // Find all pods in the data center with running or starting user vms
278+ final long dcId = dest .getDataCenter ().getId ();
279+ final List <HostPodVO > pods = listByDataCenterIdVMTypeAndStates (dcId , VirtualMachine .Type .User , VirtualMachine .State .Starting , VirtualMachine .State .Running );
280+
281+ // Loop through all the pods skip those with running or starting VRs
282+ for (final HostPodVO pod : pods ) {
283+ // Get list of VRs in starting or running state
284+ final long podId = pod .getId ();
285+ final List <DomainRouterVO > virtualRouters = routerDao .listByPodIdAndStates (podId , VirtualMachine .State .Starting , VirtualMachine .State .Running );
286+
287+ assert (virtualRouters .size () <= 1 ) : "Pod can have utmost one VR in Basic Zone, please check!" ;
288+
289+ // Add virtualRouters to the routers, this avoids the situation when
290+ // all routers are skipped and VirtualRouterElement throws exception
291+ this .routers .addAll (virtualRouters );
292+
293+ // If List size is one, we already have a starting or running VR, skip deployment
294+ if (virtualRouters .size () == 1 ) {
295+ logger .debug ("Skipping VR deployment: Found a running or starting VR in Pod " + pod .getName () + " id=" + podId );
296+ continue ;
297+ }
298+ // Add new DeployDestination for this pod
299+ destinations .add (new DeployDestination (dest .getDataCenter (), pod , null , null ));
300+ }
301+ } else {
302+ // Else, just add the supplied dest
303+ destinations .add (dest );
304+ }
305+ return destinations ;
306+ }
307+
308+ protected void proceedEffectiveDeployment ()
309+ throws ConcurrentOperationException , InsufficientCapacityException , ResourceUnavailableException {
310+ // 2) Figure out required routers count
311+ int routerCount = 1 ;
312+ if (this .isRedundant ) {
313+ routerCount = 2 ;
314+ //Check current redundant routers, if possible(all routers are stopped), reset the priority
315+ if (this .routers .size () != 0 ) {
316+ checkAndResetPriorityOfRedundantRouter (this .routers );
317+ }
318+ }
319+
320+ // If old network is redundant but new is single router, then routers.size() = 2 but routerCount = 1
321+ if (this .routers .size () >= routerCount ) {
322+ return ;
323+ }
324+
325+ if (this .routers .size () >= 5 ) {
326+ logger .error ("Too much redundant routers!" );
327+ }
328+
329+ // Check if providers are supported in the physical networks
330+ final Type type = Type .VirtualRouter ;
331+ final Long physicalNetworkId = networkModel .getPhysicalNetworkId (guestNetwork );
332+ final PhysicalNetworkServiceProvider provider = physicalProviderDao .findByServiceProvider (physicalNetworkId , type .toString ());
333+ if (provider == null ) {
334+ throw new CloudRuntimeException ("Cannot find service provider " + type .toString () + " in physical network " + physicalNetworkId );
335+ }
336+ final VirtualRouterProvider vrProvider = vrProviderDao .findByNspIdAndType (provider .getId (), type );
337+ if (vrProvider == null ) {
338+ throw new CloudRuntimeException ("Cannot find virtual router provider " + type .toString () + " as service provider " + provider .getId ());
339+ }
340+
341+ if (networkModel .isNetworkSystem (guestNetwork ) || guestNetwork .getGuestType () == Network .GuestType .Shared ) {
342+ this .owner = accountMgr .getAccount (Account .ACCOUNT_ID_SYSTEM );
343+ }
344+
345+ // Check if public network has to be set on VR
346+ boolean publicNetwork = false ;
347+ if (networkModel .isProviderSupportServiceInNetwork (guestNetwork .getId (), Service .SourceNat , Provider .VirtualRouter )) {
348+ publicNetwork = true ;
349+ }
350+ if (this .isRedundant && !publicNetwork ) {
351+ logger .error ("Didn't support redundant virtual router without public network!" );
352+ this .routers = null ;
353+ return ;
354+ }
355+
356+ Long offeringId = networkOfferingDao .findById (guestNetwork .getNetworkOfferingId ()).getServiceOfferingId ();
357+ if (offeringId == null ) {
358+ offeringId = getOfferingId ();
359+ }
360+
361+ PublicIp sourceNatIp = null ;
362+ if (publicNetwork ) {
363+ sourceNatIp = ipAddrMgr .assignSourceNatIpAddressToGuestNetwork (
364+ this .owner , guestNetwork );
365+ }
366+
367+ // 3) deploy virtual router(s)
368+ final int count = routerCount - this .routers .size ();
369+ for (int i = 0 ; i < count ; i ++) {
370+ LinkedHashMap <Network , List <? extends NicProfile >> networks =
371+ createRouterNetworks (new Pair <Boolean , PublicIp >(
372+ publicNetwork , sourceNatIp ));
373+ //don't start the router as we are holding the network lock that needs to be released at the end of router allocation
374+ DomainRouterVO router = nwHelper .deployRouter (this , vrProvider , offeringId , networks , false , null );
375+
376+ if (router != null ) {
377+ routerDao .addRouterToGuestNetwork (router , guestNetwork );
378+ this .routers .add (router );
379+ }
380+ }
381+ }
382+
383+ @ DB
384+ protected void findOrDeployVirtualRouterOLD ()
385+ throws ConcurrentOperationException , InsufficientCapacityException , ResourceUnavailableException {
386+
223387 final Network lock = networkDao .acquireInLockTable (guestNetwork .getId (), NetworkOrchestrationService .NetworkLockTimeout .value ());
224388 if (lock == null ) {
225389 throw new ConcurrentOperationException ("Unable to lock network " + guestNetwork .getId ());
0 commit comments