Mercurial > hg > thermostat-ng > agent
changeset 1777:4f3f8e95473f
Add getAgentIds and getAliveAgentIds to AgentInfoDAO.
This patch adds getAgentIds() and getAliveAgentIds() to AgentInfoDAOImpl.
These methods are meant to replace getHosts and getAliveHosts in HostInfoDAO.
Reviewed-by: omajid, jerboaa
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-June/014265.html
line wrap: on
line diff
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/dao/AgentInfoDAO.java Mon Jun 29 16:29:08 2015 -0400 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/dao/AgentInfoDAO.java Mon Jun 29 14:58:01 2015 -0400 @@ -36,6 +36,7 @@ package com.redhat.thermostat.storage.dao; +import java.util.Collection; import java.util.List; import com.redhat.thermostat.annotations.Service; @@ -104,6 +105,18 @@ AgentInformation getAgentInformation(AgentId agentId); /** + * + * @return A collection of AgentIds, which may be empty. + */ + Collection<AgentId> getAgentIds(); + + /** + * + * @return A collection of alive AgentIds which may be empty. + */ + Collection<AgentId> getAliveAgentIds(); + + /** * Publish information about agent into the storage. */ void addAgentInformation(AgentInformation agentInfo);
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/dao/HostInfoDAO.java Mon Jun 29 16:29:08 2015 -0400 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/dao/HostInfoDAO.java Mon Jun 29 14:58:01 2015 -0400 @@ -39,7 +39,6 @@ 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; @@ -74,21 +73,20 @@ * * @return A collection of hosts (HostRefs), which may be empty. * - * @deprecated use {@link #getAgentIds()} instead. + * @deprecated use {@link com.redhat.thermostat.storage.dao.AgentInfoDAO#getAgentIds()} + * instead. */ @Deprecated Collection<HostRef> getHosts(); - - /** - * - * @return The a collection of hosts (AgentIds), which may be empty. - */ - Collection<AgentId> getAgentIds(); /** * * @return A collection of alive hosts which may be empty. + * + * @deprecated use {@link com.redhat.thermostat.storage.dao.AgentInfoDAO#getAliveAgentIds()} + * instead. */ + @Deprecated Collection<HostRef> getAliveHosts(); /**
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/AgentInfoDAOImpl.java Mon Jun 29 16:29:08 2015 -0400 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/AgentInfoDAOImpl.java Mon Jun 29 14:58:01 2015 -0400 @@ -37,6 +37,7 @@ package com.redhat.thermostat.storage.internal.dao; import java.util.ArrayList; +import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.logging.Level; @@ -230,6 +231,32 @@ } @Override + public Collection<AgentId> getAgentIds() { + List<AgentId> agentIds = new ArrayList<>(); + List<AgentInformation> agentInfos = getAllAgentInformation(); + for (AgentInformation agentInfo : agentInfos) { + agentIds.add(toAgentId(agentInfo)); + } + + return agentIds; + } + + @Override + public Collection<AgentId> getAliveAgentIds() { + List<AgentId> agentIds = new ArrayList<>(); + List<AgentInformation> agentInfos = getAliveAgents(); + for (AgentInformation agentInfo : agentInfos) { + agentIds.add(toAgentId(agentInfo)); + } + + return agentIds; + } + + private AgentId toAgentId(AgentInformation agentInfo) { + return new AgentId(agentInfo.getAgentId()); + } + + @Override public void addAgentInformation(AgentInformation agentInfo) { StatementDescriptor<AgentInformation> desc = new StatementDescriptor<>(CATEGORY, DESC_ADD_AGENT_INFO); PreparedStatement<AgentInformation> prepared;
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOImpl.java Mon Jun 29 16:29:08 2015 -0400 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOImpl.java Mon Jun 29 14:58:01 2015 -0400 @@ -44,7 +44,6 @@ 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; @@ -157,27 +156,13 @@ @Override public Collection<HostRef> getHosts() { List<HostRef> result = new ArrayList<>(); - for(HostInfo hostInfo : getHostInfos()) { + 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) { @@ -191,6 +176,23 @@ return result; } + private Cursor<HostInfo> getAllHostInfoCursor() { + StatementDescriptor<HostInfo> desc = new StatementDescriptor<>(hostInfoCategory, QUERY_ALL_HOSTS); + PreparedStatement<HostInfo> prepared; + try { + prepared = storage.prepareStatement(desc); + return prepared.executeQuery(); + } catch (DescriptorParsingException e) { + // should not happen, but if it *does* happen, at least log it + logger.log(Level.SEVERE, "Preparing query '" + desc + "' failed!", e); + return null; + } catch (StatementExecutionException e) { + // should not happen, but if it *does* happen, at least log it + logger.log(Level.SEVERE, "Executing query '" + desc + "' failed!", e); + return null; + } + } + @Override public Collection<HostRef> getAliveHosts() { List<HostRef> hosts = new ArrayList<>(); @@ -212,23 +214,6 @@ String hostName = hostInfo.getHostname(); return new HostRef(agentId, hostName); } - - private Cursor<HostInfo> getAllHostInfoCursor() { - StatementDescriptor<HostInfo> desc = new StatementDescriptor<>(hostInfoCategory, QUERY_ALL_HOSTS); - PreparedStatement<HostInfo> prepared; - try { - prepared = storage.prepareStatement(desc); - return prepared.executeQuery(); - } catch (DescriptorParsingException e) { - // should not happen, but if it *does* happen, at least log it - logger.log(Level.SEVERE, "Preparing query '" + desc + "' failed!", e); - return null; - } catch (StatementExecutionException e) { - // should not happen, but if it *does* happen, at least log it - logger.log(Level.SEVERE, "Executing query '" + desc + "' failed!", e); - return null; - } - } @Override public long getCount() {
--- a/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/AgentInfoDAOTest.java Mon Jun 29 16:29:08 2015 -0400 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/AgentInfoDAOTest.java Mon Jun 29 14:58:01 2015 -0400 @@ -46,6 +46,7 @@ import static org.mockito.Mockito.when; import java.util.Collection; +import java.util.Collections; import java.util.List; import org.junit.Before; @@ -53,6 +54,8 @@ import org.mockito.ArgumentCaptor; import org.mockito.Mockito; +import com.redhat.thermostat.common.Pair; +import com.redhat.thermostat.storage.core.AgentId; import com.redhat.thermostat.storage.core.Category; import com.redhat.thermostat.storage.core.Cursor; import com.redhat.thermostat.storage.core.DescriptorParsingException; @@ -331,5 +334,154 @@ verify(remove).execute(); } + @Test + public void testGetAgentIdsSingleAgent() throws DescriptorParsingException, + StatementExecutionException { + + Pair<Storage, PreparedStatement<AgentInformation>> setup = setupStorageForSingleAgent(); + Storage storage = setup.getFirst(); + PreparedStatement<AgentInformation> stmt = setup.getSecond(); + + AgentInfoDAO agentInfoDAO = new AgentInfoDAOImpl(storage); + Collection<AgentId> agentIds = agentInfoDAO.getAgentIds(); + + assertEquals(1, agentIds.size()); + assertTrue(agentIds.contains(new AgentId("1234"))); + verify(stmt).executeQuery(); + } + + @Test + public void testGetAgentIds3Agents() throws DescriptorParsingException, + StatementExecutionException { + + Pair<Storage, PreparedStatement<AgentInformation>> setup = setupStorageFor3Agents(); + Storage storage = setup.getFirst(); + PreparedStatement<AgentInformation> stmt = setup.getSecond(); + + AgentInfoDAO agentInfoDAO = new AgentInfoDAOImpl(storage); + Collection<AgentId> agentIds = agentInfoDAO.getAgentIds(); + + assertEquals(3, agentIds.size()); + assertTrue(agentIds.contains(new AgentId("1234"))); + assertTrue(agentIds.contains(new AgentId("4567"))); + assertTrue(agentIds.contains(new AgentId("8910"))); + verify(storage).prepareStatement(anyDescriptor()); + verify(stmt).executeQuery(); + } + + @Test + public void getAliveAgentIdsSingle() throws DescriptorParsingException, + StatementExecutionException { + + Pair<Storage, PreparedStatement<AgentInformation>> setup = setupStorageForSingleAgent(); + Storage storage = setup.getFirst(); + PreparedStatement<AgentInformation> stmt = setup.getSecond(); + + AgentInfoDAO agentInfoDAO = new AgentInfoDAOImpl(storage); + Collection<AgentId> agentIds = agentInfoDAO.getAliveAgentIds(); + + assertEquals(1, agentIds.size()); + assertTrue(agentIds.contains(new AgentId("1234"))); + verify(storage).prepareStatement(anyDescriptor()); + verify(stmt).setBoolean(0, true); + verify(stmt).executeQuery(); + } + + @Test + public void getAliveAgentIds3() throws DescriptorParsingException, StatementExecutionException { + Pair<Storage, PreparedStatement<AgentInformation>> setup = setupStorageFor3Agents(); + Storage storage = setup.getFirst(); + PreparedStatement<AgentInformation> stmt = setup.getSecond(); + + AgentInfoDAO agentInfoDAO = new AgentInfoDAOImpl(storage); + Collection<AgentId> agentIds = agentInfoDAO.getAliveAgentIds(); + + assertEquals(3, agentIds.size()); + assertTrue(agentIds.contains(new AgentId("1234"))); + assertTrue(agentIds.contains(new AgentId("4567"))); + assertTrue(agentIds.contains(new AgentId("8910"))); + verify(storage).prepareStatement(anyDescriptor()); + verify(stmt).setBoolean(0, true); + verify(stmt).executeQuery(); + } + + @Test + public void getAliveAgentIdsDescriptorException() throws DescriptorParsingException { + Collection<AgentId> agentIds = Collections.emptyList(); + Storage storage = mock(Storage.class); + AgentInfoDAO agentInfoDAO = new AgentInfoDAOImpl(storage); + + when(storage.prepareStatement(anyDescriptor())).thenThrow(new DescriptorParsingException + ("testException")); + + agentIds = agentInfoDAO.getAgentIds(); + + assertEquals(0, agentIds.size()); + } + + @Test + public void getAliveAgentIdsStatementException() throws DescriptorParsingException, + StatementExecutionException { + + Collection<AgentId> agentIds = Collections.emptyList(); + Storage storage = mock(Storage.class); + AgentInfoDAO agentInfoDAO = new AgentInfoDAOImpl(storage); + + @SuppressWarnings("unchecked") + PreparedStatement<AgentInformation> stmt = (PreparedStatement<AgentInformation>) mock(PreparedStatement.class); + when(storage.prepareStatement(anyDescriptor())).thenReturn(stmt); + + StatementExecutionException testException = new StatementExecutionException(new Throwable + ("testException")); + when(stmt.executeQuery()).thenThrow(testException); + + agentIds = agentInfoDAO.getAliveAgentIds(); + + assertEquals(0, agentIds.size()); + } + + private Pair<Storage, PreparedStatement<AgentInformation>> setupStorageForSingleAgent() + throws DescriptorParsingException, StatementExecutionException { + + @SuppressWarnings("unchecked") + Cursor<AgentInformation> agentCursor = (Cursor<AgentInformation>) mock(Cursor.class); + when(agentCursor.hasNext()).thenReturn(true).thenReturn(false); + when(agentCursor.next()).thenReturn(agentInfo1); + + Storage storage = mock(Storage.class); + @SuppressWarnings("unchecked") + PreparedStatement<AgentInformation> stmt = (PreparedStatement<AgentInformation>) mock(PreparedStatement.class); + when(storage.prepareStatement(anyDescriptor())).thenReturn(stmt); + when(stmt.executeQuery()).thenReturn(agentCursor); + return new Pair<>(storage, stmt); + } + + private Pair<Storage, PreparedStatement<AgentInformation>> setupStorageFor3Agents() + throws DescriptorParsingException, StatementExecutionException { + + AgentInformation agentInfo2 = new AgentInformation("4567"); + agentInfo2.setAlive(true); + agentInfo2.setConfigListenAddress("foobar:666"); + agentInfo2.setStartTime(100); + agentInfo2.setStopTime(10); + + AgentInformation agentInfo3 = new AgentInformation("8910"); + agentInfo3.setAlive(true); + agentInfo3.setConfigListenAddress("foobar:666"); + agentInfo3.setStartTime(100); + agentInfo3.setStopTime(10); + + @SuppressWarnings("unchecked") + Cursor<AgentInformation> agentCursor = (Cursor<AgentInformation>) mock(Cursor.class); + when(agentCursor.hasNext()).thenReturn(true).thenReturn(true).thenReturn(true).thenReturn(false); + when(agentCursor.next()).thenReturn(agentInfo1).thenReturn(agentInfo2).thenReturn(agentInfo3); + + Storage storage = mock(Storage.class); + @SuppressWarnings("unchecked") + PreparedStatement<AgentInformation> stmt = (PreparedStatement<AgentInformation>) mock(PreparedStatement.class); + when(storage.prepareStatement(anyDescriptor())).thenReturn(stmt); + when(stmt.executeQuery()).thenReturn(agentCursor); + return new Pair<>(storage, stmt); + } }
--- a/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOTest.java Mon Jun 29 16:29:08 2015 -0400 +++ b/storage/core/src/test/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOTest.java Mon Jun 29 14:58:01 2015 -0400 @@ -52,7 +52,6 @@ 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; @@ -162,19 +161,6 @@ 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/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ListHeapDumpsCommand.java Mon Jun 29 16:29:08 2015 -0400 +++ b/vm-heap-analysis/command/src/main/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ListHeapDumpsCommand.java Mon Jun 29 14:58:01 2015 -0400 @@ -51,7 +51,7 @@ import com.redhat.thermostat.shared.locale.Translate; 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.AgentInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; import com.redhat.thermostat.storage.model.VmInfo; import com.redhat.thermostat.vm.heap.analysis.command.locale.LocaleResources; @@ -89,9 +89,9 @@ renderer.printLine(COLUMN_NAMES); - ServiceReference hostDAORef = context.getServiceReference(HostInfoDAO.class.getName()); - requireNonNull(hostDAORef, translator.localize(LocaleResources.HOST_SERVICE_UNAVAILABLE)); - HostInfoDAO hostDAO = (HostInfoDAO) context.getService(hostDAORef); + ServiceReference agentDAORef = context.getServiceReference(AgentInfoDAO.class.getName()); + requireNonNull(agentDAORef, translator.localize(LocaleResources.AGENT_SERVICE_UNAVAILABLE)); + AgentInfoDAO agentDAO = (AgentInfoDAO) context.getService(agentDAORef); ServiceReference vmDAORef = context.getServiceReference(VmInfoDAO.class.getName()); requireNonNull(vmDAORef, translator.localize(LocaleResources.VM_SERVICE_UNAVAILABLE)); @@ -119,7 +119,7 @@ if (stringAgentId != null) agentId = new AgentId(stringAgentId); - Collection<AgentId> hosts = stringAgentId != null ? Arrays.asList(agentId) : hostDAO.getAgentIds(); + Collection<AgentId> hosts = stringAgentId != null ? Arrays.asList(agentId) : agentDAO.getAgentIds(); for (AgentId host : hosts) { Collection<VmId> vms = stringVmId != null ? Arrays.asList(vmId) : vmDAO.getVmIds(host); for (VmId vm : vms) { @@ -129,7 +129,7 @@ context.ungetService(heapDAORef); context.ungetService(vmDAORef); - context.ungetService(hostDAORef); + context.ungetService(agentDAORef); renderer.render(ctx.getConsole().getOutput()); }
--- a/vm-heap-analysis/command/src/test/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ListHeapDumpsCommandTest.java Mon Jun 29 16:29:08 2015 -0400 +++ b/vm-heap-analysis/command/src/test/java/com/redhat/thermostat/vm/heap/analysis/command/internal/ListHeapDumpsCommandTest.java Mon Jun 29 14:58:01 2015 -0400 @@ -54,7 +54,7 @@ import com.redhat.thermostat.common.cli.SimpleArguments; 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.AgentInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; import com.redhat.thermostat.storage.model.VmInfo; import com.redhat.thermostat.test.TestCommandContextFactory; @@ -86,20 +86,20 @@ try { command.run(factory.createContext(new SimpleArguments())); fail(); - } catch (CommandException hostDaoNotAvailableException) { - assertEquals("Unable to access host information (HostInfoDAO unavailable)", - hostDaoNotAvailableException.getMessage()); + } catch (CommandException agentDaoNotAvailableException) { + assertEquals("Unable to access agent information (AgentInfoDAO unavailable)", + agentDaoNotAvailableException.getMessage()); } } @Test public void verifyWorksWithoutAnyInformation() throws CommandException { - HostInfoDAO hostInfo = mock(HostInfoDAO.class); + AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); VmInfoDAO vmInfo = mock(VmInfoDAO.class); HeapDAO heapDao = mock(HeapDAO.class); StubBundleContext context = new StubBundleContext(); - context.registerService(HostInfoDAO.class, hostInfo, null); + context.registerService(AgentInfoDAO.class, agentInfoDAO, null); context.registerService(VmInfoDAO.class, vmInfo, null); context.registerService(HeapDAO.class, heapDao, null); @@ -125,13 +125,13 @@ VmInfoDAO vmInfoDAO = mock(VmInfoDAO.class); when(vmInfoDAO.getVmIds(agentId)).thenReturn(Arrays.asList(vmId)); - HostInfoDAO hostInfo = mock(HostInfoDAO.class); - when(hostInfo.getAgentIds()).thenReturn(Arrays.asList(agentId)); + AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); + when(agentInfoDAO.getAgentIds()).thenReturn(Arrays.asList(agentId)); when(heapDao.getAllHeapInfo(agentId, vmId)).thenReturn(Arrays.asList(heapInfo)); StubBundleContext context = new StubBundleContext(); - context.registerService(HostInfoDAO.class, hostInfo, null); + context.registerService(AgentInfoDAO.class, agentInfoDAO, null); context.registerService(VmInfoDAO.class, vmInfoDAO, null); context.registerService(HeapDAO.class, heapDao, null); @@ -164,14 +164,14 @@ VmInfoDAO vmInfo = mock(VmInfoDAO.class); when(vmInfo.getVmIds(agentId1)).thenReturn(Arrays.asList(vmId1)).thenReturn(Arrays.asList(vmId2)); - HostInfoDAO hostInfo = mock(HostInfoDAO.class); - when(hostInfo.getAgentIds()).thenReturn(Arrays.asList(agentId1, agentId2)); + AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); + when(agentInfoDAO.getAgentIds()).thenReturn(Arrays.asList(agentId1, agentId2)); 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); + context.registerService(AgentInfoDAO.class, agentInfoDAO, null); context.registerService(VmInfoDAO.class, vmInfo, null); context.registerService(HeapDAO.class, heapDao, null); @@ -210,13 +210,13 @@ null, null,0, "myUsername"); when(vmInfoDAO.getVmInfo(vmId1)).thenReturn(vmInfo1); - HostInfoDAO hostInfo = mock(HostInfoDAO.class); - when(hostInfo.getAgentIds()).thenReturn(Arrays.asList(agentId1, agentId2)); + AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); + when(agentInfoDAO.getAgentIds()).thenReturn(Arrays.asList(agentId1, agentId2)); when(heapDao.getAllHeapInfo(agentId1, vmId1)).thenReturn(Arrays.asList(heapInfo)); StubBundleContext context = new StubBundleContext(); - context.registerService(HostInfoDAO.class, hostInfo, null); + context.registerService(AgentInfoDAO.class, agentInfoDAO, null); context.registerService(VmInfoDAO.class, vmInfoDAO, null); context.registerService(HeapDAO.class, heapDao, null);