6464import org .apache .cloudstack .poll .BackgroundPollManager ;
6565import org .apache .cloudstack .poll .BackgroundPollTask ;
6666import org .apache .cloudstack .utils .identity .ManagementServerNode ;
67+ import org .apache .commons .collections .CollectionUtils ;
6768import org .apache .commons .lang3 .StringUtils ;
6869
6970import com .cloud .host .HostVO ;
9091import com .cloud .vm .dao .VMInstanceDao ;
9192import com .cloud .vm .UserVmService ;
9293import com .cloud .org .Cluster ;
94+ import com .cloud .resource .ResourceManager ;
9395import com .cloud .resource .ResourceService ;
9496import com .cloud .storage .DiskOfferingVO ;
9597import com .cloud .storage .VolumeVO ;
108110import com .google .common .base .Preconditions ;
109111import org .apache .cloudstack .api .ResponseGenerator ;
110112import com .cloud .agent .AgentManager ;
113+ import com .cloud .agent .api .UpdateHaStateCommand ;
111114import com .cloud .api .query .vo .UserVmJoinVO ;
112115import com .cloud .api .query .dao .UserVmJoinDao ;
113116
@@ -152,6 +155,9 @@ public final class HAManagerImpl extends ManagerBase implements HAManager, Clust
152155 @ Inject
153156 private DiskOfferingDao diskOfferingDao ;
154157
158+ @ Inject
159+ private ResourceManager resourceManager ;
160+
155161 private List <HAProvider <HAResource >> haProviders ;
156162 private Map <String , HAProvider <HAResource >> haProviderMap = new HashMap <>();
157163
@@ -466,10 +472,14 @@ public boolean disableHA(final Long resourceId, final HAResource.ResourceType re
466472 public boolean enableHA (final Cluster cluster , Boolean includeHost ) {
467473 clusterDetailsDao .persist (cluster .getId (), HA_ENABLED_DETAIL , String .valueOf (true ));
468474
475+ List <? extends HAResource > hosts = hostDao .findHypervisorHostInCluster (cluster .getId ());
476+ if (CollectionUtils .isNotEmpty (hosts )) {
477+ UpdateHaStateCommand cmd = new UpdateHaStateCommand ("enable" );
478+ _agentMgr .easySend (hosts .get (0 ).getId (), cmd );
479+ }
469480 //host enableHA
470481 if (includeHost ) {
471- List <? extends HAResource > resources = hostDao .findByClusterId (cluster .getId ());
472- for (HAResource resource : resources ) {
482+ for (HAResource resource : hosts ) {
473483 final HAConfig haConfig = haConfigDao .findHAResource (resource .getId (), resource .resourceType ());
474484 if (haConfig == null ) {
475485 boolean configureHA = configureHA (resource .getId (), resource .resourceType (), true , "kvmhaprovider" );
@@ -486,10 +496,14 @@ public boolean enableHA(final Cluster cluster, Boolean includeHost) {
486496 public boolean disableHA (final Cluster cluster , Boolean includeHost ) {
487497 clusterDetailsDao .persist (cluster .getId (), HA_ENABLED_DETAIL , String .valueOf (false ));
488498
499+ List <? extends HAResource > hosts = hostDao .findHypervisorHostInCluster (cluster .getId ());
500+ if (CollectionUtils .isNotEmpty (hosts )) {
501+ UpdateHaStateCommand cmd = new UpdateHaStateCommand ("disable" );
502+ _agentMgr .easySend (hosts .get (0 ).getId (), cmd );
503+ }
489504 //host disableHA
490505 if (includeHost ) {
491- List <? extends HAResource > resources = hostDao .findByClusterId (cluster .getId ());
492- for (HAResource resource : resources ) {
506+ for (HAResource resource : hosts ) {
493507 final HAConfig haConfig = haConfigDao .findHAResource (resource .getId (), resource .resourceType ());
494508 if (haConfig != null && haConfig .isEnabled ()) {
495509 boolean result = disableHA (resource .getId (), resource .resourceType ());
0 commit comments