1717package org .apache .cloudstack .service ;
1818
1919import com .cloud .network .Network ;
20+ import com .cloud .network .nsx .NsxService ;
2021import com .cloud .utils .exception .CloudRuntimeException ;
2122import com .vmware .nsx .cluster .Status ;
2223import 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 ) {
0 commit comments