Java Code Examples for org.apache.hadoop.hbase.HRegionLocation

The following examples show how to use org.apache.hadoop.hbase.HRegionLocation. These examples are extracted from open source projects. 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 Project: phoenix   Source File: BaseResultIterators.java    License: 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 2
@Test
public void testGetSplitsWithSkipScanFilter() throws Exception {
    byte[][] splits = new byte[][] {Ka1A, Ka1B, Ka1E, Ka1G, Ka1I, Ka2A};
    long ts = nextTimestamp();
    createTestTable(getUrl(),DDL,splits, ts-2);
    String url = getUrl() + ";" + PhoenixRuntime.CURRENT_SCN_ATTRIB + "=" + ts;
    Properties props = new Properties(TEST_PROPERTIES);
    Connection conn = DriverManager.getConnection(url, props);
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    TableRef tableRef = new TableRef(null,pconn.getPMetaData().getTable(SchemaUtil.getTableName(SCHEMA_NAME, TABLE_NAME)),ts, false);
    List<HRegionLocation> regions = pconn.getQueryServices().getAllTableRegions(tableRef.getTable().getPhysicalName().getBytes());
    
    conn.close();
    initTableValues();
    List<KeyRange> ranges = getSplits(tableRef, scan, regions, scanRanges);
    assertEquals("Unexpected number of splits: " + ranges.size(), expectedSplits.size(), ranges.size());
    for (int i=0; i<expectedSplits.size(); i++) {
        assertEquals(expectedSplits.get(i), ranges.get(i));
    }
}
 
Example 3
Source Project: beam   Source File: HBaseUtils.java    License: Apache License 2.0 6 votes vote down vote up
/** Returns the list of ranges intersecting a list of regions for the given table and scan. */
static List<ByteKeyRange> getRanges(
    List<HRegionLocation> regionLocations, String tableId, ByteKeyRange range) {
  byte[] startRow = range.getStartKey().getBytes();
  byte[] stopRow = range.getEndKey().getBytes();

  final List<ByteKeyRange> splits = new ArrayList<>();
  final boolean scanWithNoLowerBound = startRow.length == 0;
  final boolean scanWithNoUpperBound = stopRow.length == 0;

  for (HRegionLocation regionLocation : regionLocations) {
    final byte[] startKey = regionLocation.getRegionInfo().getStartKey();
    final byte[] endKey = regionLocation.getRegionInfo().getEndKey();
    boolean isLastRegion = endKey.length == 0;

    final byte[] splitStart =
        (scanWithNoLowerBound || Bytes.compareTo(startKey, startRow) >= 0) ? startKey : startRow;
    final byte[] splitStop =
        (scanWithNoUpperBound || Bytes.compareTo(endKey, stopRow) <= 0) && !isLastRegion
            ? endKey
            : stopRow;
    splits.add(ByteKeyRange.of(ByteKey.copyFrom(splitStart), ByteKey.copyFrom(splitStop)));
  }
  return splits;
}
 
Example 4
Source Project: hbase   Source File: ReopenTableRegionsProcedure.java    License: Apache License 2.0 6 votes vote down vote up
private List<HRegionLocation> getRegionLocationsForReopen(
    List<HRegionLocation> tableRegionsForReopen) {

  List<HRegionLocation> regionsToReopen = new ArrayList<>();
  if (CollectionUtils.isNotEmpty(regionNames) &&
    CollectionUtils.isNotEmpty(tableRegionsForReopen)) {
    for (byte[] regionName : regionNames) {
      for (HRegionLocation hRegionLocation : tableRegionsForReopen) {
        if (Bytes.equals(regionName, hRegionLocation.getRegion().getRegionName())) {
          regionsToReopen.add(hRegionLocation);
          break;
        }
      }
    }
  } else {
    regionsToReopen = tableRegionsForReopen;
  }
  return regionsToReopen;
}
 
