001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.master; 019 020import java.util.ArrayList; 021import java.util.List; 022import org.apache.hadoop.hbase.HBaseClassTestRule; 023import org.apache.hadoop.hbase.ServerName; 024import org.apache.hadoop.hbase.testclassification.MasterTests; 025import org.apache.hadoop.hbase.testclassification.SmallTests; 026import org.apache.hadoop.hbase.util.EnvironmentEdgeManager; 027import org.apache.hadoop.hbase.util.ManualEnvironmentEdge; 028import org.apache.hadoop.hbase.util.Pair; 029import org.junit.Assert; 030import org.junit.Before; 031import org.junit.ClassRule; 032import org.junit.Test; 033import org.junit.experimental.categories.Category; 034 035@Category({ MasterTests.class, SmallTests.class }) // Plays with the ManualEnvironmentEdge 036public class TestClusterStatusPublisher { 037 038 @ClassRule 039 public static final HBaseClassTestRule CLASS_RULE = 040 HBaseClassTestRule.forClass(TestClusterStatusPublisher.class); 041 042 private ManualEnvironmentEdge mee = new ManualEnvironmentEdge(); 043 044 @Before 045 public void before() { 046 mee.setValue(0); 047 EnvironmentEdgeManager.injectEdge(mee); 048 } 049 050 @Test 051 public void testEmpty() { 052 ClusterStatusPublisher csp = new ClusterStatusPublisher() { 053 @Override 054 protected List<Pair<ServerName, Long>> getDeadServers(long since) { 055 return new ArrayList<>(); 056 } 057 }; 058 059 Assert.assertTrue(csp.generateDeadServersListToSend().isEmpty()); 060 } 061 062 @Test 063 public void testMaxSend() { 064 ClusterStatusPublisher csp = new ClusterStatusPublisher() { 065 @SuppressWarnings("MissingDefault") 066 @Override 067 protected List<Pair<ServerName, Long>> getDeadServers(long since) { 068 List<Pair<ServerName, Long>> res = new ArrayList<>(); 069 switch ((int) EnvironmentEdgeManager.currentTime()) { 070 case 2: 071 res.add(new Pair<>(ServerName.valueOf("hn", 10, 10), 1L)); 072 break; 073 case 1000: 074 break; 075 } 076 077 return res; 078 } 079 }; 080 081 mee.setValue(2); 082 for (int i = 0; i < ClusterStatusPublisher.NB_SEND; i++) { 083 Assert.assertEquals("i=" + i, 1, csp.generateDeadServersListToSend().size()); 084 } 085 mee.setValue(1000); 086 Assert.assertTrue(csp.generateDeadServersListToSend().isEmpty()); 087 } 088 089 @Test 090 public void testOrder() { 091 ClusterStatusPublisher csp = new ClusterStatusPublisher() { 092 @Override 093 protected List<Pair<ServerName, Long>> getDeadServers(long since) { 094 List<Pair<ServerName, Long>> res = new ArrayList<>(); 095 for (int i = 0; i < 25; i++) { 096 res.add(new Pair<>(ServerName.valueOf("hn" + i, 10, 10), 20L)); 097 } 098 099 return res; 100 } 101 }; 102 103 mee.setValue(3); 104 List<ServerName> allSNS = csp.generateDeadServersListToSend(); 105 106 Assert.assertEquals(10, ClusterStatusPublisher.MAX_SERVER_PER_MESSAGE); 107 Assert.assertEquals(10, allSNS.size()); 108 109 List<ServerName> nextMes = csp.generateDeadServersListToSend(); 110 Assert.assertEquals(10, nextMes.size()); 111 for (ServerName sn : nextMes) { 112 if (!allSNS.contains(sn)) { 113 allSNS.add(sn); 114 } 115 } 116 Assert.assertEquals(20, allSNS.size()); 117 118 nextMes = csp.generateDeadServersListToSend(); 119 Assert.assertEquals(10, nextMes.size()); 120 for (ServerName sn : nextMes) { 121 if (!allSNS.contains(sn)) { 122 allSNS.add(sn); 123 } 124 } 125 Assert.assertEquals(25, allSNS.size()); 126 127 nextMes = csp.generateDeadServersListToSend(); 128 Assert.assertEquals(10, nextMes.size()); 129 for (ServerName sn : nextMes) { 130 if (!allSNS.contains(sn)) { 131 allSNS.add(sn); 132 } 133 } 134 Assert.assertEquals(25, allSNS.size()); 135 } 136}