Java Code Examples for org.apache.hadoop.hbase.client.RegionLocator#getRegionLocation()

The following examples show how to use org.apache.hadoop.hbase.client.RegionLocator#getRegionLocation() . 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: TestSequenceIdMonotonicallyIncreasing.java    From hbase with Apache License 2.0 6 votes vote down vote up
@Test
public void testSplit()
    throws IOException, InterruptedException, ExecutionException, TimeoutException {
  try (Table table = createTable(false)) {
    table.put(new Put(Bytes.toBytes(0)).addColumn(CF, CQ, Bytes.toBytes(0)));
    table.put(new Put(Bytes.toBytes(1)).addColumn(CF, CQ, Bytes.toBytes(0)));
  }
  UTIL.flush(NAME);
  HRegionServer rs = UTIL.getRSForFirstRegionInTable(NAME);
  RegionInfo region = UTIL.getMiniHBaseCluster().getRegions(NAME).get(0).getRegionInfo();
  UTIL.getAdmin().splitRegionAsync(region.getRegionName(), Bytes.toBytes(1)).get(1,
    TimeUnit.MINUTES);
  long maxSeqId = getMaxSeqId(rs, region);
  RegionLocator locator = UTIL.getConnection().getRegionLocator(NAME);
  HRegionLocation locA = locator.getRegionLocation(Bytes.toBytes(0), true);
  HRegionLocation locB = locator.getRegionLocation(Bytes.toBytes(1), true);
  assertEquals(maxSeqId + 1, locA.getSeqNum());
  assertEquals(maxSeqId + 1, locB.getSeqNum());
}
 
Example 2
Source File: PhoenixSplitManager.java    From presto with Apache License 2.0 5 votes vote down vote up
private List<InputSplit> generateSplits(QueryPlan queryPlan, List<KeyRange> splits)
        throws IOException
{
    requireNonNull(queryPlan, "queryPlan is null");
    requireNonNull(splits, "splits is null");

    try (org.apache.hadoop.hbase.client.Connection connection = phoenixClient.getHConnection()) {
        RegionLocator regionLocator = connection.getRegionLocator(TableName.valueOf(queryPlan.getTableRef().getTable().getPhysicalName().toString()));
        long regionSize = -1;
        List<InputSplit> inputSplits = new ArrayList<>(splits.size());
        for (List<Scan> scans : queryPlan.getScans()) {
            HRegionLocation location = regionLocator.getRegionLocation(scans.get(0).getStartRow(), false);
            String regionLocation = location.getHostname();

            if (log.isDebugEnabled()) {
                log.debug(
                        "Scan count[%d] : %s ~ %s",
                        scans.size(),
                        Bytes.toStringBinary(scans.get(0).getStartRow()),
                        Bytes.toStringBinary(scans.get(scans.size() - 1).getStopRow()));
                log.debug("First scan : %swith scanAttribute : %s [scanCache, cacheBlock, scanBatch] : [%d, %s, %d] and  regionLocation : %s",
                        scans.get(0), scans.get(0).getAttributesMap(), scans.get(0).getCaching(), scans.get(0).getCacheBlocks(), scans.get(0).getBatch(), regionLocation);
                for (int i = 0, limit = scans.size(); i < limit; i++) {
                    log.debug("EXPECTED_UPPER_REGION_KEY[%d] : %s", i, Bytes.toStringBinary(scans.get(i).getAttribute(EXPECTED_UPPER_REGION_KEY)));
                }
            }
            inputSplits.add(new PhoenixInputSplit(scans, regionSize, regionLocation));
        }
        return inputSplits;
    }
}
 
