You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sling.apache.org by cz...@apache.org on 2010/10/14 09:10:40 UTC

svn commit: r1022391 - in /sling/branches/eventing-3.0/src: main/java/org/apache/sling/event/impl/jobs/ main/java/org/apache/sling/event/impl/jobs/console/ main/java/org/apache/sling/event/jobs/ test/java/org/apache/sling/event/impl/jobs/

Author: cziegeler
Date: Thu Oct 14 07:10:40 2010
New Revision: 1022391

URL: http://svn.apache.org/viewvc?rev=1022391&view=rev
Log:
Fix statistics and implement reset of statistics

Modified:
    sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/impl/jobs/DefaultJobManager.java
    sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/impl/jobs/StatisticsImpl.java
    sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/impl/jobs/console/WebConsolePlugin.java
    sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/jobs/Statistics.java
    sling/branches/eventing-3.0/src/test/java/org/apache/sling/event/impl/jobs/JobEventHandlerTest.java
    sling/branches/eventing-3.0/src/test/java/org/apache/sling/event/impl/jobs/StatisticsImplTest.java

Modified: sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/impl/jobs/DefaultJobManager.java
URL: http://svn.apache.org/viewvc/sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/impl/jobs/DefaultJobManager.java?rev=1022391&r1=1022390&r2=1022391&view=diff
==============================================================================
--- sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/impl/jobs/DefaultJobManager.java (original)
+++ sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/impl/jobs/DefaultJobManager.java Thu Oct 14 07:10:40 2010
@@ -92,6 +92,7 @@ import org.slf4j.LoggerFactory;
             intValue=ConfigurationConstants.DEFAULT_MAX_PARALLEL)
 })
 public class DefaultJobManager
