1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.client;
21
22 import java.lang.reflect.Field;
23 import java.util.ArrayList;
24 import java.util.HashSet;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.Random;
28 import java.util.Set;
29
30 import org.apache.hadoop.conf.Configuration;
31 import org.apache.hadoop.hbase.HBaseConfiguration;
32 import org.apache.hadoop.hbase.HBaseTestingUtility;
33 import org.apache.hadoop.hbase.HRegionLocation;
34 import org.apache.hadoop.hbase.ZooKeeperConnectionException;
35 import org.apache.hadoop.hbase.util.Bytes;
36 import org.junit.AfterClass;
37 import org.junit.Assert;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.apache.commons.logging.Log;
41 import org.apache.commons.logging.LogFactory;
42
43 import static org.junit.Assert.assertNull;
44 import static org.junit.Assert.assertNotNull;
45
46
47
48
49 public class TestHCM {
50 private static final Log LOG = LogFactory.getLog(TestHCM.class);
51 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
52 private static final byte[] TABLE_NAME = Bytes.toBytes("test");
53 private static final byte[] FAM_NAM = Bytes.toBytes("f");
54 private static final byte[] ROW = Bytes.toBytes("bbb");
55
56 @BeforeClass
57 public static void setUpBeforeClass() throws Exception {
58 TEST_UTIL.startMiniCluster(1);
59 }
60
61 @AfterClass public static void tearDownAfterClass() throws Exception {
62 TEST_UTIL.shutdownMiniCluster();
63 }
64
65
66
67
68
69
70
71
72
73
74 @Test public void testManyNewConnectionsDoesnotOOME()
75 throws SecurityException, IllegalArgumentException,
76 ZooKeeperConnectionException, NoSuchFieldException, IllegalAccessException,
77 InterruptedException {
78 createNewConfigurations();
79 }
80
81 private static Random _randy = new Random();
82
83 public static void createNewConfigurations() throws SecurityException,
84 IllegalArgumentException, NoSuchFieldException,
85 IllegalAccessException, InterruptedException, ZooKeeperConnectionException {
86 HConnection last = null;
87 for (int i = 0; i <= (HConnectionManager.MAX_CACHED_HBASE_INSTANCES * 2); i++) {
88
89 Configuration configuration = HBaseConfiguration.create();
90 configuration.set("somekey", String.valueOf(_randy.nextInt()));
91 System.out.println("Hash Code: " + configuration.hashCode());
92 HConnection connection =
93 HConnectionManager.getConnection(configuration);
94 if (last != null) {
95 if (last == connection) {
96 System.out.println("!! Got same connection for once !!");
97 }
98 }
99
100
101
102 configuration.set("someotherkey", String.valueOf(_randy.nextInt()));
103 last = connection;
104 LOG.info("Cache Size: "
105 + getHConnectionManagerCacheSize() + ", Valid Keys: "
106 + getValidKeyCount());
107 Thread.sleep(100);
108 }
109 Assert.assertEquals(HConnectionManager.MAX_CACHED_HBASE_INSTANCES,
110 getHConnectionManagerCacheSize());
111 Assert.assertEquals(HConnectionManager.MAX_CACHED_HBASE_INSTANCES,
112 getValidKeyCount());
113 }
114
115 private static int getHConnectionManagerCacheSize()
116 throws SecurityException, NoSuchFieldException,
117 IllegalArgumentException, IllegalAccessException {
118 Field cacheField =
119 HConnectionManager.class.getDeclaredField("HBASE_INSTANCES");
120 cacheField.setAccessible(true);
121 Map<?, ?> cache = (Map<?, ?>) cacheField.get(null);
122 return cache.size();
123 }
124
125 private static int getValidKeyCount() throws SecurityException,
126 NoSuchFieldException, IllegalArgumentException,
127 IllegalAccessException {
128 Field cacheField =
129 HConnectionManager.class.getDeclaredField("HBASE_INSTANCES");
130 cacheField.setAccessible(true);
131 Map<?, ?> cache = (Map<?, ?>) cacheField.get(null);
132 List<Object> keys = new ArrayList<Object>(cache.keySet());
133 Set<Object> values = new HashSet<Object>();
134 for (Object key : keys) {
135 values.add(cache.get(key));
136 }
137 return values.size();
138 }
139
140
141
142
143
144
145 @Test
146 public void testRegionCaching() throws Exception{
147 HTable table = TEST_UTIL.createTable(TABLE_NAME, FAM_NAM);
148 TEST_UTIL.createMultiRegions(table, FAM_NAM);
149 Put put = new Put(ROW);
150 put.add(FAM_NAM, ROW, ROW);
151 table.put(put);
152 HConnectionManager.HConnectionImplementation conn =
153 (HConnectionManager.HConnectionImplementation)table.getConnection();
154 assertNotNull(conn.getCachedLocation(TABLE_NAME, ROW));
155 conn.deleteCachedLocation(TABLE_NAME, ROW);
156 HRegionLocation rl = conn.getCachedLocation(TABLE_NAME, ROW);
157 assertNull("What is this location?? " + rl, rl);
158 }
159 }