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.apache.hadoop.hbase.ipc.TestProtobufRpcServiceImpl.SERVICE;
21  import static org.junit.Assert.assertEquals;
22  
23  import java.io.BufferedInputStream;
24  import java.io.DataInputStream;
25  import java.io.DataOutputStream;
26  import java.io.IOException;
27  import java.net.InetSocketAddress;
28  import java.net.Socket;
29  
30  import org.apache.hadoop.conf.Configuration;
31  import org.apache.hadoop.hbase.HBaseConfiguration;
32  import org.apache.hadoop.hbase.HConstants;
33  import org.apache.hadoop.hbase.client.MetricsConnection;
34  import org.apache.hadoop.hbase.ipc.RpcServer.BlockingServiceAndInterface;
35  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.EmptyRequestProto;
36  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.EmptyResponseProto;
37  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestRpcServiceProtos;
38  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestRpcServiceProtos.TestProtobufRpcProto;
39  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
40  import org.apache.hadoop.hbase.protobuf.generated.RPCProtos.ConnectionHeader;
41  import org.apache.hadoop.hbase.protobuf.generated.RPCProtos.RequestHeader;
42  import org.apache.hadoop.hbase.protobuf.generated.RPCProtos.ResponseHeader;
43  import org.apache.hadoop.hbase.security.AuthMethod;
44  import org.apache.hadoop.hbase.testclassification.MediumTests;
45  import org.apache.hadoop.hbase.testclassification.RPCTests;
46  import org.junit.After;
47  import org.junit.Before;
48  import org.junit.Test;
49  import org.junit.experimental.categories.Category;
50  
51  import com.google.common.collect.Lists;
52  
53  @Category({ RPCTests.class, MediumTests.class })
54  public class TestRpcServerSlowConnectionSetup {
55  
56    private RpcServer server;
57  
58    private Socket socket;
59  
60    @Before
61    public void setUp() throws IOException {
62      Configuration conf = HBaseConfiguration.create();
63      server = new RpcServer(null, "testRpcServer",
64          Lists.newArrayList(new BlockingServiceAndInterface(SERVICE, null)),
65          new InetSocketAddress("localhost", 0), conf, new FifoRpcScheduler(conf, 1));
66      server.start();
67      socket = new Socket("localhost", server.getListenerAddress().getPort());
68    }
69  
70    @After
71    public void tearDown() throws IOException {
72      if (socket != null) {
73        socket.close();
74      }
75      if (server != null) {
76        server.stop();
77      }
78    }
79  
80    @Test
81    public void test() throws IOException, InterruptedException {
82      int rpcHeaderLen = HConstants.RPC_HEADER.length;
83      byte[] preamble = new byte[rpcHeaderLen + 2];
84      System.arraycopy(HConstants.RPC_HEADER, 0, preamble, 0, rpcHeaderLen);
85      preamble[rpcHeaderLen] = HConstants.RPC_CURRENT_VERSION;
86      preamble[rpcHeaderLen + 1] = AuthMethod.SIMPLE.code;
87      socket.getOutputStream().write(preamble, 0, rpcHeaderLen + 1);
88      socket.getOutputStream().flush();
89      Thread.sleep(5000);
90      socket.getOutputStream().write(preamble, rpcHeaderLen + 1, 1);
91      socket.getOutputStream().flush();
92  
93      ConnectionHeader header = ConnectionHeader.newBuilder()
94          .setServiceName(TestRpcServiceProtos.TestProtobufRpcProto.getDescriptor().getFullName())
95          .setVersionInfo(ProtobufUtil.getVersionInfo()).build();
96      DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
97      dos.writeInt(header.getSerializedSize());
98      header.writeTo(dos);
99      dos.flush();
100 
101     int callId = 10;
102     Call call = new Call(callId, TestProtobufRpcProto.getDescriptor().findMethodByName("ping"),
103         EmptyRequestProto.getDefaultInstance(), null, EmptyResponseProto.getDefaultInstance(), 1000,
104         HConstants.NORMAL_QOS, null, MetricsConnection.newCallStats());
105     RequestHeader requestHeader = IPCUtil.buildRequestHeader(call, null);
106     dos.writeInt(IPCUtil.getTotalSizeWhenWrittenDelimited(requestHeader, call.param));
107     requestHeader.writeDelimitedTo(dos);
108     call.param.writeDelimitedTo(dos);
109     dos.flush();
110 
111     DataInputStream dis = new DataInputStream(new BufferedInputStream(socket.getInputStream()));
112     int size = dis.readInt();
113     ResponseHeader responseHeader = ResponseHeader.parseDelimitedFrom(dis);
114     assertEquals(callId, responseHeader.getCallId());
115     EmptyResponseProto.Builder builder = EmptyResponseProto.newBuilder();
116     builder.mergeDelimitedFrom(dis);
117     assertEquals(size, IPCUtil.getTotalSizeWhenWrittenDelimited(responseHeader, builder.build()));
118   }
119 }