mx4j/src/core/mx4j/monitor MX4JCounterMonitor.java,1.4,1.5

Jeremy Boynes <[email protected]> Tue, 15 Feb 2005 22:31:42 +0000
Newsgroups gmane.comp.java.mx4j.cvs
Message-ID <[email protected]>
Update of /cvsroot/mx4j/mx4j/src/core/mx4j/monitor
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8223/src/core/mx4j/monitor

Modified Files:
	MX4JCounterMonitor.java 
Log Message:
Fix for [ 1123524 ] CounterMonitor behaves strangely on rollover
Big change to CounterMonitor to make it mimic the RI

Index: MX4JCounterMonitor.java
===================================================================
RCS file: /cvsroot/mx4j/mx4j/src/core/mx4j/monitor/MX4JCounterMonitor.java,v
retrieving revision 1.4
retrieving revision 1.5
diff -C2 -d -r1.4 -r1.5
*** MX4JCounterMonitor.java	13 Feb 2005 17:17:56 -0000	1.4
--- MX4JCounterMonitor.java	15 Feb 2005 22:31:36 -0000	1.5
***************
*** 165,170 ****
           modulus = getModulus();
        }
!       Number gauge = (Number)value;
!       Class gaugeClass = gauge.getClass();
        if (threshold != ZERO && threshold.getClass() != gaugeClass)
        {
--- 165,170 ----
           modulus = getModulus();
        }
!       Number counter = (Number)value;
!       Class gaugeClass = counter.getClass();
        if (threshold != ZERO && threshold.getClass() != gaugeClass)
        {
***************
*** 184,270 ****
  
        Logger logger = getLogger();
        // Contains previous gauge and threshold
        CounterMonitorInfo info = (CounterMonitorInfo)monitorInfo;
-       if (logger.isEnabledFor(Logger.DEBUG))
-       {
-          logger.debug("Computing gauge, previous values are: " + info);
-          logger.debug("Current values are: threshold=" + threshold + ", offset=" + offset + ", modulus=" + modulus);
-       }
  
!       boolean updateThreshold = false;
        if (getDifferenceMode())
        {
!          Number diffGauge = sub(gauge, info.getGauge());
!          if (compare(diffGauge, ZERO) < 0) diffGauge = sum(diffGauge, getModulus());
!          if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("CounterMonitor in difference mode, difference gauge=" + diffGauge);
!          updateThreshold = compareAndSendNotification(diffGauge, threshold, info, name, attribute);
        }
        else
        {
!          if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("CounterMonitor in absolute mode, gauge=" + gauge);
!          updateThreshold = compareAndSendNotification(gauge, threshold, info, name, attribute);
!       }
! 
!       if (updateThreshold)
!       {
!          if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Updating threshold, old value = " + threshold);
! 
!          // Offset the threshold until exceeds the gauge
!          if (compare(offset, ZERO) != 0)
!             while (compare(threshold, gauge) <= 0) threshold = sum(threshold, offset);
! 
!          if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Threshold has been offset, new value = " + threshold);
! 
!          // Check for rollover
!          if (getModulus() != ZERO && compare(threshold, getModulus()) > 0) threshold = getInitThreshold();
  
!          if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Threshold has been rolled over, new value = " + threshold);
        }
!       else
        {
!          if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("No need to update the threshold, value remains = " + threshold);
        }
  
!       CounterMonitorInfo newInfo = (CounterMonitorInfo)createMonitorInfo();
!       newInfo.setThresholdNotified(info.isThresholdNotified());
!       newInfo.setGauge(gauge);
!       newInfo.setTimestamp(System.currentTimeMillis());
!       newInfo.setThreshold(threshold);
!       putMonitorInfo(name, newInfo);
!    }
  
-    private boolean compareAndSendNotification(Number gauge, Number threshold, CounterMonitorInfo info, ObjectName name, String attribute)
-    {
-       Logger logger = getLogger();
  
!       if (compare(gauge, threshold) >= 0)
        {
!          if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Gauge above threshold: gauge=" + gauge + ", threshold=" + threshold);
           if (getNotify())
           {
              if (info.isThresholdNotified())
              {
!                if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Threshold exceeded already notified: " + gauge);
              }
              else
              {
                 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Sending threshold exceeded notification");
!                info.setThresholdNotified(true);
!                sendNotification(MonitorNotification.THRESHOLD_VALUE_EXCEEDED, "Threshold " + threshold + " exceeded: " + gauge, name, attribute, gauge, threshold);
              }
           }
           else
           {
!             info.setThresholdNotified(false);
!             if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("CounterMonitor is configured in non-notification mode");
           }
-          return true;
        }
        else
        {
!          info.setThresholdNotified(false);
!          if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Gauge below threshold: gauge=" + gauge + ", threshold=" + threshold);
!          return false;
        }
     }
  
--- 184,291 ----
  
        Logger logger = getLogger();
+ 
        // Contains previous gauge and threshold
        CounterMonitorInfo info = (CounterMonitorInfo)monitorInfo;
  
