Mercurial > hg > thermostat-ng > agent
changeset 2772:9e3d6eedbbee
Add jvm-cpu plugin
Reviewed-by: jmatsuok, stooke
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-August/024663.html
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-September/025090.html
Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2017-October/025307.html
author | Christopher Koehler <chkoehle@redhat.com> |
---|---|
date | Tue, 10 Oct 2017 12:29:18 -0400 |
parents | 1a0d260468d1 |
children | 4f12f8ffdc9b |
files | agent/core/src/main/java/com/redhat/thermostat/backend/HostPollingBackend.java agent/core/src/main/java/com/redhat/thermostat/backend/PollingBackend.java agent/core/src/test/java/com/redhat/thermostat/backend/PollingBackendTest.java distribution/assembly/plugin-assembly.xml distribution/pom.xml plugins/jvm-overview/agent/src/main/java/com/redhat/thermostat/jvm/overview/agent/VmPollingBackend.java plugins/jvm-overview/agent/src/test/java/com/redhat/thermostat/jvm/overview/agent/VmPollingBackendTest.java plugins/pom.xml plugins/vm-cpu/agent/pom.xml plugins/vm-cpu/agent/src/main/java/com/redhat/thermostat/vm/cpu/agent/internal/Activator.java plugins/vm-cpu/agent/src/main/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuBackend.java plugins/vm-cpu/agent/src/main/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuStatBuilder.java plugins/vm-cpu/agent/src/main/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuStatDAO.java plugins/vm-cpu/agent/src/main/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuStatDAOImpl.java plugins/vm-cpu/agent/src/main/java/com/redhat/thermostat/vm/cpu/agent/model/VmCpuStat.java plugins/vm-cpu/agent/src/main/java/com/redhat/thermostat/vm/cpu/agent/model/VmCpuStatTypeAdapter.java plugins/vm-cpu/agent/src/test/java/com/redhat/thermostat/vm/cpu/agent/internal/ActivatorTest.java plugins/vm-cpu/agent/src/test/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuBackendTest.java plugins/vm-cpu/agent/src/test/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuStatBuilderTest.java plugins/vm-cpu/agent/src/test/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuStatDAOImplTest.java plugins/vm-cpu/common/pom.xml plugins/vm-cpu/common/src/main/java/com/redhat/thermostat/vm/cpu/common/VmCpuStatDAO.java plugins/vm-cpu/common/src/main/java/com/redhat/thermostat/vm/cpu/common/internal/Activator.java plugins/vm-cpu/common/src/main/java/com/redhat/thermostat/vm/cpu/common/internal/VmCpuStatCategoryRegistration.java plugins/vm-cpu/common/src/main/java/com/redhat/thermostat/vm/cpu/common/internal/VmCpuStatDAOImpl.java plugins/vm-cpu/common/src/main/java/com/redhat/thermostat/vm/cpu/common/internal/VmCpuStatDAOImplStatementDescriptorRegistration.java plugins/vm-cpu/common/src/main/java/com/redhat/thermostat/vm/cpu/common/model/VmCpuStat.java plugins/vm-cpu/common/src/main/resources/META-INF/services/com.redhat.thermostat.storage.core.auth.CategoryRegistration plugins/vm-cpu/common/src/main/resources/META-INF/services/com.redhat.thermostat.storage.core.auth.StatementDescriptorRegistration plugins/vm-cpu/common/src/test/java/com/redhat/thermostat/vm/cpu/common/internal/ActivatorTest.java plugins/vm-cpu/common/src/test/java/com/redhat/thermostat/vm/cpu/common/internal/VmCpuStatCategoryRegistrationTest.java plugins/vm-cpu/common/src/test/java/com/redhat/thermostat/vm/cpu/common/internal/VmCpuStatDAOImplStatementDescriptorRegistrationTest.java plugins/vm-cpu/common/src/test/java/com/redhat/thermostat/vm/cpu/common/internal/VmCpuStatDAOTest.java plugins/vm-cpu/common/src/test/java/com/redhat/thermostat/vm/cpu/common/model/VmCpuStatTest.java plugins/vm-cpu/distribution/assemblies/plugin-assembly.xml plugins/vm-cpu/distribution/configFiles/gateway.properties plugins/vm-cpu/distribution/thermostat-plugin.xml plugins/vm-cpu/pom.xml |
diffstat | 38 files changed, 719 insertions(+), 1499 deletions(-) [+] |
line wrap: on
line diff
--- a/agent/core/src/main/java/com/redhat/thermostat/backend/HostPollingBackend.java Mon Oct 09 15:56:10 2017 +0200 +++ b/agent/core/src/main/java/com/redhat/thermostat/backend/HostPollingBackend.java Tue Oct 10 12:29:18 2017 -0400 @@ -62,7 +62,7 @@ public HostPollingBackend(String name, String description, String vendor, Version version, ScheduledExecutorService executor) { - super(name, description, vendor, version, executor); + super(name, description, vendor, version.getVersionNumber(), executor); actions = new CopyOnWriteArraySet<>(); badActions = new HashMap<>(); }
--- a/agent/core/src/main/java/com/redhat/thermostat/backend/PollingBackend.java Mon Oct 09 15:56:10 2017 +0200 +++ b/agent/core/src/main/java/com/redhat/thermostat/backend/PollingBackend.java Tue Oct 10 12:29:18 2017 -0400 @@ -36,12 +36,13 @@ package com.redhat.thermostat.backend; +import java.util.Objects; +import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; import java.util.logging.Level; import java.util.logging.Logger; -import com.redhat.thermostat.common.Version; import com.redhat.thermostat.common.utils.LoggingUtils; /* @@ -59,11 +60,9 @@ private boolean isActive; public PollingBackend(String name, String description, String vendor, - Version version, - ScheduledExecutorService executor) { - super(name, description, vendor, version.getVersionNumber(), true); - this.executor = executor; - setVersion(version.getVersionNumber()); + String version, ScheduledExecutorService executor) { + super(name, description, vendor, version, true); + this.executor = Objects.requireNonNull(executor); } @Override
--- a/agent/core/src/test/java/com/redhat/thermostat/backend/PollingBackendTest.java Mon Oct 09 15:56:10 2017 +0200 +++ b/agent/core/src/test/java/com/redhat/thermostat/backend/PollingBackendTest.java Tue Oct 10 12:29:18 2017 -0400 @@ -65,9 +65,8 @@ mockActivate = mock(CustomActivateTester.class); Version mockVersion = mock(Version.class); when(mockVersion.getVersionNumber()).thenReturn("backend-version"); - backend = new PollingBackend("backend-name", "backend-description", - "backend-vendor", mockVersion, mockExecutor) { - + backend = new PollingBackend("backend-name", "backend-description", "backend-vendor", + mockVersion.getVersionNumber(), mockExecutor) { @Override protected void doScheduledActions() { // Won't be called because mock executor. @@ -81,7 +80,8 @@ @Override protected void postDeactivate() { mockActivate.deactivate(); - }}; + } + }; } @After @@ -127,12 +127,13 @@ when(mockVersion.getVersionNumber()).thenReturn("backend-version"); ScheduledExecutorService mockExecutor = mock(ScheduledExecutorService.class); backend = new PollingBackend("backend-name", "backend-description", - "backend-vendor", mockVersion, mockExecutor) { - + "backend-vendor", mockVersion.getVersionNumber(), + mockExecutor) { @Override protected void doScheduledActions() { mockAction.doAction(); - }}; + } + }; backend.activate(); ArgumentCaptor<Runnable> scheduledRunnableCaptor = ArgumentCaptor.forClass(Runnable.class); verify(mockExecutor).scheduleAtFixedRate(scheduledRunnableCaptor.capture(), eq( (long) 0),
--- a/distribution/assembly/plugin-assembly.xml Mon Oct 09 15:56:10 2017 +0200 +++ b/distribution/assembly/plugin-assembly.xml Tue Oct 10 12:29:18 2017 -0400 @@ -65,7 +65,7 @@ <!-- <include>com.redhat.thermostat.agent:thermostat-numa-distribution</include>--> <!-- <include>com.redhat.thermostat.agent:thermostat-thread-distribution</include>--> <!-- <include>com.redhat.thermostat.agent:thermostat-vm-classstat-distribution</include>--> -<!-- <include>com.redhat.thermostat.agent:thermostat-vm-cpu-distribution</include>--> + <include>com.redhat.thermostat.agent:thermostat-vm-cpu-distribution</include> <!-- <include>com.redhat.thermostat.agent:thermostat-vm-heap-analysis-distribution</include>--> <!-- <include>com.redhat.thermostat.agent:thermostat-vm-jmx-distribution</include>--> <!-- <include>com.redhat.thermostat.agent:thermostat-vm-profiler-distribution</include>-->
--- a/distribution/pom.xml Mon Oct 09 15:56:10 2017 +0200 +++ b/distribution/pom.xml Tue Oct 10 12:29:18 2017 -0400 @@ -569,12 +569,12 @@ <version>${project.version}</version> <type>zip</type> </dependency> - <!--<dependency>--> - <!--<groupId>com.redhat.thermostat.agent</groupId>--> - <!--<artifactId>thermostat-vm-cpu-distribution</artifactId>--> - <!--<version>${project.version}</version>--> - <!--<type>zip</type>--> - <!--</dependency>--> + <dependency> + <groupId>com.redhat.thermostat.agent</groupId> + <artifactId>thermostat-vm-cpu-distribution</artifactId> + <version>${project.version}</version> + <type>zip</type> + </dependency> <!--<dependency>--> <!--<groupId>com.redhat.thermostat.agent</groupId>--> <!--<artifactId>thermostat-vm-heap-analysis-distribution</artifactId>-->
--- a/plugins/jvm-overview/agent/src/main/java/com/redhat/thermostat/jvm/overview/agent/VmPollingBackend.java Mon Oct 09 15:56:10 2017 +0200 +++ b/plugins/jvm-overview/agent/src/main/java/com/redhat/thermostat/jvm/overview/agent/VmPollingBackend.java Tue Oct 10 12:29:18 2017 -0400 @@ -39,6 +39,7 @@ import java.util.HashMap; import java.util.Map; import java.util.Map.Entry; +import java.util.Objects; import java.util.Set; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.CopyOnWriteArraySet; @@ -48,10 +49,8 @@ import com.redhat.thermostat.backend.PollingBackend; import com.redhat.thermostat.common.Pair; -import com.redhat.thermostat.common.Version; import com.redhat.thermostat.common.utils.LoggingUtils; -import com.redhat.thermostat.jvm.overview.agent.internal.model.VmMapperServiceImpl; -import com.redhat.thermostat.jvm.overview.agent.model.VmMapperService; +import org.osgi.framework.BundleContext; /** * Convenience {@link com.redhat.thermostat.backend.Backend} class for implementations that will take some @@ -61,24 +60,47 @@ */ public abstract class VmPollingBackend extends PollingBackend implements VmStatusListener { - private final Set<VmPollingAction> actions; - private final Map<String, Pair<Integer, VmPollingAction>> badActions; + private final Set<VmPollingAction> actions = new CopyOnWriteArraySet<>(); + private final Map<String, Pair<Integer, VmPollingAction>> badActions = new HashMap<>(); private final Map<Integer, String> pidsToMonitor = new ConcurrentHashMap<>(); - private final VmStatusListenerRegistrar registrar; private static final Logger logger = LoggingUtils.getLogger(VmPollingBackend.class); private static final int EXCEPTIONS_THRESHOLD = 10; + private VmStatusListenerRegistrar registrar; + private boolean initialized; - public VmPollingBackend(String name, String description, - String vendor, Version version, ScheduledExecutorService executor, - VmStatusListenerRegistrar registrar) { + protected VmPollingBackend(String name, String description, String vendor, String version, ScheduledExecutorService executor) { + this(name, description, vendor, version, executor, null); + } + + protected VmPollingBackend(String name, String description, String vendor, String version, + ScheduledExecutorService executor, VmStatusListenerRegistrar registrar) { super(name, description, vendor, version, executor); this.registrar = registrar; - this.actions = new CopyOnWriteArraySet<>(); - this.badActions = new HashMap<>(); + } + + /** + * Performs initialization of the backend that must be done before any + * actions (like activating) can be done. This is needed since declarative + * services make it not possible to do this via constructor since it must + * be a no-arg constructor, and we need the BundleContext to create the + * {@link VmStatusListenerRegistrar}. + * @param bundleContext The bundle context, which should not be null. + */ + protected void initialize(BundleContext bundleContext) { + Objects.requireNonNull(bundleContext); + initialize(bundleContext, new VmStatusListenerRegistrarCreator()); + } + + void initialize(BundleContext bundleContext, VmStatusListenerRegistrarCreator registrarCreator) { + registrar = registrarCreator.create(bundleContext); + initialized = true; } @Override final public void preActivate() { + if (!initialized) { + throw new IllegalStateException("Cannot pre-activate if not initialized"); + } registrar.register(this); } @@ -107,7 +129,7 @@ final String actionKey = actionName + vmId; Pair<Integer, VmPollingAction> actionPair = badActions.remove(actionKey); if (actionPair == null) { - actionPair = new Pair<>(Integer.valueOf(1), action); + actionPair = new Pair<>(1, action); } int exceptionsPerAction = actionPair.getFirst(); if (exceptionsPerAction < EXCEPTIONS_THRESHOLD) { @@ -157,4 +179,11 @@ break; } } + + // For testing purposes only. + static class VmStatusListenerRegistrarCreator { + public VmStatusListenerRegistrar create(BundleContext bundleContext) { + return new VmStatusListenerRegistrar(bundleContext); + } + } }
--- a/plugins/jvm-overview/agent/src/test/java/com/redhat/thermostat/jvm/overview/agent/VmPollingBackendTest.java Mon Oct 09 15:56:10 2017 +0200 +++ b/plugins/jvm-overview/agent/src/test/java/com/redhat/thermostat/jvm/overview/agent/VmPollingBackendTest.java Tue Oct 10 12:29:18 2017 -0400 @@ -36,18 +36,10 @@ package com.redhat.thermostat.jvm.overview.agent; -import java.util.HashMap; -import java.util.Map; -import java.util.concurrent.ScheduledExecutorService; - -import com.redhat.thermostat.jvm.overview.agent.VmStatusListener.Status; -import org.junit.Before; -import org.junit.Test; - -import com.redhat.thermostat.common.Version; -import com.redhat.thermostat.common.internal.test.Bug; - import static org.junit.Assert.assertEquals; +import static org.mockito.Matchers.any; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -55,11 +47,28 @@ import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.util.Dictionary; +import java.util.HashMap; +import java.util.Map; +import java.util.concurrent.ScheduledExecutorService; + +import com.redhat.thermostat.common.Version; +import com.redhat.thermostat.common.internal.test.Bug; +import com.redhat.thermostat.jvm.overview.agent.VmPollingBackend.VmStatusListenerRegistrarCreator; +import com.redhat.thermostat.jvm.overview.agent.VmStatusListener.Status; +import org.junit.Before; +import org.junit.Test; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; + public class VmPollingBackendTest { private VmPollingBackend backend; private ScheduledExecutorService mockExecutor; private VmStatusListenerRegistrar mockRegistrar; + private BundleContext mockBundleContext; + private ServiceRegistration mockServiceRegistration; + private VmStatusListenerRegistrarCreator registrarCreator; @Before public void setUp() { @@ -67,9 +76,16 @@ Version mockVersion = mock(Version.class); when(mockVersion.getVersionNumber()).thenReturn("backend-version"); mockRegistrar = mock(VmStatusListenerRegistrar.class); + registrarCreator = mock(VmStatusListenerRegistrarCreator.class); + mockBundleContext = mock(BundleContext.class); + mockServiceRegistration = mock(ServiceRegistration.class); + VmStatusListenerRegistrarCreator registrarCreator = mock(VmStatusListenerRegistrarCreator.class); + when(registrarCreator.create(mockBundleContext)).thenReturn(mockRegistrar); + when(mockBundleContext.registerService(anyString(), anyObject(), any(Dictionary.class))).thenReturn(mockServiceRegistration); + when(registrarCreator.create(mockBundleContext)).thenReturn(mockRegistrar); backend = new VmPollingBackend("backend-name", "backend-description", - "backend-vendor", mockVersion, mockExecutor, mockRegistrar) { - }; + "backend-vendor", mockVersion.getVersionNumber(), + mockExecutor, mockRegistrar) { }; if (!backend.getObserveNewJvm()) { /* At time of writing, default is true. This is * inherited from parent PollingBackend. In case @@ -77,6 +93,7 @@ */ backend.setObserveNewJvm(true); } + backend.initialize(mockBundleContext, registrarCreator); } /** @@ -245,6 +262,15 @@ verify(action).run(eq(vmId1), eq(pid1)); verify(action, never()).run(eq(vmId2), eq(pid2)); } + + @Test(expected = IllegalStateException.class) + public void throwsIfTryingToActivateWithoutInitialize() { + ScheduledExecutorService executor = mock(ScheduledExecutorService.class); + VmStatusListenerRegistrar registrar = mock(VmStatusListenerRegistrar.class); + VmPollingBackend backend = new VmPollingBackend("backend-name", "backend-description", "backend-vendor", + "1.2.3", executor, registrar) { }; + backend.preActivate(); + } private static class BadVmPollingAction implements VmPollingAction { @@ -254,17 +280,16 @@ public void run(String vmId, int pid) { Integer currCount = callCounts.remove(vmId); if (currCount == null) { - currCount = Integer.valueOf(1); + currCount = 1; } else { currCount++; } - callCounts.put(vmId, Integer.valueOf(currCount)); + callCounts.put(vmId, currCount); throw new RuntimeException("doScheduledActions() testing!"); } private Integer getCallCount(String vmId) { return callCounts.get(vmId); } - } }
--- a/plugins/pom.xml Mon Oct 09 15:56:10 2017 +0200 +++ b/plugins/pom.xml Tue Oct 10 12:29:18 2017 -0400 @@ -65,7 +65,7 @@ <module>vm-byteman</module> <module>vm-compiler</module> <module>jcmd-stats-collector</module> - <!--<module>vm-cpu</module>--> + <module>vm-cpu</module> <!--<module>vm-classstat</module>--> <!--<module>vm-heap-analysis</module>--> <!--<module>vm-jmx</module>-->
--- a/plugins/vm-cpu/agent/pom.xml Mon Oct 09 15:56:10 2017 +0200 +++ b/plugins/vm-cpu/agent/pom.xml Tue Oct 10 12:29:18 2017 -0400 @@ -60,8 +60,9 @@ <instructions> <Bundle-Vendor>Red Hat, Inc.</Bundle-Vendor> <Bundle-SymbolicName>com.redhat.thermostat.vm.cpu.agent</Bundle-SymbolicName> - <Bundle-Activator>com.redhat.thermostat.vm.cpu.agent.internal.Activator</Bundle-Activator> - <Export-Package /> + <Export-Package> + com.redhat.thermostat.vm.cpu.agent.model + </Export-Package> <Private-Package> com.redhat.thermostat.vm.cpu.agent.internal </Private-Package> @@ -72,6 +73,19 @@ </plugin> <plugin> + <groupId>org.apache.felix</groupId> + <artifactId>maven-scr-plugin</artifactId> + <executions> + <execution> + <id>generate-scr-scrdescriptor</id> + <goals> + <goal>scr</goal> + </goals> + </execution> + </executions> + </plugin> + + <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <executions> @@ -132,6 +146,10 @@ <scope>provided</scope> </dependency> <dependency> + <groupId>org.apache.felix</groupId> + <artifactId>org.apache.felix.scr.annotations</artifactId> + </dependency> + <dependency> <groupId>com.redhat.thermostat.agent</groupId> <artifactId>thermostat-common-core</artifactId> <version>${project.version}</version> @@ -143,11 +161,6 @@ </dependency> <dependency> <groupId>com.redhat.thermostat.agent</groupId> - <artifactId>thermostat-vm-cpu-common</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>com.redhat.thermostat.agent</groupId> <artifactId>thermostat-agent-core</artifactId> <version>${project.version}</version> </dependency> @@ -158,6 +171,11 @@ </dependency> <dependency> <groupId>com.redhat.thermostat.agent</groupId> + <artifactId>thermostat-jvm-overview-agent</artifactId> + <version>${project.version}</version> + </dependency> + <dependency> + <groupId>com.redhat.thermostat.agent</groupId> <artifactId>thermostat-common-test</artifactId> <version>${project.version}</version> <scope>test</scope>
--- a/plugins/vm-cpu/agent/src/main/java/com/redhat/thermostat/vm/cpu/agent/internal/Activator.java Mon Oct 09 15:56:10 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,108 +0,0 @@ -/* - * Copyright 2012-2017 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.vm.cpu.agent.internal; - -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; - -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceRegistration; - -import com.redhat.thermostat.agent.VmStatusListenerRegistrar; -import com.redhat.thermostat.backend.Backend; -import com.redhat.thermostat.backend.BackendService; -import com.redhat.thermostat.common.MultipleServiceTracker; -import com.redhat.thermostat.common.MultipleServiceTracker.Action; -import com.redhat.thermostat.common.MultipleServiceTracker.DependencyProvider; -import com.redhat.thermostat.common.Version; -import com.redhat.thermostat.storage.core.WriterID; -import com.redhat.thermostat.vm.cpu.common.VmCpuStatDAO; - -public class Activator implements BundleActivator { - - private ScheduledExecutorService executor; - private MultipleServiceTracker tracker; - private VmCpuBackend backend; - private ServiceRegistration<Backend> reg; - - @Override - public void start(final BundleContext context) throws Exception { - final VmStatusListenerRegistrar registrar = new VmStatusListenerRegistrar(context); - - executor = Executors.newSingleThreadScheduledExecutor(); - - Class<?>[] deps = new Class<?>[] { - BackendService.class, - VmCpuStatDAO.class, - WriterID.class, // vm cpu backend uses it - }; - tracker = new MultipleServiceTracker(context, deps, new Action() { - - @Override - public void dependenciesAvailable(DependencyProvider services) { - VmCpuStatDAO vmCpuStatDao = services.get(VmCpuStatDAO.class); - Version version = new Version(context.getBundle()); - WriterID writerId = services.get(WriterID.class); - backend = new VmCpuBackend(executor, vmCpuStatDao, version, registrar, writerId); - reg = context.registerService(Backend.class, backend, null); - } - - @Override - public void dependenciesUnavailable() { - if (backend.isActive()) { - backend.deactivate(); - } - reg.unregister(); - } - }); - tracker.open(); - } - - @Override - public void stop(BundleContext context) throws Exception { - tracker.close(); - } - - /* - * For testing purposes only. - */ - VmCpuBackend getBackend() { - return backend; - } -} -
--- a/plugins/vm-cpu/agent/src/main/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuBackend.java Mon Oct 09 15:56:10 2017 +0200 +++ b/plugins/vm-cpu/agent/src/main/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuBackend.java Tue Oct 10 12:29:18 2017 -0400 @@ -38,43 +38,79 @@ import java.io.BufferedReader; import java.io.IOException; +import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.logging.Level; import java.util.logging.Logger; -import com.redhat.thermostat.agent.VmStatusListenerRegistrar; -import com.redhat.thermostat.common.portability.linux.ProcDataSource; -import com.redhat.thermostat.common.portability.SysConf; -import com.redhat.thermostat.backend.VmPollingAction; -import com.redhat.thermostat.backend.VmPollingBackend; +import com.redhat.thermostat.backend.Backend; import com.redhat.thermostat.common.Clock; import com.redhat.thermostat.common.SystemClock; import com.redhat.thermostat.common.Version; import com.redhat.thermostat.common.portability.PortableHostFactory; +import com.redhat.thermostat.common.portability.SysConf; +import com.redhat.thermostat.common.portability.linux.ProcDataSource; import com.redhat.thermostat.common.utils.LoggingUtils; +import com.redhat.thermostat.jvm.overview.agent.VmPollingAction; +import com.redhat.thermostat.jvm.overview.agent.VmPollingBackend; import com.redhat.thermostat.shared.config.OS; import com.redhat.thermostat.storage.core.WriterID; -import com.redhat.thermostat.vm.cpu.common.Constants; -import com.redhat.thermostat.vm.cpu.common.VmCpuStatDAO; -import com.redhat.thermostat.vm.cpu.common.model.VmCpuStat; +import com.redhat.thermostat.vm.cpu.agent.model.VmCpuStat; +import org.apache.felix.scr.annotations.Activate; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Deactivate; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.Service; +import org.osgi.framework.BundleContext; +@Component +@Service(value = Backend.class) public class VmCpuBackend extends VmPollingBackend { private static final Logger LOGGER = LoggingUtils.getLogger(VmCpuBackend.class); + @Reference + private VmCpuStatDAO vmCpuStatDAO; + + @Reference + private WriterID writerId; + private VmCpuBackendAction action; - public VmCpuBackend(ScheduledExecutorService executor, VmCpuStatDAO vmCpuStatDao, Version version, - VmStatusListenerRegistrar registrar, WriterID writerId) { - super("VM CPU Backend", - "Gathers CPU statistics about a JVM", - "Red Hat, Inc.", - version, executor, registrar); - VmCpuBackendAction action = new VmCpuBackendAction(writerId, vmCpuStatDao); - this.action = action; + public VmCpuBackend() { + this(Executors.newSingleThreadScheduledExecutor()); + } + + VmCpuBackend(ScheduledExecutorService executor) { + super("VM CPU Backend", "Gathers CPU statistics for a JVM", + "Red Hat, Inc.", "1.0", executor); + } + + @Activate + protected void componentActivated(BundleContext context) { + this.action = new VmCpuBackendAction(writerId, vmCpuStatDAO); + initialize(context); + setVersion(new Version(context.getBundle()).getVersionNumber()); registerAction(action); } - + + @Deactivate + protected void componentDeactivated() { + if (isActive()) { + deactivate(); + } + } + + // DS bind method. + protected void bindVmCpuStats(VmCpuStatDAO dao) { + this.vmCpuStatDAO = dao; + } + + // DS bind method. + protected void bindWriterId(WriterID id) { + this.writerId = id; + } + private static class VmCpuBackendAction implements VmPollingAction { private VmCpuStatBuilder builder; @@ -95,7 +131,7 @@ if (builder.knowsAbout(pid)) { VmCpuStat dataBuilt = builder.build(vmId, pid); if (dataBuilt != null) { - dao.putVmCpuStat(dataBuilt); + dao.put(dataBuilt); } } else { builder.learnAbout(pid); @@ -136,12 +172,9 @@ } } - /* - * For testing purposes only. - */ + // For testing purposes only. void setVmCpuStatBuilder(VmCpuStatBuilder vmCpuStatBuilder) { action.builder = vmCpuStatBuilder; } - }
--- a/plugins/vm-cpu/agent/src/main/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuStatBuilder.java Mon Oct 09 15:56:10 2017 +0200 +++ b/plugins/vm-cpu/agent/src/main/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuStatBuilder.java Tue Oct 10 12:29:18 2017 -0400 @@ -44,7 +44,7 @@ import com.redhat.thermostat.common.Clock; import com.redhat.thermostat.common.utils.LoggingUtils; import com.redhat.thermostat.storage.core.WriterID; -import com.redhat.thermostat.vm.cpu.common.model.VmCpuStat; +import com.redhat.thermostat.vm.cpu.agent.model.VmCpuStat; public class VmCpuStatBuilder {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/vm-cpu/agent/src/main/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuStatDAO.java Tue Oct 10 12:29:18 2017 -0400 @@ -0,0 +1,46 @@ +/* + * Copyright 2012-2017 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.vm.cpu.agent.internal; + +import com.redhat.thermostat.annotations.Service; +import com.redhat.thermostat.vm.cpu.agent.model.VmCpuStat; + +@Service +public interface VmCpuStatDAO { + + void put(VmCpuStat vmCpuStat); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/vm-cpu/agent/src/main/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuStatDAOImpl.java Tue Oct 10 12:29:18 2017 -0400 @@ -0,0 +1,151 @@ +/* + * Copyright 2012-2017 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.vm.cpu.agent.internal; + +import java.io.IOException; +import java.net.URI; +import java.util.Collections; +import java.util.List; +import java.util.logging.Logger; + +import com.redhat.thermostat.agent.http.HttpRequestService; +import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource; +import com.redhat.thermostat.common.plugin.PluginConfiguration; +import com.redhat.thermostat.common.plugin.PluginDAOBase; +import com.redhat.thermostat.common.plugin.SystemID; +import com.redhat.thermostat.common.utils.LoggingUtils; +import com.redhat.thermostat.vm.cpu.agent.model.VmCpuStat; +import com.redhat.thermostat.vm.cpu.agent.model.VmCpuStatTypeAdapter; +import org.apache.felix.scr.annotations.Activate; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.Reference; +import org.apache.felix.scr.annotations.Service; + +@Component +@Service(value = VmCpuStatDAO.class) +public class VmCpuStatDAOImpl extends PluginDAOBase<VmCpuStat> implements VmCpuStatDAO { + + private static final Logger logger = LoggingUtils.getLogger(VmCpuStatDAOImpl.class); + public static final String PLUGIN_ID = "vm-cpu"; + + @Reference + private ConfigurationInfoSource configurationInfoSource; + + @Reference + private SystemID systemID; + + @Reference + private HttpRequestService httpRequestService; + + private final JsonHelper jsonHelper; + private final ConfigurationCreator configurationCreator; + private PluginConfiguration pluginConfiguration; + + public VmCpuStatDAOImpl() { + this(new JsonHelper(new VmCpuStatTypeAdapter()), new ConfigurationCreator()); + } + + VmCpuStatDAOImpl(JsonHelper jsonHelper, ConfigurationCreator configurationCreator) { + this.jsonHelper = jsonHelper; + this.configurationCreator = configurationCreator; + } + + @Activate + public void activate() throws Exception { + pluginConfiguration = configurationCreator.create(configurationInfoSource); + } + + @Override + protected String toJsonString(VmCpuStat obj) throws IOException { + return jsonHelper.toJson(Collections.singletonList(obj)); + } + + @Override + protected HttpRequestService getHttpRequestService() { + return httpRequestService; + } + + @Override + protected PluginConfiguration getConfig() { + return pluginConfiguration; + } + + @Override + protected URI getPostURI(URI basepath, VmCpuStat vmCpuStat) { + return basepath.resolve("systems/" + systemID.getSystemID() + "/jvms/" + vmCpuStat.getVmId()); + } + + @Override + protected Logger getLogger() { + return logger; + } + + // DS bind method. + protected void bindSystemID(SystemID systemid) { + this.systemID = systemid; + } + + // DS bind method. + protected void bindConfigurationInfoSource(ConfigurationInfoSource cfg) { + this.configurationInfoSource = cfg; + } + + // DS bind method. + protected void bindHttpRequestService(HttpRequestService httpRequestService) { + this.httpRequestService = httpRequestService; + } + + // For testing. + static class JsonHelper { + private final VmCpuStatTypeAdapter typeAdapter; + + JsonHelper(VmCpuStatTypeAdapter typeAdapter) { + this.typeAdapter = typeAdapter; + } + + String toJson(List<VmCpuStat> infos) throws IOException { + return typeAdapter.toJson(infos); + } + } + + // For testing. + static class ConfigurationCreator { + PluginConfiguration create(ConfigurationInfoSource source) { + return new PluginConfiguration(source, PLUGIN_ID); + } + } +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/vm-cpu/agent/src/main/java/com/redhat/thermostat/vm/cpu/agent/model/VmCpuStat.java Tue Oct 10 12:29:18 2017 -0400 @@ -0,0 +1,98 @@ +/* + * Copyright 2012-2017 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.vm.cpu.agent.model; + +import com.redhat.thermostat.storage.core.Entity; +import com.redhat.thermostat.storage.core.Persist; +import com.redhat.thermostat.storage.model.BasePojo; +import com.redhat.thermostat.storage.model.TimeStampedPojo; + +@Entity +public class VmCpuStat extends BasePojo implements TimeStampedPojo { + + private long timeStamp; + private String vmId; + private double cpuLoad; + + public VmCpuStat() { + super(null); + } + + public VmCpuStat(String writerId, long timeStamp, String vmId, double cpuLoad) { + super(writerId); + this.timeStamp = timeStamp; + this.vmId = vmId; + this.cpuLoad = cpuLoad; + } + + @Override + @Persist + public long getTimeStamp() { + return timeStamp; + } + + @Persist + public void setTimeStamp(long timeStamp) { + this.timeStamp = timeStamp; + } + + @Persist + public String getVmId() { + return vmId; + } + + @Persist + public void setVmId(String vmId) { + this.vmId = vmId; + } + + /** + * The cpu load in percent (as in 100.0 for 100%). This value should be + * normalized to be in the range [0, 100] + */ + @Persist + public double getCpuLoad() { + return cpuLoad; + } + + @Persist + public void setCpuLoad(double cpuLoad) { + this.cpuLoad = cpuLoad; + } + +} +
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/vm-cpu/agent/src/main/java/com/redhat/thermostat/vm/cpu/agent/model/VmCpuStatTypeAdapter.java Tue Oct 10 12:29:18 2017 -0400 @@ -0,0 +1,87 @@ +/* + * Copyright 2012-2017 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.vm.cpu.agent.model; + +import java.io.IOException; +import java.util.List; + +import com.google.gson.TypeAdapter; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +public class VmCpuStatTypeAdapter extends TypeAdapter<List<VmCpuStat>> { + + private static final String TYPE_LONG = "$numberLong"; + private static final String TIMESTAMP = "timeStamp"; + private static final String VM_ID = "jvmId"; + private static final String CPU_LOAD = "cpuLoad"; + private static final String AGENT_ID = "agentId"; + + private void writeMemoryStat(JsonWriter out, VmCpuStat stat) throws IOException { + out.beginObject(); + out.name(TIMESTAMP); + writeLong(out, stat.getTimeStamp()); + out.name(VM_ID); + out.value(stat.getVmId()); + out.name(CPU_LOAD); + out.value(stat.getCpuLoad()); + out.name(AGENT_ID); + out.value(stat.getAgentId()); + out.endObject(); + } + + private void writeLong(JsonWriter out, long input) throws IOException { + out.beginObject(); + out.name(TYPE_LONG); + out.value(String.valueOf(input)); + out.endObject(); + } + + @Override + public void write(JsonWriter jsonWriter, List<VmCpuStat> vmCpuStats) throws IOException { + jsonWriter.beginArray(); + for (VmCpuStat stat : vmCpuStats) { + writeMemoryStat(jsonWriter, stat); + } + jsonWriter.endArray(); + } + + @Override + public List<VmCpuStat> read(JsonReader jsonReader) throws IOException { + return null; + } +}
--- a/plugins/vm-cpu/agent/src/test/java/com/redhat/thermostat/vm/cpu/agent/internal/ActivatorTest.java Mon Oct 09 15:56:10 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,110 +0,0 @@ -/* - * Copyright 2012-2017 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.vm.cpu.agent.internal; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.when; - -import org.junit.Test; -import org.osgi.framework.Bundle; -import org.osgi.framework.Version; - -import com.redhat.thermostat.backend.Backend; -import com.redhat.thermostat.backend.BackendService; -import com.redhat.thermostat.storage.core.WriterID; -import com.redhat.thermostat.testutils.StubBundleContext; -import com.redhat.thermostat.vm.cpu.common.VmCpuStatDAO; - -public class ActivatorTest { - - @Test - public void verifyActivatorDoesNotRegisterServiceOnMissingDeps() throws Exception { - StubBundleContext context = new StubBundleContext(); - - Activator activator = new Activator(); - - activator.start(context); - - assertEquals(0, context.getAllServices().size()); - assertEquals(3, context.getServiceListeners().size()); - - activator.stop(context); - } - - @Test - public void verifyActivatorRegistersServices() throws Exception { - StubBundleContext context = new StubBundleContext() { - @Override - public Bundle getBundle() { - Bundle result = mock(Bundle.class); - when(result.getVersion()).thenReturn(Version.emptyVersion); - return result; - } - }; - BackendService service = mock(BackendService.class); - VmCpuStatDAO vmCpuStatDAO = mock(VmCpuStatDAO.class); - WriterID idService = mock(WriterID.class); - - context.registerService(BackendService.class.getName(), service, null); - context.registerService(VmCpuStatDAO.class, vmCpuStatDAO, null); - context.registerService(WriterID.class, idService, null); - - Activator activator = new Activator(); - - activator.start(context); - - assertTrue(context.isServiceRegistered(Backend.class.getName(), VmCpuBackend.class)); - VmCpuBackend backend = activator.getBackend(); - assertNotNull(backend); - - // core thermostat will activate the backend once it's registered - backend.activate(); - - activator.stop(context); - - assertFalse(backend.isActive()); - - assertEquals(0, context.getServiceListeners().size()); - assertEquals(3, context.getAllServices().size()); - } - -} -
--- a/plugins/vm-cpu/agent/src/test/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuBackendTest.java Mon Oct 09 15:56:10 2017 +0200 +++ b/plugins/vm-cpu/agent/src/test/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuBackendTest.java Tue Oct 10 12:29:18 2017 -0400 @@ -36,11 +36,12 @@ package com.redhat.thermostat.vm.cpu.agent.internal; -import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.Matchers.any; import static org.mockito.Matchers.anyInt; +import static org.mockito.Matchers.anyObject; +import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Matchers.isA; import static org.mockito.Mockito.mock; @@ -48,41 +49,50 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; +import java.util.Dictionary; import java.util.HashSet; import java.util.Set; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; +import com.redhat.thermostat.jvm.overview.agent.VmStatusListener.Status; +import com.redhat.thermostat.storage.core.WriterID; +import com.redhat.thermostat.vm.cpu.agent.model.VmCpuStat; import org.junit.Before; import org.junit.Test; import org.mockito.ArgumentCaptor; - -import com.redhat.thermostat.agent.VmStatusListener.Status; -import com.redhat.thermostat.agent.VmStatusListenerRegistrar; -import com.redhat.thermostat.common.Version; -import com.redhat.thermostat.storage.core.WriterID; -import com.redhat.thermostat.vm.cpu.common.VmCpuStatDAO; -import com.redhat.thermostat.vm.cpu.common.model.VmCpuStat; +import org.osgi.framework.Bundle; +import org.osgi.framework.BundleContext; +import org.osgi.framework.ServiceRegistration; +import org.osgi.framework.Version; public class VmCpuBackendTest { private VmCpuBackend backend; private ScheduledExecutorService executor; private VmCpuStatDAO vmCpuStatDao; - private VmStatusListenerRegistrar registrar; + private Bundle bundle; + private BundleContext bundleContext; + private ServiceRegistration serviceRegistration; @Before public void setup() { executor = mock(ScheduledExecutorService.class); vmCpuStatDao = mock(VmCpuStatDAO.class); - - Version version = mock(Version.class); - when(version.getVersionNumber()).thenReturn("0.0.0"); - - registrar = mock(VmStatusListenerRegistrar.class); - - WriterID id = mock(WriterID.class); - backend = new VmCpuBackend(executor, vmCpuStatDao, version, registrar, id); + + bundleContext = mock(BundleContext.class); + bundle = mock(Bundle.class); + serviceRegistration = mock(ServiceRegistration.class); + Version version = new Version(1, 2, 3); + when(bundle.getVersion()).thenReturn(version); + when(bundleContext.getBundle()).thenReturn(bundle); + when(bundleContext.registerService(anyString(), anyObject(), any(Dictionary.class))).thenReturn(serviceRegistration); + + backend = new VmCpuBackend(executor); + backend.bindVmCpuStats(vmCpuStatDao); + backend.bindWriterId(mock(WriterID.class)); + + backend.componentActivated(bundleContext); } @Test @@ -90,39 +100,15 @@ backend.activate(); verify(executor).scheduleAtFixedRate(isA(Runnable.class), eq(0l), eq(1000l), eq(TimeUnit.MILLISECONDS)); - verify(registrar).register(backend); assertTrue(backend.isActive()); } @Test - public void testActivateTwice() { - assertTrue(backend.activate()); - assertTrue(backend.isActive()); - assertTrue(backend.activate()); - assertTrue(backend.isActive()); - - assertTrue(backend.deactivate()); - } - - @Test public void testDeactivate() { backend.activate(); backend.deactivate(); verify(executor).shutdown(); - verify(registrar).unregister(backend); - assertFalse(backend.isActive()); - } - - - @Test - public void testDeactivateTwice() { - assertTrue(backend.activate()); - assertTrue(backend.isActive()); - - assertTrue(backend.deactivate()); - assertFalse(backend.isActive()); - assertTrue(backend.deactivate()); assertFalse(backend.isActive()); } @@ -142,7 +128,6 @@ backend.activate(); - verify(registrar).register(backend); ArgumentCaptor<Runnable> captor = ArgumentCaptor.forClass(Runnable.class); verify(executor).scheduleAtFixedRate(captor.capture(), any(Long.class), any(Long.class), any(TimeUnit.class)); assertTrue(backend.isActive()); @@ -157,8 +142,8 @@ when(builder.knowsAbout(anyInt())).thenReturn(true); runnable.run(); - verify(vmCpuStatDao).putVmCpuStat(stat0); - verify(vmCpuStatDao).putVmCpuStat(stat1); + verify(vmCpuStatDao).put(stat0); + verify(vmCpuStatDao).put(stat1); backend.vmStatusChanged(Status.VM_STOPPED, "vm1", 0); backend.vmStatusChanged(Status.VM_STOPPED, "vm2", 1);
--- a/plugins/vm-cpu/agent/src/test/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuStatBuilderTest.java Mon Oct 09 15:56:10 2017 +0200 +++ b/plugins/vm-cpu/agent/src/test/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuStatBuilderTest.java Tue Oct 10 12:29:18 2017 -0400 @@ -44,13 +44,13 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import com.redhat.thermostat.vm.cpu.agent.model.VmCpuStat; import org.junit.Before; import org.junit.Test; import com.redhat.thermostat.common.Clock; import com.redhat.thermostat.common.internal.test.Bug; import com.redhat.thermostat.storage.core.WriterID; -import com.redhat.thermostat.vm.cpu.common.model.VmCpuStat; public class VmCpuStatBuilderTest {
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/vm-cpu/agent/src/test/java/com/redhat/thermostat/vm/cpu/agent/internal/VmCpuStatDAOImplTest.java Tue Oct 10 12:29:18 2017 -0400 @@ -0,0 +1,105 @@ +/* + * Copyright 2012-2017 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.vm.cpu.agent.internal; + +import static com.redhat.thermostat.vm.cpu.agent.internal.VmCpuStatDAOImpl.ConfigurationCreator; +import static com.redhat.thermostat.vm.cpu.agent.internal.VmCpuStatDAOImpl.JsonHelper; +import static org.mockito.Matchers.anyListOf; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import java.net.URI; +import java.util.Collections; + +import com.redhat.thermostat.agent.http.HttpRequestService; +import com.redhat.thermostat.common.config.experimental.ConfigurationInfoSource; +import com.redhat.thermostat.common.plugin.PluginConfiguration; +import com.redhat.thermostat.common.plugin.SystemID; +import com.redhat.thermostat.vm.cpu.agent.model.VmCpuStat; +import org.junit.Before; +import org.junit.Test; + +public class VmCpuStatDAOImplTest { + + private static final String AGENT_ID = "some-agent"; + private static final String JSON = "{\"this\":\"is\",\"also\":\"JSON\"}"; + private static final URI GATEWAY_URI = URI.create("http://example.com/jvm-cpu/"); + private static final String SYSTEM_ID = "1234"; + public static final String JVM_ID = "vm-123"; + + private VmCpuStat stat; + private JsonHelper jsonHelper; + private VmCpuStatDAOImpl dao; + + private HttpRequestService httpRequestService; + + @Before + public void setup() throws Exception { + stat = new VmCpuStat(); + stat.setAgentId(AGENT_ID); + stat.setTimeStamp(1234L); + stat.setCpuLoad(42.0); + stat.setVmId(JVM_ID); + + jsonHelper = mock(JsonHelper.class); + when(jsonHelper.toJson(anyListOf(VmCpuStat.class))).thenReturn(JSON); + + ConfigurationInfoSource source = mock(ConfigurationInfoSource.class); + PluginConfiguration config = mock(PluginConfiguration.class); + when(config.getGatewayURL()).thenReturn(GATEWAY_URI); + ConfigurationCreator creator = mock(ConfigurationCreator.class); + when(creator.create(source)).thenReturn(config); + SystemID systemID = mock(SystemID.class); + when(systemID.getSystemID()).thenReturn(SYSTEM_ID); + + httpRequestService = mock(HttpRequestService.class); + dao = new VmCpuStatDAOImpl(jsonHelper, creator); + dao.bindHttpRequestService(httpRequestService); + dao.bindConfigurationInfoSource(source); + dao.bindSystemID(systemID); + } + + @Test + public void verifyAddVmCpuStat() throws Exception { + dao.activate(); + dao.put(stat); + verify(jsonHelper).toJson(eq(Collections.singletonList(stat))); + verify(httpRequestService).sendHttpRequest(JSON, GATEWAY_URI.resolve("systems/" + SYSTEM_ID + "/jvms/" + JVM_ID), HttpRequestService.Method.POST); + } +}
--- a/plugins/vm-cpu/common/pom.xml Mon Oct 09 15:56:10 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,134 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - - Copyright 2012-2017 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. - ---> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> - <modelVersion>4.0.0</modelVersion> - <parent> - <artifactId>thermostat-vm-cpu</artifactId> - <groupId>com.redhat.thermostat.agent</groupId> - <version>2.99.0-SNAPSHOT</version> - </parent> - <artifactId>thermostat-vm-cpu-common</artifactId> - <packaging>bundle</packaging> - <name>Thermostat VM CPU Common plugin</name> - <build> - <resources> - <resource> - <directory>src/main/resources</directory> - <filtering>true</filtering> - <excludes> - <exclude>**/*.png</exclude> - </excludes> - </resource> - <resource> - <directory>src/main/resources</directory> - <filtering>false</filtering> - <includes> - <include>**/*.png</include> - </includes> - </resource> - </resources> - <plugins> - <plugin> - <groupId>org.apache.felix</groupId> - <artifactId>maven-bundle-plugin</artifactId> - <extensions>true</extensions> - <configuration> - <instructions> - <Bundle-Vendor>Red Hat, Inc.</Bundle-Vendor> - <Bundle-SymbolicName>com.redhat.thermostat.vm.cpu.common</Bundle-SymbolicName> - <Bundle-Activator>com.redhat.thermostat.vm.cpu.common.internal.Activator</Bundle-Activator> - <Export-Package> - com.redhat.thermostat.vm.cpu.common, - com.redhat.thermostat.vm.cpu.common.model - </Export-Package> - <Private-Package> - com.redhat.thermostat.vm.cpu.common.internal - </Private-Package> - <!-- Do not autogenerate uses clauses in Manifests --> - <_nouses>true</_nouses> - </instructions> - </configuration> - </plugin> - </plugins> - </build> - <dependencies> - <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.mockito</groupId> - <artifactId>mockito-core</artifactId> - <scope>test</scope> - </dependency> - <dependency> - <groupId>org.osgi</groupId> - <artifactId>org.osgi.core</artifactId> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>org.osgi</groupId> - <artifactId>org.osgi.compendium</artifactId> - <scope>provided</scope> - </dependency> - <dependency> - <groupId>com.redhat.thermostat.agent</groupId> - <artifactId>thermostat-common-core</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>com.redhat.thermostat.agent</groupId> - <artifactId>thermostat-storage-core</artifactId> - <version>${project.version}</version> - </dependency> - <dependency> - <groupId>com.redhat.thermostat.agent</groupId> - <artifactId>thermostat-common-test</artifactId> - <version>${project.version}</version> - <scope>test</scope> - </dependency> - <dependency> - <groupId>com.redhat.thermostat.agent</groupId> - <artifactId>thermostat-storage-testutils</artifactId> - <version>${project.version}</version> - <scope>test</scope> - </dependency> - </dependencies> -</project>
--- a/plugins/vm-cpu/common/src/main/java/com/redhat/thermostat/vm/cpu/common/VmCpuStatDAO.java Mon Oct 09 15:56:10 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,72 +0,0 @@ -/* - * Copyright 2012-2017 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.vm.cpu.common; - -import java.util.Arrays; -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.Key; -import com.redhat.thermostat.storage.core.VmId; -import com.redhat.thermostat.storage.core.VmRef; -import com.redhat.thermostat.vm.cpu.common.model.VmCpuStat; - -@Service -public interface VmCpuStatDAO { - - static final Key<Double> vmCpuLoadKey = new Key<>("cpuLoad"); - - static final Category<VmCpuStat> vmCpuStatCategory = new Category<>("vm-cpu-stats", VmCpuStat.class, - Arrays.<Key<?>>asList(Key.AGENT_ID, Key.VM_ID, Key.TIMESTAMP, vmCpuLoadKey), Arrays.<Key<?>>asList(Key.TIMESTAMP)); - - public abstract VmCpuStat getOldest(VmRef ref); - - public abstract VmCpuStat getNewest(VmRef ref); - - @Deprecated - public abstract List<VmCpuStat> getLatestVmCpuStats(VmRef ref, long since); - - public abstract List<VmCpuStat> getLatestVmCpuStats(AgentId agentId, VmId vmId, long since); - - public abstract List<VmCpuStat> getVmCpuStats(VmRef ref, long since, long to); - - public abstract void putVmCpuStat(VmCpuStat stat); - -} -
--- a/plugins/vm-cpu/common/src/main/java/com/redhat/thermostat/vm/cpu/common/internal/Activator.java Mon Oct 09 15:56:10 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,80 +0,0 @@ -/* - * Copyright 2012-2017 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.vm.cpu.common.internal; - -import org.osgi.framework.BundleActivator; -import org.osgi.framework.BundleContext; -import org.osgi.framework.ServiceReference; -import org.osgi.framework.ServiceRegistration; -import org.osgi.util.tracker.ServiceTracker; - -import com.redhat.thermostat.storage.core.Storage; -import com.redhat.thermostat.vm.cpu.common.VmCpuStatDAO; - -public class Activator implements BundleActivator { - - private ServiceTracker tracker; - private ServiceRegistration reg; - - @Override - public void start(BundleContext context) throws Exception { - tracker = new ServiceTracker(context, Storage.class.getName(), null) { - @Override - public Object addingService(ServiceReference reference) { - Storage storage = (Storage) context.getService(reference); - VmCpuStatDAO vmCpuStatDao = new VmCpuStatDAOImpl(storage); - reg = context.registerService(VmCpuStatDAO.class.getName(), vmCpuStatDao, null); - return super.addingService(reference); - } - - @Override - public void removedService(ServiceReference reference, - Object service) { - reg.unregister(); - super.removedService(reference, service); - } - }; - tracker.open(); - } - - @Override - public void stop(BundleContext context) throws Exception { - tracker.close(); - } - -} -
--- a/plugins/vm-cpu/common/src/main/java/com/redhat/thermostat/vm/cpu/common/internal/VmCpuStatCategoryRegistration.java Mon Oct 09 15:56:10 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,61 +0,0 @@ -/* - * Copyright 2012-2017 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.vm.cpu.common.internal; - -import java.util.HashSet; -import java.util.Set; - -import com.redhat.thermostat.storage.core.auth.CategoryRegistration; -import com.redhat.thermostat.vm.cpu.common.VmCpuStatDAO; - -/** - * Registers the category used by this maven module. The web storage - * endpoint only allows categories to be registered which it knows of - * ahead of time. - * - */ -public class VmCpuStatCategoryRegistration implements CategoryRegistration { - - @Override - public Set<String> getCategoryNames() { - Set<String> categories = new HashSet<>(1); - categories.add(VmCpuStatDAO.vmCpuStatCategory.getName()); - return categories; - } - -} -
--- a/plugins/vm-cpu/common/src/main/java/com/redhat/thermostat/vm/cpu/common/internal/VmCpuStatDAOImpl.java Mon Oct 09 15:56:10 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,133 +0,0 @@ -/* - * Copyright 2012-2017 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.vm.cpu.common.internal; - -import java.util.List; -import java.util.logging.Level; -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.DescriptorParsingException; -import com.redhat.thermostat.storage.core.Key; -import com.redhat.thermostat.storage.core.PreparedStatement; -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.VmBoundaryPojoGetter; -import com.redhat.thermostat.storage.core.VmId; -import com.redhat.thermostat.storage.core.VmLatestPojoListGetter; -import com.redhat.thermostat.storage.core.VmRef; -import com.redhat.thermostat.storage.core.VmTimeIntervalPojoListGetter; -import com.redhat.thermostat.storage.dao.AbstractDao; -import com.redhat.thermostat.storage.dao.AbstractDaoStatement; -import com.redhat.thermostat.storage.model.Pojo; -import com.redhat.thermostat.vm.cpu.common.VmCpuStatDAO; -import com.redhat.thermostat.vm.cpu.common.model.VmCpuStat; - -public class VmCpuStatDAOImpl extends AbstractDao implements VmCpuStatDAO { - - private static final Logger logger = LoggingUtils.getLogger(VmCpuStatDAOImpl.class); - // ADD vm-cpu-stats SET 'agentId' = ?s , \ - // 'vmId' = ?s , \ - // 'timeStamp' = ?l , \ - // 'cpuLoad' = ?d - static final String DESC_ADD_VM_CPU_STAT = "ADD " + vmCpuStatCategory.getName() + - " SET '" + Key.AGENT_ID.getName() + "' = ?s , " + - "'" + Key.VM_ID.getName() + "' = ?s , " + - "'" + Key.TIMESTAMP.getName() + "' = ?l , " + - "'" + vmCpuLoadKey.getName() + "' = ?d"; - - private final Storage storage; - private final VmLatestPojoListGetter<VmCpuStat> latestGetter; - private final VmTimeIntervalPojoListGetter<VmCpuStat> intervalGetter; - private final VmBoundaryPojoGetter<VmCpuStat> boundaryGetter; - - VmCpuStatDAOImpl(Storage storage) { - this.storage = storage; - storage.registerCategory(vmCpuStatCategory); - this.latestGetter = new VmLatestPojoListGetter<>(storage, vmCpuStatCategory); - this.intervalGetter = new VmTimeIntervalPojoListGetter<>(storage, vmCpuStatCategory); - this.boundaryGetter = new VmBoundaryPojoGetter<>(storage, vmCpuStatCategory); - } - - @Override - public List<VmCpuStat> getLatestVmCpuStats(VmRef ref, long since) { - return latestGetter.getLatest(ref, since); - } - - @Override - public List<VmCpuStat> getLatestVmCpuStats(AgentId agentId, VmId vmId, long since) { - return latestGetter.getLatest(agentId, vmId, since); - } - - @Override - public List<VmCpuStat> getVmCpuStats(VmRef ref, long since, long to) { - return intervalGetter.getLatest(ref, since, to); - } - - @Override - public VmCpuStat getNewest(VmRef ref) { - return boundaryGetter.getNewestStat(ref); - } - - @Override - public VmCpuStat getOldest(VmRef ref) { - return boundaryGetter.getOldestStat(ref); - } - - @Override - public void putVmCpuStat(final VmCpuStat stat) { - executeStatement(new AbstractDaoStatement<VmCpuStat>(storage, vmCpuStatCategory, DESC_ADD_VM_CPU_STAT) { - @Override - public PreparedStatement<VmCpuStat> customize(PreparedStatement<VmCpuStat> preparedStatement) { - preparedStatement.setString(0, stat.getAgentId()); - preparedStatement.setString(1, stat.getVmId()); - preparedStatement.setLong(2, stat.getTimeStamp()); - preparedStatement.setDouble(3, stat.getCpuLoad()); - return preparedStatement; - } - }); - } - - @Override - protected Logger getLogger() { - return logger; - } - -} -
--- a/plugins/vm-cpu/common/src/main/java/com/redhat/thermostat/vm/cpu/common/internal/VmCpuStatDAOImplStatementDescriptorRegistration.java Mon Oct 09 15:56:10 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,78 +0,0 @@ -/* - * Copyright 2012-2017 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.vm.cpu.common.internal; - -import java.util.HashSet; -import java.util.Set; - -import com.redhat.thermostat.storage.core.VmBoundaryPojoGetter; -import com.redhat.thermostat.storage.core.VmLatestPojoListGetter; -import com.redhat.thermostat.storage.core.VmTimeIntervalPojoListGetter; -import com.redhat.thermostat.storage.core.auth.StatementDescriptorRegistration; -import com.redhat.thermostat.vm.cpu.common.VmCpuStatDAO; - -/** - * Registers the prepared query issued by this maven module via - * {@link VmLatestPojoListGetter}. - * - */ -public class VmCpuStatDAOImplStatementDescriptorRegistration implements - StatementDescriptorRegistration { - - static final String latestDescriptor = String.format(VmLatestPojoListGetter.VM_LATEST_QUERY_FORMAT, - VmCpuStatDAO.vmCpuStatCategory.getName()); - static final String rangeDescriptor = String.format(VmTimeIntervalPojoListGetter.VM_INTERVAL_QUERY_FORMAT, - VmCpuStatDAO.vmCpuStatCategory.getName()); - static final String latestStatDescriptor = String.format(VmBoundaryPojoGetter.DESC_NEWEST_VM_STAT, - VmCpuStatDAO.vmCpuStatCategory.getName()); - static final String oldestStatDescriptor = String.format(VmBoundaryPojoGetter.DESC_OLDEST_VM_STAT, - VmCpuStatDAO.vmCpuStatCategory.getName()); - - - @Override - public Set<String> getStatementDescriptors() { - Set<String> descs = new HashSet<>(); - descs.add(VmCpuStatDAOImpl.DESC_ADD_VM_CPU_STAT); - descs.add(latestStatDescriptor); - descs.add(oldestStatDescriptor); - descs.add(latestDescriptor); - descs.add(rangeDescriptor); - return descs; - } - -} -
--- a/plugins/vm-cpu/common/src/main/java/com/redhat/thermostat/vm/cpu/common/model/VmCpuStat.java Mon Oct 09 15:56:10 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,98 +0,0 @@ -/* - * Copyright 2012-2017 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.vm.cpu.common.model; - -import com.redhat.thermostat.storage.core.Entity; -import com.redhat.thermostat.storage.core.Persist; -import com.redhat.thermostat.storage.model.BasePojo; -import com.redhat.thermostat.storage.model.TimeStampedPojo; - -@Entity -public class VmCpuStat extends BasePojo implements TimeStampedPojo { - - private long timeStamp; - private String vmId; - private double cpuLoad; - - public VmCpuStat() { - super(null); - } - - public VmCpuStat(String writerId, long timeStamp, String vmId, double cpuLoad) { - super(writerId); - this.timeStamp = timeStamp; - this.vmId = vmId; - this.cpuLoad = cpuLoad; - } - - @Override - @Persist - public long getTimeStamp() { - return timeStamp; - } - - @Persist - public void setTimeStamp(long timeStamp) { - this.timeStamp = timeStamp; - } - - @Persist - public String getVmId() { - return vmId; - } - - @Persist - public void setVmId(String vmId) { - this.vmId = vmId; - } - - /** - * The cpu load in percent (as in 100.0 for 100%). This value should be - * normalized to be in the range [0, 100] - */ - @Persist - public double getCpuLoad() { - return cpuLoad; - } - - @Persist - public void setCpuLoad(double cpuLoad) { - this.cpuLoad = cpuLoad; - } - -} -
--- a/plugins/vm-cpu/common/src/main/resources/META-INF/services/com.redhat.thermostat.storage.core.auth.CategoryRegistration Mon Oct 09 15:56:10 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -com.redhat.thermostat.vm.cpu.common.internal.VmCpuStatCategoryRegistration \ No newline at end of file
--- a/plugins/vm-cpu/common/src/main/resources/META-INF/services/com.redhat.thermostat.storage.core.auth.StatementDescriptorRegistration Mon Oct 09 15:56:10 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,1 +0,0 @@ -com.redhat.thermostat.vm.cpu.common.internal.VmCpuStatDAOImplStatementDescriptorRegistration \ No newline at end of file
--- a/plugins/vm-cpu/common/src/test/java/com/redhat/thermostat/vm/cpu/common/internal/ActivatorTest.java Mon Oct 09 15:56:10 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,85 +0,0 @@ -/* - * Copyright 2012-2017 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.vm.cpu.common.internal; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.mockito.Mockito.mock; - -import org.junit.Test; - -import com.redhat.thermostat.storage.core.Storage; -import com.redhat.thermostat.testutils.StubBundleContext; -import com.redhat.thermostat.vm.cpu.common.VmCpuStatDAO; - -public class ActivatorTest { - - @Test - public void verifyActivatorDoesNotRegisterServiceOnMissingDeps() throws Exception { - StubBundleContext context = new StubBundleContext(); - - Activator activator = new Activator(); - - activator.start(context); - - assertEquals(0, context.getAllServices().size()); - assertEquals(1, context.getServiceListeners().size()); - - activator.stop(context); - } - - @Test - public void verifyActivatorRegistersServices() throws Exception { - StubBundleContext context = new StubBundleContext(); - Storage storage = mock(Storage.class); - - context.registerService(Storage.class, storage, null); - - Activator activator = new Activator(); - - activator.start(context); - - assertTrue(context.isServiceRegistered(VmCpuStatDAO.class.getName(), VmCpuStatDAOImpl.class)); - - activator.stop(context); - - assertEquals(0, context.getServiceListeners().size()); - assertEquals(1, context.getAllServices().size()); - } - -} -
--- a/plugins/vm-cpu/common/src/test/java/com/redhat/thermostat/vm/cpu/common/internal/VmCpuStatCategoryRegistrationTest.java Mon Oct 09 15:56:10 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,67 +0,0 @@ -/* - * Copyright 2012-2017 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.vm.cpu.common.internal; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - -import java.util.Set; - -import org.junit.Test; - -import com.redhat.thermostat.storage.core.auth.CategoryRegistration; -import com.redhat.thermostat.testutils.ServiceLoaderTest; -import com.redhat.thermostat.vm.cpu.common.VmCpuStatDAO; - -public class VmCpuStatCategoryRegistrationTest extends ServiceLoaderTest<CategoryRegistration> { - - public VmCpuStatCategoryRegistrationTest() { - super(CategoryRegistration.class, STORAGE_SERVICES, VmCpuStatCategoryRegistration.class); - } - - @Test - public void registersAllCategories() { - VmCpuStatCategoryRegistration reg = new VmCpuStatCategoryRegistration(); - Set<String> categories = reg.getCategoryNames(); - assertEquals(1, categories.size()); - assertFalse("null descriptor not allowed", categories.contains(null)); - assertTrue(categories.contains(VmCpuStatDAO.vmCpuStatCategory.getName())); - } - -} -
--- a/plugins/vm-cpu/common/src/test/java/com/redhat/thermostat/vm/cpu/common/internal/VmCpuStatDAOImplStatementDescriptorRegistrationTest.java Mon Oct 09 15:56:10 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,64 +0,0 @@ -/* - * Copyright 2012-2017 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.vm.cpu.common.internal; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; - -import java.util.Set; - -import org.junit.Test; - -import com.redhat.thermostat.storage.core.auth.StatementDescriptorRegistration; -import com.redhat.thermostat.testutils.ServiceLoaderTest; - -public class VmCpuStatDAOImplStatementDescriptorRegistrationTest extends ServiceLoaderTest<StatementDescriptorRegistration> { - - public VmCpuStatDAOImplStatementDescriptorRegistrationTest() { - super(StatementDescriptorRegistration.class, STORAGE_SERVICES, VmCpuStatDAOImplStatementDescriptorRegistration.class); - } - - @Test - public void registersAllDescriptors() { - VmCpuStatDAOImplStatementDescriptorRegistration reg = new VmCpuStatDAOImplStatementDescriptorRegistration(); - Set<String> descriptors = reg.getStatementDescriptors(); - assertEquals(5, descriptors.size()); - assertFalse("null descriptor not allowed", descriptors.contains(null)); - } - -} -
--- a/plugins/vm-cpu/common/src/test/java/com/redhat/thermostat/vm/cpu/common/internal/VmCpuStatDAOTest.java Mon Oct 09 15:56:10 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,221 +0,0 @@ -/* - * Copyright 2012-2017 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.vm.cpu.common.internal; - -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; -import static org.mockito.Matchers.any; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; -import static org.mockito.Mockito.when; - -import java.util.Collection; -import java.util.List; - -import org.junit.Before; -import org.junit.Test; -import org.mockito.ArgumentCaptor; - -import com.redhat.thermostat.common.Pair; -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; -import com.redhat.thermostat.storage.core.Key; -import com.redhat.thermostat.storage.core.PreparedStatement; -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.testutils.StatementDescriptorTester; -import com.redhat.thermostat.vm.cpu.common.VmCpuStatDAO; -import com.redhat.thermostat.vm.cpu.common.model.VmCpuStat; - -public class VmCpuStatDAOTest { - - private static final Long TIMESTAMP = 1234L; - private static final String VM_ID = "321"; - private static final Double CPU_LOAD = 9.9; - - private VmCpuStat cpuStat; - - @Before - public void setUp() { - cpuStat = new VmCpuStat("foo-agent", TIMESTAMP, VM_ID, CPU_LOAD); - } - - @Test - public void verifyDescriptorsAreSane() { - String addCpuStat = "ADD vm-cpu-stats SET 'agentId' = ?s , " + - "'vmId' = ?s , " + - "'timeStamp' = ?l , " + - "'cpuLoad' = ?d"; - assertEquals(addCpuStat, VmCpuStatDAOImpl.DESC_ADD_VM_CPU_STAT); - } - - @Test - public void canParseDescriptor() { - StatementDescriptorTester<VmCpuStat> tester = new StatementDescriptorTester<>(); - StatementDescriptor<VmCpuStat> desc = new StatementDescriptor<>(VmCpuStatDAO.vmCpuStatCategory, VmCpuStatDAOImpl.DESC_ADD_VM_CPU_STAT); - try { - tester.testParseBasic(desc); - tester.testParseSemantic(desc); - // pass - } catch (DescriptorParsingException e) { - fail(e.getMessage()); - } - } - - @Test - public void testCategory() { - assertEquals("vm-cpu-stats", VmCpuStatDAO.vmCpuStatCategory.getName()); - Collection<Key<?>> keys = VmCpuStatDAO.vmCpuStatCategory.getKeys(); - assertTrue(keys.contains(new Key<>("agentId"))); - assertTrue(keys.contains(new Key<Long>("timeStamp"))); - assertTrue(keys.contains(new Key<Integer>("vmId"))); - assertTrue(keys.contains(new Key<Integer>("cpuLoad"))); - assertEquals(4, keys.size()); - } - - @Test - public void testVmRefGetLatestCpuStatsBasic() throws DescriptorParsingException, StatementExecutionException { - Pair<Storage, PreparedStatement<VmCpuStat>> setup = setupGetLatest(); - Storage storage = setup.getFirst(); - PreparedStatement<VmCpuStat> stmt = setup.getSecond(); - - HostRef hostRef = mock(HostRef.class); - when(hostRef.getAgentId()).thenReturn("system"); - - VmRef vmRef = mock(VmRef.class); - when(vmRef.getHostRef()).thenReturn(hostRef); - when(vmRef.getVmId()).thenReturn(VM_ID); - - VmCpuStatDAO dao = new VmCpuStatDAOImpl(storage); - List<VmCpuStat> vmCpuStats = dao.getLatestVmCpuStats(vmRef, Long.MIN_VALUE); - - verify(storage).prepareStatement(anyDescriptor()); - verify(stmt).setString(0, "system"); - verify(stmt).setString(1, VM_ID); - verify(stmt).setLong(2, Long.MIN_VALUE); - verify(stmt).executeQuery(); - verifyNoMoreInteractions(stmt); - - assertEquals(1, vmCpuStats.size()); - VmCpuStat stat = vmCpuStats.get(0); - assertEquals(TIMESTAMP, (Long) stat.getTimeStamp()); - assertEquals(CPU_LOAD, stat.getCpuLoad(), 0.001); - assertEquals(VM_ID, stat.getVmId()); - } - - @Test - public void testGetLatestCpuStatsBasic() throws DescriptorParsingException, StatementExecutionException { - Pair<Storage, PreparedStatement<VmCpuStat>> setup = setupGetLatest(); - Storage storage = setup.getFirst(); - PreparedStatement<VmCpuStat> stmt = setup.getSecond(); - - AgentId agentId = new AgentId("system"); - VmId vmId = new VmId(VM_ID); - - VmCpuStatDAO dao = new VmCpuStatDAOImpl(storage); - List<VmCpuStat> vmCpuStats = dao.getLatestVmCpuStats(agentId, vmId, Long.MIN_VALUE); - - verify(storage).prepareStatement(anyDescriptor()); - verify(stmt).setString(0, "system"); - verify(stmt).setString(1, VM_ID); - verify(stmt).setLong(2, Long.MIN_VALUE); - verify(stmt).executeQuery(); - verifyNoMoreInteractions(stmt); - - assertEquals(1, vmCpuStats.size()); - VmCpuStat stat = vmCpuStats.get(0); - assertEquals(TIMESTAMP, (Long) stat.getTimeStamp()); - assertEquals(CPU_LOAD, stat.getCpuLoad(), 0.001); - assertEquals(VM_ID, stat.getVmId()); - } - - private Pair<Storage, PreparedStatement<VmCpuStat>> setupGetLatest() throws - DescriptorParsingException, StatementExecutionException { - - @SuppressWarnings("unchecked") - Cursor<VmCpuStat> cursor = (Cursor<VmCpuStat>) mock(Cursor.class); - when(cursor.hasNext()).thenReturn(true).thenReturn(false); - when(cursor.next()).thenReturn(cpuStat); - - Storage storage = mock(Storage.class); - @SuppressWarnings("unchecked") - PreparedStatement<VmCpuStat> stmt = (PreparedStatement<VmCpuStat>) mock(PreparedStatement.class); - when(storage.prepareStatement(anyDescriptor())).thenReturn(stmt); - when(stmt.executeQuery()).thenReturn(cursor); - - return new Pair<>(storage, stmt); - } - - @SuppressWarnings("unchecked") - private StatementDescriptor<VmCpuStat> anyDescriptor() { - return (StatementDescriptor<VmCpuStat>) any(StatementDescriptor.class); - } - - @SuppressWarnings("unchecked") - @Test - public void testPutVmCpuStat() throws DescriptorParsingException, StatementExecutionException { - Storage storage = mock(Storage.class); - PreparedStatement<VmCpuStat> add = mock(PreparedStatement.class); - when(storage.prepareStatement(any(StatementDescriptor.class))).thenReturn(add); - - VmCpuStat stat = new VmCpuStat("foo-agent", TIMESTAMP, VM_ID, CPU_LOAD); - VmCpuStatDAO dao = new VmCpuStatDAOImpl(storage); - dao.putVmCpuStat(stat); - - @SuppressWarnings("rawtypes") - ArgumentCaptor<StatementDescriptor> captor = ArgumentCaptor.forClass(StatementDescriptor.class); - - verify(storage).prepareStatement(captor.capture()); - StatementDescriptor<?> desc = captor.getValue(); - assertEquals(VmCpuStatDAOImpl.DESC_ADD_VM_CPU_STAT, desc.getDescriptor()); - - verify(add).setString(0, stat.getAgentId()); - verify(add).setString(1, stat.getVmId()); - verify(add).setLong(2, stat.getTimeStamp()); - verify(add).setDouble(3, stat.getCpuLoad()); - verify(add).execute(); - verifyNoMoreInteractions(add); - } -} -
--- a/plugins/vm-cpu/common/src/test/java/com/redhat/thermostat/vm/cpu/common/model/VmCpuStatTest.java Mon Oct 09 15:56:10 2017 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,48 +0,0 @@ -/* - * Copyright 2012-2017 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.vm.cpu.common.model; - -import com.redhat.thermostat.testutils.DataObjectTest; - -public class VmCpuStatTest extends DataObjectTest { - - @Override - public Class<?>[] getDataClasses() { - return new Class[] { VmCpuStat.class }; - } -} -
--- a/plugins/vm-cpu/distribution/assemblies/plugin-assembly.xml Mon Oct 09 15:56:10 2017 +0200 +++ b/plugins/vm-cpu/distribution/assemblies/plugin-assembly.xml Tue Oct 10 12:29:18 2017 -0400 @@ -43,26 +43,30 @@ <formats> <format>zip</format> </formats> - <baseDirectory>${thermostat.plugin}</baseDirectory> - <includeBaseDirectory>true</includeBaseDirectory> + <includeBaseDirectory>false</includeBaseDirectory> <dependencySets> <dependencySet> <includes> - <include>com.redhat.thermostat.agent:thermostat-vm-cpu-common</include> <include>com.redhat.thermostat.agent:thermostat-vm-cpu-agent</include> </includes> <useProjectArtifact>false</useProjectArtifact> <useStrictFiltering>true</useStrictFiltering> + <outputDirectory>plugins/${thermostat.plugin}</outputDirectory> </dependencySet> </dependencySets> - - <files> - <file> - <source>thermostat-plugin.xml</source> - <outputDirectory>/</outputDirectory> + <fileSets> + <fileSet> + <includes> + <include>thermostat-plugin.xml</include> + </includes> + <outputDirectory>plugins/${thermostat.plugin}</outputDirectory> <filtered>true</filtered> - </file> - </files> + </fileSet> + <fileSet> + <directory>configFiles</directory> + <outputDirectory>etc/plugins.d/${thermostat.plugin}</outputDirectory> + <filtered>true</filtered> + </fileSet> + </fileSets> </assembly> -
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/plugins/vm-cpu/distribution/configFiles/gateway.properties Tue Oct 10 12:29:18 2017 -0400 @@ -0,0 +1,2 @@ +# URL to the jvm-cpu microservice provided by the Thermostat web gateway +gatewayURL=https://localhost:30000/jvm-cpu/0.0.1
--- a/plugins/vm-cpu/distribution/thermostat-plugin.xml Mon Oct 09 15:56:10 2017 +0200 +++ b/plugins/vm-cpu/distribution/thermostat-plugin.xml Tue Oct 10 12:29:18 2017 -0400 @@ -43,7 +43,6 @@ <extension> <name>agent</name> <bundles> - <bundle><symbolic-name>com.redhat.thermostat.vm.cpu.common</symbolic-name><version>${project.version}</version></bundle> <bundle><symbolic-name>com.redhat.thermostat.vm.cpu.agent</symbolic-name><version>${project.version}</version></bundle> </bundles> </extension>