changeset 1769:688a79263a66

Avoid using HostVMArguments in list-heap-dumps. Alterations to list-heap-dumps command to reflect avoidance of HostVMArguments, updates to related tests that are affected. Updates to DAOs and associated tests to accomodate changes in list-heap-dumps and reflect general preference for AgentId and VmId over HostRef and VmRef. PR2455 Reviewed-by: omajid, jerboaa Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-June/014224.html
author James Aziz <jaziz@redhat.com>
date Wed, 24 Jun 2015 15:10:55 -0400
parents 3cb516445ee5
children 53e035eb0171
files storage/core/src/main/java/com/redhat/thermostat/storage/dao/HostInfoDAO.java storage/core/src/main/java/com/redhat/thermostat/storage/dao/VmInfoDAO.java storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOImpl.java storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/VmInfoDAOImpl.java storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOTest.java storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/VmInfoDAOTest.java vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ListHeapDumpsCommand.java vm-heap-analysis/command/src/test/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ListHeapDumpsCommandTest.java vm-heap-analysis/common/src/main/java/com/redhat/thermostat/vm/heap/analysis/common/HeapDAO.java vm-heap-analysis/common/src/main/java/com/redhat/thermostat/vm/heap/analysis/common/internal/HeapDAOImpl.java
diffstat 10 files changed, 268 insertions(+), 90 deletions(-) [+]
line wrap: on
line diff
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/dao/HostInfoDAO.java	Thu Jun 25 09:58:54 2015 -0400
+++ b/storage/core/src/main/java/com/redhat/thermostat/storage/dao/HostInfoDAO.java	Wed Jun 24 15:10:55 2015 -0400
@@ -39,6 +39,7 @@
 import java.util.Collection;
 
 import com.redhat.thermostat.annotations.Service;
+import com.redhat.thermostat.storage.core.AgentId;
 import com.redhat.thermostat.storage.core.Category;
 import com.redhat.thermostat.storage.core.Countable;
 import com.redhat.thermostat.storage.core.HostRef;
@@ -71,9 +72,18 @@
 
     /**
      * 
-     * @return A collection of hosts, which may be empty.
+     * @return A collection of hosts (HostRefs), which may be empty.
+     *
+     * @deprecated use {@link #getAgentIds()} instead.
      */
+    @Deprecated
     Collection<HostRef> getHosts();
+
+    /**
+     *
+     * @return The a collection of hosts (AgentIds), which may be empty.
+     */
+    Collection<AgentId> getAgentIds();
     
     /**
      * 
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/dao/VmInfoDAO.java	Thu Jun 25 09:58:54 2015 -0400
+++ b/storage/core/src/main/java/com/redhat/thermostat/storage/dao/VmInfoDAO.java	Wed Jun 24 15:10:55 2015 -0400
@@ -36,14 +36,15 @@
 
 package com.redhat.thermostat.storage.dao;
 
-import com.redhat.thermostat.storage.core.VmId;
 import java.util.Collection;
 
 import com.redhat.thermostat.annotations.Service;
+import com.redhat.thermostat.storage.core.AgentId;
 import com.redhat.thermostat.storage.core.Category;
 import com.redhat.thermostat.storage.core.Countable;
 import com.redhat.thermostat.storage.core.HostRef;
 import com.redhat.thermostat.storage.core.Key;
+import com.redhat.thermostat.storage.core.VmId;
 import com.redhat.thermostat.storage.core.VmRef;
 import com.redhat.thermostat.storage.model.VmInfo;
 import com.redhat.thermostat.storage.model.VmInfo.KeyValuePair;
@@ -80,8 +81,23 @@
 
     VmInfo getVmInfo(VmRef ref);
 
+    /**
+     *
+     * @param host The host to get the VM(s) for.
+     * @return A collection of the VM(s) as VmRef(s).
+     *
+     * @deprecated use {@link #getVmIds(AgentId)}
+     */
+    @Deprecated
     Collection<VmRef> getVMs(HostRef host);
 
+    /**
+     *
+     * @param agentId The id of host to get the VM(s) for.
+     * @return A collection of the VmId(s).
+     */
+    Collection<VmId> getVmIds(AgentId agentId);
+
     void putVmInfo(VmInfo info);
 
     void putVmStoppedTime(String vmId, long since);
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOImpl.java	Thu Jun 25 09:58:54 2015 -0400
+++ b/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOImpl.java	Wed Jun 24 15:10:55 2015 -0400
@@ -44,6 +44,7 @@
 import java.util.logging.Logger;
 
 import com.redhat.thermostat.common.utils.LoggingUtils;
