From cf34cff951d2fe9a895734cf1e257dfd8780c43c Mon Sep 17 00:00:00 2001 From: zhaoyuan Date: Tue, 12 Jun 2018 23:27:05 +0800 Subject: [PATCH] update --- .../org/apache/hadoop/hbase/client/HRegionLocator.java | 18 +++++++++++++++--- .../java/org/apache/hadoop/hbase/client/TestHCM.java | 8 ++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HRegionLocator.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HRegionLocator.java index d2706074e2..c5885a9ade 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HRegionLocator.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/HRegionLocator.java @@ -20,6 +20,8 @@ package org.apache.hadoop.hbase.client; import java.io.IOException; import java.util.ArrayList; +import java.util.Map; +import java.util.HashMap; import java.util.List; import java.util.NavigableMap; import java.util.Map.Entry; @@ -89,11 +91,21 @@ public class HRegionLocator implements RegionLocator { NavigableMap locations = MetaScanner.allTableRegions(this.connection, tableName); ArrayList regions = new ArrayList<>(locations.size()); + Map,List> regionLocationsMap = new HashMap<>(); for (Entry entry : locations.entrySet()) { - regions.add(new HRegionLocation(entry.getKey(), entry.getValue())); + HRegionLocation regionLocation = new HRegionLocation(entry.getKey(), entry.getValue()); + Pair key = new Pair<>(regionLocation.getRegionInfo().getStartKey(), + regionLocation.getRegionInfo().getEndKey()); + List regionLocationList = regionLocationsMap.get(key); + if(null == regionLocationList){ + regionLocationList = new ArrayList<>(); + regionLocationsMap.put(key,regionLocationList); + } + regionLocationList.add(regionLocation); + regions.add(regionLocation); } - if (regions.size() > 0) { - connection.cacheLocation(tableName, new RegionLocations(regions)); + for(Entry,List> entry:regionLocationsMap.entrySet()){ + connection.cacheLocation(tableName,new RegionLocations(entry.getValue())); } return regions; } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestHCM.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestHCM.java index e592fa8303..e6dec3c122 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestHCM.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestHCM.java @@ -18,6 +18,7 @@ */ package org.apache.hadoop.hbase.client; +import static org.apache.hadoop.hbase.HBaseTestingUtility.KEYS_FOR_HBA_CREATE_TABLE; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; @@ -738,6 +739,13 @@ public class TestHCM { conn.clearRegionCache(TABLE_NAME, ROW.clone()); RegionLocations rl = conn.getCachedLocation(TABLE_NAME, ROW); assertNull("What is this location?? " + rl, rl); + // We're now going to test getAllRegionLocations() whether or not cache all region locations + conn.clearRegionCache(TABLE_NAME); + conn.getRegionLocator(TABLE_NAME).getAllRegionLocations(); + assertNotNull(conn.getCachedLocation(TABLE_NAME,Bytes.toBytes("aaa"))); + for(byte[] startKey:KEYS_FOR_HBA_CREATE_TABLE){ + assertNotNull(conn.getCachedLocation(TABLE_NAME,startKey)); + } // We're now going to move the region and check that it works for the client // First a new put to add the location in the cache -- 2.13.3