Skip to content

Commit 8852153

Browse files
Antonio Forniewilderrodrigues
authored andcommitted
Splitting Vpc and NonVpc deployment into phases & testing it
1 parent 325026d commit 8852153

2 files changed

Lines changed: 232 additions & 4 deletions

File tree

‎server/src/org/cloud/network/router/deployment/RouterDeploymentDefinition.java‎

Lines changed: 166 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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());

‎server/src/org/cloud/network/router/deployment/VpcRouterDeploymentDefinition.java‎

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,73 @@ public boolean isVpcRouter() {
7474
return true;
7575
}
7676

77+
@Override
78+
protected void lock() {
79+
Vpc vpcLock = vpcDao.acquireInLockTable(vpc.getId());
80+
if (vpcLock == null) {
81+
throw new ConcurrentOperationException("Unable to lock vpc " + vpc.getId());
82+
}
83+
this.tableLockId = vpcLock.getId();
84+
}
85+
86+
@Override
87+
protected void unlock() {
88+
if (this.tableLockId != null) {
89+
vpcDao.releaseFromLockTable(this.tableLockId);
90+
if (logger.isDebugEnabled()) {
91+
logger.debug("Lock is released for vpc id " + this.tableLockId
92+
+ " as a part of router startup in " + dest);
93+
}
94+
}
95+
}
96+
97+
@Override
98+
protected void checkPreconditions() {
99+
// No preconditions for Vpc
100+
}
101+
102+
@Override
103+
protected List<DeployDestination> findDestinations() {
104+
final List<DeployDestination> destinations = new ArrayList<>();
105+
destinations.add(this.dest);
106+
return destinations;
107+
}
108+
109+
@Override
110+
protected void proceedEffectiveDeployment()
111+
throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
112+
//2) Return routers if exist, otherwise...
113+
if (this.routers.size() < 1) {
114+
Long offeringId = vpcOffDao.findById(vpc.getVpcOfferingId()).getServiceOfferingId();
115+
if (offeringId == null) {
116+
offeringId = offering.getId();
117+
}
118+
//3) Deploy Virtual Router
119+
List<? extends PhysicalNetwork> pNtwks = pNtwkDao.listByZone(vpc.getZoneId());
120+
121+
VirtualRouterProvider vpcVrProvider = null;
122+
123+
for (PhysicalNetwork pNtwk : pNtwks) {
124+
PhysicalNetworkServiceProvider provider = physicalProviderDao.findByServiceProvider(pNtwk.getId(), Type.VPCVirtualRouter.toString());
125+
if (provider == null) {
126+
throw new CloudRuntimeException("Cannot find service provider " + Type.VPCVirtualRouter.toString() + " in physical network " + pNtwk.getId());
127+
}
128+
vpcVrProvider = vrProviderDao.findByNspIdAndType(provider.getId(), Type.VPCVirtualRouter);
129+
if (vpcVrProvider != null) {
130+
break;
131+
}
132+
}
133+
134+
PublicIp sourceNatIp = vpcMgr.assignSourceNatIpAddressToVpc(this.owner, vpc);
135+
136+
DomainRouterVO router = deployVpcRouter(vpcVrProvider, offeringId, sourceNatIp);
137+
this.routers.add(router);
138+
}
139+
}
140+
77141
@Override
78142
@DB
79-
protected void findOrDeployVirtualRouter()
143+
protected void findOrDeployVirtualRouterOLD()
80144
throws ConcurrentOperationException, InsufficientCapacityException, ResourceUnavailableException {
81145

82146
logger.debug("Deploying Virtual Router in VPC " + vpc);
@@ -87,7 +151,7 @@ protected void findOrDeployVirtualRouter()
87151
}
88152

89153
//1) Find out the list of routers and generate deployment plan
90-
planDeploymentRouters();
154+
this.planDeploymentRouters();
91155
this.generateDeploymentPlan();
92156

93157
//2) Return routers if exist, otherwise...

0 commit comments

Comments
 (0)