+import com.redhat.thermostat.storage.core.AgentId;
 import com.redhat.thermostat.storage.core.Category;
 import com.redhat.thermostat.storage.core.CategoryAdapter;
 import com.redhat.thermostat.storage.core.Cursor;
@@ -155,15 +156,38 @@
 
     @Override
     public Collection<HostRef> getHosts() {
+        List<HostRef> result = new ArrayList<>();
+        for(HostInfo hostInfo : getHostInfos()) {
+            result.add(toHostRef(hostInfo));
+        }
+
+        return result;
+    }
+
+    @Override
+    public Collection<AgentId> getAgentIds() {
+        List<AgentId> result = new ArrayList<>();
+        for(HostInfo hostInfo : getHostInfos()) {
+            result.add(toAgentId(hostInfo));
+        }
+
+        return result;
+    }
+
+    private AgentId toAgentId(HostInfo hostInfo) {
+        return new AgentId(hostInfo.getAgentId());
+    }
+
+    private Collection<HostInfo> getHostInfos() {
         Cursor<HostInfo> cursor = getAllHostInfoCursor();
         if (cursor == null) {
             return Collections.emptyList();
         }
-        List<HostRef> result = new ArrayList<>();
+        List<HostInfo> result = new ArrayList<>();
         while (cursor.hasNext()) {
-            HostInfo hostInfo = cursor.next();
-            result.add(toHostRef(hostInfo));
+            result.add(cursor.next());
         }
+
         return result;
     }
 
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/VmInfoDAOImpl.java	Thu Jun 25 09:58:54 2015 -0400
+++ b/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/VmInfoDAOImpl.java	Wed Jun 24 15:10:55 2015 -0400
@@ -36,7 +36,6 @@
 
 package com.redhat.thermostat.storage.internal.dao;
 
-import com.redhat.thermostat.storage.core.VmId;
 import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
@@ -45,6 +44,7 @@
 import java.util.logging.Logger;
 
 import com.redhat.thermostat.common.utils.LoggingUtils;
+import com.redhat.thermostat.storage.core.AgentId;
 import com.redhat.thermostat.storage.core.Category;
 import com.redhat.thermostat.storage.core.CategoryAdapter;
 import com.redhat.thermostat.storage.core.Cursor;
@@ -55,6 +55,7 @@
 import com.redhat.thermostat.storage.core.StatementDescriptor;
 import com.redhat.thermostat.storage.core.StatementExecutionException;
 import com.redhat.thermostat.storage.core.Storage;
+import com.redhat.thermostat.storage.core.VmId;
 import com.redhat.thermostat.storage.core.VmRef;
 import com.redhat.thermostat.storage.dao.DAOException;
 import com.redhat.thermostat.storage.dao.VmInfoDAO;
@@ -194,12 +195,63 @@
 
     @Override
     public Collection<VmRef> getVMs(HostRef host) {
+        AgentId agentId = new AgentId(host.getAgentId());
+
+        Collection<VmInfo> vmInfos = getAllVmInfosForHost(agentId);
+        if(vmInfos.equals(Collections.emptyList())) {
+            return Collections.emptyList();
+        }
+
+        return buildVMsFromQuery(vmInfos, host);
+    }
+
+    @Deprecated
+    private Collection<VmRef> buildVMsFromQuery(Collection<VmInfo> vmInfos, HostRef host) {
+        List<VmRef> vmRefs = new ArrayList<VmRef>();
+        for(VmInfo vmInfo : vmInfos) {
+            VmRef vm = buildVmRefFromChunk(vmInfo, host);
+            vmRefs.add(vm);
+        }
+
+        return vmRefs;
+    }
+
+    @Deprecated
+    private VmRef buildVmRefFromChunk(VmInfo vmInfo, HostRef host) {
+        String id = vmInfo.getVmId();
+        Integer pid = vmInfo.getVmPid();
+        // TODO can we do better than the main class?
+        String mainClass = vmInfo.getMainClass();
+        VmRef ref = new VmRef(host, id, pid, mainClass);
+        return ref;
+    }
+
+    @Override
+    public Collection<VmId> getVmIds(AgentId agentId) {
+        Collection<VmInfo> vmInfos = getAllVmInfosForHost(agentId);
+        if(vmInfos.equals(Collections.emptyList())) {
+            return Collections.emptyList();
+        }
+
+        return buildVmIdsFromQuery(vmInfos);
+    }
+
+    private Collection<VmId> buildVmIdsFromQuery(Collection<VmInfo> vmInfos) {
+        List<VmId> vmIds = new ArrayList<VmId>();
+        for(VmInfo vmInfo : vmInfos) {
+            vmIds.add(new VmId(vmInfo.getVmId()));
+        }
+
+        return vmIds;
+    }
+
+    private Collection<VmInfo> getAllVmInfosForHost(AgentId agentId) {
         StatementDescriptor<VmInfo> desc = new StatementDescriptor<>(vmInfoCategory, QUERY_ALL_VMS_FOR_HOST);
         PreparedStatement<VmInfo> stmt;
         Cursor<VmInfo> cursor;
         try {
             stmt = storage.prepareStatement(desc);
-            stmt.setString(0, host.getAgentId());
+            stmt.setString(0, agentId.get());
             cursor = stmt.executeQuery();
         } catch (DescriptorParsingException e) {
             // should not happen, but if it *does* happen, at least log it
@@ -210,27 +262,13 @@
             logger.log(Level.SEVERE, "Executing query '" + desc + "' failed!", e);
             return Collections.emptyList();
         }
-        return buildVMsFromQuery(cursor, host);
-    }
 