+    extends StatisticsImpl
     implements Runnable, JobManager {
 
     /** Default logger. */
@@ -115,11 +116,8 @@ public class DefaultJobManager
     /** Main configuration. */
     private InternalQueueConfiguration mainConfiguration;
 
-    /** Base statistics. */
-    private final StatisticsImpl baseStatistics = new StatisticsImpl();
-
     /** Current statistics. */
-    private StatisticsImpl currentStatistics;
+    private final StatisticsImpl baseStatistics = new StatisticsImpl();
 
     /** Last update for current statistics. */
     private long lastUpdatedStatistics;
@@ -310,13 +308,13 @@ public class DefaultJobManager
      */
     public synchronized Statistics getStatistics() {
         final long now = System.currentTimeMillis();
-        if ( this.currentStatistics == null || this.lastUpdatedStatistics + 1500 < now ) {
-            this.currentStatistics = this.baseStatistics.copy();
+        if ( this.lastUpdatedStatistics + 1500 < now ) {
+            this.copyFrom(this.baseStatistics);
             for(final AbstractJobQueue jq : this.queues.values() ) {
-                this.currentStatistics.add(jq);
+                this.add(jq);
             }
         }
-        return this.currentStatistics;
+        return this;
     }
 
     /**
@@ -619,4 +617,16 @@ public class DefaultJobManager
             this.logger.debug("Ignored exception " + e.getMessage(), e);
         }
     }
+
+    /**
+     * @see org.apache.sling.event.impl.jobs.StatisticsImpl#reset()
+     * Reset this statistics and all queues.
+     */
+    public synchronized void reset() {
+        this.baseStatistics.reset();
+        for(final AbstractJobQueue jq : this.queues.values() ) {
+            jq.reset();
+        }
+        this.lastUpdatedStatistics = 0;
+    }
 }

Modified: sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/impl/jobs/StatisticsImpl.java
URL: http://svn.apache.org/viewvc/sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/impl/jobs/StatisticsImpl.java?rev=1022391&r1=1022390&r2=1022391&view=diff
==============================================================================
--- sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/impl/jobs/StatisticsImpl.java (original)
+++ sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/impl/jobs/StatisticsImpl.java Thu Oct 14 07:10:40 2010
@@ -25,7 +25,7 @@ import org.apache.sling.event.jobs.Stati
  */
 public class StatisticsImpl implements Statistics {
 
-    private final long startTime;
+    private volatile long startTime;
 
     private volatile long activeJobs;
 
@@ -64,7 +64,7 @@ public class StatisticsImpl implements S
     /**
      * @see org.apache.sling.event.jobs.Statistics#getStartTime()
      */
-    public long getStartTime() {
+    public synchronized long getStartTime() {
         return startTime;
     }
 
@@ -177,7 +177,7 @@ public class StatisticsImpl implements S
     }
 
     /**
-     * Clear
+     * Clear all queued
      */
     public synchronized void clearQueued() {
         this.queuedJobs = 0;
@@ -196,16 +196,18 @@ public class StatisticsImpl implements S
         this.lastActivated = System.currentTimeMillis();
     }
 
+    /**
+     * Add another statistics information.
+     */
     public synchronized void add(final StatisticsImpl other) {
         synchronized ( other ) {
-            this.queuedJobs += other.queuedJobs;
-
             if ( other.lastActivated > this.lastActivated ) {
                 this.lastActivated = other.lastActivated;
             }
             if ( other.lastFinished > this.lastFinished ) {
                 this.lastFinished = other.lastFinished;
             }
+            this.queuedJobs += other.queuedJobs;
             this.waitingTime += other.waitingTime;
             this.waitingCount += other.waitingCount;
             this.averageWaitingTime = this.waitingTime / this.waitingCount;
@@ -215,23 +217,44 @@ public class StatisticsImpl implements S
             this.finishedJobs += other.finishedJobs;
             this.failedJobs += other.failedJobs;
             this.cancelledJobs += other.cancelledJobs;
+            this.activeJobs += other.activeJobs;
         }
     }
 
-    public synchronized StatisticsImpl copy() {
-        final StatisticsImpl other = new StatisticsImpl(this.startTime);
-        other.queuedJobs = this.queuedJobs;
-        other.lastActivated = this.lastActivated;
-        other.lastFinished = this.lastFinished;
-        other.averageWaitingTime = this.averageWaitingTime;
-        other.averageProcessingTime = this.averageProcessingTime;
-        other.waitingTime = this.waitingTime;
-        other.processingTime = this.processingTime;
-        other.waitingCount = this.waitingCount;
-        other.processingCount = this.processingCount;
-        other.finishedJobs = this.finishedJobs;
-        other.failedJobs = this.failedJobs;
-        other.cancelledJobs = this.cancelledJobs;
-        return other;
+    /**
+     * Create a new statistics object with exactly the same values.
+     */
+    public synchronized void copyFrom(final StatisticsImpl other) {
+        this.queuedJobs = other.queuedJobs;
+        this.lastActivated = other.lastActivated;
+        this.lastFinished = other.lastFinished;
+        this.averageWaitingTime = other.averageWaitingTime;
+        this.averageProcessingTime = other.averageProcessingTime;
+        this.waitingTime = other.waitingTime;
+        this.processingTime = other.processingTime;
+        this.waitingCount = other.waitingCount;
+        this.processingCount = other.processingCount;
+        this.finishedJobs = other.finishedJobs;
+        this.failedJobs = other.failedJobs;
+        this.cancelledJobs = other.cancelledJobs;
+        this.activeJobs = other.activeJobs;
+    }
+
+    /**
+     * @see org.apache.sling.event.jobs.Statistics#reset()
+     */
+    public synchronized void reset() {
+        this.startTime = System.currentTimeMillis();
+        this.lastActivated = -1;
+        this.lastFinished = -1;
+        this.averageWaitingTime = 0;
+        this.averageProcessingTime = 0;
+        this.waitingTime = 0;
+        this.processingTime = 0;
+        this.waitingCount = 0;
+        this.processingCount = 0;
+        this.finishedJobs = 0;
+        this.failedJobs = 0;
+        this.cancelledJobs = 0;
     }
 }

Modified: sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/impl/jobs/console/WebConsolePlugin.java
URL: http://svn.apache.org/viewvc/sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/impl/jobs/console/WebConsolePlugin.java?rev=1022391&r1=1022390&r2=1022391&view=diff
==============================================================================
--- sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/impl/jobs/console/WebConsolePlugin.java (original)
+++ sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/impl/jobs/console/WebConsolePlugin.java Thu Oct 14 07:10:40 2010
@@ -73,8 +73,10 @@ public class WebConsolePlugin extends Ht
         return text.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
     }
 
+    private static final String PAR_QUEUE = "queue";
+
     private Queue getQueue(final HttpServletRequest req) throws ServletException {
-        final String name = req.getParameter("queue");
+        final String name = req.getParameter(PAR_QUEUE);
         if ( name != null ) {
             for(final Queue q : this.jobManager.getQueues()) {
                 if ( name.equals(q.getName()) ) {
@@ -98,6 +100,13 @@ public class WebConsolePlugin extends Ht
         } else if ( "clear".equals(cmd) ) {
             final Queue q = this.getQueue(req);
             q.clear();
+        } else if ( "reset".equals(cmd) ) {
+            if ( req.getParameter(PAR_QUEUE) == null || req.getParameter(PAR_QUEUE).length() == 0 ) {
+                this.jobManager.getStatistics().reset();
+            } else {
+                final Queue q = this.getQueue(req);
+                q.getStatistics().reset();
+            }
         } else if ( "dropall".equals(cmd) ) {
             final Queue q = this.getQueue(req);
             q.removeAll();
@@ -113,10 +122,13 @@ public class WebConsolePlugin extends Ht
         final PrintWriter pw = res.getWriter();
 
         pw.println("<p class='statline ui-state-highlight'>Apache Sling Eventing</p>");
+        pw.println("<div class='ui-widget-header ui-corner-top buttonGroup'>");
+        pw.println("<span style='float: left; margin-left: 1em'>Apache Sling Eventing: Overall Statistics</span>");
+        this.printForm(pw, null, "Reset Stats", "reset");
+        pw.println("</div>");
 
         pw.println("<table class='nicetable'><tbody>");
         Statistics s = this.jobManager.getStatistics();
-        pw.println("<tr><th colspan='2'>Overall Statistics</th></tr>");
         pw.printf("<tr><td>Start Time</td><td>%s</td></tr>", formatDate(s.getStartTime()));
         pw.printf("<tr><td>Last Activated</td><td>%s</td></tr>", formatDate(s.getLastActivatedJobTime()));
         pw.printf("<tr><td>Last Finished</td><td>%s</td></tr>", formatDate(s.getLastFinishedJobTime()));
@@ -138,12 +150,13 @@ public class WebConsolePlugin extends Ht
             pw.println("<div class='ui-widget-header ui-corner-top buttonGroup'>");
             pw.printf("<span style='float: left; margin-left: 1em'>Active JobQueue: %s %s</span>", escape(q.getName()),
                     q.isSuspended() ? "(SUSPENDED)" : "");
+            this.printForm(pw, q, "Reset Stats", "reset");
             if ( q.isSuspended() ) {
                 this.printForm(pw, q, "Resume", "resume");
             } else {
                 this.printForm(pw, q, "Suspend", "suspend");
             }
-            this.printForm(pw, q, "Clear", "clear");
+            this.printForm(pw, q, "Clear Queue", "clear");
             this.printForm(pw, q, "Drop All", "dropall");
             pw.println("</div>");
             pw.println("<table class='nicetable'><tbody>");
@@ -163,7 +176,7 @@ public class WebConsolePlugin extends Ht
             pw.printf("<tr><td>Processed Jobs</td><td>%s</td><td colspan='2'>&nbsp</td></tr>", s.getNumberOfProcessedJobs());
             pw.printf("<tr><td>Average Processing Time</td><td>%s</td><td colspan='2'>&nbsp</td></tr>", formatTime(s.getAverageProcessingTime()));
             pw.printf("<tr><td>Average Waiting Time</td><td>%s</td><td colspan='2'>&nbsp</td></tr>", formatTime(s.getAverageWaitingTime()));
-            pw.printf("<tr><td>Status Info</td><td>%s</td></tr>", escape(q.getStatusInfo()));
+            pw.printf("<tr><td>Status Info</td><td colspan='3'>%s</td></tr>", escape(q.getStatusInfo()));
             pw.println("</tbody></table>");
             pw.println("<br/>");
         }
@@ -271,7 +284,7 @@ public class WebConsolePlugin extends Ht
         pw.printf("<form method='POST' name='%s'><input type='hidden' name='action' value='%s'/>"+
                 "<input type='hidden' name='queue' value='%s'/>" +
                 "<button class='ui-state-default ui-corner-all' onclick='javascript:document.forms[\"%s\"].submit();'>" +
-                "%s</button></form>", hiddenValue, hiddenValue, q.getName(), hiddenValue, buttonLabel);
+                "%s</button></form>", hiddenValue, hiddenValue, (q != null ? q.getName() : ""), hiddenValue, buttonLabel);
     }
 
     /** Configuration printer for the web console. */

Modified: sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/jobs/Statistics.java
URL: http://svn.apache.org/viewvc/sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/jobs/Statistics.java?rev=1022391&r1=1022390&r2=1022391&view=diff
==============================================================================
--- sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/jobs/Statistics.java (original)
+++ sling/branches/eventing-3.0/src/main/java/org/apache/sling/event/jobs/Statistics.java Thu Oct 14 07:10:40 2010
@@ -87,4 +87,11 @@ public interface Statistics {
      * The average processing time of a job - this only counts finished jobs.
      */
     long getAverageProcessingTime();
+
+    /**
+     * Clear all collected statistics and set the starting time to the current time.
+     * Note that not all fields are cleared, last waiting time or number of active and queued
+     * jobs is not cleared as these are currently used.
+     */
+    void reset();
 }

Modified: sling/branches/eventing-3.0/src/test/java/org/apache/sling/event/impl/jobs/JobEventHandlerTest.java
URL: http://svn.apache.org/viewvc/sling/branches/eventing-3.0/src/test/java/org/apache/sling/event/impl/jobs/JobEventHandlerTest.java?rev=1022391&r1=1022390&r2=1022391&view=diff
==============================================================================
--- sling/branches/eventing-3.0/src/test/java/org/apache/sling/event/impl/jobs/JobEventHandlerTest.java (original)
+++ sling/branches/eventing-3.0/src/test/java/org/apache/sling/event/impl/jobs/JobEventHandlerTest.java Thu Oct 14 07:10:40 2010
@@ -606,7 +606,9 @@ public class JobEventHandlerTest extends
                                 public boolean process(Event job) {
                                     try {
                                         Thread.sleep(200);
-                                    } catch (InterruptedException ie) {}
+                                    } catch (InterruptedException ie) {
+                                        // ignore
+                                    }
                                     return true;
                                 }
                             });
