Mercurial > hg > release > thermostat-1.6
changeset 1967:9ae4735019dc
Perform bulk DAO queries in find-vm command to reduce amount of database access
Reviewed-by: jerboaa, jkang
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-March/017952.html
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-June/019718.html
PR3040
author | Andrew Azores <aazores@redhat.com> |
---|---|
date | Mon, 07 Mar 2016 12:01:10 -0500 |
parents | f3f9cc19b17c |
children | 22f2df5a0100 |
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 vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/internal/FindVmCommand.java vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/internal/MatchContext.java vm-find/command/src/test/java/com/redhat/thermostat/vm/find/command/internal/FindVmCommandTest.java |
diffstat | 7 files changed, 204 insertions(+), 117 deletions(-) [+] |
line wrap: on
line diff
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/dao/HostInfoDAO.java Wed Aug 19 10:11:52 2015 -0400 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/dao/HostInfoDAO.java Mon Mar 07 12:01:10 2016 -0500 @@ -37,6 +37,7 @@ package com.redhat.thermostat.storage.dao; import java.util.Collection; +import java.util.List; import com.redhat.thermostat.annotations.Service; import com.redhat.thermostat.storage.core.Category; @@ -59,6 +60,9 @@ Key.AGENT_ID, hostNameKey, osNameKey, osKernelKey, cpuCountKey, cpuModelKey, hostMemoryTotalKey); + /** @return information on all known hosts */ + List<HostInfo> getAllHostInfos(); + /** * * @param ref The host ref for which to get the HostInfo object for.
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/dao/VmInfoDAO.java Wed Aug 19 10:11:52 2015 -0400 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/dao/VmInfoDAO.java Mon Mar 07 12:01:10 2016 -0500 @@ -77,6 +77,9 @@ startTimeKey, stopTimeKey, uidKey, usernameKey); + /** @return information on all known VMs */ + List<VmInfo> getAllVmInfos(); + VmInfo getVmInfo(VmId id); VmInfo getVmInfo(VmRef ref);
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOImpl.java Wed Aug 19 10:11:52 2015 -0400 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/HostInfoDAOImpl.java Mon Mar 07 12:01:10 2016 -0500 @@ -168,6 +168,19 @@ } @Override + public List<HostInfo> getAllHostInfos() { + Cursor<HostInfo> cursor = getAllHostInfoCursor(); + if (cursor == null) { + return Collections.emptyList(); + } + List<HostInfo> result = new ArrayList<>(); + while (cursor.hasNext()) { + result.add(cursor.next()); + } + return result; + } + + @Override public Collection<HostRef> getAliveHosts() { List<HostRef> hosts = new ArrayList<>(); List<AgentInformation> agentInfos = agentInfoDao.getAliveAgents();
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/VmInfoDAOImpl.java Wed Aug 19 10:11:52 2015 -0400 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/VmInfoDAOImpl.java Mon Mar 07 12:01:10 2016 -0500 @@ -64,7 +64,7 @@ public class VmInfoDAOImpl extends BaseCountable implements VmInfoDAO { private final Logger logger = LoggingUtils.getLogger(VmInfoDAOImpl.class); - static final String QUERY_VM_INFO = "QUERY " + static final String QUERY_VM_INFO = "QUERY " + vmInfoCategory.getName() + " WHERE '" + Key.AGENT_ID.getName() + "' = ?s AND '" + Key.VM_ID.getName() + "' = ?s LIMIT 1"; @@ -133,6 +133,33 @@ } @Override + public List<VmInfo> getAllVmInfos() { + List<VmInfo> result = new ArrayList<>(); + + StatementDescriptor<VmInfo> desc = new StatementDescriptor<>(vmInfoCategory, QUERY_ALL_VMS); + PreparedStatement<VmInfo> stmt; + + Cursor<VmInfo> cursor; + try { + stmt = storage.prepareStatement(desc); + + cursor = stmt.executeQuery(); + while (cursor.hasNext()) { + result.add(cursor.next()); + } + + } catch (DescriptorParsingException e) { + // should not happen, but if it *does* happen, at least log it + logger.log(Level.SEVERE, "Preparing query '" + desc + "' failed!", e); + + } 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 result; + } + + @Override public VmInfo getVmInfo(VmId id) { VmInfo result = null;
--- a/vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/internal/FindVmCommand.java Wed Aug 19 10:11:52 2015 -0400 +++ b/vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/internal/FindVmCommand.java Mon Mar 07 12:01:10 2016 -0500 @@ -42,7 +42,6 @@ import com.redhat.thermostat.common.cli.CommandException; 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.dao.AgentInfoDAO; import com.redhat.thermostat.storage.dao.HostInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; @@ -72,7 +71,10 @@ private HostInfoDAO hostInfoDAO; private VmInfoDAO vmInfoDAO; private CountDownLatch servicesLatch = new CountDownLatch(3); - + + private final Map<HostRef, List<VmInfo>> hostVmsMap = new HashMap<>(); + private final Map<HostRef, HostInfo> hostInfoMap = new HashMap<>(); + @Override public void run(CommandContext ctx) throws CommandException { try { @@ -85,8 +87,10 @@ requireNonNull(hostInfoDAO, translator.localize(LocaleResources.HOST_SERVICE_UNAVAILABLE)); requireNonNull(vmInfoDAO, translator.localize(LocaleResources.VM_SERVICE_UNAVAILABLE)); + initDaoData(); + Arguments arguments = ctx.getArguments(); - List<AgentInformation> agentsToSearch = getAgentsToSearch(arguments, agentInfoDAO); + List<AgentInformation> agentsToSearch = getAgentsToSearch(arguments); Map<String, String> hostCriteria = getHostCriteria(arguments); Map<String, String> vmCriteria = getVmCriteria(arguments); @@ -95,20 +99,39 @@ HostMatcher hostMatcher = new HostMatcher(hostCriteria); VmMatcher vmMatcher = new VmMatcher(vmCriteria); - List<MatchContext> results = performSearch(hostInfoDAO, vmInfoDAO, - agentsToSearch, hostMatcher, vmMatcher); + List<MatchContext> results = performSearch(agentsToSearch, hostMatcher, vmMatcher); ResultsRenderer resultsRenderer = new ResultsRenderer(arguments); resultsRenderer.print(ctx.getConsole().getOutput(), results); } - static List<AgentInformation> getAgentsToSearch(Arguments arguments, AgentInfoDAO agentInfoDAO) throws CommandException { + void initDaoData() { + hostVmsMap.clear(); + hostInfoMap.clear(); + + List<VmInfo> allVms = vmInfoDAO.getAllVmInfos(); + List<HostInfo> allHosts = hostInfoDAO.getAllHostInfos(); + + for (HostInfo host : allHosts) { + HostRef ref = getHostRefForAgentInformation(host.getAgentId()); + hostInfoMap.put(ref, host); + } + for (VmInfo vm : allVms) { + HostRef hostRef = getHostRefForAgentInformation(vm.getAgentId()); + if (!hostVmsMap.containsKey(hostRef)) { + hostVmsMap.put(hostRef, new ArrayList<VmInfo>()); + } + hostVmsMap.get(hostRef).add(vm); + } + } + + List<AgentInformation> getAgentsToSearch(Arguments arguments) throws CommandException { validateAgentStatusArguments(arguments); List<AgentInformation> aliveAgents; if (arguments.hasArgument(AGENT_ID_ARGUMENT)) { String agentId = arguments.getArgument(AGENT_ID_ARGUMENT); - aliveAgents = Collections.singletonList(agentInfoDAO.getAgentInformation(new HostRef(agentId, "dummy"))); + aliveAgents = Collections.singletonList(agentInfoDAO.getAgentInformation(getHostRefForAgentInformation(agentId))); } else if (arguments.hasArgument(ALIVE_AGENTS_ONLY_ARGUMENT)) { aliveAgents = agentInfoDAO.getAliveAgents(); } else { @@ -151,12 +174,11 @@ return vmCriteria; } - static List<MatchContext> performSearch(HostInfoDAO hostInfoDAO, VmInfoDAO vmInfoDAO, - Iterable<AgentInformation> agents, HostMatcher hostMatcher, VmMatcher vmMatcher) throws UnrecognizedArgumentException { + List<MatchContext> performSearch(Iterable<AgentInformation> agents, HostMatcher hostMatcher, VmMatcher vmMatcher) throws UnrecognizedArgumentException { List<MatchContext> matchContexts = new ArrayList<>(); - for (AgentInformation agentInformation : filterAgents(hostInfoDAO, agents, hostMatcher)) { - HostInfo hostInfo = getHostInfo(hostInfoDAO, agentInformation); - List<VmInfo> matchingVms = getMatchingVms(vmInfoDAO, agentInformation, hostInfo, vmMatcher); + for (AgentInformation agentInformation : filterAgents(agents, hostMatcher)) { + HostInfo hostInfo = getHostInfo(agentInformation); + List<VmInfo> matchingVms = getMatchingVms(agentInformation, hostInfo, vmMatcher); for (VmInfo vm : matchingVms) { MatchContext context = MatchContext.builder() .agentInfo(agentInformation) @@ -169,10 +191,10 @@ return matchContexts; } - static List<AgentInformation> filterAgents(HostInfoDAO hostInfoDAO, Iterable<AgentInformation> agents, HostMatcher hostMatcher) throws UnrecognizedArgumentException { + List<AgentInformation> filterAgents(Iterable<AgentInformation> agents, HostMatcher hostMatcher) throws UnrecognizedArgumentException { List<AgentInformation> list = new ArrayList<>(); for (AgentInformation agent : agents) { - HostInfo hostInfo = hostInfoDAO.getHostInfo(new HostRef(agent.getAgentId(), "dummy")); + HostInfo hostInfo = getHostInfo(agent); MatchContext context = MatchContext.builder() .agentInfo(agent) .hostInfo(hostInfo) @@ -184,14 +206,17 @@ return list; } - static HostInfo getHostInfo(HostInfoDAO hostInfoDAO, AgentInformation agentInformation) { - return hostInfoDAO.getHostInfo(new HostRef(agentInformation.getAgentId(), "dummy")); + private HostInfo getHostInfo(AgentInformation agentInformation) { + return hostInfoMap.get(getHostRefForAgentInformation(agentInformation.getAgentId())); } - static List<VmInfo> getMatchingVms(VmInfoDAO vmInfoDAO, AgentInformation agent, HostInfo hostInfo, VmMatcher vmMatcher) throws UnrecognizedArgumentException { + private HostRef getHostRefForAgentInformation(String agentId) { + return new HostRef(agentId, "dummy"); + } + + List<VmInfo> getMatchingVms(AgentInformation agent, HostInfo hostInfo, VmMatcher vmMatcher) throws UnrecognizedArgumentException { List<VmInfo> list = new ArrayList<>(); - for (VmRef vmRef : vmInfoDAO.getVMs(new HostRef(agent.getAgentId(), "dummy"))) { - VmInfo vmInfo = vmInfoDAO.getVmInfo(vmRef); + for (VmInfo vmInfo : getVmInfos(agent.getAgentId())) { MatchContext context = MatchContext.builder() .agentInfo(agent) .hostInfo(hostInfo) @@ -204,6 +229,14 @@ return list; } + List<VmInfo> getVmInfos(String agentId) { + return getVmInfos(getHostRefForAgentInformation(agentId)); + } + + List<VmInfo> getVmInfos(HostRef hostRef) { + return hostVmsMap.get(hostRef); + } + public void setAgentInfoDAO(AgentInfoDAO agentInfoDAO) { this.agentInfoDAO = agentInfoDAO; servicesLatch.countDown();
--- a/vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/internal/MatchContext.java Wed Aug 19 10:11:52 2015 -0400 +++ b/vm-find/command/src/main/java/com/redhat/thermostat/vm/find/command/internal/MatchContext.java Mon Mar 07 12:01:10 2016 -0500 @@ -83,6 +83,15 @@ return Objects.hash(hostInfo, agentInfo, vmInfo); } + @Override + public String toString() { + return "MatchContext{" + + "hostInfo=" + hostInfo + + ", agentInfo=" + agentInfo + + ", vmInfo=" + vmInfo + + '}'; + } + public static Builder builder() { return new Builder(); }
--- a/vm-find/command/src/test/java/com/redhat/thermostat/vm/find/command/internal/FindVmCommandTest.java Wed Aug 19 10:11:52 2015 -0400 +++ b/vm-find/command/src/test/java/com/redhat/thermostat/vm/find/command/internal/FindVmCommandTest.java Mon Mar 07 12:01:10 2016 -0500 @@ -74,15 +74,28 @@ public class FindVmCommandTest { private Arguments args; + private VmInfoDAO vmInfoDAO; + private HostInfoDAO hostInfoDAO; + private AgentInfoDAO agentInfoDAO; + private FindVmCommand command; @Before public void setup() { args = mock(Arguments.class); + vmInfoDAO = mock(VmInfoDAO.class); + when(vmInfoDAO.getAllVmInfos()).thenReturn(Collections.<VmInfo>emptyList()); + hostInfoDAO = mock(HostInfoDAO.class); + when(hostInfoDAO.getAllHostInfos()).thenReturn(Collections.<HostInfo>emptyList()); + agentInfoDAO = mock(AgentInfoDAO.class); + command = new FindVmCommand(); + command.setHostInfoDAO(hostInfoDAO); + command.setVmInfoDAO(vmInfoDAO); + command.setAgentInfoDAO(agentInfoDAO); } @Test public void testCommandFailsWhenDaosUnavailable() { - FindVmCommand command = new FindVmCommand(); + command.servicesUnavailable(); try { command.run(new TestCommandContextFactory().createContext(args)); fail("should have received CommandException"); @@ -95,7 +108,7 @@ @Test public void testCommandFailsWhenAgentInfoDaoUnavailable() { - FindVmCommand command = new FindVmCommand(); + command.servicesUnavailable(); command.setVmInfoDAO(mock(VmInfoDAO.class)); command.setHostInfoDAO(mock(HostInfoDAO.class)); try { @@ -108,7 +121,7 @@ @Test public void testCommandFailsWhenHostInfoDaoUnavailable() { - FindVmCommand command = new FindVmCommand(); + command.servicesUnavailable(); command.setAgentInfoDAO(mock(AgentInfoDAO.class)); command.setVmInfoDAO(mock(VmInfoDAO.class)); try { @@ -121,7 +134,7 @@ @Test public void testCommandFailsWhenVmInfoDaoUnavailable() { - FindVmCommand command = new FindVmCommand(); + command.servicesUnavailable(); command.setAgentInfoDAO(mock(AgentInfoDAO.class)); command.setHostInfoDAO(mock(HostInfoDAO.class)); try { @@ -134,10 +147,6 @@ @Test public void testCommandFailsWhenNoCriteriaSupplied() { - FindVmCommand command = new FindVmCommand(); - command.setAgentInfoDAO(mock(AgentInfoDAO.class)); - command.setHostInfoDAO(mock(HostInfoDAO.class)); - command.setVmInfoDAO(mock(VmInfoDAO.class)); try { command.run(new TestCommandContextFactory().createContext(args)); fail("should have received CommandException"); @@ -159,23 +168,20 @@ HostInfo hostInfo = new HostInfo(); hostInfo.setHostname("foo-host"); + hostInfo.setAgentId(agent.getAgentId()); - VmInfoDAO vmInfoDAO = mock(VmInfoDAO.class); + when(vmInfoDAO.getAllVmInfos()).thenReturn(Collections.singletonList(vmInfo)); when(vmInfoDAO.getVmInfo(any(VmRef.class))).thenReturn(vmInfo); when(vmInfoDAO.getVMs(any(HostRef.class))).thenReturn(Collections.singleton(new VmRef(new HostRef(agent.getAgentId(), "dummy"), vmInfo))); - HostInfoDAO hostInfoDAO = mock(HostInfoDAO.class); + when(hostInfoDAO.getAllHostInfos()).thenReturn(Collections.singletonList(hostInfo)); when(hostInfoDAO.getHostInfo(any(HostRef.class))).thenReturn(hostInfo); - AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); when(agentInfoDAO.getAllAgentInformation()).thenReturn(Collections.singletonList(agent)); + when(agentInfoDAO.getAliveAgents()).thenReturn(Collections.singletonList(agent)); when(agentInfoDAO.getAgentInformation(any(HostRef.class))).thenReturn(agent); TestCommandContextFactory testCommandContextFactory = new TestCommandContextFactory(); - FindVmCommand command = new FindVmCommand(); - command.setVmInfoDAO(vmInfoDAO); - command.setAgentInfoDAO(agentInfoDAO); - command.setHostInfoDAO(hostInfoDAO); command.run(testCommandContextFactory.createContext(args)); String output = testCommandContextFactory.getOutput(); @@ -195,23 +201,20 @@ vmInfo.setUsername("foo-user"); HostInfo hostInfo = new HostInfo(); + hostInfo.setAgentId("foo-agent"); - VmInfoDAO vmInfoDAO = mock(VmInfoDAO.class); + when(vmInfoDAO.getAllVmInfos()).thenReturn(Collections.singletonList(vmInfo)); when(vmInfoDAO.getVmInfo(any(VmRef.class))).thenReturn(vmInfo); when(vmInfoDAO.getVMs(any(HostRef.class))).thenReturn(Collections.singleton(new VmRef(new HostRef(agent.getAgentId(), "dummy"), vmInfo))); - HostInfoDAO hostInfoDAO = mock(HostInfoDAO.class); + when(hostInfoDAO.getAllHostInfos()).thenReturn(Collections.singletonList(hostInfo)); when(hostInfoDAO.getHostInfo(any(HostRef.class))).thenReturn(hostInfo); - AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); + when(agentInfoDAO.getAliveAgents()).thenReturn(Collections.singletonList(agent)); when(agentInfoDAO.getAllAgentInformation()).thenReturn(Collections.singletonList(agent)); when(agentInfoDAO.getAgentInformation(any(HostRef.class))).thenReturn(agent); TestCommandContextFactory testCommandContextFactory = new TestCommandContextFactory(); - FindVmCommand command = new FindVmCommand(); - command.setVmInfoDAO(vmInfoDAO); - command.setAgentInfoDAO(agentInfoDAO); - command.setHostInfoDAO(hostInfoDAO); command.run(testCommandContextFactory.createContext(args)); String output = testCommandContextFactory.getOutput(); @@ -221,10 +224,10 @@ @Test public void testGetAgentsToSearchWithoutAgentArgs() throws CommandException { List<AgentInformation> list = Arrays.asList(mock(AgentInformation.class), mock(AgentInformation.class)); - AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); when(agentInfoDAO.getAllAgentInformation()).thenReturn(list); - List<AgentInformation> agentsToSearch = FindVmCommand.getAgentsToSearch(args, agentInfoDAO); + command.initDaoData(); + List<AgentInformation> agentsToSearch = command.getAgentsToSearch(args); assertThat(agentsToSearch, is(equalTo(list))); } @@ -235,7 +238,6 @@ final AgentInformation bar = new AgentInformation("bar"); bar.setAlive(true); List<AgentInformation> list = Arrays.asList(foo, bar); - AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); when(agentInfoDAO.getAliveAgents()).thenReturn(list); when(agentInfoDAO.getAgentInformation(isA(HostRef.class))).thenAnswer(new Answer<AgentInformation>() { @Override @@ -250,8 +252,9 @@ }); when(args.hasArgument(FindVmCommand.AGENT_ID_ARGUMENT)).thenReturn(true); when(args.getArgument(FindVmCommand.AGENT_ID_ARGUMENT)).thenReturn(foo.getAgentId()); - List<AgentInformation> agentsToSearch = FindVmCommand.getAgentsToSearch(args, agentInfoDAO); + command.initDaoData(); + List<AgentInformation> agentsToSearch = command.getAgentsToSearch(args); assertThat(agentsToSearch, is(equalTo(Collections.singletonList(foo)))); } @@ -261,12 +264,13 @@ foo.setAlive(false); AgentInformation bar = new AgentInformation("bar"); bar.setAlive(true); - AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); when(agentInfoDAO.getAliveAgents()).thenReturn(Collections.singletonList(bar)); when(agentInfoDAO.getAgentInformation(new HostRef("foo", "dummy"))).thenReturn(foo); when(agentInfoDAO.getAgentInformation(new HostRef("bar", "dummy"))).thenReturn(bar); when(args.hasArgument(FindVmCommand.ALIVE_AGENTS_ONLY_ARGUMENT)).thenReturn(true); - List<AgentInformation> agentsToSearch = FindVmCommand.getAgentsToSearch(args, agentInfoDAO); + + command.initDaoData(); + List<AgentInformation> agentsToSearch = command.getAgentsToSearch(args); assertThat(agentsToSearch, is(equalTo(Collections.singletonList(bar)))); } @@ -275,15 +279,7 @@ when(args.hasArgument(FindVmCommand.AGENT_ID_ARGUMENT)).thenReturn(true); when(args.hasArgument(FindVmCommand.ALIVE_AGENTS_ONLY_ARGUMENT)).thenReturn(true); - VmInfoDAO vmInfoDAO = mock(VmInfoDAO.class); - HostInfoDAO hostInfoDAO = mock(HostInfoDAO.class); - AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); - TestCommandContextFactory testCommandContextFactory = new TestCommandContextFactory(); - FindVmCommand command = new FindVmCommand(); - command.setVmInfoDAO(vmInfoDAO); - command.setAgentInfoDAO(agentInfoDAO); - command.setHostInfoDAO(hostInfoDAO); command.run(testCommandContextFactory.createContext(args)); } @@ -408,7 +404,7 @@ } @Test - public void testPerformSearch() throws UnrecognizedArgumentException { + public void testPerformSearch() throws CommandException { AgentInformation agentInfo1 = new AgentInformation("agentInfo1"); HostRef hostRef1 = new HostRef(agentInfo1.getAgentId(), "dummy"); AgentInformation agentInfo2 = new AgentInformation("agentInfo2"); @@ -417,28 +413,36 @@ HostRef hostRef3 = new HostRef(agentInfo3.getAgentId(), "dummy"); List<AgentInformation> agents = Arrays.asList(agentInfo1, agentInfo2, agentInfo3); - HostInfo host1 = mock(HostInfo.class); - when(host1.getHostname()).thenReturn("foo-host"); - HostInfo host2 = host1; - HostInfo host3 = mock(HostInfo.class); - when(host3.getHostname()).thenReturn("bar-host"); + HostInfo host1 = new HostInfo(); + host1.setHostname("foo-host"); + host1.setAgentId(agentInfo1.getAgentId()); + HostInfo host2 = new HostInfo(); + host2.setHostname("foo-host"); + host2.setAgentId(agentInfo2.getAgentId()); + HostInfo host3 = new HostInfo(); + host3.setHostname("baz-host"); + host3.setAgentId(agentInfo3.getAgentId()); - VmInfo vm1 = mock(VmInfo.class); - when(vm1.getVmId()).thenReturn("vm1"); - when(vm1.getJavaVersion()).thenReturn("1.8"); - when(vm1.getMainClass()).thenReturn("foo-class"); - VmInfo vm2 = mock(VmInfo.class); - when(vm2.getVmId()).thenReturn("vm2"); - when(vm2.getJavaVersion()).thenReturn("1.8"); - when(vm2.getMainClass()).thenReturn("bar-class"); - VmInfo vm3 = mock(VmInfo.class); - when(vm3.getVmId()).thenReturn("vm3"); - when(vm3.getJavaVersion()).thenReturn("1.7"); - when(vm3.getMainClass()).thenReturn("baz-class"); - VmInfo vm4 = mock(VmInfo.class); - when(vm4.getVmId()).thenReturn("vm3"); - when(vm4.getJavaVersion()).thenReturn("1.6"); - when(vm4.getMainClass()).thenReturn("boz-class"); + VmInfo vm1 = new VmInfo(); + vm1.setVmId("vm1"); + vm1.setJavaVersion("1.8"); + vm1.setMainClass("foo-class"); + vm1.setAgentId(agentInfo1.getAgentId()); + VmInfo vm2 = new VmInfo(); + vm2.setVmId("vm2"); + vm2.setJavaVersion("1.8"); + vm2.setMainClass("bar-class"); + vm2.setAgentId(agentInfo1.getAgentId()); + VmInfo vm3 = new VmInfo(); + vm3.setVmId("vm3"); + vm3.setJavaVersion("1.7"); + vm3.setMainClass("baz-class"); + vm3.setAgentId(agentInfo2.getAgentId()); + VmInfo vm4 = new VmInfo(); + vm4.setVmId("vm4"); + vm4.setJavaVersion("1.6"); + vm4.setMainClass("boz-class"); + vm4.setAgentId(agentInfo3.getAgentId()); VmRef vmRef1 = new VmRef(hostRef1, vm1); VmRef vmRef2 = new VmRef(hostRef1, vm2); @@ -469,22 +473,18 @@ .vmInfo(vm4) .build(); - FindVmCommand command = new FindVmCommand(); - - AgentInfoDAO agentInfoDAO = mock(AgentInfoDAO.class); + when(agentInfoDAO.getAllAgentInformation()).thenReturn(agents); when(agentInfoDAO.getAliveAgents()).thenReturn(agents); when(agentInfoDAO.getAgentInformation(hostRef1)).thenReturn(agentInfo1); when(agentInfoDAO.getAgentInformation(hostRef2)).thenReturn(agentInfo2); when(agentInfoDAO.getAgentInformation(hostRef3)).thenReturn(agentInfo3); - command.setAgentInfoDAO(agentInfoDAO); - HostInfoDAO hostInfoDAO = mock(HostInfoDAO.class); + when(hostInfoDAO.getAllHostInfos()).thenReturn(Arrays.asList(host1, host2, host3)); when(hostInfoDAO.getHostInfo(hostRef1)).thenReturn(host1); when(hostInfoDAO.getHostInfo(hostRef2)).thenReturn(host2); when(hostInfoDAO.getHostInfo(hostRef3)).thenReturn(host3); - command.setHostInfoDAO(hostInfoDAO); - VmInfoDAO vmInfoDAO = mock(VmInfoDAO.class); + when(vmInfoDAO.getAllVmInfos()).thenReturn(Arrays.asList(vm1, vm2, vm3, vm4)); when(vmInfoDAO.getVMs(hostRef1)).thenReturn(vmRefs1); when(vmInfoDAO.getVMs(hostRef2)).thenReturn(vmRefs2); when(vmInfoDAO.getVMs(hostRef3)).thenReturn(vmRefs3); @@ -492,14 +492,14 @@ when(vmInfoDAO.getVmInfo(vmRef2)).thenReturn(vm2); when(vmInfoDAO.getVmInfo(vmRef3)).thenReturn(vm3); when(vmInfoDAO.getVmInfo(vmRef4)).thenReturn(vm4); - command.setVmInfoDAO(vmInfoDAO); + + command.initDaoData(); Map<String, String> hostMap1 = new HashMap<>(); HostMatcher hostMatcher1 = new HostMatcher(hostMap1); Map<String, String> vmMap1 = new HashMap<>(); VmMatcher vmMatcher1 = new VmMatcher(vmMap1); - List<MatchContext> result1 = FindVmCommand.performSearch(hostInfoDAO, vmInfoDAO, - agents, hostMatcher1, vmMatcher1); + List<MatchContext> result1 = command.performSearch(agents, hostMatcher1, vmMatcher1); assertThat(result1.size(), is(4)); for (MatchContext context : Arrays.asList(context1, context2, context3, context4)) { assertThat(result1.contains(context), is(true)); @@ -511,21 +511,16 @@ Map<String, String> vmMap2 = new HashMap<>(); vmMap2.put("javaversion", "1.8"); VmMatcher vmMatcher2 = new VmMatcher(vmMap2); - List<MatchContext> result2 = FindVmCommand.performSearch(hostInfoDAO, vmInfoDAO, - agents, hostMatcher2, vmMatcher2); - assertThat(result2.size(), is(0)); + List<MatchContext> result2 = command.performSearch(agents, hostMatcher2, vmMatcher2); + assertThat(result2, is(equalTo(Collections.<MatchContext>emptyList()))); Map<String, String> hostMap3 = new HashMap<>(); hostMap3.put("hostname", "foo-host"); HostMatcher hostMatcher3 = new HostMatcher(hostMap3); Map<String, String> vmMap3 = new HashMap<>(); VmMatcher vmMatcher3 = new VmMatcher(vmMap3); - List<MatchContext> result3 = FindVmCommand.performSearch(hostInfoDAO, vmInfoDAO, - agents, hostMatcher3, vmMatcher3); - assertThat(result3.size(), is(3)); - for (MatchContext context : Arrays.asList(context1, context2, context3)) { - assertThat(result3.contains(context), is(true)); - } + List<MatchContext> result3 = command.performSearch(agents, hostMatcher3, vmMatcher3); + assertThat(new HashSet<>(result3), is(equalTo(new HashSet<>(Arrays.asList(context1, context2, context3))))); Map<String, String> hostMap4 = new HashMap<>(); hostMap3.put("hostname", "foo-host"); @@ -533,8 +528,7 @@ Map<String, String> vmMap4 = new HashMap<>(); vmMap4.put("javaversion", "1.8"); VmMatcher vmMatcher4 = new VmMatcher(vmMap4); - List<MatchContext> result4 = FindVmCommand.performSearch(hostInfoDAO, vmInfoDAO, - agents, hostMatcher4, vmMatcher4); + List<MatchContext> result4 = command.performSearch(agents, hostMatcher4, vmMatcher4); assertThat(result4.size(), is(2)); for (MatchContext context : Arrays.asList(context1, context2)) { assertThat(result4.contains(context), is(true)); @@ -547,32 +541,36 @@ HostRef fooRef = new HostRef(foo.getAgentId(), "dummy"); AgentInformation bar = new AgentInformation("bar"); HostRef barRef = new HostRef(bar.getAgentId(), "dummy"); - HostInfo hostInfo1 = mock(HostInfo.class); - when(hostInfo1.getHostname()).thenReturn("foo-host"); - HostInfo hostInfo2 = mock(HostInfo.class); - when(hostInfo2.getHostname()).thenReturn("bar-host"); - HostInfoDAO hostInfoDAO = mock(HostInfoDAO.class); + + HostInfo hostInfo1 = new HostInfo(); + hostInfo1.setHostname("foo-host"); + hostInfo1.setAgentId(foo.getAgentId()); + + HostInfo hostInfo2 = new HostInfo(); + hostInfo2.setHostname("bar-host"); + hostInfo2.setAgentId(bar.getAgentId()); + + when(hostInfoDAO.getAllHostInfos()).thenReturn(Arrays.asList(hostInfo1, hostInfo2)); when(hostInfoDAO.getHostInfo(fooRef)).thenReturn(hostInfo1); when(hostInfoDAO.getHostInfo(barRef)).thenReturn(hostInfo2); List<AgentInformation> agents = Arrays.asList(foo, bar); - FindVmCommand command = new FindVmCommand(); - command.setHostInfoDAO(hostInfoDAO); + command.initDaoData(); HostMatcher allMatcher = new HostMatcher(Collections.<String, String>emptyMap()); - List<AgentInformation> all = FindVmCommand.filterAgents(hostInfoDAO, agents, allMatcher); + List<AgentInformation> all = command.filterAgents(agents, allMatcher); assertThat(all, is(equalTo(agents))); Map<String, String> noneMap = new HashMap<>(); noneMap.put("hostname", "none-host"); HostMatcher noneMatcher = new HostMatcher(noneMap); - List<AgentInformation> none = FindVmCommand.filterAgents(hostInfoDAO, agents, noneMatcher); + List<AgentInformation> none = command.filterAgents(agents, noneMatcher); assertThat(none, is(equalTo(Collections.<AgentInformation>emptyList()))); Map<String, String> fooMap = new HashMap<>(); fooMap.put("hostname", "foo-host"); HostMatcher fooMatcher = new HostMatcher(fooMap); - List<AgentInformation> fooList = FindVmCommand.filterAgents(hostInfoDAO, agents, fooMatcher); + List<AgentInformation> fooList = command.filterAgents(agents, fooMatcher); assertThat(fooList, is(equalTo(Collections.singletonList(foo)))); } @@ -603,6 +601,7 @@ VmRef vmRef3 = new VmRef(hostRef, vmInfo3); HostInfo hostInfo = new HostInfo(); + hostInfo.setAgentId(agent.getAgentId()); hostInfo.setHostname("foo-host"); hostInfo.setOsKernel("linux"); hostInfo.setAgentId("foo-agent"); @@ -611,39 +610,38 @@ hostInfo.setCpuModel("foo-cpu"); hostInfo.setTotalMemory(4096l); - HostInfoDAO hostInfoDAO = mock(HostInfoDAO.class); + when(hostInfoDAO.getAllHostInfos()).thenReturn(Collections.singletonList(hostInfo)); when(hostInfoDAO.getAliveHosts()).thenReturn(Collections.singleton(hostRef)); when(hostInfoDAO.getAliveHosts()).thenReturn(Collections.singleton(hostRef)); - VmInfoDAO vmInfoDAO = mock(VmInfoDAO.class); + when(vmInfoDAO.getAllVmInfos()).thenReturn(Arrays.asList(vmInfo1, vmInfo2, vmInfo3)); when(vmInfoDAO.getVMs(hostRef)).thenReturn(new HashSet<>(Arrays.asList(vmRef1, vmRef2, vmRef3))); when(vmInfoDAO.getVmInfo(vmRef1)).thenReturn(vmInfo1); when(vmInfoDAO.getVmInfo(vmRef2)).thenReturn(vmInfo2); when(vmInfoDAO.getVmInfo(vmRef3)).thenReturn(vmInfo3); - FindVmCommand command = new FindVmCommand(); - command.setVmInfoDAO(vmInfoDAO); + command.initDaoData(); VmMatcher allMatcher = new VmMatcher(Collections.<String, String>emptyMap()); - List<VmInfo> all = FindVmCommand.getMatchingVms(vmInfoDAO, agent, hostInfo, allMatcher); + List<VmInfo> all = command.getMatchingVms(agent, hostInfo, allMatcher); assertThat(all, is(equalTo(Arrays.asList(vmInfo1, vmInfo2, vmInfo3)))); Map<String, String> userMap = new HashMap<>(); userMap.put("username", "foo-user"); VmMatcher userMatcher = new VmMatcher(userMap); - List<VmInfo> users = FindVmCommand.getMatchingVms(vmInfoDAO, agent, hostInfo, userMatcher); + List<VmInfo> users = command.getMatchingVms(agent, hostInfo, userMatcher); assertThat(users, is(equalTo(Collections.singletonList(vmInfo1)))); Map<String, String> versionMap = new HashMap<>(); versionMap.put("javaversion", "1.8"); VmMatcher versionMatcher = new VmMatcher(versionMap); - List<VmInfo> versions = FindVmCommand.getMatchingVms(vmInfoDAO, agent, hostInfo, versionMatcher); + List<VmInfo> versions = command.getMatchingVms(agent, hostInfo, versionMatcher); assertThat(versions, is(equalTo(Arrays.asList(vmInfo1, vmInfo2)))); Map<String, String> noneMap = new HashMap<>(); noneMap.put("javaversion", "1.0"); VmMatcher noneMatcher = new VmMatcher(noneMap); - List<VmInfo> none = FindVmCommand.getMatchingVms(vmInfoDAO, agent, hostInfo, noneMatcher); + List<VmInfo> none = command.getMatchingVms(agent, hostInfo, noneMatcher); assertThat(none, is(equalTo(Collections.<VmInfo>emptyList()))); }