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