1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.hadoop.hbase.coprocessor;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.apache.hadoop.conf.Configuration;
24 import org.apache.hadoop.hbase.*;
25 import org.apache.hadoop.hbase.client.Admin;
26 import org.apache.hadoop.hbase.regionserver.Region;
27 import org.apache.hadoop.hbase.regionserver.TestServerCustomProtocol;
28 import org.apache.hadoop.hbase.testclassification.MediumTests;
29 import org.apache.hadoop.hbase.util.ClassLoaderTestHelper;
30 import org.apache.hadoop.hbase.util.CoprocessorClassLoader;
31 import org.apache.hadoop.hdfs.MiniDFSCluster;
32 import org.apache.hadoop.fs.FileSystem;
33 import org.apache.hadoop.fs.Path;
34 import org.apache.hadoop.hbase.ServerLoad;
35 import org.apache.hadoop.hbase.RegionLoad;
36
37 import java.io.*;
38 import java.util.*;
39
40 import org.junit.*;
41 import org.junit.experimental.categories.Category;
42
43 import static org.junit.Assert.assertEquals;
44 import static org.junit.Assert.assertNotNull;
45 import static org.junit.Assert.assertTrue;
46 import static org.junit.Assert.assertFalse;
47
48
49
50
51 @Category(MediumTests.class)
52 public class TestClassLoading {
53 private static final Log LOG = LogFactory.getLog(TestClassLoading.class);
54 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
55
56 private static MiniDFSCluster cluster;
57
58 static final TableName tableName = TableName.valueOf("TestClassLoading");
59 static final String cpName1 = "TestCP1";
60 static final String cpName2 = "TestCP2";
61 static final String cpName3 = "TestCP3";
62 static final String cpName4 = "TestCP4";
63 static final String cpName5 = "TestCP5";
64 static final String cpName6 = "TestCP6";
65
66 private static Class<?> regionCoprocessor1 = ColumnAggregationEndpoint.class;
67
68 private static Class<?> regionCoprocessor2 = TestServerCustomProtocol.PingHandler.class;
69 private static Class<?> regionServerCoprocessor = SampleRegionWALObserver.class;
70 private static Class<?> masterCoprocessor = BaseMasterObserver.class;
71
72 private static final String[] regionServerSystemCoprocessors =
73 new String[]{
74 regionServerCoprocessor.getSimpleName()
75 };
76
77 @BeforeClass
78 public static void setUpBeforeClass() throws Exception {
79 Configuration conf = TEST_UTIL.getConfiguration();
80
81
82
83 conf.setStrings(CoprocessorHost.REGION_COPROCESSOR_CONF_KEY,
84 regionCoprocessor1.getName());
85
86
87
88
89 conf.setStrings(CoprocessorHost.USER_REGION_COPROCESSOR_CONF_KEY,
90 regionCoprocessor2.getName());
91
92 conf.setStrings(CoprocessorHost.WAL_COPROCESSOR_CONF_KEY,
93 regionServerCoprocessor.getName());
94 conf.setStrings(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
95 masterCoprocessor.getName());
96 TEST_UTIL.startMiniCluster(1);
97 cluster = TEST_UTIL.getDFSCluster();
98 }
99
100 @AfterClass
101 public static void tearDownAfterClass() throws Exception {
102 TEST_UTIL.shutdownMiniCluster();
103 }
104
105 static File buildCoprocessorJar(String className) throws Exception {
106 String code = "import org.apache.hadoop.hbase.coprocessor.*;" +
107 "public class " + className + " extends BaseRegionObserver {}";
108 return ClassLoaderTestHelper.buildJar(
109 TEST_UTIL.getDataTestDir().toString(), className, code);
110 }
111
112 @Test
113
114 public void testClassLoadingFromHDFS() throws Exception {
115 FileSystem fs = cluster.getFileSystem();
116
117 File jarFile1 = buildCoprocessorJar(cpName1);
118 File jarFile2 = buildCoprocessorJar(cpName2);
119
120
121 fs.copyFromLocalFile(new Path(jarFile1.getPath()),
122 new Path(fs.getUri().toString() + Path.SEPARATOR));
123 String jarFileOnHDFS1 = fs.getUri().toString() + Path.SEPARATOR +
124 jarFile1.getName();
125 Path pathOnHDFS1 = new Path(jarFileOnHDFS1);
126 assertTrue("Copy jar file to HDFS failed.",
127 fs.exists(pathOnHDFS1));
128 LOG.info("Copied jar file to HDFS: " + jarFileOnHDFS1);
129
130 fs.copyFromLocalFile(new Path(jarFile2.getPath()),
131 new Path(fs.getUri().toString() + Path.SEPARATOR));
132 String jarFileOnHDFS2 = fs.getUri().toString() + Path.SEPARATOR +
133 jarFile2.getName();
134 Path pathOnHDFS2 = new Path(jarFileOnHDFS2);
135 assertTrue("Copy jar file to HDFS failed.",
136 fs.exists(pathOnHDFS2));
137 LOG.info("Copied jar file to HDFS: " + jarFileOnHDFS2);
138
139
140 HTableDescriptor htd = new HTableDescriptor(tableName);
141 htd.addFamily(new HColumnDescriptor("test"));
142
143 htd.setValue("COPROCESSOR$1", jarFileOnHDFS1.toString() + "|" + cpName1 +
144 "|" + Coprocessor.PRIORITY_USER);
145
146 htd.setValue("COPROCESSOR$2", jarFileOnHDFS2.toString() + "|" + cpName2 +
147 "|" + Coprocessor.PRIORITY_USER + "|k1=v1,k2=v2,k3=v3");
148 Admin admin = TEST_UTIL.getHBaseAdmin();
149 if (admin.tableExists(tableName)) {
150 if (admin.isTableEnabled(tableName)) {
151 admin.disableTable(tableName);
152 }
153 admin.deleteTable(tableName);
154 }
155 CoprocessorClassLoader.clearCache();
156 byte[] startKey = {10, 63};
157 byte[] endKey = {12, 43};
158 admin.createTable(htd, startKey, endKey, 4);
159 waitForTable(htd.getTableName());
160
161
162 boolean foundTableRegion=false;
163 boolean found1 = true, found2 = true, found2_k1 = true, found2_k2 = true, found2_k3 = true;
164 Map<Region, Set<ClassLoader>> regionsActiveClassLoaders =
165 new HashMap<Region, Set<ClassLoader>>();
166 MiniHBaseCluster hbase = TEST_UTIL.getHBaseCluster();
167 for (Region region:
168 hbase.getRegionServer(0).getOnlineRegionsLocalContext()) {
169 if (region.getRegionInfo().getRegionNameAsString().startsWith(tableName.getNameAsString())) {
170 foundTableRegion = true;
171 CoprocessorEnvironment env;
172 env = region.getCoprocessorHost().findCoprocessorEnvironment(cpName1);
173 found1 = found1 && (env != null);
174 env = region.getCoprocessorHost().findCoprocessorEnvironment(cpName2);
175 found2 = found2 && (env != null);
176 if (env != null) {
177 Configuration conf = env.getConfiguration();
178 found2_k1 = found2_k1 && (conf.get("k1") != null);
179 found2_k2 = found2_k2 && (conf.get("k2") != null);
180 found2_k3 = found2_k3 && (conf.get("k3") != null);
181 } else {
182 found2_k1 = found2_k2 = found2_k3 = false;
183 }
184 regionsActiveClassLoaders
185 .put(region, ((CoprocessorHost) region.getCoprocessorHost()).getExternalClassLoaders());
186 }
187 }
188
189 assertTrue("No region was found for table " + tableName, foundTableRegion);
190 assertTrue("Class " + cpName1 + " was missing on a region", found1);
191 assertTrue("Class " + cpName2 + " was missing on a region", found2);
192 assertTrue("Configuration key 'k1' was missing on a region", found2_k1);
193 assertTrue("Configuration key 'k2' was missing on a region", found2_k2);
194 assertTrue("Configuration key 'k3' was missing on a region", found2_k3);
195
196 assertNotNull(jarFileOnHDFS1 + " was not cached",
197 CoprocessorClassLoader.getIfCached(pathOnHDFS1));
198 assertNotNull(jarFileOnHDFS2 + " was not cached",
199 CoprocessorClassLoader.getIfCached(pathOnHDFS2));
200
201 assertEquals("The number of cached classloaders should be equal to the number" +
202 " of external jar files",
203 2, CoprocessorClassLoader.getAllCached().size());
204
205 Set<ClassLoader> externalClassLoaders = new HashSet<ClassLoader>(
206 CoprocessorClassLoader.getAllCached());
207 for (Map.Entry<Region, Set<ClassLoader>> regionCP : regionsActiveClassLoaders.entrySet()) {
208 assertTrue("Some CP classloaders for region " + regionCP.getKey() + " are not cached."
209 + " ClassLoader Cache:" + externalClassLoaders
210 + " Region ClassLoaders:" + regionCP.getValue(),
211 externalClassLoaders.containsAll(regionCP.getValue()));
212 }
213 }
214
215 private String getLocalPath(File file) {
216 return new Path(file.toURI()).toString();
217 }
218
219 @Test
220
221 public void testClassLoadingFromLocalFS() throws Exception {
222 File jarFile = buildCoprocessorJar(cpName3);
223
224
225 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(cpName3));
226 htd.addFamily(new HColumnDescriptor("test"));
227 htd.setValue("COPROCESSOR$1", getLocalPath(jarFile) + "|" + cpName3 + "|" +
228 Coprocessor.PRIORITY_USER);
229 Admin admin = TEST_UTIL.getHBaseAdmin();
230 admin.createTable(htd);
231 waitForTable(htd.getTableName());
232
233
234 boolean found = false;
235 MiniHBaseCluster hbase = TEST_UTIL.getHBaseCluster();
236 for (Region region: hbase.getRegionServer(0).getOnlineRegionsLocalContext()) {
237 if (region.getRegionInfo().getRegionNameAsString().startsWith(cpName3)) {
238 found = (region.getCoprocessorHost().findCoprocessor(cpName3) != null);
239 }
240 }
241 assertTrue("Class " + cpName3 + " was missing on a region", found);
242 }
243
244 @Test
245
246 public void testPrivateClassLoader() throws Exception {
247 File jarFile = buildCoprocessorJar(cpName4);
248
249
250 HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(cpName4));
251 htd.addFamily(new HColumnDescriptor("test"));
252 htd.setValue("COPROCESSOR$1", getLocalPath(jarFile) + "|" + cpName4 + "|" +
253 Coprocessor.PRIORITY_USER);
254 Admin admin = TEST_UTIL.getHBaseAdmin();
255 admin.createTable(htd);
256 waitForTable(htd.getTableName());
257
258
259 boolean found = false;
260 MiniHBaseCluster hbase = TEST_UTIL.getHBaseCluster();
261 for (Region region: hbase.getRegionServer(0).getOnlineRegionsLocalContext()) {
262 if (region.getRegionInfo().getRegionNameAsString().startsWith(cpName4)) {
263 Coprocessor cp = region.getCoprocessorHost().findCoprocessor(cpName4);
264 if (cp != null) {
265 found = true;
266 assertEquals("Class " + cpName4 + " was not loaded by CoprocessorClassLoader",
267 cp.getClass().getClassLoader().getClass(), CoprocessorClassLoader.class);
268 }
269 }
270 }
271 assertTrue("Class " + cpName4 + " was missing on a region", found);
272 }
273
274 @Test
275
276
277 public void testHBase3810() throws Exception {
278
279
280 File jarFile1 = buildCoprocessorJar(cpName1);
281 File jarFile2 = buildCoprocessorJar(cpName2);
282 File jarFile5 = buildCoprocessorJar(cpName5);
283 File jarFile6 = buildCoprocessorJar(cpName6);
284
285 String cpKey1 = "COPROCESSOR$1";
286 String cpKey2 = " Coprocessor$2 ";
287 String cpKey3 = " coprocessor$03 ";
288
289 String cpValue1 = getLocalPath(jarFile1) + "|" + cpName1 + "|" +
290 Coprocessor.PRIORITY_USER;
291 String cpValue2 = getLocalPath(jarFile2) + " | " + cpName2 + " | ";
292
293 String cpValue3 =
294 " | org.apache.hadoop.hbase.coprocessor.SimpleRegionObserver | | k=v ";
295
296
297 HTableDescriptor htd = new HTableDescriptor(tableName);
298 htd.addFamily(new HColumnDescriptor("test"));
299
300
301 htd.setValue(cpKey1, cpValue1);
302 htd.setValue(cpKey2, cpValue2);
303 htd.setValue(cpKey3, cpValue3);
304
305
306 htd.addCoprocessor(cpName5, new Path(getLocalPath(jarFile5)),
307 Coprocessor.PRIORITY_USER, null);
308 Map<String, String> kvs = new HashMap<String, String>();
309 kvs.put("k1", "v1");
310 kvs.put("k2", "v2");
311 kvs.put("k3", "v3");
312 htd.addCoprocessor(cpName6, new Path(getLocalPath(jarFile6)),
313 Coprocessor.PRIORITY_USER, kvs);
314
315 Admin admin = TEST_UTIL.getHBaseAdmin();
316 if (admin.tableExists(tableName)) {
317 if (admin.isTableEnabled(tableName)) {
318 admin.disableTable(tableName);
319 }
320 admin.deleteTable(tableName);
321 }
322 admin.createTable(htd);
323 waitForTable(htd.getTableName());
324
325
326 boolean found_2 = false, found_1 = false, found_3 = false,
327 found_5 = false, found_6 = false;
328 boolean found6_k1 = false, found6_k2 = false, found6_k3 = false,
329 found6_k4 = false;
330
331 MiniHBaseCluster hbase = TEST_UTIL.getHBaseCluster();
332 for (Region region: hbase.getRegionServer(0).getOnlineRegionsLocalContext()) {
333 if (region.getRegionInfo().getRegionNameAsString().startsWith(tableName.getNameAsString())) {
334 found_1 = found_1 ||
335 (region.getCoprocessorHost().findCoprocessor(cpName1) != null);
336 found_2 = found_2 ||
337 (region.getCoprocessorHost().findCoprocessor(cpName2) != null);
338 found_3 = found_3 ||
339 (region.getCoprocessorHost().findCoprocessor("SimpleRegionObserver")
340 != null);
341 found_5 = found_5 ||
342 (region.getCoprocessorHost().findCoprocessor(cpName5) != null);
343
344 CoprocessorEnvironment env =
345 region.getCoprocessorHost().findCoprocessorEnvironment(cpName6);
346 if (env != null) {
347 found_6 = true;
348 Configuration conf = env.getConfiguration();
349 found6_k1 = conf.get("k1") != null;
350 found6_k2 = conf.get("k2") != null;
351 found6_k3 = conf.get("k3") != null;
352 }
353 }
354 }
355
356 assertTrue("Class " + cpName1 + " was missing on a region", found_1);
357 assertTrue("Class " + cpName2 + " was missing on a region", found_2);
358 assertTrue("Class SimpleRegionObserver was missing on a region", found_3);
359 assertTrue("Class " + cpName5 + " was missing on a region", found_5);
360 assertTrue("Class " + cpName6 + " was missing on a region", found_6);
361
362 assertTrue("Configuration key 'k1' was missing on a region", found6_k1);
363 assertTrue("Configuration key 'k2' was missing on a region", found6_k2);
364 assertTrue("Configuration key 'k3' was missing on a region", found6_k3);
365 assertFalse("Configuration key 'k4' wasn't configured", found6_k4);
366 }
367
368 @Test
369 public void testClassLoadingFromLibDirInJar() throws Exception {
370 loadingClassFromLibDirInJar("/lib/");
371 }
372
373 @Test
374 public void testClassLoadingFromRelativeLibDirInJar() throws Exception {
375 loadingClassFromLibDirInJar("lib/");
376 }
377
378 void loadingClassFromLibDirInJar(String libPrefix) throws Exception {
379 FileSystem fs = cluster.getFileSystem();
380
381 File innerJarFile1 = buildCoprocessorJar(cpName1);
382 File innerJarFile2 = buildCoprocessorJar(cpName2);
383 File outerJarFile = new File(TEST_UTIL.getDataTestDir().toString(), "outer.jar");
384
385 ClassLoaderTestHelper.addJarFilesToJar(
386 outerJarFile, libPrefix, innerJarFile1, innerJarFile2);
387
388
389 fs.copyFromLocalFile(new Path(outerJarFile.getPath()),
390 new Path(fs.getUri().toString() + Path.SEPARATOR));
391 String jarFileOnHDFS = fs.getUri().toString() + Path.SEPARATOR +
392 outerJarFile.getName();
393 assertTrue("Copy jar file to HDFS failed.",
394 fs.exists(new Path(jarFileOnHDFS)));
395 LOG.info("Copied jar file to HDFS: " + jarFileOnHDFS);
396
397
398 HTableDescriptor htd = new HTableDescriptor(tableName);
399 htd.addFamily(new HColumnDescriptor("test"));
400
401 htd.setValue("COPROCESSOR$1", jarFileOnHDFS.toString() + "|" + cpName1 +
402 "|" + Coprocessor.PRIORITY_USER);
403
404 htd.setValue("COPROCESSOR$2", jarFileOnHDFS.toString() + "|" + cpName2 +
405 "|" + Coprocessor.PRIORITY_USER + "|k1=v1,k2=v2,k3=v3");
406 Admin admin = TEST_UTIL.getHBaseAdmin();
407 if (admin.tableExists(tableName)) {
408 if (admin.isTableEnabled(tableName)) {
409 admin.disableTable(tableName);
410 }
411 admin.deleteTable(tableName);
412 }
413 admin.createTable(htd);
414 waitForTable(htd.getTableName());
415
416
417 boolean found1 = false, found2 = false, found2_k1 = false,
418 found2_k2 = false, found2_k3 = false;
419 MiniHBaseCluster hbase = TEST_UTIL.getHBaseCluster();
420 for (Region region: hbase.getRegionServer(0).getOnlineRegionsLocalContext()) {
421 if (region.getRegionInfo().getRegionNameAsString().startsWith(tableName.getNameAsString())) {
422 CoprocessorEnvironment env;
423 env = region.getCoprocessorHost().findCoprocessorEnvironment(cpName1);
424 if (env != null) {
425 found1 = true;
426 }
427 env = region.getCoprocessorHost().findCoprocessorEnvironment(cpName2);
428 if (env != null) {
429 found2 = true;
430 Configuration conf = env.getConfiguration();
431 found2_k1 = conf.get("k1") != null;
432 found2_k2 = conf.get("k2") != null;
433 found2_k3 = conf.get("k3") != null;
434 }
435 }
436 }
437 assertTrue("Class " + cpName1 + " was missing on a region", found1);
438 assertTrue("Class " + cpName2 + " was missing on a region", found2);
439 assertTrue("Configuration key 'k1' was missing on a region", found2_k1);
440 assertTrue("Configuration key 'k2' was missing on a region", found2_k2);
441 assertTrue("Configuration key 'k3' was missing on a region", found2_k3);
442 }
443
444 @Test
445 public void testRegionServerCoprocessorsReported() throws Exception {
446
447
448
449 assertAllRegionServers(regionServerSystemCoprocessors,null);
450 }
451
452
453
454
455
456
457
458
459
460
461 Map<ServerName, ServerLoad> serversForTable(String tableName) {
462 Map<ServerName, ServerLoad> serverLoadHashMap =
463 new HashMap<ServerName, ServerLoad>();
464 for(Map.Entry<ServerName,ServerLoad> server:
465 TEST_UTIL.getMiniHBaseCluster().getMaster().getServerManager().
466 getOnlineServers().entrySet()) {
467 for( Map.Entry<byte[], RegionLoad> region:
468 server.getValue().getRegionsLoad().entrySet()) {
469 if (region.getValue().getNameAsString().equals(tableName)) {
470
471 serverLoadHashMap.put(server.getKey(),server.getValue());
472
473 break;
474 }
475 }
476 }
477 return serverLoadHashMap;
478 }
479
480 void assertAllRegionServers(String[] expectedCoprocessors, String tableName)
481 throws InterruptedException {
482 Map<ServerName, ServerLoad> servers;
483 String[] actualCoprocessors = null;
484 boolean success = false;
485 for(int i = 0; i < 5; i++) {
486 if (tableName == null) {
487
488 servers =
489 TEST_UTIL.getMiniHBaseCluster().getMaster().getServerManager().
490 getOnlineServers();
491 } else {
492 servers = serversForTable(tableName);
493 }
494 boolean any_failed = false;
495 for(Map.Entry<ServerName,ServerLoad> server: servers.entrySet()) {
496 actualCoprocessors = server.getValue().getRsCoprocessors();
497 if (!Arrays.equals(actualCoprocessors, expectedCoprocessors)) {
498 LOG.debug("failed comparison: actual: " +
499 Arrays.toString(actualCoprocessors) +
500 " ; expected: " + Arrays.toString(expectedCoprocessors));
501 any_failed = true;
502 break;
503 }
504 }
505 if (any_failed == false) {
506 success = true;
507 break;
508 }
509 LOG.debug("retrying after failed comparison: " + i);
510 Thread.sleep(1000);
511 }
512 assertTrue(success);
513 }
514
515 @Test
516 public void testMasterCoprocessorsReported() {
517
518
519
520 final String loadedMasterCoprocessorsVerify =
521 "[" + masterCoprocessor.getSimpleName() + "]";
522 String loadedMasterCoprocessors =
523 java.util.Arrays.toString(
524 TEST_UTIL.getHBaseCluster().getMaster().getMasterCoprocessors());
525 assertEquals(loadedMasterCoprocessorsVerify, loadedMasterCoprocessors);
526 }
527
528 @Test
529 public void testFindCoprocessors() {
530
531 CoprocessorHost masterCpHost =
532 TEST_UTIL.getHBaseCluster().getMaster().getMasterCoprocessorHost();
533
534 List<MasterObserver> masterObservers = masterCpHost.findCoprocessors(MasterObserver.class);
535
536 assertTrue(masterObservers != null && masterObservers.size() > 0);
537 assertEquals(masterCoprocessor.getSimpleName(),
538 masterObservers.get(0).getClass().getSimpleName());
539 }
540
541 private void waitForTable(TableName name) throws InterruptedException, IOException {
542
543 TEST_UTIL.waitTableEnabled(name);
544
545 Thread.sleep(1000);
546 }
547
548 }
549