Skip to content

Commit 89aa0a5

Browse files
nvazquezPearl1594
authored andcommitted
Externalize retries and inverval for NSX segment deletion (#67)
1 parent 459c23b commit 89aa0a5

File tree

3 files changed

+40
-13
lines changed

3 files changed

+40
-13
lines changed

api/src/main/java/com/cloud/network/nsx/NsxService.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,19 @@
1818

1919
import com.cloud.network.IpAddress;
2020
import com.cloud.network.vpc.Vpc;
21+
import org.apache.cloudstack.framework.config.ConfigKey;
2122

2223
public interface NsxService {
2324

25+
ConfigKey<Integer> NSX_API_FAILURE_RETRIES = new ConfigKey<>("Advanced", Integer.class,
26+
"nsx.api.failure.retries", "30",
27+
"Number of retries for NSX API operations in case of failures",
28+
true, ConfigKey.Scope.Zone);
29+
ConfigKey<Integer> NSX_API_FAILURE_INTERVAL = new ConfigKey<>("Advanced", Integer.class,
30+
"nsx.api.failure.interval", "60",
31+
"Waiting time (in seconds) before retrying an NSX API operation in case of failure",
32+
true, ConfigKey.Scope.Zone);
33+
2434
boolean createVpcNetwork(Long zoneId, long accountId, long domainId, Long vpcId, String vpcName, boolean sourceNatEnabled);
2535
boolean updateVpcSourceNatIp(Vpc vpc, IpAddress address);
2636
}

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/service/NsxApiClient.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.apache.cloudstack.service;
1818

1919
import com.cloud.network.Network;
20+
import com.cloud.network.nsx.NsxService;
2021
import com.cloud.utils.exception.CloudRuntimeException;
2122
import com.vmware.nsx.cluster.Status;
2223
import com.vmware.nsx.model.ClusterStatus;
@@ -460,7 +461,7 @@ public void deleteSegment(long zoneId, long domainId, long accountId, Long vpcId
460461
String t1GatewayName = getTier1GatewayName(domainId, accountId, zoneId, networkId, false);
461462
deleteLoadBalancer(getLoadBalancerName(t1GatewayName));
462463
}
463-
removeSegment(segmentName);
464+
removeSegment(segmentName, zoneId);
464465
DhcpRelayConfigs dhcpRelayConfig = (DhcpRelayConfigs) nsxService.apply(DhcpRelayConfigs.class);
465466
String dhcpRelayConfigId = NsxControllerUtils.getNsxDhcpRelayConfigId(zoneId, domainId, accountId, vpcId, networkId);
466467
logger.debug(String.format("Removing the DHCP relay config with ID %s", dhcpRelayConfigId));
@@ -473,7 +474,8 @@ public void deleteSegment(long zoneId, long domainId, long accountId, Long vpcId
473474
}
474475
}
475476

