Index: src/test/org/apache/hadoop/hbase/TestHBaseConfiguration.java =================================================================== --- src/test/org/apache/hadoop/hbase/TestHBaseConfiguration.java (revision 0) +++ src/test/org/apache/hadoop/hbase/TestHBaseConfiguration.java (revision 0) @@ -0,0 +1,60 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.hbase; + +import junit.framework.TestCase; + +/** + * Test HBaseConfiguration. + */ +public class TestHBaseConfiguration extends TestCase { + + private HBaseConfiguration conf1, conf2, conf3; + + @Override + protected void setUp() throws Exception { + conf1 = new HBaseConfiguration(); + conf2 = new HBaseConfiguration(); + conf3 = new HBaseConfiguration(); + } + + /** + * Tests HBaseConfiguration.hashcode() + */ + public void testHashcode() { + assertEquals(conf1.hashCode(), conf1.hashCode()); + assertEquals(conf1.hashCode(), conf2.hashCode()); + assertEquals(conf1.hashCode(), conf3.hashCode()); + + conf1.set("attribute1", "value1"); + conf2.set("attribute1", "value1"); + conf3.set("attribute2", "value2"); + assertEquals(conf1.hashCode(), conf1.hashCode()); + assertEquals(conf1.hashCode(), conf2.hashCode()); + assertNotSame(conf1.hashCode(), conf3.hashCode()); + + conf1.set("attribute2", "value2"); + conf2.set("attribute2", "value2"); + conf3.set("attribute1", "value1"); + assertEquals(conf1.hashCode(), conf1.hashCode()); + assertEquals(conf1.hashCode(), conf2.hashCode()); + assertEquals(conf1.hashCode(), conf3.hashCode()); + } + +} \ No newline at end of file Index: src/test/org/apache/hadoop/hbase/client/TestHConnectionManager.java =================================================================== --- src/test/org/apache/hadoop/hbase/client/TestHConnectionManager.java (revision 0) +++ src/test/org/apache/hadoop/hbase/client/TestHConnectionManager.java (revision 0) @@ -0,0 +1,75 @@ +/** + * Copyright 2007 The Apache Software Foundation + * + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.hadoop.hbase.client; + +import junit.framework.TestCase; + +import org.apache.hadoop.hbase.HBaseConfiguration; +import org.apache.hadoop.hbase.HConstants; + +/** + * Tests HConnectionManager + */ +public class TestHConnectionManager extends TestCase { + + private HBaseConfiguration conf1, conf2; + + @Override + protected void setUp() throws Exception { + conf1 = new HBaseConfiguration(); + conf1.set(HConstants.HBASE_DIR, "hdfs://localhost:9000/hbase"); + conf1.set(HConstants.MASTER_ADDRESS, "localhost"); + conf1.set(HConstants.HBASE_CLIENT_RETRIES_NUMBER_KEY, "10"); + conf2 = new HBaseConfiguration(); + conf2.set(HConstants.HBASE_DIR, "hdfs://localhost:9000/hbase"); + conf2.set(HConstants.MASTER_ADDRESS, "localhost"); + conf1.set(HConstants.HBASE_CLIENT_RETRIES_NUMBER_KEY, "1"); + } + + /** + * Tests HConnectionManager.getConnection(). + */ + public void testGetConnection() { + HConnection connection = HConnectionManager.getConnection(conf1); + assertEquals(connection, HConnectionManager.getConnection(conf1)); + assertNotSame(connection, HConnectionManager.getConnection(conf2)); + + conf1.set("property", "value"); + assertNotSame(connection, HConnectionManager.getConnection(conf1)); + + connection = HConnectionManager.getConnection(conf1); + assertEquals(connection, HConnectionManager.getConnection(conf1)); + conf1.set(HConstants.HBASE_DIR, "hdfs://localhost:9000/hbase1"); + assertNotSame(connection, HConnectionManager.getConnection(conf1)); + } + + /** + * Tests HConnectionManager.deleteConnectionInfo() + */ + public void testDeleteConnectionInfo() { + HConnection connection1 = HConnectionManager.getConnection(conf1); + HConnectionManager.deleteConnectionInfo(conf1, false); + assertNotSame(connection1, HConnectionManager.getConnection(conf1)); + + connection1 = HConnectionManager.getConnection(conf1); + HConnectionManager.deleteConnectionInfo(conf2, false); + assertEquals(connection1, HConnectionManager.getConnection(conf1)); + } +} Index: src/java/org/apache/hadoop/hbase/HBaseConfiguration.java =================================================================== --- src/java/org/apache/hadoop/hbase/HBaseConfiguration.java (revision 752960) +++ src/java/org/apache/hadoop/hbase/HBaseConfiguration.java (working copy) @@ -19,6 +19,7 @@ */ package org.apache.hadoop.hbase; +import java.util.Iterator; import java.util.Map.Entry; import org.apache.hadoop.conf.Configuration; @@ -48,4 +49,22 @@ addResource("hbase-default.xml"); addResource("hbase-site.xml"); } + + /** + * Returns the hash code value for this HBaseConfiguration. The hash code of a + * HBaseConfiguration is defined by the sum of the hash codes of its entries. + * + * @see Configuration#iterator() How the entries are obtained. + */ + @Override + public int hashCode() { + int hash = 0; + + Iterator> propertyIterator = this.iterator(); + while (propertyIterator.hasNext()) { + hash ^= propertyIterator.next().hashCode(); + } + return hash; + } + } Index: src/java/org/apache/hadoop/hbase/client/HConnectionManager.java =================================================================== --- src/java/org/apache/hadoop/hbase/client/HConnectionManager.java (revision 752960) +++ src/java/org/apache/hadoop/hbase/client/HConnectionManager.java (working copy) @@ -27,6 +27,7 @@ import java.util.List; import java.util.Map; import java.util.TreeSet; +import java.util.WeakHashMap; import java.util.concurrent.ConcurrentHashMap; import org.apache.commons.logging.Log; @@ -47,10 +48,10 @@ import org.apache.hadoop.hbase.io.BatchUpdate; import org.apache.hadoop.hbase.io.Cell; import org.apache.hadoop.hbase.io.RowResult; +import org.apache.hadoop.hbase.ipc.HBaseRPC; import org.apache.hadoop.hbase.ipc.HBaseRPCProtocolVersion; import org.apache.hadoop.hbase.ipc.HMasterInterface; import org.apache.hadoop.hbase.ipc.HRegionInterface; -import org.apache.hadoop.hbase.ipc.HBaseRPC; import org.apache.hadoop.hbase.util.Bytes; import org.apache.hadoop.hbase.util.MetaUtils; import org.apache.hadoop.hbase.util.SoftValueSortedMap; @@ -73,12 +74,13 @@ super(); } - // A Map of master HServerAddress -> connection information for that instance - // Note that although the Map is synchronized, the objects it contains - // are mutable and hence require synchronized access to them - private static final Map HBASE_INSTANCES = - new ConcurrentHashMap(); - + // A Map of master HBaseConfiguration -> connection information for that + // instance. Note that although the Map is synchronized, the objects it + // contains are mutable and hence require synchronized access to them + private static + final Map HBASE_INSTANCES = + new WeakHashMap(); + /** * Get the connection object for the instance specified by the configuration * If no current connection exists, create a new connection for that instance @@ -88,11 +90,10 @@ public static HConnection getConnection(HBaseConfiguration conf) { TableServers connection; synchronized (HBASE_INSTANCES) { - String instanceName = conf.get(HBASE_DIR); - connection = HBASE_INSTANCES.get(instanceName); + connection = HBASE_INSTANCES.get(conf); if (connection == null) { connection = new TableServers(conf); - HBASE_INSTANCES.put(instanceName, connection); + HBASE_INSTANCES.put(conf, connection); } } return connection; @@ -106,7 +107,7 @@ public static void deleteConnectionInfo(HBaseConfiguration conf, boolean stopProxy) { synchronized (HBASE_INSTANCES) { - TableServers t = HBASE_INSTANCES.remove(conf.get(HBASE_DIR)); + TableServers t = HBASE_INSTANCES.remove(conf); if (t != null) { t.close(stopProxy); }