1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.hadoop.hbase.ipc;
19
20 import static org.junit.Assert.assertTrue;
21 import static org.junit.Assert.fail;
22 import io.netty.channel.ChannelHandlerContext;
23 import io.netty.channel.ChannelInitializer;
24 import io.netty.channel.ChannelOutboundHandlerAdapter;
25 import io.netty.channel.ChannelPromise;
26 import io.netty.channel.epoll.EpollEventLoopGroup;
27 import io.netty.channel.nio.NioEventLoopGroup;
28 import io.netty.channel.socket.SocketChannel;
29
30 import java.io.IOException;
31 import java.net.InetSocketAddress;
32 import java.util.ArrayList;
33 import java.util.Collection;
34 import java.util.List;
35 import java.util.concurrent.atomic.AtomicBoolean;
36
37 import org.apache.commons.logging.Log;
38 import org.apache.commons.logging.LogFactory;
39 import org.apache.hadoop.conf.Configuration;
40 import org.apache.hadoop.hbase.CellScannable;
41 import org.apache.hadoop.hbase.CellUtil;
42 import org.apache.hadoop.hbase.HBaseConfiguration;
43 import org.apache.hadoop.hbase.HBaseTestingUtility;
44 import org.apache.hadoop.hbase.HConstants;
45 import org.apache.hadoop.hbase.HRegionInfo;
46 import org.apache.hadoop.hbase.KeyValue;
47 import org.apache.hadoop.hbase.ServerName;
48 import org.apache.hadoop.hbase.Waiter;
49 import org.apache.hadoop.hbase.client.Put;
50 import org.apache.hadoop.hbase.client.RowMutations;
51 import org.apache.hadoop.hbase.codec.Codec;
52 import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.EchoRequestProto;
53 import org.apache.hadoop.hbase.protobuf.RequestConverter;
54 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos;
55 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.MutationProto;
56 import org.apache.hadoop.hbase.protobuf.generated.ClientProtos.RegionAction;
57 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionSpecifier;
58 import org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.RegionSpecifier.RegionSpecifierType;
59 import org.apache.hadoop.hbase.security.User;
60 import org.apache.hadoop.hbase.testclassification.SmallTests;
61 import org.apache.hadoop.util.StringUtils;
62 import org.junit.Test;
63 import org.junit.experimental.categories.Category;
64 import org.junit.runner.RunWith;
65 import org.junit.runners.Parameterized;
66 import org.junit.runners.Parameterized.Parameters;
67
68 import com.google.protobuf.ByteString;
69 import com.google.protobuf.Descriptors.MethodDescriptor;
70 import com.google.protobuf.Message;
71 import com.google.protobuf.RpcCallback;
72 import com.google.protobuf.RpcChannel;
73
74 @RunWith(Parameterized.class)
75 @Category({ SmallTests.class })
76 public class TestAsyncIPC extends AbstractTestIPC {
77
78 private static final Log LOG = LogFactory.getLog(TestAsyncIPC.class);
79
80 private final static HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
81
82 @Parameters
83 public static Collection<Object[]> parameters() {
84 List<Object[]> paramList = new ArrayList<Object[]>();
85 paramList.add(new Object[] { false, false });
86 paramList.add(new Object[] { false, true });
87 paramList.add(new Object[] { true, false });
88 paramList.add(new Object[] { true, true });
89 return paramList;
90 }
91
92 private final boolean useNativeTransport;
93
94 private final boolean useGlobalEventLoopGroup;
95
96 public TestAsyncIPC(boolean useNativeTransport, boolean useGlobalEventLoopGroup) {
97 this.useNativeTransport = useNativeTransport;
98 this.useGlobalEventLoopGroup = useGlobalEventLoopGroup;
99 }
100
101 private void setConf(Configuration conf) {
102 conf.setBoolean(AsyncRpcClient.USE_NATIVE_TRANSPORT, useNativeTransport);
103 conf.setBoolean(AsyncRpcClient.USE_GLOBAL_EVENT_LOOP_GROUP, useGlobalEventLoopGroup);
104 if (useGlobalEventLoopGroup && AsyncRpcClient.GLOBAL_EVENT_LOOP_GROUP != null) {
105 if (useNativeTransport
106 && !(AsyncRpcClient.GLOBAL_EVENT_LOOP_GROUP.getFirst() instanceof EpollEventLoopGroup)
107 || (!useNativeTransport
108 && !(AsyncRpcClient.GLOBAL_EVENT_LOOP_GROUP.getFirst() instanceof NioEventLoopGroup))) {
109 AsyncRpcClient.GLOBAL_EVENT_LOOP_GROUP.getFirst().shutdownGracefully();
110 AsyncRpcClient.GLOBAL_EVENT_LOOP_GROUP = null;
111 }
112 }
113 }
114
115 @Override
116 protected AsyncRpcClient createRpcClientNoCodec(Configuration conf) {
117 setConf(conf);
118 return new AsyncRpcClient(conf, HConstants.CLUSTER_ID_DEFAULT, null) {
119
120 @Override
121 Codec getCodec() {
122 return null;
123 }
124
125 };
126 }
127
128 @Override
129 protected AsyncRpcClient createRpcClient(Configuration conf) {
130 setConf(conf);
131 return new AsyncRpcClient(conf, HConstants.CLUSTER_ID_DEFAULT, null);
132 }
133
134 @Override
135 protected AsyncRpcClient createRpcClientRTEDuringConnectionSetup(Configuration conf) {
136 setConf(conf);
137 return new AsyncRpcClient(conf, HConstants.CLUSTER_ID_DEFAULT, null,
138 new ChannelInitializer<SocketChannel>() {
139
140 @Override
141 protected void initChannel(SocketChannel ch) throws Exception {
142 ch.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {
143 @Override
144 public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
145 throws Exception {
146 promise.setFailure(new RuntimeException("Injected fault"));
147 }
148 });
149 }
150 });
151 }
152
153 @Test
154 public void testAsyncConnectionSetup() throws Exception {
155 TestRpcServer rpcServer = new TestRpcServer();
156 AsyncRpcClient client = createRpcClient(CONF);
157 try {
158 rpcServer.start();
159 InetSocketAddress address = rpcServer.getListenerAddress();
160 MethodDescriptor md = SERVICE.getDescriptorForType().findMethodByName("echo");
161 EchoRequestProto param = EchoRequestProto.newBuilder().setMessage("hello").build();
162
163 RpcChannel channel =
164 client.createRpcChannel(ServerName.valueOf(address.getHostName(), address.getPort(),
165 System.currentTimeMillis()), User.getCurrent(), 0);
166
167 final AtomicBoolean done = new AtomicBoolean(false);
168
169 channel.callMethod(md, new PayloadCarryingRpcController(), param, md.getOutputType()
170 .toProto(), new RpcCallback<Message>() {
171 @Override
172 public void run(Message parameter) {
173 done.set(true);
174 }
175 });
176
177 TEST_UTIL.waitFor(1000, new Waiter.Predicate<Exception>() {
178 @Override
179 public boolean evaluate() throws Exception {
180 return done.get();
181 }
182 });
183 } finally {
184 client.close();
185 rpcServer.stop();
186 }
187 }
188
189 @Test
190 public void testRTEDuringAsyncConnectionSetup() throws Exception {
191 TestRpcServer rpcServer = new TestRpcServer();
192 AsyncRpcClient client = createRpcClientRTEDuringConnectionSetup(CONF);
193 try {
194 rpcServer.start();
195 InetSocketAddress address = rpcServer.getListenerAddress();
196 MethodDescriptor md = SERVICE.getDescriptorForType().findMethodByName("echo");
197 EchoRequestProto param = EchoRequestProto.newBuilder().setMessage("hello").build();
198
199 RpcChannel channel =
200 client.createRpcChannel(ServerName.valueOf(address.getHostName(), address.getPort(),
201 System.currentTimeMillis()), User.getCurrent(), 0);
202
203 final AtomicBoolean done = new AtomicBoolean(false);
204
205 PayloadCarryingRpcController controller = new PayloadCarryingRpcController();
206 controller.notifyOnFail(new RpcCallback<IOException>() {
207 @Override
208 public void run(IOException e) {
209 done.set(true);
210 LOG.info("Caught expected exception: " + e.toString());
211 assertTrue(StringUtils.stringifyException(e).contains("Injected fault"));
212 }
213 });
214
215 channel.callMethod(md, controller, param, md.getOutputType().toProto(),
216 new RpcCallback<Message>() {
217 @Override
218 public void run(Message parameter) {
219 done.set(true);
220 fail("Expected an exception to have been thrown!");
221 }
222 });
223
224 TEST_UTIL.waitFor(1000, new Waiter.Predicate<Exception>() {
225 @Override
226 public boolean evaluate() throws Exception {
227 return done.get();
228 }
229 });
230 } finally {
231 client.close();
232 rpcServer.stop();
233 }
234 }
235
236 public static void main(String[] args) throws IOException, SecurityException,
237 NoSuchMethodException, InterruptedException {
238 if (args.length != 2) {
239 System.out.println("Usage: TestAsyncIPC <CYCLES> <CELLS_PER_CYCLE>");
240 return;
241 }
242
243
244 int cycles = Integer.parseInt(args[0]);
245 int cellcount = Integer.parseInt(args[1]);
246 Configuration conf = HBaseConfiguration.create();
247 TestRpcServer rpcServer = new TestRpcServer();
248 MethodDescriptor md = SERVICE.getDescriptorForType().findMethodByName("echo");
249 EchoRequestProto param = EchoRequestProto.newBuilder().setMessage("hello").build();
250 AsyncRpcClient client = new AsyncRpcClient(conf, HConstants.CLUSTER_ID_DEFAULT, null);
251 KeyValue kv = BIG_CELL;
252 Put p = new Put(CellUtil.cloneRow(kv));
253 for (int i = 0; i < cellcount; i++) {
254 p.add(kv);
255 }
256 RowMutations rm = new RowMutations(CellUtil.cloneRow(kv));
257 rm.add(p);
258 try {
259 rpcServer.start();
260 InetSocketAddress address = rpcServer.getListenerAddress();
261 long startTime = System.currentTimeMillis();
262 User user = User.getCurrent();
263 for (int i = 0; i < cycles; i++) {
264 List<CellScannable> cells = new ArrayList<CellScannable>();
265
266 ClientProtos.RegionAction.Builder builder =
267 RequestConverter.buildNoDataRegionAction(HConstants.EMPTY_BYTE_ARRAY, rm, cells,
268 RegionAction.newBuilder(), ClientProtos.Action.newBuilder(),
269 MutationProto.newBuilder());
270 builder.setRegion(RegionSpecifier
271 .newBuilder()
272 .setType(RegionSpecifierType.REGION_NAME)
273 .setValue(
274 ByteString.copyFrom(HRegionInfo.FIRST_META_REGIONINFO.getEncodedNameAsBytes())));
275 if (i % 100000 == 0) {
276 LOG.info("" + i);
277
278
279
280 }
281 PayloadCarryingRpcController pcrc =
282 new PayloadCarryingRpcController(CellUtil.createCellScanner(cells));
283
284 client.call(pcrc, md, builder.build(), param, user, address);
285
286
287
288
289 }
290 LOG.info("Cycled " + cycles + " time(s) with " + cellcount + " cell(s) in "
291 + (System.currentTimeMillis() - startTime) + "ms");
292 } finally {
293 client.close();
294 rpcServer.stop();
295 }
296 }
297 }