@@ -2621,7 +2621,7 @@ private void waitForAllHostsToSeeDatastore(List<Pair<ManagedObjectReference, Str
26212621 private boolean verifyAllHostsSeeDatastore (List <Pair <ManagedObjectReference , String >> lstHosts , DatastoreMO dsMO ) throws Exception {
26222622 int numHostsChecked = 0 ;
26232623
2624- for (Pair <ManagedObjectReference , String > host : lstHosts ) {
2624+ for (Pair <ManagedObjectReference , String > host : lstHosts ) {
26252625 ManagedObjectReference morHostToMatch = host .first ();
26262626 HostMO hostToMatchMO = new HostMO (dsMO .getContext (), morHostToMatch );
26272627
@@ -2657,13 +2657,40 @@ private void waitForAllHostsToMountDatastore(List<Pair<ManagedObjectReference, S
26572657 }
26582658 }
26592659
2660+ private void waitForAllHostsToMountDatastore2 (List <HostMO > lstHosts , DatastoreMO dsMO ) throws Exception {
2661+ long secondsToWait = 120 ;
2662+ long endWaitTime = System .currentTimeMillis () + secondsToWait * 1000 ;
2663+
2664+ boolean isConditionMet = false ;
2665+
2666+ while (System .currentTimeMillis () < endWaitTime && !isConditionMet ) {
2667+ Thread .sleep (5000 );
2668+
2669+ isConditionMet = verifyAllHostsMountedDatastore2 (lstHosts , dsMO );
2670+ }
2671+
2672+ if (!isConditionMet ) {
2673+ throw new CloudRuntimeException ("Not all hosts mounted the datastore" );
2674+ }
2675+ }
2676+
26602677 private boolean verifyAllHostsMountedDatastore (List <Pair <ManagedObjectReference , String >> lstHosts , DatastoreMO dsMO ) throws Exception {
2661- int numHostsChecked = 0 ;
2678+ List < HostMO > hostMOs = new ArrayList <>( lstHosts . size ()) ;
26622679
2663- for (Pair <ManagedObjectReference , String > host : lstHosts ) {
2680+ for (Pair <ManagedObjectReference , String > host : lstHosts ) {
26642681 ManagedObjectReference morHostToMatch = host .first ();
26652682 HostMO hostToMatchMO = new HostMO (dsMO .getContext (), morHostToMatch );
26662683
2684+ hostMOs .add (hostToMatchMO );
2685+ }
2686+
2687+ return verifyAllHostsMountedDatastore2 (hostMOs , dsMO );
2688+ }
2689+
2690+ private boolean verifyAllHostsMountedDatastore2 (List <HostMO > lstHosts , DatastoreMO dsMO ) throws Exception {
2691+ int numHostsChecked = 0 ;
2692+
2693+ for (HostMO hostToMatchMO : lstHosts ) {
26672694 List <DatastoreHostMount > datastoreHostMounts = dsMO .getHostMounts ();
26682695
26692696 for (DatastoreHostMount datastoreHostMount : datastoreHostMounts ) {
@@ -2753,31 +2780,62 @@ private void mountVmfsDatastore(DatastoreMO dsMO, List<Pair<ManagedObjectReferen
27532780 for (Pair <ManagedObjectReference , String > host : hosts ) {
27542781 HostMO hostMO = new HostMO (dsMO .getContext (), host .first ());
27552782
2783+ List <HostMO > hostMOs = new ArrayList <>(1 );
2784+
2785+ hostMOs .add (hostMO );
2786+
2787+ mountVmfsDatastore2 (dsMO , hostMOs );
2788+ }
2789+ }
2790+
2791+ private void mountVmfsDatastore2 (DatastoreMO dsMO , List <HostMO > hosts ) throws Exception {
2792+ for (HostMO hostMO : hosts ) {
27562793 if (!isDatastoreMounted (dsMO , hostMO )) {
27572794 HostStorageSystemMO hostStorageSystemMO = hostMO .getHostStorageSystemMO ();
27582795
27592796 try {
27602797 hostStorageSystemMO .mountVmfsVolume (getDatastoreUuid (dsMO , hostMO ));
27612798 }
27622799 catch (InvalidStateFaultMsg ex ) {
2763- List <Pair <ManagedObjectReference , String >> currentHosts = new ArrayList <>(1 );
2800+ s_logger .trace ("'" + ex .getClass ().getName () + "' exception thrown: " + ex .getMessage ());
2801+
2802+ List <HostMO > currentHosts = new ArrayList <>(1 );
2803+
2804+ currentHosts .add (hostMO );
27642805
2765- currentHosts . add ( host );
2806+ s_logger . trace ( "Waiting for host " + hostMO . getHostName () + " to mount datastore " + dsMO . getName () );
27662807
2767- waitForAllHostsToMountDatastore (currentHosts , dsMO );
2808+ waitForAllHostsToMountDatastore2 (currentHosts , dsMO );
27682809 }
27692810 }
27702811 }
27712812 }
27722813
27732814 private void unmountVmfsDatastore (VmwareContext context , VmwareHypervisorHost hyperHost , String datastoreName ,
27742815 List <Pair <ManagedObjectReference , String >> hosts ) throws Exception {
2816+ for (Pair <ManagedObjectReference , String > host : hosts ) {
2817+ HostMO hostMO = new HostMO (context , host .first ());
2818+
2819+ List <HostMO > hostMOs = new ArrayList <>(1 );
2820+
2821+ hostMOs .add (hostMO );
2822+
2823+ unmountVmfsDatastore2 (context , hyperHost , datastoreName , hostMOs );
2824+ }
2825+ }
2826+
2827+ private void unmountVmfsDatastore2 (VmwareContext context , VmwareHypervisorHost hyperHost , String datastoreName ,
2828+ List <HostMO > hosts ) throws Exception {
27752829 ManagedObjectReference morDs = HypervisorHostHelper .findDatastoreWithBackwardsCompatibility (hyperHost , datastoreName );
27762830 DatastoreMO dsMO = new DatastoreMO (context , morDs );
27772831
2778- for (Pair <ManagedObjectReference , String > host : hosts ) {
2779- HostMO hostMO = new HostMO (context , host .first ());
2832+ for (HostMO hostMO : hosts ) {
2833+ unmountVmfsVolume (dsMO , hostMO );
2834+ }
2835+ }
27802836
2837+ private void unmountVmfsVolume (DatastoreMO dsMO , HostMO hostMO ) throws Exception {
2838+ if (isDatastoreMounted (dsMO , hostMO )) {
27812839 HostStorageSystemMO hostStorageSystemMO = hostMO .getHostStorageSystemMO ();
27822840
27832841 hostStorageSystemMO .unmountVmfsVolume (getDatastoreUuid (dsMO , hostMO ));
@@ -2902,6 +2960,20 @@ public void handleTargets(boolean add, ModifyTargetsCommand.TargetTypeToRemove t
29022960
29032961 if (rescan ) {
29042962 rescanAllHosts (hosts , true , false );
2963+
2964+ List <HostInternetScsiHbaStaticTarget > targetsToAdd = new ArrayList <>();
2965+
2966+ targetsToAdd .addAll (getTargets (staticTargetsForHost ));
2967+ targetsToAdd .addAll (getTargets (dynamicTargetsForHost ));
2968+
2969+ for (HostInternetScsiHbaStaticTarget targetToAdd : targetsToAdd ) {
2970+ HostDatastoreSystemMO hostDatastoreSystemMO = host .getHostDatastoreSystemMO ();
2971+ String datastoreName = waitForDatastoreName (hostDatastoreSystemMO , targetToAdd .getIScsiName ());
2972+ ManagedObjectReference morDs = hostDatastoreSystemMO .findDatastoreByName (datastoreName );
2973+ DatastoreMO datastoreMO = new DatastoreMO (host .getContext (), morDs );
2974+
2975+ mountVmfsDatastore2 (datastoreMO , hosts );
2976+ }
29052977 }
29062978 }
29072979 catch (Exception ex ) {
@@ -2925,24 +2997,11 @@ public void handleTargets(boolean add, ModifyTargetsCommand.TargetTypeToRemove t
29252997 if (targetsToRemove .size () > 0 ) {
29262998 if (isRemoveAsync ) {
29272999 new Thread (() -> {
2928- try {
2929- addRemoveInternetScsiTargetsToAllHosts (false , targetsToRemove , hosts );
2930-
2931- rescanAllHosts (hosts , true , false );
2932- } catch (Exception ex ) {
2933- s_logger .warn (ex .getMessage ());
2934- }
3000+ handleRemove (targetsToRemove , host , hosts );
29353001 }).start ();
29363002 } else {
29373003 executorService .submit (new Thread (() -> {
2938- try {
2939- addRemoveInternetScsiTargetsToAllHosts (false , targetsToRemove , hosts );
2940-
2941- rescanAllHosts (hosts , true , false );
2942- }
2943- catch (Exception ex ) {
2944- s_logger .warn (ex .getMessage ());
2945- }
3004+ handleRemove (targetsToRemove , host , hosts );
29463005 }));
29473006 }
29483007 }
@@ -2956,6 +3015,61 @@ public void handleTargets(boolean add, ModifyTargetsCommand.TargetTypeToRemove t
29563015 }
29573016 }
29583017
3018+ private String waitForDatastoreName (HostDatastoreSystemMO hostDatastoreSystemMO , String iqn ) throws Exception {
3019+ long secondsToWait = 120 ;
3020+ long endWaitTime = System .currentTimeMillis () + secondsToWait * 1000 ;
3021+
3022+ do {
3023+ String datastoreName = getDatastoreName (hostDatastoreSystemMO , iqn );
3024+
3025+ if (datastoreName != null ) {
3026+ return datastoreName ;
3027+ }
3028+
3029+ Thread .sleep (5000 );
3030+ }
3031+ while (System .currentTimeMillis () < endWaitTime );
3032+
3033+ throw new CloudRuntimeException ("Could not find the datastore name" );
3034+ }
3035+
3036+ private String getDatastoreName (HostDatastoreSystemMO hostDatastoreSystemMO , String iqn ) throws Exception {
3037+ String datastoreName = "-" + iqn + "-0" ;
3038+
3039+ ManagedObjectReference morDs = hostDatastoreSystemMO .findDatastoreByName (datastoreName );
3040+
3041+ if (morDs != null ) {
3042+ return datastoreName ;
3043+ }
3044+
3045+ datastoreName = "_" + iqn + "_0" ;
3046+
3047+ morDs = hostDatastoreSystemMO .findDatastoreByName (datastoreName );
3048+
3049+ if (morDs != null ) {
3050+ return datastoreName ;
3051+ }
3052+
3053+ return null ;
3054+ }
3055+
3056+ private void handleRemove (List <HostInternetScsiHbaStaticTarget > targetsToRemove , HostMO host , List <HostMO > hosts ) {
3057+ try {
3058+ for (HostInternetScsiHbaStaticTarget target : targetsToRemove ) {
3059+ String datastoreName = waitForDatastoreName (host .getHostDatastoreSystemMO (), target .getIScsiName ());
3060+
3061+ unmountVmfsDatastore2 (host .getContext (), host , datastoreName , hosts );
3062+ }
3063+
3064+ addRemoveInternetScsiTargetsToAllHosts (false , targetsToRemove , hosts );
3065+
3066+ rescanAllHosts (hosts , true , false );
3067+ }
3068+ catch (Exception ex ) {
3069+ s_logger .warn (ex .getMessage ());
3070+ }
3071+ }
3072+
29593073 private void addRemoveInternetScsiTargetsToAllHosts (VmwareContext context , final boolean add , final List <HostInternetScsiHbaStaticTarget > targets ,
29603074 List <Pair <ManagedObjectReference , String >> hostPairs ) throws Exception {
29613075 List <HostMO > hosts = new ArrayList <>();
0 commit comments