Skip to content

Commit 6e31c18

Browse files
huaxiangsunHuaxiang Sunndimiduk
committed
HBASE-27250 MasterRpcService#setRegionStateInMeta does not support replica region encodedNames or region names
- Added sanity check to make sure input region encoded name or region name is valid - Assignment improvements pertaining to read replica regions - make several MetaTableAccessor methods more precise in their handling of replica regions - hbck2 setRegionStateInMeta and HBCKServerCrashProcedure handle read replicas - separate AM helper methods -- loading RegionInfo from cache vs. refreshing cache from meta - AM helper method support loading RegionInfo from cache via either region name and encoded region name (both caches are maintained, and under lock) - consolidate, extend tests to cover read replica regions Co-authored-by: Huaxiang Sun <[email protected]> Co-authored-by: Nick Dimiduk <[email protected]> Signed-off-by: Peter Somogyi <[email protected]>
1 parent 2cdcab2 commit 6e31c18

File tree

20 files changed

+394
-294
lines changed

20 files changed

+394
-294
lines changed

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

Lines changed: 45 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,8 @@ public static HRegionLocation getRegionLocation(Connection connection, byte[] re
289289
RegionLocations locations = getRegionLocations(r);
290290
return locations == null
291291
? null
292-
: locations.getRegionLocation(parsedInfo == null ? 0 : parsedInfo.getReplicaId());
292+
: locations.getRegionLocation(
293+
parsedInfo == null ? RegionInfo.DEFAULT_REPLICA_ID : parsedInfo.getReplicaId());
293294
}
294295

295296
/**
@@ -333,12 +334,12 @@ public static RegionInfo parseRegionInfoFromRegionName(byte[] regionName) throws
333334
/**
334335
* Gets the result in hbase:meta for the specified region.
335336
* @param connection connection we're using
336-
* @param regionName region we're looking for
337+
* @param regionInfo region we're looking for
337338
* @return result of the specified region
338339
*/
339-
public static Result getRegionResult(Connection connection, byte[] regionName)
340+
public static Result getRegionResult(Connection connection, RegionInfo regionInfo)
340341
throws IOException {
341-
Get get = new Get(regionName);
342+
Get get = new Get(getMetaKeyForRegion(regionInfo));
342343
get.addFamily(HConstants.CATALOG_FAMILY);
343344
return get(getMetaHTable(connection), get);
344345
}
@@ -364,20 +365,20 @@ public static Result scanByRegionEncodedName(Connection connection, String regio
364365
}
365366

366367
/**
367-
* Returns Return all regioninfos listed in the 'info:merge*' columns of the
368-
* <code>regionName</code> row.
368+
* Returns Return all regioninfos listed in the 'info:merge*' columns of the {@code regionInfo}
369+
* row.
369370
*/
370371
@Nullable
371-
public static List<RegionInfo> getMergeRegions(Connection connection, byte[] regionName)
372+
public static List<RegionInfo> getMergeRegions(Connection connection, RegionInfo regionInfo)
372373
throws IOException {
373-
return getMergeRegions(getRegionResult(connection, regionName).rawCells());
374+
return getMergeRegions(getRegionResult(connection, regionInfo).rawCells());
374375
}
375376

