changeset 710:67718d2bd49c

6683213: CounterMonitor's derived Gauge badly initialized Reviewed-by: emcmanus
author dfuchs
date Fri, 14 Nov 2008 17:22:10 +0100
parents dcb8d806d731
children 44941f893cea 4f985ba72055
files src/share/classes/javax/management/monitor/CounterMonitor.java src/share/classes/javax/management/monitor/GaugeMonitor.java src/share/classes/javax/management/monitor/Monitor.java test/javax/management/monitor/DerivedGaugeMonitorTest.java
diffstat 4 files changed, 277 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/src/share/classes/javax/management/monitor/CounterMonitor.java	Thu Nov 13 23:25:10 2008 -0800
+++ b/src/share/classes/javax/management/monitor/CounterMonitor.java	Fri Nov 14 17:22:10 2008 +0100
@@ -265,6 +265,7 @@
      * @return The derived gauge of the specified object.
      *
      */
+    @Override
     public synchronized Number getDerivedGauge(ObjectName object) {
         return (Number) super.getDerivedGauge(object);
     }
@@ -280,6 +281,7 @@
      * @return The derived gauge timestamp of the specified object.
      *
      */
+    @Override
     public synchronized long getDerivedGaugeTimeStamp(ObjectName object) {
         return super.getDerivedGaugeTimeStamp(object);
     }
@@ -595,6 +597,7 @@
      * name of the Java class of the notification and the notification
      * types sent by the counter monitor.
      */
+    @Override
     public MBeanNotificationInfo[] getNotificationInfo() {
         return notifsInfo;
     }
--- a/src/share/classes/javax/management/monitor/GaugeMonitor.java	Thu Nov 13 23:25:10 2008 -0800
+++ b/src/share/classes/javax/management/monitor/GaugeMonitor.java	Fri Nov 14 17:22:10 2008 +0100
@@ -258,6 +258,7 @@
      * @return The derived gauge of the specified object.
      *
      */
+    @Override
     public synchronized Number getDerivedGauge(ObjectName object) {
         return (Number) super.getDerivedGauge(object);
     }
@@ -273,6 +274,7 @@
      * @return The derived gauge timestamp of the specified object.
      *
      */
+    @Override
     public synchronized long getDerivedGaugeTimeStamp(ObjectName object) {
         return super.getDerivedGaugeTimeStamp(object);
     }
@@ -477,6 +479,7 @@
      * name of the Java class of the notification and the notification
      * types sent by the gauge monitor.
      */
+    @Override
     public MBeanNotificationInfo[] getNotificationInfo() {
         return notifsInfo;
     }
--- a/src/share/classes/javax/management/monitor/Monitor.java	Thu Nov 13 23:25:10 2008 -0800
+++ b/src/share/classes/javax/management/monitor/Monitor.java	Fri Nov 14 17:22:10 2008 +0100
@@ -34,7 +34,6 @@
 import java.security.PrivilegedAction;
 import java.util.List;
 import java.util.concurrent.CopyOnWriteArrayList;
-import java.util.concurrent.ExecutorService;
 import java.util.concurrent.Executors;
 import java.util.concurrent.Future;
 import java.util.concurrent.LinkedBlockingQueue;
@@ -517,7 +516,7 @@
         //
         ObservedObject o = createObservedObject(object);
         o.setAlreadyNotified(RESET_FLAGS_ALREADY_NOTIFIED);
