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     */
018    package org.apache.hadoop.ha.protocolPB;
019    
020    import java.io.Closeable;
021    import java.io.IOException;
022    import java.net.InetSocketAddress;
023    
024    import javax.net.SocketFactory;
025    
026    import org.apache.hadoop.conf.Configuration;
027    import org.apache.hadoop.ha.ZKFCProtocol;
028    import org.apache.hadoop.ha.proto.ZKFCProtocolProtos.CedeActiveRequestProto;
029    import org.apache.hadoop.ha.proto.ZKFCProtocolProtos.GracefulFailoverRequestProto;
030    import org.apache.hadoop.ipc.ProtobufHelper;
031    import org.apache.hadoop.ipc.ProtobufRpcEngine;
032    import org.apache.hadoop.ipc.ProtocolTranslator;
033    import org.apache.hadoop.ipc.RPC;
034    import org.apache.hadoop.security.AccessControlException;
035    import org.apache.hadoop.security.UserGroupInformation;
036    
037    import com.google.protobuf.RpcController;
038    import com.google.protobuf.ServiceException;
039    
040    
041    public 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    }