org.apache.hadoop.hbase.master.RegionState Java Examples

The following examples show how to use org.apache.hadoop.hbase.master.RegionState. 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: TestMetaBrowserNoCluster.java    From hbase with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #2
Source File: TestMetaTableLocator.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * 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));
}
 
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: TestSerialReplicationChecker.java    From hbase with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #5
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 #6
Source File: MetaTableAccessor.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * 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());
}
 
Example #7
Source File: IndexLoadBalancer.java    From phoenix with Apache License 2.0 6 votes vote down vote up
/**
 * 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());
            }
        }
    }
}
 
Example #8
Source File: RestoreSnapshotProcedure.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
      }
    }
  }
}
 
Example #9
Source File: TestRegionStateStore.java    From hbase with Apache License 2.0 6 votes vote down vote up
@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());
}
 
Example #10
Source File: TestSerialReplicationChecker.java    From hbase with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #11
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 #12
Source File: TestHBCKMetaTableAccessor.java    From hbase-operator-tools with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #13
Source File: TestFsRegionsMetaRecoverer.java    From hbase-operator-tools with Apache License 2.0 6 votes vote down vote up
@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));
}
 
Example #14
Source File: ClusterMetricsBuilder.java    From hbase with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example #15
Source File: TestHBaseFsckCleanReplicationBarriers.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 #16
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 #17
Source File: TestRSGroupsAdmin2.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Randomly choose a region to set state.
 * @param newGroup target group
 * @return source server of region, and region state
 * @throws IOException if methods called throw
 */
private Pair<ServerName, RegionStateNode>
  randomlySetOneRegionStateToSplitting(RSGroupInfo newGroup) throws IOException {
  // get target server to move, which should has more than one regions
  // randomly set a region state to SPLITTING to make move fail
  return randomlySetRegionState(newGroup, RegionState.State.SPLITTING, tableName);
}
 
Example #18
Source File: HBCKServerCrashProcedure.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public boolean visit(Result result) throws IOException {
  RegionLocations rls = CatalogFamilyFormat.getRegionLocations(result);
  if (rls == null) {
    return true;
  }
  for (HRegionLocation hrl: rls.getRegionLocations()) {
    if (hrl == null) {
      continue;
    }
    if (hrl.getRegion() == null) {
      continue;
    }
    if (hrl.getServerName() == null) {
      continue;
    }
    if (!hrl.getServerName().equals(this.unknownServerName)) {
      continue;
    }
    RegionState.State state = RegionStateStore.getRegionState(result, hrl.getRegion());
    RegionState rs = new RegionState(hrl.getRegion(), state, hrl.getServerName());
    if (rs.isClosing()) {
      // Move region to CLOSED in hbase:meta.
      LOG.info("Moving {} from CLOSING to CLOSED in hbase:meta",
          hrl.getRegion().getRegionNameAsString());
      try {
        MetaTableAccessor.updateRegionState(this.connection, hrl.getRegion(),
            RegionState.State.CLOSED);
      } catch (IOException ioe) {
        LOG.warn("Failed moving {} from CLOSING to CLOSED",
          hrl.getRegion().getRegionNameAsString(), ioe);
      }
    } else if (rs.isOpening() || rs.isOpened()) {
      this.reassigns.add(hrl.getRegion());
    } else {
      LOG.info("Passing {}", rs);
    }
  }
  return true;
}
 
Example #19
Source File: TestSerialReplicationChecker.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testCanPushUnder() throws IOException, ReplicationException {
  RegionInfo region = RegionInfoBuilder.newBuilder(tableName).build();
  addStateAndBarrier(region, RegionState.State.OPEN, 10, 100);
  updatePushedSeqId(region, 9);
  Cell cell = createCell(region);
  assertTrue(checker.canPush(createEntry(region, 20), cell));
  verify(conn, times(1)).getTable(any(TableName.class));
  // not continuous
  for (int i = 22; i < 100; i += 2) {
    assertTrue(checker.canPush(createEntry(region, i), cell));
  }
  // verify that we do not go to meta table
  verify(conn, times(1)).getTable(any(TableName.class));
}
 
Example #20
Source File: MetaTableLocator.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the meta region location, if available.  Does not block.
 * @param zkw reference to the {@link ZKWatcher} which also contains configuration and operation
 * @param replicaId the ID of the replica
 * @return server name
 */
public static ServerName getMetaRegionLocation(final ZKWatcher zkw, int replicaId) {
  try {
    RegionState state = getMetaRegionState(zkw, replicaId);
    return state.isOpened() ? state.getServerName() : null;
  } catch (KeeperException ke) {
    return null;
  }
}
 
