cassandra-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jbel...@apache.org
Subject svn commit: r1151304 - /cassandra/trunk/src/java/org/apache/cassandra/db/ReadVerbHandler.java
Date Wed, 27 Jul 2011 01:20:16 GMT
Author: jbellis
Date: Wed Jul 27 01:20:15 2011
New Revision: 1151304

URL: http://svn.apache.org/viewvc?rev=1151304&view=rev
Log:
clean up ReadVerbHandler

Modified:
    cassandra/trunk/src/java/org/apache/cassandra/db/ReadVerbHandler.java

Modified: cassandra/trunk/src/java/org/apache/cassandra/db/ReadVerbHandler.java
URL: http://svn.apache.org/viewvc/cassandra/trunk/src/java/org/apache/cassandra/db/ReadVerbHandler.java?rev=1151304&r1=1151303&r2=1151304&view=diff
==============================================================================
--- cassandra/trunk/src/java/org/apache/cassandra/db/ReadVerbHandler.java (original)
+++ cassandra/trunk/src/java/org/apache/cassandra/db/ReadVerbHandler.java Wed Jul 27 01:20:15
2011
@@ -35,48 +35,38 @@ import org.apache.cassandra.utils.FBUtil
 
 public class ReadVerbHandler implements IVerbHandler
 {
-    protected static class ReadContext
-    {
-        protected ByteArrayInputStream bufIn_;
-        protected DataOutputBuffer bufOut_ = new DataOutputBuffer();
-    }
-
     private static Logger logger_ = LoggerFactory.getLogger( ReadVerbHandler.class );
-    /* We use this so that we can reuse readcontext objects */
-    private static ThreadLocal<ReadVerbHandler.ReadContext> tls_ = new InheritableThreadLocal<ReadVerbHandler.ReadContext>();
+
+    // re-use output buffers between requests
+    private static ThreadLocal<DataOutputBuffer> threadLocalOut = new ThreadLocal<DataOutputBuffer>()
+    {
+        @Override
+        protected DataOutputBuffer initialValue()
+        {
+            return new DataOutputBuffer();
+        }
+    };
 
     public void doVerb(Message message, String id)
     {
-        byte[] body = message.getMessageBody();
-        /* Obtain a Read Context from TLS */
-        ReadContext readCtx = tls_.get();
-        if ( readCtx == null )
+        if (StorageService.instance.isBootstrapMode())
         {
-            readCtx = new ReadContext();
-            tls_.set(readCtx);
+            throw new RuntimeException("Cannot service reads while bootstrapping!");
         }
-        readCtx.bufIn_ = new ByteArrayInputStream(body);
 
         try
         {
-            if (StorageService.instance.isBootstrapMode())
-            {
-                /* Don't service reads! */
-                throw new RuntimeException("Cannot service reads while bootstrapping!");
-            }
-            ReadCommand command = ReadCommand.serializer().deserialize(new DataInputStream(readCtx.bufIn_),
message.getVersion());
+            ByteArrayInputStream in = new ByteArrayInputStream(message.getMessageBody());
+            ReadCommand command = ReadCommand.serializer().deserialize(new DataInputStream(in),
message.getVersion());
             Table table = Table.open(command.table);
             Row row = command.getRow(table);
-            ReadResponse readResponse = getResponse(command, row);
-            /* serialize the ReadResponseMessage. */
-            readCtx.bufOut_.reset();
-
-            ReadResponse.serializer().serialize(readResponse, readCtx.bufOut_, message.getVersion());
-
-            byte[] bytes = new byte[readCtx.bufOut_.getLength()];
-            System.arraycopy(readCtx.bufOut_.getData(), 0, bytes, 0, bytes.length);
 
+            DataOutputBuffer out = threadLocalOut.get();
+            ReadResponse.serializer().serialize(getResponse(command, row), out, message.getVersion());
+            byte[] bytes = new byte[out.getLength()];
+            System.arraycopy(out.getData(), 0, bytes, 0, bytes.length);
             Message response = message.getReply(FBUtilities.getBroadcastAddress(), bytes,
message.getVersion());
+
             if (logger_.isDebugEnabled())
               logger_.debug(String.format("Read key %s; sending response to %s@%s",
                                           ByteBufferUtil.bytesToHex(command.key), id, message.getFrom()));



Mime
View raw message