From 9f7361c9bb85aca5d41fe0cb9954c4f4f52f9193 Mon Sep 17 00:00:00 2001 From: Muhammet Toprak Date: Thu, 19 Nov 2026 21:57:40 +0300 Subject: [PATCH] agent-lb: order clusters by size in sortByClusterSize The comparator used by ClusterBasedAgentLoadBalancerPlanner.sortByClusterSize never returned a negative value and returned 0 when the first cluster was larger, so it broke the Comparator contract and left the clusters mostly in HashMap order. It also threw a NullPointerException when only the second cluster had no host list. Order the clusters by host count, largest first, with clusters without a host list last, as getHostsToRebalance expects. Add unit tests. Fixes #14625 --- .../ClusterBasedAgentLoadBalancerPlanner.java | 23 ++--- ...sterBasedAgentLoadBalancerPlannerTest.java | 99 +++++++++++++++++++ 2 files changed, 105 insertions(+), 17 deletions(-) create mode 100644 engine/orchestration/src/test/java/com/cloud/cluster/agentlb/ClusterBasedAgentLoadBalancerPlannerTest.java diff --git a/engine/orchestration/src/main/java/com/cloud/cluster/agentlb/ClusterBasedAgentLoadBalancerPlanner.java b/engine/orchestration/src/main/java/com/cloud/cluster/agentlb/ClusterBasedAgentLoadBalancerPlanner.java index 5b05b4df0423..1918f9606a1e 100644 --- a/engine/orchestration/src/main/java/com/cloud/cluster/agentlb/ClusterBasedAgentLoadBalancerPlanner.java +++ b/engine/orchestration/src/main/java/com/cloud/cluster/agentlb/ClusterBasedAgentLoadBalancerPlanner.java @@ -17,7 +17,6 @@ package com.cloud.cluster.agentlb; import java.util.ArrayList; -import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; @@ -122,25 +121,15 @@ public List getHostsToRebalance(ManagementServerHostVO ms, int avLoad) { return hostsToReturn; } + /** + * Orders the clusters by the number of hosts in them, largest first. Clusters without a host + * list go last. Clusters of the same size keep the order in which the map returns them. + */ public static LinkedHashMap> sortByClusterSize(final Map> hostToClusterMap) { List keys = new ArrayList(); keys.addAll(hostToClusterMap.keySet()); - Collections.sort(keys, new Comparator() { - @Override - public int compare(Long o1, Long o2) { - List v1 = hostToClusterMap.get(o1); - List v2 = hostToClusterMap.get(o2); - if (v1 == null) { - return (v2 == null) ? 0 : 1; - } - - if (v1.size() < v2.size()) { - return 1; - } else { - return 0; - } - } - }); + Comparator> largestFirst = Comparator.comparingInt((List hosts) -> hosts.size()).reversed(); + keys.sort(Comparator.comparing(hostToClusterMap::get, Comparator.nullsLast(largestFirst))); LinkedHashMap> sortedMap = new LinkedHashMap>(); for (Long key : keys) { diff --git a/engine/orchestration/src/test/java/com/cloud/cluster/agentlb/ClusterBasedAgentLoadBalancerPlannerTest.java b/engine/orchestration/src/test/java/com/cloud/cluster/agentlb/ClusterBasedAgentLoadBalancerPlannerTest.java new file mode 100644 index 000000000000..9c1ed698e3d1 --- /dev/null +++ b/engine/orchestration/src/test/java/com/cloud/cluster/agentlb/ClusterBasedAgentLoadBalancerPlannerTest.java @@ -0,0 +1,99 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// to you under the Apache License, Version 2.0 (the +// "License"); you may not use this file except in compliance +// with the License. You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, +// software distributed under the License is distributed on an +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +// KIND, either express or implied. See the License for the +// specific language governing permissions and limitations +// under the License. +package com.cloud.cluster.agentlb; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Map; + +import org.junit.Assert; +import org.junit.Test; +import org.mockito.Mockito; + +import com.cloud.host.HostVO; + +public class ClusterBasedAgentLoadBalancerPlannerTest { + + private static List hosts(int count) { + List hosts = new ArrayList<>(); + for (int i = 0; i < count; i++) { + hosts.add(Mockito.mock(HostVO.class)); + } + return hosts; + } + + private static List sortedClusterIds(Map> hostToClusterMap) { + LinkedHashMap> sorted = ClusterBasedAgentLoadBalancerPlanner.sortByClusterSize(hostToClusterMap); + return new ArrayList<>(sorted.keySet()); + } + + @Test + public void sortByClusterSizePutsLargestClusterFirst() { + Map> hostToClusterMap = new HashMap<>(); + hostToClusterMap.put(1L, hosts(1)); + hostToClusterMap.put(2L, hosts(3)); + hostToClusterMap.put(3L, hosts(2)); + + Assert.assertEquals(Arrays.asList(2L, 3L, 1L), sortedClusterIds(hostToClusterMap)); + } + + @Test + public void sortByClusterSizeOrdersManyClustersBySizeDescending() { + Map> hostToClusterMap = new HashMap<>(); + int[] sizes = {4, 1, 7, 2, 9, 3, 5, 8, 6, 10}; + for (int i = 0; i < sizes.length; i++) { + hostToClusterMap.put((long) i + 1, hosts(sizes[i])); + } + + List sortedIds = sortedClusterIds(hostToClusterMap); + + Assert.assertEquals(sizes.length, sortedIds.size()); + for (int i = 1; i < sortedIds.size(); i++) { + int previousSize = hostToClusterMap.get(sortedIds.get(i - 1)).size(); + int currentSize = hostToClusterMap.get(sortedIds.get(i)).size(); + Assert.assertTrue("clusters are not ordered by size: " + sortedIds, previousSize >= currentSize); + } + } + + @Test + public void sortByClusterSizePutsClustersWithoutHostListLast() { + Map> hostToClusterMap = new HashMap<>(); + hostToClusterMap.put(1L, null); + hostToClusterMap.put(2L, hosts(1)); + hostToClusterMap.put(3L, hosts(2)); + + Assert.assertEquals(Arrays.asList(3L, 2L, 1L), sortedClusterIds(hostToClusterMap)); + } + + @Test + public void sortByClusterSizeKeepsAllEntries() { + Map> hostToClusterMap = new HashMap<>(); + List cluster1 = hosts(2); + List cluster2 = hosts(5); + hostToClusterMap.put(1L, cluster1); + hostToClusterMap.put(2L, cluster2); + + LinkedHashMap> sorted = ClusterBasedAgentLoadBalancerPlanner.sortByClusterSize(hostToClusterMap); + + Assert.assertEquals(2, sorted.size()); + Assert.assertSame(cluster1, sorted.get(1L)); + Assert.assertSame(cluster2, sorted.get(2L)); + } +}