HBASE-23949 refactor loadBalancer implements for rsgroup balance by table to achieve...
[hbase.git] / hbase-server / src / test / java / org / apache / hadoop / hbase / master / TestClusterStatusPublisher.java
blob8ac5442bc48667ea0e002c2f9a828d05c40c935f
1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org.apache.hadoop.hbase.master;
20 import java.util.ArrayList;
21 import java.util.List;
22 import org.apache.hadoop.hbase.HBaseClassTestRule;
23 import org.apache.hadoop.hbase.ServerName;
24 import org.apache.hadoop.hbase.testclassification.MasterTests;
25 import org.apache.hadoop.hbase.testclassification.SmallTests;
26 import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
27 import org.apache.hadoop.hbase.util.ManualEnvironmentEdge;
28 import org.apache.hadoop.hbase.util.Pair;
29 import org.junit.Assert;
30 import org.junit.Before;
31 import org.junit.ClassRule;
32 import org.junit.Test;
33 import org.junit.experimental.categories.Category;
35 @Category({MasterTests.class, SmallTests.class}) // Plays with the ManualEnvironmentEdge
36 public class TestClusterStatusPublisher {
38 @ClassRule
39 public static final HBaseClassTestRule CLASS_RULE =
40 HBaseClassTestRule.forClass(TestClusterStatusPublisher.class);
42 private ManualEnvironmentEdge mee = new ManualEnvironmentEdge();
44 @Before
45 public void before() {
46 mee.setValue(0);
47 EnvironmentEdgeManager.injectEdge(mee);
50 @Test
51 public void testEmpty() {
52 ClusterStatusPublisher csp = new ClusterStatusPublisher() {
53 @Override
54 protected List<Pair<ServerName, Long>> getDeadServers(long since) {
55 return new ArrayList<>();
59 Assert.assertTrue(csp.generateDeadServersListToSend().isEmpty());
62 @Test
63 public void testMaxSend() {
64 ClusterStatusPublisher csp = new ClusterStatusPublisher() {
65 @SuppressWarnings("MissingDefault")
66 @Override
67 protected List<Pair<ServerName, Long>> getDeadServers(long since) {
68 List<Pair<ServerName, Long>> res = new ArrayList<>();
69 switch ((int) EnvironmentEdgeManager.currentTime()) {
70 case 2:
71 res.add(new Pair<>(ServerName.valueOf("hn", 10, 10), 1L));
72 break;
73 case 1000:
74 break;
77 return res;
81 mee.setValue(2);
82 for (int i = 0; i < ClusterStatusPublisher.NB_SEND; i++) {
83 Assert.assertEquals("i=" + i, 1, csp.generateDeadServersListToSend().size());
85 mee.setValue(1000);
86 Assert.assertTrue(csp.generateDeadServersListToSend().isEmpty());
89 @Test
90 public void testOrder() {
91 ClusterStatusPublisher csp = new ClusterStatusPublisher() {
92 @Override
93 protected List<Pair<ServerName, Long>> getDeadServers(long since) {
94 List<Pair<ServerName, Long>> res = new ArrayList<>();
95 for (int i = 0; i < 25; i++) {
96 res.add(new Pair<>(ServerName.valueOf("hn" + i, 10, 10), 20L));
99 return res;
104 mee.setValue(3);
105 List<ServerName> allSNS = csp.generateDeadServersListToSend();
107 Assert.assertEquals(10, ClusterStatusPublisher.MAX_SERVER_PER_MESSAGE);
108 Assert.assertEquals(10, allSNS.size());
110 List<ServerName> nextMes = csp.generateDeadServersListToSend();
111 Assert.assertEquals(10, nextMes.size());
112 for (ServerName sn : nextMes) {
113 if (!allSNS.contains(sn)) {
114 allSNS.add(sn);
117 Assert.assertEquals(20, allSNS.size());
119 nextMes = csp.generateDeadServersListToSend();
120 Assert.assertEquals(10, nextMes.size());
121 for (ServerName sn : nextMes) {
122 if (!allSNS.contains(sn)) {
123 allSNS.add(sn);
126 Assert.assertEquals(25, allSNS.size());
128 nextMes = csp.generateDeadServersListToSend();
129 Assert.assertEquals(10, nextMes.size());
130 for (ServerName sn : nextMes) {
131 if (!allSNS.contains(sn)) {
132 allSNS.add(sn);
135 Assert.assertEquals(25, allSNS.size());