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