/// <exception cref="System.Exception"/>
        public virtual void TestHAUtilClonesDelegationTokens()
        {
            Org.Apache.Hadoop.Security.Token.Token <DelegationTokenIdentifier> token = GetDelegationToken
                                                                                           (fs, "JobTracker");
            UserGroupInformation ugi = UserGroupInformation.CreateRemoteUser("test");
            URI haUri = new URI("hdfs://my-ha-uri/");

            token.SetService(HAUtil.BuildTokenServiceForLogicalUri(haUri, HdfsConstants.HdfsUriScheme
                                                                   ));
            ugi.AddToken(token);
            ICollection <IPEndPoint> nnAddrs = new HashSet <IPEndPoint>();

            nnAddrs.AddItem(new IPEndPoint("localhost", nn0.GetNameNodeAddress().Port));
            nnAddrs.AddItem(new IPEndPoint("localhost", nn1.GetNameNodeAddress().Port));
            HAUtil.CloneDelegationTokenForLogicalUri(ugi, haUri, nnAddrs);
            ICollection <Org.Apache.Hadoop.Security.Token.Token <TokenIdentifier> > tokens = ugi
                                                                                             .GetTokens();

            NUnit.Framework.Assert.AreEqual(3, tokens.Count);
            Log.Info("Tokens:\n" + Joiner.On("\n").Join(tokens));
            DelegationTokenSelector dts = new DelegationTokenSelector();

            // check that the token selected for one of the physical IPC addresses
            // matches the one we received
            foreach (IPEndPoint addr in nnAddrs)
            {
                Text ipcDtService = SecurityUtil.BuildTokenService(addr);
                Org.Apache.Hadoop.Security.Token.Token <DelegationTokenIdentifier> token2 = dts.SelectToken
                                                                                                (ipcDtService, ugi.GetTokens());
                NUnit.Framework.Assert.IsNotNull(token2);
                Assert.AssertArrayEquals(token.GetIdentifier(), token2.GetIdentifier());
                Assert.AssertArrayEquals(token.GetPassword(), token2.GetPassword());
            }
            // switch to host-based tokens, shouldn't match existing tokens
            SecurityUtilTestHelper.SetTokenServiceUseIp(false);
            foreach (IPEndPoint addr_1 in nnAddrs)
            {
                Text ipcDtService = SecurityUtil.BuildTokenService(addr_1);
                Org.Apache.Hadoop.Security.Token.Token <DelegationTokenIdentifier> token2 = dts.SelectToken
                                                                                                (ipcDtService, ugi.GetTokens());
                NUnit.Framework.Assert.IsNull(token2);
            }
            // reclone the tokens, and see if they match now
            HAUtil.CloneDelegationTokenForLogicalUri(ugi, haUri, nnAddrs);
            foreach (IPEndPoint addr_2 in nnAddrs)
            {
                Text ipcDtService = SecurityUtil.BuildTokenService(addr_2);
                Org.Apache.Hadoop.Security.Token.Token <DelegationTokenIdentifier> token2 = dts.SelectToken
                                                                                                (ipcDtService, ugi.GetTokens());
                NUnit.Framework.Assert.IsNotNull(token2);
                Assert.AssertArrayEquals(token.GetIdentifier(), token2.GetIdentifier());
                Assert.AssertArrayEquals(token.GetPassword(), token2.GetPassword());
            }
        }
        public ConfiguredFailoverProxyProvider(Configuration conf, URI uri, Type xface)
        {
            Preconditions.CheckArgument(xface.IsAssignableFrom(typeof(NamenodeProtocols)), "Interface class %s is not a valid NameNode protocol!"
                                        );
            this.xface = xface;
            this.conf  = new Configuration(conf);
            int maxRetries = this.conf.GetInt(DFSConfigKeys.DfsClientFailoverConnectionRetriesKey
                                              , DFSConfigKeys.DfsClientFailoverConnectionRetriesDefault);

            this.conf.SetInt(CommonConfigurationKeysPublic.IpcClientConnectMaxRetriesKey, maxRetries
                             );
            int maxRetriesOnSocketTimeouts = this.conf.GetInt(DFSConfigKeys.DfsClientFailoverConnectionRetriesOnSocketTimeoutsKey
                                                              , DFSConfigKeys.DfsClientFailoverConnectionRetriesOnSocketTimeoutsDefault);

            this.conf.SetInt(CommonConfigurationKeysPublic.IpcClientConnectMaxRetriesOnSocketTimeoutsKey
                             , maxRetriesOnSocketTimeouts);
            try
            {
                ugi = UserGroupInformation.GetCurrentUser();
                IDictionary <string, IDictionary <string, IPEndPoint> > map = DFSUtil.GetHaNnRpcAddresses
                                                                                  (conf);
                IDictionary <string, IPEndPoint> addressesInNN = map[uri.GetHost()];
                if (addressesInNN == null || addressesInNN.Count == 0)
                {
                    throw new RuntimeException("Could not find any configured addresses " + "for URI "
                                               + uri);
                }
                ICollection <IPEndPoint> addressesOfNns = addressesInNN.Values;
                foreach (IPEndPoint address in addressesOfNns)
                {
                    proxies.AddItem(new ConfiguredFailoverProxyProvider.AddressRpcProxyPair <T>(address
                                                                                                ));
                }
                // The client may have a delegation token set for the logical
                // URI of the cluster. Clone this token to apply to each of the
                // underlying IPC addresses so that the IPC code can find it.
                HAUtil.CloneDelegationTokenForLogicalUri(ugi, uri, addressesOfNns);
            }
            catch (IOException e)
            {
                throw new RuntimeException(e);
            }
        }