-    private Collection<VmRef> buildVMsFromQuery(Cursor<VmInfo> cursor, HostRef host) {
-        List<VmRef> vmRefs = new ArrayList<VmRef>();
-        while (cursor.hasNext()) {
-            VmInfo vmInfo = cursor.next();
-            VmRef vm = buildVmRefFromChunk(vmInfo, host);
-            vmRefs.add(vm);
+        List<VmInfo> vmInfos = new ArrayList<VmInfo>();
+        while(cursor.hasNext()) {
+            vmInfos.add(cursor.next());
         }
 
-        return vmRefs;
-    }
-
-    private VmRef buildVmRefFromChunk(VmInfo vmInfo, HostRef host) {
-        String id = vmInfo.getVmId();
-        Integer pid = vmInfo.getVmPid();
-        // TODO can we do better than the main class?
-        String mainClass = vmInfo.getMainClass();
-        VmRef ref = new VmRef(host, id, pid, mainClass);
-        return ref;
+        return vmInfos;
     }
 
     @Override
--- a/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOTest.java	Thu Jun 25 09:58:54 2015 -0400
+++ b/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOTest.java	Wed Jun 24 15:10:55 2015 -0400
@@ -52,6 +52,7 @@
 import org.mockito.ArgumentCaptor;
 import org.mockito.Mockito;
 
+import com.redhat.thermostat.storage.core.AgentId;
 import com.redhat.thermostat.storage.core.Cursor;
 import com.redhat.thermostat.storage.core.DescriptorParsingException;
 import com.redhat.thermostat.storage.core.HostRef;
@@ -161,6 +162,19 @@
         assertTrue(hosts.contains(new HostRef("123", "fluffhost1")));
     }
 
+    @Test
+    public void testGetAgentIdsSingleHost() throws DescriptorParsingException, StatementExecutionException {
+
+        Storage storage = setupStorageForSingleHost();
+        AgentInfoDAO agentInfo = mock(AgentInfoDAO.class);
+
+        HostInfoDAO hostsDAO = new HostInfoDAOImpl(storage, agentInfo);
+        Collection<AgentId> agentIds = hostsDAO.getAgentIds();
+
+        assertEquals(1, agentIds.size());
+        assertTrue(agentIds.contains(new AgentId("123")));
+    }
+
     private Storage setupStorageForSingleHost() throws DescriptorParsingException, StatementExecutionException {
         HostInfo hostConfig = new HostInfo("foo-agent", "fluffhost1", OS_NAME, OS_KERNEL, CPU_MODEL, CPU_NUM, MEMORY_TOTAL);
         hostConfig.setAgentId("123");
--- a/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/VmInfoDAOTest.java	Thu Jun 25 09:58:54 2015 -0400
+++ b/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/VmInfoDAOTest.java	Wed Jun 24 15:10:55 2015 -0400
@@ -46,6 +46,7 @@
 import static org.mockito.Mockito.when;
 
 import java.util.Collection;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -53,6 +54,7 @@
 import org.junit.Test;
 import org.mockito.ArgumentCaptor;
 
+import com.redhat.thermostat.storage.core.AgentId;
 import com.redhat.thermostat.storage.core.Cursor;
 import com.redhat.thermostat.storage.core.DescriptorParsingException;
 import com.redhat.thermostat.storage.core.HostRef;
@@ -61,6 +63,7 @@
 import com.redhat.thermostat.storage.core.StatementDescriptor;
 import com.redhat.thermostat.storage.core.StatementExecutionException;
 import com.redhat.thermostat.storage.core.Storage;
+import com.redhat.thermostat.storage.core.VmId;
 import com.redhat.thermostat.storage.core.VmRef;
 import com.redhat.thermostat.storage.dao.DAOException;
 import com.redhat.thermostat.storage.dao.VmInfoDAO;
@@ -168,6 +171,30 @@
     }
 
     @Test
