changeset 84:1cec7da69972

Implement capturing information about loaded classes.
author Roman Kennke <rkennke@redhat.com>
date Fri, 24 Feb 2012 23:30:00 +0100
parents 71da4a71e1c0
children 3a3204f4705b
files agent/src/main/java/com/redhat/thermostat/backend/Backend.java agent/src/main/java/com/redhat/thermostat/backend/system/JvmStatDataExtractor.java agent/src/main/java/com/redhat/thermostat/backend/system/JvmStatHostListener.java agent/src/main/java/com/redhat/thermostat/backend/system/JvmStatVmClassListener.java agent/src/main/java/com/redhat/thermostat/backend/system/SystemBackend.java agent/src/test/java/com/redhat/thermostat/backend/system/JvmStatHostListenerTest.java agent/src/test/java/com/redhat/thermostat/backend/system/JvmStatVmClassListenerTest.java common/src/main/java/com/redhat/thermostat/common/VmClassStat.java common/src/main/java/com/redhat/thermostat/common/dao/VmClassStatConverter.java common/src/main/java/com/redhat/thermostat/common/dao/VmClassStatDAO.java common/src/main/java/com/redhat/thermostat/common/storage/Category.java common/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java common/src/test/java/com/redhat/thermostat/common/dao/VmClassStatConverterTest.java common/src/test/java/com/redhat/thermostat/common/dao/VmClassStatDAOTest.java
diffstat 14 files changed, 583 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/agent/src/main/java/com/redhat/thermostat/backend/Backend.java	Wed Feb 22 23:27:25 2012 +0100
+++ b/agent/src/main/java/com/redhat/thermostat/backend/Backend.java	Fri Feb 24 23:30:00 2012 +0100
@@ -189,11 +189,11 @@
         observeNewJvm = newValue;
     }
 
-    public final void store(Chunk chunk) {
+    public void store(Chunk chunk) {
         storage.putChunk(chunk);
     }
 
-    public final void update(Chunk chunk) {
+    public void update(Chunk chunk) {
         storage.updateChunk(chunk);
     }
 }
--- a/agent/src/main/java/com/redhat/thermostat/backend/system/JvmStatDataExtractor.java	Wed Feb 22 23:27:25 2012 +0100
+++ b/agent/src/main/java/com/redhat/thermostat/backend/system/JvmStatDataExtractor.java	Fri Feb 24 23:30:00 2012 +0100
@@ -159,4 +159,8 @@
         return (Long) vm.findByName("sun.gc.generation." + generation + ".space." + space + ".used").getValue();
     }
 
+    public long getLoadedClasses() throws MonitorException {
+        return (Long) vm.findByName("java.cls.loadedClasses").getValue();
+    }
+
 }
--- a/agent/src/main/java/com/redhat/thermostat/backend/system/JvmStatHostListener.java	Wed Feb 22 23:27:25 2012 +0100
+++ b/agent/src/main/java/com/redhat/thermostat/backend/system/JvmStatHostListener.java	Fri Feb 24 23:30:00 2012 +0100
@@ -177,6 +177,7 @@
                 JvmStatVmListener listener = new JvmStatVmListener(backend, vmId);
                 listenerMap.put(vmId, listener);
                 vm.addVmListener(listener);
+                vm.addVmListener(new JvmStatVmClassListener(backend, vmId));
             } else {
                 logger.log(Level.FINE, "skipping new vm " + vmId);
             }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/agent/src/main/java/com/redhat/thermostat/backend/system/JvmStatVmClassListener.java	Fri Feb 24 23:30:00 2012 +0100
