Mercurial > hg > thermostat-ng > agent
changeset 1746:6e836359b658
Remove requirement for hostId option for kill-vm
Introduce an AgentId class represent agent ids. Update kill-vm to use that.
Deprecate AgentInfoDAO.getAgentInformation(HostRef agentRef) and prefer the
AgentId-based variant.
PR2379
Reviewed-by: omajid, jerboaa
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2015-May/013766.html
line wrap: on
line diff
--- a/killvm/client-swing/src/main/java/com/redhat/thermostat/killvm/client/internal/KillVMAction.java Fri May 29 18:38:40 2015 +0200 +++ b/killvm/client-swing/src/main/java/com/redhat/thermostat/killvm/client/internal/KillVMAction.java Thu May 28 15:07:23 2015 -0400 @@ -49,6 +49,7 @@ import com.redhat.thermostat.killvm.common.KillVMRequest; import com.redhat.thermostat.shared.locale.LocalizedString; import com.redhat.thermostat.shared.locale.Translate; +import com.redhat.thermostat.storage.core.AgentId; import com.redhat.thermostat.storage.core.Ref; import com.redhat.thermostat.storage.core.VmRef; import com.redhat.thermostat.storage.dao.AgentInfoDAO; @@ -95,7 +96,8 @@ } VmRef reference = (VmRef) ref; - request.sendKillVMRequestToAgent(reference, agentDao, listener); + request.sendKillVMRequestToAgent(new AgentId(reference.getHostRef().getAgentId()), reference.getPid(), + agentDao, listener); } @Override
--- a/killvm/client-swing/src/test/java/com/redhat/thermostat/killvm/client/internal/KillVMActionTest.java Fri May 29 18:38:40 2015 +0200 +++ b/killvm/client-swing/src/test/java/com/redhat/thermostat/killvm/client/internal/KillVMActionTest.java Thu May 28 15:07:23 2015 -0400 @@ -49,6 +49,7 @@ import com.redhat.thermostat.common.Filter; import com.redhat.thermostat.common.command.RequestResponseListener; import com.redhat.thermostat.killvm.common.KillVMRequest; +import com.redhat.thermostat.storage.core.AgentId; import com.redhat.thermostat.storage.core.HostRef; import com.redhat.thermostat.storage.core.Ref; import com.redhat.thermostat.storage.core.VmRef; @@ -115,6 +116,14 @@ AgentInfoDAO agentDao = mock(AgentInfoDAO.class); VmInfoDAO vmInfoDao = mock(VmInfoDAO.class); + HostRef hostRef = mock(HostRef.class); + when(ref.getHostRef()).thenReturn(hostRef); + + AgentId agentId = new AgentId("a1b2c3"); + when(hostRef.getAgentId()).thenReturn(agentId.get()); + + int pid = 0; + RequestResponseListener agentResponseListener = mock(RequestResponseListener.class); KillVMRequest request = mock(KillVMRequest.class); @@ -128,7 +137,7 @@ complete[0] = true; return null; } - }).when(request).sendKillVMRequestToAgent(ref, agentDao, agentResponseListener); + }).when(request).sendKillVMRequestToAgent(agentId, pid, agentDao, agentResponseListener); action.execute(ref);
--- a/killvm/command/src/main/java/com/redhat/thermostat/killvm/command/KillVMCommand.java Fri May 29 18:38:40 2015 +0200 +++ b/killvm/command/src/main/java/com/redhat/thermostat/killvm/command/KillVMCommand.java Thu May 28 15:07:23 2015 -0400 @@ -44,23 +44,19 @@ import com.redhat.thermostat.killvm.command.locale.LocaleResources; import com.redhat.thermostat.killvm.common.KillVMRequest; import com.redhat.thermostat.shared.locale.Translate; -import com.redhat.thermostat.storage.core.HostRef; +import com.redhat.thermostat.storage.core.AgentId; +import com.redhat.thermostat.storage.core.VmId; import com.redhat.thermostat.storage.core.VmRef; import com.redhat.thermostat.storage.dao.AgentInfoDAO; -import com.redhat.thermostat.storage.dao.DAOException; -import com.redhat.thermostat.storage.dao.HostInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; import com.redhat.thermostat.storage.model.VmInfo; public class KillVMCommand extends AbstractCommand { - private static final String RECEIVER = "com.redhat.thermostat.killvm.agent.internal.KillVmReceiver"; - private static final String CMD_CHANNEL_ACTION_NAME = "killvm"; private static final Translate<LocaleResources> translator = LocaleResources.createLocalizer(); private final ShellVMKilledListener listener; - private HostInfoDAO hostInfoDAO; private VmInfoDAO vmInfoDAO; private AgentInfoDAO agentInfoDAO; private KillVMRequest request; @@ -72,33 +68,28 @@ @Override public void run(CommandContext ctx) throws CommandException { requireNonNull(vmInfoDAO, translator.localize(LocaleResources.VM_SERVICE_UNAVAILABLE)); - requireNonNull(hostInfoDAO, translator.localize(LocaleResources.HOST_SERVICE_UNAVAILABLE)); requireNonNull(agentInfoDAO, translator.localize(LocaleResources.AGENT_SERVICE_UNAVAILABLE)); requireNonNull(request, translator.localize(LocaleResources.REQUEST_SERVICE_UNAVAILABLE)); listener.setOut(ctx.getConsole().getOutput()); listener.setErr(ctx.getConsole().getError()); - HostVMArguments args = new HostVMArguments(ctx.getArguments(), true, true); + HostVMArguments args = new HostVMArguments(ctx.getArguments(), false, true); attemptToKillVM(args.getVM()); } private void attemptToKillVM(VmRef vmRef) throws CommandException { - try { - VmInfo result = vmInfoDAO.getVmInfo(vmRef); - sendKillRequest(vmRef.getHostRef(), result.getVmPid()); - } catch (DAOException e) { - //FIXME: VmInfoDaoImpl currently throws DAOException when VM is not found - //This should be changed when VmInfoDAOImpl gets fixed + VmId id = new VmId(vmRef.getVmId()); + VmInfo result = vmInfoDAO.getVmInfo(id); + if (result == null) { throw new CommandException(translator.localize(LocaleResources.VM_NOT_FOUND, vmRef.getVmId())); } - + sendKillRequest(new AgentId(result.getAgentId()), result.getVmPid()); } - private void sendKillRequest(HostRef ref, int vmPid) throws CommandException { - VmRef vmRef = new VmRef(ref, "dummy", vmPid, "dummy"); - request.sendKillVMRequestToAgent(vmRef, agentInfoDAO, listener); + private void sendKillRequest(AgentId agentId, int pid) throws CommandException { + request.sendKillVMRequestToAgent(agentId, pid, agentInfoDAO, listener); waitForListenerResponse(); } @@ -111,10 +102,6 @@ } } - public void setHostInfoDAO(HostInfoDAO hostInfoDAO) { - this.hostInfoDAO = hostInfoDAO; - } - public void setVmInfoDAO(VmInfoDAO vmInfoDAO) { this.vmInfoDAO = vmInfoDAO; }
--- a/killvm/command/src/main/java/com/redhat/thermostat/killvm/command/internal/Activator.java Fri May 29 18:38:40 2015 +0200 +++ b/killvm/command/src/main/java/com/redhat/thermostat/killvm/command/internal/Activator.java Thu May 28 15:07:23 2015 -0400 @@ -44,12 +44,11 @@ import org.osgi.framework.ServiceRegistration; import com.redhat.thermostat.client.command.RequestQueue; -import com.redhat.thermostat.killvm.common.KillVMRequest; import com.redhat.thermostat.common.MultipleServiceTracker; import com.redhat.thermostat.common.cli.Command; import com.redhat.thermostat.killvm.command.KillVMCommand; +import com.redhat.thermostat.killvm.common.KillVMRequest; import com.redhat.thermostat.storage.dao.AgentInfoDAO; -import com.redhat.thermostat.storage.dao.HostInfoDAO; import com.redhat.thermostat.storage.dao.VmInfoDAO; public class Activator implements BundleActivator { @@ -61,7 +60,6 @@ @Override public void start(final BundleContext context) throws Exception { Class<?>[] serviceDeps = new Class<?>[] { - HostInfoDAO.class, AgentInfoDAO.class, VmInfoDAO.class, KillVMRequest.class, @@ -71,13 +69,11 @@ serviceTracker = new MultipleServiceTracker(context, serviceDeps, new MultipleServiceTracker.Action() { @Override public void dependenciesAvailable(Map<String, Object> services) { - HostInfoDAO hostDAO = (HostInfoDAO) services.get(HostInfoDAO.class.getName()); AgentInfoDAO agentDao = (AgentInfoDAO) services.get(AgentInfoDAO.class.getName()); VmInfoDAO vmDao = (VmInfoDAO) services.get(VmInfoDAO.class.getName()); KillVMRequest request= (KillVMRequest) services.get(KillVMRequest.class.getName()); command.setAgentInfoDAO(agentDao); - command.setHostInfoDAO(hostDAO); command.setVmInfoDAO(vmDao); command.setKillVMRequest(request); } @@ -85,7 +81,6 @@ @Override public void dependenciesUnavailable() { command.setAgentInfoDAO(null); - command.setHostInfoDAO(null); command.setVmInfoDAO(null); command.setKillVMRequest(null); }
--- a/killvm/command/src/test/java/com/redhat/thermostat/killvm/command/KillVmCommandTest.java Fri May 29 18:38:40 2015 +0200 +++ b/killvm/command/src/test/java/com/redhat/thermostat/killvm/command/KillVmCommandTest.java Thu May 28 15:07:23 2015 -0400 @@ -50,10 +50,9 @@ import com.redhat.thermostat.common.command.RequestResponseListener; import com.redhat.thermostat.killvm.command.internal.ShellVMKilledListener; import com.redhat.thermostat.killvm.common.KillVMRequest; -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.AgentInfoDAO; -import com.redhat.thermostat.storage.dao.DAOException; -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; @@ -63,7 +62,6 @@ private TestCommandContextFactory cmdCtxFactory; private KillVMCommand cmd; - private HostInfoDAO hostInfoDAO; private VmInfoDAO vmInfoDAO; private AgentInfoDAO agentInfoDAO; @@ -74,7 +72,6 @@ public void setup() { cmdCtxFactory = new TestCommandContextFactory(); - hostInfoDAO = mock(HostInfoDAO.class); vmInfoDAO = mock(VmInfoDAO.class); agentInfoDAO = mock(AgentInfoDAO.class); @@ -87,27 +84,27 @@ @Test public void testKillLiveVM() throws CommandException, InterruptedException { String vmId = "liveVM"; - String hostId = "hostId"; VmInfo vmInfo = mock(VmInfo.class); - when(vmInfoDAO.getVmInfo(any(VmRef.class))).thenReturn(vmInfo); + when(vmInfoDAO.getVmInfo(any(VmId.class))).thenReturn(vmInfo); - CommandContext ctx = createContext(vmId, hostId); + CommandContext ctx = createContext(vmId); setServices(); cmd.run(ctx); - verify(request).sendKillVMRequestToAgent(any(VmRef.class), any(AgentInfoDAO.class), any(RequestResponseListener.class)); + verify(request).sendKillVMRequestToAgent(any(AgentId.class), any(int.class), any(AgentInfoDAO.class), any + (RequestResponseListener + .class)); } @Test(expected = CommandException.class) public void testKillNonexistentVM() throws CommandException { String vmId = "nonexistentVM"; - String hostId = "hostId"; - when(vmInfoDAO.getVmInfo(any(VmRef.class))).thenThrow(DAOException.class); + when(vmInfoDAO.getVmInfo(any(VmId.class))).thenReturn(null); - CommandContext ctx = createContext(vmId, hostId); + CommandContext ctx = createContext(vmId); setServices(); cmd.run(ctx); @@ -116,32 +113,20 @@ @Test(expected = CommandException.class) public void testNoVMArgument() throws CommandException { SimpleArguments args = new SimpleArguments(); - args.addArgument("hostId", "hostId"); CommandContext ctx = cmdCtxFactory.createContext(args); setServices(); cmd.run(ctx); } - @Test(expected = CommandException.class) - public void testNoHostArgument() throws CommandException { - SimpleArguments args = new SimpleArguments(); - args.addArgument("vmId", "vmId"); - CommandContext ctx = cmdCtxFactory.createContext(args); - - cmd.run(ctx); - } - - public CommandContext createContext(String vmId, String hostId) { + public CommandContext createContext(String vmId) { SimpleArguments args = new SimpleArguments(); args.addArgument("vmId", vmId); - args.addArgument("hostId", hostId); return cmdCtxFactory.createContext(args); } private void setServices() { cmd.setVmInfoDAO(vmInfoDAO); cmd.setAgentInfoDAO(agentInfoDAO); - cmd.setHostInfoDAO(hostInfoDAO); cmd.setKillVMRequest(request); }
--- a/killvm/common/src/main/java/com/redhat/thermostat/killvm/common/KillVMRequest.java Fri May 29 18:38:40 2015 +0200 +++ b/killvm/common/src/main/java/com/redhat/thermostat/killvm/common/KillVMRequest.java Thu May 28 15:07:23 2015 -0400 @@ -41,7 +41,7 @@ import com.redhat.thermostat.client.command.RequestQueue; import com.redhat.thermostat.common.command.Request; import com.redhat.thermostat.common.command.RequestResponseListener; -import com.redhat.thermostat.storage.core.VmRef; +import com.redhat.thermostat.storage.core.AgentId; import com.redhat.thermostat.storage.dao.AgentInfoDAO; public class KillVMRequest { @@ -54,12 +54,13 @@ this.queue = queue; } - public void sendKillVMRequestToAgent(VmRef vmRef, AgentInfoDAO agentInfoDAO, RequestResponseListener listener) { - InetSocketAddress target = agentInfoDAO.getAgentInformation(vmRef.getHostRef()).getRequestQueueAddress(); + public void sendKillVMRequestToAgent(AgentId agentId, int pid, AgentInfoDAO agentInfoDAO, RequestResponseListener + listener) { + InetSocketAddress target = agentInfoDAO.getAgentInformation(agentId).getRequestQueueAddress(); Request murderer = getKillRequest(target); murderer.setParameter(Request.ACTION, CMD_CHANNEL_ACTION_NAME); - murderer.setParameter("vm-pid", String.valueOf(vmRef.getPid())); + murderer.setParameter("vm-pid", String.valueOf(pid)); murderer.setReceiver(RECEIVER); murderer.addListener(listener);
--- a/killvm/common/src/test/java/com/redhat/thermostat/killvm/common/KillVMRequestTest.java Fri May 29 18:38:40 2015 +0200 +++ b/killvm/common/src/test/java/com/redhat/thermostat/killvm/common/KillVMRequestTest.java Thu May 28 15:07:23 2015 -0400 @@ -49,7 +49,7 @@ import com.redhat.thermostat.client.command.RequestQueue; import com.redhat.thermostat.common.command.Request; import com.redhat.thermostat.common.command.RequestResponseListener; -import com.redhat.thermostat.storage.core.HostRef; +import com.redhat.thermostat.storage.core.AgentId; import com.redhat.thermostat.storage.core.VmRef; import com.redhat.thermostat.storage.dao.AgentInfoDAO; import com.redhat.thermostat.storage.model.AgentInformation; @@ -59,6 +59,8 @@ private AgentInfoDAO agentDAO; private RequestQueue queue; private VmRef vm; + private AgentId agentId; + private AgentInformation info; private KillVMRequest killVMRequest; private Request request; @@ -68,19 +70,18 @@ public void setup() { agentDAO = mock(AgentInfoDAO.class); vm = mock(VmRef.class); + agentId = mock(AgentId.class); request = mock(Request.class); listener = mock(RequestResponseListener.class); - HostRef ref = mock(HostRef.class); - when(vm.getHostRef()).thenReturn(ref); when(vm.getPid()).thenReturn(123456); - AgentInformation info = mock(AgentInformation.class); + info = mock(AgentInformation.class); when(info.getRequestQueueAddress()).thenReturn(new InetSocketAddress("0.0.42.42", 42)); - when(agentDAO.getAgentInformation(ref)).thenReturn(info); + when(agentDAO.getAgentInformation(agentId)).thenReturn(info); queue = mock(RequestQueue.class); } @@ -103,9 +104,10 @@ } }; - killVMRequest.sendKillVMRequestToAgent(vm, agentDAO, listener); - verify(vm).getHostRef(); + killVMRequest.sendKillVMRequestToAgent(agentId, vm.getPid(), agentDAO, listener); verify(vm).getPid(); + verify(agentDAO).getAgentInformation(agentId); + verify(info).getRequestQueueAddress(); assertTrue(results[0]); assertTrue(results[1]);
--- a/killvm/distribution/thermostat-plugin.xml Fri May 29 18:38:40 2015 +0200 +++ b/killvm/distribution/thermostat-plugin.xml Thu May 28 15:07:23 2015 -0400 @@ -52,13 +52,6 @@ <required>true</required> <description>the ID of the VM</description> </option> - <option> - <long>hostId</long> - <short>h</short> - <argument>host</argument> - <required>true</required> - <description>the ID of the Host</description> - </option> </options> <environments> <environment>cli</environment>
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/AgentId.java Thu May 28 15:07:23 2015 -0400 @@ -0,0 +1,68 @@ +/* + * Copyright 2012-2015 Red Hat, Inc. + * + * This file is part of Thermostat. + * + * Thermostat is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published + * by the Free Software Foundation; either version 2, or (at your + * option) any later version. + * + * Thermostat is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Thermostat; see the file COPYING. If not see + * <http://www.gnu.org/licenses/>. + * + * Linking this code with other modules is making a combined work + * based on this code. Thus, the terms and conditions of the GNU + * General Public License cover the whole combination. + * + * As a special exception, the copyright holders of this code give + * you permission to link this code with independent modules to + * produce an executable, regardless of the license terms of these + * independent modules, and to copy and distribute the resulting + * executable under terms of your choice, provided that you also + * meet, for each linked independent module, the terms and conditions + * of the license of that module. An independent module is a module + * which is not derived from or based on this code. If you modify + * this code, you may extend this exception to your version of the + * library, but you are not obligated to do so. If you do not wish + * to do so, delete this exception statement from your version. + */ + +package com.redhat.thermostat.storage.core; + +/** + * + */ +public class AgentId { + String uuid; + + public AgentId(String id) { + this.uuid = id; + } + + public String get() { + return uuid; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + AgentId agentId = (AgentId) o; + + return uuid.equals(agentId.uuid); + + } + + @Override + public int hashCode() { + return uuid.hashCode(); + } +}
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/dao/AgentInfoDAO.java Fri May 29 18:38:40 2015 +0200 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/dao/AgentInfoDAO.java Thu May 28 15:07:23 2015 -0400 @@ -39,6 +39,7 @@ import java.util.List; 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; @@ -88,9 +89,21 @@ * indicated by {@code agentRef}. {@code null} if no information about the * agent could be located. */ + + @Deprecated AgentInformation getAgentInformation(HostRef agentRef); /** + * Get information about a specific agent. + * + * @return a {@link AgentInformation} describing information about the agent + * indicated by {@code agentId}. {@code null} if no information about the + * agent could be located. + */ + + AgentInformation getAgentInformation(AgentId agentId); + + /** * Publish information about agent into the storage. */ void addAgentInformation(AgentInformation agentInfo);
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/AgentInfoDAOImpl.java Fri May 29 18:38:40 2015 +0200 +++ b/storage/core/src/main/java/com/redhat/thermostat/storage/internal/dao/AgentInfoDAOImpl.java Thu May 28 15:07:23 2015 -0400 @@ -43,6 +43,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; @@ -203,6 +204,32 @@ } @Override + public AgentInformation getAgentInformation(AgentId agentId) { + StatementDescriptor<AgentInformation> desc = new StatementDescriptor<>(CATEGORY, QUERY_AGENT_INFO); + PreparedStatement<AgentInformation> prepared; + Cursor<AgentInformation> agentCursor; + try { + prepared = storage.prepareStatement(desc); + prepared.setString(0, agentId.get()); + agentCursor = 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; + } + + AgentInformation result = null; + if (agentCursor.hasNext()) { + result = agentCursor.next(); + } + return result; + } + + @Override public void addAgentInformation(AgentInformation agentInfo) { StatementDescriptor<AgentInformation> desc = new StatementDescriptor<>(CATEGORY, DESC_ADD_AGENT_INFO); PreparedStatement<AgentInformation> prepared;