Example 5
Source Project: phoenix   Source File: ServerCacheClient.java    License: Apache License 2.0 6 votes vote down vote up
public ServerCache(byte[] id, Set<HRegionLocation> servers, ImmutableBytesWritable cachePtr,
        ConnectionQueryServices services, boolean storeCacheOnClient) throws IOException {
    maxServerCacheTTL = services.getProps().getInt(
            QueryServices.MAX_SERVER_CACHE_TIME_TO_LIVE_MS_ATTRIB,
            QueryServicesOptions.DEFAULT_MAX_SERVER_CACHE_TIME_TO_LIVE_MS);
    this.id = id;
    this.servers = new HashMap();
    long currentTime = EnvironmentEdgeManager.currentTimeMillis();
    for(HRegionLocation loc : servers) {
        this.servers.put(loc, currentTime);
    }
    this.size =  cachePtr.getLength();
    if (storeCacheOnClient) {
        try {
            this.chunk = services.getMemoryManager().allocate(cachePtr.getLength());
            this.cachePtr = cachePtr;
        } catch (InsufficientMemoryException e) {
            this.outputFile = File.createTempFile("HashJoinCacheSpooler", ".bin", new File(services.getProps()
                    .get(QueryServices.SPOOL_DIRECTORY, QueryServicesOptions.DEFAULT_SPOOL_DIRECTORY)));
            try (OutputStream fio = Files.newOutputStream(outputFile.toPath())) {
                fio.write(cachePtr.get(), cachePtr.getOffset(), cachePtr.getLength());
            }
        }
    }
    
}
 
Example 6
Source Project: hbase   Source File: TestClientMetaServiceRPCs.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Verifies that the meta region locations RPC returns consistent results across all masters.
 */
@Test public void TestMetaLocations() throws Exception {
  HBaseRpcController rpcController = getRpcController();
  List<HRegionLocation> metaLocations = TEST_UTIL.getMiniHBaseCluster().getMaster()
      .getMetaRegionLocationCache().getMetaRegionLocations().get();
  Collections.sort(metaLocations);
  int rpcCount = 0;
  for (JVMClusterUtil.MasterThread masterThread:
    TEST_UTIL.getMiniHBaseCluster().getMasterThreads()) {
    ClientMetaService.BlockingInterface stub =
        getMasterStub(masterThread.getMaster().getServerName());
    GetMetaRegionLocationsResponse resp = stub.getMetaRegionLocations(
        rpcController, GetMetaRegionLocationsRequest.getDefaultInstance());
    List<HRegionLocation> result = new ArrayList<>();
    resp.getMetaLocationsList().forEach(
      location -> result.add(ProtobufUtil.toRegionLocation(location)));
    Collections.sort(result);
    assertEquals(metaLocations, result);
    rpcCount++;
  }
  assertEquals(MASTER_COUNT, rpcCount);
}
 
