You are viewing a plain text version of this content. The canonical link for it is here.
Posted to hdfs-commits@hadoop.apache.org by ji...@apache.org on 2011/04/13 02:44:15 UTC

svn commit: r1091619 - in /hadoop/hdfs/trunk: CHANGES.txt src/java/org/apache/hadoop/fs/Hdfs.java src/java/org/apache/hadoop/hdfs/DistributedFileSystem.java src/test/hdfs/org/apache/hadoop/fs/TestResolveHdfsSymlink.java

Author: jitendra
Date: Wed Apr 13 00:44:14 2011
New Revision: 1091619

URL: http://svn.apache.org/viewvc?rev=1091619&view=rev
Log:
HDFS-1442. Api to get delegation token in Hdfs. Contributed by jitendra.

Added:
    hadoop/hdfs/trunk/src/test/hdfs/org/apache/hadoop/fs/TestResolveHdfsSymlink.java
Modified:
    hadoop/hdfs/trunk/CHANGES.txt
    hadoop/hdfs/trunk/src/java/org/apache/hadoop/fs/Hdfs.java
    hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/DistributedFileSystem.java

Modified: hadoop/hdfs/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/hdfs/trunk/CHANGES.txt?rev=1091619&r1=1091618&r2=1091619&view=diff
==============================================================================
--- hadoop/hdfs/trunk/CHANGES.txt (original)
+++ hadoop/hdfs/trunk/CHANGES.txt Wed Apr 13 00:44:14 2011
@@ -30,6 +30,8 @@ Trunk (unreleased changes)
     HDFS-1606. Provide a stronger data guarantee in the write pipeline by
     adding a new datanode when an existing datanode failed.  (szetszwo)
 
+    HDFS-1442. Api to get delegation token in Hdfs class. (jitendra)
+
   IMPROVEMENTS
 
     HDFS-1510. Added test-patch.properties required by test-patch.sh (nigel)

Modified: hadoop/hdfs/trunk/src/java/org/apache/hadoop/fs/Hdfs.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/trunk/src/java/org/apache/hadoop/fs/Hdfs.java?rev=1091619&r1=1091618&r2=1091619&view=diff
==============================================================================
--- hadoop/hdfs/trunk/src/java/org/apache/hadoop/fs/Hdfs.java (original)
+++ hadoop/hdfs/trunk/src/java/org/apache/hadoop/fs/Hdfs.java Wed Apr 13 00:44:14 2011
@@ -25,6 +25,8 @@ import java.net.URI;
 import java.net.URISyntaxException;
 import java.util.ArrayList;
 import java.util.EnumSet;
+import java.util.List;
+import java.util.NoSuchElementException;
 
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.classification.InterfaceStability;
@@ -37,8 +39,13 @@ import org.apache.hadoop.hdfs.protocol.D
 import org.apache.hadoop.hdfs.protocol.FSConstants;
 import org.apache.hadoop.hdfs.protocol.HdfsFileStatus;
 import org.apache.hadoop.hdfs.protocol.HdfsLocatedFileStatus;
+import org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenIdentifier;
 import org.apache.hadoop.hdfs.server.namenode.NameNode;
+import org.apache.hadoop.io.Text;
 import org.apache.hadoop.security.AccessControlException;
+import org.apache.hadoop.security.token.Token;
+import org.apache.hadoop.security.token.SecretManager.InvalidToken;
+import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenIdentifier;
 import org.apache.hadoop.util.Progressable;
 
 @InterfaceAudience.Private
@@ -249,7 +256,7 @@ public class Hdfs extends AbstractFileSy
       if (hasNext()) {
         return thisListing.getPartialListing()[i++];
       }
-      throw new java.util.NoSuchElementException("No more entry in " + src);
+      throw new NoSuchElementException("No more entry in " + src);
     }
   }
 
@@ -384,4 +391,43 @@ public class Hdfs extends AbstractFileSy
   public Path getLinkTarget(Path p) throws IOException { 
     return new Path(dfs.getLinkTarget(getUriPath(p)));
   }
+  
+  @Override //AbstractFileSystem
+  public List<Token<?>> getDelegationTokens(String renewer) throws IOException {
+    Token<DelegationTokenIdentifier> result = dfs
+        .getDelegationToken(renewer == null ? null : new Text(renewer));
+    result.setService(new Text(this.getCanonicalServiceName()));
+    List<Token<?>> tokenList = new ArrayList<Token<?>>();
+    tokenList.add(result);
+    return tokenList;
+  }
+
+  /**
+   * Renew an existing delegation token.
+   * 
+   * @param token delegation token obtained earlier
+   * @return the new expiration time
+   * @throws InvalidToken
+   * @throws IOException
+   */
+  @SuppressWarnings("unchecked")
+  public long renewDelegationToken(
+      Token<? extends AbstractDelegationTokenIdentifier> token)
+      throws InvalidToken, IOException {
+    return dfs.renewDelegationToken((Token<DelegationTokenIdentifier>) token);
+  }
+
+  /**
+   * Cancel an existing delegation token.
+   * 
+   * @param token delegation token
+   * @throws InvalidToken
+   * @throws IOException
+   */
+  @SuppressWarnings("unchecked")
+  public void cancelDelegationToken(
+      Token<? extends AbstractDelegationTokenIdentifier> token)
+      throws InvalidToken, IOException {
+    dfs.cancelDelegationToken((Token<DelegationTokenIdentifier>) token);
+  }
 }