-        o.setDerivedGauge(null);
+        o.setDerivedGauge(INTEGER_ZERO);
         o.setDerivedGaugeTimeStamp(System.currentTimeMillis());
         observedObjects.add(o);
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/test/javax/management/monitor/DerivedGaugeMonitorTest.java	Fri Nov 14 17:22:10 2008 +0100
@@ -0,0 +1,270 @@
+/*
+ * Copyright 2008 Sun Microsystems, Inc.  All Rights Reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
+ * CA 95054 USA or visit www.sun.com if you need additional information or
+ * have any questions.
+ */
+
+/*
+ * @test
+ * @bug 6683213
+ * @summary Test that the initial derived gauge is (Integer)0
+ * @author Daniel Fuchs
+ * @run clean DerivedGaugeMonitorTest
+ * @run build DerivedGaugeMonitorTest
+ * @run main DerivedGaugeMonitorTest
+ */
+
+import java.io.Serializable;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import javax.management.MBeanServer;
+import javax.management.MBeanServerFactory;
+import javax.management.ObjectName;
+import javax.management.monitor.CounterMonitor;
+import javax.management.monitor.GaugeMonitor;
+
+public class DerivedGaugeMonitorTest {
+
+    public static interface Things {
+        public long getALong();
+        public int getAnInt();
+        public double getADouble();
+        public short getAShort();
+        public byte getAByte();
+        public float getAFloat();
+    }
+    public static interface MyMBean extends Things {
+        public Things getAThing();
+    }
+
+    public static class MyThings implements Things, Serializable {
+        private static final long serialVersionUID = -4333982919572564126L;
+
+        private volatile long along = 0;
+        private volatile int anint = 0;
+        private volatile short ashort = 0;
+        private volatile byte abyte = 0;
+        private volatile float afloat = 0;
+        private volatile double adouble = 0;
+
+        private volatile transient boolean mutable;
+
+        public MyThings() {
+            this(false);
+        }
+
+        protected MyThings(boolean mutable) {
+            this.mutable=mutable;
+        }
+
+        public long getALong() {
+            return mutable?along++:along;
+        }
+
+        public int getAnInt() {
+            return mutable?anint++:anint;
+        }
+
+        public double getADouble() {
+            return mutable?adouble++:adouble;
+        }
+
+        public short getAShort() {
+            return mutable?ashort++:ashort;
+        }
+
+        public byte getAByte() {
+            return mutable?abyte++:abyte;
+        }
+
+        public float getAFloat() {
+            return mutable?afloat++:afloat;
+        }
+
+        @Override
+        public Object clone() throws CloneNotSupportedException {
+            final MyThings other = (MyThings)super.clone();
+            other.mutable=false;
+            return other;
+        }
+    }
+
+    public static class My implements MyMBean {
+
+        public final CountDownLatch cdl = new CountDownLatch(6);
+
+        private final MyThings things = new MyThings(true);
+        private volatile int count = 0;
+
+        public Things getAThing() {
+            count++;
+            cdl.countDown();
+            try {
+                return (Things) things.clone();
+            } catch (CloneNotSupportedException ex) {
+                return null;
+            }
+        }
+
+        public long getALong() {
+            count++;
+            cdl.countDown();
+            return things.getALong();
+        }
+
+        public int getAnInt() {
+            count++;
+            cdl.countDown();
+            return things.getAnInt();
+        }
+
+        public double getADouble() {
+            count++;
+            cdl.countDown();
+            return things.getADouble();
+        }
+
+        public short getAShort() {
+            count++;
+            cdl.countDown();
+            return things.getAShort();
+        }
+
+        public byte getAByte() {
+            count++;
+            cdl.countDown();
+            return things.getAByte();
+        }
+
+        public float getAFloat() {
+            count++;
+            cdl.countDown();
+            return things.getAFloat();
+        }
+
+    }
+
+
+    public static String[] attributes = {
+        "AByte","AShort","AnInt","ALong","AFloat","ADouble"
+    };
+    public static String[] things = {
+        "AThing.AByte","AThing.AShort","AThing.AnInt","AThing.ALong",
+        "AThing.AFloat","AThing.ADouble"
+    };
+
+    public static void check(String attr, MBeanServer server, ObjectName mon,
+            ObjectName mbean) throws Exception {
+        final Object obj = server.getAttribute(mon, "DerivedGauge");
+        check(attr,server,mon,mbean,obj);
+    }
+
+    public static void check(String attr, MBeanServer server, ObjectName mon,
+            ObjectName mbean, Object obj) throws Exception  {
+        if (obj == null)
+            throw new Exception("Derived gauge for: " + attr +
+                    " ["+mon+", "+mbean+"] is null!");
+        if (!Integer.valueOf(0).equals(obj))
+            throw new Exception("Derived gauge for: " + attr +
+                    " ["+mon+", "+mbean+"] is "+obj);
+    }
+
+    public static void check(String attr, MBeanServer server, ObjectName mon,
+            ObjectName mbean, long start) throws Exception {
+        final Object obj = server.getAttribute(mon, "DerivedGauge");
+        final long now = System.currentTimeMillis();
+        final long gran = (Long)server.getAttribute(mon, "GranularityPeriod");
+        if (now > start +2*gran) {
+            throw new Exception(attr+": Can't verify test case: " +
+                    "granularity period expired!");
+        }
+        check(attr,server,mon,mbean,obj);
+    }
+
+    public static void test(String attr) throws Exception {
+        System.err.println("Testing "+ attr);
+        final MBeanServer server = MBeanServerFactory.createMBeanServer();
+        final ObjectName mbean = new ObjectName("ugly:type=cr.p");
+        final My my = new My();
+        final GaugeMonitor mon2 = new GaugeMonitor();
+        final ObjectName mon2n = new ObjectName("mon1:type=GaugeMonitor");
+        final CounterMonitor mon1 = new CounterMonitor();
+        final ObjectName mon1n = new ObjectName("mon2:type=CounterMonitor");
+
+        server.registerMBean(my, mbean);
+        server.registerMBean(mon1, mon1n);
+        server.registerMBean(mon2, mon2n);
+
+        mon1.addObservedObject(mbean);
+        mon1.setGranularityPeriod(60000); // 60 sec...
+        mon1.setObservedAttribute(attr);
+        mon1.setDifferenceMode(true);
+        check(attr,server,mon1n,mbean);
+
+        mon2.addObservedObject(mbean);
+        mon2.setGranularityPeriod(60000); // 60 sec...
+        mon2.setObservedAttribute(attr);
+        mon2.setDifferenceMode(true);
+        check(attr,server,mon2n,mbean);
+
+        final long approxStart = System.currentTimeMillis();
+        mon1.start();
+        mon2.start();
+
+        try {
+            check(attr,server,mon1n,mbean,approxStart);
+            check(attr,server,mon2n,mbean,approxStart);
+            check(attr,server,mon1n,mbean,approxStart);
+            check(attr,server,mon2n,mbean,approxStart);
+
+
+            mon1.setGranularityPeriod(5);
+            mon2.setGranularityPeriod(5);
+
+            my.cdl.await(1000, TimeUnit.MILLISECONDS);
+            if (my.cdl.getCount() > 0)
+                throw new Exception(attr+": Count down not reached!");
+
+            // just check that we don't get an exception now...
+            System.err.println(attr+": [" + mon1n+
+                    "] DerivedGauge is now "+
+                    server.getAttribute(mon1n, "DerivedGauge"));
+            System.err.println(attr+": [" + mon2n+
+                    "] DerivedGauge is now "+
+                    server.getAttribute(mon2n, "DerivedGauge"));
+        } finally {
+           try {mon1.stop();} catch (Exception x) {}
+           try {mon2.stop();} catch (Exception x) {}
+        }
+    }
+
+
+
+
+    public static void main(String[] args) throws Exception {
+        for (String attr:attributes) {
+            test(attr);
+        }
+        for (String attr:things) {
+            test(attr);
+        }
+    }
+
+}