下面列出了怎么用org.apache.hadoop.hbase.master.RegionState的API类实例代码及写法,或者点击链接到github查看源代码。
@Test
public void testPutRegionInfoFromHdfsInMeta() throws Exception {
RegionInfo info = this.createRegionInfo("test-tbl");
Path regionPath = new Path("/hbase/data/default/test-tbl/" + info.getEncodedName());
FSDataInputStream fis = new FSDataInputStream(new TestInputStreamSeekable(info));
when(this.mockedFileSystem.open(new Path(regionPath, ".regioninfo")))
.thenReturn(fis);
fixer.putRegionInfoFromHdfsInMeta(regionPath);
Mockito.verify(this.mockedConnection).getTable(TableName.META_TABLE_NAME);
ArgumentCaptor<Put> captor = ArgumentCaptor.forClass(Put.class);
Mockito.verify(this.mockedTable).put(captor.capture());
Put capturedPut = captor.getValue();
List<Cell> cells = capturedPut.get(HConstants.CATALOG_FAMILY,
HConstants.STATE_QUALIFIER);
assertEquals(1, cells.size());
String state = Bytes.toString(cells.get(0).getValueArray(),
cells.get(0).getValueOffset(), cells.get(0).getValueLength());
assertEquals(RegionState.State.valueOf(state), RegionState.State.CLOSED);
cells = capturedPut.get(HConstants.CATALOG_FAMILY,
HConstants.REGIONINFO_QUALIFIER);
byte[] returnedInfo = Bytes.copy(cells.get(0).getValueArray(),
cells.get(0).getValueOffset(), cells.get(0).getValueLength());
assertEquals(info, RegionInfo.parseFrom(returnedInfo));
}
@Test
public void testAddRegionToMeta() throws Exception {
RegionInfo regionInfo = createTableAnddeleteFirstRegion();
HBCKMetaTableAccessor.addRegionToMeta(TEST_UTIL.getConnection(), regionInfo);
Connection connection = TEST_UTIL.getConnection();
Table meta = connection.getTable(TableName.META_TABLE_NAME);
Get get = new Get(regionInfo.getRegionName());
Result r = meta.get(get);
assertNotNull(r);
assertFalse(r.isEmpty());
RegionInfo returnedRI = RegionInfo.parseFrom(r.getValue(HConstants.CATALOG_FAMILY,
HConstants.REGIONINFO_QUALIFIER));
assertEquals(regionInfo, returnedRI);
String state = Bytes.toString(r.getValue(HConstants.CATALOG_FAMILY,
HConstants.STATE_QUALIFIER));
assertEquals(RegionState.State.valueOf(state), RegionState.State.CLOSED);
}
@Override
public Map<String, RegionState.State> setRegionStateInMeta(
Map<String, RegionState.State> nameOrEncodedName2State) throws IOException {
try {
if (LOG.isDebugEnabled()) {
nameOrEncodedName2State.forEach((k, v) -> LOG.debug("region={}, state={}", k, v));
}
MasterProtos.SetRegionStateInMetaResponse response =
hbck.setRegionStateInMeta(rpcControllerFactory.newController(),
RequestConverter.buildSetRegionStateInMetaRequest(nameOrEncodedName2State));
Map<String, RegionState.State> result = new HashMap<>();
for (RegionSpecifierAndState nameAndState : response.getStatesList()) {
result.put(nameAndState.getRegionSpecifier().getValue().toStringUtf8(),
RegionState.State.convert(nameAndState.getState()));
}
return result;
} catch (ServiceException se) {
throw new IOException(se);
}
}
@Test
public void testVisitMetaForRegionExistingRegion() throws Exception {
final TableName tableName = TableName.valueOf("testVisitMetaForRegion");
UTIL.createTable(tableName, "cf");
final List<HRegion> regions = UTIL.getHBaseCluster().getRegions(tableName);
final String encodedName = regions.get(0).getRegionInfo().getEncodedName();
final RegionStateStore regionStateStore = UTIL.getHBaseCluster().getMaster().
getAssignmentManager().getRegionStateStore();
final AtomicBoolean visitorCalled = new AtomicBoolean(false);
regionStateStore.visitMetaForRegion(encodedName, new RegionStateStore.RegionStateVisitor() {
@Override
public void visitRegionState(Result result, RegionInfo regionInfo, RegionState.State state,
ServerName regionLocation, ServerName lastHost, long openSeqNum) {
assertEquals(encodedName, regionInfo.getEncodedName());
visitorCalled.set(true);
}
});
assertTrue("Visitor has not been called.", visitorCalled.get());
}
/**
* Populates table's region locations into co-location info from master.
* @param table
*/
public void populateRegionLocations(TableName table) {
synchronized (this.colocationInfo) {
if (!isTableColocated(table)) {
throw new IllegalArgumentException("Specified table " + table
+ " should be in one of the tables to co-locate.");
}
RegionStates regionStates = this.master.getAssignmentManager().getRegionStates();
List<HRegionInfo> onlineRegions = regionStates.getRegionsOfTable(table);
for (HRegionInfo hri : onlineRegions) {
regionOnline(hri, regionStates.getRegionServerOfRegion(hri));
}
Map<String, RegionState> regionsInTransition = regionStates.getRegionsInTransition();
for (RegionState regionState : regionsInTransition.values()) {
if (table.equals(regionState.getRegion().getTable())
&& regionState.getServerName() != null) {
regionOnline(regionState.getRegion(), regionState.getServerName());
}
}
}
}
/**
* Adds a hbase:meta row for each of the specified new regions. Initial state for new regions is
* CLOSED.
* @param connection connection we're using
* @param regionInfos region information list
* @param ts desired timestamp
* @throws IOException if problem connecting or updating meta
*/
private static void addRegionsToMeta(Connection connection, List<RegionInfo> regionInfos,
int regionReplication, long ts) throws IOException {
List<Put> puts = new ArrayList<>();
for (RegionInfo regionInfo : regionInfos) {
if (RegionReplicaUtil.isDefaultReplica(regionInfo)) {
Put put = makePutFromRegionInfo(regionInfo, ts);
// New regions are added with initial state of CLOSED.
addRegionStateToPut(put, RegionState.State.CLOSED);
// Add empty locations for region replicas so that number of replicas can be cached
// whenever the primary region is looked up from meta
for (int i = 1; i < regionReplication; i++) {
addEmptyLocation(put, i);
}
puts.add(put);
}
}
putsToMetaTable(connection, puts);
LOG.info("Added {} regions to meta.", puts.size());
}
ClusterMetricsImpl(String hbaseVersion, List<ServerName> deadServerNames,
Map<ServerName, ServerMetrics> liveServerMetrics,
ServerName masterName,
List<ServerName> backupMasterNames,
List<RegionState> regionsInTransition,
String clusterId,
List<String> masterCoprocessorNames,
Boolean balancerOn,
int masterInfoPort,
List<ServerName> serversName,
Map<TableName, RegionStatesCount> tableRegionStatesCount) {
this.hbaseVersion = hbaseVersion;
this.deadServerNames = Preconditions.checkNotNull(deadServerNames);
this.liveServerMetrics = Preconditions.checkNotNull(liveServerMetrics);
this.masterName = masterName;
this.backupMasterNames = Preconditions.checkNotNull(backupMasterNames);
this.regionsInTransition = Preconditions.checkNotNull(regionsInTransition);
this.clusterId = clusterId;
this.masterCoprocessorNames = Preconditions.checkNotNull(masterCoprocessorNames);
this.balancerOn = balancerOn;
this.masterInfoPort = masterInfoPort;
this.serversName = serversName;
this.tableRegionStatesCount = Preconditions.checkNotNull(tableRegionStatesCount);
}
@Test
public void buildFirstPageQueryStringNonNullParams() {
final HttpServletRequest request = new MockRequestBuilder()
.setLimit(50)
.setRegionState(RegionState.State.ABNORMALLY_CLOSED)
.setTable("foo%3Abar")
.build();
final MetaBrowser metaBrowser = new MetaBrowser(connection, request);
assertEquals(50, metaBrowser.getScanLimit().intValue());
assertEquals(RegionState.State.ABNORMALLY_CLOSED, metaBrowser.getScanRegionState());
assertEquals(TableName.valueOf("foo", "bar"), metaBrowser.getScanTable());
assertEquals(
"/table.jsp?name=hbase%3Ameta"
+ "&scan_limit=50"
+ "&scan_region_state=ABNORMALLY_CLOSED"
+ "&scan_table=foo%3Abar",
metaBrowser.buildNextPageUrl(null));
}
public void regionClosedAbnormally(RegionStateNode regionNode) throws IOException {
RegionState.State state = regionNode.getState();
ServerName regionLocation = regionNode.getRegionLocation();
regionNode.transitionState(State.ABNORMALLY_CLOSED);
regionNode.setRegionLocation(null);
boolean succ = false;
try {
regionStateStore.updateRegionLocation(regionNode);
succ = true;
} finally {
if (!succ) {
// revert
regionNode.setState(state);
regionNode.setRegionLocation(regionLocation);
}
}
if (regionLocation != null) {
regionNode.setLastHost(regionLocation);
regionStates.removeRegionFromServer(regionLocation, regionNode);
}
}
/**
* Creates a protocol buffer SetRegionStateInMetaRequest
* @param nameOrEncodedName2State list of regions states to update in Meta
* @return a SetRegionStateInMetaRequest
*/
public static SetRegionStateInMetaRequest
buildSetRegionStateInMetaRequest(Map<String, RegionState.State> nameOrEncodedName2State) {
SetRegionStateInMetaRequest.Builder builder = SetRegionStateInMetaRequest.newBuilder();
nameOrEncodedName2State.forEach((name, state) -> {
byte[] bytes = Bytes.toBytes(name);
RegionSpecifier spec;
if (RegionInfo.isEncodedRegionName(bytes)) {
spec = buildRegionSpecifier(RegionSpecifierType.ENCODED_REGION_NAME, bytes);
} else {
spec = buildRegionSpecifier(RegionSpecifierType.REGION_NAME, bytes);
}
builder.addStates(RegionSpecifierAndState.newBuilder().setRegionSpecifier(spec)
.setState(state.convert()).build());
});
return builder.build();
}
/**
* Add regions to in-memory states
* @param regionInfos regions to add
* @param env MasterProcedureEnv
* @param regionReplication the number of region replications
*/
private void addRegionsToInMemoryStates(List<RegionInfo> regionInfos, MasterProcedureEnv env,
int regionReplication) {
AssignmentManager am = env.getAssignmentManager();
for (RegionInfo regionInfo : regionInfos) {
if (regionInfo.isSplit()) {
am.getRegionStates().updateRegionState(regionInfo, RegionState.State.SPLIT);
} else {
am.getRegionStates().updateRegionState(regionInfo, RegionState.State.CLOSED);
// For region replicas
for (int i = 1; i < regionReplication; i++) {
RegionInfo regionInfoForReplica =
RegionReplicaUtil.getRegionInfoForReplica(regionInfo, i);
am.getRegionStates().updateRegionState(regionInfoForReplica, RegionState.State.CLOSED);
}
}
}
}
@Test
public void testLastRegionAndOpeningCanNotPush() throws IOException, ReplicationException {
RegionInfo region = RegionInfoBuilder.newBuilder(tableName).build();
addStateAndBarrier(region, RegionState.State.OPEN, 10);
Cell cell = createCell(region);
// can push since we are in the first range
assertTrue(checker.canPush(createEntry(region, 100), cell));
setState(region, RegionState.State.OPENING);
// can not push since we are in the last range and the state is OPENING
assertFalse(checker.canPush(createEntry(region, 102), cell));
addStateAndBarrier(region, RegionState.State.OPEN, 50);
// can not push since the previous range has not been finished yet
assertFalse(checker.canPush(createEntry(region, 102), cell));
updatePushedSeqId(region, 49);
// can push since the previous range has been finished
assertTrue(checker.canPush(createEntry(region, 102), cell));
setState(region, RegionState.State.OPENING);
assertFalse(checker.canPush(createEntry(region, 104), cell));
}
@Test
public void testCanPushAfterSplit() throws IOException, ReplicationException {
// 0xFF is the escape byte when storing region name so let's make sure it can work.
byte[] endKey = new byte[] { (byte) 0xFF, 0x00, (byte) 0xFF, (byte) 0xFF, 0x01 };
RegionInfo region = RegionInfoBuilder.newBuilder(tableName).setRegionId(1).build();
RegionInfo regionA =
RegionInfoBuilder.newBuilder(tableName).setEndKey(endKey).setRegionId(2).build();
RegionInfo regionB =
RegionInfoBuilder.newBuilder(tableName).setStartKey(endKey).setRegionId(3).build();
addStateAndBarrier(region, null, 10, 100);
addStateAndBarrier(regionA, RegionState.State.OPEN, 100, 200);
addStateAndBarrier(regionB, RegionState.State.OPEN, 100, 300);
addParents(regionA, Arrays.asList(region));
addParents(regionB, Arrays.asList(region));
Cell cellA = createCell(regionA);
Cell cellB = createCell(regionB);
// can not push since parent has not been finished yet
assertFalse(checker.canPush(createEntry(regionA, 150), cellA));
assertFalse(checker.canPush(createEntry(regionB, 200), cellB));
updatePushedSeqId(region, 99);
// can push since parent has been finished
assertTrue(checker.canPush(createEntry(regionA, 150), cellA));
assertTrue(checker.canPush(createEntry(regionB, 200), cellB));
}
/**
* Test waiting on meat w/ no timeout specified.
*/
@Test
public void testNoTimeoutWaitForMeta() throws IOException, InterruptedException, KeeperException {
ServerName hsa = MetaTableLocator.getMetaRegionLocation(watcher);
assertNull(hsa);
// Now test waiting on meta location getting set.
Thread t = new WaitOnMetaThread();
startWaitAliveThenWaitItLives(t, 1);
// Set a meta location.
MetaTableLocator.setMetaLocation(this.watcher, SN, RegionState.State.OPEN);
hsa = SN;
// Join the thread... should exit shortly.
t.join();
// Now meta is available.
assertTrue(MetaTableLocator.getMetaRegionLocation(watcher).equals(hsa));
}
public static void waitUntilAssigned(Admin admin,
RegionInfo region) throws IOException, InterruptedException {
long timeout = admin.getConfiguration().getLong("hbase.hbck.assign.timeout", 120000);
long expiration = timeout + System.currentTimeMillis();
while (System.currentTimeMillis() < expiration) {
try {
boolean inTransition = false;
for (RegionState rs : admin.getClusterMetrics(EnumSet.of(Option.REGIONS_IN_TRANSITION))
.getRegionStatesInTransition()) {
if (RegionInfo.COMPARATOR.compare(rs.getRegion(), region) == 0) {
inTransition = true;
break;
}
}
if (!inTransition) {
// yay! no longer RIT
return;
}
// still in rit
LOG.info("Region still in transition, waiting for "
+ "it to become assigned: " + region);
} catch (IOException e) {
LOG.warn("Exception when waiting for region to become assigned,"
+ " retrying", e);
}
Thread.sleep(1000);
}
throw new IOException("Region " + region + " failed to move out of " +
"transition within timeout " + timeout + "ms");
}
private Pair<RegionState.State, ServerName> getStateAndServerName(
ZooKeeperProtos.MetaRegionServer proto) {
RegionState.State state;
if (proto.hasState()) {
state = RegionState.State.convert(proto.getState());
} else {
state = RegionState.State.OPEN;
}
HBaseProtos.ServerName snProto = proto.getServer();
return Pair.newPair(state,
ServerName.valueOf(snProto.getHostName(), snProto.getPort(), snProto.getStartCode()));
}
@Test
public void testRegionDetailsForDisplay() throws IOException {
byte[] startKey = new byte[] {0x01, 0x01, 0x02, 0x03};
byte[] endKey = new byte[] {0x01, 0x01, 0x02, 0x04};
Configuration conf = new Configuration();
conf.setBoolean("hbase.display.keys", false);
RegionInfo h = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName()))
.setStartKey(startKey).setEndKey(endKey).build();
checkEquality(h, conf);
// check HRIs with non-default replicaId
h = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName())).setStartKey(startKey)
.setEndKey(endKey).setRegionId(System.currentTimeMillis()).setReplicaId(1).build();
checkEquality(h, conf);
assertArrayEquals(RegionInfoDisplay.HIDDEN_END_KEY,
RegionInfoDisplay.getEndKeyForDisplay(h, conf));
assertArrayEquals(RegionInfoDisplay.HIDDEN_START_KEY,
RegionInfoDisplay.getStartKeyForDisplay(h, conf));
RegionState state = RegionState.createForTesting(h, RegionState.State.OPEN);
String descriptiveNameForDisplay =
RegionInfoDisplay.getDescriptiveNameFromRegionStateForDisplay(state, conf);
checkDescriptiveNameEquality(descriptiveNameForDisplay, state.toDescriptiveString(), startKey);
conf.setBoolean("hbase.display.keys", true);
assertArrayEquals(endKey, RegionInfoDisplay.getEndKeyForDisplay(h, conf));
assertArrayEquals(startKey, RegionInfoDisplay.getStartKeyForDisplay(h, conf));
assertEquals(state.toDescriptiveString(),
RegionInfoDisplay.getDescriptiveNameFromRegionStateForDisplay(state, conf));
}
private void validateOpen(List<RegionInfo> regions) {
for (RegionInfo ri : regions) {
RegionState rs = TEST_UTIL.getHBaseCluster().getMaster().getAssignmentManager().
getRegionStates().getRegionState(ri.getEncodedName());
LOG.info("RS: {}", rs.toString());
assertTrue(rs.toString(), rs.isOpened());
}
}
private RegionState.State getCurrentRegionState(RegionInfo regionInfo) throws IOException{
Table metaTable = TEST_UTIL.getConnection().getTable(TableName.valueOf("hbase:meta"));
Get get = new Get(regionInfo.getRegionName());
get.addColumn(HConstants.CATALOG_FAMILY, HConstants.STATE_QUALIFIER);
Result result = metaTable.get(get);
byte[] currentStateValue = result.getValue(HConstants.CATALOG_FAMILY,
HConstants.STATE_QUALIFIER);
return currentStateValue != null ?
RegionState.State.valueOf(Bytes.toString(currentStateValue))
: null;
}
private static Put addRegionStateToPut(Put put, RegionState.State state) throws IOException {
put.add(CellBuilderFactory.create(CellBuilderType.SHALLOW_COPY).setRow(put.getRow())
.setFamily(HConstants.CATALOG_FAMILY).setQualifier(HConstants.STATE_QUALIFIER)
.setTimestamp(put.getTimestamp()).setType(Cell.Type.Put).setValue(Bytes.toBytes(state.name()))
.build());
return put;
}
@Test
public void testRegionDetailsForDisplay() throws IOException {
byte[] startKey = new byte[] {0x01, 0x01, 0x02, 0x03};
byte[] endKey = new byte[] {0x01, 0x01, 0x02, 0x04};
Configuration conf = new Configuration();
conf.setBoolean("hbase.display.keys", false);
RegionInfo ri = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName()))
.setStartKey(startKey).setEndKey(endKey).build();
checkEquality(ri, conf);
// check HRIs with non-default replicaId
ri = RegionInfoBuilder.newBuilder(TableName.valueOf(name.getMethodName()))
.setStartKey(startKey)
.setEndKey(endKey)
.setSplit(false)
.setRegionId(System.currentTimeMillis())
.setReplicaId(1).build();
checkEquality(ri, conf);
Assert.assertArrayEquals(RegionInfoDisplay.HIDDEN_END_KEY,
RegionInfoDisplay.getEndKeyForDisplay(ri, conf));
Assert.assertArrayEquals(RegionInfoDisplay.HIDDEN_START_KEY,
RegionInfoDisplay.getStartKeyForDisplay(ri, conf));
RegionState state = RegionState.createForTesting(ri, RegionState.State.OPEN);
String descriptiveNameForDisplay =
RegionInfoDisplay.getDescriptiveNameFromRegionStateForDisplay(state, conf);
String originalDescriptive = state.toDescriptiveString();
checkDescriptiveNameEquality(descriptiveNameForDisplay, originalDescriptive, startKey);
conf.setBoolean("hbase.display.keys", true);
Assert.assertArrayEquals(endKey, RegionInfoDisplay.getEndKeyForDisplay(ri, conf));
Assert.assertArrayEquals(startKey, RegionInfoDisplay.getStartKeyForDisplay(ri, conf));
Assert.assertEquals(originalDescriptive,
RegionInfoDisplay.getDescriptiveNameFromRegionStateForDisplay(state, conf));
}
private static ReplicationBarrierResult getReplicationBarrierResult(Result result) {
long[] barriers = getReplicationBarriers(result);
byte[] stateBytes = result.getValue(HConstants.CATALOG_FAMILY, HConstants.STATE_QUALIFIER);
RegionState.State state =
stateBytes != null ? RegionState.State.valueOf(Bytes.toString(stateBytes)) : null;
byte[] parentRegionsBytes =
result.getValue(HConstants.REPLICATION_BARRIER_FAMILY, REPLICATION_PARENT_QUALIFIER);
List<byte[]> parentRegionNames =
parentRegionsBytes != null ? parseParentsBytes(parentRegionsBytes) : Collections.emptyList();
return new ReplicationBarrierResult(barriers, state, parentRegionNames);
}
/**
* Get the descriptive name as {@link RegionState} does it but with hidden
* startkey optionally
* @return descriptive string
*/
public static String getDescriptiveNameFromRegionStateForDisplay(RegionState state,
Configuration conf) {
if (conf.getBoolean(DISPLAY_KEYS_KEY, true)) return state.toDescriptiveString();
String descriptiveStringFromState = state.toDescriptiveString();
int idx = descriptiveStringFromState.lastIndexOf(" state=");
String regionName = getRegionNameAsStringForDisplay(
RegionInfoBuilder.newBuilder(state.getRegion()).build(), conf);
return regionName + descriptiveStringFromState.substring(idx);
}
/**
* Determine if a region in {@link RegionState} should be considered for a split operation.
*/
private static boolean skipForSplit(final RegionState state, final RegionInfo regionInfo) {
final String name = regionInfo.getEncodedName();
return
logTraceReason(
() -> state == null,
"skipping split of region {} because no state information is available.", name)
|| logTraceReason(
() -> !Objects.equals(state.getState(), RegionState.State.OPEN),
"skipping merge of region {} because it is not open.", name);
}
ArrayList<RegionState> getTableRegionStates(final TableName tableName) {
final ArrayList<RegionState> regions = new ArrayList<RegionState>();
for (RegionStateNode node: regionsMap.tailMap(tableName.getName()).values()) {
if (!node.getTable().equals(tableName)) break;
regions.add(node.toRegionState());
}
return regions;
}
/** @return A snapshot of region state nodes for all the regions. */
public ArrayList<RegionState> getRegionStates() {
final ArrayList<RegionState> regions = new ArrayList<>(regionsMap.size());
for (RegionStateNode node: regionsMap.values()) {
regions.add(node.toRegionState());
}
return regions;
}
public RegionState getRegionState(final String encodedRegionName) {
final RegionStateNode node = encodedRegionsMap.get(encodedRegionName);
if (node == null) {
return null;
}
return node.toRegionState();
}
public Map<RegionState.State, List<RegionInfo>> getRegionByStateOfTable(TableName tableName) {
final State[] states = State.values();
final Map<RegionState.State, List<RegionInfo>> tableRegions =
new HashMap<State, List<RegionInfo>>(states.length);
for (int i = 0; i < states.length; ++i) {
tableRegions.put(states[i], new ArrayList<RegionInfo>());
}
for (RegionStateNode node: regionsMap.values()) {
if (node.getTable().equals(tableName)) {
tableRegions.get(node.getState()).add(node.getRegionInfo());
}
}
return tableRegions;
}
public RegionState getRegionTransitionState(RegionInfo hri) {
RegionStateNode node = regionInTransition.get(hri);
if (node == null) {
return null;
}
node.lock();
try {
return node.isInTransition() ? node.toRegionState() : null;
} finally {
node.unlock();
}
}
/**
* Gets the meta region location, if available. Does not block.
* @param zkw zookeeper connection to use
* @return server name or null if we failed to get the data.
*/
public static ServerName getMetaRegionLocation(final ZKWatcher zkw) {
try {
RegionState state = getMetaRegionState(zkw);
return state.isOpened() ? state.getServerName() : null;
} catch (KeeperException ke) {
return null;
}
}