001/*
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 * http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.apache.hadoop.hbase.coprocessor;
019
020import com.google.protobuf.RpcCallback;
021import com.google.protobuf.RpcController;
022import com.google.protobuf.Service;
023
024import java.io.IOException;
025import java.util.Collections;
026
027import org.apache.hadoop.hbase.CoprocessorEnvironment;
028import org.apache.hadoop.hbase.ipc.CoprocessorRpcUtils;
029import org.apache.hadoop.hbase.ipc.RpcServer;
030import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos;
031import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.AddrResponseProto;
032import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.EmptyRequestProto;
033import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.EmptyResponseProto;
034import org.apache.hadoop.hbase.ipc.protobuf.generated.TestProtos.PauseRequestProto;
035import org.apache.hadoop.hbase.ipc.protobuf.generated.TestRpcServiceProtos;
036import org.apache.hadoop.hbase.util.Threads;
037
038/**
039 * Test implementation of a coprocessor endpoint exposing the
040 * {@link TestRpcServiceProtos.TestProtobufRpcProto} service methods. For internal use by unit tests
041 * only.
042 */
043public class ProtobufCoprocessorService extends TestRpcServiceProtos.TestProtobufRpcProto
044        implements MasterCoprocessor, RegionCoprocessor {
045  public ProtobufCoprocessorService() {}
046
047  @Override
048  public Iterable<Service> getServices() {
049    return Collections.singleton(this);
050  }
051
052  @Override
053  public void ping(RpcController controller, TestProtos.EmptyRequestProto request,
054          RpcCallback<TestProtos.EmptyResponseProto> done) {
055    done.run(TestProtos.EmptyResponseProto.getDefaultInstance());
056  }
057
058  @Override
059  public void echo(RpcController controller, TestProtos.EchoRequestProto request,
060          RpcCallback<TestProtos.EchoResponseProto> done) {
061    String message = request.getMessage();
062    done.run(TestProtos.EchoResponseProto.newBuilder().setMessage(message).build());
063  }
064
065  @Override
066  public void error(RpcController controller, TestProtos.EmptyRequestProto request,
067          RpcCallback<TestProtos.EmptyResponseProto> done) {
068    CoprocessorRpcUtils.setControllerException(controller, new IOException("Test exception"));
069    done.run(null);
070  }
071
072  @Override
073  public void pause(RpcController controller, PauseRequestProto request,
074          RpcCallback<EmptyResponseProto> done) {
075    Threads.sleepWithoutInterrupt(request.getMs());
076    done.run(EmptyResponseProto.getDefaultInstance());
077  }
078
079  @Override
080  public void addr(RpcController controller, EmptyRequestProto request,
081          RpcCallback<AddrResponseProto> done) {
082    done.run(AddrResponseProto.newBuilder()
083        .setAddr(RpcServer.getRemoteAddress().get().getHostAddress()).build());
084  }
085
086  @Override
087  public void start(CoprocessorEnvironment env) throws IOException {
088    // To change body of implemented methods use File | Settings | File Templates.
089  }
090
091  @Override
092  public void stop(CoprocessorEnvironment env) throws IOException {
093    // To change body of implemented methods use File | Settings | File Templates.
094  }
095}