Example #21
Source File: HBaseFsckRepair.java    From hbase with Apache License 2.0 5 votes vote down vote up
public static void waitUntilAssigned(Admin admin,
    RegionInfo region) throws IOException, InterruptedException {
  long timeout = admin.getConfiguration().getLong("hbase.hbck.assign.timeout", 120000);
  long expiration = timeout + EnvironmentEdgeManager.currentTime();
  while (EnvironmentEdgeManager.currentTime() < 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");
}
 
Example #22
Source File: TestSerialReplicationChecker.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void setState(RegionInfo region, RegionState.State state) throws IOException {
  Put put = new Put(region.getRegionName(), EnvironmentEdgeManager.currentTime());
  put.addColumn(HConstants.CATALOG_FAMILY, HConstants.STATE_QUALIFIER,
    Bytes.toBytes(state.name()));
  try (Table table = UTIL.getConnection().getTable(TableName.META_TABLE_NAME)) {
    table.put(put);
  }
}
 
Example #23
Source File: ProtobufUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Get the Meta region state from the passed data bytes. Can handle both old and new style
 * server names.
 * @param data protobuf serialized data with meta server name.
 * @param replicaId replica ID for this region
 * @return RegionState instance corresponding to the serialized data.
 * @throws DeserializationException if the data is invalid.
 */
public static RegionState parseMetaRegionStateFrom(final byte[] data, int replicaId)
    throws DeserializationException {
  RegionState.State state = RegionState.State.OPEN;
  ServerName serverName;
  if (data != null && data.length > 0 && ProtobufUtil.isPBMagicPrefix(data)) {
    try {
      int prefixLen = ProtobufUtil.lengthOfPBMagic();
      ZooKeeperProtos.MetaRegionServer rl =
          ZooKeeperProtos.MetaRegionServer.parser().parseFrom(data, prefixLen,
              data.length - prefixLen);
      if (rl.hasState()) {
        state = RegionState.State.convert(rl.getState());
      }
      HBaseProtos.ServerName sn = rl.getServer();
      serverName = ServerName.valueOf(
          sn.getHostName(), sn.getPort(), sn.getStartCode());
    } catch (InvalidProtocolBufferException e) {
      throw new DeserializationException("Unable to parse meta region location");
    }
  } else {
    // old style of meta region location?
    serverName = parseServerNameFrom(data);
  }
  if (serverName == null) {
    state = RegionState.State.OFFLINE;
  }
  return new RegionState(RegionReplicaUtil.getRegionInfoForReplica(
      RegionInfoBuilder.FIRST_META_REGIONINFO, replicaId), state, serverName);
}
 
Example #24
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 #25
Source File: TestSerialReplicationChecker.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testCanPushEqualsToBarrier() throws IOException, ReplicationException {
  // For binary search, equals to an element will result to a positive value, let's test whether
  // it works.
  RegionInfo region = RegionInfoBuilder.newBuilder(tableName).build();
  addStateAndBarrier(region, RegionState.State.OPEN, 10, 100);
  Cell cell = createCell(region);
  assertTrue(checker.canPush(createEntry(region, 10), cell));
  assertFalse(checker.canPush(createEntry(region, 100), cell));
  updatePushedSeqId(region, 99);
  assertTrue(checker.canPush(createEntry(region, 100), cell));
}
 
Example #26
Source File: HBaseFsckRepair.java    From hbase-operator-tools with Apache License 2.0 5 votes vote down vote up
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");
}
 
Example #27
Source File: TestRegionStateStore.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Test
public void testVisitMetaForBadRegionState() throws Exception {
  final TableName tableName = TableName.valueOf("testVisitMetaForBadRegionState");
  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();

  // add the BAD_STATE which does not exist in enum RegionState.State
  Put put = new Put(regions.get(0).getRegionInfo().getRegionName(),
      EnvironmentEdgeManager.currentTime());
  put.addColumn(HConstants.CATALOG_FAMILY, HConstants.STATE_QUALIFIER,
      Bytes.toBytes("BAD_STATE"));

  try (Table table = UTIL.getConnection().getTable(TableName.META_TABLE_NAME)) {
    table.put(put);
  }

  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());
      assertNull(state);
      visitorCalled.set(true);
    }
  });
  assertTrue("Visitor has not been called.", visitorCalled.get());
}
 
Example #28
Source File: AssignmentManager.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
protected void periodicExecute(final MasterProcedureEnv env) {
  final AssignmentManager am = env.getAssignmentManager();

  final RegionInTransitionStat ritStat = am.computeRegionInTransitionStat();
  if (ritStat.hasRegionsOverThreshold()) {
    for (RegionState hri: ritStat.getRegionOverThreshold()) {
      am.handleRegionOverStuckWarningThreshold(hri.getRegion());
    }
  }

  // update metrics
  am.updateRegionsInTransitionMetrics(ritStat);
}
 
Example #29
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 #30
Source File: AssignmentManager.java    From hbase with Apache License 2.0 5 votes vote down vote up
public void start() throws IOException, KeeperException {
  if (!running.compareAndSet(false, true)) {
    return;
  }

  LOG.trace("Starting assignment manager");

  // Start the Assignment Thread
  startAssignmentThread();

  // load meta region state
  ZKWatcher zkw = master.getZooKeeper();
  // it could be null in some tests
  if (zkw != null) {
    // here we are still in the early steps of active master startup. There is only one thread(us)
    // can access AssignmentManager and create region node, so here we do not need to lock the
    // region node.
    RegionState regionState = MetaTableLocator.getMetaRegionState(zkw);
    RegionStateNode regionNode =
      regionStates.getOrCreateRegionStateNode(RegionInfoBuilder.FIRST_META_REGIONINFO);
    regionNode.setRegionLocation(regionState.getServerName());
    regionNode.setState(regionState.getState());
    if (regionNode.getProcedure() != null) {
      regionNode.getProcedure().stateLoaded(this, regionNode);
    }
    if (regionState.getServerName() != null) {
      regionStates.addRegionToServer(regionNode);
    }
    setMetaAssigned(regionState.getRegion(), regionState.getState() == State.OPEN);
  }
}