HBASE-1502 Aftermath; fix up of broke tests. Fix TestMultiParallel. HRegionLocation compare should do Location part only, not include HRI that its carrying

git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1097327 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Stack 2011-04-28 05:52:05 +00:00
parent dbccc88d44
commit 46d615b210
2 changed files with 43 additions and 37 deletions

View File

@ -19,18 +19,23 @@
*/ */
package org.apache.hadoop.hbase; package org.apache.hadoop.hbase;
import java.net.InetSocketAddress;
import org.apache.hadoop.hbase.util.Addressing; import org.apache.hadoop.hbase.util.Addressing;
/** /**
* Data structure to hold HRegionInfo and the address for the hosting * Data structure to hold HRegionInfo and the address for the hosting
* HRegionServer. Immutable. * HRegionServer. Immutable. Comparable, but we compare the 'location' only:
* i.e. the hostname and port, and *not* the regioninfo. This means two
* instances are the same if they refer to the same 'location' (the same
* hostname and port), though they may be carrying different regions.
*/ */
public class HRegionLocation implements Comparable<HRegionLocation> { public class HRegionLocation implements Comparable<HRegionLocation> {
private final HRegionInfo regionInfo; private final HRegionInfo regionInfo;
private final String hostname; private final String hostname;
private final int port; private final int port;
// Cache of the 'toString' result.
private String cachedString = null;
// Cache of the hostname + port
private String cachedHostnamePort;
/** /**
* Constructor * Constructor
@ -49,9 +54,12 @@ public class HRegionLocation implements Comparable<HRegionLocation> {
* @see java.lang.Object#toString() * @see java.lang.Object#toString()
*/ */
@Override @Override
public String toString() { public synchronized String toString() {
return "region=" + this.regionInfo.getRegionNameAsString() + if (this.cachedString == null) {
this.cachedString = "region=" + this.regionInfo.getRegionNameAsString() +
", hostname=" + this.hostname + ", port=" + this.port; ", hostname=" + this.hostname + ", port=" + this.port;
}
return this.cachedString;
} }
/** /**
@ -76,8 +84,7 @@ public class HRegionLocation implements Comparable<HRegionLocation> {
*/ */
@Override @Override
public int hashCode() { public int hashCode() {
int result = this.regionInfo.hashCode(); int result = this.hostname.hashCode();
result ^= this.hostname.hashCode();
result ^= this.port; result ^= this.port;
return result; return result;
} }
@ -105,12 +112,12 @@ public class HRegionLocation implements Comparable<HRegionLocation> {
/** /**
* @return String made of hostname and port formatted as per {@link Addressing#createHostAndPortStr(String, int)} * @return String made of hostname and port formatted as per {@link Addressing#createHostAndPortStr(String, int)}
*/ */
public String getHostnamePort() { public synchronized String getHostnamePort() {
return Addressing.createHostAndPortStr(this.hostname, this.port); if (this.cachedHostnamePort == null) {
} this.cachedHostnamePort =
Addressing.createHostAndPortStr(this.hostname, this.port);
public InetSocketAddress getInetSocketAddress() { }
return new InetSocketAddress(this.hostname, this.port); return this.cachedHostnamePort;
} }
// //
@ -118,9 +125,7 @@ public class HRegionLocation implements Comparable<HRegionLocation> {
// //
public int compareTo(HRegionLocation o) { public int compareTo(HRegionLocation o) {
int result = this.regionInfo.compareTo(o.regionInfo); int result = this.hostname.compareTo(o.getHostname());
if (result != 0) return result;
result = this.hostname.compareTo(o.getHostname());
if (result != 0) return result; if (result != 0) return result;
return this.port - o.getPort(); return this.port - o.getPort();
} }

View File

@ -23,7 +23,6 @@ import java.io.IOException;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.ThreadPoolExecutor;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -52,6 +51,7 @@ public class TestMultiParallel {
private static final byte [][] KEYS = makeKeys(); private static final byte [][] KEYS = makeKeys();
private static final int slaves = 2; // also used for testing HTable pool size private static final int slaves = 2; // also used for testing HTable pool size
@BeforeClass public static void beforeClass() throws Exception { @BeforeClass public static void beforeClass() throws Exception {
UTIL.startMiniCluster(slaves); UTIL.startMiniCluster(slaves);
HTable t = UTIL.createTable(Bytes.toBytes(TEST_TABLE), Bytes.toBytes(FAMILY)); HTable t = UTIL.createTable(Bytes.toBytes(TEST_TABLE), Bytes.toBytes(FAMILY));
@ -105,6 +105,27 @@ public class TestMultiParallel {
return keys.toArray(new byte [][] {new byte [] {}}); return keys.toArray(new byte [][] {new byte [] {}});
} }
/**
* This is for testing the active number of threads that were used while
* doing a batch operation. It inserts one row per region via the batch
* operation, and then checks the number of active threads.
* For HBASE-3553
* @throws IOException
* @throws InterruptedException
* @throws NoSuchFieldException
* @throws SecurityException
*/
@Test public void testActiveThreadsCount() throws Exception{
HTable table = new HTable(UTIL.getConfiguration(), TEST_TABLE);
List<Row> puts = constructPutRequests(); // creates a Put for every region
table.batch(puts);
Field poolField = table.getClass().getDeclaredField("pool");
poolField.setAccessible(true);
ThreadPoolExecutor tExecutor = (ThreadPoolExecutor) poolField.get(table);
assertEquals(slaves, tExecutor.getLargestPoolSize());
}
@Test public void testBatchWithGet() throws Exception { @Test public void testBatchWithGet() throws Exception {
LOG.info("test=testBatchWithGet"); LOG.info("test=testBatchWithGet");
HTable table = new HTable(UTIL.getConfiguration(), TEST_TABLE); HTable table = new HTable(UTIL.getConfiguration(), TEST_TABLE);
@ -466,24 +487,4 @@ public class TestMultiParallel {
validateEmpty(result); validateEmpty(result);
} }
} }
/**
* This is for testing the active number of threads that were used while
* doing a batch operation. It inserts one row per region via the batch
* operation, and then checks the number of active threads.
* For HBASE-3553
* @throws IOException
* @throws InterruptedException
* @throws NoSuchFieldException
* @throws SecurityException
*/
@Test public void testActiveThreadsCount() throws Exception{
HTable table = new HTable(UTIL.getConfiguration(), TEST_TABLE);
List<Row> puts = constructPutRequests(); // creates a Put for every region
table.batch(puts);
Field poolField = table.getClass().getDeclaredField("pool");
poolField.setAccessible(true);
ThreadPoolExecutor tExecutor = (ThreadPoolExecutor) poolField.get(table);
assertEquals(slaves, tExecutor.getLargestPoolSize());
}
} }