1919package com .cloud .ha ;
2020
2121import com .cloud .agent .AgentManager ;
22- import com .cloud .agent .api .Answer ;
23- import com .cloud .agent .api .CheckOnHostCommand ;
2422import com .cloud .host .Host ;
25- import com .cloud .host .HostVO ;
2623import com .cloud .host .Status ;
2724import com .cloud .host .dao .HostDao ;
2825import com .cloud .hypervisor .Hypervisor ;
3431import org .apache .cloudstack .engine .subsystem .api .storage .DataStoreProviderManager ;
3532import org .apache .cloudstack .engine .subsystem .api .storage .PrimaryDataStoreDriver ;
3633import org .apache .cloudstack .ha .HAManager ;
34+ import org .apache .cloudstack .kvm .ha .KVMHostActivityChecker ;
3735import org .apache .cloudstack .storage .datastore .db .PrimaryDataStoreDao ;
3836import org .apache .cloudstack .storage .datastore .db .StoragePoolVO ;
3937
4038import javax .inject .Inject ;
41- import java .util .Arrays ;
39+ import java .util .Collections ;
4240import java .util .List ;
4341
4442public class KVMInvestigator extends AdapterBase implements Investigator {
@@ -54,13 +52,15 @@ public class KVMInvestigator extends AdapterBase implements Investigator {
5452 private HAManager haManager ;
5553 @ Inject
5654 private DataStoreProviderManager dataStoreProviderMgr ;
55+ @ Inject
56+ private KVMHostActivityChecker hostActivityChecker ;
5757
5858 @ Override
5959 public boolean isVmAlive (com .cloud .vm .VirtualMachine vm , Host host ) throws UnknownVM {
6060 if (haManager .isHAEligible (host )) {
6161 return haManager .isVMAliveOnHost (host );
6262 }
63- Status status = isAgentAlive (host );
63+ Status status = getHostAgentStatus (host );
6464 logger .debug ("HA: HOST is ineligible legacy state {} for host {}" , status , host );
6565 if (status == null ) {
6666 throw new UnknownVM ();
@@ -73,86 +73,41 @@ public boolean isVmAlive(com.cloud.vm.VirtualMachine vm, Host host) throws Unkno
7373 }
7474
7575 @ Override
76- public Status isAgentAlive (Host agent ) {
77- if (agent .getHypervisorType () != Hypervisor .HypervisorType .KVM && agent .getHypervisorType () != Hypervisor .HypervisorType .LXC ) {
76+ public Status getHostAgentStatus (Host host ) {
77+ if (host .getHypervisorType () != Hypervisor .HypervisorType .KVM && host .getHypervisorType () != Hypervisor .HypervisorType .LXC ) {
7878 return null ;
7979 }
8080
81- if (haManager .isHAEligible (agent )) {
82- return haManager .getHostStatus ( agent );
81+ if (haManager .isHAEligible (host )) {
82+ return haManager .getHostStatusFromHAConfig ( host );
8383 }
8484
85- List <StoragePoolVO > clusterPools = _storagePoolDao .findPoolsInClusters (Arrays . asList ( agent .getClusterId ()), null );
86- boolean storageSupportHA = storageSupportHa (clusterPools );
87- if (!storageSupportHA ) {
88- List <StoragePoolVO > zonePools = _storagePoolDao .findZoneWideStoragePoolsByHypervisor (agent .getDataCenterId (), agent .getHypervisorType ());
89- storageSupportHA = storageSupportHa (zonePools );
85+ List <StoragePoolVO > clusterPools = _storagePoolDao .findPoolsInClusters (Collections . singletonList ( host .getClusterId ()), null );
86+ boolean storageSupportsHA = storageSupportsHA (clusterPools );
87+ if (!storageSupportsHA ) {
88+ List <StoragePoolVO > zonePools = _storagePoolDao .findZoneWideStoragePoolsByHypervisor (host .getDataCenterId (), host .getHypervisorType ());
89+ storageSupportsHA = storageSupportsHA (zonePools );
9090 }
91- if (!storageSupportHA ) {
92- logger .warn ("Agent investigation was requested on host {}, but host does not support investigation because it has no NFS storage. Skipping investigation." , agent );
91+ if (!storageSupportsHA ) {
92+ logger .warn ("Agent investigation was requested on host {}, but host does not support investigation" +
93+ " because it has no HA supported storage. Skipping investigation." , host );
9394 return null ;
9495 }
9596
96- Status hostStatus = null ;
97- Status neighbourStatus = null ;
98- boolean reportFailureIfOneStorageIsDown = HighAvailabilityManager .KvmHAFenceHostIfHeartbeatFailsOnStorage .value ();
99- CheckOnHostCommand cmd = new CheckOnHostCommand (agent , reportFailureIfOneStorageIsDown );
100-
101- try {
102- Answer answer = _agentMgr .easySend (agent .getId (), cmd );
103- if (answer != null ) {
104- hostStatus = answer .getResult () ? Status .Down : Status .Up ;
105- }
106- } catch (Exception e ) {
107- logger .debug ("Failed to send command to host: {}" , agent );
108- }
109- if (hostStatus == null ) {
110- hostStatus = Status .Disconnected ;
111- }
112-
113- List <HostVO > neighbors = _resourceMgr .listHostsInClusterByStatus (agent .getClusterId (), Status .Up );
114- for (HostVO neighbor : neighbors ) {
115- if (neighbor .getId () == agent .getId ()
116- || (neighbor .getHypervisorType () != Hypervisor .HypervisorType .KVM && neighbor .getHypervisorType () != Hypervisor .HypervisorType .LXC )) {
117- continue ;
118- }
119- logger .debug ("Investigating host:{} via neighbouring host:{}" , agent , neighbor );
120- try {
121- Answer answer = _agentMgr .easySend (neighbor .getId (), cmd );
122- if (answer != null ) {
123- neighbourStatus = answer .getResult () ? Status .Down : Status .Up ;
124- logger .debug ("Neighbouring host:{} returned status:{} for the investigated host:{}" , neighbor , neighbourStatus , agent );
125- if (neighbourStatus == Status .Up ) {
126- break ;
127- }
128- }
129- } catch (Exception e ) {
130- logger .debug ("Failed to send command to host: {}" , neighbor );
131- }
132- }
133- if (neighbourStatus == Status .Up && (hostStatus == Status .Disconnected || hostStatus == Status .Down )) {
134- hostStatus = Status .Disconnected ;
135- }
136- if (neighbourStatus == Status .Down && (hostStatus == Status .Disconnected || hostStatus == Status .Down )) {
137- hostStatus = Status .Down ;
138- }
139- logger .debug ("HA: HOST is ineligible legacy state {} for host {}" , hostStatus , agent );
140- return hostStatus ;
97+ return hostActivityChecker .getHostAgentStatus (host );
14198 }
14299
143- private boolean storageSupportHa (List <StoragePoolVO > pools ) {
144- boolean storageSupportHA = false ;
100+ private boolean storageSupportsHA (List <StoragePoolVO > pools ) {
145101 for (StoragePoolVO pool : pools ) {
146102 DataStoreProvider storeProvider = dataStoreProviderMgr .getDataStoreProvider (pool .getStorageProviderName ());
147103 DataStoreDriver storeDriver = storeProvider .getDataStoreDriver ();
148104 if (storeDriver instanceof PrimaryDataStoreDriver ) {
149105 PrimaryDataStoreDriver primaryStoreDriver = (PrimaryDataStoreDriver )storeDriver ;
150106 if (primaryStoreDriver .isStorageSupportHA (pool .getPoolType ())) {
151- storageSupportHA = true ;
152- break ;
107+ return true ;
153108 }
154109 }
155110 }
156- return storageSupportHA ;
111+ return false ;
157112 }
158113}
0 commit comments