Example 7
Source Project: hbase   Source File: TestAccessController.java    License: 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 8
Source Project: hbase   Source File: TestAccessController.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testUnassign() 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();
  AccessTestAction action = new AccessTestAction() {
    @Override
    public Object run() throws Exception {
      ACCESS_CONTROLLER.preUnassign(ObserverContextImpl.createAndPrepare(CP_ENV), hri, false);
      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 9
private static List<KeyRange> getSplits(Connection conn, long ts, final Scan scan)
        throws SQLException {
    TableRef tableRef = getTableRef(conn, ts);
    PhoenixConnection pconn = conn.unwrap(PhoenixConnection.class);
    final List<HRegionLocation> regions =  pconn.getQueryServices().getAllTableRegions(tableRef.getTable().getPhysicalName().getBytes());
    StatementContext context = new StatementContext(new PhoenixStatement(pconn), null, Collections.emptyList(), scan);
    DefaultParallelIteratorRegionSplitter splitter = new DefaultParallelIteratorRegionSplitter(context, tableRef, HintNode.EMPTY_HINT_NODE) {
        @Override
        protected List<HRegionLocation> getAllRegions() throws SQLException {
            return DefaultParallelIteratorRegionSplitter.filterRegions(regions, scan.getStartRow(), scan.getStopRow());
        }
    };
    List<KeyRange> keyRanges = splitter.getSplits();
    Collections.sort(keyRanges, new Comparator<KeyRange>() {
        @Override
        public int compare(KeyRange o1, KeyRange o2) {
            return Bytes.compareTo(o1.getLowerRange(),o2.getLowerRange());
        }
    });
    return keyRanges;
}
 
Example 10
Source Project: hbase   Source File: AsyncClientScanner.java    License: Apache License 2.0 6 votes vote down vote up
private CompletableFuture<OpenScannerResponse> callOpenScanner(HBaseRpcController controller,
    HRegionLocation loc, ClientService.Interface stub) {
  boolean isRegionServerRemote = isRemote(loc.getHostname());
  incRPCCallsMetrics(scanMetrics, isRegionServerRemote);
  if (openScannerTries.getAndIncrement() > 1) {
    incRPCRetriesMetrics(scanMetrics, isRegionServerRemote);
  }
  CompletableFuture<OpenScannerResponse> future = new CompletableFuture<>();
  try {
    ScanRequest request = RequestConverter.buildScanRequest(loc.getRegion().getRegionName(), scan,
      scan.getCaching(), false);
    stub.scan(controller, request, resp -> {
      if (controller.failed()) {
        future.completeExceptionally(controller.getFailed());
        return;
      }
      future.complete(new OpenScannerResponse(loc, isRegionServerRemote, stub, controller, resp));
    });
  } catch (IOException e) {
    future.completeExceptionally(e);
  }
  return future;
}
 
Example 11
Source Project: hbase   Source File: TestMetaRegionLocationCache.java    License: Apache License 2.0 6 votes vote down vote up
private void verifyCachedMetaLocations(HMaster master) throws Exception {
  // Wait until initial meta locations are loaded.
  int retries = 0;
  while (!master.getMetaRegionLocationCache().getMetaRegionLocations().isPresent()) {
    Thread.sleep(1000);
    if (++retries == 10) {
      break;
    }
  }
  List<HRegionLocation> metaHRLs =
      master.getMetaRegionLocationCache().getMetaRegionLocations().get();
  assertFalse(metaHRLs.isEmpty());
  ZKWatcher zk = master.getZooKeeper();
  List<String> metaZnodes = zk.getMetaReplicaNodes();
  assertEquals(metaZnodes.size(), metaHRLs.size());
  List<HRegionLocation> actualHRLs = getCurrentMetaLocations(zk);
  Collections.sort(metaHRLs);
  Collections.sort(actualHRLs);
  assertEquals(actualHRLs, metaHRLs);
}
 
Example 12
Source Project: presto   Source File: PhoenixSplitManager.java    License: 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 13
Source Project: pxf   Source File: HBaseDataFragmenter.java    License: 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 14
Source Project: hbase   Source File: MasterProcedureTestingUtility.java    License: Apache License 2.0 5 votes vote down vote up
private static int countMetaRegions(final HMaster master, final TableName tableName)
    throws IOException {
  final AtomicInteger actualRegCount = new AtomicInteger(0);
  final ClientMetaTableAccessor.Visitor visitor = new ClientMetaTableAccessor.Visitor() {
    @Override
    public boolean visit(Result rowResult) throws IOException {
      RegionLocations list = CatalogFamilyFormat.getRegionLocations(rowResult);
      if (list == null) {
        LOG.warn("No serialized RegionInfo in " + rowResult);
        return true;
      }
      HRegionLocation l = list.getRegionLocation();
      if (l == null) {
        return true;
      }
      if (!l.getRegion().getTable().equals(tableName)) {
        return false;
      }
      if (l.getRegion().isOffline() || l.getRegion().isSplit()) {
        return true;
      }

      HRegionLocation[] locations = list.getRegionLocations();
      for (HRegionLocation location : locations) {
        if (location == null) continue;
        ServerName serverName = location.getServerName();
        // Make sure that regions are assigned to server
        if (serverName != null && serverName.getAddress() != null) {
          actualRegCount.incrementAndGet();
        }
      }
      return true;
    }
  };
  MetaTableAccessor.scanMetaForTableRegions(master.getConnection(), visitor, tableName);
  return actualRegCount.get();
}
 
Example 15
Source Project: hbase   Source File: RestartRsHoldingTableAction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public void perform() throws Exception {
  getLogger().info(
    "Performing action: Restart random RS holding table " + this.locator.getName());

  List<HRegionLocation> locations = locator.getAllRegionLocations();
  restartRs(locations.get(RandomUtils.nextInt(0, locations.size())).getServerName(), sleepTime);
}
 
Example 16
Source Project: phoenix   Source File: ConnectionlessQueryServicesImpl.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public List<HRegionLocation> getAllTableRegions(byte[] tableName) throws SQLException {
    List<HRegionLocation> regions = tableSplits.get(Bytes.toString(tableName));
    if (regions != null) {
        return regions;
    }
    RegionInfo hri =
            RegionInfoBuilder.newBuilder(TableName.valueOf(tableName))
                    .setStartKey(HConstants.EMPTY_START_ROW)
                    .setStartKey(HConstants.EMPTY_END_ROW).build();
    return Collections.singletonList(new HRegionLocation(hri, SERVER_NAME, -1));
}
 
Example 17
Source Project: hbase   Source File: MultiThreadedReader.java    License: Apache License 2.0 5 votes vote down vote up
private void verifyResultsAndUpdateMetricsOnAPerGetBasis(boolean verify, Get get,
    Result result, Table table, boolean isNullExpected) throws IOException {
  if (!result.isEmpty()) {
    if (verify) {
      numKeysVerified.incrementAndGet();
    }
  } else {
    HRegionLocation hloc;
    try (RegionLocator locator = connection.getRegionLocator(tableName)) {
      hloc = locator.getRegionLocation(get.getRow());
    }
    String rowKey = Bytes.toString(get.getRow());
    LOG.info("Key = " + rowKey + ", Region location: " + hloc);
    if(isNullExpected) {
      nullResult.incrementAndGet();
      LOG.debug("Null result obtained for the key ="+rowKey);
      return;
    }
  }
  boolean isOk = verifyResultAgainstDataGenerator(result, verify, false);
  long numErrorsAfterThis = 0;
  if (isOk) {
    long cols = 0;
    // Count the columns for reporting purposes.
    for (byte[] cf : result.getMap().keySet()) {
      cols += result.getFamilyMap(cf).size();
    }
    numCols.addAndGet(cols);
  } else {
    if (writer != null) {
      LOG.error("At the time of failure, writer wrote " + writer.numKeys.get() + " keys");
    }
    numErrorsAfterThis = numReadErrors.incrementAndGet();
  }

  if (numErrorsAfterThis > maxErrors) {
    LOG.error("Aborting readers -- found more than " + maxErrors + " errors");
    aborted = true;
  }
}
 
Example 18
Source Project: phoenix   Source File: ServerCacheClient.java    License: Apache License 2.0 5 votes vote down vote up
public boolean isExpired(HRegionLocation loc) {
    if(this.servers.containsKey(loc)) {
        Long time = this.servers.get(loc);
        if(EnvironmentEdgeManager.currentTimeMillis() - time > maxServerCacheTTL)
            return true; // cache was send more than maxTTL ms ago, expecting that it's expired
    } else {
        return false; // should be on server yet.
    }
    return false; // Unknown region location. Need to send the cache.
}
 
Example 19
Source Project: beam   Source File: HBaseUtils.java    License: Apache License 2.0 5 votes vote down vote up
/** Returns a list of region locations for a given table and scan. */
static List<HRegionLocation> getRegionLocations(
    Connection connection, String tableId, ByteKeyRange range) throws Exception {
  byte[] startRow = range.getStartKey().getBytes();
  byte[] stopRow = range.getEndKey().getBytes();

  final List<HRegionLocation> regionLocations = new ArrayList<>();

  final boolean scanWithNoLowerBound = startRow.length == 0;
  final boolean scanWithNoUpperBound = stopRow.length == 0;

  TableName tableName = TableName.valueOf(tableId);
  RegionLocator regionLocator = connection.getRegionLocator(tableName);
  List<HRegionLocation> tableRegionInfos = regionLocator.getAllRegionLocations();
  for (HRegionLocation regionLocation : tableRegionInfos) {
    final byte[] startKey = regionLocation.getRegionInfo().getStartKey();
    final byte[] endKey = regionLocation.getRegionInfo().getEndKey();
    boolean isLastRegion = endKey.length == 0;
    // filters regions who are part of the scan
    if ((scanWithNoLowerBound || isLastRegion || Bytes.compareTo(startRow, endKey) < 0)
        && (scanWithNoUpperBound || Bytes.compareTo(stopRow, startKey) > 0)) {
      regionLocations.add(regionLocation);
    }
  }

  return regionLocations;
}
 
Example 20
private List<Partition> formatPartitions(List<HRegionLocation> tableLocations) {
    List<Partition> partitions=new ArrayList<>(tableLocations.size());
    for(HRegionLocation location : tableLocations){
        HRegionInfo regionInfo=location.getRegionInfo();
        partitions.add(new RangedClientPartition(this,regionInfo,new RLServer(location)));
    }
    return partitions;
}
 
Example 21
Source Project: hbase   Source File: RawAsyncTableImpl.java    License: Apache License 2.0 5 votes vote down vote up
private <S, R> void onLocateComplete(Function<RpcChannel, S> stubMaker,
    ServiceCaller<S, R> callable, CoprocessorCallback<R> callback, List<HRegionLocation> locs,
    byte[] endKey, boolean endKeyInclusive, AtomicBoolean locateFinished,
    AtomicInteger unfinishedRequest, HRegionLocation loc, Throwable error) {
  if (error != null) {
    callback.onError(error);
    return;
  }
  unfinishedRequest.incrementAndGet();
  RegionInfo region = loc.getRegion();
  if (locateFinished(region, endKey, endKeyInclusive)) {
    locateFinished.set(true);
  } else {
    addListener(
      conn.getLocator().getRegionLocation(tableName, region.getEndKey(), RegionLocateType.CURRENT,
        operationTimeoutNs),
      (l, e) -> onLocateComplete(stubMaker, callable, callback, locs, endKey, endKeyInclusive,
        locateFinished, unfinishedRequest, l, e));
  }
  addListener(coprocessorService(stubMaker, callable, region, region.getStartKey()), (r, e) -> {
    if (e != null) {
      callback.onRegionError(region, e);
    } else {
      callback.onRegionComplete(region, r);
    }
    if (unfinishedRequest.decrementAndGet() == 0 && locateFinished.get()) {
      callback.onComplete();
    }
  });
}
 
Example 22
Source Project: hbase   Source File: TestAsyncNonMetaRegionLocator.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testRegionMove() throws IOException, InterruptedException, ExecutionException {
  createSingleRegionTable();
  ServerName serverName = TEST_UTIL.getRSForFirstRegionInTable(TABLE_NAME).getServerName();
  HRegionLocation loc =
    getDefaultRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, false).get();
  assertLocEquals(EMPTY_START_ROW, EMPTY_END_ROW, serverName, loc);
  ServerName newServerName = TEST_UTIL.getHBaseCluster().getRegionServerThreads().stream()
    .map(t -> t.getRegionServer().getServerName()).filter(sn -> !sn.equals(serverName)).findAny()
    .get();

  TEST_UTIL.getAdmin().move(Bytes.toBytes(loc.getRegion().getEncodedName()), newServerName);
  while (!TEST_UTIL.getRSForFirstRegionInTable(TABLE_NAME).getServerName()
    .equals(newServerName)) {
    Thread.sleep(100);
  }
  // Should be same as it is in cache
  assertSame(loc,
    getDefaultRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, false).get());
  LOCATOR.updateCachedLocationOnError(loc, null);
  // null error will not trigger a cache cleanup
  assertSame(loc,
    getDefaultRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, false).get());
  LOCATOR.updateCachedLocationOnError(loc, new NotServingRegionException());
  assertLocEquals(EMPTY_START_ROW, EMPTY_END_ROW, newServerName,
    getDefaultRegionLocation(TABLE_NAME, EMPTY_START_ROW, RegionLocateType.CURRENT, false).get());
}
 
