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  
21  import com.google.protobuf.BlockingService;
22  import com.google.protobuf.RpcController;
23  import com.google.protobuf.ServiceException;
24  
25  import java.io.IOException;
26  import java.net.InetSocketAddress;
27  import java.util.ArrayList;
28  import java.util.List;
29  
30  import org.apache.hadoop.hbase.Cell;
31  import org.apache.hadoop.hbase.CellScanner;
32  import org.apache.hadoop.hbase.CellUtil;
33  import org.apache.hadoop.hbase.DoNotRetryIOException;
34  import org.apache.hadoop.hbase.ServerName;
35  import org.apache.hadoop.hbase.classification.InterfaceAudience;
36  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.AddrResponseProto;
37  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.EchoRequestProto;
38  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.EchoResponseProto;
39  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.EmptyRequestProto;
40  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.EmptyResponseProto;
41  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.PauseRequestProto;
42  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestRpcServiceProtos.TestProtobufRpcProto;
43  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestRpcServiceProtos.TestProtobufRpcProto.BlockingInterface;
44  import org.apache.hadoop.hbase.ipc.protobuf.generated.TestRpcServiceProtos.TestProtobufRpcProto.Interface;
45  import org.apache.hadoop.hbase.security.User;
46  import org.apache.hadoop.hbase.util.Threads;
47  
48  @InterfaceAudience.Private
49  public class TestProtobufRpcServiceImpl implements BlockingInterface {
50  
51    public static final BlockingService SERVICE = TestProtobufRpcProto
52        .newReflectiveBlockingService(new TestProtobufRpcServiceImpl());
53  
54    public static BlockingInterface newBlockingStub(RpcClient client, InetSocketAddress addr)
55        throws IOException {
56      return newBlockingStub(client, addr, User.getCurrent());
57    }
58  
59    public static BlockingInterface newBlockingStub(RpcClient client, InetSocketAddress addr,
60        User user) throws IOException {
61      return TestProtobufRpcProto.newBlockingStub(client.createBlockingRpcChannel(
62        ServerName.valueOf(addr.getHostName(), addr.getPort(), System.currentTimeMillis()), user, 0));
63    }
64  
65    public static Interface newStub(RpcClient client, InetSocketAddress addr) throws IOException {
66      return TestProtobufRpcProto.newStub(client.createRpcChannel(
67        ServerName.valueOf(addr.getHostName(), addr.getPort(), System.currentTimeMillis()),
68        User.getCurrent(), 0));
69    }
70  
71    @Override
72    public EmptyResponseProto ping(RpcController controller, EmptyRequestProto request)
73        throws ServiceException {
74      return EmptyResponseProto.getDefaultInstance();
75    }
76  
77    @Override
78    public EchoResponseProto echo(RpcController controller, EchoRequestProto request)
79        throws ServiceException {
80      if (controller instanceof HBaseRpcController) {
81        HBaseRpcController pcrc = (HBaseRpcController) controller;
82        // If cells, scan them to check we are able to iterate what we were given and since this is an
83        // echo, just put them back on the controller creating a new block. Tests our block building.
84        CellScanner cellScanner = pcrc.cellScanner();
85        List<Cell> list = null;
86        if (cellScanner != null) {
87          list = new ArrayList<>();
88          try {
89            while (cellScanner.advance()) {
90              list.add(cellScanner.current());
91            }
92          } catch (IOException e) {
93            throw new ServiceException(e);
94          }
95        }
96        cellScanner = CellUtil.createCellScanner(list);
97        pcrc.setCellScanner(cellScanner);
98      }
99      return EchoResponseProto.newBuilder().setMessage(request.getMessage()).build();
100   }
101 
102   @Override
103   public EmptyResponseProto error(RpcController controller, EmptyRequestProto request)
104       throws ServiceException {
105     throw new ServiceException(new DoNotRetryIOException("server error!"));
106   }
107 
108   @Override
109   public EmptyResponseProto pause(RpcController controller, PauseRequestProto request)
110       throws ServiceException {
111     Threads.sleepWithoutInterrupt(request.getMs());
112     return EmptyResponseProto.getDefaultInstance();
113   }
114 
115   @Override
116   public AddrResponseProto addr(RpcController controller, EmptyRequestProto request)
117       throws ServiceException {
118     return AddrResponseProto.newBuilder().setAddr(RpcServer.getRemoteAddress().getHostAddress())
119         .build();
120   }
121 }