Skip to content

Commit 75bcd3c

Browse files
authored
HBASE-27991 [hbase-examples] MultiThreadedClientExample throws java.lang.ClassCastException (#5346)
Signed-off-by: Nihal Jain <[email protected]> Signed-off-by: Nick Dimiduk <[email protected]>
1 parent afb3c0d commit 75bcd3c

File tree

2 files changed

+77
-2
lines changed

2 files changed

+77
-2
lines changed

hbase-examples/src/main/java/org/apache/hadoop/hbase/client/example/MultiThreadedClientExample.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
import java.util.concurrent.Callable;
2424
import java.util.concurrent.ExecutorService;
2525
import java.util.concurrent.Executors;
26-
import java.util.concurrent.ForkJoinPool;
2726
import java.util.concurrent.Future;
27+
import java.util.concurrent.LinkedBlockingQueue;
2828
import java.util.concurrent.ThreadFactory;
2929
import java.util.concurrent.ThreadLocalRandom;
30+
import java.util.concurrent.ThreadPoolExecutor;
3031
import java.util.concurrent.TimeUnit;
3132
import org.apache.hadoop.conf.Configured;
3233
import org.apache.hadoop.hbase.Cell;
@@ -129,7 +130,8 @@ public int run(String[] args) throws Exception {
129130
//
130131
// We don't want to mix hbase and business logic.
131132
//
132-
ExecutorService service = new ForkJoinPool(threads * 2);
133+
ThreadPoolExecutor service = new ThreadPoolExecutor(threads * 2, threads * 2, 60L,
134+
TimeUnit.SECONDS, new LinkedBlockingQueue<>());
133135

134136
// Create two different connections showing how it's possible to
135137
// separate different types of requests onto different connections
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.hadoop.hbase.client.example;
19+
20+
import static org.junit.Assert.assertEquals;
21+
import static org.junit.Assert.assertNotEquals;
22+
23+
import org.apache.hadoop.hbase.HBaseClassTestRule;
24+
import org.apache.hadoop.hbase.HBaseTestingUtility;
25+
import org.apache.hadoop.hbase.TableName;
26+
import org.apache.hadoop.hbase.client.Table;
27+
import org.apache.hadoop.hbase.testclassification.ClientTests;
28+
import org.apache.hadoop.hbase.testclassification.MediumTests;
29+
import org.apache.hadoop.hbase.util.Bytes;
30+
import org.junit.AfterClass;
31+
import org.junit.BeforeClass;
32+
import org.junit.ClassRule;
33+
import org.junit.Test;
34+
import org.junit.experimental.categories.Category;
35+
36+
@Category({ ClientTests.class, MediumTests.class })
37+
public class TestMultiThreadedClientExample {
38+
39+
private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
40+
private static String tableName = "test_mt_table";
41+
private static Table table;
42+
static final TableName MY_TABLE_NAME = TableName.valueOf(tableName);
43+
private static byte[] familyName = Bytes.toBytes("d");
44+
private static byte[] columnName = Bytes.toBytes("col");
45+
46+
@ClassRule
47+
public static final HBaseClassTestRule CLASS_RULE =
48+
HBaseClassTestRule.forClass(TestMultiThreadedClientExample.class);
49+
50+
@BeforeClass
51+
public static void setup() throws Exception {
52+
TEST_UTIL.startMiniCluster(1);
53+
table = TEST_UTIL.createTable(MY_TABLE_NAME, familyName);
54+
}
55+
56+
@AfterClass
57+
public static void tearDown() throws Exception {
58+
TEST_UTIL.deleteTable(MY_TABLE_NAME);
59+
TEST_UTIL.shutdownMiniCluster();
60+
}
61+
62+
@Test
63+
public void testMultiThreadedClientExample() throws Exception {
64+
MultiThreadedClientExample example = new MultiThreadedClientExample();
65+
example.setConf(TEST_UTIL.getConfiguration());
66+
String[] args = { tableName, "200" };
67+
// Define assertions to check the returned data here
68+
assertEquals(0, example.run(args));
69+
// Define assertions to check the row count of the table
70+
int rows = TEST_UTIL.countRows(table);
71+
assertNotEquals(0, rows);
72+
}
73+
}

0 commit comments

Comments
 (0)