Java Code Examples for org.apache.hadoop.hbase.HRegionLocation#getServerName()

The following examples show how to use org.apache.hadoop.hbase.HRegionLocation#getServerName() . 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: ThriftUtilities.java    From hbase with Apache License 2.0 6 votes vote down vote up
public static THRegionLocation regionLocationFromHBase(HRegionLocation hrl) {
  RegionInfo hri = hrl.getRegion();
  ServerName serverName = hrl.getServerName();

  THRegionInfo thRegionInfo = new THRegionInfo();
  THRegionLocation thRegionLocation = new THRegionLocation();
  TServerName tServerName = new TServerName();

  tServerName.setHostName(serverName.getHostname());
  tServerName.setPort(serverName.getPort());
  tServerName.setStartCode(serverName.getStartcode());

  thRegionInfo.setTableName(hri.getTable().getName());
  thRegionInfo.setEndKey(hri.getEndKey());
  thRegionInfo.setStartKey(hri.getStartKey());
  thRegionInfo.setOffline(hri.isOffline());
  thRegionInfo.setSplit(hri.isSplit());
  thRegionInfo.setReplicaId(hri.getReplicaId());

  thRegionLocation.setRegionInfo(thRegionInfo);
  thRegionLocation.setServerName(tServerName);

  return thRegionLocation;
}
 
Example 2
Source File: CanaryTool.java    From hbase with Apache License 2.0 6 votes vote down vote up
private static List<Future<Void>> sniff(final Admin admin, final Sink sink,
    TableDescriptor tableDesc, ExecutorService executor, TaskType taskType,
    boolean rawScanEnabled, LongAdder rwLatency, boolean readAllCF) throws Exception {
  LOG.debug("Reading list of regions for table {}", tableDesc.getTableName());
  try (Table table = admin.getConnection().getTable(tableDesc.getTableName())) {
    List<RegionTask> tasks = new ArrayList<>();
    try (RegionLocator regionLocator =
             admin.getConnection().getRegionLocator(tableDesc.getTableName())) {
      for (HRegionLocation location: regionLocator.getAllRegionLocations()) {
        if (location == null) {
          LOG.warn("Null location");
          continue;
        }
        ServerName rs = location.getServerName();
        RegionInfo region = location.getRegion();
        tasks.add(new RegionTask(admin.getConnection(), region, rs, (RegionStdOutSink)sink,
            taskType, rawScanEnabled, rwLatency, readAllCF));
        Map<String, List<RegionTaskResult>> regionMap = ((RegionStdOutSink) sink).getRegionMap();
        regionMap.put(region.getRegionNameAsString(), new ArrayList<RegionTaskResult>());
      }
      return executor.invokeAll(tasks);
    }
  } catch (TableNotFoundException e) {
    return Collections.EMPTY_LIST;
  }
}
 
Example 3
Source File: TestShutdownOfMetaReplicaHolder.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testShutdownOfReplicaHolder() throws Exception {
  // checks that the when the server holding meta replica is shut down, the meta replica
  // can be recovered
  try (Connection conn = ConnectionFactory.createConnection(TEST_UTIL.getConfiguration());
    RegionLocator locator = conn.getRegionLocator(TableName.META_TABLE_NAME)) {
    HRegionLocation hrl = locator.getRegionLocations(HConstants.EMPTY_START_ROW, true).get(1);
    ServerName oldServer = hrl.getServerName();
    TEST_UTIL.getHBaseClusterInterface().killRegionServer(oldServer);
    LOG.debug("Waiting for the replica {} to come up", hrl.getRegion());
    TEST_UTIL.waitFor(30000, () -> {
      HRegionLocation loc = locator.getRegionLocations(HConstants.EMPTY_START_ROW, true).get(1);
      return loc != null && !loc.getServerName().equals(oldServer);
    });
    LOG.debug("Replica {} is online on {}, old server is {}", hrl.getRegion(),
      locator.getRegionLocations(HConstants.EMPTY_START_ROW, true).get(1).getServerName(),
      oldServer);
  }
}
 