Example 3
Source File: TestServerCustomProtocol.java    From hbase with Apache License 2.0 5 votes vote down vote up
private void verifyRegionResults(RegionLocator regionLocator, Map<byte[], String> results,
        String expected, byte[] row) throws Exception {
  for (Map.Entry<byte [], String> e: results.entrySet()) {
    LOG.info("row=" + Bytes.toString(row) + ", expected=" + expected +
      ", result key=" + Bytes.toString(e.getKey()) +
      ", value=" + e.getValue());
  }
  HRegionLocation loc = regionLocator.getRegionLocation(row, true);
  byte[] region = loc.getRegion().getRegionName();
  assertTrue("Results should contain region " +
    Bytes.toStringBinary(region) + " for row '" + Bytes.toStringBinary(row)+ "'",
    results.containsKey(region));
  assertEquals("Invalid result for row '"+Bytes.toStringBinary(row)+"'",
    expected, results.get(region));
}
 
Example 4
Source File: TestRegionReplicaReplicationEndpoint.java    From hbase with Apache License 2.0 4 votes vote down vote up
private void testRegionReplicaReplicationIgnores(boolean dropTable, boolean disableReplication)
    throws Exception {
  // tests having edits from a disabled or dropped table is handled correctly by skipping those
  // entries and further edits after the edits from dropped/disabled table can be replicated
  // without problems.
  final TableName tableName = TableName.valueOf(
    name.getMethodName() + "_drop_" + dropTable + "_disabledReplication_" + disableReplication);
  HTableDescriptor htd = HTU.createTableDescriptor(tableName);
  int regionReplication = 3;
  htd.setRegionReplication(regionReplication);
  HTU.deleteTableIfAny(tableName);

  HTU.getAdmin().createTable(htd);
  TableName toBeDisabledTable = TableName.valueOf(
    dropTable ? "droppedTable" : (disableReplication ? "disableReplication" : "disabledTable"));
  HTU.deleteTableIfAny(toBeDisabledTable);
  htd = HTU.createTableDescriptor(TableName.valueOf(toBeDisabledTable.toString()),
    HColumnDescriptor.DEFAULT_MIN_VERSIONS, 3, HConstants.FOREVER,
    HColumnDescriptor.DEFAULT_KEEP_DELETED);
  htd.setRegionReplication(regionReplication);
  HTU.getAdmin().createTable(htd);

  // both tables are created, now pause replication
  HTU.getAdmin().disableReplicationPeer(ServerRegionReplicaUtil.getReplicationPeerId());

  // now that the replication is disabled, write to the table to be dropped, then drop the table.

  Connection connection = ConnectionFactory.createConnection(HTU.getConfiguration());
  Table table = connection.getTable(tableName);
  Table tableToBeDisabled = connection.getTable(toBeDisabledTable);

  HTU.loadNumericRows(tableToBeDisabled, HBaseTestingUtility.fam1, 6000, 7000);

  RegionLocator rl = connection.getRegionLocator(toBeDisabledTable);
  HRegionLocation hrl = rl.getRegionLocation(HConstants.EMPTY_BYTE_ARRAY);
  byte[] encodedRegionName = hrl.getRegion().getEncodedNameAsBytes();

  Cell cell = CellBuilderFactory.create(CellBuilderType.DEEP_COPY).setRow(Bytes.toBytes("A"))
      .setFamily(HTU.fam1).setValue(Bytes.toBytes("VAL")).setType(Type.Put).build();
  Entry entry = new Entry(
    new WALKeyImpl(encodedRegionName, toBeDisabledTable, 1),
      new WALEdit()
          .add(cell));
  HTU.getAdmin().disableTable(toBeDisabledTable); // disable the table
  if (dropTable) {
    HTU.getAdmin().deleteTable(toBeDisabledTable);
  } else if (disableReplication) {
    htd.setRegionReplication(regionReplication - 2);
    HTU.getAdmin().modifyTable(htd);
    HTU.getAdmin().enableTable(toBeDisabledTable);
  }

  HRegionServer rs = HTU.getMiniHBaseCluster().getRegionServer(0);
  MetricsSource metrics = mock(MetricsSource.class);
  ReplicationEndpoint.Context ctx =
    new ReplicationEndpoint.Context(rs, HTU.getConfiguration(), HTU.getConfiguration(),
      HTU.getTestFileSystem(), ServerRegionReplicaUtil.getReplicationPeerId(),
      UUID.fromString(rs.getClusterId()), rs.getReplicationSourceService().getReplicationPeers()
        .getPeer(ServerRegionReplicaUtil.getReplicationPeerId()),
      metrics, rs.getTableDescriptors(), rs);
  RegionReplicaReplicationEndpoint rrpe = new RegionReplicaReplicationEndpoint();
  rrpe.init(ctx);
  rrpe.start();
  ReplicationEndpoint.ReplicateContext repCtx = new ReplicationEndpoint.ReplicateContext();
  repCtx.setEntries(Lists.newArrayList(entry, entry));
  assertTrue(rrpe.replicate(repCtx));
  verify(metrics, times(1)).incrLogEditsFiltered(eq(2L));
  rrpe.stop();
  if (disableReplication) {
    // enable replication again so that we can verify replication
    HTU.getAdmin().disableTable(toBeDisabledTable); // disable the table
    htd.setRegionReplication(regionReplication);
    HTU.getAdmin().modifyTable(htd);
    HTU.getAdmin().enableTable(toBeDisabledTable);
  }

  try {
    // load some data to the to-be-dropped table
    // load the data to the table
    HTU.loadNumericRows(table, HBaseTestingUtility.fam1, 0, 1000);

    // now enable the replication
    HTU.getAdmin().enableReplicationPeer(ServerRegionReplicaUtil.getReplicationPeerId());

    verifyReplication(tableName, regionReplication, 0, 1000);
  } finally {
    table.close();
    rl.close();
    tableToBeDisabled.close();
    HTU.deleteTableIfAny(toBeDisabledTable);
    connection.close();
  }
}
 
