From cf18feba1c763d9b94354879db456c18514fff44 Mon Sep 17 00:00:00 2001
From: Chris Ridd <chris.ridd@forgerock.com>
Date: Thu, 15 Nov 2012 16:28:43 +0000
Subject: [PATCH] Fix OPENDJ-637 - Connection handler bytesRead and bytesWritten statistics are wrong

---
 opendj-sdk/opends/src/server/org/opends/server/protocols/ldap/LDAPStatistics.java       |   19 +++++++++++++++----
 opendj-sdk/opends/src/server/org/opends/server/protocols/ldap/LDAPClientConnection.java |   30 +++++++++++++++++++++++-------
 2 files changed, 38 insertions(+), 11 deletions(-)

diff --git a/opendj-sdk/opends/src/server/org/opends/server/protocols/ldap/LDAPClientConnection.java b/opendj-sdk/opends/src/server/org/opends/server/protocols/ldap/LDAPClientConnection.java
index 6c9dc22..1b62eb1 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/protocols/ldap/LDAPClientConnection.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/protocols/ldap/LDAPClientConnection.java
@@ -161,7 +161,12 @@
 
     public int read(ByteBuffer byteBuffer) throws IOException
     {
-      return clientChannel.read(byteBuffer);
+      int bytesRead = clientChannel.read(byteBuffer);
+      if (bytesRead > 0 && keepStats)
+      {
+        statTracker.updateBytesRead(bytesRead);
+      }
+      return bytesRead;
     }
 
     public boolean isOpen()
@@ -182,7 +187,11 @@
       try
       {
         int bytesToWrite = byteBuffer.remaining();
-        clientChannel.write(byteBuffer);
+        int bytesWritten = clientChannel.write(byteBuffer);
+        if (bytesWritten > 0 && keepStats)
+        {
+          statTracker.updateBytesWritten(bytesWritten);
+        }
         if (!byteBuffer.hasRemaining())
         {
           return bytesToWrite;
@@ -208,11 +217,16 @@
           while (byteBuffer.hasRemaining()
               && (System.currentTimeMillis() < stopTime))
           {
-            if (clientChannel.write(byteBuffer) < 0)
+            bytesWritten = clientChannel.write(byteBuffer);
+            if (bytesWritten < 0)
             {
               // The client connection has been closed.
               throw new ClosedChannelException();
             }
+            if (bytesWritten > 0 && keepStats)
+            {
+              statTracker.updateBytesWritten(bytesWritten);
+            }
           }
 
           if (byteBuffer.hasRemaining())
@@ -250,12 +264,16 @@
               SelectionKey k = iterator.next();
               if (k.isWritable())
               {
-                int bytesWritten = clientChannel.write(byteBuffer);
+                bytesWritten = clientChannel.write(byteBuffer);
                 if (bytesWritten < 0)
                 {
                   // The client connection has been closed.
                   throw new ClosedChannelException();
                 }
+                if (bytesWritten > 0 && keepStats)
+                {
+                  statTracker.updateBytesWritten(bytesWritten);
+                }
 
                 iterator.remove();
               }
@@ -960,9 +978,7 @@
 
       if (keepStats)
       {
-        // TODO hard-coded for now, flush probably needs to
-        // return how many bytes were flushed.
-        statTracker.updateMessageWritten(message, 4096);
+        statTracker.updateMessageWritten(message);
       }
     }
     catch (Exception e)
diff --git a/opendj-sdk/opends/src/server/org/opends/server/protocols/ldap/LDAPStatistics.java b/opendj-sdk/opends/src/server/org/opends/server/protocols/ldap/LDAPStatistics.java
index 28ab04d..26a3009 100644
--- a/opendj-sdk/opends/src/server/org/opends/server/protocols/ldap/LDAPStatistics.java
+++ b/opendj-sdk/opends/src/server/org/opends/server/protocols/ldap/LDAPStatistics.java
@@ -491,6 +491,20 @@
 
 
   /**
+   * Updates the appropriate set of counters to indicate that the
+   * specified number of bytes have been written to the client.
+   *
+   * @param bytesWritten
+   *          The number of bytes written to the client.
+   */
+  public void updateBytesWritten(int bytesWritten)
+  {
+     this.bytesWritten.getAndAdd(bytesWritten);
+  }
+
+
+
+  /**
    * Updates the appropriate set of counters based on the provided
    * message that has been read from the client.
    *
@@ -562,12 +576,9 @@
    *
    * @param message
    *          The message that was written to the client.
-   * @param bytesWritten
-   *          The size of the message written in bytes.
    */
-  public void updateMessageWritten(LDAPMessage message, int bytesWritten)
+  public void updateMessageWritten(LDAPMessage message)
   {
-      this.bytesWritten.getAndAdd(bytesWritten);
       messagesWritten.getAndIncrement();
 
       switch (message.getProtocolOp().getType())

--
Gitblit v1.10.0