Example 23
Source Project: phoenix   Source File: BaseResultIterators.java    License: Apache License 2.0 5 votes vote down vote up
private static List<byte[]> toBoundaries(List<HRegionLocation> regionLocations) {
    int nBoundaries = regionLocations.size() - 1;
    List<byte[]> ranges = Lists.newArrayListWithExpectedSize(nBoundaries);
    for (int i = 0; i < nBoundaries; i++) {
        HRegionInfo regionInfo = regionLocations.get(i).getRegionInfo();
        ranges.add(regionInfo.getEndKey());
    }
    return ranges;
}
 
Example 24
Source Project: hbase   Source File: TestCatalogJanitorInMemoryStates.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Test clearing a split parent from memory.
 */
@Test
public void testInMemoryParentCleanup()
    throws IOException, InterruptedException, ExecutionException {
  final AssignmentManager am = TEST_UTIL.getHBaseCluster().getMaster().getAssignmentManager();
  final ServerManager sm = TEST_UTIL.getHBaseCluster().getMaster().getServerManager();
  final CatalogJanitor janitor = TEST_UTIL.getHBaseCluster().getMaster().getCatalogJanitor();

  Admin admin = TEST_UTIL.getAdmin();
  admin.catalogJanitorSwitch(false);

  final TableName tableName = TableName.valueOf(name.getMethodName());
  Table t = TEST_UTIL.createTable(tableName, FAMILY);
  int rowCount = TEST_UTIL.loadTable(t, FAMILY, false);

  RegionLocator locator = TEST_UTIL.getConnection().getRegionLocator(tableName);
  List<HRegionLocation> allRegionLocations = locator.getAllRegionLocations();

  // We need to create a valid split with daughter regions
  HRegionLocation parent = allRegionLocations.get(0);
  List<HRegionLocation> daughters = splitRegion(parent.getRegion());
  LOG.info("Parent region: " + parent);
  LOG.info("Daughter regions: " + daughters);
  assertNotNull("Should have found daughter regions for " + parent, daughters);

  assertTrue("Parent region should exist in RegionStates",
      am.getRegionStates().isRegionInRegionStates(parent.getRegion()));
  assertTrue("Parent region should exist in ServerManager",
      sm.isRegionInServerManagerStates(parent.getRegion()));

  // clean the parent
  Result r = MetaMockingUtil.getMetaTableRowResult(parent.getRegion(), null,
      daughters.get(0).getRegion(), daughters.get(1).getRegion());
  janitor.cleanParent(parent.getRegion(), r);
  assertFalse("Parent region should have been removed from RegionStates",
      am.getRegionStates().isRegionInRegionStates(parent.getRegion()));
  assertFalse("Parent region should have been removed from ServerManager",
      sm.isRegionInServerManagerStates(parent.getRegion()));

}
 