@@ -0,0 +1,91 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat 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 for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.backend.system;
+
+import java.util.logging.Level;
+import java.util.logging.Logger;
+
+import sun.jvmstat.monitor.MonitorException;
+import sun.jvmstat.monitor.MonitoredVm;
+import sun.jvmstat.monitor.event.MonitorStatusChangeEvent;
+import sun.jvmstat.monitor.event.VmEvent;
+import sun.jvmstat.monitor.event.VmListener;
+
+import com.redhat.thermostat.common.VmClassStat;
+import com.redhat.thermostat.common.dao.VmClassStatConverter;
+import com.redhat.thermostat.common.utils.LoggingUtils;
+
+class JvmStatVmClassListener implements VmListener {
+
+    private static final Logger logger = LoggingUtils.getLogger(JvmStatVmClassListener.class);
+
+    private SystemBackend backend;
+    private int vmId;
+
+    JvmStatVmClassListener(SystemBackend backend, int vmId) {
+        this.backend = backend;
+        this.vmId = vmId;
+    }
+
+    @Override
+    public void disconnected(VmEvent vmEvent) {
+        /* nothing to do here */
+    }
+
+    @Override
+    public void monitorStatusChanged(MonitorStatusChangeEvent vmEvent) {
+        /* nothing to do here */
+    }
+
+    @Override
+    public void monitorsUpdated(VmEvent vmEvent) {
+        MonitoredVm vm = vmEvent.getMonitoredVm();
+        try {
+            JvmStatDataExtractor extractor = new JvmStatDataExtractor(vm);
+            long loadedClasses = extractor.getLoadedClasses();
+            long timestamp = System.currentTimeMillis();
+            VmClassStat stat = new VmClassStat(vmId, timestamp, loadedClasses);
+            VmClassStatConverter dao = new VmClassStatConverter();
+            backend.store(dao.vmClassStatToChunk(stat));
+        } catch (MonitorException e) {
+            logger.log(Level.WARNING, "error gathering class info for vm " + vmId, e);
+        }
+
+
+    }
+
+}
--- a/agent/src/main/java/com/redhat/thermostat/backend/system/SystemBackend.java	Wed Feb 22 23:27:25 2012 +0100
+++ b/agent/src/main/java/com/redhat/thermostat/backend/system/SystemBackend.java	Fri Feb 24 23:30:00 2012 +0100
@@ -59,6 +59,7 @@
 import com.redhat.thermostat.common.MemoryStat;
 import com.redhat.thermostat.common.NetworkInterfaceInfo;
 import com.redhat.thermostat.common.VmCpuStat;
+import com.redhat.thermostat.common.dao.VmClassStatDAO;
 import com.redhat.thermostat.common.storage.Category;
 import com.redhat.thermostat.common.storage.Chunk;
 import com.redhat.thermostat.common.storage.Key;
@@ -162,6 +163,7 @@
 
         categories.addAll(JvmStatHostListener.getCategories());
         categories.addAll(JvmStatVmListener.getCategories());