!       // see if the counter rolled over (the value went down)
!       Number lastCounter = info.getCounter();
!       boolean rolledOver = (lastCounter != null) ? compare(counter, lastCounter) < 0 : false;
! 
!       // calculate V[t] using rules from spec
!       Number vt;
        if (getDifferenceMode())
        {
!          if (lastCounter == null)
!          {
!             // we had no previous sample so the value is ZERO
!             vt = ZERO;
!          }
!          else
!          {
!             vt = sub(counter, lastCounter);
!             if (rolledOver)
!             {
!                // the delta was negative so add the modulus
!                vt = sum(vt, modulus);
!             }
! 
!             // if we rolled over reset the threshold
!             if (rolledOver)
!             {
!                threshold = getInitThreshold();
!                if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Threshold has been rolled over, new value = " + threshold);
!             }
!          }
        }
        else
        {
!          vt = counter;
  
!          // if we rolled over and have a modulus that is greater than the threshold, reset it
!          if (rolledOver && compare(modulus, ZERO) > 0 && compare(threshold, modulus) > 0)
!          {
!             threshold = getInitThreshold();
!             if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Threshold has been rolled over, new value = " + threshold);
!          }
        }
! 
!       if (logger.isEnabledFor(Logger.DEBUG))
        {
!          logger.debug("Computing gauge, previous values are: " + info);
!          logger.debug("Current values are: threshold=" + threshold + ", offset=" + offset + ", modulus=" + modulus);
!          logger.debug("V[t] = " + vt + ", rolledOver = " + rolledOver);
        }
  
!       info.setGauge(vt);
  
  
!       boolean notified;
!       if (compare(vt, threshold) >= 0)
        {
!          if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Threshold exceeded: V[t]=" + vt + ", threshold=" + threshold);
! 
!          // send any notification that is needed
           if (getNotify())
           {
              if (info.isThresholdNotified())
              {
!                if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Threshold exceeded already notified");
              }
              else
              {
                 if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Sending threshold exceeded notification");
!                sendNotification(MonitorNotification.THRESHOLD_VALUE_EXCEEDED, "Threshold " + threshold + " exceeded: " + vt, name, attribute, counter, threshold);
              }
+             notified = true;
           }
           else
           {
!             notified = false;
!          }
! 
!          // adjust the threshold upward
!          if (compare(offset, ZERO) > 0)
!          {
!             do
!             {
!                threshold = sum(threshold, offset);
!             } while (compare(vt, threshold) >= 0);
!             if (logger.isEnabledFor(Logger.DEBUG)) logger.debug("Threshold has been offset, new value = " + threshold);
! 
           }
        }
        else
        {
!          // cancel any notification status
!          notified = false;
        }
+ 
+       CounterMonitorInfo newInfo = (CounterMonitorInfo)createMonitorInfo();
+       newInfo.setThresholdNotified(notified);
+       newInfo.setCounter(counter);
+       newInfo.setGauge(vt);
+       newInfo.setTimestamp(System.currentTimeMillis());
+       newInfo.setThreshold(threshold);
+       putMonitorInfo(name, newInfo);
     }
  
***************
*** 277,284 ****
--- 298,317 ----
     {
        private boolean thresholdNotified;
+       private Number counter = null;
        private Number gauge = ZERO;
        private long timestamp;
        private Number threshold = ZERO;
  
+       public void setThreshold(Number threshold)
+       {
+          this.threshold = threshold;
+       }
+ 
+       public Number getThreshold()
+       {
+          if (threshold == ZERO) return getInitThreshold();
+          return threshold;
+       }
+ 
        public void setThresholdNotified(boolean thresholdNotified)
        {
***************
*** 286,307 ****
        }
  
!       public void setGauge(Number gauge)
        {
!          this.gauge = gauge;
        }
  
!       public void setTimestamp(long timestamp)
        {
!          this.timestamp = timestamp;
        }
  
!       public void setThreshold(Number threshold)
        {
!          this.threshold = threshold;
        }
  
!       public boolean isThresholdNotified()
        {
!          return thresholdNotified;
        }
  
--- 319,340 ----
        }
  
!       public boolean isThresholdNotified()
        {
!          return thresholdNotified;
        }
  
!       public void setCounter(Number counter)
        {
!          this.counter = counter;
        }
  
!       public Number getCounter()
        {
!          return counter;
        }
  
!       public void setGauge(Number gauge)
        {
!          this.gauge = gauge;
        }
  
***************
*** 311,323 ****
        }
  
!       public long getTimestamp()
        {
!          return timestamp;
        }
  
!       public Number getThreshold()
        {
!          if (threshold == ZERO) return getInitThreshold();
!          return threshold;
        }
  
--- 344,355 ----
        }
  
!       public void setTimestamp(long timestamp)
        {
!          this.timestamp = timestamp;
        }
  
!       public long getTimestamp()
        {
!          return timestamp;
        }
  
***************
*** 327,330 ****
--- 359,363 ----
           buffer.append(", thresholdNotified=").append(isThresholdNotified());
           buffer.append(", gauge=").append(getGauge());
+          buffer.append(", counter=").append(getCounter());
           buffer.append(", threshold=").append(threshold);
           return buffer.toString();



-------------------------------------------------------
SF email is sponsored by - The IT Product Guide
Read honest & candid reviews on hundreds of IT Products from real users.
Discover which products truly live up to the hype. Start reading now.
http://ads.osdn.com/?ad_id=6595&alloc_id=14396&op=click