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 static org.junit.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertNotNull; 022import static org.junit.jupiter.api.Assertions.assertTrue; 023 024import java.util.List; 025import org.apache.hadoop.hbase.HConstants; 026import org.apache.hadoop.hbase.ServerName; 027import org.apache.hadoop.hbase.SingleProcessHBaseCluster; 028import org.apache.hadoop.hbase.TableName; 029import org.apache.hadoop.hbase.client.RegionInfo; 030import org.apache.hadoop.hbase.testclassification.MasterTests; 031import org.apache.hadoop.hbase.testclassification.MediumTests; 032import org.apache.hadoop.hbase.util.JVMClusterUtil; 033import org.junit.jupiter.api.Tag; 034import org.junit.jupiter.api.Test; 035import org.slf4j.Logger; 036import org.slf4j.LoggerFactory; 037 038@Tag(MasterTests.TAG) 039@Tag(MediumTests.TAG) 040public class TestRoundRobinAssignmentOnRestart extends AbstractTestRestartCluster { 041 042 private static final Logger LOG = 043 LoggerFactory.getLogger(TestRoundRobinAssignmentOnRestart.class); 044 045 @Override 046 protected boolean splitWALCoordinatedByZk() { 047 return true; 048 } 049 050 private final int regionNum = 10; 051 private final int rsNum = 2; 052 053 /** 054 * This tests retaining assignments on a cluster restart 055 */ 056 @Test 057 public void test() throws Exception { 058 UTIL.startMiniCluster(rsNum); 059 // Turn off balancer 060 UTIL.getMiniHBaseCluster().getMaster().getMasterRpcServices().synchronousBalanceSwitch(false); 061 LOG.info("\n\nCreating tables"); 062 for (TableName TABLE : TABLES) { 063 UTIL.createMultiRegionTable(TABLE, FAMILY, regionNum); 064 } 065 // Wait until all regions are assigned 066 for (TableName TABLE : TABLES) { 067 UTIL.waitTableEnabled(TABLE); 068 } 069 UTIL.waitUntilNoRegionsInTransition(60000); 070 071 SingleProcessHBaseCluster cluster = UTIL.getHBaseCluster(); 072 List<JVMClusterUtil.RegionServerThread> threads = cluster.getLiveRegionServerThreads(); 073 assertEquals(2, threads.size()); 074 075 ServerName testServer = threads.get(0).getRegionServer().getServerName(); 076 int port = testServer.getPort(); 077 List<RegionInfo> regionInfos = 078 cluster.getMaster().getAssignmentManager().getRegionsOnServer(testServer); 079 LOG.debug("RegionServer {} has {} regions", testServer, regionInfos.size()); 080 assertTrue(regionInfos.size() >= (TABLES.length * regionNum / rsNum)); 081 082 // Restart 1 regionserver 083 cluster.stopRegionServer(testServer); 084 cluster.waitForRegionServerToStop(testServer, 60000); 085 cluster.getConf().setInt(HConstants.REGIONSERVER_PORT, port); 086 cluster.startRegionServer(); 087 088 HMaster master = UTIL.getMiniHBaseCluster().getMaster(); 089 List<ServerName> localServers = master.getServerManager().getOnlineServersList(); 090 ServerName newTestServer = null; 091 for (ServerName serverName : localServers) { 092 if (serverName.getAddress().equals(testServer.getAddress())) { 093 newTestServer = serverName; 094 break; 095 } 096 } 097 assertNotNull(newTestServer); 098 099 // Wait until all regions are assigned 100 for (TableName TABLE : TABLES) { 101 UTIL.waitTableAvailable(TABLE); 102 } 103 UTIL.waitUntilNoRegionsInTransition(60000); 104 105 List<RegionInfo> newRegionInfos = 106 cluster.getMaster().getAssignmentManager().getRegionsOnServer(newTestServer); 107 LOG.debug("RegionServer {} has {} regions", newTestServer, newRegionInfos.size()); 108 assertTrue(newRegionInfos.size() < regionInfos.size(), 109 "Should not retain all regions when restart"); 110 } 111}