public synchronized RegionLocations add(RegionLocations locs) {
    byte[] startKey = locs.getRegionLocation().getRegion().getStartKey();
    RegionLocations oldLocs = cache.putIfAbsent(startKey, locs);
    if (oldLocs == null) {
      cleanProblematicOverlappedRegions(locs);
      return locs;
    }

    // check whether the regions are the same, this usually happens when table is split/merged,
    // or deleted and recreated again.
    RegionInfo region = locs.getRegionLocation().getRegion();
    RegionInfo oldRegion = oldLocs.getRegionLocation().getRegion();
    if (region.getEncodedName().equals(oldRegion.getEncodedName())) {
      RegionLocations mergedLocs = oldLocs.mergeLocations(locs);
      if (isEqual(mergedLocs, oldLocs)) {
        // the merged one is the same with the old one, give up
        LOG.trace("Will not add {} to cache because the old value {} "
          + " is newer than us or has the same server name."
          + " Maybe it is updated before we replace it", locs, oldLocs);
        return oldLocs;
      }
      locs = mergedLocs;
    } else {
      // the region is different, here we trust the one we fetched. This maybe wrong but finally
      // the upper layer can detect this and trigger removal of the wrong locations
      if (LOG.isDebugEnabled()) {
        LOG.debug("The newly fetch region {} is different from the old one {} for row '{}',"
          + " try replaying the old one...", region, oldRegion, Bytes.toStringBinary(startKey));
      }
    }

    cache.put(startKey, locs);
    cleanProblematicOverlappedRegions(locs);
    return locs;
  }
Add the given locations to the cache, merging with existing if necessary. Also cleans out any
previously cached locations which may have been superseded by this one (i.e. in case of merged
regions). See {@link #cleanProblematicOverlappedRegions(RegionLocations)}
@param locs the locations to cache
@return the final location (possibly merged) that was added to the cache
hbase-client/src/main/java/org/apache/hadoop/hbase/client/AsyncRegionLocationCache.java