HBASE-5563 Add comparison of regionId to HRegionInfo#compareTo (chunhui and jmhsieh)

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1301779 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jonathan Hsieh 2012-03-16 21:50:05 +00:00
parent 1d24d71821
commit f34edd6378
3 changed files with 24 additions and 1 deletions

View File

@ -791,6 +791,15 @@ implements WritableComparable<HRegionInfo> {
} }
return result; return result;
} }
// regionId is usually milli timestamp -- this defines older stamps
// to be "smaller" than newer stamps in sort order.
if (this.regionId > o.regionId) {
return 1;
} else if (this.regionId < o.regionId) {
return -1;
}
if (this.offLine == o.offLine) if (this.offLine == o.offLine)
return 0; return 0;
if (this.offLine == true) return -1; if (this.offLine == true) return -1;

View File

@ -2780,8 +2780,10 @@ public class AssignmentManager extends ZooKeeperListener {
*/ */
public List<HRegionInfo> getRegionsOfTable(byte[] tableName) { public List<HRegionInfo> getRegionsOfTable(byte[] tableName) {
List<HRegionInfo> tableRegions = new ArrayList<HRegionInfo>(); List<HRegionInfo> tableRegions = new ArrayList<HRegionInfo>();
// boundary needs to have table's name but regionID 0 so that it is sorted
// before all table's regions.
HRegionInfo boundary = HRegionInfo boundary =
new HRegionInfo(tableName, null, null); new HRegionInfo(tableName, null, null, false, 0L);
synchronized (this.regions) { synchronized (this.regions) {
for (HRegionInfo regionInfo: this.regions.tailMap(boundary).keySet()) { for (HRegionInfo regionInfo: this.regions.tailMap(boundary).keySet()) {
if(Bytes.equals(regionInfo.getTableName(), tableName)) { if(Bytes.equals(regionInfo.getTableName(), tableName)) {

View File

@ -130,6 +130,18 @@ public class TestHRegionInfo {
assertTrue(HRegionInfo.FIRST_META_REGIONINFO.isMetaTable()); assertTrue(HRegionInfo.FIRST_META_REGIONINFO.isMetaTable());
} }
@Test
public void testComparator() {
byte[] tablename = Bytes.toBytes("comparatorTablename");
byte[] empty = new byte[0];
HRegionInfo older = new HRegionInfo(tablename, empty, empty, false, 0L);
HRegionInfo newer = new HRegionInfo(tablename, empty, empty, false, 1L);
assertTrue(older.compareTo(newer) < 0);
assertTrue(newer.compareTo(older) > 0);
assertTrue(older.compareTo(older) == 0);
assertTrue(newer.compareTo(newer) == 0);
}
@org.junit.Rule @org.junit.Rule
public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu = public org.apache.hadoop.hbase.ResourceCheckerJUnitRule cu =
new org.apache.hadoop.hbase.ResourceCheckerJUnitRule(); new org.apache.hadoop.hbase.ResourceCheckerJUnitRule();