Skip to content

Commit 672b0a5

Browse files
authored
HBASE-26649 Support meta replica LoadBalance mode for RegionLocator#getAllRegionLocations() (#4442) (#4464)
Signed-off-by: Duo Zhang <[email protected]>
1 parent f97af48 commit 672b0a5

8 files changed

Lines changed: 88 additions & 16 deletions

File tree

hbase-client/src/main/java/org/apache/hadoop/hbase/AsyncMetaTableAccessor.java

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818
package org.apache.hadoop.hbase;
1919

20+
import static org.apache.hadoop.hbase.client.RegionLocator.LOCATOR_META_REPLICAS_MODE;
2021
import static org.apache.hadoop.hbase.util.FutureUtils.addListener;
2122

2223
import java.io.IOException;
@@ -29,6 +30,7 @@
2930
import java.util.Optional;
3031
import java.util.SortedMap;
3132
import java.util.concurrent.CompletableFuture;
33+
import java.util.concurrent.ThreadLocalRandom;
3234
import java.util.regex.Matcher;
3335
import java.util.regex.Pattern;
3436
import java.util.stream.Collectors;
@@ -295,7 +297,37 @@ private static CompletableFuture<Void> scanMeta(AsyncTable<AdvancedScanResultCon
295297
}
296298

297299
CompletableFuture<Void> future = new CompletableFuture<Void>();
298-
metaTable.scan(scan, new MetaTableScanResultConsumer(rowUpperLimit, visitor, future));
300+
// Get the region locator's meta replica mode.
301+
CatalogReplicaMode metaReplicaMode = CatalogReplicaMode.fromString(metaTable.getConfiguration()
302+
.get(LOCATOR_META_REPLICAS_MODE, CatalogReplicaMode.NONE.toString()));
303+
304+
if (metaReplicaMode == CatalogReplicaMode.LOAD_BALANCE) {
305+
addListener(metaTable.getDescriptor(), (desc, error) -> {
306+
if (error != null) {
307+
LOG.error("Failed to get meta table descriptor, error: ", error);
308+
future.completeExceptionally(error);
309+
return;
310+
}
311+
312+
int numOfReplicas = desc.getRegionReplication();
313+
if (numOfReplicas > 1) {
314+
int replicaId = ThreadLocalRandom.current().nextInt(numOfReplicas);
315+
316+
// When the replicaId is 0, do not set to Consistency.TIMELINE
317+
if (replicaId > 0) {
318+
scan.setReplicaId(replicaId);
319+
scan.setConsistency(Consistency.TIMELINE);
320+
}
321+
}
322+
metaTable.scan(scan, new MetaTableScanResultConsumer(rowUpperLimit, visitor, future));
323+
});
324+
} else {
325+
if (metaReplicaMode == CatalogReplicaMode.HEDGED_READ) {
326+
scan.setConsistency(Consistency.TIMELINE);
327+
}
328+
metaTable.scan(scan, new MetaTableScanResultConsumer(rowUpperLimit, visitor, future));
329+
}
330+
299331
return future;
300332
}
301333

hbase-client/src/main/java/org/apache/hadoop/hbase/client/CatalogReplicaMode.java renamed to hbase-client/src/main/java/org/apache/hadoop/hbase/CatalogReplicaMode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* See the License for the specific language governing permissions and
1616
* limitations under the License.
1717
*/
18-
package org.apache.hadoop.hbase.client;
18+
package org.apache.hadoop.hbase;
1919

2020
import org.apache.yetus.audience.InterfaceAudience;
2121

