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.GetMostRecentCheckpointTxIdRequestProto; 036 import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.GetMostRecentCheckpointTxIdResponseProto; 037 import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.GetTransactionIdRequestProto; 038 import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.GetTransactionIdResponseProto; 039 import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.RegisterRequestProto; 040 import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.RegisterResponseProto; 041 import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.RollEditLogRequestProto; 042 import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.RollEditLogResponseProto; 043 import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.StartCheckpointRequestProto; 044 import org.apache.hadoop.hdfs.protocol.proto.NamenodeProtocolProtos.StartCheckpointResponseProto; 045 import org.apache.hadoop.hdfs.security.token.block.ExportedBlockKeys; 046 import org.apache.hadoop.hdfs.server.namenode.CheckpointSignature; 047 import org.apache.hadoop.hdfs.server.protocol.BlocksWithLocations; 048 import org.apache.hadoop.hdfs.server.protocol.NamenodeCommand; 049 import org.apache.hadoop.hdfs.server.protocol.NamenodeProtocol; 050 import org.apache.hadoop.hdfs.server.protocol.NamenodeRegistration; 051 import org.apache.hadoop.hdfs.server.protocol.NamespaceInfo; 052 import org.apache.hadoop.hdfs.server.protocol.RemoteEditLogManifest; 053 054 import com.google.protobuf.RpcController; 055 import 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 */ 062 public 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 register(RpcController unused, 161 RegisterRequestProto request) throws ServiceException { 162 NamenodeRegistration reg; 163 try { 164 reg = impl.register(PBHelper.convert(request.getRegistration())); 165 } catch (IOException e) { 166 throw new ServiceException(e); 167 } 168 return RegisterResponseProto.newBuilder() 169 .setRegistration(PBHelper.convert(reg)).build(); 170 } 171 172 @Override 173 public StartCheckpointResponseProto startCheckpoint(RpcController unused, 174 StartCheckpointRequestProto request) throws ServiceException { 175 NamenodeCommand cmd; 176 try { 177 cmd = impl.startCheckpoint(PBHelper.convert(request.getRegistration())); 178 } catch (IOException e) { 179 throw new ServiceException(e); 180 } 181 return StartCheckpointResponseProto.newBuilder() 182 .setCommand(PBHelper.convert(cmd)).build(); 183 } 184 185 @Override 186 public EndCheckpointResponseProto endCheckpoint(RpcController unused, 187 EndCheckpointRequestProto request) throws ServiceException { 188 try { 189 impl.endCheckpoint(PBHelper.convert(request.getRegistration()), 190 PBHelper.convert(request.getSignature())); 191 } catch (IOException e) { 192 throw new ServiceException(e); 193 } 194 return VOID_END_CHECKPOINT_RESPONSE; 195 } 196 197 @Override 198 public GetEditLogManifestResponseProto getEditLogManifest( 199 RpcController unused, GetEditLogManifestRequestProto request) 200 throws ServiceException { 201 RemoteEditLogManifest manifest; 202 try { 203 manifest = impl.getEditLogManifest(request.getSinceTxId()); 204 } catch (IOException e) { 205 throw new ServiceException(e); 206 } 207 return GetEditLogManifestResponseProto.newBuilder() 208 .setManifest(PBHelper.convert(manifest)).build(); 209 } 210 211 @Override 212 public VersionResponseProto versionRequest(RpcController controller, 213 VersionRequestProto request) throws ServiceException { 214 NamespaceInfo info; 215 try { 216 info = impl.versionRequest(); 217 } catch (IOException e) { 218 throw new ServiceException(e); 219 } 220 return VersionResponseProto.newBuilder() 221 .setInfo(PBHelper.convert(info)).build(); 222 } 223 }