You are viewing a plain text version of this content. The canonical link for it is here.
Posted to common-commits@hadoop.apache.org by om...@apache.org on 2011/03/04 04:58:53 UTC

svn commit: r1077275 - /hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/io/UTF8.java

Author: omalley
Date: Fri Mar  4 03:58:53 2011
New Revision: 1077275

URL: http://svn.apache.org/viewvc?rev=1077275&view=rev
Log:
commit e8bffa8bdd60e96766a728163d6b85f5537e13d9
Author: Owen O'Malley <om...@apache.org>
Date:   Wed Mar 3 16:48:17 2010 -0800

    HADOOP-6609. Fix UTF8 to use a thread local DataOutputBuffer instead of
    a static that was causing a deadlock in RPC. (omalley)
    
    +++ b/YAHOO-CHANGES.txt
    +    HADOOP-6609. Fix UTF8 to use a thread local DataOutputBuffer instead of
    +    a static that was causing a deadlock in RPC. (omalley)
    +

Modified:
    hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/io/UTF8.java

Modified: hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/io/UTF8.java
URL: http://svn.apache.org/viewvc/hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/io/UTF8.java?rev=1077275&r1=1077274&r2=1077275&view=diff
==============================================================================
--- hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/io/UTF8.java (original)
+++ hadoop/common/branches/branch-0.20-security-patches/src/core/org/apache/hadoop/io/UTF8.java Fri Mar  4 03:58:53 2011
@@ -33,9 +33,16 @@ import org.apache.commons.logging.*;
  */
 public class UTF8 implements WritableComparable {
   private static final Log LOG= LogFactory.getLog(UTF8.class);
-  private static final DataOutputBuffer OBUF = new DataOutputBuffer();
   private static final DataInputBuffer IBUF = new DataInputBuffer();
 
+  private static final ThreadLocal<DataOutputBuffer> OBUF_FACTORY =
+    new ThreadLocal<DataOutputBuffer>(){
+    @Override
+    protected DataOutputBuffer initialValue() {
+      return new DataOutputBuffer();
+    }
+  };
+
   private static final byte[] EMPTY_BYTES = new byte[0];
 
   private byte[] bytes = EMPTY_BYTES;
@@ -81,11 +88,10 @@ public class UTF8 implements WritableCom
       bytes = new byte[length];
 
     try {                                         // avoid sync'd allocations
-      synchronized (OBUF) {
-        OBUF.reset();
-        writeChars(OBUF, string, 0, string.length());
-        System.arraycopy(OBUF.getData(), 0, bytes, 0, length);
-      }
+      DataOutputBuffer obuf = OBUF_FACTORY.get();
+      obuf.reset();
+      writeChars(obuf, string, 0, string.length());
+      System.arraycopy(obuf.getData(), 0, bytes, 0, length);
     } catch (IOException e) {
       throw new RuntimeException(e);
     }
@@ -182,11 +188,10 @@ public class UTF8 implements WritableCom
   public static byte[] getBytes(String string) {
     byte[] result = new byte[utf8Length(string)];
     try {                                         // avoid sync'd allocations
-      synchronized (OBUF) {
-        OBUF.reset();
-        writeChars(OBUF, string, 0, string.length());
-        System.arraycopy(OBUF.getData(), 0, result, 0, OBUF.getLength());
-      }
+      DataOutputBuffer obuf = OBUF_FACTORY.get();
+      obuf.reset();
+      writeChars(obuf, string, 0, string.length());
+      System.arraycopy(obuf.getData(), 0, result, 0, obuf.getLength());
     } catch (IOException e) {
       throw new RuntimeException(e);
     }
@@ -206,23 +211,22 @@ public class UTF8 implements WritableCom
 
   private static void readChars(DataInput in, StringBuffer buffer, int nBytes)
     throws IOException {
-    synchronized (OBUF) {
-      OBUF.reset();
-      OBUF.write(in, nBytes);
-      byte[] bytes = OBUF.getData();
-      int i = 0;
-      while (i < nBytes) {
-        byte b = bytes[i++];
-        if ((b & 0x80) == 0) {
-          buffer.append((char)(b & 0x7F));
-        } else if ((b & 0xE0) != 0xE0) {
-          buffer.append((char)(((b & 0x1F) << 6)
-                               | (bytes[i++] & 0x3F)));
-        } else {
-          buffer.append((char)(((b & 0x0F) << 12)
-                               | ((bytes[i++] & 0x3F) << 6)
-                               |  (bytes[i++] & 0x3F)));
-        }
+    DataOutputBuffer obuf = OBUF_FACTORY.get();
+    obuf.reset();
+    obuf.write(in, nBytes);
+    byte[] bytes = obuf.getData();
+    int i = 0;
+    while (i < nBytes) {
+      byte b = bytes[i++];
+      if ((b & 0x80) == 0) {
+        buffer.append((char)(b & 0x7F));
+      } else if ((b & 0xE0) != 0xE0) {
+        buffer.append((char)(((b & 0x1F) << 6)
+            | (bytes[i++] & 0x3F)));
+      } else {
+        buffer.append((char)(((b & 0x0F) << 12)
+            | ((bytes[i++] & 0x3F) << 6)
+            |  (bytes[i++] & 0x3F)));
       }
     }
   }