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.ha.protocolPB;
019
020import java.io.Closeable;
021import java.io.IOException;
022import java.net.InetSocketAddress;
023
024import javax.net.SocketFactory;
025
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.ha.ZKFCProtocol;
028import org.apache.hadoop.ha.proto.ZKFCProtocolProtos.CedeActiveRequestProto;
029import org.apache.hadoop.ha.proto.ZKFCProtocolProtos.GracefulFailoverRequestProto;
030import org.apache.hadoop.ipc.ProtobufHelper;
031import org.apache.hadoop.ipc.ProtobufRpcEngine;
032import org.apache.hadoop.ipc.ProtocolTranslator;
033import org.apache.hadoop.ipc.RPC;
034import org.apache.hadoop.security.AccessControlException;
035import org.apache.hadoop.security.UserGroupInformation;
036
037import com.google.protobuf.RpcController;
038import com.google.protobuf.ServiceException;
039
040
041public class ZKFCProtocolClientSideTranslatorPB implements
042  ZKFCProtocol, Closeable, ProtocolTranslator {
043
044  private final static RpcController NULL_CONTROLLER = null;
045  private final ZKFCProtocolPB rpcProxy;
046
047  public ZKFCProtocolClientSideTranslatorPB(
048      InetSocketAddress addr, Configuration conf,
049      SocketFactory socketFactory, int timeout) throws IOException {
050    RPC.setProtocolEngine(conf, ZKFCProtocolPB.class,
051        ProtobufRpcEngine.class);
052    rpcProxy = RPC.getProxy(ZKFCProtocolPB.class,
053        RPC.getProtocolVersion(ZKFCProtocolPB.class), addr,
054        UserGroupInformation.getCurrentUser(), conf, socketFactory, timeout);
055  }
056
057  @Override
058  public void cedeActive(int millisToCede) throws IOException,
059      AccessControlException {
060    try {
061      CedeActiveRequestProto req = CedeActiveRequestProto.newBuilder()
062          .setMillisToCede(millisToCede)
063          .build();
064      rpcProxy.cedeActive(NULL_CONTROLLER, req);      
065    } catch (ServiceException e) {
066      throw ProtobufHelper.getRemoteException(e);
067    }
068  }
069
070  @Override
071  public void gracefulFailover() throws IOException, AccessControlException {
072    try {
073      rpcProxy.gracefulFailover(NULL_CONTROLLER,
074          GracefulFailoverRequestProto.getDefaultInstance());
075    } catch (ServiceException e) {
076      throw ProtobufHelper.getRemoteException(e);
077    }
078  }
079
080
081  @Override
082  public void close() {
083    RPC.stopProxy(rpcProxy);
084  }
085
086  @Override
087  public Object getUnderlyingProxyObject() {
088    return rpcProxy;
089  }
090}