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 static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23
24 import java.io.IOException;
25 import java.util.Collections;
26 import java.util.concurrent.ExecutorService;
27 import java.util.concurrent.SynchronousQueue;
28 import java.util.concurrent.ThreadPoolExecutor;
29 import java.util.concurrent.TimeUnit;
30
31 import org.apache.hadoop.hbase.HBaseTestingUtility;
32 import org.apache.hadoop.hbase.HColumnDescriptor;
33 import org.apache.hadoop.hbase.HTableDescriptor;
34 import org.apache.hadoop.hbase.testclassification.MediumTests;
35 import org.apache.hadoop.hbase.TableName;
36 import org.apache.hadoop.hbase.client.Admin;
37 import org.apache.hadoop.hbase.client.Durability;
38 import org.apache.hadoop.hbase.client.HTable;
39 import org.apache.hadoop.hbase.client.Put;
40 import org.apache.hadoop.hbase.client.Result;
41 import org.apache.hadoop.hbase.client.ResultScanner;
42 import org.apache.hadoop.hbase.client.Scan;
43 import org.apache.hadoop.hbase.client.Table;
44 import org.apache.hadoop.hbase.regionserver.wal.WALEdit;
45 import org.apache.hadoop.hbase.util.Threads;
46 import org.junit.After;
47 import org.junit.AfterClass;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50 import org.junit.experimental.categories.Category;
51
52
53
54
55 @Category(MediumTests.class)
56 public class TestOpenTableInCoprocessor {
57
58 private static final TableName otherTable = TableName.valueOf("otherTable");
59 private static final TableName primaryTable = TableName.valueOf("primary");
60 private static final byte[] family = new byte[] { 'f' };
61
62 private static boolean[] completed = new boolean[1];
63
64
65
66 public static class SendToOtherTableCoprocessor extends BaseRegionObserver {
67
68 @Override
69 public void prePut(final ObserverContext<RegionCoprocessorEnvironment> e, final Put put,
70 final WALEdit edit, final Durability durability) throws IOException {
71 Table table = e.getEnvironment().getTable(otherTable);
72 table.put(put);
73 completed[0] = true;
74 table.close();
75 }
76
77 }
78
79 private static boolean[] completedWithPool = new boolean[1];
80
81
82
83 public static class CustomThreadPoolCoprocessor extends BaseRegionObserver {
84
85
86
87
88
89 private ExecutorService getPool() {
90 int maxThreads = 1;
91 long keepAliveTime = 60;
92 ThreadPoolExecutor pool =
93 new ThreadPoolExecutor(1, maxThreads, keepAliveTime, TimeUnit.SECONDS,
94 new SynchronousQueue<Runnable>(), Threads.newDaemonThreadFactory("hbase-table"));
95 pool.allowCoreThreadTimeOut(true);
96 return pool;
97 }
98
99 @Override
100 public void prePut(final ObserverContext<RegionCoprocessorEnvironment> e, final Put put,
101 final WALEdit edit, final Durability durability) throws IOException {
102 Table table = e.getEnvironment().getTable(otherTable, getPool());
103 Put p = new Put(new byte[] { 'a' });
104 p.add(family, null, new byte[] { 'a' });
105 try {
106 table.batch(Collections.singletonList(put));
107 } catch (InterruptedException e1) {
108 throw new IOException(e1);
109 }
110 completedWithPool[0] = true;
111 table.close();
112 }
113 }
114
115 private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
116
117 @BeforeClass
118 public static void setupCluster() throws Exception {
119 UTIL.startMiniCluster();
120 }
121
122 @After
123 public void cleanupTestTable() throws Exception {
124 UTIL.getHBaseAdmin().disableTable(primaryTable);
125 UTIL.getHBaseAdmin().deleteTable(primaryTable);
126
127 UTIL.getHBaseAdmin().disableTable(otherTable);
128 UTIL.getHBaseAdmin().deleteTable(otherTable);
129
130 }
131
132 @AfterClass
133 public static void teardownCluster() throws Exception {
134 UTIL.shutdownMiniCluster();
135 }
136
137 @Test
138 public void testCoprocessorCanCreateConnectionToRemoteTable() throws Throwable {
139 runCoprocessorConnectionToRemoteTable(SendToOtherTableCoprocessor.class, completed);
140 }
141
142 @Test
143 public void testCoprocessorCanCreateConnectionToRemoteTableWithCustomPool() throws Throwable {
144 runCoprocessorConnectionToRemoteTable(CustomThreadPoolCoprocessor.class, completedWithPool);
145 }
146
147 private void runCoprocessorConnectionToRemoteTable(Class<? extends BaseRegionObserver> clazz,
148 boolean[] completeCheck) throws Throwable {
149 HTableDescriptor primary = new HTableDescriptor(primaryTable);
150 primary.addFamily(new HColumnDescriptor(family));
151
152 primary.addCoprocessor(clazz.getName());
153
154 HTableDescriptor other = new HTableDescriptor(otherTable);
155 other.addFamily(new HColumnDescriptor(family));
156
157
158 Admin admin = UTIL.getHBaseAdmin();
159 admin.createTable(primary);
160 admin.createTable(other);
161
162 Table table = new HTable(UTIL.getConfiguration(), TableName.valueOf("primary"));
163 Put p = new Put(new byte[] { 'a' });
164 p.add(family, null, new byte[] { 'a' });
165 table.put(p);
166 table.close();
167
168 Table target = new HTable(UTIL.getConfiguration(), otherTable);
169 assertTrue("Didn't complete update to target table!", completeCheck[0]);
170 assertEquals("Didn't find inserted row", 1, getKeyValueCount(target));
171 target.close();
172 }
173
174
175
176
177
178
179
180 private int getKeyValueCount(Table table) throws IOException {
181 Scan scan = new Scan();
182 scan.setMaxVersions(Integer.MAX_VALUE - 1);
183
184 ResultScanner results = table.getScanner(scan);
185 int count = 0;
186 for (Result res : results) {
187 count += res.listCells().size();
188 System.out.println(count + ") " + res);
189 }
190 results.close();
191
192 return count;
193 }
194 }