Example 4
Source File: BaseResultIterators.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private List<Scan> addNewScan(List<List<Scan>> parallelScans, List<Scan> scans, Scan scan,
        byte[] startKey, boolean crossedRegionBoundary, HRegionLocation regionLocation) {
    boolean startNewScan = scanGrouper.shouldStartNewScan(plan, scans, startKey, crossedRegionBoundary);
    if (scan != null) {
        if (regionLocation.getServerName() != null) {
            scan.setAttribute(BaseScannerRegionObserver.SCAN_REGION_SERVER, regionLocation.getServerName().getVersionedBytes());
        }
        if (useStatsForParallelization || crossedRegionBoundary) {
            scans.add(scan);
        }
    }
    if (startNewScan && !scans.isEmpty()) {
        parallelScans.add(scans);
        scans = Lists.newArrayListWithExpectedSize(1);
    }
    return scans;
}
 
Example 5
Source File: TestAccessController.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testMove() throws Exception {
  List<HRegionLocation> regions;
  try (RegionLocator locator = systemUserConnection.getRegionLocator(TEST_TABLE)) {
    regions = locator.getAllRegionLocations();
  }
  HRegionLocation location = regions.get(0);
  final RegionInfo hri = location.getRegion();
  final ServerName server = location.getServerName();
  AccessTestAction action = new AccessTestAction() {
    @Override
    public Object run() throws Exception {
      ACCESS_CONTROLLER.preMove(ObserverContextImpl.createAndPrepare(CP_ENV),
        hri, server, server);
      return null;
    }
  };

  verifyAllowed(action, SUPERUSER, USER_ADMIN, USER_OWNER, USER_GROUP_ADMIN);
  verifyDenied(action, USER_CREATE, USER_RW, USER_RO, USER_NONE, USER_GROUP_READ,
    USER_GROUP_WRITE, USER_GROUP_CREATE);
}
 
Example 6
Source File: HBaseFsck.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Record the location of the hbase:meta region as found in ZooKeeper.
 */
private boolean recordMetaRegion() throws IOException {
  List<HRegionLocation> locs;
  try (RegionLocator locator = connection.getRegionLocator(TableName.META_TABLE_NAME)) {
    locs = locator.getRegionLocations(HConstants.EMPTY_START_ROW, true);
  }
  if (locs == null || locs.isEmpty()) {
    errors.reportError(ERROR_CODE.NULL_META_REGION, "META region was not found in ZooKeeper");
    return false;
  }
  for (HRegionLocation metaLocation : locs) {
    // Check if Meta region is valid and existing
    if (metaLocation == null) {
      errors.reportError(ERROR_CODE.NULL_META_REGION, "META region location is null");
      return false;
    }
    if (metaLocation.getRegion() == null) {
      errors.reportError(ERROR_CODE.NULL_META_REGION, "META location regionInfo is null");
      return false;
    }
    if (metaLocation.getHostname() == null) {
      errors.reportError(ERROR_CODE.NULL_META_REGION, "META location hostName is null");
      return false;
    }
    ServerName sn = metaLocation.getServerName();
    HbckRegionInfo.MetaEntry m = new HbckRegionInfo.MetaEntry(metaLocation.getRegion(), sn,
        EnvironmentEdgeManager.currentTime());
    HbckRegionInfo hbckRegionInfo = regionInfoMap.get(metaLocation.getRegion().getEncodedName());
    if (hbckRegionInfo == null) {
      regionInfoMap.put(metaLocation.getRegion().getEncodedName(), new HbckRegionInfo(m));
    } else {
      hbckRegionInfo.setMetaEntry(m);
    }
  }
  return true;
}
 
