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/03/03 00:44:06 UTC

svn commit: r1076460 - in /hadoop/hdfs/branches/HDFS-1052: ./ src/java/org/apache/hadoop/hdfs/ src/java/org/apache/hadoop/hdfs/server/datanode/ src/java/org/apache/hadoop/hdfs/server/namenode/ src/test/hdfs/org/apache/hadoop/hdfs/ src/test/hdfs/org/apa...

Author: jitendra
Date: Wed Mar  2 23:44:05 2011
New Revision: 1076460

URL: http://svn.apache.org/viewvc?rev=1076460&view=rev
Log:
Federation: Configuration for namenodes. Contributed by suresh and jitendra.

Modified:
    hadoop/hdfs/branches/HDFS-1052/CHANGES.txt
    hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/DFSConfigKeys.java
    hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/DFSUtil.java
    hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/server/datanode/DataNode.java
    hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/server/namenode/NameNode.java
    hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/server/namenode/SecondaryNameNode.java
    hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/MiniDFSCluster.java
    hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/TestDFSUtil.java
    hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/server/datanode/TestMulitipleNNDataBlockScanner.java
    hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestCheckpoint.java

Modified: hadoop/hdfs/branches/HDFS-1052/CHANGES.txt
URL: http://svn.apache.org/viewvc/hadoop/hdfs/branches/HDFS-1052/CHANGES.txt?rev=1076460&r1=1076459&r2=1076460&view=diff
==============================================================================
--- hadoop/hdfs/branches/HDFS-1052/CHANGES.txt (original)
+++ hadoop/hdfs/branches/HDFS-1052/CHANGES.txt Wed Mar  2 23:44:05 2011
@@ -149,6 +149,8 @@ Trunk (unreleased changes)
     HDFS-1694. Federation: SimulatedFSDataset changes to work with
     federation and multiple block pools. (suresh)
 
+    HDFS-1689. Federation: Configuration for namenodes. (suresh and jitendra)
+
   IMPROVEMENTS
 
     HDFS-1510. Added test-patch.properties required by test-patch.sh (nigel)

Modified: hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/DFSConfigKeys.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/DFSConfigKeys.java?rev=1076460&r1=1076459&r2=1076460&view=diff
==============================================================================
--- hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/DFSConfigKeys.java (original)
+++ hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/DFSConfigKeys.java Wed Mar  2 23:44:05 2011
@@ -50,6 +50,7 @@ public class DFSConfigKeys extends Commo
   public static final long    DFS_DATANODE_BALANCE_BANDWIDTHPERSEC_DEFAULT = 1024*1024;
   public static final String  DFS_NAMENODE_HTTP_ADDRESS_KEY = "dfs.namenode.http-address";
   public static final String  DFS_NAMENODE_HTTP_ADDRESS_DEFAULT = "0.0.0.0:50070";
+  public static final String  DFS_NAMENODE_RPC_ADDRESS_KEY = "dfs.namenode.rpc-address";
   public static final String  DFS_NAMENODE_SERVICE_RPC_ADDRESS_KEY = "dfs.namenode.servicerpc-address";
   public static final String  DFS_NAMENODE_MAX_OBJECTS_KEY = "dfs.namenode.max.objects";
   public static final long    DFS_NAMENODE_MAX_OBJECTS_DEFAULT = 0;
@@ -230,5 +231,6 @@ public class DFSConfigKeys extends Commo
   public static final String  DFS_NAMENODE_NAME_CACHE_THRESHOLD_KEY = "dfs.namenode.name.cache.threshold";
   public static final int     DFS_NAMENODE_NAME_CACHE_THRESHOLD_DEFAULT = 10;
   
-  public static final String DFS_FEDERATION_NAMENODES = "dfs.federation.namenodes.uri";
+  public static final String DFS_FEDERATION_NAMESERVICES = "dfs.federation.nameservices";
+  public static final String DFS_FEDERATION_NAMESERVICE_ID = "dfs.federation.nameservice.id";
 }

Modified: hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/DFSUtil.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/DFSUtil.java?rev=1076460&r1=1076459&r2=1076460&view=diff
==============================================================================
--- hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/DFSUtil.java (original)
+++ hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/DFSUtil.java Wed Mar  2 23:44:05 2011
@@ -24,7 +24,6 @@ import java.net.InetSocketAddress;
 import java.net.URI;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Iterator;
 import java.util.List;
 import java.util.StringTokenizer;
 
@@ -38,12 +37,11 @@ import org.apache.hadoop.hdfs.protocol.L
 import org.apache.hadoop.hdfs.protocol.LocatedBlocks;
 import org.apache.hadoop.hdfs.server.namenode.NameNode;
 import org.apache.hadoop.net.NodeBase;