@@ -626,7 +628,9 @@ public class JobEventHandlerTest extends
         while ( count.get() < COUNT ) {
             try {
                 Thread.sleep(500);
-            } catch (InterruptedException ie) {}
+            } catch (InterruptedException ie) {
+                // ignore
+            }
         }
         assertEquals("Finished count", COUNT, count.get());
         assertEquals("Finished count", COUNT, this.jobManager.getStatistics().getNumberOfFinishedJobs());

Modified: sling/branches/eventing-3.0/src/test/java/org/apache/sling/event/impl/jobs/StatisticsImplTest.java
URL: http://svn.apache.org/viewvc/sling/branches/eventing-3.0/src/test/java/org/apache/sling/event/impl/jobs/StatisticsImplTest.java?rev=1022391&r1=1022390&r2=1022391&view=diff
==============================================================================
--- sling/branches/eventing-3.0/src/test/java/org/apache/sling/event/impl/jobs/StatisticsImplTest.java (original)
+++ sling/branches/eventing-3.0/src/test/java/org/apache/sling/event/impl/jobs/StatisticsImplTest.java Thu Oct 14 07:10:40 2010
@@ -190,7 +190,8 @@ public class StatisticsImplTest {
         this.testFailAndCancel();
 
         long now = System.currentTimeMillis();
-        final StatisticsImpl copy = this.stat.copy();
+        final StatisticsImpl copy = new StatisticsImpl();
+        copy.copyFrom(this.stat);
         assertTrue(copy.getStartTime() >= now);
         assertEquals(400, copy.getAverageProcessingTime());
         assertEquals(200, copy.getAverageWaitingTime());