Example 25
Source Project: hbase   Source File: ThriftUtilities.java    License: Apache License 2.0 5 votes vote down vote up
public static List<THRegionLocation> regionLocationsFromHBase(List<HRegionLocation> locations) {
  List<THRegionLocation> tlocations = new ArrayList<>(locations.size());
  for (HRegionLocation hrl:locations) {
    tlocations.add(regionLocationFromHBase(hrl));
  }
  return tlocations;
}
 
Example 26
Source Project: phoenix   Source File: MetaDataUtil.java    License: 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 27
@Override
public List<HRegionLocation> getRegionBoundaries(StatementContext context,
        byte[] tableName) throws SQLException {
    List<HRegionLocation> regionLocations = super.getRegionBoundaries(context, tableName);
    numCallsToGetRegionBoundaries.incrementAndGet();
    return regionLocations;
}
 
Example 28
public AsyncScanSingleRegionRpcRetryingCaller(Timer retryTimer, AsyncConnectionImpl conn,
    Scan scan, ScanMetrics scanMetrics, long scannerId, ScanResultCache resultCache,
    AdvancedScanResultConsumer consumer, Interface stub, HRegionLocation loc,
    boolean isRegionServerRemote, int priority, long scannerLeaseTimeoutPeriodNs, long pauseNs,
    long pauseForCQTBENs, int maxAttempts, long scanTimeoutNs, long rpcTimeoutNs,
    int startLogErrorsCnt) {
  this.retryTimer = retryTimer;
  this.scan = scan;
  this.scanMetrics = scanMetrics;
  this.scannerId = scannerId;
  this.resultCache = resultCache;
  this.consumer = consumer;
  this.stub = stub;
  this.loc = loc;
  this.regionServerRemote = isRegionServerRemote;
  this.scannerLeaseTimeoutPeriodNs = scannerLeaseTimeoutPeriodNs;
  this.pauseNs = pauseNs;
  this.pauseForCQTBENs = pauseForCQTBENs;
  this.maxAttempts = maxAttempts;
  this.scanTimeoutNs = scanTimeoutNs;
  this.rpcTimeoutNs = rpcTimeoutNs;
  this.startLogErrorsCnt = startLogErrorsCnt;
  if (scan.isReversed()) {
    completeWhenNoMoreResultsInRegion = this::completeReversedWhenNoMoreResultsInRegion;
  } else {
    completeWhenNoMoreResultsInRegion = this::completeWhenNoMoreResultsInRegion;
  }
  this.future = new CompletableFuture<>();
  this.priority = priority;
  this.controller = conn.rpcControllerFactory.newController();
  this.controller.setPriority(priority);
  this.exceptions = new ArrayList<>();
}
 
Example 29
Source Project: hbase   Source File: RawAsyncTableImpl.java    License: Apache License 2.0 5 votes vote down vote up
private static <REQ, RESP> CompletableFuture<RESP> mutate(HBaseRpcController controller,
    HRegionLocation loc, ClientService.Interface stub, REQ req,
    Converter<MutateRequest, byte[], REQ> reqConvert,
    Converter<RESP, HBaseRpcController, MutateResponse> respConverter) {
  return ConnectionUtils.call(controller, loc, stub, req, reqConvert,
    (s, c, r, done) -> s.mutate(c, r, done), respConverter);
}
 
Example 30
Source Project: hbase   Source File: RegionReplicaInfo.java    License: 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;
  }
}