/** * Put a newly discovered HRegionLocation into the cache. * @param tableName The table name. * @param locations the new locations */ public void cacheLocation(final TableName tableName, final RegionLocations locations) { byte [] startKey = locations.getRegionLocation().getRegion().getStartKey(); ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(tableName); RegionLocations oldLocation = tableLocations.putIfAbsent(startKey, locations); boolean isNewCacheEntry = (oldLocation == null); if (isNewCacheEntry) { if (LOG.isTraceEnabled()) { LOG.trace("Cached location: " + locations); } addToCachedServers(locations); return; } // merge old and new locations and add it to the cache // Meta record might be stale - some (probably the same) server has closed the region // with later seqNum and told us about the new location. RegionLocations mergedLocation = oldLocation.mergeLocations(locations); boolean replaced = tableLocations.replace(startKey, oldLocation, mergedLocation); if (replaced && LOG.isTraceEnabled()) { LOG.trace("Merged cached locations: " + mergedLocation); } addToCachedServers(locations); }
/** * Put a newly discovered HRegionLocation into the cache. * @param tableName The table name. * @param location the new location */ @Override public void cacheLocation(final TableName tableName, final RegionLocations location) { metaCache.cacheLocation(tableName, location); }
@Override public void clearCaches(final ServerName serverName) { metaCache.clearCache(serverName); }
/** * Delete a cached location, no matter what it is. Called when we were told to not use cache. * @param tableName tableName * @param row */ public void clearCache(final TableName tableName, final byte [] row) { ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(tableName); RegionLocations regionLocations = getCachedLocation(tableName, row); if (regionLocations != null) { byte[] startKey = regionLocations.getRegionLocation().getRegion().getStartKey(); boolean removed = tableLocations.remove(startKey, regionLocations); if (removed) { if (metrics != null) { metrics.incrMetaCacheNumClearRegion(); } if (LOG.isTraceEnabled()) { LOG.trace("Removed " + regionLocations + " from cache"); } } } }
/** * Deletes the cached location of the region if necessary, based on some error from source. * @param hri The region in question. */ public void clearCache(RegionInfo hri) { ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(hri.getTable()); RegionLocations regionLocations = tableLocations.get(hri.getStartKey()); if (regionLocations != null) { HRegionLocation oldLocation = regionLocations.getRegionLocation(hri.getReplicaId()); if (oldLocation == null) return; RegionLocations updatedLocations = regionLocations.remove(oldLocation); boolean removed; if (updatedLocations != regionLocations) { if (updatedLocations.isEmpty()) { removed = tableLocations.remove(hri.getStartKey(), regionLocations); } else { removed = tableLocations.replace(hri.getStartKey(), regionLocations, updatedLocations); } if (removed) { if (metrics != null) { metrics.incrMetaCacheNumClearRegion(); } if (LOG.isTraceEnabled()) { LOG.trace("Removed " + oldLocation + " from cache"); } } } } }
/** * Search the cache for a location that fits our table and row key. * Return null if no suitable region is located. * @return Null or region location found in cache. */ RegionLocations getCachedLocation(final TableName tableName, final byte [] row) { return metaCache.getCachedLocation(tableName, row); }
@VisibleForTesting int getNumberOfCachedRegionLocations(final TableName tableName) { return metaCache.getNumberOfCachedRegionLocations(tableName); }
this.metrics = null; this.metaCache = new MetaCache(this.metrics);
getTableLocations(tableName); getRowComparator(tableName).compareRows( endKey, 0, endKey.length, row, 0, row.length) > 0) { if (metrics != null) metrics.incrMetaCacheHit();
/** * Delete a cached location for a table, row and server */ public void clearCache(final TableName tableName, final byte [] row, ServerName serverName) { ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(tableName); RegionLocations regionLocations = getCachedLocation(tableName, row); if (regionLocations != null) { RegionLocations updatedLocations = regionLocations.removeByServer(serverName); if (updatedLocations != regionLocations) { byte[] startKey = regionLocations.getRegionLocation().getRegion().getStartKey(); boolean removed = false; if (updatedLocations.isEmpty()) { removed = tableLocations.remove(startKey, regionLocations); } else { removed = tableLocations.replace(startKey, regionLocations, updatedLocations); } if (removed) { if (metrics != null) { metrics.incrMetaCacheNumClearRegion(); } if (LOG.isTraceEnabled()) { LOG.trace("Removed locations of table: " + tableName + " ,row: " + Bytes.toString(row) + " mapping to server: " + serverName + " from cache"); } } } } }
public void clearCache(final HRegionLocation location) { if (location == null) { return; } TableName tableName = location.getRegion().getTable(); ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(tableName); RegionLocations regionLocations = tableLocations.get(location.getRegion().getStartKey()); if (regionLocations != null) { RegionLocations updatedLocations = regionLocations.remove(location); boolean removed; if (updatedLocations != regionLocations) { if (updatedLocations.isEmpty()) { removed = tableLocations.remove(location.getRegion().getStartKey(), regionLocations); } else { removed = tableLocations.replace(location.getRegion().getStartKey(), regionLocations, updatedLocations); } if (removed) { if (metrics != null) { metrics.incrMetaCacheNumClearRegion(); } if (LOG.isTraceEnabled()) { LOG.trace("Removed " + location + " from cache"); } } } } } }
/** * Check the region cache to see whether a region is cached yet or not. * @param tableName tableName * @param row row * @return Region cached or not. */ public boolean isRegionCached(TableName tableName, final byte[] row) { RegionLocations location = getCachedLocation(tableName, row); return location != null; }
@VisibleForTesting int getNumberOfCachedRegionLocations(final TableName tableName) { return metaCache.getNumberOfCachedRegionLocations(tableName); }
this.metrics = null; this.metaCache = new MetaCache(this.metrics);
ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(tableName); RegionLocations regionLocations = getCachedLocation(tableName, row); if (regionLocations != null) { HRegionLocation toBeRemoved = regionLocations.getRegionLocation(replicaId);
assert source != null; byte [] startKey = location.getRegion().getStartKey(); ConcurrentMap<byte[], RegionLocations> tableLocations = getTableLocations(tableName); RegionLocations locations = new RegionLocations(new HRegionLocation[] {location}) ; RegionLocations oldLocations = tableLocations.putIfAbsent(startKey, locations); LOG.trace("Cached location: " + location); addToCachedServers(locations); return; LOG.trace("Changed cached location to: " + location); addToCachedServers(updatedLocations);
@Override public void clearRegionLocationCache() { metaCache.clearCache(); }
getTableLocations(tableName);
/** * Put a newly discovered HRegionLocation into the cache. * @param tableName The table name. * @param source the source of the new location, if it's not coming from meta * @param location the new location */ private void cacheLocation(final TableName tableName, final ServerName source, final HRegionLocation location) { metaCache.cacheLocation(tableName, source, location); }
/** * Search the cache for a location that fits our table and row key. * Return null if no suitable region is located. * @return Null or region location found in cache. */ RegionLocations getCachedLocation(final TableName tableName, final byte [] row) { return metaCache.getCachedLocation(tableName, row); }