476-
protected void removeSegment(String segmentName) {
477+
478+
protected void removeSegment(String segmentName, long zoneId) {
477479
logger.debug(String.format("Removing the segment with ID %s", segmentName));
478480
Segments segmentService = (Segments) nsxService.apply(Segments.class);
479481
String errMsg = String.format("The segment with ID %s is not found, skipping removal", segmentName);
@@ -492,15 +494,16 @@ protected void removeSegment(String segmentName) {
492494
SegmentPorts segmentPortsService = (SegmentPorts) nsxService.apply(SegmentPorts.class);
493495
PolicyGroupMembersListResult segmentPortsList = getSegmentPortList(segmentPortsService, segmentName, enforcementPointPath);
494496
Long portCount = segmentPortsList.getResultCount();
495-
portCount = retrySegmentDeletion(segmentPortsService, portCount, segmentName, enforcementPointPath);
496-
logger.info("Port count: " + portCount);
497+
if (portCount > 0L) {
498+
portCount = retrySegmentDeletion(segmentPortsService, segmentName, enforcementPointPath, zoneId);
499+
}
497500
if (portCount == 0L) {
498501
logger.debug(String.format("Removing the segment with ID %s", segmentName));
499502
removeGroupForSegment(segmentName);
500503
segmentService.delete(segmentName);
501504
} else {
502505
String msg = String.format("Cannot remove the NSX segment %s because there are still %s port group(s) attached to it", segmentName, portCount);
503-
logger.debug(msg);
506+
logger.error(msg);
504507
throw new CloudRuntimeException(msg);
505508
}
506509
}
@@ -510,13 +513,16 @@ private PolicyGroupMembersListResult getSegmentPortList(SegmentPorts segmentPort
510513
false, null, 50L, false, null);
511514
}
512515

513-
private Long retrySegmentDeletion(SegmentPorts segmentPortsService, Long portCount, String segmentName, String enforcementPointPath) {
514-
int retries = 20;
516+
private Long retrySegmentDeletion(SegmentPorts segmentPortsService, String segmentName, String enforcementPointPath, long zoneId) {
517+
int retries = NsxService.NSX_API_FAILURE_RETRIES.valueIn(zoneId);
518+
int waitingSecs = NsxService.NSX_API_FAILURE_INTERVAL.valueIn(zoneId);
515519
int count = 1;
520+
Long portCount;
516521
do {
517522
try {
518-
logger.info("Waiting for all port groups to be unlinked from the segment - Attempt: " + count++ + " Waiting for 5 secs");
519-
Thread.sleep(5000);
523+
logger.info(String.format("Waiting for all port groups to be unlinked from the segment %s - " +
524+
"Attempt: %s. Waiting for %s secs", segmentName, count++, waitingSecs));
525+
Thread.sleep(waitingSecs * 1000L);
520526
portCount = getSegmentPortList(segmentPortsService, segmentName, enforcementPointPath).getResultCount();
521527
retries--;
522528
} catch (InterruptedException e) {

plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/service/NsxServiceImpl.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.cloud.network.IpAddress;
2020
import com.cloud.network.Network;
2121
import com.cloud.network.nsx.NsxService;
22-
import com.cloud.network.dao.NetworkDao;
2322
import com.cloud.network.dao.NetworkVO;
2423
import com.cloud.network.vpc.Vpc;
2524
import com.cloud.network.vpc.VpcVO;
@@ -37,6 +36,8 @@
3736
import org.apache.cloudstack.agent.api.DeleteNsxSegmentCommand;
3837
import org.apache.cloudstack.agent.api.DeleteNsxNatRuleCommand;
3938
import org.apache.cloudstack.agent.api.DeleteNsxTier1GatewayCommand;
39+
import org.apache.cloudstack.framework.config.ConfigKey;
40+
import org.apache.cloudstack.framework.config.Configurable;
4041
import org.apache.cloudstack.resource.NsxNetworkRule;
4142
import org.apache.cloudstack.utils.NsxControllerUtils;
4243
import org.apache.cloudstack.utils.NsxHelper;
@@ -47,13 +48,11 @@
4748
import java.util.List;
4849
import java.util.Objects;
4950

50-
public class NsxServiceImpl implements NsxService {
51+
public class NsxServiceImpl implements NsxService, Configurable {
5152
@Inject
5253
NsxControllerUtils nsxControllerUtils;
5354
@Inject
5455
VpcDao vpcDao;
55-
@Inject
56-
NetworkDao networkDao;
5756

5857
protected Logger logger = LogManager.getLogger(getClass());
5958

@@ -189,4 +188,16 @@ public boolean deleteFirewallRules(Network network, List<NsxNetworkRule> netRule
189188
NsxAnswer result = nsxControllerUtils.sendNsxCommand(command, network.getDataCenterId());
190189
return result.getResult();
191190
}
191+
192+
@Override
193+
public String getConfigComponentName() {
194+
return NsxApiClient.class.getSimpleName();
195+
}
196+
197+
@Override
198+
public ConfigKey<?>[] getConfigKeys() {
199+
return new ConfigKey<?>[] {
200+
NSX_API_FAILURE_RETRIES, NSX_API_FAILURE_INTERVAL
201+
};
202+
}
192203
}

0 commit comments

Comments
 (0)