@@ -33,7 +33,7 @@
3333
* </ol>
3434
*/
3535
@InterfaceAudience.Private
36-
enum CatalogReplicaMode {
36+
public enum CatalogReplicaMode {
3737
NONE {
3838
@Override
3939
public String toString() {

hbase-client/src/main/java/org/apache/hadoop/hbase/MetaTableAccessor.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import java.util.NavigableMap;
3434
import java.util.SortedMap;
3535
import java.util.TreeMap;
36+
import java.util.concurrent.ThreadLocalRandom;
3637
import java.util.regex.Matcher;
3738
import java.util.regex.Pattern;
3839
import java.util.stream.Collectors;
@@ -697,15 +698,21 @@ public static void fullScanMetaAndPrint(Connection connection) throws IOExceptio
697698
scanMeta(connection, null, null, QueryType.ALL, v);
698699
}
699700

701+
public static void scanMetaForTableRegions(Connection connection, Visitor visitor,
702+
TableName tableName, CatalogReplicaMode metaReplicaMode) throws IOException {
703+
scanMeta(connection, tableName, QueryType.REGION, Integer.MAX_VALUE, visitor, metaReplicaMode);
704+
}
705+
700706
public static void scanMetaForTableRegions(Connection connection, Visitor visitor,
701707
TableName tableName) throws IOException {
702-
scanMeta(connection, tableName, QueryType.REGION, Integer.MAX_VALUE, visitor);
708+
scanMetaForTableRegions(connection, visitor, tableName, CatalogReplicaMode.NONE);
703709
}
704710

705711
private static void scanMeta(Connection connection, TableName table, QueryType type, int maxRows,
706-
final Visitor visitor) throws IOException {
712+
final Visitor visitor, CatalogReplicaMode metaReplicaMode) throws IOException {
707713
scanMeta(connection, getTableStartRowForMeta(table, type), getTableStopRowForMeta(table, type),
708-
type, maxRows, visitor);
714+
type, null, maxRows, visitor, metaReplicaMode);
715+
709716
}
710717

711718
private static void scanMeta(Connection connection, @Nullable final byte[] startRow,
@@ -749,12 +756,12 @@ public static void scanMeta(Connection connection, final Visitor visitor,
749756
static void scanMeta(Connection connection, @Nullable final byte[] startRow,
750757
@Nullable final byte[] stopRow, QueryType type, int maxRows, final Visitor visitor)
751758
throws IOException {
752-
scanMeta(connection, startRow, stopRow, type, null, maxRows, visitor);
759+
scanMeta(connection, startRow, stopRow, type, null, maxRows, visitor, CatalogReplicaMode.NONE);
753760
}
754761

755762
private static void scanMeta(Connection connection, @Nullable final byte[] startRow,
756763
@Nullable final byte[] stopRow, QueryType type, @Nullable Filter filter, int maxRows,
757-
final Visitor visitor) throws IOException {
764+
final Visitor visitor, CatalogReplicaMode metaReplicaMode) throws IOException {
758765
int rowUpperLimit = maxRows > 0 ? maxRows : Integer.MAX_VALUE;
759766
Scan scan = getMetaScan(connection.getConfiguration(), rowUpperLimit);
760767

@@ -779,6 +786,25 @@ private static void scanMeta(Connection connection, @Nullable final byte[] start
779786

780787
int currentRow = 0;
781788
try (Table metaTable = getMetaHTable(connection)) {
789+
switch (metaReplicaMode) {
790+
case LOAD_BALANCE:
791+
int numOfReplicas = metaTable.getDescriptor().getRegionReplication();
792+
if (numOfReplicas > 1) {
793+
int replicaId = ThreadLocalRandom.current().nextInt(numOfReplicas);
794+
795+
// When the replicaId is 0, do not set to Consistency.TIMELINE
796+
if (replicaId > 0) {
797+
scan.setReplicaId(replicaId);
798+
scan.setConsistency(Consistency.TIMELINE);
799+
}
800+
}
801+
break;
802+
case HEDGED_READ:
803+
scan.setConsistency(Consistency.TIMELINE);
804+
break;
805+
default:
806+
// Do nothing
807+
}
782808
try (ResultScanner scanner = metaTable.getScanner(scan)) {
783809
Result data;
784810
while ((data = scanner.next()) != null) {
@@ -2056,7 +2082,7 @@ public static List<String> getTableEncodedRegionNamesForSerialReplication(Connec
20562082
new FirstKeyOnlyFilter(), Integer.MAX_VALUE, r -> {
20572083
list.add(RegionInfo.encodeRegionName(r.getRow()));
20582084
return true;
2059-
});
2085+
}, CatalogReplicaMode.NONE);
20602086
return list;
20612087
}
20622088

hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncNonMetaRegionLocator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
import java.util.concurrent.ConcurrentSkipListMap;
5050
import java.util.concurrent.TimeUnit;
5151
import org.apache.commons.lang3.ObjectUtils;
52+
import org.apache.hadoop.hbase.CatalogReplicaMode;
5253
import org.apache.hadoop.hbase.HBaseIOException;
5354
import org.apache.hadoop.hbase.HConstants;
5455
import org.apache.hadoop.hbase.HRegionLocation;

hbase-client/src/main/java/org/apache/hadoop/hbase/client/ConnectionImplementation.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454
import java.util.stream.Collectors;
5555
import org.apache.hadoop.conf.Configuration;
5656
import org.apache.hadoop.hbase.AuthUtil;
57+
import org.apache.hadoop.hbase.CatalogReplicaMode;
5758
import org.apache.hadoop.hbase.ChoreService;
5859
import org.apache.hadoop.hbase.DoNotRetryIOException;
5960
import org.apache.hadoop.hbase.HBaseServerException;

hbase-client/src/main/java/org/apache/hadoop/hbase/client/HRegionLocator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import java.util.function.Function;
3232
import java.util.function.Supplier;
3333
import java.util.stream.Collectors;
34+
import org.apache.hadoop.hbase.CatalogReplicaMode;
3435
import org.apache.hadoop.hbase.HConstants;
3536
import org.apache.hadoop.hbase.HRegionLocation;
3637
import org.apache.hadoop.hbase.MetaTableAccessor;
@@ -143,7 +144,9 @@ public boolean visitInternal(Result result) throws IOException {
143144
return true;
144145
}
145146
};
146-
MetaTableAccessor.scanMetaForTableRegions(connection, visitor, tableName);
147+
CatalogReplicaMode metaReplicaMode = CatalogReplicaMode.fromString(connection.getConfiguration()
148+
.get(LOCATOR_META_REPLICAS_MODE, CatalogReplicaMode.NONE.toString()));
149+
MetaTableAccessor.scanMetaForTableRegions(connection, visitor, tableName, metaReplicaMode);
147150
return regions;
148151
}
149152

hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestAsyncNonMetaRegionLocator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.concurrent.ThreadLocalRandom;
3838
import java.util.stream.IntStream;
3939
import org.apache.hadoop.conf.Configuration;
40+
import org.apache.hadoop.hbase.CatalogReplicaMode;
4041
import org.apache.hadoop.hbase.HBaseClassTestRule;
4142
import org.apache.hadoop.hbase.HBaseTestingUtility;
4243
import org.apache.hadoop.hbase.HRegionLocation;

hbase-server/src/test/java/org/apache/hadoop/hbase/replication/regionserver/TestMetaRegionReplicaReplicationEndpoint.java

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -516,11 +516,8 @@ private void primaryMayIncreaseReplicaNoChange(final long[] before, final long[]
516516
}
517517

518518
private void primaryIncreaseReplicaIncrease(final long[] before, final long[] after) {
519-
// There are read requests increase for primary meta replica.
520-
assertTrue(after[RegionInfo.DEFAULT_REPLICA_ID] > before[RegionInfo.DEFAULT_REPLICA_ID]);
521-
522-
// There are read requests incrase for meta replica regions.
523-
for (int i = 1; i < after.length; i++) {
519+
// There are read requests increase for all meta replica regions,
520+
for (int i = 0; i < after.length; i++) {
524521
assertTrue(after[i] > before[i]);
525522
}
526523
}
@@ -541,6 +538,7 @@ public void testHBaseMetaReplicaGets() throws Exception {
541538
final Region[] metaRegions = getAllRegions(TableName.META_TABLE_NAME, numOfMetaReplica);
542539
long[] readReqsForMetaReplicas = new long[numOfMetaReplica];
543540
long[] readReqsForMetaReplicasAfterGet = new long[numOfMetaReplica];
541+
long[] readReqsForMetaReplicasAfterGetAllLocations = new long[numOfMetaReplica];
544542
long[] readReqsForMetaReplicasAfterMove = new long[numOfMetaReplica];
545543
long[] readReqsForMetaReplicasAfterSecondMove = new long[numOfMetaReplica];
546544
long[] readReqsForMetaReplicasAfterThirdGet = new long[numOfMetaReplica];
@@ -588,6 +586,16 @@ public void testHBaseMetaReplicaGets() throws Exception {
588586
// There are more reads against all meta replica regions, including the primary region.
589587
primaryIncreaseReplicaIncrease(readReqsForMetaReplicas, readReqsForMetaReplicasAfterGet);
590588

589+
RegionLocator locator = tableForGet.getRegionLocator();
590+
591+
for (int j = 0; j < numOfMetaReplica * 3; j++) {
592+
locator.getAllRegionLocations();
593+
}
594+
595+
getMetaReplicaReadRequests(metaRegions, readReqsForMetaReplicasAfterGetAllLocations);
596+
primaryIncreaseReplicaIncrease(readReqsForMetaReplicasAfterGet,
597+
readReqsForMetaReplicasAfterGetAllLocations);
598+
591599
// move one of regions so it meta cache may be invalid.
592600
HTU.moveRegionAndWait(userRegion.getRegionInfo(), destRs.getServerName());
593601

@@ -597,7 +605,7 @@ public void testHBaseMetaReplicaGets() throws Exception {
597605

598606
// There are read requests increase for primary meta replica.
599607
// For rest of meta replicas, there is no change as regionMove will tell the new location
600-
primaryIncreaseReplicaNoChange(readReqsForMetaReplicasAfterGet,
608+
primaryIncreaseReplicaNoChange(readReqsForMetaReplicasAfterGetAllLocations,
601609
readReqsForMetaReplicasAfterMove);
602610
// Move region again.
603611
HTU.moveRegionAndWait(userRegion.getRegionInfo(), srcRs.getServerName());

0 commit comments

Comments
 (0)