Example 7
Source File: AsyncRegionLocatorHelper.java    From hbase with Apache License 2.0 5 votes vote down vote up
static boolean canUpdateOnError(HRegionLocation loc, HRegionLocation oldLoc) {
  // Do not need to update if no such location, or the location is newer, or the location is not
  // the same with us
  if (loc == null || loc.getServerName() == null) {
    return false;
  }
  if (oldLoc == null || oldLoc.getServerName() == null) {
    return false;
  }
  return oldLoc.getSeqNum() <= loc.getSeqNum() &&
    oldLoc.getServerName().equals(loc.getServerName());
}
 
Example 8
Source File: ProtobufUtil.java    From hbase with Apache License 2.0 5 votes vote down vote up
public static HBaseProtos.RegionLocation toRegionLocation(HRegionLocation loc) {
  HBaseProtos.RegionLocation.Builder builder = HBaseProtos.RegionLocation.newBuilder();
  builder.setRegionInfo(toRegionInfo(loc.getRegion()));
  if (loc.getServerName() != null) {
    builder.setServerName(toServerName(loc.getServerName()));
  }
  builder.setSeqNum(loc.getSeqNum());
  return builder.build();
}
 
Example 9
Source File: MetaDataUtil.java    From phoenix with Apache License 2.0 5 votes vote down vote up
/**
 * This function checks if all regions of a table is online
 * @param table
 * @return true when all regions of a table are online
 * @throws IOException
 * @throws
 */
public static boolean tableRegionsOnline(Configuration conf, PTable table) {
    try (ClusterConnection hcon =
            (ClusterConnection) ConnectionFactory.createConnection(conf)) {
        List<HRegionLocation> locations = hcon.locateRegions(
          org.apache.hadoop.hbase.TableName.valueOf(table.getPhysicalName().getBytes()));

        for (HRegionLocation loc : locations) {
            try {
                ServerName sn = loc.getServerName();
                if (sn == null) continue;

                AdminService.BlockingInterface admin = hcon.getAdmin(sn);
                HBaseRpcController controller = hcon.getRpcControllerFactory().newController();
                org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil.getRegionInfo(controller,
                    admin, loc.getRegion().getRegionName());
            } catch (RemoteException e) {
                LOGGER.debug("Cannot get region " + loc.getRegion().getEncodedName() + " info due to error:" + e);
                return false;
            }
        }
    } catch (IOException ex) {
        LOGGER.warn("tableRegionsOnline failed due to:", ex);
        return false;
    }
    return true;
}
 
Example 10
Source File: TestAdmin.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void verifyRoundRobinDistribution(RegionLocator regionLocator, int expectedRegions)
    throws IOException {
  int numRS = TEST_UTIL.getMiniHBaseCluster().getNumLiveRegionServers();
  List<HRegionLocation> regions = regionLocator.getAllRegionLocations();
  Map<ServerName, List<RegionInfo>> server2Regions = new HashMap<>();
  for (HRegionLocation loc : regions) {
    ServerName server = loc.getServerName();
    List<RegionInfo> regs = server2Regions.get(server);
    if (regs == null) {
      regs = new ArrayList<>();
      server2Regions.put(server, regs);
    }
    regs.add(loc.getRegion());
  }
  boolean tablesOnMaster = LoadBalancer.isTablesOnMaster(TEST_UTIL.getConfiguration());
  if (tablesOnMaster) {
    // Ignore the master region server,
    // which contains less regions by intention.
    numRS--;
  }
  float average = (float) expectedRegions / numRS;
  int min = (int) Math.floor(average);
  int max = (int) Math.ceil(average);
  for (List<RegionInfo> regionList : server2Regions.values()) {
    assertTrue(
      "numRS=" + numRS + ", min=" + min + ", max=" + max + ", size=" + regionList.size() +
        ", tablesOnMaster=" + tablesOnMaster,
      regionList.size() == min || regionList.size() == max);
  }
}
 
Example 11
Source File: HBaseDataFragmenter.java    From pxf with Apache License 2.0 5 votes vote down vote up
private void addFragment(HRegionLocation location,
        byte[] userData) throws IOException {
    ServerName serverInfo = location.getServerName();
    String[] hosts = new String[] {serverInfo.getHostname()};
    HRegionInfo region = location.getRegionInfo();
    byte[] fragmentMetadata = prepareFragmentMetadata(region);
    Fragment fragment = new Fragment(context.getDataSource(), hosts, fragmentMetadata, userData);
    fragments.add(fragment);
}
 
