View Javadoc

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.ipc;
19  
20  import static org.junit.Assert.assertEquals;
21  import static org.junit.Assert.assertTrue;
22  import static org.junit.Assert.fail;
23  import static org.mockito.Matchers.anyObject;
24  import static org.mockito.Mockito.spy;
25  import static org.mockito.Mockito.verify;
26  import static org.mockito.internal.verification.VerificationModeFactory.times;
27  
28  import java.io.IOException;
29  import java.net.InetSocketAddress;
30  import java.util.ArrayList;
31  import java.util.List;
32  
33  import org.apache.commons.logging.Log;
34  import org.apache.commons.logging.LogFactory;
35  import org.apache.hadoop.conf.Configuration;
36  import org.apache.hadoop.hbase.Cell;
37  import org.apache.hadoop.hbase.CellScanner;
38  import org.apache.hadoop.hbase.CellUtil;
39  import org.apache.hadoop.hbase.HBaseConfiguration;
40  import org.apache.hadoop.hbase.KeyValue;
41  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.EchoRequestProto;
42  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.EchoResponseProto;
43  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.EmptyRequestProto;
44  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.EmptyResponseProto;
45  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestRpcServiceProtos;
46  import org.apache.hadoop.hbase.monitoring.MonitoredRPCHandler;
47  import org.apache.hadoop.hbase.security.User;
48  import org.apache.hadoop.hbase.util.Bytes;
49  import org.apache.hadoop.hbase.util.Pair;
50  import org.apache.hadoop.io.compress.GzipCodec;
51  import org.apache.hadoop.util.StringUtils;
52  import org.junit.Test;
53  
54  import com.google.common.collect.ImmutableList;
55  import com.google.common.collect.Lists;
56  import com.google.protobuf.BlockingService;
57  import com.google.protobuf.Descriptors.MethodDescriptor;
58  import com.google.protobuf.Message;
59  import com.google.protobuf.RpcController;
60  import com.google.protobuf.ServiceException;
61  
62  /**
63   * Some basic ipc tests.
64   */
65  public abstract class AbstractTestIPC {
66  
67    private static final Log LOG = LogFactory.getLog(AbstractTestIPC.class);
68  
69    private static byte[] CELL_BYTES = Bytes.toBytes("xyz");
70    private static KeyValue CELL = new KeyValue(CELL_BYTES, CELL_BYTES, CELL_BYTES, CELL_BYTES);
71    static byte[] BIG_CELL_BYTES = new byte[10 * 1024];
72    static KeyValue BIG_CELL = new KeyValue(CELL_BYTES, CELL_BYTES, CELL_BYTES, BIG_CELL_BYTES);
73    static final Configuration CONF = HBaseConfiguration.create();
74    // We are using the test TestRpcServiceProtos generated classes and Service because they are
75    // available and basic with methods like 'echo', and ping. Below we make a blocking service
76    // by passing in implementation of blocking interface. We use this service in all tests that
77    // follow.
78    static final BlockingService SERVICE =
79        TestRpcServiceProtos.TestProtobufRpcProto
80            .newReflectiveBlockingService(new TestRpcServiceProtos.TestProtobufRpcProto.BlockingInterface() {
81  
82              @Override
83              public EmptyResponseProto ping(RpcController controller, EmptyRequestProto request)
84                  throws ServiceException {
85                return null;
86              }
87  
88              @Override
89              public EmptyResponseProto error(RpcController controller, EmptyRequestProto request)
90                  throws ServiceException {
91                return null;
92              }
93  
94              @Override
95              public EchoResponseProto echo(RpcController controller, EchoRequestProto request)
96                  throws ServiceException {
97                if (controller instanceof PayloadCarryingRpcController) {
98                  PayloadCarryingRpcController pcrc = (PayloadCarryingRpcController) controller;
99                  // If cells, scan them to check we are able to iterate what we were given and since
100                 // this is
101                 // an echo, just put them back on the controller creating a new block. Tests our
102                 // block
103                 // building.
104                 CellScanner cellScanner = pcrc.cellScanner();
105                 List<Cell> list = null;
106                 if (cellScanner != null) {
107                   list = new ArrayList<Cell>();
108                   try {
109                     while (cellScanner.advance()) {
110                       list.add(cellScanner.current());
111                     }
112                   } catch (IOException e) {
113                     throw new ServiceException(e);
114                   }
115                 }
116                 cellScanner = CellUtil.createCellScanner(list);
117                 ((PayloadCarryingRpcController) controller).setCellScanner(cellScanner);
118               }
119               return EchoResponseProto.newBuilder().setMessage(request.getMessage()).build();
120             }
121           });
122 
123   /**
124    * Instance of server. We actually don't do anything speical in here so could just use
125    * HBaseRpcServer directly.
126    */
127   static class TestRpcServer extends RpcServer {
128 
129     TestRpcServer() throws IOException {
130       this(new FifoRpcScheduler(CONF, 1));
131     }
132 
133     TestRpcServer(RpcScheduler scheduler) throws IOException {
134       super(null, "testRpcServer", Lists
135           .newArrayList(new BlockingServiceAndInterface(SERVICE, null)), new InetSocketAddress(
136           "localhost", 0), CONF, scheduler);
137     }
138 
139     @Override
140     public Pair<Message, CellScanner> call(BlockingService service, MethodDescriptor md,
141         Message param, CellScanner cellScanner, long receiveTime, MonitoredRPCHandler status)
142         throws IOException {
143       return super.call(service, md, param, cellScanner, receiveTime, status);
144     }
145   }
146 
147   protected abstract AbstractRpcClient createRpcClientNoCodec(Configuration conf);
148 
149   /**
150    * Ensure we do not HAVE TO HAVE a codec.
151    * @throws InterruptedException
152    * @throws IOException
153    */
154   @Test
155   public void testNoCodec() throws InterruptedException, IOException {
156     Configuration conf = HBaseConfiguration.create();
157     AbstractRpcClient client = createRpcClientNoCodec(conf);
158     TestRpcServer rpcServer = new TestRpcServer();
159     try {
160       rpcServer.start();
161       InetSocketAddress address = rpcServer.getListenerAddress();
162       MethodDescriptor md = SERVICE.getDescriptorForType().findMethodByName("echo");
163       final String message = "hello";
164       EchoRequestProto param = EchoRequestProto.newBuilder().setMessage(message).build();
165       Pair<Message, CellScanner> r =
166           client.call(null, md, param, md.getOutputType().toProto(), User.getCurrent(), address);
167       assertTrue(r.getSecond() == null);
168       // Silly assertion that the message is in the returned pb.
169       assertTrue(r.getFirst().toString().contains(message));
170     } finally {
171       client.close();
172       rpcServer.stop();
173     }
174   }
175 
176   protected abstract AbstractRpcClient createRpcClient(Configuration conf);
177 
178   /**
179    * It is hard to verify the compression is actually happening under the wraps. Hope that if
180    * unsupported, we'll get an exception out of some time (meantime, have to trace it manually to
181    * confirm that compression is happening down in the client and server).
182    * @throws IOException
183    * @throws InterruptedException
184    * @throws SecurityException
185    * @throws NoSuchMethodException
186    */
187   @Test
188   public void testCompressCellBlock() throws IOException, InterruptedException, SecurityException,
189       NoSuchMethodException, ServiceException {
190     Configuration conf = new Configuration(HBaseConfiguration.create());
191     conf.set("hbase.client.rpc.compressor", GzipCodec.class.getCanonicalName());
192     List<Cell> cells = new ArrayList<Cell>();
193     int count = 3;
194     for (int i = 0; i < count; i++) {
195       cells.add(CELL);
196     }
197     AbstractRpcClient client = createRpcClient(conf);
198     TestRpcServer rpcServer = new TestRpcServer();
199     try {
200       rpcServer.start();
201       InetSocketAddress address = rpcServer.getListenerAddress();
202       MethodDescriptor md = SERVICE.getDescriptorForType().findMethodByName("echo");
203       EchoRequestProto param = EchoRequestProto.newBuilder().setMessage("hello").build();
204 
205       PayloadCarryingRpcController pcrc =
206           new PayloadCarryingRpcController(CellUtil.createCellScanner(cells));
207       Pair<Message, CellScanner> r =
208           client.call(pcrc, md, param, md.getOutputType().toProto(), User.getCurrent(), address);
209       int index = 0;
210       while (r.getSecond().advance()) {
211         assertTrue(CELL.equals(r.getSecond().current()));
212         index++;
213       }
214       assertEquals(count, index);
215     } finally {
216       client.close();
217       rpcServer.stop();
218     }
219   }
220 
221   protected abstract AbstractRpcClient createRpcClientRTEDuringConnectionSetup(Configuration conf)
222       throws IOException;
223 
224   @Test
225   public void testRTEDuringConnectionSetup() throws Exception {
226     Configuration conf = HBaseConfiguration.create();
227     TestRpcServer rpcServer = new TestRpcServer();
228     AbstractRpcClient client = createRpcClientRTEDuringConnectionSetup(conf);
229     try {
230       rpcServer.start();
231       InetSocketAddress address = rpcServer.getListenerAddress();
232       MethodDescriptor md = SERVICE.getDescriptorForType().findMethodByName("echo");
233       EchoRequestProto param = EchoRequestProto.newBuilder().setMessage("hello").build();
234       client.call(null, md, param, null, User.getCurrent(), address);
235       fail("Expected an exception to have been thrown!");
236     } catch (Exception e) {
237       LOG.info("Caught expected exception: " + e.toString());
238       assertTrue(StringUtils.stringifyException(e).contains("Injected fault"));
239     } finally {
240       client.close();
241       rpcServer.stop();
242     }
243   }
244 
245   /** Tests that the rpc scheduler is called when requests arrive. */
246   @Test
247   public void testRpcScheduler() throws IOException, InterruptedException {
248     RpcScheduler scheduler = spy(new FifoRpcScheduler(CONF, 1));
249     RpcServer rpcServer = new TestRpcServer(scheduler);
250     verify(scheduler).init((RpcScheduler.Context) anyObject());
251     AbstractRpcClient client = createRpcClient(CONF);
252     try {
253       rpcServer.start();
254       verify(scheduler).start();
255       MethodDescriptor md = SERVICE.getDescriptorForType().findMethodByName("echo");
256       EchoRequestProto param = EchoRequestProto.newBuilder().setMessage("hello").build();
257       for (int i = 0; i < 10; i++) {
258         client.call(
259           new PayloadCarryingRpcController(
260               CellUtil.createCellScanner(ImmutableList.<Cell> of(CELL))), md, param, md
261               .getOutputType().toProto(), User.getCurrent(), rpcServer.getListenerAddress());
262       }
263       verify(scheduler, times(10)).dispatch((CallRunner) anyObject());
264     } finally {
265       rpcServer.stop();
266       verify(scheduler).stop();
267     }
268   }
269 }