Modified: hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/DistributedFileSystem.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/DistributedFileSystem.java?rev=1091619&r1=1091618&r2=1091619&view=diff
==============================================================================
--- hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/DistributedFileSystem.java (original)
+++ hadoop/hdfs/trunk/src/java/org/apache/hadoop/hdfs/DistributedFileSystem.java Wed Apr 13 00:44:14 2011
@@ -24,6 +24,7 @@ import java.net.InetSocketAddress;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.EnumSet;
+import java.util.List;
 
 import org.apache.hadoop.classification.InterfaceAudience;
 import org.apache.hadoop.classification.InterfaceStability;
@@ -810,6 +811,14 @@ public class DistributedFileSystem exten
       throws IOException {
     return dfs.getDelegationToken(renewer);
   }
+  
+  @Override // FileSystem
+  public List<Token<?>> getDelegationTokens(String renewer) throws IOException {
+    List<Token<?>> tokenList = new ArrayList<Token<?>>();
+    Token<DelegationTokenIdentifier> token = this.getDelegationToken(renewer);
+    tokenList.add(token);
+    return tokenList;
+  }
 
   /**
    * Renew an existing delegation token.

Added: hadoop/hdfs/trunk/src/test/hdfs/org/apache/hadoop/fs/TestResolveHdfsSymlink.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/trunk/src/test/hdfs/org/apache/hadoop/fs/TestResolveHdfsSymlink.java?rev=1091619&view=auto
==============================================================================
--- hadoop/hdfs/trunk/src/test/hdfs/org/apache/hadoop/fs/TestResolveHdfsSymlink.java (added)
+++ hadoop/hdfs/trunk/src/test/hdfs/org/apache/hadoop/fs/TestResolveHdfsSymlink.java Wed Apr 13 00:44:14 2011
@@ -0,0 +1,122 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hadoop.fs;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Set;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hdfs.DFSTestUtil;
+import org.apache.hadoop.hdfs.HdfsConfiguration;
+import org.apache.hadoop.hdfs.MiniDFSCluster;
+import org.apache.hadoop.hdfs.security.token.delegation.DelegationTokenIdentifier;
+import org.apache.hadoop.security.UserGroupInformation;
+import org.apache.hadoop.security.token.Token;
+import org.apache.hadoop.security.token.delegation.AbstractDelegationTokenIdentifier;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Tests whether FileContext can resolve an hdfs path that has a symlink to
+ * local file system. Also tests getDelegationTokens API in file context with
+ * underlying file system as Hdfs.
+ */
+public class TestResolveHdfsSymlink {
+  private static MiniDFSCluster cluster = null;
+
+  @BeforeClass
+  public static void setUp() throws IOException {
+    Configuration conf = new HdfsConfiguration();
+    cluster = new MiniDFSCluster.Builder(conf).build();
+    cluster.getNamesystem().getDelegationTokenSecretManager().startThreads();
+    cluster.waitActive();
+  }
+
+  @AfterClass
+  public static void tearDown() {
+    if (cluster != null) {
+      cluster.shutdown();
+    }
+  }
+
+  /**
+   * Tests resolution of an hdfs symlink to the local file system.
+   * @throws IOException
+   * @throws InterruptedException
+   */
+  @Test
+  public void testFcResolveAfs() throws IOException, InterruptedException {
+    Configuration conf = new Configuration();
+    FileContext fcLocal = FileContext.getLocalFSFileContext();
+    FileContext fcHdfs = FileContext.getFileContext(cluster.getFileSystem()
+        .getUri());
+
+    Path alphaLocalPath = new Path(fcLocal.getDefaultFileSystem().getUri()
+        .toString(), "/tmp/alpha");
+    DFSTestUtil.createFile(FileSystem.getLocal(conf), alphaLocalPath, 16,
+        (short) 1, 2);
+
+    Path linkTarget = new Path(fcLocal.getDefaultFileSystem().getUri()
+        .toString(), "/tmp");
+    Path hdfsLink = new Path(fcHdfs.getDefaultFileSystem().getUri().toString(),
+        "/tmp/link");
+    fcHdfs.createSymlink(linkTarget, hdfsLink, true);
+
+    Path alphaHdfsPathViaLink = new Path(fcHdfs.getDefaultFileSystem().getUri()
+        .toString()
+        + "/tmp/link/alpha");
+
+    Set<AbstractFileSystem> afsList = fcHdfs
+        .resolveAbstractFileSystems(alphaHdfsPathViaLink);
+    Assert.assertEquals(2, afsList.size());
+    for (AbstractFileSystem afs : afsList) {
+      if ((!afs.equals(fcHdfs.getDefaultFileSystem()))
+          && (!afs.equals(fcLocal.getDefaultFileSystem()))) {
+        Assert.fail("Failed to resolve AFS correctly");
+      }
+    }
+  }
+  
+  /**
+   * Tests delegation token APIs in FileContext for Hdfs; and renew and cancel
+   * APIs in Hdfs.
+   * 
+   * @throws UnsupportedFileSystemException
+   * @throws IOException
+   * @throws InterruptedException
+   */
+  @SuppressWarnings("unchecked")
+  @Test
+  public void testFcDelegationToken() throws UnsupportedFileSystemException,
+      IOException, InterruptedException {
+    FileContext fcHdfs = FileContext.getFileContext(cluster.getFileSystem()
+        .getUri());
+    final AbstractFileSystem afs = fcHdfs.getDefaultFileSystem();
+    final List<Token<?>> tokenList =
+        afs.getDelegationTokens(UserGroupInformation.getCurrentUser()
+            .getUserName());
+    ((Hdfs) afs).renewDelegationToken((Token<DelegationTokenIdentifier>) tokenList
+        .get(0));
+    ((Hdfs) afs).cancelDelegationToken(
+        (Token<? extends AbstractDelegationTokenIdentifier>) tokenList.get(0));
+  }
+}