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
75
76
77 public void testManyNewConnectionsDoesnotOOME()
78 throws SecurityException, IllegalArgumentException,
79 ZooKeeperConnectionException, NoSuchFieldException, IllegalAccessException,
80 InterruptedException {
81 createNewConfigurations();
82 }
83
84 private static Random _randy = new Random();
85
86 public static void createNewConfigurations() throws SecurityException,
87 IllegalArgumentException, NoSuchFieldException,
88 IllegalAccessException, InterruptedException, ZooKeeperConnectionException {
89 HConnection last = null;
90 for (int i = 0; i <= (HConnectionManager.MAX_CACHED_HBASE_INSTANCES * 2); i++) {
91
92 Configuration configuration = HBaseConfiguration.create();
93 configuration.set("somekey", String.valueOf(_randy.nextInt()));
94 System.out.println("Hash Code: " + configuration.hashCode());
95 HConnection connection = HConnectionManager.getConnection(configuration);
96 if (last != null) {
97 if (last == connection) {
98 System.out.println("!! Got same connection for once !!");
99 }
100 }
101
102
103
104 configuration.set("someotherkey", String.valueOf(_randy.nextInt()));
105 last = connection;
106 LOG.info("Cache Size: "
107 + getHConnectionManagerCacheSize() + ", Valid Keys: "
108 + getValidKeyCount());
109 Thread.sleep(100);
110 }
111 Assert.assertEquals(HConnectionManager.MAX_CACHED_HBASE_INSTANCES,
112 getHConnectionManagerCacheSize());
113 Assert.assertEquals(HConnectionManager.MAX_CACHED_HBASE_INSTANCES,
114 getValidKeyCount());
115 }
116
117 private static int getHConnectionManagerCacheSize()
118 throws SecurityException, NoSuchFieldException,
119 IllegalArgumentException, IllegalAccessException {
120 Field cacheField =
121 HConnectionManager.class.getDeclaredField("HBASE_INSTANCES");
122 cacheField.setAccessible(true);
123 Map<?, ?> cache = (Map<?, ?>) cacheField.get(null);
124 return cache.size();
125 }
126
127 private static int getValidKeyCount() throws SecurityException,
128 NoSuchFieldException, IllegalArgumentException,
129 IllegalAccessException {
130 Field cacheField =
131 HConnectionManager.class.getDeclaredField("HBASE_INSTANCES");
132 cacheField.setAccessible(true);
133 Map<?, ?> cache = (Map<?, ?>) cacheField.get(null);
134 List<Object> keys = new ArrayList<Object>(cache.keySet());
135 Set<Object> values = new HashSet<Object>();
136 for (Object key : keys) {
137 values.add(cache.get(key));
138 }
139 return values.size();
140 }
141
142
143
144
145
146
147 @Test
148 public void testRegionCaching() throws Exception{
149 HTable table = TEST_UTIL.createTable(TABLE_NAME, FAM_NAM);
150 TEST_UTIL.createMultiRegions(table, FAM_NAM);
151 Put put = new Put(ROW);
152 put.add(FAM_NAM, ROW, ROW);
153 table.put(put);
154 HConnectionManager.HConnectionImplementation conn =
155 (HConnectionManager.HConnectionImplementation)table.getConnection();
156 assertNotNull(conn.getCachedLocation(TABLE_NAME, ROW));
157 conn.deleteCachedLocation(TABLE_NAME, ROW);
158 HRegionLocation rl = conn.getCachedLocation(TABLE_NAME, ROW);
159 assertNull("What is this location?? " + rl, rl);
160 }
161 }