|
32 | 32 | import java.util.concurrent.ThreadLocalRandom; |
33 | 33 | import java.util.stream.Collectors; |
34 | 34 | import org.apache.hadoop.conf.Configuration; |
| 35 | +import org.apache.hadoop.hbase.CatalogFamilyFormat; |
35 | 36 | import org.apache.hadoop.hbase.ClusterMetricsBuilder; |
36 | 37 | import org.apache.hadoop.hbase.DoNotRetryIOException; |
37 | 38 | import org.apache.hadoop.hbase.HBaseRpcServicesBase; |
|
49 | 50 | import org.apache.hadoop.hbase.client.NormalizeTableFilterParams; |
50 | 51 | import org.apache.hadoop.hbase.client.Put; |
51 | 52 | import org.apache.hadoop.hbase.client.RegionInfo; |
| 53 | +import org.apache.hadoop.hbase.client.RegionInfoBuilder; |
52 | 54 | import org.apache.hadoop.hbase.client.Table; |
53 | 55 | import org.apache.hadoop.hbase.client.TableDescriptor; |
54 | 56 | import org.apache.hadoop.hbase.client.TableState; |
@@ -2479,30 +2481,41 @@ public SetRegionStateInMetaResponse setRegionStateInMeta(RpcController controlle |
2479 | 2481 | for (RegionSpecifierAndState s : request.getStatesList()) { |
2480 | 2482 | RegionSpecifier spec = s.getRegionSpecifier(); |
2481 | 2483 | String encodedName; |
| 2484 | + RegionInfo info; |
| 2485 | + int replicaId; |
2482 | 2486 | if (spec.getType() == RegionSpecifierType.ENCODED_REGION_NAME) { |
2483 | | - encodedName = spec.getValue().toStringUtf8(); |
| 2487 | + info = this.server.getAssignmentManager().getRegionInfoFromEncodedRegionName( |
| 2488 | + spec.getValue().toStringUtf8()); |
2484 | 2489 | } else { |
2485 | 2490 | // TODO: actually, a full region name can save a lot on meta scan, improve later. |
2486 | | - encodedName = RegionInfo.encodeRegionName(spec.getValue().toByteArray()); |
| 2491 | + info = CatalogFamilyFormat.parseRegionInfoFromRegionName(spec.getValue().toByteArray()); |
2487 | 2492 | } |
2488 | | - RegionInfo info = this.server.getAssignmentManager().loadRegionFromMeta(encodedName); |
2489 | | - LOG.trace("region info loaded from meta table: {}", info); |
| 2493 | + replicaId = info.getReplicaId(); |
| 2494 | + LOG.trace("region info", info); |
2490 | 2495 | RegionState prevState = |
2491 | 2496 | this.server.getAssignmentManager().getRegionStates().getRegionState(info); |
2492 | 2497 | RegionState.State newState = RegionState.State.convert(s.getState()); |
2493 | 2498 | LOG.info("{} set region={} state from {} to {}", server.getClientIdAuditPrefix(), info, |
2494 | 2499 | prevState.getState(), newState); |
2495 | | - Put metaPut = |
2496 | | - MetaTableAccessor.makePutFromRegionInfo(info, EnvironmentEdgeManager.currentTime()); |
2497 | | - metaPut.addColumn(HConstants.CATALOG_FAMILY, HConstants.STATE_QUALIFIER, |
2498 | | - Bytes.toBytes(newState.name())); |
2499 | | - List<Put> putList = new ArrayList<>(); |
2500 | | - putList.add(metaPut); |
2501 | | - MetaTableAccessor.putsToMetaTable(this.server.getConnection(), putList); |
2502 | | - // Loads from meta again to refresh AM cache with the new region state |
2503 | | - this.server.getAssignmentManager().loadRegionFromMeta(encodedName); |
2504 | | - builder.addStates(RegionSpecifierAndState.newBuilder().setRegionSpecifier(spec) |
2505 | | - .setState(prevState.getState().convert())); |
| 2500 | + // If state does not change, no need to set. |
| 2501 | + if (prevState.getState() != newState) { |
| 2502 | + if (replicaId > RegionInfo.DEFAULT_REPLICA_ID) { |
| 2503 | + // If it is a non-primary replica region, use primary region as the key. |
| 2504 | + info = RegionInfoBuilder.newBuilder(info).setReplicaId(RegionInfo.DEFAULT_REPLICA_ID). |
| 2505 | + build(); |
| 2506 | + } |
| 2507 | + Put metaPut = new Put(info.getRegionName()); |
| 2508 | + metaPut.addColumn(HConstants.CATALOG_FAMILY, |
| 2509 | + CatalogFamilyFormat.getRegionStateColumn(replicaId), |
| 2510 | + Bytes.toBytes(newState.name())); |
| 2511 | + List<Put> putList = new ArrayList<>(); |
| 2512 | + putList.add(metaPut); |
| 2513 | + MetaTableAccessor.putsToMetaTable(this.server.getConnection(), putList); |
| 2514 | + // Loads from meta again to refresh AM cache with the new region state |
| 2515 | + this.server.getAssignmentManager().loadRegionFromMeta(info.getEncodedName()); |
| 2516 | + builder.addStates(RegionSpecifierAndState.newBuilder().setRegionSpecifier(spec). |
| 2517 | + setState(prevState.getState().convert())); |
| 2518 | + } |
2506 | 2519 | } |
2507 | 2520 | } catch (Exception e) { |
2508 | 2521 | throw new ServiceException(e); |
|
0 commit comments