Example 12
Source File: MajorCompactor.java    From hbase with Apache License 2.0 5 votes vote down vote up
private Map<ServerName, List<RegionInfo>> getServerRegionsMap() throws IOException {
  Map<ServerName, List<RegionInfo>> snRegionMap = Maps.newHashMap();
  List<HRegionLocation> regionLocations =
      connection.getRegionLocator(tableName).getAllRegionLocations();
  for (HRegionLocation regionLocation : regionLocations) {
    ServerName sn = regionLocation.getServerName();
    RegionInfo hri = regionLocation.getRegion();
    if (!snRegionMap.containsKey(sn)) {
      snRegionMap.put(sn, Lists.newArrayList());
    }
    snRegionMap.get(sn).add(hri);
  }
  return snRegionMap;
}
 
Example 13
Source File: RegionMover.java    From hbase with Apache License 2.0 5 votes vote down vote up
/**
 * Get servername that is up in hbase:meta hosting the given region. this is hostname + port +
 * startcode comma-delimited. Can return null
 * @return regionServer hosting the given region
 */
private ServerName getServerNameForRegion(RegionInfo region) throws IOException {
  if (!admin.isTableEnabled(region.getTable())) {
    return null;
  }
  HRegionLocation loc =
    conn.getRegionLocator(region.getTable()).getRegionLocation(region.getStartKey(),
      region.getReplicaId(),true);
  if (loc != null) {
    return loc.getServerName();
  } else {
    return null;
  }
}
 
Example 14
Source File: RegionReplicaInfo.java    From hbase with Apache License 2.0 5 votes vote down vote up
private RegionReplicaInfo(final Result result, final HRegionLocation location) {
  this.row = result != null ? result.getRow() : null;
  this.regionInfo = location != null ? location.getRegion() : null;
  this.regionState = (result != null && regionInfo != null)
    ? RegionStateStore.getRegionState(result, regionInfo)
    : null;
  this.serverName = location != null ? location.getServerName() : null;
  this.seqNum = (location != null) ? location.getSeqNum() : HConstants.NO_SEQNUM;
  this.targetServerName = (result != null && regionInfo != null)
    ? MetaTableAccessor.getTargetServerName(result, regionInfo.getReplicaId())
    : null;
  this.mergeRegionInfo = (result != null)
    ? MetaTableAccessor.getMergeRegionsWithName(result.rawCells())
    : null;

  if (result != null) {
    PairOfSameType<RegionInfo> daughterRegions = MetaTableAccessor.getDaughterRegions(result);
    this.splitRegionInfo = new LinkedHashMap<>();
    if (daughterRegions.getFirst() != null) {
      splitRegionInfo.put(HConstants.SPLITA_QUALIFIER_STR, daughterRegions.getFirst());
    }
    if (daughterRegions.getSecond() != null) {
      splitRegionInfo.put(HConstants.SPLITB_QUALIFIER_STR, daughterRegions.getSecond());
    }
  } else {
    this.splitRegionInfo = null;
  }
}
 
Example 15
Source File: RegionStateStore.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void visitMetaEntry(final RegionStateVisitor visitor, final Result result)
    throws IOException {
  final RegionLocations rl = CatalogFamilyFormat.getRegionLocations(result);
  if (rl == null) return;

  final HRegionLocation[] locations = rl.getRegionLocations();
  if (locations == null) return;

  for (int i = 0; i < locations.length; ++i) {
    final HRegionLocation hrl = locations[i];
    if (hrl == null) continue;

    final RegionInfo regionInfo = hrl.getRegion();
    if (regionInfo == null) continue;

    final int replicaId = regionInfo.getReplicaId();
    final State state = getRegionState(result, regionInfo);

    final ServerName lastHost = hrl.getServerName();
    ServerName regionLocation = MetaTableAccessor.getTargetServerName(result, replicaId);
    final long openSeqNum = hrl.getSeqNum();

    // TODO: move under trace, now is visible for debugging
    LOG.info(
      "Load hbase:meta entry region={}, regionState={}, lastHost={}, " +
        "regionLocation={}, openSeqNum={}",
      regionInfo.getEncodedName(), state, lastHost, regionLocation, openSeqNum);
    visitor.visitRegionState(result, regionInfo, state, regionLocation, lastHost, openSeqNum);
  }
}
 
