From d392e6ad23622590364b9feb36e7b52d251c5205 Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Mon, 6 Jan 2020 12:51:56 +0100 Subject: [PATCH 1/4] add host if needed --- .../agent/lb/IndirectAgentLBServiceImpl.java | 55 ++++++++++++++++--- 1 file changed, 47 insertions(+), 8 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 c3cca0e9058f..48c0d69e343a 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 @@ -88,6 +88,14 @@ public List getManagementServerList(final Long hostId, final Long dcId, hostIdList = getOrderedHostIdList(dcId); } + // just in case we have a host in creating state make sure it is in the list: + if (! 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,17 +144,48 @@ private List getAllAgentBasedHosts() { } final List agentBasedHosts = new ArrayList<>(); for (final Host host : allHosts) { - if (host == null || host.getResourceState() == ResourceState.Creating || host.getResourceState() == ResourceState.Error) { - continue; + 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"); } - 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); + return; + } + if (host.getResourceState() == ResourceState.Creating) { + if (LOG.isTraceEnabled()) { + LOG.trace(String.format("host is in creating state, not adding to the host list, (id = %s)", host.getUuid())); } + return; } - return agentBasedHosts; + if (host.getResourceState() == ResourceState.Error) { + if (LOG.isTraceEnabled()) { + LOG.trace(String.format("host is in error state, not adding to the host list, (id = %s)", 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() { From 5808ad89bf0b41209801848890ad1714540e5532 Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Mon, 6 Jan 2020 14:33:07 +0100 Subject: [PATCH 2/4] allow for test with null-host --- .../apache/cloudstack/agent/lb/IndirectAgentLBServiceImpl.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 48c0d69e343a..458ca3c314f3 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 @@ -89,7 +89,7 @@ public List getManagementServerList(final Long hostId, final Long dcId, } // just in case we have a host in creating state make sure it is in the list: - if (! hostIdList.contains(hostId)) { + 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); } From 5735e6eaecca28258f31db93c482f927647f5024 Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Tue, 7 Jan 2020 10:38:06 +0100 Subject: [PATCH 3/4] full coverage of states --- .../agent/lb/IndirectAgentLBServiceImpl.java | 24 ++++++++++++------- 1 file changed, 16 insertions(+), 8 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 458ca3c314f3..eb79736e1591 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 @@ -49,6 +49,8 @@ import com.cloud.utils.exception.CloudRuntimeException; import com.google.common.base.Strings; +import static java.util.Arrays.binarySearch; + public class IndirectAgentLBServiceImpl extends ComponentLifecycleBase implements IndirectAgentLB, Configurable { public static final Logger LOG = Logger.getLogger(IndirectAgentLBServiceImpl.class); @@ -156,18 +158,22 @@ private void conditionallyAddHost(List agentBasedHosts, Host host) { } return; } - if (host.getResourceState() == ResourceState.Creating) { - if (LOG.isTraceEnabled()) { - LOG.trace(String.format("host is in creating state, not adding to the host list, (id = %s)", host.getUuid())); - } - return; - } - if (host.getResourceState() == ResourceState.Error) { + + ResourceState[] allowedStates = new ResourceState[]{ + ResourceState.Enabled, + ResourceState.Maintenance, + ResourceState.Disabled, + ResourceState.ErrorInMaintenance, + ResourceState.PrepareForMaintenance + }; + // so the remaining ResourceState[] disallowedStates = new ResourceState[]{ResourceState.Creating, ResourceState.Error}; + if (binarySearch(allowedStates, host.getResourceState()) < 0) { if (LOG.isTraceEnabled()) { - LOG.trace(String.format("host is in error state, not adding to the host list, (id = %s)", host.getUuid())); + 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 @@ -177,6 +183,7 @@ private void conditionallyAddHost(List agentBasedHosts, Host host) { } return; } + if (host.getHypervisorType() != null && ! (host.getHypervisorType() == Hypervisor.HypervisorType.KVM || host.getHypervisorType() == Hypervisor.HypervisorType.LXC)) { @@ -185,6 +192,7 @@ private void conditionallyAddHost(List agentBasedHosts, Host host) { } return; } + agentBasedHosts.add(host); } From 78db672ffe18ab93289152137b406ed04c1556f4 Mon Sep 17 00:00:00 2001 From: Daan Hoogland Date: Tue, 7 Jan 2020 15:12:14 +0100 Subject: [PATCH 4/4] EnumSet.contains instead of Arrays.binarySearch --- .../agent/lb/IndirectAgentLBServiceImpl.java | 13 ++++++------- 1 file changed, 6 insertions(+), 7 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 eb79736e1591..0e9b32cb5dd7 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.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; @@ -49,8 +50,6 @@ import com.cloud.utils.exception.CloudRuntimeException; import com.google.common.base.Strings; -import static java.util.Arrays.binarySearch; - public class IndirectAgentLBServiceImpl extends ComponentLifecycleBase implements IndirectAgentLB, Configurable { public static final Logger LOG = Logger.getLogger(IndirectAgentLBServiceImpl.class); @@ -159,15 +158,15 @@ private void conditionallyAddHost(List agentBasedHosts, Host host) { return; } - ResourceState[] allowedStates = new ResourceState[]{ + EnumSet allowedStates = EnumSet.of( ResourceState.Enabled, ResourceState.Maintenance, ResourceState.Disabled, ResourceState.ErrorInMaintenance, - ResourceState.PrepareForMaintenance - }; - // so the remaining ResourceState[] disallowedStates = new ResourceState[]{ResourceState.Creating, ResourceState.Error}; - if (binarySearch(allowedStates, host.getResourceState()) < 0) { + 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())); }