Java Code Examples for org.apache.hadoop.hbase.master.RegionState#State

The following examples show how to use org.apache.hadoop.hbase.master.RegionState#State . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RequestConverter.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * 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();
}
 
Example 2
Source File: HBaseHbck.java    From hbase with Apache License 2.0 6 votes vote down vote up
@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);
  }
}
 
Example 3
Source File: AssignmentManager.java    From hbase with Apache License 2.0 6 votes vote down vote up
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);
  }
}
 
Example 4
Source File: AssignmentManager.java    From hbase with Apache License 2.0 6 votes vote down vote up
void regionFailedOpen(RegionStateNode regionNode, boolean giveUp) throws IOException {
  RegionState.State state = regionNode.getState();
  ServerName regionLocation = regionNode.getRegionLocation();
  if (giveUp) {
    regionNode.setState(State.FAILED_OPEN);
    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) {
    regionStates.removeRegionFromServer(regionLocation, regionNode);
  }
}
 
Example 5
Source File: TestMetaTableLocator.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Test normal operations
 */
@Test
public void testMetaLookup()
    throws IOException, InterruptedException, ServiceException, KeeperException {
  final ClientProtos.ClientService.BlockingInterface client =
    Mockito.mock(ClientProtos.ClientService.BlockingInterface.class);

  Mockito.when(client.get((RpcController) Mockito.any(), (GetRequest) Mockito.any()))
    .thenReturn(GetResponse.newBuilder().build());

  assertNull(MetaTableLocator.getMetaRegionLocation(this.watcher));
  for (RegionState.State state : RegionState.State.values()) {
    if (state.equals(RegionState.State.OPEN)) {
      continue;
    }
    MetaTableLocator.setMetaLocation(this.watcher, SN, state);
    assertNull(MetaTableLocator.getMetaRegionLocation(this.watcher));
    assertEquals(state, MetaTableLocator.getMetaRegionState(this.watcher).getState());
  }
  MetaTableLocator.setMetaLocation(this.watcher, SN, RegionState.State.OPEN);
  assertEquals(SN, MetaTableLocator.getMetaRegionLocation(this.watcher));
  assertEquals(RegionState.State.OPEN,
    MetaTableLocator.getMetaRegionState(this.watcher).getState());

  MetaTableLocator.deleteMetaLocation(this.watcher);
  assertNull(MetaTableLocator.getMetaRegionState(this.watcher).getServerName());
  assertEquals(RegionState.State.OFFLINE,
    MetaTableLocator.getMetaRegionState(this.watcher).getState());
  assertNull(MetaTableLocator.getMetaRegionLocation(this.watcher));
}
 
Example 6
Source File: HBCK2.java    From hbase-operator-tools with Apache License 2.0 5 votes vote down vote up
int setRegionState(ClusterConnection connection, String region,
      RegionState.State newState)
    throws IOException {
  if (newState == null) {
    throw new IllegalArgumentException("State can't be null.");
  }
  RegionState.State currentState = null;
  Table table = connection.getTable(TableName.valueOf("hbase:meta"));
  RowFilter filter = new RowFilter(CompareOperator.EQUAL, new SubstringComparator(region));
  Scan scan = new Scan();
  scan.setFilter(filter);
  Result result = table.getScanner(scan).next();
  if (result != null) {
    byte[] currentStateValue = result.getValue(HConstants.CATALOG_FAMILY,
      HConstants.STATE_QUALIFIER);
    if (currentStateValue == null) {
      System.out.println("WARN: Region state info on meta was NULL");
    } else {
      currentState = RegionState.State.valueOf(
          org.apache.hadoop.hbase.util.Bytes.toString(currentStateValue));
    }
    Put put = new Put(result.getRow());
    put.addColumn(HConstants.CATALOG_FAMILY, HConstants.STATE_QUALIFIER,
      org.apache.hadoop.hbase.util.Bytes.toBytes(newState.name()));
    table.put(put);
    System.out.println("Changed region " + region + " STATE from "
      + currentState + " to " + newState);
    return EXIT_SUCCESS;
  } else {
    System.out.println("ERROR: Could not find region " + region + " in meta.");
  }
  return EXIT_FAILURE;
}
 