Example 16
Source File: AsyncRegionLocatorHelper.java    From hbase with Apache License 2.0 5 votes vote down vote up
static boolean isGood(RegionLocations locs, int replicaId) {
  if (locs == null) {
    return false;
  }
  HRegionLocation loc = locs.getRegionLocation(replicaId);
  return loc != null && loc.getServerName() != null;
}
 
Example 17
Source File: HBaseFsck.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Attempts to undeploy a region from a region server based in information in
 * META.  Any operations that modify the file system should make sure that
 * its corresponding region is not deployed to prevent data races.
 *
 * A separate call is required to update the master in-memory region state
 * kept in the AssignementManager.  Because disable uses this state instead of
 * that found in META, we can't seem to cleanly disable/delete tables that
 * have been hbck fixed.  When used on a version of HBase that does not have
 * the offline ipc call exposed on the master (&lt;0.90.5, &lt;0.92.0) a master
 * restart or failover may be required.
 */
void closeRegion(HbckRegionInfo hi) throws IOException, InterruptedException {
  if (hi.getMetaEntry() == null && hi.getHdfsEntry() == null) {
    undeployRegions(hi);
    return;
  }

  // get assignment info and hregioninfo from meta.
  Get get = new Get(hi.getRegionName());
  get.addColumn(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER);
  get.addColumn(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER);
  get.addColumn(HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER);
  // also get the locations of the replicas to close if the primary region is being closed
  if (hi.getReplicaId() == RegionInfo.DEFAULT_REPLICA_ID) {
    int numReplicas = admin.getDescriptor(hi.getTableName()).getRegionReplication();
    for (int i = 0; i < numReplicas; i++) {
      get.addColumn(HConstants.CATALOG_FAMILY, CatalogFamilyFormat.getServerColumn(i));
      get.addColumn(HConstants.CATALOG_FAMILY, CatalogFamilyFormat.getStartCodeColumn(i));
    }
  }
  Result r = meta.get(get);
  RegionLocations rl = CatalogFamilyFormat.getRegionLocations(r);
  if (rl == null) {
    LOG.warn("Unable to close region " + hi.getRegionNameAsString() +
        " since meta does not have handle to reach it");
    return;
  }
  for (HRegionLocation h : rl.getRegionLocations()) {
    ServerName serverName = h.getServerName();
    if (serverName == null) {
      errors.reportError("Unable to close region "
          + hi.getRegionNameAsString() +  " because meta does not "
          + "have handle to reach it.");
      continue;
    }
    RegionInfo hri = h.getRegion();
    if (hri == null) {
      LOG.warn("Unable to close region " + hi.getRegionNameAsString()
          + " because hbase:meta had invalid or missing "
          + HConstants.CATALOG_FAMILY_STR + ":"
          + Bytes.toString(HConstants.REGIONINFO_QUALIFIER)
          + " qualifier value.");
      continue;
    }
    // close the region -- close files and remove assignment
    HBaseFsckRepair.closeRegionSilentlyAndWait(connection, serverName, hri);
  }
}
 
Example 18
Source File: HBaseFsck.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Scan hbase:meta, adding all regions found to the regionInfo map.
 * @throws IOException if an error is encountered
 */