376377
/**
377-
* Check whether the given {@code regionName} has any 'info:merge*' columns.
378+
* Check whether the given {@code regionInfo} has any 'info:merge*' columns.
378379
*/
379-
public static boolean hasMergeRegions(Connection conn, byte[] regionName) throws IOException {
380-
return hasMergeRegions(getRegionResult(conn, regionName).rawCells());
380+
public static boolean hasMergeRegions(Connection conn, RegionInfo regionInfo) throws IOException {
381+
return hasMergeRegions(getRegionResult(conn, regionInfo).rawCells());
381382
}
382383

383384
/**
@@ -1294,11 +1295,21 @@ public final boolean visit(Result rowResult) throws IOException {
12941295
////////////////////////
12951296
// Editing operations //
12961297
////////////////////////
1298+
1299+
/**
1300+
* Generates and returns a {@link Put} containing the {@link RegionInfo} for the catalog table.
1301+
* @throws IllegalArgumentException when the provided RegionInfo is not the default replica.
1302+
*/
1303+
public static Put makePutFromRegionInfo(RegionInfo regionInfo) throws IOException {
1304+
return makePutFromRegionInfo(regionInfo, EnvironmentEdgeManager.currentTime());
1305+
}
1306+
12971307
/**
1298-
* Generates and returns a Put containing the region into for the catalog table
1308+
* Generates and returns a {@link Put} containing the {@link RegionInfo} for the catalog table.
1309+
* @throws IllegalArgumentException when the provided RegionInfo is not the default replica.
12991310
*/
13001311
public static Put makePutFromRegionInfo(RegionInfo regionInfo, long ts) throws IOException {
1301-
return addRegionInfo(new Put(regionInfo.getRegionName(), ts), regionInfo);
1312+
return addRegionInfo(new Put(getMetaKeyForRegion(regionInfo), ts), regionInfo);
13021313
}
13031314

13041315
/**
@@ -1308,7 +1319,11 @@ public static Delete makeDeleteFromRegionInfo(RegionInfo regionInfo, long ts) {
13081319
if (regionInfo == null) {
13091320
throw new IllegalArgumentException("Can't make a delete for null region");
13101321
}
1311-
Delete delete = new Delete(regionInfo.getRegionName());
1322+
if (regionInfo.getReplicaId() != RegionInfo.DEFAULT_REPLICA_ID) {
1323+
throw new IllegalArgumentException(
1324+
"Can't make delete for a replica region. Operate on the primary");
1325+
}
1326+
Delete delete = new Delete(getMetaKeyForRegion(regionInfo));
13121327
delete.addFamily(getCatalogFamily(), ts);
13131328
return delete;
13141329
}
@@ -1399,9 +1414,14 @@ private static void deleteFromMetaTable(final Connection connection, final List<
13991414
}
14001415
}
14011416

1402-
private static Put addRegionStateToPut(Put put, RegionState.State state) throws IOException {
1417+
/**
1418+
* Set the column value corresponding to this {@code replicaId}'s {@link RegionState} to the
1419+
* provided {@code state}. Mutates the provided {@link Put}.
1420+
*/
1421+
private static Put addRegionStateToPut(Put put, int replicaId, RegionState.State state)
1422+
throws IOException {
14031423
put.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(put.getRow())
1404-
.setFamily(HConstants.CATALOG_FAMILY).setQualifier(getRegionStateColumn())
1424+
.setFamily(HConstants.CATALOG_FAMILY).setQualifier(getRegionStateColumn(replicaId))
14051425
.setTimestamp(put.getTimestamp()).setType(Cell.Type.Put).setValue(Bytes.toBytes(state.name()))
14061426
.build());
14071427
return put;
@@ -1412,9 +1432,9 @@ private static Put addRegionStateToPut(Put put, RegionState.State state) throws
14121432
*/
14131433
public static void updateRegionState(Connection connection, RegionInfo ri,
14141434
RegionState.State state) throws IOException {
1415-
Put put = new Put(RegionReplicaUtil.getRegionInfoForDefaultReplica(ri).getRegionName());
1416-
MetaTableAccessor.putsToMetaTable(connection,
1417-
Collections.singletonList(addRegionStateToPut(put, state)));
1435+
final Put put = makePutFromRegionInfo(ri);
1436+
addRegionStateToPut(put, ri.getReplicaId(), state);
1437+
putsToMetaTable(connection, Collections.singletonList(put));
14181438
}
14191439

14201440
/**
@@ -1432,7 +1452,7 @@ public static void updateRegionState(Connection connection, RegionInfo ri,
14321452
public static void addSplitsToParent(Connection connection, RegionInfo regionInfo,
14331453
RegionInfo splitA, RegionInfo splitB) throws IOException {
14341454
try (Table meta = getMetaHTable(connection)) {
1435-
Put put = makePutFromRegionInfo(regionInfo, EnvironmentEdgeManager.currentTime());
1455+
Put put = makePutFromRegionInfo(regionInfo);
14361456
addDaughtersToPut(put, splitA, splitB);
14371457
meta.put(put);
14381458
debugLogMutation(put);
@@ -1483,7 +1503,7 @@ private static void addRegionsToMeta(Connection connection, List<RegionInfo> reg
14831503
if (RegionReplicaUtil.isDefaultReplica(regionInfo)) {
14841504
Put put = makePutFromRegionInfo(regionInfo, ts);
14851505
// New regions are added with initial state of CLOSED.
1486-
addRegionStateToPut(put, RegionState.State.CLOSED);
1506+
addRegionStateToPut(put, regionInfo.getReplicaId(), RegionState.State.CLOSED);
14871507
// Add empty locations for region replicas so that number of replicas can be cached
14881508
// whenever the primary region is looked up from meta
14891509
for (int i = 1; i < regionReplication; i++) {
@@ -1548,7 +1568,7 @@ public static void mergeRegions(Connection connection, RegionInfo mergedRegion,
15481568
// default OFFLINE state. If Master gets restarted after this step, start up sequence of
15491569
// master tries to assign this offline region. This is followed by re-assignments of the
15501570
// merged region from resumed {@link MergeTableRegionsProcedure}
1551-
addRegionStateToPut(putOfMerged, RegionState.State.CLOSED);
1571+
addRegionStateToPut(putOfMerged, RegionInfo.DEFAULT_REPLICA_ID, RegionState.State.CLOSED);
15521572
mutations.add(putOfMerged);
15531573
// The merged is a new region, openSeqNum = 1 is fine. ServerName may be null
15541574
// if crash after merge happened but before we got to here.. means in-memory
@@ -1606,8 +1626,8 @@ public static void splitRegion(Connection connection, RegionInfo parent, long pa
16061626
// default OFFLINE state. If Master gets restarted after this step, start up sequence of
16071627
// master tries to assign these offline regions. This is followed by re-assignments of the
16081628
// daughter regions from resumed {@link SplitTableRegionProcedure}
1609-
addRegionStateToPut(putA, RegionState.State.CLOSED);
1610-
addRegionStateToPut(putB, RegionState.State.CLOSED);
1629+
addRegionStateToPut(putA, RegionInfo.DEFAULT_REPLICA_ID, RegionState.State.CLOSED);
1630+
addRegionStateToPut(putB, RegionInfo.DEFAULT_REPLICA_ID, RegionState.State.CLOSED);
16111631

16121632
addSequenceNum(putA, 1, splitA.getReplicaId()); // new regions, openSeqNum = 1 is fine.
16131633
addSequenceNum(putB, 1, splitB.getReplicaId());
@@ -1820,7 +1840,7 @@ public static void deleteMergeQualifiers(Connection connection, final RegionInfo
18201840
throws IOException {
18211841
Delete delete = new Delete(mergeRegion.getRegionName());
18221842
// NOTE: We are doing a new hbase:meta read here.
1823-
Cell[] cells = getRegionResult(connection, mergeRegion.getRegionName()).rawCells();
1843+
Cell[] cells = getRegionResult(connection, mergeRegion).rawCells();
18241844
if (cells == null || cells.length == 0) {
18251845
return;
18261846
}

0 commit comments

Comments
 (0)