+import static org.apache.hadoop.hdfs.DFSConfigKeys.*;
 
 @InterfaceAudience.Private
 public class DFSUtil {
   
-  final private static String PRIMARY_NAMENODE_SUFFIX = "-primary.namenode";
-  
   /**
    * Whether the pathname is valid.  Currently prohibits relative paths, 
    * and names which contain a ":" or "/" 
@@ -244,68 +242,108 @@ public class DFSUtil {
   }
   
   /**
-   * Returns the list of namenode URIs
+   * Returns collection of nameservice Ids from the configuration.
    * 
-   * @param conf
-   * @return list of namenode URIs
+   * @param conf configuration
+   * @return collection of nameservice Ids
    */
-  public static List<URI> getNamenodeList(Configuration conf) {
-    Collection<String> namenodes = conf
-        .getStringCollection(DFSConfigKeys.DFS_FEDERATION_NAMENODES);
-
-    List<URI> namenodeUris = new ArrayList<URI>();
-    if (namenodes.isEmpty()) {
-      namenodeUris.add(getDefaultNamenode(conf));
+  public static Collection<String> getNameServiceIds(Configuration conf) {
+    return conf.getStringCollection(DFS_FEDERATION_NAMESERVICES);
+  }
+  
+  /**
+   * Returns the InetSocketAddresses of namenodes from the configuration.
+   * Note this is to be used by datanodes to get the list of namenode addresses
+   * to talk to.
+   * 
+   * If namenode address specifically configured for datanodes (using service
+   * ports) is found, it is returned. If not, regular RPC address configured 
+   * for other clients is returned.
+   * 
+   * @param conf configuration
+   * @return list of InetSocketAddresses
+   * @throws IOException on error
+   */
+  public static List<InetSocketAddress> getNNServiceRpcAddresses(
+      Configuration conf) throws IOException {
+    Collection<String> nameserviceIds = getNameServiceIds(conf);
+    List<InetSocketAddress> isas = new ArrayList<InetSocketAddress>();
+
+    // Configuration with a single namenode
+    if (nameserviceIds == null || nameserviceIds.isEmpty()) {
+      String namenodeAddress = conf
+          .get(DFS_NAMENODE_SERVICE_RPC_ADDRESS_KEY);
+      if (namenodeAddress == null) {
+        // Fall back to regular rpc address
+        namenodeAddress = conf.get(DFS_NAMENODE_RPC_ADDRESS_KEY);
+      }
+      if (namenodeAddress == null) {
+        isas.add(NameNode.getAddress(getDefaultNamenode(conf)));
+      } else {
+        isas.add(NameNode.getAddress(namenodeAddress));
+      }
     } else {
-      Iterator<String> nIter = namenodes.iterator();
-      while (nIter.hasNext()) {
-        namenodeUris.add(URI.create(nIter.next()));
+      // Get the namenodes for all the configured nameServiceIds
+      for (String nameserviceId : nameserviceIds) {
+        String namenodeAddress = conf.get(getNameServiceIdKey(
+            DFS_NAMENODE_SERVICE_RPC_ADDRESS_KEY, nameserviceId));
+        if (namenodeAddress == null) {
+          // Fallback to regular rpc address
+          namenodeAddress = conf.get(getNameServiceIdKey(
+              DFS_NAMENODE_RPC_ADDRESS_KEY, nameserviceId));
+        }
+        if (namenodeAddress == null) {
+          throw new IOException("Incorrect configuration: "
+              + "No namenode rpc address configured for nameserviceId "
+              + nameserviceId);
+        }
+        isas.add(NameNode.getAddress(namenodeAddress));
       }
     }
-    return namenodeUris;
+    return isas;
   }
-
+  
   /**
-   * Returns the hostname of the primary namenode for a given secondary
-   * namenode.
+   * @return key specific to a nameserviceId from a generic key
+   */
+  public static String getNameServiceIdKey(String key, String nameserviceId) {
+    return key + "." + nameserviceId;
+  }
+  
+  /**
+   * Sets the node specific setting into generic configuration key. Looks up
+   * value of "key.nameserviceId" and if found sets that value into generic key 
+   * in the conf. Note that this only modifies the runtime conf.
    * 
    * @param conf
-   * @param secondaryHostName
-   *          hostname of the secondary namenode.
-   * @return primary namenode hostname
-   */
-  public static URI getPrimaryNamenode(Configuration conf,
-      String secondaryHostName) {
-    String key = secondaryHostName + PRIMARY_NAMENODE_SUFFIX;
-    String nn = conf.get(key);
-    if (nn == null) {
-      return getDefaultNamenode(conf);
-    } else {
-      return URI.create(nn);
+   *          Configuration object to lookup specific key and to set the value
+   *          to the key passed. Note the conf object is modified.
+   * @param nameserviceId
+   *          nameservice Id to construct the node specific key.
+   * @param keys
+   *          The key for which node specific value is looked up
+   */
+  public static void setGenericConf(Configuration conf,
+      String nameserviceId, String... keys) {
+    for (String key : keys) {
+      String value = conf.get(getNameServiceIdKey(key, nameserviceId));
+      if (value != null) {
+        conf.set(key, value);
+      }
     }
   }
   
   /**
-   * Returns the InetSocketAddresses for each configured namenode
+   * Returns the configured nameservice Id
+   * 
    * @param conf
-   * @return Array of InetSocketAddresses
-   * @throws IOException
+   *          Configuration object to lookup the nameserviceId
+   * @return nameserviceId string from conf
    */
-  public static List<InetSocketAddress> getNNAddresses(Configuration conf)
-      throws IOException {
-    List<URI> nns = getNamenodeList(conf);
-    if (nns == null) {
-      throw new IOException("Federation namnodes are not configured correctly");
-    }
-
-    List<InetSocketAddress> isas = new ArrayList<InetSocketAddress>();
-    for (URI u : nns) {
-      isas.add(NameNode.getAddress(u));
-    }
-    return isas;
+  public static String getNameServiceId(Configuration conf) {
+    return conf.get(DFS_FEDERATION_NAMESERVICE_ID);
   }
   
-  
   /** Return used as percentage of capacity */
   public static float getPercentUsed(long used, long capacity) {
     return capacity <= 0 ? 100 : ((float)used * 100.0f)/(float)capacity; 

Modified: hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/server/datanode/DataNode.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/server/datanode/DataNode.java?rev=1076460&r1=1076459&r2=1076460&view=diff
==============================================================================
--- hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/server/datanode/DataNode.java (original)
+++ hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/server/datanode/DataNode.java Wed Mar  2 23:44:05 2011
@@ -215,7 +215,7 @@ public class DataNode extends Configured
       bpMapping = new HashMap<String, BPOfferService>();
       nameNodeThreads = new HashMap<InetSocketAddress, BPOfferService>();
   
-      List<InetSocketAddress> isas = DFSUtil.getNNAddresses(conf);
+      List<InetSocketAddress> isas = DFSUtil.getNNServiceRpcAddresses(conf);
       for(InetSocketAddress isa : isas) {
         BPOfferService bpos = new BPOfferService(isa);
         nameNodeThreads.put(bpos.getNNSocketAddress(), bpos);
@@ -278,9 +278,10 @@ public class DataNode extends Configured
     
     void refreshNamenodes(Configuration conf)
         throws IOException, InterruptedException {
-      LOG.info("Refresh request received for namnodes: "
-          + conf.get(DFSConfigKeys.DFS_FEDERATION_NAMENODES));
-      List<InetSocketAddress> newAddresses = DFSUtil.getNNAddresses(conf);
+      LOG.info("Refresh request received for nameservices: "
+          + conf.get(DFSConfigKeys.DFS_FEDERATION_NAMESERVICES));
+      List<InetSocketAddress> newAddresses = 
+        DFSUtil.getNNServiceRpcAddresses(conf);
       List<BPOfferService> toShutdown = new ArrayList<BPOfferService>();
       List<InetSocketAddress> toStart = new ArrayList<InetSocketAddress>();
       synchronized (refreshNamenodesLock) {

Modified: hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/server/namenode/NameNode.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/server/namenode/NameNode.java?rev=1076460&r1=1076459&r2=1076460&view=diff
==============================================================================
--- hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/server/namenode/NameNode.java (original)
+++ hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/server/namenode/NameNode.java Wed Mar  2 23:44:05 2011
@@ -43,6 +43,7 @@ import org.apache.hadoop.fs.UnresolvedLi
 import org.apache.hadoop.fs.permission.FsPermission;
 import org.apache.hadoop.fs.permission.PermissionStatus;
 import org.apache.hadoop.hdfs.DFSConfigKeys;
+import org.apache.hadoop.hdfs.DFSUtil;
 import org.apache.hadoop.hdfs.HDFSPolicyProvider;
 import org.apache.hadoop.hdfs.HdfsConfiguration;
 import org.apache.hadoop.hdfs.protocol.Block;
@@ -579,6 +580,7 @@ public class NameNode implements Namenod
       throws IOException { 
     this.role = role;
     try {
+      initializeGenericKeys(conf);
       initialize(conf);
     } catch (IOException e) {
       this.stop();
@@ -1573,6 +1575,51 @@ public class NameNode implements Namenod
         return new NameNode(conf);
     }
   }
+
+  /**
+   * In federation configuration is set for a set of
+   * namenode and secondary namenode/backup/checkpointer, which are
+   * grouped under a logical nameservice ID. The configuration keys specific 
+   * to them have suffix set to configured nameserviceId.
+   * 
+   * This method copies the value from specific key of format key.nameserviceId
+   * to key, to set up the generic configuration. Once this is done, only
+   * generic version of the configuration is read in rest of the code, for
+   * backward compatibility and simpler code changes.
+   * 
+   * @param conf
+   *          Configuration object to lookup specific key and to set the value
+   *          to the key passed. Note the conf object is modified
+   * @see DFSUtil#setGenericConf()
+   */
+  static void initializeGenericKeys(Configuration conf) {
+    final String nameserviceId = DFSUtil.getNameServiceId(conf);
+    if ((nameserviceId == null) || nameserviceId.isEmpty()) {
+      return;
+    }
+    
+    DFSUtil.setGenericConf(conf, nameserviceId,
+        DFSConfigKeys.DFS_NAMENODE_RPC_ADDRESS_KEY,
+        DFSConfigKeys.DFS_NAMENODE_SERVICE_RPC_ADDRESS_KEY,
+        DFSConfigKeys.DFS_NAMENODE_HTTP_ADDRESS_KEY,
+        DFSConfigKeys.DFS_NAMENODE_HTTPS_ADDRESS_KEY,
+        DFSConfigKeys.DFS_NAMENODE_USER_NAME_KEY,
+        DFSConfigKeys.DFS_NAMENODE_KRB_HTTPS_USER_NAME_KEY,
+        DFSConfigKeys.DFS_NAMENODE_KEYTAB_FILE_KEY,
+        DFSConfigKeys.DFS_SECONDARY_NAMENODE_USER_NAME_KEY,
+        DFSConfigKeys.DFS_SECONDARY_NAMENODE_KRB_HTTPS_USER_NAME_KEY,
+        DFSConfigKeys.DFS_NAMENODE_SECONDARY_HTTP_ADDRESS_KEY,
+        DFSConfigKeys.DFS_SECONDARY_NAMENODE_KEYTAB_FILE_KEY,
+        DFSConfigKeys.DFS_NAMENODE_BACKUP_ADDRESS_KEY,
+        DFSConfigKeys.DFS_NAMENODE_BACKUP_HTTP_ADDRESS_KEY,
+        DFSConfigKeys.DFS_NAMENODE_BACKUP_SERVICE_RPC_ADDRESS_KEY);
+    
+    if (conf.get(DFSConfigKeys.DFS_NAMENODE_RPC_ADDRESS_KEY) != null) {
+      URI defaultUri = URI.create(FSConstants.HDFS_URI_SCHEME + "://"
+          + conf.get(DFSConfigKeys.DFS_NAMENODE_RPC_ADDRESS_KEY));
+      conf.set(DFSConfigKeys.FS_DEFAULT_NAME_KEY, defaultUri.toString());
+    }
+  }
     
   /**
    */

Modified: hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/server/namenode/SecondaryNameNode.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/server/namenode/SecondaryNameNode.java?rev=1076460&r1=1076459&r2=1076460&view=diff
==============================================================================
--- hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/server/namenode/SecondaryNameNode.java (original)
+++ hadoop/hdfs/branches/HDFS-1052/src/java/org/apache/hadoop/hdfs/server/namenode/SecondaryNameNode.java Wed Mar  2 23:44:05 2011
@@ -121,6 +121,7 @@ public class SecondaryNameNode implement
    */
   public SecondaryNameNode(Configuration conf)  throws IOException {
     try {
+      NameNode.initializeGenericKeys(conf);
       initialize(conf);
     } catch(IOException e) {
       shutdown();
@@ -362,6 +363,10 @@ public class SecondaryNameNode implement
         throw new RuntimeException(e);
       }
   }
+  
+  InetSocketAddress getNameNodeAddress() {
+    return nameNodeAddr;
+  }
 
   /**
    * Copy the new fsimage into the NameNode

Modified: hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/MiniDFSCluster.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/MiniDFSCluster.java?rev=1076460&r1=1076459&r2=1076460&view=diff
==============================================================================
--- hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/MiniDFSCluster.java (original)
+++ hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/MiniDFSCluster.java Wed Mar  2 23:44:05 2011
@@ -76,6 +76,9 @@ import org.apache.hadoop.util.ToolRunner
  */
 public class MiniDFSCluster {
 
+  private static final String NAMESERVICE_ID_PREFIX = "nameserviceId";
+  private static final Log LOG = LogFactory.getLog(MiniDFSCluster.class);
+
   /**
    * Class to construct instances of MiniDFSClusters with specific options.
    */
@@ -222,7 +225,6 @@ public class MiniDFSCluster {
       this.dnArgs = args;
     }
   }
-  private static final Log LOG = LogFactory.getLog(MiniDFSCluster.class);
 
   private Configuration conf;
   private NameNodeInfo[] nameNodes;
@@ -232,8 +234,6 @@ public class MiniDFSCluster {
   private File base_dir;
   private File data_dir;
   
-  public final static String FINALIZED_DIR_NAME = "/current/finalized/";
-  
   /**
    * Stores the information related to a namenode in the cluster
    */
@@ -242,7 +242,7 @@ public class MiniDFSCluster {
     final Configuration conf;
     NameNodeInfo(NameNode nn, Configuration conf) {
       this.nameNode = nn;
-      this.conf = new Configuration(conf);
+      this.conf = conf;
     }
   }
   
@@ -449,15 +449,30 @@ public class MiniDFSCluster {
                       false);
     }
     
-    // Create namenodes in the cluster
-    for (int i = 0; i < nameNodes.length; i++) {
-    int nnPort = nameNodePort + 2 * i;
-      createNameNode(i, conf, numDataNodes, manageNameDfsDirs, nnPort,
+    int replication = conf.getInt("dfs.replication", 3);
+    conf.setInt("dfs.replication", Math.min(replication, numDataNodes));
+    conf.setInt(DFSConfigKeys.DFS_NAMENODE_SAFEMODE_EXTENSION_KEY, 0);
+    conf.setInt("dfs.namenode.decommission.interval", 3); // 3 second
+    conf.setClass(DFSConfigKeys.NET_TOPOLOGY_NODE_SWITCH_MAPPING_IMPL_KEY, 
+                   StaticMapping.class, DNSToSwitchMapping.class);
+    
+    Collection<String> nameserviceIds = DFSUtil.getNameServiceIds(conf);
+    if (nameserviceIds.isEmpty() && nameNodes.length == 1) {
+      conf.set(DFSConfigKeys.FS_DEFAULT_NAME_KEY, "127.0.0.1:" + nameNodePort);
+      conf.set(DFSConfigKeys.DFS_NAMENODE_HTTP_ADDRESS_KEY, "127.0.0.1:0");
+      NameNode nn = createNameNode(0, conf, numDataNodes, manageNameDfsDirs,
           format, operation, clusterId);
-    }
-    // Set default URI for a single namenode cluster
-    if (nameNodes.length == 1) {
+      nameNodes[0] = new NameNodeInfo(nn, conf);
       FileSystem.setDefaultUri(conf, getURI(0));
+    } else {
+      if (nameserviceIds.isEmpty()) {
+        for (int i = 0; i < nameNodes.length; i++) {
+          nameserviceIds.add(NAMESERVICE_ID_PREFIX + i);
+        }
+      }
+      initFederationConf(conf, nameserviceIds, numDataNodes, nameNodePort);
+      createFederationNamenodes(conf, nameserviceIds, manageNameDfsDirs, format,
+          operation, clusterId);
     }
     
     if (format) {
@@ -474,17 +489,51 @@ public class MiniDFSCluster {
     ProxyUsers.refreshSuperUserGroupsConfiguration(conf);
   }
   
-  private void createNameNode(int nnIndex, Configuration conf,
-              int numDataNodes,
-              boolean manageNameDfsDirs,
-              int nameNodePort,
-              boolean format,
-              StartupOption operation,
-              String clusterId) throws IOException {
-    // TODO:FEDERATION cleanup - cluster configuration needs to change
-    // Setup the NameNode configuration
-    conf.set(DFSConfigKeys.FS_DEFAULT_NAME_KEY, "127.0.0.1:" + nameNodePort);
-    conf.set(DFSConfigKeys.DFS_NAMENODE_HTTP_ADDRESS_KEY, "127.0.0.1:0");  
+  /** Initialize configuration for federated cluster */
+  private static void initFederationConf(Configuration conf,
+      Collection<String> nameserviceIds, int numDataNodes, int nnPort) {
+    String nameserviceIdList = "";
+    for (String nameserviceId : nameserviceIds) {
+      // Create comma separated list of nameserviceIds
+      if (nameserviceIdList.length() > 0) {
+        nameserviceIdList += ",";
+      }
+      nameserviceIdList += nameserviceId;
+      initFederatedNamenodeAddress(conf, nameserviceId, nnPort);
+      nnPort = nnPort == 0 ? 0 : nnPort + 2;
+    }
+    conf.set(DFSConfigKeys.DFS_FEDERATION_NAMESERVICES, nameserviceIdList);
+  }
+
+  /* For federated namenode initialize the address:port */
+  private static void initFederatedNamenodeAddress(Configuration conf,
+      String nameserviceId, int nnPort) {
+    // Set nameserviceId specific key
+    String key = DFSUtil.getNameServiceIdKey(
+        DFSConfigKeys.DFS_NAMENODE_HTTP_ADDRESS_KEY, nameserviceId);
+    conf.set(key, "127.0.0.1:0");
+
+    key = DFSUtil.getNameServiceIdKey(
+        DFSConfigKeys.DFS_NAMENODE_RPC_ADDRESS_KEY, nameserviceId);
+    conf.set(key, "127.0.0.1:" + nnPort);
+  }
+  
+  private void createFederationNamenodes(Configuration conf,
+      Collection<String> nameserviceIds, boolean manageNameDfsDirs,
+      boolean format, StartupOption operation, String clusterId)
+      throws IOException {
+    // Create namenodes in the cluster
+    int nnCounter = 0;
+    for (String nameserviceId : nameserviceIds) {
+      createFederatedNameNode(nnCounter++, conf, numDataNodes, manageNameDfsDirs,
+          format, operation, clusterId, nameserviceId);
+    }
+  }
+  
+  private NameNode createNameNode(int nnIndex, Configuration conf,
+      int numDataNodes, boolean manageNameDfsDirs, boolean format,
+      StartupOption operation, String clusterId)
+      throws IOException {
     if (manageNameDfsDirs) {
       conf.set(DFSConfigKeys.DFS_NAMENODE_NAME_DIR_KEY,
           fileAsURI(new File(base_dir, "name" + (2*nnIndex + 1)))+","+
@@ -494,11 +543,6 @@ public class MiniDFSCluster {
           fileAsURI(new File(base_dir, "namesecondary" + (2*nnIndex + 2))));
     }
     
-    int replication = conf.getInt("dfs.replication", 3);
-    conf.setInt("dfs.replication", Math.min(replication, numDataNodes));
-    conf.setInt(DFSConfigKeys.DFS_NAMENODE_SAFEMODE_EXTENSION_KEY, 0);
-    conf.setInt("dfs.namenode.decommission.interval", 3); // 3 second
-    
     // Format and clean out DataNode directories
     if (format) {
       GenericTestUtils.formatNamenode(conf);
@@ -512,12 +556,27 @@ public class MiniDFSCluster {
                      operation == StartupOption.FORMAT ||
                      operation == StartupOption.REGULAR) ?
       new String[] {} : new String[] {operation.getName()};
-    conf.setClass(DFSConfigKeys.NET_TOPOLOGY_NODE_SWITCH_MAPPING_IMPL_KEY, 
-                   StaticMapping.class, DNSToSwitchMapping.class);
-    NameNode nn = NameNode.createNameNode(args, conf);
-    nameNodes[nnIndex] = new NameNodeInfo(nn, conf);
+    return NameNode.createNameNode(args, conf);
   }
   
+  private void createFederatedNameNode(int nnIndex, Configuration conf,
+      int numDataNodes, boolean manageNameDfsDirs, boolean format,
+      StartupOption operation, String clusterId, String nameserviceId)
+      throws IOException {
+    conf.set(DFSConfigKeys.DFS_FEDERATION_NAMESERVICE_ID, nameserviceId);
+    NameNode nn = createNameNode(nnIndex, conf, numDataNodes, manageNameDfsDirs,
+        format, operation, clusterId);
+    conf.set(DFSUtil.getNameServiceIdKey(
+        DFSConfigKeys.DFS_NAMENODE_RPC_ADDRESS_KEY, nameserviceId), NameNode
+        .getHostPortString(nn.getNameNodeAddress()));
+    conf.set(DFSUtil.getNameServiceIdKey(
+        DFSConfigKeys.DFS_NAMENODE_HTTP_ADDRESS_KEY, nameserviceId), NameNode
+        .getHostPortString(nn.getHttpAddress()));
+    DFSUtil.setGenericConf(conf, nameserviceId, 
+        DFSConfigKeys.DFS_NAMENODE_HTTP_ADDRESS_KEY);
+    nameNodes[nnIndex] = new NameNodeInfo(nn, new Configuration(conf));
+  }
+
   private void setRpcEngine(Configuration conf, Class<?> protocol, Class<?> engine) {
     conf.setClass("rpc.engine."+protocol.getName(), engine, Object.class);
   }
@@ -608,7 +667,6 @@ public class MiniDFSCluster {
     // If minicluster's name node is null assume that the conf has been
     // set with the right address:port of the name node.
     //
-    setNamenodeList(conf);
     
     if (racks != null && numDataNodes > racks.length ) {
       throw new IllegalArgumentException( "The length of racks [" + racks.length
@@ -1508,23 +1566,6 @@ public class MiniDFSCluster {
   }
   
   /**
-   * Add comma separated list of namenode addresses to the config file
-   */
-  private void setNamenodeList(Configuration conf) {
-    String list = "";
-    for (int i = 0 ; i < nameNodes.length; i++) {
-      if (list.length() > 0) {
-        list += ",";
-      }
-      if (nameNodes[i].nameNode == null) {
-        continue;
-      }
-      list += getURI(i);
-    }
-    conf.set(DFSConfigKeys.DFS_FEDERATION_NAMENODES, list);
-  }
-  
-  /**
    * Throw an exception if the MiniDFSCluster is not started with a single
    * namenode
    */
@@ -1533,10 +1574,11 @@ public class MiniDFSCluster {
       throw new IllegalArgumentException("Namenode index is needed");
     }
   }
-  
+
   /**
-   * Add a namenode to cluster and start it. Configuration of datanodes
-   * in the cluster is refreshed to register with the new namenode.
+   * Add a namenode to a federated cluster and start it. Configuration of
+   * datanodes in the cluster is refreshed to register with the new namenode.
+   * 
    * @return newly started namenode
    */
   public NameNode addNameNode(Configuration conf, int namenodePort)
@@ -1546,14 +1588,21 @@ public class MiniDFSCluster {
     NameNodeInfo[] newlist = new NameNodeInfo[numNameNodes];
     System.arraycopy(nameNodes, 0, newlist, 0, nameNodes.length);
     nameNodes = newlist;
-    createNameNode(nnIndex, conf, numDataNodes, true, namenodePort, true, null,
-        null);
+    String nameserviceId = NAMESERVICE_ID_PREFIX + nnIndex;
+    
+    String nameserviceIds = conf.get(DFSConfigKeys.DFS_FEDERATION_NAMESERVICES);
+    nameserviceIds += "," + nameserviceId;
+    conf.set(DFSConfigKeys.DFS_FEDERATION_NAMESERVICES, nameserviceIds);
+    
+    initFederatedNamenodeAddress(conf, nameserviceId, namenodePort);
+    createFederatedNameNode(nnIndex, conf, numDataNodes, true, true, null,
+        null, nameserviceId);
 
     // Refresh datanodes with the newly started namenode
     for (DataNodeProperties dn : dataNodes) {
       DataNode datanode = dn.datanode;
       Configuration dnConf = datanode.getConf();
-      setNamenodeList(dnConf);
+      dnConf.set(DFSConfigKeys.DFS_FEDERATION_NAMESERVICES, nameserviceIds);
       datanode.refreshNamenodes(dnConf);
     }
 

Modified: hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/TestDFSUtil.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/TestDFSUtil.java?rev=1076460&r1=1076459&r2=1076460&view=diff
==============================================================================
--- hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/TestDFSUtil.java (original)
+++ hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/TestDFSUtil.java Wed Mar  2 23:44:05 2011
@@ -20,39 +20,99 @@ package org.apache.hadoop.hdfs;
 
 import static org.junit.Assert.assertEquals;
 
-import java.net.URI;
+import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 
+import org.apache.hadoop.conf.Configuration;
 import org.junit.Test;
 
 
 public class TestDFSUtil {
   
+  /** 
+   * Test for
+   * {@link DFSUtil#getNameServiceIds(Configuration)}
+   * {@link DFSUtil#getNameServiceId(Configuration)}
+   * {@link DFSUtil#getNNServiceRpcAddresses(Configuration)}
+   */
   @Test
-  public void testMultipleNamenodes() {
+  public void testMultipleNamenodes() throws IOException {
     HdfsConfiguration conf = new HdfsConfiguration();
-    conf.set(DFSConfigKeys.DFS_FEDERATION_NAMENODES, "nn1,nn2");
-    conf.set("sn1-primary.namenode", "nn1");
-    conf.set("sn2-primary.namenode", "nn2");
-    List<URI> namenodes = DFSUtil.getNamenodeList(conf);
-    Iterator<URI> it = namenodes.iterator();
+    conf.set(DFSConfigKeys.DFS_FEDERATION_NAMESERVICES, "nn1,nn2");
+    
+    // Test - The configured nameserviceIds are returned
+    Collection<String> nameserviceIds = DFSUtil.getNameServiceIds(conf);
+    Iterator<String> it = nameserviceIds.iterator();
+    assertEquals(2, nameserviceIds.size());
     assertEquals("nn1", it.next().toString());
     assertEquals("nn2", it.next().toString());
-    assertEquals("nn1", DFSUtil.getPrimaryNamenode(conf, "sn1").toString());
-    assertEquals("nn2", DFSUtil.getPrimaryNamenode(conf, "sn2").toString());
+    
+    // Tests default nameserviceId is returned
+    conf.set(DFSConfigKeys.DFS_FEDERATION_NAMESERVICE_ID, "nn1");
+    assertEquals("nn1", DFSUtil.getNameServiceId(conf));
+    
+    // Test - configured list of namenodes are returned
+    conf.set(DFSUtil.getNameServiceIdKey(
+        DFSConfigKeys.DFS_NAMENODE_RPC_ADDRESS_KEY, "nn1"), "localhost:9000");
+    conf.set(DFSUtil.getNameServiceIdKey(
+        DFSConfigKeys.DFS_NAMENODE_RPC_ADDRESS_KEY, "nn2"), "localhost:9001");
+    
+    Collection<InetSocketAddress> nnAddresses = 
+      DFSUtil.getNNServiceRpcAddresses(conf);
+    assertEquals(2, nnAddresses.size());
+    Iterator<InetSocketAddress> iterator = nnAddresses.iterator();
+    assertEquals(2, nameserviceIds.size());
+    InetSocketAddress addr = iterator.next();
+    assertEquals("localhost", addr.getHostName());
+    assertEquals(9000, addr.getPort());
+    addr = iterator.next();
+    assertEquals("localhost", addr.getHostName());
+    assertEquals(9001, addr.getPort());
   }
   
+  /** Tests to ensure default namenode is used as fallback */
   @Test
-  public void testDefaultNamenode() {
+  public void testDefaultNamenode() throws IOException {
     HdfsConfiguration conf = new HdfsConfiguration();
-    final String hdfs_default = "hdfs://x.y.z:9999/";
+    final String hdfs_default = "hdfs://localhost:9999/";
     conf.set(DFSConfigKeys.FS_DEFAULT_NAME_KEY, hdfs_default);
-    List<URI> namenodes = DFSUtil.getNamenodeList(conf);
-    assertEquals(1, namenodes.size());
-    Iterator<URI> it = namenodes.iterator();
-    assertEquals(hdfs_default, it.next().toString());
-    assertEquals(hdfs_default, DFSUtil.getPrimaryNamenode(conf, "sn1")
-        .toString());
+    // If DFSConfigKeys.DFS_FEDERATION_NAMESERVICES is not set, verify that 
+    // default namenode address is returned.
+    List<InetSocketAddress> addrList = DFSUtil.getNNServiceRpcAddresses(conf);
+    assertEquals(1, addrList.size());
+    assertEquals(9999, addrList.get(0).getPort());
+  }
+  
+  /**
+   * Test to ensure nameservice specific keys in the configuration are
+   * copied to generic keys when the namenode starts.
+   */
+  @Test
+  public void testConfModification() throws IOException {
+    final HdfsConfiguration conf = new HdfsConfiguration();
+    conf.set(DFSConfigKeys.DFS_FEDERATION_NAMESERVICES, "nn1");
+    conf.set(DFSConfigKeys.DFS_FEDERATION_NAMESERVICE_ID, "nn1");
+    final String nameserviceId = DFSUtil.getNameServiceId(conf);
+    
+    // Set the nameservice specific keys with nameserviceId
+    conf.set(DFSUtil.getNameServiceIdKey(
+        DFSConfigKeys.DFS_NAMENODE_USER_NAME_KEY, nameserviceId), "namenode1");
+    conf.set(DFSUtil.getNameServiceIdKey(
+            DFSConfigKeys.DFS_NAMENODE_RPC_ADDRESS_KEY, nameserviceId),
+            "localhost:9090");
+    
+    MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf)
+        .nameNodePort(9090).build();
+    
+    // Make sure the specific keys are copied to generic keys post startup
+    final Configuration nnConf = cluster.getConfiguration(0);
+    assertEquals("namenode1", nnConf
+        .get(DFSConfigKeys.DFS_NAMENODE_USER_NAME_KEY));
+    assertEquals("hdfs://localhost:9090", nnConf
+        .get(DFSConfigKeys.FS_DEFAULT_NAME_KEY));
+    cluster.shutdown();
   }
 }

Modified: hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/server/datanode/TestMulitipleNNDataBlockScanner.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/server/datanode/TestMulitipleNNDataBlockScanner.java?rev=1076460&r1=1076459&r2=1076460&view=diff
==============================================================================
--- hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/server/datanode/TestMulitipleNNDataBlockScanner.java (original)
+++ hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/server/datanode/TestMulitipleNNDataBlockScanner.java Wed Mar  2 23:44:05 2011
@@ -27,6 +27,7 @@ import org.apache.hadoop.fs.FileSystem;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.hdfs.DFSConfigKeys;
 import org.apache.hadoop.hdfs.DFSTestUtil;
+import org.apache.hadoop.hdfs.DFSUtil;
 import org.apache.hadoop.hdfs.HdfsConfiguration;
 import org.apache.hadoop.hdfs.MiniDFSCluster;
 import org.junit.Test;
@@ -90,17 +91,17 @@ public class TestMulitipleNNDataBlockSca
       InterruptedException {
     setUp(9933);
     try {
-      Configuration conf = new HdfsConfiguration();
+      Configuration conf = new HdfsConfiguration(cluster.getConfiguration(0));
       StringBuilder namenodesBuilder = new StringBuilder();
 
       String bpidToShutdown = cluster.getNamesystem(2).getBlockPoolId();
       for (int i = 0; i < 2; i++) {
-        FileSystem fs = cluster.getFileSystem(i);
-        namenodesBuilder.append(fs.getUri());
+        String nsId = DFSUtil.getNameServiceId(cluster.getConfiguration(i));
+        namenodesBuilder.append(nsId);
         namenodesBuilder.append(",");
       }
 
-      conf.set(DFSConfigKeys.DFS_FEDERATION_NAMENODES, namenodesBuilder
+      conf.set(DFSConfigKeys.DFS_FEDERATION_NAMESERVICES, namenodesBuilder
           .toString());
       DataNode dn = cluster.getDataNodes().get(0);
       dn.refreshNamenodes(conf);
@@ -115,8 +116,9 @@ public class TestMulitipleNNDataBlockSca
         LOG.info(ex.getMessage());
       }
 
-      namenodesBuilder.append(cluster.getFileSystem(2).getUri());
-      conf.set(DFSConfigKeys.DFS_FEDERATION_NAMENODES, namenodesBuilder
+      namenodesBuilder.append(DFSUtil.getNameServiceId(cluster
+          .getConfiguration(2)));
+      conf.set(DFSConfigKeys.DFS_FEDERATION_NAMESERVICES, namenodesBuilder
           .toString());
       dn.refreshNamenodes(conf);
 

Modified: hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestCheckpoint.java
URL: http://svn.apache.org/viewvc/hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestCheckpoint.java?rev=1076460&r1=1076459&r2=1076460&view=diff
==============================================================================
--- hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestCheckpoint.java (original)
+++ hadoop/hdfs/branches/HDFS-1052/src/test/hdfs/org/apache/hadoop/hdfs/server/namenode/TestCheckpoint.java Wed Mar  2 23:44:05 2011
@@ -19,6 +19,7 @@ package org.apache.hadoop.hdfs.server.na
 
 import junit.framework.TestCase;
 import java.io.*;
+import java.net.InetSocketAddress;
 import java.net.URI;
 import java.util.Collection;
 import java.util.List;
@@ -26,6 +27,7 @@ import java.util.Iterator;
 import java.util.Random;
 
 import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hdfs.DFSUtil;
 import org.apache.hadoop.hdfs.DistributedFileSystem;
 import org.apache.hadoop.hdfs.HdfsConfiguration;
 import org.apache.hadoop.hdfs.MiniDFSCluster;
@@ -817,4 +819,55 @@ public class TestCheckpoint extends Test
     secondary.shutdown();
     cluster.shutdown();
   }
+  
+  /**
+   * Starts two namenodes and two secondary namenodes, verifies that secondary
+   * namenodes are configured correctly to talk to their respective namenodes
+   * and can do the checkpoint.
+   * 
+   * @throws IOException
+   */
+  @SuppressWarnings("deprecation")
+  public void testMultipleSecondaryNamenodes() throws IOException {
+    Configuration conf = new HdfsConfiguration();
+    String nameserviceId1 = "ns1";
+    String nameserviceId2 = "ns2";
+    conf.set(DFSConfigKeys.DFS_FEDERATION_NAMESERVICES, nameserviceId1
+        + "," + nameserviceId2);
+    MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).numNameNodes(2)
+        .nameNodePort(9928).build();
+    Configuration snConf1 = new HdfsConfiguration(cluster.getConfiguration(0));
+    Configuration snConf2 = new HdfsConfiguration(cluster.getConfiguration(1));
+    InetSocketAddress nn1RpcAddress = cluster.getNameNode(0).rpcAddress;
+    InetSocketAddress nn2RpcAddress = cluster.getNameNode(1).rpcAddress;
+    String nn1 = nn1RpcAddress.getHostName() + ":" + nn1RpcAddress.getPort();
+    String nn2 = nn2RpcAddress.getHostName() + ":" + nn2RpcAddress.getPort();
+
+    // Set the Service Rpc address to empty to make sure the node specific
+    // setting works
+    snConf1.set(DFSConfigKeys.DFS_NAMENODE_SERVICE_RPC_ADDRESS_KEY, "");
+    snConf2.set(DFSConfigKeys.DFS_NAMENODE_SERVICE_RPC_ADDRESS_KEY, "");
+
+    // Set the nameserviceIds
+    snConf1.set(DFSUtil.getNameServiceIdKey(
+        DFSConfigKeys.DFS_NAMENODE_SERVICE_RPC_ADDRESS_KEY, nameserviceId1), nn1);
+    snConf2.set(DFSUtil.getNameServiceIdKey(
+        DFSConfigKeys.DFS_NAMENODE_SERVICE_RPC_ADDRESS_KEY, nameserviceId2), nn2);
+
+    SecondaryNameNode secondary1 = startSecondaryNameNode(snConf1);
+    SecondaryNameNode secondary2 = startSecondaryNameNode(snConf2);
+
+    // make sure the two secondary namenodes are talking to correct namenodes.
+    assertEquals(secondary1.getNameNodeAddress().getPort(), nn1RpcAddress.getPort());
+    assertEquals(secondary2.getNameNodeAddress().getPort(), nn2RpcAddress.getPort());
+    assertTrue(secondary1.getNameNodeAddress().getPort() != secondary2
+        .getNameNodeAddress().getPort());
+
+    // both should checkpoint.
+    secondary1.doCheckpoint();
+    secondary2.doCheckpoint();
+    secondary1.shutdown();
+    secondary2.shutdown();
+    cluster.shutdown();
+  }
 }