From 6ebd02e7af2dfdfbd54728f367464eba2cec8ca5 Mon Sep 17 00:00:00 2001 From: dahn Date: Tue, 14 Jan 2020 15:40:21 +0100 Subject: [PATCH] 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 --- .../agent/lb/IndirectAgentLBServiceImpl.java | 64 ++++++++++++++++--- 1 file changed, 55 insertions(+), 9 deletions(-) diff --git a/server/src/main/java/org/apache/cloudstack/agent/lb/IndirectAgentLBServiceImpl.java b/server/src/main/java/org/apache/cloudstack/agent/lb/IndirectAgentLBServiceImpl.java index c3cca0e9058..0e9b32cb5dd 100644 --- a/server/src/main/java/org/apache/cloudstack/agent/lb/IndirectAgentLBServiceImpl.java +++ b/server/src/main/java/org/apache/cloudstack/agent/lb/IndirectAgentLBServiceImpl.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; +import java.util.EnumSet; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -88,6 +89,14 @@ public class IndirectAgentLBServiceImpl extends ComponentLifecycleBase implement hostIdList = getOrderedHostIdList(dcId); } + // just in case we have a host in creating state make sure it is in the list: + if (null != hostId && ! hostIdList.contains(hostId)) { + if (LOG.isTraceEnabled()) { + LOG.trace("adding requested host to host list as it does not seem to be there; " + hostId); + } + hostIdList.add(hostId); + } + final org.apache.cloudstack.agent.lb.IndirectAgentLBAlgorithm algorithm = getAgentMSLBAlgorithm(); final List msList = Arrays.asList(msServerAddresses.replace(" ", "").split(",")); return algorithm.sort(msList, hostIdList, hostId); @@ -136,19 +145,56 @@ public class IndirectAgentLBServiceImpl extends ComponentLifecycleBase implement } final List agentBasedHosts = new ArrayList<>(); for (final Host host : allHosts) { - if (host == null || host.getResourceState() == ResourceState.Creating || host.getResourceState() == ResourceState.Error) { - continue; - } - if (host.getType() == Host.Type.Routing || host.getType() == Host.Type.ConsoleProxy || host.getType() == Host.Type.SecondaryStorage || host.getType() == Host.Type.SecondaryStorageVM) { - if (host.getHypervisorType() != null && host.getHypervisorType() != Hypervisor.HypervisorType.KVM && host.getHypervisorType() != Hypervisor.HypervisorType.LXC) { - continue; - } - agentBasedHosts.add(host); - } + conditionallyAddHost(agentBasedHosts, host); } return agentBasedHosts; } + private void conditionallyAddHost(List agentBasedHosts, Host host) { + if (host == null) { + if (LOG.isTraceEnabled()) { + LOG.trace("trying to add no host to a list"); + } + return; + } + + EnumSet allowedStates = EnumSet.of( + ResourceState.Enabled, + ResourceState.Maintenance, + ResourceState.Disabled, + ResourceState.ErrorInMaintenance, + ResourceState.PrepareForMaintenance); + // so the remaining EnumSet disallowedStates = EnumSet.complementOf(allowedStates) + // would be {ResourceState.Creating, ResourceState.Error}; + if (!allowedStates.contains(host.getResourceState())) { + if (LOG.isTraceEnabled()) { + LOG.trace(String.format("host is in '%s' state, not adding to the host list, (id = %s)", host.getResourceState(), host.getUuid())); + } + return; + } + + if (host.getType() != Host.Type.Routing + && host.getType() != Host.Type.ConsoleProxy + && host.getType() != Host.Type.SecondaryStorage + && host.getType() != Host.Type.SecondaryStorageVM) { + if (LOG.isTraceEnabled()) { + LOG.trace(String.format("host is of wrong type, not adding to the host list, (id = %s, type = %s)", host.getUuid(), host.getType())); + } + return; + } + + if (host.getHypervisorType() != null + && ! (host.getHypervisorType() == Hypervisor.HypervisorType.KVM || host.getHypervisorType() == Hypervisor.HypervisorType.LXC)) { + + if (LOG.isTraceEnabled()) { + LOG.trace(String.format("hypervisor is not the right type, not adding to the host list, (id = %s, hypervisortype = %s)", host.getUuid(), host.getHypervisorType())); + } + return; + } + + agentBasedHosts.add(host); + } + private org.apache.cloudstack.agent.lb.IndirectAgentLBAlgorithm getAgentMSLBAlgorithm() { final String algorithm = getLBAlgorithmName(); if (algorithmMap.containsKey(algorithm)) {