|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | +package com.cloud.cluster.agentlb; |
| 18 | + |
| 19 | +import static org.junit.Assert.assertEquals; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | + |
| 22 | +import java.util.ArrayList; |
| 23 | +import java.util.LinkedHashMap; |
| 24 | +import java.util.List; |
| 25 | + |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +import com.cloud.host.HostVO; |
| 29 | + |
| 30 | +public class ClusterBasedAgentLoadBalancerPlannerTest { |
| 31 | + |
| 32 | + private final ClusterBasedAgentLoadBalancerPlanner planner = new ClusterBasedAgentLoadBalancerPlanner(); |
| 33 | + |
| 34 | + private HostVO host(long clusterId) { |
| 35 | + HostVO host = new HostVO("guid-" + clusterId + "-" + System.nanoTime()); |
| 36 | + host.setClusterId(clusterId); |
| 37 | + return host; |
| 38 | + } |
| 39 | + |
| 40 | + private LinkedHashMap<Long, List<HostVO>> clusterOf(int... clusterSizes) { |
| 41 | + LinkedHashMap<Long, List<HostVO>> map = new LinkedHashMap<>(); |
| 42 | + long clusterId = 1; |
| 43 | + for (int size : clusterSizes) { |
| 44 | + List<HostVO> hosts = new ArrayList<>(); |
| 45 | + for (int i = 0; i < size; i++) { |
| 46 | + hosts.add(host(clusterId)); |
| 47 | + } |
| 48 | + map.put(clusterId, hosts); |
| 49 | + clusterId++; |
| 50 | + } |
| 51 | + return map; |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void singleOversizedClusterGivesAwayPartialHosts() { |
| 56 | + LinkedHashMap<Long, List<HostVO>> hostToClusterMap = clusterOf(2); |
| 57 | + int totalDirectHosts = 2; |
| 58 | + int avLoad = 1; |
| 59 | + int hostsToGive = totalDirectHosts - avLoad; |
| 60 | + |
| 61 | + List<HostVO> hostsToReturn = planner.selectHostsToGiveAway(hostToClusterMap, hostsToGive, totalDirectHosts); |
| 62 | + |
| 63 | + assertEquals("a lone oversized cluster must still give away hosts to satisfy the quota", 1, hostsToReturn.size()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void wholeClusterIsPreferredWhenItExactlyFitsTheQuota() { |
| 68 | + LinkedHashMap<Long, List<HostVO>> hostToClusterMap = clusterOf(2, 3); |
| 69 | + int totalDirectHosts = 5; |
| 70 | + int hostsToGive = 2; |
| 71 | + |
| 72 | + List<HostVO> hostsToReturn = planner.selectHostsToGiveAway(hostToClusterMap, hostsToGive, totalDirectHosts); |
| 73 | + |
| 74 | + assertEquals(2, hostsToReturn.size()); |
| 75 | + assertTrue("should take the whole 2-host cluster rather than split the larger one", hostToClusterMap.get(1L).containsAll(hostsToReturn)); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void smallerClusterIsPreferredOverSplittingWhenBothCanSatisfyQuota() { |
| 80 | + LinkedHashMap<Long, List<HostVO>> hostToClusterMap = clusterOf(5, 2); |
| 81 | + int totalDirectHosts = 7; |
| 82 | + int hostsToGive = 2; |
| 83 | + |
| 84 | + List<HostVO> hostsToReturn = planner.selectHostsToGiveAway(hostToClusterMap, hostsToGive, totalDirectHosts); |
| 85 | + |
| 86 | + assertEquals(2, hostsToReturn.size()); |
| 87 | + assertTrue("should skip the oversized cluster and take the smaller cluster whole", hostToClusterMap.get(2L).containsAll(hostsToReturn)); |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + public void noHostsGivenAwayWhenAlreadyUnderThreshold() { |
| 92 | + LinkedHashMap<Long, List<HostVO>> hostToClusterMap = clusterOf(1); |
| 93 | + |
| 94 | + List<HostVO> hostsToReturn = planner.selectHostsToGiveAway(hostToClusterMap, 0, 1); |
| 95 | + |
| 96 | + assertTrue(hostsToReturn.isEmpty()); |
| 97 | + } |
| 98 | +} |
0 commit comments