瀏覽代碼

HADOOP-3461. Remove hdfs.StringBytesWritable. (szetszwo)

git-svn-id: https://svn.apache.org/repos/asf/hadoop/core/trunk@709609 13f79535-47bb-0310-9956-ffa450edef68
Tsz-wo Sze 16 年之前
父節點
當前提交
3745759eab

+ 2 - 0
CHANGES.txt

@@ -53,6 +53,8 @@ Trunk (unreleased changes)
 
     HADOOP-4454. Allow # comments in slaves file. (Rama Ramasamy via omalley)
 
+    HADOOP-3461. Remove hdfs.StringBytesWritable. (szetszwo)
+
   OPTIMIZATIONS
 
   BUG FIXES

+ 4 - 7
src/hdfs/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java

@@ -1043,7 +1043,7 @@ public class FSNamesystem implements FSConstants, FSNamesystemMBean {
         // If the file is under construction , then it must be in our
         // leases. Find the appropriate lease record.
         //
-        Lease lease = leaseManager.getLease(new StringBytesWritable(holder));
+        Lease lease = leaseManager.getLease(holder);
         //
         // We found the lease for this file. And surprisingly the original
         // holder is trying to recreate this file. This should never occur.
@@ -1327,7 +1327,7 @@ public class FSNamesystem implements FSConstants, FSNamesystemMBean {
                                                      throws IOException {
 
     if (file == null || file.isDirectory()) {
-      Lease lease = leaseManager.getLease(new StringBytesWritable(holder));
+      Lease lease = leaseManager.getLease(holder);
       throw new LeaseExpiredException("No lease on " + src +
                                       " File does not exist. " +
                                       (lease != null ? lease.toString() :
@@ -1335,7 +1335,7 @@ public class FSNamesystem implements FSConstants, FSNamesystemMBean {
                                        " does not have any open files."));
     }
     if (!file.isUnderConstruction()) {
-      Lease lease = leaseManager.getLease(new StringBytesWritable(holder));
+      Lease lease = leaseManager.getLease(holder);
       throw new LeaseExpiredException("No lease on " + src + 
                                       " File is not open for writing. " +
                                       (lease != null ? lease.toString() :
@@ -4553,10 +4553,7 @@ public class FSNamesystem implements FSConstants, FSNamesystemMBean {
       out.writeInt(leaseManager.countPath()); // write the size
 
       for (Lease lease : leaseManager.getSortedLeases()) {
-        Collection<StringBytesWritable> files = lease.getPaths();
-        for (Iterator<StringBytesWritable> i = files.iterator(); i.hasNext();){
-          String path = i.next().getString();
-
+        for(String path : lease.getPaths()) {
           // verify that path exists in namespace
           INode node = dir.getFileINode(path);
           if (node == null) {

+ 13 - 17
src/hdfs/org/apache/hadoop/hdfs/server/namenode/INodeFileUnderConstruction.java

@@ -25,28 +25,25 @@ import org.apache.hadoop.hdfs.server.namenode.BlocksMap.BlockInfo;
 
 
 public class INodeFileUnderConstruction extends INodeFile {
-  StringBytesWritable clientName = null;         // lease holder
-  StringBytesWritable clientMachine = null;
-  DatanodeDescriptor clientNode = null; // if client is a cluster node too.
+  final String clientName;         // lease holder
+  private final String clientMachine;
+  private final DatanodeDescriptor clientNode; // if client is a cluster node too.
 
   private int primaryNodeIndex = -1; //the node working on lease recovery
   private DatanodeDescriptor[] targets = null;   //locations for last block
   private long lastRecoveryTime = 0;
   
-  INodeFileUnderConstruction() {}
-
   INodeFileUnderConstruction(PermissionStatus permissions,
                              short replication,
                              long preferredBlockSize,
                              long modTime,
                              String clientName,
                              String clientMachine,
-                             DatanodeDescriptor clientNode) 
-                             throws IOException {
+                             DatanodeDescriptor clientNode) {
     super(permissions.applyUMask(UMASK), 0, replication, modTime, modTime,
         preferredBlockSize);
-    this.clientName = new StringBytesWritable(clientName);
-    this.clientMachine = new StringBytesWritable(clientMachine);
+    this.clientName = clientName;
+    this.clientMachine = clientMachine;
     this.clientNode = clientNode;
   }
 
@@ -58,22 +55,21 @@ public class INodeFileUnderConstruction extends INodeFile {
                              PermissionStatus perm,
                              String clientName,
                              String clientMachine,
-                             DatanodeDescriptor clientNode)
-                             throws IOException {
+                             DatanodeDescriptor clientNode) {
     super(perm, blocks, blockReplication, modificationTime, modificationTime,
           preferredBlockSize);
     setLocalName(name);
-    this.clientName = new StringBytesWritable(clientName);
-    this.clientMachine = new StringBytesWritable(clientMachine);
+    this.clientName = clientName;
+    this.clientMachine = clientMachine;
     this.clientNode = clientNode;
   }
 
-  String getClientName() throws IOException {
-    return clientName.getString();
+  String getClientName() {
+    return clientName;
   }
 
-  String getClientMachine() throws IOException {
-    return clientMachine.getString();
+  String getClientMachine() {
+    return clientMachine;
   }
 
   DatanodeDescriptor getClientNode() {

+ 22 - 26
src/hdfs/org/apache/hadoop/hdfs/server/namenode/LeaseManager.java

@@ -20,7 +20,6 @@ package org.apache.hadoop.hdfs.server.namenode;
 import java.io.IOException;
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.SortedMap;
@@ -67,7 +66,7 @@ public class LeaseManager {
   // Used for handling lock-leases
   // Mapping: leaseHolder -> Lease
   //
-  private SortedMap<StringBytesWritable, Lease> leases = new TreeMap<StringBytesWritable, Lease>();
+  private SortedMap<String, Lease> leases = new TreeMap<String, Lease>();
   // Set of: Lease
   private SortedSet<Lease> sortedLeases = new TreeSet<Lease>();
 
@@ -79,7 +78,7 @@ public class LeaseManager {
 
   LeaseManager(FSNamesystem fsnamesystem) {this.fsnamesystem = fsnamesystem;}
 
-  Lease getLease(StringBytesWritable holder) throws IOException {
+  Lease getLease(String holder) {
     return leases.get(holder);
   }
   
@@ -103,8 +102,7 @@ public class LeaseManager {
   /**
    * Adds (or re-adds) the lease for the specified file.
    */
-  synchronized void addLease(StringBytesWritable holder, String src
-      ) throws IOException {
+  synchronized void addLease(String holder, String src) {
     Lease lease = getLease(holder);
     if (lease == null) {
       lease = new Lease(holder);
@@ -114,13 +112,13 @@ public class LeaseManager {
       renewLease(lease);
     }
     sortedLeasesByPath.put(src, lease);
-    lease.paths.add(new StringBytesWritable(src));
+    lease.paths.add(src);
   }
 
   /**
    * Remove the specified lease and src.
    */
-  synchronized void removeLease(Lease lease, String src) throws IOException {
+  synchronized void removeLease(Lease lease, String src) {
     sortedLeasesByPath.remove(src);
     if (!lease.removePath(src)) {
       LOG.error(src + " not found in lease.paths (=" + lease.paths + ")");
@@ -137,8 +135,7 @@ public class LeaseManager {
   /**
    * Remove the lease for the specified holder and src
    */
-  synchronized void removeLease(StringBytesWritable holder, String src
-      ) throws IOException {
+  synchronized void removeLease(String holder, String src) {
     Lease lease = getLease(holder);
     if (lease != null) {
       removeLease(lease, src);
@@ -164,8 +161,8 @@ public class LeaseManager {
   /**
    * Renew the lease(s) held by the given client
    */
-  synchronized void renewLease(String holder) throws IOException {
-    renewLease(getLease(new StringBytesWritable(holder)));
+  synchronized void renewLease(String holder) {
+    renewLease(getLease(holder));
   }
   synchronized void renewLease(Lease lease) {
     if (lease != null) {
@@ -183,12 +180,12 @@ public class LeaseManager {
    * expire, all the corresponding locks can be released.
    *************************************************************/
   class Lease implements Comparable<Lease> {
-    private StringBytesWritable holder;
+    private final String holder;
     private long lastUpdate;
-    private Collection<StringBytesWritable> paths = new TreeSet<StringBytesWritable>();
+    private final Collection<String> paths = new TreeSet<String>();
   
     /** Only LeaseManager object can create a lease */
-    private Lease(StringBytesWritable holder) throws IOException {
+    private Lease(String holder) {
       this.holder = holder;
       renew();
     }
@@ -211,8 +208,7 @@ public class LeaseManager {
      * @return the path associated with the pendingFile and null if not found.
      */
     private String findPath(INodeFileUnderConstruction pendingFile) {
-      for(Iterator<StringBytesWritable> i = paths.iterator(); i.hasNext(); ) {
-        String src = i.next().toString();
+      for(String src : paths) {
         if (fsnamesystem.dir.getFileINode(src) == pendingFile) {
           return src;
         }
@@ -223,8 +219,8 @@ public class LeaseManager {
     /** Does this lease contain any path? */
     boolean hasPath() {return !paths.isEmpty();}
 
-    boolean removePath(String src) throws IOException {
-      return paths.remove(new StringBytesWritable(src));
+    boolean removePath(String src) {
+      return paths.remove(src);
     }
 
     /** {@inheritDoc} */
@@ -266,18 +262,18 @@ public class LeaseManager {
       return holder.hashCode();
     }
     
-    Collection<StringBytesWritable> getPaths() {
+    Collection<String> getPaths() {
       return paths;
     }
     
-    void replacePath(String oldpath, String newpath) throws IOException {
-      paths.remove(new StringBytesWritable(oldpath));
-      paths.add(new StringBytesWritable(newpath));
+    void replacePath(String oldpath, String newpath) {
+      paths.remove(oldpath);
+      paths.add(newpath);
     }
   }
 
   synchronized void changeLease(String src, String dst,
-      String overwrite, String replaceBy) throws IOException {
+      String overwrite, String replaceBy) {
     if (LOG.isDebugEnabled()) {
       LOG.debug(getClass().getSimpleName() + ".changelease: " +
                " src=" + src + ", dest=" + dst + 
@@ -298,7 +294,7 @@ public class LeaseManager {
     }
   }
 
-  synchronized void removeLeaseWithPrefixPath(String prefix) throws IOException {
+  synchronized void removeLeaseWithPrefixPath(String prefix) {
     for(Map.Entry<String, Lease> entry : findLeaseWithPrefixPath(prefix, sortedLeasesByPath)) {
       if (LOG.isDebugEnabled()) {
         LOG.debug(LeaseManager.class.getSimpleName()
@@ -352,8 +348,8 @@ public class LeaseManager {
                 if (top.expiredHardLimit()) {
                   LOG.info("Lease Monitor: Removing lease " + top
                       + ", sortedLeases.size()=: " + sortedLeases.size());
-                  for(StringBytesWritable s : top.paths) {
-                    fsnamesystem.internalReleaseLease(top, s.getString());
+                  for(String s : top.paths) {
+                    fsnamesystem.internalReleaseLease(top, s);
                   }
                 } else {
                   break;

+ 0 - 65
src/hdfs/org/apache/hadoop/hdfs/server/namenode/StringBytesWritable.java

@@ -1,65 +0,0 @@
-/**
- * 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.hdfs.server.namenode;
-
-import java.io.IOException;
-import org.apache.hadoop.io.BytesWritable;
-import org.apache.hadoop.io.WritableComparator;
-
-class StringBytesWritable extends BytesWritable {
-
-  StringBytesWritable() {
-    super();
-  }
-
-  /*StringBytesWritable(byte[] bytes) {
-    super(bytes);
-  }*/
-
-  /**
-   * Create a BytesWritable by extracting bytes from the input string.
-   */
-  StringBytesWritable(String str) throws IOException {
-    super(str.getBytes("UTF8"));
-  }
-
-  /**
-   * Convert BytesWritable to a String.
-   */
-  String getString() throws IOException {
-    return new String(getBytes(),"UTF8");
-  }
-
-  /** {@inheritDoc} */
-  public String toString() {
-    try {
-      return getString();
-    } catch (IOException e) {
-      throw (RuntimeException)new RuntimeException().initCause(e);
-    }
-  }
-
-  /**
-   * Compare to a String.
-   */
-  boolean equals(String str) throws IOException {
-    return WritableComparator.compareBytes(getBytes(), 0, getLength(), 
-                                   str.getBytes("UTF8"), 0, str.length()) == 0;
-  }
-}