public void reset(ServerName location, MultiAction multiAction) { this.location = new HRegionLocation(null, location); this.multiAction = multiAction; this.cellBlock = isCellBlock(); }
MultiServerCallable(final ClusterConnection connection, final TableName tableName, final ServerName location, final MultiAction multi, RpcController rpcController, int rpcTimeout, RetryingTimeTracker tracker, int priority) { super(connection, tableName, null, rpcController, rpcTimeout, tracker, priority); this.multiAction = multi; // RegionServerCallable has HRegionLocation field, but this is a multi-region request. // Using region info from parent HRegionLocation would be a mistake for this class; so // we will store the server here, and throw if someone tries to obtain location/regioninfo. this.location = new HRegionLocation(null, location); this.cellBlock = isCellBlock(); }
private HRegionLocation hrl(HRegionInfo hri, ServerName sn, long seqNum) { return new HRegionLocation(hri, sn, seqNum); }
private HRegionLocation hrl(HRegionInfo hri, ServerName sn) { return new HRegionLocation(hri, sn); }
void updateCachedLocation(RegionInfo hri, ServerName source, ServerName serverName, long seqNum) { HRegionLocation newHrl = new HRegionLocation(hri, serverName, seqNum); cacheLocation(hri.getTable(), source, newHrl); }
@Override public CompletableFuture<RegionLocations> getMetaRegionLocation() { return CompletableFuture.completedFuture(new RegionLocations( new HRegionLocation(RegionInfoBuilder.FIRST_META_REGIONINFO, META_HOST))); }
/** * Used to get all region locations for the specific table. * @param metaTable * @param tableName table we're looking for, can be null for getting all regions * @return the list of region locations. The return value will be wrapped by a * {@link CompletableFuture}. */ public static CompletableFuture<List<HRegionLocation>> getTableHRegionLocations( AsyncTable<AdvancedScanResultConsumer> metaTable, Optional<TableName> tableName) { CompletableFuture<List<HRegionLocation>> future = new CompletableFuture<>(); getTableRegionsAndLocations(metaTable, tableName, true).whenComplete( (locations, err) -> { if (err != null) { future.completeExceptionally(err); } else if (locations == null || locations.isEmpty()) { future.complete(Collections.emptyList()); } else { List<HRegionLocation> regionLocations = locations.stream() .map(loc -> new HRegionLocation(loc.getFirst(), loc.getSecond())) .collect(Collectors.toList()); future.complete(regionLocations); } }); return future; }
/** * Returns the HRegionLocation parsed from the given meta row Result * for the given regionInfo and replicaId. The regionInfo can be the default region info * for the replica. * @param r the meta row result * @param regionInfo RegionInfo for default replica * @param replicaId the replicaId for the HRegionLocation * @return HRegionLocation parsed from the given meta row Result for the given replicaId */ private static HRegionLocation getRegionLocation(final Result r, final RegionInfo regionInfo, final int replicaId) { Optional<ServerName> serverName = getServerName(r, replicaId); long seqNum = getSeqNumDuringOpen(r, replicaId); RegionInfo replicaInfo = RegionReplicaUtil.getRegionInfoForReplica(regionInfo, replicaId); return new HRegionLocation(replicaInfo, serverName.orElse(null), seqNum); }
static void updateCachedLocationOnError(HRegionLocation loc, Throwable exception, Function<HRegionLocation, HRegionLocation> cachedLocationSupplier, Consumer<HRegionLocation> addToCache, Consumer<HRegionLocation> removeFromCache) { HRegionLocation oldLoc = cachedLocationSupplier.apply(loc); LOG.debug("Try updating {} , the old value is {}", loc, oldLoc, exception); if (!canUpdateOnError(loc, oldLoc)) { return; } Throwable cause = findException(exception); LOG.debug("The actual exception when updating {}", loc, cause); if (cause == null || !isMetaClearingException(cause)) { LOG.debug("Will not update {} because the exception is null or not the one we care about", loc); return; } if (cause instanceof RegionMovedException) { RegionMovedException rme = (RegionMovedException) cause; HRegionLocation newLoc = new HRegionLocation(loc.getRegion(), rme.getServerName(), rme.getLocationSeqNum()); LOG.debug("Try updating {} with the new location {} constructed by {}", loc, newLoc, rme); addToCache.accept(newLoc); } else { LOG.debug("Try removing {} from cache", loc); removeFromCache.accept(loc); } }
/** * Returns the HRegionLocation parsed from the given meta row Result * for the given regionInfo and replicaId. The regionInfo can be the default region info * for the replica. * @param r the meta row result * @param regionInfo RegionInfo for default replica * @param replicaId the replicaId for the HRegionLocation * @return HRegionLocation parsed from the given meta row Result for the given replicaId */ private static HRegionLocation getRegionLocation(final Result r, final RegionInfo regionInfo, final int replicaId) { ServerName serverName = getServerName(r, replicaId); long seqNum = getSeqNumDuringOpen(r, replicaId); RegionInfo replicaInfo = RegionReplicaUtil.getRegionInfoForReplica(regionInfo, replicaId); return new HRegionLocation(replicaInfo, serverName, seqNum); }
/** * Makes some table with given region names. * */ private RegionLocator mockRegionLocator(String... regionNames) throws IOException { RegionLocator mockedTable = Mockito.mock(RegionLocator.class); when(mockedTable.getName()).thenReturn(TableName.valueOf("sizeTestTable")); List<HRegionLocation> regionLocations = new ArrayList<>(regionNames.length); when(mockedTable.getAllRegionLocations()).thenReturn(regionLocations); for (String regionName : regionNames) { HRegionInfo info = Mockito.mock(HRegionInfo.class); when(info.getRegionName()).thenReturn(regionName.getBytes()); regionLocations.add(new HRegionLocation(info, sn)); } return mockedTable; }
public static HRegionLocation toRegionLocation(HBaseProtos.RegionLocation proto) { org.apache.hadoop.hbase.client.RegionInfo regionInfo = toRegionInfo(proto.getRegionInfo()); ServerName serverName = proto.hasServerName() ? toServerName(proto.getServerName()) : null; return new HRegionLocation(regionInfo, serverName, proto.getSeqNum()); }
@Test public void testToString() { ServerName hsa1 = ServerName.valueOf("localhost", 1234, -1L); HRegionLocation hrl1 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1); System.out.println(hrl1.toString()); }
private ClusterConnection getMockedConnection(final Configuration conf) throws IOException, org.apache.hbase.thirdparty.com.google.protobuf.ServiceException { ClusterConnection c = Mockito.mock(ClusterConnection.class); Mockito.when(c.getConfiguration()).thenReturn(conf); Mockito.doNothing().when(c).close(); // Make it so we return a particular location when asked. final HRegionLocation loc = new HRegionLocation(RegionInfoBuilder.FIRST_META_REGIONINFO, ServerName.valueOf("example.org", 1234, 0)); Mockito.when( c.getRegionLocation((TableName) Mockito.any(), (byte[]) Mockito.any(), Mockito.anyBoolean())) .thenReturn(loc); Mockito.when(c.locateRegion((TableName) Mockito.any(), (byte[]) Mockito.any())).thenReturn(loc); ClientProtos.ClientService.BlockingInterface hri = Mockito.mock(ClientProtos.ClientService.BlockingInterface.class); Mockito .when( hri.bulkLoadHFile((RpcController) Mockito.any(), (BulkLoadHFileRequest) Mockito.any())) .thenThrow(new ServiceException(new IOException("injecting bulk load error"))); Mockito.when(c.getClient(Mockito.any())).thenReturn(hri); return c; }
private HRegionLocation createRegionForReopen(RegionStateNode node) { node.lock(); try { if (!include(node, false)) { return null; } if (node.isInState(State.OPEN)) { return new HRegionLocation(node.getRegionInfo(), node.getRegionLocation(), node.getOpenSeqNum()); } else if (node.isInState(State.OPENING)) { return new HRegionLocation(node.getRegionInfo(), node.getRegionLocation(), -1); } else { return null; } } finally { node.unlock(); } }
@Override public RegionLocator getRegionLocator(TableName tableName) throws IOException { final Map<byte[], HRegionLocation> locationMap = new TreeMap<>(Bytes.BYTES_COMPARATOR); for (byte[] startKey : START_KEYS) { HRegionLocation hrl = new HRegionLocation( RegionInfoBuilder.newBuilder(tableName).setStartKey(startKey).build(), ServerName.valueOf("localhost", 0, 0)); locationMap.put(startKey, hrl); } RegionLocator locator = mock(RegionLocator.class); when(locator.getRegionLocation(any(byte [].class), anyBoolean())). thenAnswer(new Answer<HRegionLocation>() { @Override public HRegionLocation answer(InvocationOnMock invocationOnMock) throws Throwable { Object [] args = invocationOnMock.getArguments(); byte [] key = (byte [])args[0]; return locationMap.get(key); } }); when(locator.getStartEndKeys()). thenReturn(new Pair<byte[][], byte[][]>(START_KEYS, END_KEYS)); return locator; }
HRegionInfo hri = new HRegionInfo( DUMMY_TABLE, Bytes.toBytes(i * 10L), Bytes.toBytes(i * 10L + 9L), false, i); HRegionLocation hrl = new HRegionLocation(hri, i % 2 == 0 ? sn : sn2); hrls.add(hrl);
/** * HRegionLocations are equal if they have the same 'location' -- i.e. host and * port -- even if they are carrying different regions. Verify that is indeed * the case. */ @Test public void testHashAndEqualsCode() { ServerName hsa1 = ServerName.valueOf("localhost", 1234, -1L); HRegionLocation hrl1 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1); HRegionLocation hrl2 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1); assertEquals(hrl1.hashCode(), hrl2.hashCode()); assertTrue(hrl1.equals(hrl2)); HRegionLocation hrl3 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1); assertNotSame(hrl1, hrl3); // They are equal because they have same location even though they are // carrying different regions or timestamp. assertTrue(hrl1.equals(hrl3)); ServerName hsa2 = ServerName.valueOf("localhost", 12345, -1L); HRegionLocation hrl4 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa2); // These have same HRI but different locations so should be different. assertFalse(hrl3.equals(hrl4)); HRegionLocation hrl5 = new HRegionLocation(hrl4.getRegionInfo(), hrl4.getServerName(), hrl4.getSeqNum() + 1); assertTrue(hrl4.equals(hrl5)); }
@SuppressWarnings("SelfComparison") @Test public void testCompareTo() { ServerName hsa1 = ServerName.valueOf("localhost", 1234, -1L); HRegionLocation hsl1 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa1); ServerName hsa2 = ServerName.valueOf("localhost", 1235, -1L); HRegionLocation hsl2 = new HRegionLocation(HRegionInfo.FIRST_META_REGIONINFO, hsa2); assertEquals(0, hsl1.compareTo(hsl1)); assertEquals(0, hsl2.compareTo(hsl2)); int compare1 = hsl1.compareTo(hsl2); int compare2 = hsl2.compareTo(hsl1); assertTrue((compare1 > 0)? compare2 < 0: compare2 > 0); } }
HRegionLocation[] arr = new HRegionLocation[regionReplication]; for (int i = 0; i < arr.length; i++) { arr[i] = new HRegionLocation(RegionReplicaUtil.getRegionInfoForReplica(region, i), null);