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.hdfs.protocolPB;
019    
020    import java.io.IOException;
021    
022    import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
023    import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.VersionRequestProto;
024    import org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.VersionResponseProto;
025    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.EndCheckpointRequestProto;
026    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.EndCheckpointResponseProto;
027    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.ErrorReportRequestProto;
028    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.ErrorReportResponseProto;
029    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.GetBlockKeysRequestProto;
030    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.GetBlockKeysResponseProto;
031    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.GetBlocksRequestProto;
032    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.GetBlocksResponseProto;
033    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.GetEditLogManifestRequestProto;
034    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.GetEditLogManifestResponseProto;
035    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.GetTransactionIdRequestProto;
036    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.GetTransactionIdResponseProto;
037    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.RegisterRequestProto;
038    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.RegisterResponseProto;
039    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.RollEditLogRequestProto;
040    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.RollEditLogResponseProto;
041    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.StartCheckpointRequestProto;
042    import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.StartCheckpointResponseProto;
043    import org.apache.hadoop.hdfs.security.token.block.ExportedBlockKeys;
044    import org.apache.hadoop.hdfs.server.namenode.CheckpointSignature;
045    import org.apache.hadoop.hdfs.server.protocol.BlocksWithLocations;
046    import org.apache.hadoop.hdfs.server.protocol.NamenodeCommand;
047    import org.apache.hadoop.hdfs.server.protocol.NamenodeProtocol;
048    import org.apache.hadoop.hdfs.server.protocol.NamenodeRegistration;
049    import org.apache.hadoop.hdfs.server.protocol.NamespaceInfo;
050    import org.apache.hadoop.hdfs.server.protocol.RemoteEditLogManifest;
051    
052    import com.google.protobuf.RpcController;
053    import com.google.protobuf.ServiceException;
054    
055    /**
056     * Implementation for protobuf service that forwards requests
057     * received on {@link NamenodeProtocolPB} to the
058     * {@link NamenodeProtocol} server implementation.
059     */
060    public class NamenodeProtocolServerSideTranslatorPB implements
061        NamenodeProtocolPB {
062      private final NamenodeProtocol impl;
063    
064      public NamenodeProtocolServerSideTranslatorPB(NamenodeProtocol impl) {
065        this.impl = impl;
066      }
067    
068      @Override
069      public GetBlocksResponseProto getBlocks(RpcController unused,
070          GetBlocksRequestProto request) throws ServiceException {
071        DatanodeInfo dnInfo = new DatanodeInfo(PBHelper.convert(request
072            .getDatanode()));
073        BlocksWithLocations blocks;
074        try {
075          blocks = impl.getBlocks(dnInfo, request.getSize());
076        } catch (IOException e) {
077          throw new ServiceException(e);
078        }
079        return GetBlocksResponseProto.newBuilder()
080            .setBlocks(PBHelper.convert(blocks)).build();
081      }
082    
083      @Override
084      public GetBlockKeysResponseProto getBlockKeys(RpcController unused,
085          GetBlockKeysRequestProto request) throws ServiceException {
086        ExportedBlockKeys keys;
087        try {
088          keys = impl.getBlockKeys();
089        } catch (IOException e) {
090          throw new ServiceException(e);
091        }
092        return GetBlockKeysResponseProto.newBuilder()
093            .setKeys(PBHelper.convert(keys)).build();
094      }
095    
096      @Override
097      public GetTransactionIdResponseProto getTransactionId(RpcController unused,
098          GetTransactionIdRequestProto request) throws ServiceException {
099        long txid;
100        try {
101          txid = impl.getTransactionID();
102        } catch (IOException e) {
103          throw new ServiceException(e);
104        }
105        return GetTransactionIdResponseProto.newBuilder().setTxId(txid).build();
106      }
107    
108      @Override
109      public RollEditLogResponseProto rollEditLog(RpcController unused,
110          RollEditLogRequestProto request) throws ServiceException {
111        CheckpointSignature signature;
112        try {
113          signature = impl.rollEditLog();
114        } catch (IOException e) {
115          throw new ServiceException(e);
116        }
117        return RollEditLogResponseProto.newBuilder()
118            .setSignature(PBHelper.convert(signature)).build();
119      }
120    
121      @Override
122      public ErrorReportResponseProto errorReport(RpcController unused,
123          ErrorReportRequestProto request) throws ServiceException {
124        try {
125          impl.errorReport(PBHelper.convert(request.getRegistration()),
126              request.getErrorCode(), request.getMsg());
127        } catch (IOException e) {
128          throw new ServiceException(e);
129        }
130        return ErrorReportResponseProto.newBuilder().build();
131      }
132    
133      @Override
134      public RegisterResponseProto register(RpcController unused,
135          RegisterRequestProto request) throws ServiceException {
136        NamenodeRegistration reg;
137        try {
138          reg = impl.register(PBHelper.convert(request.getRegistration()));
139        } catch (IOException e) {
140          throw new ServiceException(e);
141        }
142        return RegisterResponseProto.newBuilder()
143            .setRegistration(PBHelper.convert(reg)).build();
144      }
145    
146      @Override
147      public StartCheckpointResponseProto startCheckpoint(RpcController unused,
148          StartCheckpointRequestProto request) throws ServiceException {
149        NamenodeCommand cmd;
150        try {
151          cmd = impl.startCheckpoint(PBHelper.convert(request.getRegistration()));
152        } catch (IOException e) {
153          throw new ServiceException(e);
154        }
155        return StartCheckpointResponseProto.newBuilder()
156            .setCommand(PBHelper.convert(cmd)).build();
157      }
158    
159      @Override
160      public EndCheckpointResponseProto endCheckpoint(RpcController unused,
161          EndCheckpointRequestProto request) throws ServiceException {
162        try {
163          impl.endCheckpoint(PBHelper.convert(request.getRegistration()),
164              PBHelper.convert(request.getSignature()));
165        } catch (IOException e) {
166          throw new ServiceException(e);
167        }
168        return EndCheckpointResponseProto.newBuilder().build();
169      }
170    
171      @Override
172      public GetEditLogManifestResponseProto getEditLogManifest(
173          RpcController unused, GetEditLogManifestRequestProto request)
174          throws ServiceException {
175        RemoteEditLogManifest manifest;
176        try {
177          manifest = impl.getEditLogManifest(request.getSinceTxId());
178        } catch (IOException e) {
179          throw new ServiceException(e);
180        }
181        return GetEditLogManifestResponseProto.newBuilder()
182            .setManifest(PBHelper.convert(manifest)).build();
183      }
184    
185      @Override
186      public VersionResponseProto versionRequest(RpcController controller,
187          VersionRequestProto request) throws ServiceException {
188        NamespaceInfo info;
189        try {
190          info = impl.versionRequest();
191        } catch (IOException e) {
192          throw new ServiceException(e);
193        }
194        return VersionResponseProto.newBuilder()
195            .setInfo(PBHelper.convert(info)).build();
196      }
197    }