Skip to content

Commit 6ebd02e

Browse files
DaanHooglandandrijapanicsb
authored andcommitted
Agent lb on svm (#3795)
* add host if needed * allow for test with null-host * full coverage of states * EnumSet.contains instead of Arrays.binarySearch
1 parent 54cc73a commit 6ebd02e

1 file changed

Lines changed: 54 additions & 8 deletions

File tree

server/src/main/java/org/apache/cloudstack/agent/lb/IndirectAgentLBServiceImpl.java

Lines changed: 54 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.util.Arrays;
2121
import java.util.Collections;
2222
import java.util.Comparator;
23+
import java.util.EnumSet;
2324
import java.util.HashMap;
2425
import java.util.List;
2526
import java.util.Map;
@@ -88,6 +89,14 @@ public List<String> getManagementServerList(final Long hostId, final Long dcId,
8889
hostIdList = getOrderedHostIdList(dcId);
8990
}
9091

92+
// just in case we have a host in creating state make sure it is in the list:
93+
if (null != hostId && ! hostIdList.contains(hostId)) {
94+
if (LOG.isTraceEnabled()) {
95+
LOG.trace("adding requested host to host list as it does not seem to be there; " + hostId);
96+
}
97+
hostIdList.add(hostId);
98+
}
99+
91100
final org.apache.cloudstack.agent.lb.IndirectAgentLBAlgorithm algorithm = getAgentMSLBAlgorithm();
92101
final List<String> msList = Arrays.asList(msServerAddresses.replace(" ", "").split(","));
93102
return algorithm.sort(msList, hostIdList, hostId);
@@ -136,17 +145,54 @@ private List<Host> getAllAgentBasedHosts() {
136145
}
137146
final List <Host> agentBasedHosts = new ArrayList<>();
138147
for (final Host host : allHosts) {
139-
if (host == null || host.getResourceState() == ResourceState.Creating || host.getResourceState() == ResourceState.Error) {
140-
continue;
148+
conditionallyAddHost(agentBasedHosts, host);
149+
}
150+
return agentBasedHosts;
151+
}
152+
153+
private void conditionallyAddHost(List<Host> agentBasedHosts, Host host) {
154+
if (host == null) {
155+
if (LOG.isTraceEnabled()) {
156+
LOG.trace("trying to add no host to a list");
141157
}
142-
if (host.getType() == Host.Type.Routing || host.getType() == Host.Type.ConsoleProxy || host.getType() == Host.Type.SecondaryStorage || host.getType() == Host.Type.SecondaryStorageVM) {
143-
if (host.getHypervisorType() != null && host.getHypervisorType() != Hypervisor.HypervisorType.KVM && host.getHypervisorType() != Hypervisor.HypervisorType.LXC) {
144-
continue;
145-
}
146-
agentBasedHosts.add(host);
158+
return;
159+
}
160+
161+
EnumSet<ResourceState> allowedStates = EnumSet.of(
162+
ResourceState.Enabled,
163+
ResourceState.Maintenance,
164+
ResourceState.Disabled,
165+
ResourceState.ErrorInMaintenance,
166+
ResourceState.PrepareForMaintenance);
167+
// so the remaining EnumSet<ResourceState> disallowedStates = EnumSet.complementOf(allowedStates)
168+
// would be {ResourceState.Creating, ResourceState.Error};
169+
if (!allowedStates.contains(host.getResourceState())) {
170+
if (LOG.isTraceEnabled()) {
171+
LOG.trace(String.format("host is in '%s' state, not adding to the host list, (id = %s)", host.getResourceState(), host.getUuid()));
147172
}
173+
return;
148174
}
149-
return agentBasedHosts;
175+
176+
if (host.getType() != Host.Type.Routing
177+
&& host.getType() != Host.Type.ConsoleProxy
178+
&& host.getType() != Host.Type.SecondaryStorage
179+
&& host.getType() != Host.Type.SecondaryStorageVM) {
180+
if (LOG.isTraceEnabled()) {
181+
LOG.trace(String.format("host is of wrong type, not adding to the host list, (id = %s, type = %s)", host.getUuid(), host.getType()));
182+
}
183+
return;
184+
}
185+
186+
if (host.getHypervisorType() != null
187+
&& ! (host.getHypervisorType() == Hypervisor.HypervisorType.KVM || host.getHypervisorType() == Hypervisor.HypervisorType.LXC)) {
188+
189+
if (LOG.isTraceEnabled()) {
190+
LOG.trace(String.format("hypervisor is not the right type, not adding to the host list, (id = %s, hypervisortype = %s)", host.getUuid(), host.getHypervisorType()));
191+
}
192+
return;
193+
}
194+
195+
agentBasedHosts.add(host);
150196
}
151197

152198
private org.apache.cloudstack.agent.lb.IndirectAgentLBAlgorithm getAgentMSLBAlgorithm() {

0 commit comments

Comments
 (0)