Skip to content

Commit 5a4f38c

Browse files
authored
NSX: Add retry logic with sleep to delete segments (#8554)
* NSX: Add retry logic with sleep to delete segments * add logs
1 parent 80365c8 commit 5a4f38c

File tree

1 file changed

+27
-4
lines changed
  • plugins/network-elements/nsx/src/main/java/org/apache/cloudstack/service

1 file changed

+27
-4
lines changed

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,19 +453,42 @@ protected void removeSegment(String segmentName) {
453453
String siteId = getDefaultSiteId();
454454
String enforcementPointPath = getDefaultEnforcementPointPath(siteId);
455455
SegmentPorts segmentPortsService = (SegmentPorts) nsxService.apply(SegmentPorts.class);
456-
PolicyGroupMembersListResult segmentPortsList = segmentPortsService.list(DEFAULT_DOMAIN, segmentName, null, enforcementPointPath,
457-
false, null, 50L, false, null);
458-
if (segmentPortsList.getResultCount() == 0L) {
456+
PolicyGroupMembersListResult segmentPortsList = getSegmentPortList(segmentPortsService, segmentName, enforcementPointPath);
457+
Long portCount = segmentPortsList.getResultCount();
458+
portCount = retrySegmentDeletion(segmentPortsService, portCount, segmentName, enforcementPointPath);
459+
LOGGER.info("Port count: " + portCount);
460+
if (portCount == 0L) {
459461
LOGGER.debug(String.format("Removing the segment with ID %s", segmentName));
460462
removeGroupForSegment(segmentName);
461463
segmentService.delete(segmentName);
462464
} else {
463-
String msg = String.format("Cannot remove the NSX segment %s because there are still %s port group(s) attached to it", segmentName, segmentPortsList.getResultCount());
465+
String msg = String.format("Cannot remove the NSX segment %s because there are still %s port group(s) attached to it", segmentName, portCount);
464466
LOGGER.debug(msg);
465467
throw new CloudRuntimeException(msg);
466468
}
467469
}
468470

471+
private PolicyGroupMembersListResult getSegmentPortList(SegmentPorts segmentPortsService, String segmentName, String enforcementPointPath) {
472+
return segmentPortsService.list(DEFAULT_DOMAIN, segmentName, null, enforcementPointPath,
473+
false, null, 50L, false, null);
474+
}
475+
476+
private Long retrySegmentDeletion(SegmentPorts segmentPortsService, Long portCount, String segmentName, String enforcementPointPath) {
477+
int retries = 20;
478+
int count = 1;
479+
do {
480+
try {
481+
LOGGER.info("Waiting for all port groups to be unlinked from the segment - Attempt: " + count++ + " Waiting for 5 secs");
482+
Thread.sleep(5000);
483+
portCount = getSegmentPortList(segmentPortsService, segmentName, enforcementPointPath).getResultCount();
484+
retries--;
485+
} catch (InterruptedException e) {
486+
throw new CloudRuntimeException(String.format("Unable to delete segment %s due to: %s", segmentName, e.getLocalizedMessage()));
487+
}
488+
} while (retries > 0 && portCount > 0);
489+
return portCount;
490+
}
491+
469492
public void createStaticNatRule(String vpcName, String tier1GatewayName,
470493
String ruleName, String publicIp, String vmIp) {
471494
try {

0 commit comments

Comments
 (0)