boolean loadMetaEntries() throws IOException {
  ClientMetaTableAccessor.Visitor visitor = new ClientMetaTableAccessor.Visitor() {
    int countRecord = 1;

    // comparator to sort KeyValues with latest modtime
    final Comparator<Cell> comp = new Comparator<Cell>() {
      @Override
      public int compare(Cell k1, Cell k2) {
        return Long.compare(k1.getTimestamp(), k2.getTimestamp());
      }
    };

    @Override
    public boolean visit(Result result) throws IOException {
      try {

        // record the latest modification of this META record
        long ts =  Collections.max(result.listCells(), comp).getTimestamp();
        RegionLocations rl = CatalogFamilyFormat.getRegionLocations(result);
        if (rl == null) {
          emptyRegionInfoQualifiers.add(result);
          errors.reportError(ERROR_CODE.EMPTY_META_CELL,
            "Empty REGIONINFO_QUALIFIER found in hbase:meta");
          return true;
        }
        ServerName sn = null;
        if (rl.getRegionLocation(RegionInfo.DEFAULT_REPLICA_ID) == null ||
            rl.getRegionLocation(RegionInfo.DEFAULT_REPLICA_ID).getRegion() == null) {
          emptyRegionInfoQualifiers.add(result);
          errors.reportError(ERROR_CODE.EMPTY_META_CELL,
            "Empty REGIONINFO_QUALIFIER found in hbase:meta");
          return true;
        }
        RegionInfo hri = rl.getRegionLocation(RegionInfo.DEFAULT_REPLICA_ID).getRegion();
        if (!(isTableIncluded(hri.getTable())
            || hri.isMetaRegion())) {
          return true;
        }
        PairOfSameType<RegionInfo> daughters = MetaTableAccessor.getDaughterRegions(result);
        for (HRegionLocation h : rl.getRegionLocations()) {
          if (h == null || h.getRegion() == null) {
            continue;
          }
          sn = h.getServerName();
          hri = h.getRegion();

          HbckRegionInfo.MetaEntry m = null;
          if (hri.getReplicaId() == RegionInfo.DEFAULT_REPLICA_ID) {
            m = new HbckRegionInfo.MetaEntry(hri, sn, ts, daughters.getFirst(),
                daughters.getSecond());
          } else {
            m = new HbckRegionInfo.MetaEntry(hri, sn, ts, null, null);
          }
          HbckRegionInfo previous = regionInfoMap.get(hri.getEncodedName());
          if (previous == null) {
            regionInfoMap.put(hri.getEncodedName(), new HbckRegionInfo(m));
          } else if (previous.getMetaEntry() == null) {
            previous.setMetaEntry(m);
          } else {
            throw new IOException("Two entries in hbase:meta are same " + previous);
          }
        }
        List<RegionInfo> mergeParents = MetaTableAccessor.getMergeRegions(result.rawCells());
        if (mergeParents != null) {
          for (RegionInfo mergeRegion : mergeParents) {
            if (mergeRegion != null) {
              // This region is already being merged
              HbckRegionInfo hbInfo = getOrCreateInfo(mergeRegion.getEncodedName());
              hbInfo.setMerged(true);
            }
          }
        }

        // show proof of progress to the user, once for every 100 records.
        if (countRecord % 100 == 0) {
          errors.progress();
        }
        countRecord++;
        return true;
      } catch (RuntimeException e) {
        LOG.error("Result=" + result);
        throw e;
      }
    }
  };
  if (!checkMetaOnly) {
    // Scan hbase:meta to pick up user regions
    MetaTableAccessor.fullScanRegions(connection, visitor);
  }

  errors.print("");
  return true;
}
 