+        categories.add(VmClassStatDAO.vmClassStatsCategory);
     }
 
     @Override
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/agent/src/test/java/com/redhat/thermostat/backend/system/JvmStatHostListenerTest.java	Fri Feb 24 23:30:00 2012 +0100
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat 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 for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.backend.system;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.anyString;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import java.util.HashSet;
+import java.util.Set;
+
+import org.junit.Test;
+import org.mockito.Matchers;
+
+import sun.jvmstat.monitor.HostIdentifier;
+import sun.jvmstat.monitor.MonitoredHost;
+import sun.jvmstat.monitor.MonitoredVm;
+import sun.jvmstat.monitor.StringMonitor;
+import sun.jvmstat.monitor.VmIdentifier;
+import sun.jvmstat.monitor.event.VmStatusChangeEvent;
+
+import com.redhat.thermostat.backend.Backend;
+
+public class JvmStatHostListenerTest {
+
+    @Test
+    public void testVmStatusChangedAddsVmClassListener() throws Exception {
+        VmStatusChangeEvent vmEvent = mock(VmStatusChangeEvent.class);
+        Set<Integer> startedVms = new HashSet<Integer>();
+        startedVms.add(123);
+        when(vmEvent.getStarted()).thenReturn(startedVms);
+
+        MonitoredVm vm = mock(MonitoredVm.class);
+        StringMonitor monitor = mock(StringMonitor.class);
+        when(monitor.stringValue()).thenReturn("test");
+        when(monitor.getValue()).thenReturn("test");
+        when(vm.findByName(anyString())).thenReturn(monitor);
+        MonitoredHost host = mock(MonitoredHost.class);
+        HostIdentifier hostId = mock(HostIdentifier.class);
+        when(host.getHostIdentifier()).thenReturn(hostId);
+        when(host.getMonitoredVm(any(VmIdentifier.class))).thenReturn(vm);
+        when(vmEvent.getMonitoredHost()).thenReturn(host);
+
+        JvmStatHostListener l = new JvmStatHostListener();
+        SystemBackend backend = mock(SystemBackend.class);
+        when(backend.getObserveNewJvm()).thenReturn(true);
+        l.setBackend(backend);
+
+        l.vmStatusChanged(vmEvent);
+
+        verify(vm).addVmListener(Matchers.isA(JvmStatVmClassListener.class));
+    }
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/agent/src/test/java/com/redhat/thermostat/backend/system/JvmStatVmClassListenerTest.java	Fri Feb 24 23:30:00 2012 +0100
@@ -0,0 +1,96 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat 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 for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.backend.system;
+
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+import org.junit.Test;
+import org.mockito.ArgumentCaptor;
+
+import sun.jvmstat.monitor.Monitor;
+import sun.jvmstat.monitor.MonitoredVm;
+import sun.jvmstat.monitor.event.VmEvent;
+
+import com.redhat.thermostat.common.storage.Chunk;
+import com.redhat.thermostat.common.storage.Key;
+
+public class JvmStatVmClassListenerTest {
+
+    @Test
+    public void testMonitorUpdatedClassStat() throws Exception {
+
+        SystemBackend backend = mock(SystemBackend.class);
+        int vmId = 123;
+        JvmStatVmClassListener l = new JvmStatVmClassListener(backend, vmId);
+        VmEvent vmEvent = mock(VmEvent.class);
+        MonitoredVm monitoredVm = mock(MonitoredVm.class);
+        Monitor m = mock(Monitor.class);
+        when(m.getValue()).thenReturn(new Long(1234));
+        when(monitoredVm.findByName("java.cls.loadedClasses")).thenReturn(m);
+        when(vmEvent.getMonitoredVm()).thenReturn(monitoredVm);
+
+        l.monitorsUpdated(vmEvent);
+
+        ArgumentCaptor<Chunk> chunkArg = ArgumentCaptor.forClass(Chunk.class);
+        verify(backend).store(chunkArg.capture());
+        assertEquals("1234", chunkArg.getValue().get(new Key("loadedClasses", false)));
+        assertEquals("123", chunkArg.getValue().get(new Key("vm-id", false)));
+    }
+
+    @Test
+    public void testMonitorUpdatedClassStatTwice() throws Exception {
+
+        SystemBackend backend = mock(SystemBackend.class);
+        int vmId = 123;
+        JvmStatVmClassListener l = new JvmStatVmClassListener(backend, vmId);
+        VmEvent vmEvent = mock(VmEvent.class);
+        MonitoredVm monitoredVm = mock(MonitoredVm.class);
+        Monitor m = mock(Monitor.class);
+        when(m.getValue()).thenReturn(new Long(1234));
+        when(monitoredVm.findByName("java.cls.loadedClasses")).thenReturn(m);
+        when(vmEvent.getMonitoredVm()).thenReturn(monitoredVm);
+
+        l.monitorsUpdated(vmEvent);
+        l.monitorsUpdated(vmEvent);
+
+        // This checks a bug where the Category threw an IllegalStateException because the DAO
+        // created a new one on each call, thus violating the unique guarantee of Category.
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/src/main/java/com/redhat/thermostat/common/VmClassStat.java	Fri Feb 24 23:30:00 2012 +0100
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat 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 for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.common;
+
+public class VmClassStat {
+
+    private int vmId;
+    private long timestamp;
+    private long loadedClasses;
+
+    public VmClassStat(int vmId, long timestamp, long loadedClasses) {
+        this.vmId = vmId;
+        this.timestamp = timestamp;
+        this.loadedClasses = loadedClasses;
+    }
+
+    public int getVmId() {
+        return vmId;
+    }
+
+    public long getTimestamp() {
+        return timestamp;
+    }
+
+    public long getLoadedClasses() {
+        return loadedClasses;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/src/main/java/com/redhat/thermostat/common/dao/VmClassStatConverter.java	Fri Feb 24 23:30:00 2012 +0100
@@ -0,0 +1,53 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat 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 for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.common.dao;
+
+import com.redhat.thermostat.common.VmClassStat;
+import com.redhat.thermostat.common.storage.Category;
+import com.redhat.thermostat.common.storage.Chunk;
+import com.redhat.thermostat.common.storage.Key;
+
+public class VmClassStatConverter {
+
+    public Chunk vmClassStatToChunk(VmClassStat vmClassStat) {
+        Chunk chunk = new Chunk(VmClassStatDAO.vmClassStatsCategory, false);
+        chunk.put(VmClassStatDAO.vmIdKey, String.valueOf(vmClassStat.getVmId()));
+        chunk.put(Key.TIMESTAMP, String.valueOf(vmClassStat.getTimestamp()));
+        chunk.put(VmClassStatDAO.loadedClassesKey, String.valueOf(vmClassStat.getLoadedClasses()));
+        return chunk;
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/src/main/java/com/redhat/thermostat/common/dao/VmClassStatDAO.java	Fri Feb 24 23:30:00 2012 +0100
@@ -0,0 +1,55 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat 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 for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.common.dao;
+
+import com.redhat.thermostat.common.storage.Category;
+import com.redhat.thermostat.common.storage.Key;
+
+public class VmClassStatDAO {
+
+    static final Key vmIdKey = new Key("vm-id", false);
+    static final Key loadedClassesKey = new Key("loadedClasses", false);
+
+    public static final Category vmClassStatsCategory = new Category("vm-class-stats");
+
+    static {
+        vmClassStatsCategory.addKey(Key.TIMESTAMP);
+        vmClassStatsCategory.addKey(vmIdKey);
+        vmClassStatsCategory.addKey(loadedClassesKey);
+        vmClassStatsCategory.lock(); // TODO: This can be done in a better way! (E.g. pass all keys as constructor var args)
+    }
+}
--- a/common/src/main/java/com/redhat/thermostat/common/storage/Category.java	Wed Feb 22 23:27:25 2012 +0100
+++ b/common/src/main/java/com/redhat/thermostat/common/storage/Category.java	Fri Feb 24 23:30:00 2012 +0100
@@ -37,8 +37,9 @@
 package com.redhat.thermostat.common.storage;
 
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
 import java.util.HashSet;
-import java.util.Iterator;
 import java.util.List;
 import java.util.Set;
 
@@ -83,8 +84,8 @@
         }
     }
 
-    public synchronized Iterator<Key> getEntryIterator() {
-        return keys.iterator();
+    public synchronized Collection<Key> getKeys() {
+        return Collections.unmodifiableCollection(keys);
     }
 
     public void setConnectionKey(ConnectionKey connKey) {
--- a/common/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java	Wed Feb 22 23:27:25 2012 +0100
+++ b/common/src/main/java/com/redhat/thermostat/common/storage/MongoStorage.java	Fri Feb 24 23:30:00 2012 +0100
@@ -130,8 +130,7 @@
             replaceKey = getAgentDBObject();
             replaceKeyNestedParts = new HashMap<String, BasicDBObject>();
         }
-        for (Iterator<Key> iter = cat.getEntryIterator(); iter.hasNext();) {
-            Key key = iter.next();
+        for (Key key : cat.getKeys()) {
             boolean isKey = key.isPartialCategoryKey();
             String[] entryParts = key.getName().split("\\.");
             if (entryParts.length == 2) {
@@ -185,8 +184,7 @@
         BasicDBObject updateKey = getAgentDBObject();
         Map<String, BasicDBObject> nestedParts = new HashMap<String, BasicDBObject>();
         Map<String, BasicDBObject> updateKeyNestedParts = new HashMap<String, BasicDBObject>();
-        for (Iterator<Key> iter = cat.getEntryIterator(); iter.hasNext();) {
-            Key key = iter.next();
+        for (Key key : cat.getKeys()) {
             boolean isKey = key.isPartialCategoryKey();
             String[] entryParts = key.getName().split("\\.");
             if (entryParts.length == 2) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/src/test/java/com/redhat/thermostat/common/dao/VmClassStatConverterTest.java	Fri Feb 24 23:30:00 2012 +0100
@@ -0,0 +1,61 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat 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 for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.common.dao;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+import com.redhat.thermostat.common.VmClassStat;
+import com.redhat.thermostat.common.storage.Chunk;
+import com.redhat.thermostat.common.storage.Key;
+
+public class VmClassStatConverterTest {
+
+    @Test
+    public void testVmClassStatToChunk() {
+        VmClassStat stat = new VmClassStat(123, 1234L, 12345L);
+
+        VmClassStatConverter dao = new VmClassStatConverter();
+        Chunk chunk = dao.vmClassStatToChunk(stat);
+
+        assertEquals("vm-class-stats", chunk.getCategory().getName());
+        assertEquals("1234", chunk.get(Key.TIMESTAMP));
+        assertEquals("123", chunk.get(new Key("vm-id", false)));
+        assertEquals("12345", chunk.get(new Key("loadedClasses", false)));
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/src/test/java/com/redhat/thermostat/common/dao/VmClassStatDAOTest.java	Fri Feb 24 23:30:00 2012 +0100
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2012 Red Hat, Inc.
+ *
+ * This file is part of Thermostat.
+ *
+ * Thermostat is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published
+ * by the Free Software Foundation; either version 2, or (at your
+ * option) any later version.
+ *
+ * Thermostat 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 for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Thermostat; see the file COPYING.  If not see
+ * <http://www.gnu.org/licenses/>.
+ *
+ * Linking this code with other modules is making a combined work
+ * based on this code.  Thus, the terms and conditions of the GNU
+ * General Public License cover the whole combination.
+ *
+ * As a special exception, the copyright holders of this code give
+ * you permission to link this code with independent modules to
+ * produce an executable, regardless of the license terms of these
+ * independent modules, and to copy and distribute the resulting
+ * executable under terms of your choice, provided that you also
+ * meet, for each linked independent module, the terms and conditions
+ * of the license of that module.  An independent module is a module
+ * which is not derived from or based on this code.  If you modify
+ * this code, you may extend this exception to your version of the
+ * library, but you are not obligated to do so.  If you do not wish
+ * to do so, delete this exception statement from your version.
+ */
+
+package com.redhat.thermostat.common.dao;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.util.Collection;
+
+import org.junit.Test;
+
+import com.redhat.thermostat.common.storage.Key;
+
+public class VmClassStatDAOTest {
+
+    @Test
+    public void testCategory() {
+        assertEquals("vm-class-stats", VmClassStatDAO.vmClassStatsCategory.getName());
+        Collection<Key> keys = VmClassStatDAO.vmClassStatsCategory.getKeys();
+        assertTrue(keys.contains(new Key("vm-id", false)));
+        assertTrue(keys.contains(new Key("timestamp", false)));
+        assertTrue(keys.contains(new Key("loadedClasses", false)));
+        assertEquals(3, keys.size());
+
+    }
+}