Mercurial > hg > release > thermostat-1.6
changeset 1958:bd656ada63c5
dump-heap command echos new heap ID on success
Reviewed-by: omajid, jerboaa, neugens
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-January/017387.html
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-March/017961.html
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-June/019752.html
PR3042
author | Andrew Azores <aazores@redhat.com> |
---|---|
date | Thu, 03 Mar 2016 13:02:57 -0500 |
parents | 92eed4d29c16 |
children | 3784a15aef43 |
files | vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/DumpHeapCommand.java vm-heap-analysis/command/src/main/resources/com/redhat/thermostat/vm/heap/analysis/command/locale/strings.properties vm-heap-analysis/command/src/test/java/com/redhat/thermostat/vm/heap/analysis/command/internal/DumpHeapCommandTest.java |
diffstat | 3 files changed, 92 insertions(+), 15 deletions(-) [+] |
line wrap: on
line diff
--- a/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/DumpHeapCommand.java Tue Jun 28 15:22:30 2016 +0200 +++ b/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/DumpHeapCommand.java Thu Mar 03 13:02:57 2016 -0500 @@ -36,8 +36,15 @@ package com.redhat.thermostat.vm.heap.analysis.command.internal; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; import java.util.concurrent.Semaphore; +import com.redhat.thermostat.storage.core.VmRef; +import com.redhat.thermostat.vm.heap.analysis.common.model.HeapInfo; import org.osgi.framework.BundleContext; import org.osgi.framework.FrameworkUtil; import org.osgi.framework.ServiceReference; @@ -51,7 +58,7 @@ import com.redhat.thermostat.storage.dao.AgentInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; import com.redhat.thermostat.vm.heap.analysis.command.locale.LocaleResources; - +import com.redhat.thermostat.vm.heap.analysis.common.HeapDAO; public class DumpHeapCommand extends AbstractCommand { @@ -71,6 +78,22 @@ @Override public void run(final CommandContext ctx) throws CommandException { + ServiceReference vmInfoRef = context.getServiceReference(VmInfoDAO.class.getName()); + requireNonNull(vmInfoRef, translator.localize(LocaleResources.VM_SERVICE_UNAVAILABLE)); + VmInfoDAO vmInfoDAO = (VmInfoDAO) context.getService(vmInfoRef); + + ServiceReference agentInfoRef = context.getServiceReference(AgentInfoDAO.class.getName()); + requireNonNull(agentInfoRef, translator.localize(LocaleResources.AGENT_SERVICE_UNAVAILABLE)); + AgentInfoDAO agentInfoDAO = (AgentInfoDAO) context.getService(agentInfoRef); + + ServiceReference requestQueueRef = context.getServiceReference(RequestQueue.class.getName()); + requireNonNull(requestQueueRef, translator.localize(LocaleResources.REQUEST_QUEUE_UNAVAILABLE)); + RequestQueue queue = (RequestQueue) context.getService(requestQueueRef); + + ServiceReference heapRef = context.getServiceReference(HeapDAO.class.getName()); + requireNonNull(heapRef, translator.localize(LocaleResources.HEAP_SERVICE_UNAVAILABLE)); + final HeapDAO heapDAO = (HeapDAO) context.getService(heapRef); + final HostVMArguments args = new HostVMArguments(ctx.getArguments()); final CommandException[] ex = new CommandException[1]; @@ -78,7 +101,9 @@ Runnable successHandler = new Runnable() { @Override public void run() { - ctx.getConsole().getOutput().println(translator.localize(LocaleResources.COMMAND_HEAP_DUMP_DONE).getContents()); + String latestHeapId = getLatestHeapId(heapDAO, args.getVM()); + ctx.getConsole().getOutput().println(translator.localize(LocaleResources.COMMAND_HEAP_DUMP_DONE, + latestHeapId).getContents()); s.release(); } }; @@ -91,18 +116,6 @@ } }; - ServiceReference vmInfoRef = context.getServiceReference(VmInfoDAO.class.getName()); - requireNonNull(vmInfoRef, translator.localize(LocaleResources.VM_SERVICE_UNAVAILABLE)); - VmInfoDAO vmInfoDAO = (VmInfoDAO) context.getService(vmInfoRef); - - ServiceReference agentInfoRef = context.getServiceReference(AgentInfoDAO.class.getName()); - requireNonNull(agentInfoRef, translator.localize(LocaleResources.AGENT_SERVICE_UNAVAILABLE)); - AgentInfoDAO agentInfoDAO = (AgentInfoDAO) context.getService(agentInfoRef); - - ServiceReference requestQueueRef = context.getServiceReference(RequestQueue.class.getName()); - requireNonNull(requestQueueRef, translator.localize(LocaleResources.REQUEST_QUEUE_UNAVAILABLE)); - RequestQueue queue = (RequestQueue) context.getService(requestQueueRef); - implementation.execute(vmInfoDAO, agentInfoDAO, args.getVM(), queue, successHandler, errorHandler); context.ungetService(vmInfoRef); @@ -120,5 +133,19 @@ } } + static String getLatestHeapId(HeapDAO heapDao, VmRef vmRef) { + Collection<HeapInfo> heapInfos = heapDao.getAllHeapInfo(vmRef); + List<HeapInfo> sortedByLatest = new ArrayList<>(heapInfos); + Collections.sort(sortedByLatest, new Comparator<HeapInfo>() { + @Override + public int compare(HeapInfo a, HeapInfo b) { + return Long.compare(b.getTimeStamp(), a.getTimeStamp()); + } + }); + HeapInfo latest = sortedByLatest.get(0); + return latest.getHeapId(); + } + + }
--- a/vm-heap-analysis/command/src/main/resources/com/redhat/thermostat/vm/heap/analysis/command/locale/strings.properties Tue Jun 28 15:22:30 2016 +0200 +++ b/vm-heap-analysis/command/src/main/resources/com/redhat/thermostat/vm/heap/analysis/command/locale/strings.properties Thu Mar 03 13:02:57 2016 -0500 @@ -19,7 +19,7 @@ OBJECT_ID_REQUIRED = Object ID required HEAP_DUMP_ERROR = Error dumping heap (agent: {0}, vm: {1}) -COMMAND_HEAP_DUMP_DONE = Done +COMMAND_HEAP_DUMP_DONE = Heap dump ID: {0} COMMAND_FIND_ROOT_NO_ROOT_FOUND = No root found for: {0}
--- a/vm-heap-analysis/command/src/test/java/com/redhat/thermostat/vm/heap/analysis/command/internal/DumpHeapCommandTest.java Tue Jun 28 15:22:30 2016 +0200 +++ b/vm-heap-analysis/command/src/test/java/com/redhat/thermostat/vm/heap/analysis/command/internal/DumpHeapCommandTest.java Thu Mar 03 13:02:57 2016 -0500 @@ -36,7 +36,9 @@ package com.redhat.thermostat.vm.heap.analysis.command.internal; +import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThat; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import static org.mockito.Matchers.any; @@ -45,7 +47,12 @@ import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; +import com.redhat.thermostat.storage.core.HostRef; +import com.redhat.thermostat.storage.model.VmInfo; +import com.redhat.thermostat.vm.heap.analysis.common.HeapDAO; +import com.redhat.thermostat.vm.heap.analysis.common.model.HeapInfo; import org.junit.Test; import org.mockito.ArgumentCaptor; import org.mockito.invocation.InvocationOnMock; @@ -62,6 +69,10 @@ import com.redhat.thermostat.testutils.StubBundleContext; import com.redhat.thermostat.vm.heap.analysis.command.locale.LocaleResources; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; + public class DumpHeapCommandTest { private static final Translate<LocaleResources> TRANSLATOR = LocaleResources @@ -72,10 +83,12 @@ VmInfoDAO vmInfoDao = mock(VmInfoDAO.class); AgentInfoDAO agentInfoDao = mock(AgentInfoDAO.class); RequestQueue queue = mock(RequestQueue.class); + HeapDAO heapDAO = mock(HeapDAO.class); StubBundleContext context = new StubBundleContext(); context.registerService(VmInfoDAO.class, vmInfoDao, null); context.registerService(AgentInfoDAO.class, agentInfoDao, null); context.registerService(RequestQueue.class, queue, null); + context.registerService(HeapDAO.class, heapDAO, null); DumpHeapHelper impl = mock(DumpHeapHelper.class); final ArgumentCaptor<Runnable> successHandler = ArgumentCaptor @@ -90,6 +103,10 @@ }).when(impl).execute(eq(vmInfoDao), eq(agentInfoDao), any(VmRef.class), eq(queue), successHandler.capture(), any(Runnable.class)); + HeapInfo heapInfo = new HeapInfo(); + heapInfo.setHeapDumpId("0001"); + when(heapDAO.getAllHeapInfo(any(VmRef.class))).thenReturn(Collections.singleton(heapInfo)); + DumpHeapCommand command = new DumpHeapCommand(context, impl); TestCommandContextFactory factory = new TestCommandContextFactory(); @@ -110,10 +127,12 @@ VmInfoDAO vmInfoDao = mock(VmInfoDAO.class); AgentInfoDAO agentInfoDao = mock(AgentInfoDAO.class); RequestQueue queue = mock(RequestQueue.class); + HeapDAO heapDAO = mock(HeapDAO.class); StubBundleContext context = new StubBundleContext(); context.registerService(VmInfoDAO.class, vmInfoDao, null); context.registerService(AgentInfoDAO.class, agentInfoDao, null); context.registerService(RequestQueue.class, queue, null); + context.registerService(HeapDAO.class, heapDAO, null); DumpHeapHelper impl = mock(DumpHeapHelper.class); DumpHeapCommand command = new DumpHeapCommand(context, impl); @@ -134,9 +153,11 @@ public void verifyFailsIfAgentDaoIsNotAvailable() { VmInfoDAO vmInfoDao = mock(VmInfoDAO.class); RequestQueue queue = mock(RequestQueue.class); + HeapDAO heapDAO = mock(HeapDAO.class); StubBundleContext context = new StubBundleContext(); context.registerService(VmInfoDAO.class, vmInfoDao, null); context.registerService(RequestQueue.class, queue, null); + context.registerService(HeapDAO.class, heapDAO, null); DumpHeapHelper impl = mock(DumpHeapHelper.class); DumpHeapCommand command = new DumpHeapCommand(context, impl); @@ -159,9 +180,11 @@ public void verifyFailsIfRequestQueueIsNotAvailable() { VmInfoDAO vmInfoDao = mock(VmInfoDAO.class); AgentInfoDAO agentInfoDao = mock(AgentInfoDAO.class); + HeapDAO heapDAO = mock(HeapDAO.class); StubBundleContext context = new StubBundleContext(); context.registerService(VmInfoDAO.class, vmInfoDao, null); context.registerService(AgentInfoDAO.class, agentInfoDao, null); + context.registerService(HeapDAO.class, heapDAO, null); DumpHeapHelper impl = mock(DumpHeapHelper.class); DumpHeapCommand command = new DumpHeapCommand(context, impl); @@ -185,9 +208,11 @@ public void verifyFailsIfVmDaoIsNotAvailable() { AgentInfoDAO agentInfoDao = mock(AgentInfoDAO.class); RequestQueue queue = mock(RequestQueue.class); + HeapDAO heapDAO = mock(HeapDAO.class); StubBundleContext context = new StubBundleContext(); context.registerService(AgentInfoDAO.class, agentInfoDao, null); context.registerService(RequestQueue.class, queue, null); + context.registerService(HeapDAO.class, heapDAO, null); DumpHeapHelper impl = mock(DumpHeapHelper.class); DumpHeapCommand command = new DumpHeapCommand(context, impl); @@ -212,11 +237,13 @@ final String VM_ID = "myVm"; VmInfoDAO vmInfoDao = mock(VmInfoDAO.class); AgentInfoDAO agentInfoDao = mock(AgentInfoDAO.class); + HeapDAO heapDAO = mock(HeapDAO.class); RequestQueue queue = mock(RequestQueue.class); StubBundleContext context = new StubBundleContext(); context.registerService(VmInfoDAO.class, vmInfoDao, null); context.registerService(AgentInfoDAO.class, agentInfoDao, null); context.registerService(RequestQueue.class, queue, null); + context.registerService(HeapDAO.class, heapDAO, null); DumpHeapHelper impl = mock(DumpHeapHelper.class); DumpHeapCommand command = new DumpHeapCommand(context, impl); @@ -246,5 +273,28 @@ } } + @Test + public void testGetLatestHeapId() { + HeapInfo heapInfo1 = new HeapInfo(); + heapInfo1.setHeapId("heap1"); + heapInfo1.setTimeStamp(300L); + HeapInfo heapInfo2 = new HeapInfo(); + heapInfo2.setHeapId("heap2"); + heapInfo2.setTimeStamp(200L); + HeapInfo heapInfo3 = new HeapInfo(); + heapInfo3.setHeapId("heap3"); + heapInfo3.setTimeStamp(100L); + Collection<HeapInfo> heapInfos = Arrays.asList(heapInfo2, heapInfo1, heapInfo3); + + HeapDAO heapDao = mock(HeapDAO.class); + when(heapDao.getAllHeapInfo(any(VmRef.class))).thenReturn(heapInfos); + + HostRef hostRef = new HostRef("agent", "dummy"); + VmRef vmRef = new VmRef(hostRef, new VmInfo()); + + String result = DumpHeapCommand.getLatestHeapId(heapDao, vmRef); + assertThat(result, is(heapInfo1.getHeapId())); + } + }