Example 5
Source File: PhoenixInputFormat.java    From phoenix with Apache License 2.0 4 votes vote down vote up
private List<InputSplit> generateSplits(final QueryPlan qplan, Configuration config) throws IOException {
    // We must call this in order to initialize the scans and splits from the query plan
    setupParallelScansFromQueryPlan(qplan);
    final List<KeyRange> splits = qplan.getSplits();
    Preconditions.checkNotNull(splits);

    // Get the RegionSizeCalculator
    try(org.apache.hadoop.hbase.client.Connection connection =
                HBaseFactoryProvider.getHConnectionFactory().createConnection(config)) {
    RegionLocator regionLocator = connection.getRegionLocator(TableName.valueOf(qplan
            .getTableRef().getTable().getPhysicalName().toString()));
    RegionSizeCalculator sizeCalculator = new RegionSizeCalculator(regionLocator, connection
            .getAdmin());

    final List<InputSplit> psplits = Lists.newArrayListWithExpectedSize(splits.size());
    for (List<Scan> scans : qplan.getScans()) {
        // Get the region location
        HRegionLocation location = regionLocator.getRegionLocation(
                scans.get(0).getStartRow(),
                false
        );

        String regionLocation = location.getHostname();

        // Get the region size
        long regionSize = sizeCalculator.getRegionSize(
                location.getRegion().getRegionName()
        );

        // Generate splits based off statistics, or just region splits?
        boolean splitByStats = PhoenixConfigurationUtil.getSplitByStats(config);

        if(splitByStats) {
            for(Scan aScan: scans) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("Split for  scan : " + aScan + "with scanAttribute : " + aScan
                            .getAttributesMap() + " [scanCache, cacheBlock, scanBatch] : [" +
                            aScan.getCaching() + ", " + aScan.getCacheBlocks() + ", " + aScan
                            .getBatch() + "] and  regionLocation : " + regionLocation);
                }

                psplits.add(new PhoenixInputSplit(Collections.singletonList(aScan), regionSize, regionLocation));
            }
            } else {
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("Scan count[" + scans.size() + "] : " + Bytes.toStringBinary(scans
                        .get(0).getStartRow()) + " ~ " + Bytes.toStringBinary(scans.get(scans
                        .size() - 1).getStopRow()));
                LOGGER.debug("First scan : " + scans.get(0) + "with scanAttribute : " + scans
                        .get(0).getAttributesMap() + " [scanCache, cacheBlock, scanBatch] : " +
                        "[" + scans.get(0).getCaching() + ", " + scans.get(0).getCacheBlocks()
                        + ", " + scans.get(0).getBatch() + "] and  regionLocation : " +
                        regionLocation);

                for (int i = 0, limit = scans.size(); i < limit; i++) {
                    LOGGER.debug("EXPECTED_UPPER_REGION_KEY[" + i + "] : " + Bytes
                            .toStringBinary(scans.get(i).getAttribute
                                    (BaseScannerRegionObserver.EXPECTED_UPPER_REGION_KEY)));
                }
            }

            psplits.add(new PhoenixInputSplit(scans, regionSize, regionLocation));
        }
    }
    return psplits;
}
}
 