Example 7
Source File: TestSerialReplicationChecker.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void addStateAndBarrier(RegionInfo region, RegionState.State state, long... barriers)
    throws IOException {
  Put put = new Put(region.getRegionName(), EnvironmentEdgeManager.currentTime());
  if (state != null) {
    put.addColumn(HConstants.CATALOG_FAMILY, HConstants.STATE_QUALIFIER,
      Bytes.toBytes(state.name()));
  }
  for (int i = 0; i < barriers.length; i++) {
    put.addColumn(HConstants.REPLICATION_BARRIER_FAMILY, HConstants.SEQNUM_QUALIFIER,
      put.getTimestamp() - barriers.length + i, Bytes.toBytes(barriers[i]));
  }
  try (Table table = UTIL.getConnection().getTable(TableName.META_TABLE_NAME)) {
    table.put(put);
  }
}
 
Example 8
Source File: TestRSGroupsAdmin2.java    From hbase with Apache License 2.0 5 votes vote down vote up
private Pair<ServerName, RegionStateNode> randomlySetRegionState(RSGroupInfo groupInfo,
  RegionState.State state, TableName... tableNames) throws IOException {
  Preconditions.checkArgument(tableNames.length == 1 || tableNames.length == 2,
    "only support one or two tables");
  Map<TableName, Map<ServerName, List<String>>> tableServerRegionMap = getTableServerRegionMap();
  Map<ServerName, List<String>> assignMap = tableServerRegionMap.get(tableNames[0]);
  if (tableNames.length == 2) {
    Map<ServerName, List<String>> assignMap2 = tableServerRegionMap.get(tableNames[1]);
    assignMap2.forEach((k, v) -> {
      if (!assignMap.containsKey(k)) {
        assignMap.remove(k);
      }
    });
  }
  String toCorrectRegionName = null;
  ServerName srcServer = null;
  for (ServerName server : assignMap.keySet()) {
    toCorrectRegionName =
      assignMap.get(server).size() >= 1 && !groupInfo.containsServer(server.getAddress())
        ? assignMap.get(server).get(0)
        : null;
    if (toCorrectRegionName != null) {
      srcServer = server;
      break;
    }
  }
  assert srcServer != null;
  RegionInfo toCorrectRegionInfo = TEST_UTIL.getMiniHBaseCluster().getMaster()
    .getAssignmentManager().getRegionInfo(Bytes.toBytesBinary(toCorrectRegionName));
  RegionStateNode rsn = TEST_UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager()
    .getRegionStates().getRegionStateNode(toCorrectRegionInfo);
  rsn.setState(state);
  return new Pair<>(srcServer, rsn);
}
 
Example 9
Source File: MetaBrowser.java    From hbase with Apache License 2.0 5 votes vote down vote up
private static Filter buildScanRegionStateFilter(final RegionState.State state) {
  return new SingleColumnValueFilter(
    HConstants.CATALOG_FAMILY,
    HConstants.STATE_QUALIFIER,
    CompareOperator.EQUAL,
    // use the same serialization strategy as found in MetaTableAccessor#addRegionStateToPut
    Bytes.toBytes(state.name()));
}
 
Example 10
Source File: MetaBrowser.java    From hbase with Apache License 2.0 5 votes vote down vote up
private RegionState.State resolveScanRegionState(final HttpServletRequest request) {
  final String requestValueStr = resolveRequestParameter(request, SCAN_REGION_STATE_PARAM);
  if (requestValueStr == null) {
    return null;
  }
  final RegionState.State requestValue = tryValueOf(RegionState.State.class, requestValueStr);
  if (requestValue == null) {
    errorMessages.add(buildScanRegionStateMalformedErrorMessage(requestValueStr));
    return null;
  }
  return requestValue;
}
 