+    public void testGetVMsDescriptorException() throws DescriptorParsingException {
+        Storage storage = mock(Storage.class);
+        VmInfoDAO vmInfoDao = new VmInfoDAOImpl(storage);
+        HostRef host = new HostRef("123", "fluffhost");
+
+        when(storage.prepareStatement(anyDescriptor())).thenThrow(new DescriptorParsingException("testException"));
+
+        Collection<VmRef> vmRefs = vmInfoDao.getVMs(host);
+        assertEquals(Collections.emptyList(), vmRefs);
+    }
+
+    @Test
+    public void testGetVmIdsDescriptorException() throws DescriptorParsingException {
+        Storage storage = mock(Storage.class);
+        VmInfoDAO vmInfoDao = new VmInfoDAOImpl(storage);
+        AgentId agentId = new AgentId("agentId");
+
+        when(storage.prepareStatement(anyDescriptor())).thenThrow(new DescriptorParsingException("testException"));
+
+        Collection<VmId> vmIds = vmInfoDao.getVmIds(agentId);
+        assertEquals(Collections.emptyList(), vmIds);
+    }
+
+    @Test
     public void testGetVmInfo() throws DescriptorParsingException, StatementExecutionException {
         Storage storage = mock(Storage.class);
         @SuppressWarnings("unchecked")
@@ -234,14 +261,25 @@
     }
 
     @Test