Example 6
Source File: HBaseSplitsProvider.java    From geowave with Apache License 2.0 4 votes vote down vote up
protected static List<ByteArrayRange> binRanges(
    final List<ByteArrayRange> inputRanges,
    final Map<HRegionLocation, Map<HRegionInfo, List<ByteArrayRange>>> binnedRanges,
    final RegionLocator regionLocator) throws IOException {

  // Loop through ranges, getting RegionLocation and RegionInfo for
  // startKey, clipping range by that regionInfo's extent, and leaving
  // remainder in the List to be region'd
  final ListIterator<ByteArrayRange> i = inputRanges.listIterator();
  while (i.hasNext()) {
    final ByteArrayRange range = i.next();
    final byte[] startKey = range == null ? HConstants.EMPTY_BYTE_ARRAY : range.getStart();
    final byte[] endKey = range == null ? HConstants.EMPTY_BYTE_ARRAY : range.getEnd();

    final HRegionLocation location = regionLocator.getRegionLocation(startKey);

    Map<HRegionInfo, List<ByteArrayRange>> regionInfoMap = binnedRanges.get(location);
    if (regionInfoMap == null) {
      regionInfoMap = new HashMap<>();
      binnedRanges.put(location, regionInfoMap);
    }

    final HRegionInfo regionInfo = location.getRegionInfo();
    List<ByteArrayRange> rangeList = regionInfoMap.get(regionInfo);
    if (rangeList == null) {
      rangeList = new ArrayList<>();
      regionInfoMap.put(regionInfo, rangeList);
    }

    // Check if region contains range or if it's the last range
    if ((endKey == HConstants.EMPTY_BYTE_ARRAY) || regionInfo.containsRange(startKey, endKey)) {
      rangeList.add(range);
      i.remove();
    } else {
      final ByteArrayRange thisRange = new ByteArrayRange(startKey, endKey);
      final ByteArrayRange regionRange =
          new ByteArrayRange(regionInfo.getStartKey(), regionInfo.getEndKey());

      final ByteArrayRange overlappingRange = thisRange.intersection(regionRange);

      rangeList.add(new ByteArrayRange(overlappingRange.getStart(), overlappingRange.getEnd()));
      i.remove();

      i.add(new ByteArrayRange(regionInfo.getEndKey(), endKey));
    }
  }
  // the underlying assumption is that by the end of this any input range
  // at least has the partition key portion and is the same partition key
  // for start and end keys on the range, because thats really by
  // definition what a region or tablets is using split points
  return inputRanges;
}
 
Example 7
Source File: HBaseTestingUtility.java    From hbase with Apache License 2.0 2 votes vote down vote up
/**
 * Closes the region containing the given row.
 *
 * @param row  The row to find the containing region.
 * @param table  The table to find the region.
 * @throws IOException
 */
public void unassignRegionByRow(byte[] row, RegionLocator table) throws IOException {
  HRegionLocation hrl = table.getRegionLocation(row);
  unassignRegion(hrl.getRegion().getRegionName());
}