Example 11
Source File: MetaTableLocator.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Sets the location of <code>hbase:meta</code> in ZooKeeper to the specified server address.
 * @param zookeeper reference to the {@link ZKWatcher} which also contains configuration and
 *                  operation
 * @param serverName the name of the server
 * @param replicaId the ID of the replica
 * @param state the state of the region
 * @throws KeeperException if a ZooKeeper operation fails
 */
public static void setMetaLocation(ZKWatcher zookeeper, ServerName serverName, int replicaId,
    RegionState.State state) throws KeeperException {
  if (serverName == null) {
    LOG.warn("Tried to set null ServerName in hbase:meta; skipping -- ServerName required");
    return;
  }
  LOG.info("Setting hbase:meta (replicaId={}) location in ZooKeeper as {}", replicaId,
    serverName);
  // Make the MetaRegionServer pb and then get its bytes and save this as
  // the znode content.
  MetaRegionServer pbrsr = MetaRegionServer.newBuilder()
    .setServer(ProtobufUtil.toServerName(serverName))
    .setRpcVersion(HConstants.RPC_CURRENT_VERSION)
    .setState(state.convert()).build();
  byte[] data = ProtobufUtil.prependPBMagic(pbrsr.toByteArray());
  try {
    ZKUtil.setData(zookeeper,
        zookeeper.getZNodePaths().getZNodeForReplica(replicaId), data);
  } catch(KeeperException.NoNodeException nne) {
    if (replicaId == RegionInfo.DEFAULT_REPLICA_ID) {
      LOG.debug("META region location doesn't exist, create it");
    } else {
      LOG.debug("META region location doesn't exist for replicaId=" + replicaId +
          ", create it");
    }
    ZKUtil.createAndWatch(zookeeper, zookeeper.getZNodePaths().getZNodeForReplica(replicaId),
            data);
  }
}
 
Example 12
Source File: AssignmentManager.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void preTransitCheck(RegionStateNode regionNode, RegionState.State[] expectedStates)
    throws HBaseIOException {
  if (regionNode.getProcedure() != null) {
    throw new HBaseIOException(regionNode + " is currently in transition");
  }
  if (!regionNode.isInState(expectedStates)) {
    throw new DoNotRetryRegionException("Unexpected state for " + regionNode);
  }
  if (isTableDisabled(regionNode.getTable())) {
    throw new DoNotRetryIOException(regionNode.getTable() + " is disabled for " + regionNode);
  }
}
 
Example 13
Source File: RegionStates.java    From hbase with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 14
Source File: MetaTableAccessor.java    From hbase with Apache License 2.0 5 votes vote down vote up
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;
}
 
Example 15
Source File: ZKConnectionRegistry.java    From hbase with Apache License 2.0 5 votes vote down vote up
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()));
}
 
Example 16
Source File: TestMetaBrowser.java    From hbase with Apache License 2.0 4 votes vote down vote up
public MockRequestBuilder setRegionState(final RegionState.State value) {
  this.regionState = value.toString();
  return this;
}
 
Example 17
Source File: MetaBrowser.java    From hbase with Apache License 2.0 4 votes vote down vote up
public RegionState.State getScanRegionState() {
  return scanRegionState;
}
 
Example 18
Source File: MetaTableAccessor.java    From hbase with Apache License 2.0 4 votes vote down vote up
public RegionState.State getState() {
  return state;
}
 
Example 19
Source File: MetaTableAccessor.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Update state column in hbase:meta.
 */
public static void updateRegionState(Connection connection, RegionInfo ri,
  RegionState.State state) throws IOException {
  Put put = new Put(RegionReplicaUtil.getRegionInfoForDefaultReplica(ri).getRegionName());
  putsToMetaTable(connection, Collections.singletonList(addRegionStateToPut(put, state)));
}
 
Example 20
Source File: Hbck.java    From hbase with Apache License 2.0 2 votes vote down vote up
/**
 * Update region state in Meta only. No procedures are submitted to manipulate the given region or
 * any other region from same table.
 * @param nameOrEncodedName2State list of all region states to be updated in meta
 * @return previous state of the region in Meta
 */
Map<String, RegionState.State>
  setRegionStateInMeta(Map<String, RegionState.State> nameOrEncodedName2State) throws IOException;