Example 19
Source File: TestAccessController.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void testGlobalAuthorizationForNewRegisteredRS() throws Exception {
  LOG.debug("Test for global authorization for a new registered RegionServer.");
  MiniHBaseCluster hbaseCluster = TEST_UTIL.getHBaseCluster();

  final Admin admin = TEST_UTIL.getAdmin();
  TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
    new TableDescriptorBuilder.ModifyableTableDescriptor(TEST_TABLE2);
  tableDescriptor.setColumnFamily(
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(TEST_FAMILY));
  createTable(TEST_UTIL, tableDescriptor);

  // Starting a new RegionServer.
  JVMClusterUtil.RegionServerThread newRsThread = hbaseCluster
      .startRegionServer();
  final HRegionServer newRs = newRsThread.getRegionServer();

  // Move region to the new RegionServer.
  List<HRegionLocation> regions;
  try (RegionLocator locator = systemUserConnection.getRegionLocator(TEST_TABLE2)) {
    regions = locator.getAllRegionLocations();
  }
  HRegionLocation location = regions.get(0);
  final RegionInfo hri = location.getRegion();
  final ServerName server = location.getServerName();
  try (Table table = systemUserConnection.getTable(TEST_TABLE2)) {
    AccessTestAction moveAction = new AccessTestAction() {
      @Override
      public Object run() throws Exception {
        admin.move(hri.getEncodedNameAsBytes(), newRs.getServerName());
        return null;
      }
    };
    SUPERUSER.runAs(moveAction);

    final int RETRIES_LIMIT = 10;
    int retries = 0;
    while (newRs.getRegions(TEST_TABLE2).size() < 1 && retries < RETRIES_LIMIT) {
      LOG.debug("Waiting for region to be opened. Already retried " + retries
          + " times.");
      try {
        Thread.sleep(1000);
      } catch (InterruptedException e) {
      }
      retries++;
      if (retries == RETRIES_LIMIT - 1) {
        fail("Retry exhaust for waiting region to be opened.");
      }
    }
    // Verify write permission for user "admin2" who has the global
    // permissions.
    AccessTestAction putAction = new AccessTestAction() {
      @Override
      public Object run() throws Exception {
        Put put = new Put(Bytes.toBytes("test"));
        put.addColumn(TEST_FAMILY, Bytes.toBytes("qual"), Bytes.toBytes("value"));
        table.put(put);
        return null;
      }
    };
    USER_ADMIN.runAs(putAction);
  }
}
 
Example 20
Source File: HBCKMetaTableAccessor.java    From hbase-operator-tools with Apache License 2.0 4 votes vote down vote up
/**
 * Returns an HRegionLocationList extracted from the result.
 * (Copied from MetaTableAccessor)
 * @return an HRegionLocationList containing all locations for the region range or null if
 *   we can't deserialize the result.
 */
public static RegionLocations getRegionLocations(final Result r) {
  if (r == null) {
    return null;
  }
  RegionInfo regionInfo = getRegionInfo(r, REGIONINFO_QUALIFIER);
  if (regionInfo == null) {
    return null;
  }

  List<HRegionLocation> locations = new ArrayList<>(1);
  NavigableMap<byte[], NavigableMap<byte[],byte[]>> familyMap = r.getNoVersionMap();

  locations.add(getRegionLocation(r, regionInfo, 0));

  NavigableMap<byte[], byte[]> infoMap = familyMap.get(CATALOG_FAMILY);
  if (infoMap == null) {
    return new RegionLocations(locations);
  }

  // iterate until all serverName columns are seen
  int replicaId = 0;
  byte[] serverColumn = getServerColumn(replicaId);
  SortedMap<byte[], byte[]> serverMap;
  serverMap = infoMap.tailMap(serverColumn, false);

  if (serverMap.isEmpty()) {
    return new RegionLocations(locations);
  }

  for (Map.Entry<byte[], byte[]> entry : serverMap.entrySet()) {
    replicaId = parseReplicaIdFromServerColumn(entry.getKey());
    if (replicaId < 0) {
      break;
    }
    HRegionLocation location = getRegionLocation(r, regionInfo, replicaId);
    // In case the region replica is newly created, it's location might be null. We usually do not
    // have HRL's in RegionLocations object with null ServerName. They are handled as null HRLs.
    if (location.getServerName() == null) {
      locations.add(null);
    } else {
      locations.add(location);
    }
  }

  return new RegionLocations(locations);
}