|
20 | 20 | import org.apache.hadoop.classification.InterfaceAudience;
|
21 | 21 | import org.apache.hadoop.classification.InterfaceStability;
|
22 | 22 | import org.apache.hadoop.conf.Configuration;
|
| 23 | +import org.apache.hadoop.hdfs.HAUtil; |
| 24 | +import org.apache.hadoop.hdfs.NameNodeProxies; |
23 | 25 | import org.apache.hadoop.hdfs.protocol.Block;
|
| 26 | +import org.apache.hadoop.hdfs.protocol.HdfsConstants; |
24 | 27 | import org.apache.hadoop.hdfs.protocol.ProvidedStorageLocation;
|
25 | 28 | import org.apache.hadoop.hdfs.server.aliasmap.InMemoryAliasMap;
|
26 | 29 | import org.apache.hadoop.hdfs.server.aliasmap.InMemoryAliasMapProtocol;
|
27 | 30 | import org.apache.hadoop.hdfs.server.common.FileRegion;
|
| 31 | +import org.apache.hadoop.hdfs.server.namenode.ha.AbstractNNFailoverProxyProvider; |
| 32 | +import org.apache.hadoop.hdfs.server.namenode.ha.InMemoryAliasMapFailoverProxyProvider; |
28 | 33 | import org.apache.hadoop.ipc.ProtobufHelper;
|
29 |
| -import org.apache.hadoop.ipc.ProtobufRpcEngine; |
30 | 34 | import org.apache.hadoop.ipc.RPC;
|
31 | 35 | import org.apache.hadoop.net.NetUtils;
|
32 | 36 | import org.slf4j.Logger;
|
33 | 37 | import org.slf4j.LoggerFactory;
|
34 | 38 |
|
35 | 39 | import javax.annotation.Nonnull;
|
| 40 | +import java.io.Closeable; |
36 | 41 | import java.io.IOException;
|
37 |
| -import java.net.InetSocketAddress; |
| 42 | +import java.net.URI; |
| 43 | +import java.net.URISyntaxException; |
| 44 | +import java.util.ArrayList; |
| 45 | +import java.util.Collection; |
38 | 46 | import java.util.List;
|
39 | 47 | import java.util.Optional;
|
40 | 48 | import java.util.stream.Collectors;
|
41 | 49 |
|
42 | 50 | import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_PROVIDED_ALIASMAP_INMEMORY_RPC_ADDRESS;
|
43 |
| -import static org.apache.hadoop.hdfs.DFSConfigKeys.DFS_PROVIDED_ALIASMAP_INMEMORY_RPC_ADDRESS_DEFAULT; |
| 51 | +import static org.apache.hadoop.hdfs.DFSUtil.addKeySuffixes; |
| 52 | +import static org.apache.hadoop.hdfs.DFSUtil.createUri; |
| 53 | +import static org.apache.hadoop.hdfs.DFSUtilClient.getNameServiceIds; |
| 54 | +import static org.apache.hadoop.hdfs.client.HdfsClientConfigKeys.Failover.PROXY_PROVIDER_KEY_PREFIX; |
44 | 55 | import static org.apache.hadoop.hdfs.protocol.proto.AliasMapProtocolProtos.*;
|
45 | 56 | import static org.apache.hadoop.hdfs.protocol.proto.HdfsProtos.*;
|
46 | 57 |
|
|
52 | 63 | @InterfaceAudience.Private
|
53 | 64 | @InterfaceStability.Unstable
|
54 | 65 | public class InMemoryAliasMapProtocolClientSideTranslatorPB
|
55 |
| - implements InMemoryAliasMapProtocol { |
| 66 | + implements InMemoryAliasMapProtocol, Closeable { |
56 | 67 |
|
57 | 68 | private static final Logger LOG =
|
58 | 69 | LoggerFactory
|
59 | 70 | .getLogger(InMemoryAliasMapProtocolClientSideTranslatorPB.class);
|
60 | 71 |
|
61 | 72 | private AliasMapProtocolPB rpcProxy;
|
62 | 73 |
|
63 |
| - public InMemoryAliasMapProtocolClientSideTranslatorPB(Configuration conf) { |
64 |
| - String addr = conf.getTrimmed(DFS_PROVIDED_ALIASMAP_INMEMORY_RPC_ADDRESS, |
65 |
| - DFS_PROVIDED_ALIASMAP_INMEMORY_RPC_ADDRESS_DEFAULT); |
66 |
| - InetSocketAddress aliasMapAddr = NetUtils.createSocketAddr(addr); |
| 74 | + public InMemoryAliasMapProtocolClientSideTranslatorPB( |
| 75 | + AliasMapProtocolPB rpcProxy) { |
| 76 | + this.rpcProxy = rpcProxy; |
| 77 | + } |
67 | 78 |
|
68 |
| - RPC.setProtocolEngine(conf, AliasMapProtocolPB.class, |
69 |
| - ProtobufRpcEngine.class); |
70 |
| - LOG.info("Connecting to address: " + addr); |
71 |
| - try { |
72 |
| - rpcProxy = RPC.getProxy(AliasMapProtocolPB.class, |
73 |
| - RPC.getProtocolVersion(AliasMapProtocolPB.class), aliasMapAddr, null, |
74 |
| - conf, NetUtils.getDefaultSocketFactory(conf), 0); |
75 |
| - } catch (IOException e) { |
76 |
| - throw new RuntimeException( |
77 |
| - "Error in connecting to " + addr + " Got: " + e); |
| 79 | + public static Collection<InMemoryAliasMapProtocol> init(Configuration conf) { |
| 80 | + Collection<InMemoryAliasMapProtocol> aliasMaps = new ArrayList<>(); |
| 81 | + // Try to connect to all configured nameservices as it is not known which |
| 82 | + // nameservice supports the AliasMap. |
| 83 | + for (String nsId : getNameServiceIds(conf)) { |
| 84 | + try { |
| 85 | + URI namenodeURI = null; |
| 86 | + Configuration newConf = new Configuration(conf); |
| 87 | + if (HAUtil.isHAEnabled(conf, nsId)) { |
| 88 | + // set the failover-proxy provider if HA is enabled. |
| 89 | + newConf.setClass( |
| 90 | + addKeySuffixes(PROXY_PROVIDER_KEY_PREFIX, nsId), |
| 91 | + InMemoryAliasMapFailoverProxyProvider.class, |
| 92 | + AbstractNNFailoverProxyProvider.class); |
| 93 | + namenodeURI = new URI(HdfsConstants.HDFS_URI_SCHEME + "://" + nsId); |
| 94 | + } else { |
| 95 | + String key = |
| 96 | + addKeySuffixes(DFS_PROVIDED_ALIASMAP_INMEMORY_RPC_ADDRESS, nsId); |
| 97 | + String addr = conf.get(key); |
| 98 | + if (addr != null) { |
| 99 | + namenodeURI = createUri(HdfsConstants.HDFS_URI_SCHEME, |
| 100 | + NetUtils.createSocketAddr(addr)); |
| 101 | + } |
| 102 | + } |
| 103 | + if (namenodeURI != null) { |
| 104 | + aliasMaps.add(NameNodeProxies |
| 105 | + .createProxy(newConf, namenodeURI, InMemoryAliasMapProtocol.class) |
| 106 | + .getProxy()); |
| 107 | + LOG.info("Connected to InMemoryAliasMap at {}", namenodeURI); |
| 108 | + } |
| 109 | + } catch (IOException | URISyntaxException e) { |
| 110 | + LOG.warn("Exception in connecting to InMemoryAliasMap for nameservice " |
| 111 | + + "{}: {}", nsId, e); |
| 112 | + } |
78 | 113 | }
|
| 114 | + // if a separate AliasMap is configured using |
| 115 | + // DFS_PROVIDED_ALIASMAP_INMEMORY_RPC_ADDRESS, try to connect it. |
| 116 | + if (conf.get(DFS_PROVIDED_ALIASMAP_INMEMORY_RPC_ADDRESS) != null) { |
| 117 | + URI uri = createUri("hdfs", NetUtils.createSocketAddr( |
| 118 | + conf.get(DFS_PROVIDED_ALIASMAP_INMEMORY_RPC_ADDRESS))); |
| 119 | + try { |
| 120 | + aliasMaps.add(NameNodeProxies |
| 121 | + .createProxy(conf, uri, InMemoryAliasMapProtocol.class).getProxy()); |
| 122 | + LOG.info("Connected to InMemoryAliasMap at {}", uri); |
| 123 | + } catch (IOException e) { |
| 124 | + LOG.warn("Exception in connecting to InMemoryAliasMap at {}: {}", uri, |
| 125 | + e); |
| 126 | + } |
| 127 | + } |
| 128 | + return aliasMaps; |
79 | 129 | }
|
80 | 130 |
|
81 | 131 | @Override
|
@@ -168,7 +218,12 @@ public String getBlockPoolId() throws IOException {
|
168 | 218 | }
|
169 | 219 | }
|
170 | 220 |
|
171 |
| - public void stop() { |
172 |
| - RPC.stopProxy(rpcProxy); |
| 221 | + @Override |
| 222 | + public void close() throws IOException { |
| 223 | + LOG.info("Stopping rpcProxy in" + |
| 224 | + "InMemoryAliasMapProtocolClientSideTranslatorPB"); |
| 225 | + if (rpcProxy != null) { |
| 226 | + RPC.stopProxy(rpcProxy); |
| 227 | + } |
173 | 228 | }
|
174 | 229 | }
|
0 commit comments