-    public void testSingleVM() throws DescriptorParsingException, StatementExecutionException {
+    public void testGetVMsWithSingleVM() throws DescriptorParsingException, StatementExecutionException {
         Storage storage = setupStorageForSingleVM();
         VmInfoDAO dao = new VmInfoDAOImpl(storage);
         HostRef host = new HostRef("123", "fluffhost");
 
         Collection<VmRef> vms = dao.getVMs(host);
 
-        assertCollection(vms, new VmRef(host, "vmId", 123, "mainClass1"));
+        assertVmRefCollection(vms, new VmRef(host, "vmId", 123, "mainClass1"));
+    }
+
+    @Test
+    public void testgetVmIdsWithSingleVM() throws DescriptorParsingException, StatementExecutionException {
+        Storage storage = setupStorageForSingleVM();
+        VmInfoDAO dao = new VmInfoDAOImpl(storage);
+        AgentId agentId = new AgentId("123");
+
+        Collection<VmId> vmIds = dao.getVmIds(agentId);
+
+        assertVmIdCollection(vmIds, new VmId("vmId"));
     }
 
     private Storage setupStorageForSingleVM() throws DescriptorParsingException, StatementExecutionException {
@@ -265,15 +303,25 @@
   }
 
     @Test
-    public void testMultiVMs() throws DescriptorParsingException, StatementExecutionException {
+    public void testGetVMsWithMultiVMs() throws DescriptorParsingException, StatementExecutionException {
         Storage storage = setupStorageForMultiVM();
         VmInfoDAO dao = new VmInfoDAOImpl(storage);
-
         HostRef host = new HostRef("456", "fluffhost");
 
         Collection<VmRef> vms = dao.getVMs(host);
 
-        assertCollection(vms, new VmRef(host, "vmId1", 123, "mainClass1"), new VmRef(host, "vmId2", 456, "mainClass2"));
+        assertVmRefCollection(vms, new VmRef(host, "vmId1", 123, "mainClass1"), new VmRef(host, "vmId2", 456, "mainClass2"));
+    }
+
+    @Test
+    public void testGetVmIdsWithMultiVMs() throws DescriptorParsingException, StatementExecutionException {
+        Storage storage = setupStorageForMultiVM();
+        VmInfoDAO dao = new VmInfoDAOImpl(storage);
+        AgentId agentId = new AgentId("456");
+
+        Collection<VmId> vmIds = dao.getVmIds(agentId);
+
+        assertVmIdCollection(vmIds, new VmId("vmId1"), new VmId("vmId2"));
     }
 
     private Storage setupStorageForMultiVM() throws DescriptorParsingException, StatementExecutionException {
@@ -300,13 +348,20 @@
       return storage;
   }
 
-    private void assertCollection(Collection<VmRef> vms, VmRef... expectedVMs) {
+    private void assertVmRefCollection(Collection<VmRef> vms, VmRef... expectedVMs) {
         assertEquals(expectedVMs.length, vms.size());
         for (VmRef expectedVM : expectedVMs) {
             assertTrue(vms.contains(expectedVM));
         }
     }
 
+    private void assertVmIdCollection(Collection<VmId> vms, VmId... expectedVmIds) {
+        assertEquals(expectedVmIds.length, vms.size());
+        for (VmId expectedVmId : expectedVmIds) {
+            assertTrue(vms.contains(expectedVmId));
+        }
+    }
+
     @Test
     public void testGetCount()
             throws DescriptorParsingException, StatementExecutionException {
--- a/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ListHeapDumpsCommand.java	Thu Jun 25 09:58:54 2015 -0400
+++ b/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ListHeapDumpsCommand.java	Wed Jun 24 15:10:55 2015 -0400
@@ -44,22 +44,25 @@
 import org.osgi.framework.FrameworkUtil;
 import org.osgi.framework.ServiceReference;
 
-import com.redhat.thermostat.client.cli.HostVMArguments;
 import com.redhat.thermostat.common.cli.AbstractCommand;
 import com.redhat.thermostat.common.cli.CommandContext;
 import com.redhat.thermostat.common.cli.CommandException;
 import com.redhat.thermostat.common.cli.TableRenderer;
 import com.redhat.thermostat.shared.locale.Translate;
-import com.redhat.thermostat.storage.core.HostRef;
-import com.redhat.thermostat.storage.core.VmRef;
+import com.redhat.thermostat.storage.core.AgentId;
+import com.redhat.thermostat.storage.core.VmId;
 import com.redhat.thermostat.storage.dao.HostInfoDAO;
 import com.redhat.thermostat.storage.dao.VmInfoDAO;
+import com.redhat.thermostat.storage.model.VmInfo;
 import com.redhat.thermostat.vm.heap.analysis.command.locale.LocaleResources;
 import com.redhat.thermostat.vm.heap.analysis.common.HeapDAO;
 import com.redhat.thermostat.vm.heap.analysis.common.model.HeapInfo;
 
 public class ListHeapDumpsCommand extends AbstractCommand {
 
+    static final String HOST_ID_ARGUMENT = "hostId";
+    static final String VM_ID_ARGUMENT = "vmId";
+
     private static final Translate<LocaleResources> translator = LocaleResources.createLocalizer();
 
     private static final String[] COLUMN_NAMES = {
@@ -82,8 +85,6 @@
 
     @Override
     public void run(CommandContext ctx) throws CommandException {
-        HostVMArguments args = new HostVMArguments(ctx.getArguments(), false, false);
-
         TableRenderer renderer = new TableRenderer(4);
 
         renderer.printLine(COLUMN_NAMES);
@@ -100,11 +101,29 @@
         requireNonNull(heapDAORef, translator.localize(LocaleResources.HEAP_SERVICE_UNAVAILABLE));
         HeapDAO heapDAO = (HeapDAO) context.getService(heapDAORef);
 
-        Collection<HostRef> hosts = args.getHost() != null ? Arrays.asList(args.getHost()) : hostDAO.getHosts();
-        for (HostRef hostRef : hosts) {
-            Collection<VmRef> vms = args.getVM() != null ? Arrays.asList(args.getVM()) : vmDAO.getVMs(hostRef);
-            for (VmRef vmRef : vms) {
-                printDumpsForVm(heapDAO, hostRef, vmRef, renderer);
+        String stringVmId = ctx.getArguments().getArgument(VM_ID_ARGUMENT);
+        String stringAgentId = null;
+
+        VmId vmId = null;
+        AgentId agentId = null;
+
+        if (stringVmId != null) {
+            vmId = new VmId(stringVmId);
+            final VmInfo vmInfo = vmDAO.getVmInfo(vmId);
+
+            stringAgentId = vmInfo.getAgentId();
+        } else {
+            stringAgentId = ctx.getArguments().getArgument(HOST_ID_ARGUMENT);
+        }
+
+        if (stringAgentId != null)
+            agentId = new AgentId(stringAgentId);
+
+        Collection<AgentId> hosts = stringAgentId != null ? Arrays.asList(agentId) : hostDAO.getAgentIds();
+        for (AgentId host : hosts) {
+            Collection<VmId> vms = stringVmId != null ? Arrays.asList(vmId) : vmDAO.getVmIds(host);
+            for (VmId vm : vms) {
+                printDumpsForVm(heapDAO, host, vm, renderer);
             }
         }
 
@@ -115,11 +134,11 @@
         renderer.render(ctx.getConsole().getOutput());
     }
 
-    private void printDumpsForVm(HeapDAO heapDAO, HostRef hostRef, VmRef vmRef, TableRenderer renderer) {
-        Collection<HeapInfo> infos = heapDAO.getAllHeapInfo(vmRef);
+    private void printDumpsForVm(HeapDAO heapDAO, AgentId agentId, VmId vmId, TableRenderer renderer) {
+        Collection<HeapInfo> infos = heapDAO.getAllHeapInfo(agentId, vmId);
         for (HeapInfo info : infos) {
-            renderer.printLine(hostRef.getAgentId(),
-                               vmRef.getVmId(),
+            renderer.printLine(agentId.get(),
+                               vmId.get(),
                                info.getHeapId(),
                                new Date(info.getTimeStamp()).toString());
         }
--- a/vm-heap-analysis/command/src/test/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ListHeapDumpsCommandTest.java	Thu Jun 25 09:58:54 2015 -0400
+++ b/vm-heap-analysis/command/src/test/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ListHeapDumpsCommandTest.java	Wed Jun 24 15:10:55 2015 -0400
@@ -38,7 +38,6 @@
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.fail;
-import static org.mockito.Matchers.isA;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 
@@ -53,10 +52,11 @@
 import com.redhat.thermostat.common.cli.Command;
 import com.redhat.thermostat.common.cli.CommandException;
 import com.redhat.thermostat.common.cli.SimpleArguments;
-import com.redhat.thermostat.storage.core.HostRef;
-import com.redhat.thermostat.storage.core.VmRef;
+import com.redhat.thermostat.storage.core.AgentId;
+import com.redhat.thermostat.storage.core.VmId;
 import com.redhat.thermostat.storage.dao.HostInfoDAO;
 import com.redhat.thermostat.storage.dao.VmInfoDAO;
+import com.redhat.thermostat.storage.model.VmInfo;
 import com.redhat.thermostat.test.TestCommandContextFactory;
 import com.redhat.thermostat.testutils.StubBundleContext;
 import com.redhat.thermostat.vm.heap.analysis.common.HeapDAO;
@@ -111,10 +111,8 @@
 
     @Test
     public void verifyWorks() throws CommandException {
-        HostRef hostRef = mock(HostRef.class);
-        when(hostRef.getAgentId()).thenReturn("host-id");
-        VmRef vmRef = mock(VmRef.class);
-        when(vmRef.getVmId()).thenReturn("1");
+        AgentId agentId = new AgentId("host-id");
+        VmId vmId = new VmId("1");
 
         HeapInfo heapInfo = mock(HeapInfo.class);
         Calendar timestamp = Calendar.getInstance();
@@ -124,17 +122,17 @@
 
         HeapDAO heapDao = mock(HeapDAO.class);
 
-        VmInfoDAO vmInfo = mock(VmInfoDAO.class);
-        when(vmInfo.getVMs(hostRef)).thenReturn(Arrays.asList(vmRef));
+        VmInfoDAO vmInfoDAO = mock(VmInfoDAO.class);
+        when(vmInfoDAO.getVmIds(agentId)).thenReturn(Arrays.asList(vmId));
 
         HostInfoDAO hostInfo = mock(HostInfoDAO.class);
-        when(hostInfo.getHosts()).thenReturn(Arrays.asList(hostRef));
+        when(hostInfo.getAgentIds()).thenReturn(Arrays.asList(agentId));
 
-        when(heapDao.getAllHeapInfo(vmRef)).thenReturn(Arrays.asList(heapInfo));
+        when(heapDao.getAllHeapInfo(agentId, vmId)).thenReturn(Arrays.asList(heapInfo));
 
         StubBundleContext context = new StubBundleContext();
         context.registerService(HostInfoDAO.class, hostInfo, null);
-        context.registerService(VmInfoDAO.class, vmInfo, null);
+        context.registerService(VmInfoDAO.class, vmInfoDAO, null);
         context.registerService(HeapDAO.class, heapDao, null);
 
         Command command = new ListHeapDumpsCommand(context);
@@ -149,15 +147,11 @@
 
     @Test
     public void verifyWorksWithFilterOnHost() throws CommandException {
-        HostRef hostRef1 = mock(HostRef.class);
-        when(hostRef1.getStringID()).thenReturn("host1");
-        VmRef vmRef1 = mock(VmRef.class);
-        when(vmRef1.getVmId()).thenReturn("1");
+        AgentId agentId1 = new AgentId("host1");
+        VmId vmId1 = new VmId("1");
 
-        HostRef hostRef2 = mock(HostRef.class);
-        when(hostRef2.getStringID()).thenReturn("host2");
-        VmRef vmRef2 = mock(VmRef.class);
-        when(vmRef2.getVmId()).thenReturn("2");
+        AgentId agentId2 = new AgentId("host2");
+        VmId vmId2 = new VmId("2");
 
         HeapInfo heapInfo = mock(HeapInfo.class);
         Calendar timestamp = Calendar.getInstance();
@@ -168,13 +162,13 @@
         HeapDAO heapDao = mock(HeapDAO.class);
 
         VmInfoDAO vmInfo = mock(VmInfoDAO.class);
-        when(vmInfo.getVMs(isA(HostRef.class))).thenReturn(Arrays.asList(vmRef1)).thenReturn(Arrays.asList(vmRef2));
+        when(vmInfo.getVmIds(agentId1)).thenReturn(Arrays.asList(vmId1)).thenReturn(Arrays.asList(vmId2));
 
         HostInfoDAO hostInfo = mock(HostInfoDAO.class);
-        when(hostInfo.getHosts()).thenReturn(Arrays.asList(hostRef1, hostRef2));
+        when(hostInfo.getAgentIds()).thenReturn(Arrays.asList(agentId1, agentId2));
 
-        when(heapDao.getAllHeapInfo(vmRef1)).thenReturn(Arrays.asList(heapInfo));
-        when(heapDao.getAllHeapInfo(vmRef2)).thenReturn(Arrays.asList(heapInfo));
+        when(heapDao.getAllHeapInfo(agentId1, vmId1)).thenReturn(Arrays.asList(heapInfo));
+        when(heapDao.getAllHeapInfo(agentId2, vmId2)).thenReturn(Arrays.asList(heapInfo));
 
         StubBundleContext context = new StubBundleContext();
         context.registerService(HostInfoDAO.class, hostInfo, null);
@@ -197,37 +191,33 @@
 
     @Test
     public void verifyWorksWithFilterOnHostAndVM() throws CommandException {
-        HostRef hostRef1 = mock(HostRef.class);
-        when(hostRef1.getStringID()).thenReturn("host1");
-        when(hostRef1.getAgentId()).thenReturn("host1");
-        VmRef vmRef1 = mock(VmRef.class);
-        when(vmRef1.getVmId()).thenReturn("1");
+        AgentId agentId1 = new AgentId("host1");
+        VmId vmId1 = new VmId("1");
 
-        HostRef hostRef2 = mock(HostRef.class);
-        when(hostRef2.getStringID()).thenReturn("host2");
-        VmRef vmRef2 = mock(VmRef.class);
-        when(vmRef2.getVmId()).thenReturn("2");
+        AgentId agentId2 = new AgentId("host2");
+        VmId vmId2 = new VmId("2");
 
         HeapInfo heapInfo = mock(HeapInfo.class);
         Calendar timestamp = Calendar.getInstance();
         timestamp.set(2012, 5, 7, 15, 32, 0);
         when(heapInfo.getTimeStamp()).thenReturn(timestamp.getTimeInMillis());
-        when(heapInfo.getHeapDumpId()).thenReturn("0001");
+        when(heapInfo.getHeapId()).thenReturn("0001");
 
         HeapDAO heapDao = mock(HeapDAO.class);
+        VmInfoDAO vmInfoDAO = mock(VmInfoDAO.class);
 
-        VmInfoDAO vmInfo = mock(VmInfoDAO.class);
-        when(vmInfo.getVMs(isA(HostRef.class))).thenReturn(Arrays.asList(vmRef1)).thenReturn(Arrays.asList(vmRef2));
+        VmInfo vmInfo1 = new VmInfo("host1", "1", 123, 0, 0, null, null, null, null, null, null, null, null, null,
+                null, null,0, "myUsername");
+        when(vmInfoDAO.getVmInfo(vmId1)).thenReturn(vmInfo1);
 
         HostInfoDAO hostInfo = mock(HostInfoDAO.class);
-        when(hostInfo.getHosts()).thenReturn(Arrays.asList(hostRef1, hostRef2));
+        when(hostInfo.getAgentIds()).thenReturn(Arrays.asList(agentId1, agentId2));
 
-        when(heapDao.getAllHeapInfo(vmRef1)).thenReturn(Arrays.asList(heapInfo));
-        when(heapDao.getAllHeapInfo(vmRef2)).thenReturn(Arrays.asList(heapInfo));
+        when(heapDao.getAllHeapInfo(agentId1, vmId1)).thenReturn(Arrays.asList(heapInfo));
 
         StubBundleContext context = new StubBundleContext();
         context.registerService(HostInfoDAO.class, hostInfo, null);
-        context.registerService(VmInfoDAO.class, vmInfo, null);
+        context.registerService(VmInfoDAO.class, vmInfoDAO, null);
         context.registerService(HeapDAO.class, heapDao, null);
 
         Command command = new ListHeapDumpsCommand(context);
@@ -239,7 +229,8 @@
 
         command.run(factory.createContext(args));
 
-        String expected = "HOST ID VM ID HEAP ID TIMESTAMP\n";
+        String expected = "HOST ID VM ID HEAP ID TIMESTAMP\n" +
+                          "host1   1     0001    Thu Jun 07 15:32:00 UTC 2012\n";
 
         assertEquals(expected, factory.getOutput());
     }
--- a/vm-heap-analysis/common/src/main/java/com/redhat/thermostat/vm/heap/analysis/common/HeapDAO.java	Thu Jun 25 09:58:54 2015 -0400
+++ b/vm-heap-analysis/common/src/main/java/com/redhat/thermostat/vm/heap/analysis/common/HeapDAO.java	Wed Jun 24 15:10:55 2015 -0400
@@ -42,8 +42,10 @@
 import java.util.Collection;
 
 import com.redhat.thermostat.annotations.Service;
+import com.redhat.thermostat.storage.core.AgentId;
 import com.redhat.thermostat.storage.core.Category;
 import com.redhat.thermostat.storage.core.Key;
+import com.redhat.thermostat.storage.core.VmId;
 import com.redhat.thermostat.storage.core.VmRef;
 import com.redhat.thermostat.vm.heap.analysis.common.model.HeapInfo;
 
@@ -58,8 +60,11 @@
 
     void putHeapInfo(HeapInfo heapInfo, File heapDumpFile, ObjectHistogram histogramData, Runnable whenDone) throws IOException;
 
+    @Deprecated
     Collection<HeapInfo> getAllHeapInfo(VmRef vm);
 
+    Collection<HeapInfo> getAllHeapInfo(AgentId agentId, VmId vmId);
+
     InputStream getHeapDumpData(HeapInfo heapInfo);
 
     ObjectHistogram getHistogram(HeapInfo heapInfo);
--- a/vm-heap-analysis/common/src/main/java/com/redhat/thermostat/vm/heap/analysis/common/internal/HeapDAOImpl.java	Thu Jun 25 09:58:54 2015 -0400
+++ b/vm-heap-analysis/common/src/main/java/com/redhat/thermostat/vm/heap/analysis/common/internal/HeapDAOImpl.java	Wed Jun 24 15:10:55 2015 -0400
@@ -38,7 +38,6 @@
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
-import java.io.Closeable;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileNotFoundException;
@@ -53,6 +52,7 @@
 import java.util.logging.Logger;
 
 import com.redhat.thermostat.common.utils.LoggingUtils;
+import com.redhat.thermostat.storage.core.AgentId;
 import com.redhat.thermostat.storage.core.CloseOnSave;
 import com.redhat.thermostat.storage.core.Cursor;
 import com.redhat.thermostat.storage.core.DescriptorParsingException;
@@ -63,6 +63,7 @@
 import com.redhat.thermostat.storage.core.StatementExecutionException;
 import com.redhat.thermostat.storage.core.Storage;
 import com.redhat.thermostat.storage.core.StorageException;
+import com.redhat.thermostat.storage.core.VmId;
 import com.redhat.thermostat.storage.core.VmRef;
 import com.redhat.thermostat.vm.heap.analysis.common.HeapDAO;
 import com.redhat.thermostat.vm.heap.analysis.common.HeapDump;
@@ -187,13 +188,18 @@
 
     @Override
     public Collection<HeapInfo> getAllHeapInfo(VmRef vm) {
+        return getAllHeapInfo(new AgentId(vm.getHostRef().getAgentId()), new VmId(vm.getVmId()));
+    }
+
+    @Override
+    public Collection<HeapInfo> getAllHeapInfo(AgentId agentId, VmId vmId) {
         StatementDescriptor<HeapInfo> desc = new StatementDescriptor<>(heapInfoCategory, QUERY_ALL_HEAPS);
         PreparedStatement<HeapInfo> stmt;
         Cursor<HeapInfo> cursor;
         try {
             stmt = storage.prepareStatement(desc);
-            stmt.setString(0, vm.getHostRef().getAgentId());
-            stmt.setString(1, vm.getVmId());
+            stmt.setString(0, agentId.get());
+            stmt.setString(1, vmId.get());
             cursor = stmt.executeQuery();
         } catch (DescriptorParsingException e) {
             // should not happen, but if it *does* happen, at least log it
@@ -204,7 +210,7 @@
             log.log(Level.SEVERE, "Executing query '" + desc + "' failed!", e);
             return Collections.emptyList();
         }
-        
+
         Collection<HeapInfo> heapInfos = new ArrayList<>();
         while (cursor.hasNext()) {
             heapInfos.add(cursor.next());