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.client; 019 020import static org.junit.Assert.assertArrayEquals; 021import static org.junit.Assert.assertEquals; 022 023import java.io.IOException; 024import java.util.Collections; 025import java.util.List; 026import org.apache.hadoop.hbase.HBaseTestingUtil; 027import org.apache.hadoop.hbase.HConstants; 028import org.apache.hadoop.hbase.HRegionLocation; 029import org.apache.hadoop.hbase.ServerName; 030import org.apache.hadoop.hbase.TableName; 031import org.apache.hadoop.hbase.regionserver.Region; 032import org.apache.hadoop.hbase.security.User; 033import org.apache.hadoop.hbase.util.Bytes; 034import org.apache.hadoop.hbase.util.Pair; 035import org.junit.After; 036import org.junit.Test; 037 038public abstract class AbstractTestRegionLocator { 039 040 protected static final HBaseTestingUtil UTIL = new HBaseTestingUtil(); 041 042 protected static TableName TABLE_NAME = TableName.valueOf("Locator"); 043 044 protected static byte[] FAMILY = Bytes.toBytes("family"); 045 046 protected static int REGION_REPLICATION = 3; 047 048 protected static byte[][] SPLIT_KEYS; 049 050 protected static void startClusterAndCreateTable() throws Exception { 051 UTIL.startMiniCluster(3); 052 HBaseTestingUtil.setReplicas(UTIL.getAdmin(), TableName.META_TABLE_NAME, REGION_REPLICATION); 053 TableDescriptor td = 054 TableDescriptorBuilder.newBuilder(TABLE_NAME).setRegionReplication(REGION_REPLICATION) 055 .setColumnFamily(ColumnFamilyDescriptorBuilder.of(FAMILY)).build(); 056 SPLIT_KEYS = new byte[9][]; 057 for (int i = 0; i < 9; i++) { 058 SPLIT_KEYS[i] = Bytes.toBytes(Integer.toString(i + 1)); 059 } 060 UTIL.getAdmin().createTable(td, SPLIT_KEYS); 061 UTIL.waitTableAvailable(TABLE_NAME); 062 try (ConnectionRegistry registry = 063 ConnectionRegistryFactory.create(UTIL.getConfiguration(), User.getCurrent())) { 064 RegionReplicaTestHelper.waitUntilAllMetaReplicasAreReady(UTIL, registry); 065 } 066 UTIL.getAdmin().balancerSwitch(false, true); 067 } 068 069 @After 070 public void tearDownAfterTest() throws IOException { 071 clearCache(TABLE_NAME); 072 clearCache(TableName.META_TABLE_NAME); 073 } 074 075 private byte[] getStartKey(int index) { 076 return index == 0 ? HConstants.EMPTY_START_ROW : SPLIT_KEYS[index - 1]; 077 } 078 079 private byte[] getEndKey(int index) { 080 return index == SPLIT_KEYS.length ? HConstants.EMPTY_END_ROW : SPLIT_KEYS[index]; 081 } 082 083 private void assertStartKeys(byte[][] startKeys) { 084 assertEquals(SPLIT_KEYS.length + 1, startKeys.length); 085 for (int i = 0; i < startKeys.length; i++) { 086 assertArrayEquals(getStartKey(i), startKeys[i]); 087 } 088 } 089 090 private void assertEndKeys(byte[][] endKeys) { 091 assertEquals(SPLIT_KEYS.length + 1, endKeys.length); 092 for (int i = 0; i < endKeys.length; i++) { 093 assertArrayEquals(getEndKey(i), endKeys[i]); 094 } 095 } 096 097 @Test 098 public void testStartEndKeys() throws IOException { 099 assertStartKeys(getStartKeys(TABLE_NAME)); 100 assertEndKeys(getEndKeys(TABLE_NAME)); 101 Pair<byte[][], byte[][]> startEndKeys = getStartEndKeys(TABLE_NAME); 102 assertStartKeys(startEndKeys.getFirst()); 103 assertEndKeys(startEndKeys.getSecond()); 104 } 105 106 private void assertRegionLocation(HRegionLocation loc, int index, int replicaId) { 107 RegionInfo region = loc.getRegion(); 108 byte[] startKey = getStartKey(index); 109 assertArrayEquals(startKey, region.getStartKey()); 110 assertArrayEquals(getEndKey(index), region.getEndKey()); 111 assertEquals(replicaId, region.getReplicaId()); 112 ServerName expected = findRegionLocation(TABLE_NAME, region.getStartKey(), replicaId); 113 assertEquals(expected, loc.getServerName()); 114 } 115 116 private ServerName findRegionLocation(TableName tableName, byte[] startKey, int replicaId) { 117 return UTIL.getMiniHBaseCluster().getRegionServerThreads().stream() 118 .map(t -> t.getRegionServer()) 119 .filter(rs -> rs.getRegions(tableName).stream().map(Region::getRegionInfo) 120 .anyMatch(r -> r.containsRow(startKey) && r.getReplicaId() == replicaId)) 121 .findFirst().get().getServerName(); 122 } 123 124 @Test 125 public void testGetRegionLocation() throws IOException { 126 for (int i = 0; i <= SPLIT_KEYS.length; i++) { 127 for (int replicaId = 0; replicaId < REGION_REPLICATION; replicaId++) { 128 assertRegionLocation(getRegionLocation(TABLE_NAME, getStartKey(i), replicaId), i, 129 replicaId); 130 } 131 } 132 } 133 134 @Test 135 public void testGetRegionLocations() throws IOException { 136 for (int i = 0; i <= SPLIT_KEYS.length; i++) { 137 List<HRegionLocation> locs = getRegionLocations(TABLE_NAME, getStartKey(i)); 138 assertEquals(REGION_REPLICATION, locs.size()); 139 for (int replicaId = 0; replicaId < REGION_REPLICATION; replicaId++) { 140 assertRegionLocation(locs.get(replicaId), i, replicaId); 141 } 142 } 143 } 144 145 @Test 146 public void testGetAllRegionLocations() throws IOException { 147 List<HRegionLocation> locs = getAllRegionLocations(TABLE_NAME); 148 assertEquals(REGION_REPLICATION * (SPLIT_KEYS.length + 1), locs.size()); 149 Collections.sort(locs, (l1, l2) -> { 150 int c = Bytes.compareTo(l1.getRegion().getStartKey(), l2.getRegion().getStartKey()); 151 if (c != 0) { 152 return c; 153 } 154 return Integer.compare(l1.getRegion().getReplicaId(), l2.getRegion().getReplicaId()); 155 }); 156 for (int i = 0; i <= SPLIT_KEYS.length; i++) { 157 for (int replicaId = 0; replicaId < REGION_REPLICATION; replicaId++) { 158 assertRegionLocation(locs.get(i * REGION_REPLICATION + replicaId), i, replicaId); 159 } 160 } 161 } 162 163 private void assertMetaStartOrEndKeys(byte[][] keys) { 164 assertEquals(1, keys.length); 165 assertArrayEquals(HConstants.EMPTY_BYTE_ARRAY, keys[0]); 166 } 167 168 private void assertMetaRegionLocation(HRegionLocation loc, int replicaId) { 169 RegionInfo region = loc.getRegion(); 170 assertArrayEquals(HConstants.EMPTY_START_ROW, region.getStartKey()); 171 assertArrayEquals(HConstants.EMPTY_END_ROW, region.getEndKey()); 172 assertEquals(replicaId, region.getReplicaId()); 173 ServerName expected = 174 findRegionLocation(TableName.META_TABLE_NAME, region.getStartKey(), replicaId); 175 assertEquals(expected, loc.getServerName()); 176 } 177 178 private void assertMetaRegionLocations(List<HRegionLocation> locs) { 179 assertEquals(REGION_REPLICATION, locs.size()); 180 for (int replicaId = 0; replicaId < REGION_REPLICATION; replicaId++) { 181 assertMetaRegionLocation(locs.get(replicaId), replicaId); 182 } 183 } 184 185 @Test 186 public void testMeta() throws IOException { 187 assertMetaStartOrEndKeys(getStartKeys(TableName.META_TABLE_NAME)); 188 assertMetaStartOrEndKeys(getEndKeys(TableName.META_TABLE_NAME)); 189 Pair<byte[][], byte[][]> startEndKeys = getStartEndKeys(TableName.META_TABLE_NAME); 190 assertMetaStartOrEndKeys(startEndKeys.getFirst()); 191 assertMetaStartOrEndKeys(startEndKeys.getSecond()); 192 for (int replicaId = 0; replicaId < REGION_REPLICATION; replicaId++) { 193 assertMetaRegionLocation( 194 getRegionLocation(TableName.META_TABLE_NAME, HConstants.EMPTY_START_ROW, replicaId), 195 replicaId); 196 } 197 assertMetaRegionLocations( 198 getRegionLocations(TableName.META_TABLE_NAME, HConstants.EMPTY_START_ROW)); 199 assertMetaRegionLocations(getAllRegionLocations(TableName.META_TABLE_NAME)); 200 } 201 202 protected abstract byte[][] getStartKeys(TableName tableName) throws IOException; 203 204 protected abstract byte[][] getEndKeys(TableName tableName) throws IOException; 205 206 protected abstract Pair<byte[][], byte[][]> getStartEndKeys(TableName tableName) 207 throws IOException; 208 209 protected abstract HRegionLocation getRegionLocation(TableName tableName, byte[] row, 210 int replicaId) throws IOException; 211 212 protected abstract List<HRegionLocation> getRegionLocations(TableName tableName, byte[] row) 213 throws IOException; 214 215 protected abstract List<HRegionLocation> getAllRegionLocations(TableName tableName) 216 throws IOException; 217 218 protected abstract void clearCache(TableName tableName) throws IOException; 219}