changeset 2520:2bc6a978f664

Use listening backend for vm-io plugin. Reviewed-by: aazores Review-thread: http://icedtea.classpath.org/pipermail/thermostat/2016-November/021583.html
author Jie Kang <jkang@redhat.com>
date Mon, 14 Nov 2016 11:34:41 -0500
parents 8ffabc1db29a
children 4c9a6a364937
files vm-io/agent/src/main/java/com/redhat/thermostat/vm/io/agent/internal/Activator.java vm-io/agent/src/main/java/com/redhat/thermostat/vm/io/agent/internal/VmIoBackend.java vm-io/agent/src/test/java/com/redhat/thermostat/vm/io/agent/internal/VmIoBackendTest.java
diffstat 3 files changed, 31 insertions(+), 65 deletions(-) [+]
line wrap: on
line diff
--- a/vm-io/agent/src/main/java/com/redhat/thermostat/vm/io/agent/internal/Activator.java	Thu Nov 10 15:23:19 2016 -0500
+++ b/vm-io/agent/src/main/java/com/redhat/thermostat/vm/io/agent/internal/Activator.java	Mon Nov 14 11:34:41 2016 -0500
@@ -57,7 +57,6 @@
 
 public class Activator implements BundleActivator {
 
-    private ScheduledExecutorService executor;
     private MultipleServiceTracker tracker;
     private VmIoBackend backend;
     private ServiceRegistration<Backend> reg;
@@ -66,8 +65,6 @@
     public void start(final BundleContext context) throws Exception {
         final VmStatusListenerRegistrar registrar = new VmStatusListenerRegistrar(context);
 
-        executor = Executors.newSingleThreadScheduledExecutor();
-
         Class<?>[] deps = new Class<?>[] {
                 BackendService.class,
                 VmIoStatDAO.class,
@@ -80,7 +77,7 @@
                 Version version = new Version(context.getBundle());
                 WriterID writerId = services.get(WriterID.class);
                 Clock clock = new SystemClock();
-                backend = new VmIoBackend(clock, executor, version, vmIoStatDao, registrar, writerId);
+                backend = new VmIoBackend(clock, version, vmIoStatDao, registrar, writerId);
                 reg = context.registerService(Backend.class, backend, null);
             }
 
--- a/vm-io/agent/src/main/java/com/redhat/thermostat/vm/io/agent/internal/VmIoBackend.java	Thu Nov 10 15:23:19 2016 -0500
+++ b/vm-io/agent/src/main/java/com/redhat/thermostat/vm/io/agent/internal/VmIoBackend.java	Mon Nov 14 11:34:41 2016 -0500
@@ -36,12 +36,11 @@
 
 package com.redhat.thermostat.vm.io.agent.internal;
 
-import java.util.concurrent.ScheduledExecutorService;
-
 import com.redhat.thermostat.agent.VmStatusListenerRegistrar;
 import com.redhat.thermostat.agent.utils.ProcDataSource;
-import com.redhat.thermostat.backend.VmPollingAction;
-import com.redhat.thermostat.backend.VmPollingBackend;
+import com.redhat.thermostat.backend.VmListenerBackend;
+import com.redhat.thermostat.backend.VmUpdate;
+import com.redhat.thermostat.backend.VmUpdateListener;
 import com.redhat.thermostat.common.Clock;
 import com.redhat.thermostat.common.Version;
 import com.redhat.thermostat.storage.core.WriterID;
@@ -49,47 +48,57 @@
 import com.redhat.thermostat.vm.io.common.VmIoStat;
 import com.redhat.thermostat.vm.io.common.VmIoStatDAO;
 
-public class VmIoBackend extends VmPollingBackend {
+public class VmIoBackend extends VmListenerBackend {
 
-    public VmIoBackend(Clock clock, ScheduledExecutorService executor, Version version,
+    private VmIoStatDAO vmIoStatDAO;
+    private VmIoStatBuilder builder;
+
+    public VmIoBackend(Clock clock, Version version,
             VmIoStatDAO vmIoStatDao,
             VmStatusListenerRegistrar registrar, WriterID writerId) {
-        this(clock, executor, version,
+        this(version,
                 vmIoStatDao,
                 new VmIoStatBuilder(clock, new ProcIoDataReader(new ProcDataSource()), writerId),
                 registrar, writerId);
     }
 
-    VmIoBackend(Clock clock, ScheduledExecutorService executor, Version version,
-            VmIoStatDAO vmIoStatDao, VmIoStatBuilder vmIoStatBuilder,
+    VmIoBackend(Version version,
+            VmIoStatDAO vmIoStatDao, VmIoStatBuilder builder,
             VmStatusListenerRegistrar registrar, WriterID writerId) {
         super("VM IO Backend",
               "Gathers IO statistics about a JVM",
               "Red Hat, Inc.",
-              version, executor, registrar);
+              version.getVersionNumber(), true , registrar, writerId);
+        this.vmIoStatDAO = vmIoStatDao;
+        this.builder = builder;
+    }
 
-        VmIoBackendAction action = new VmIoBackendAction(vmIoStatDao, vmIoStatBuilder);
-        registerAction(action);
+    @Override
+    protected VmUpdateListener createVmListener(String writerId, String vmId, int pid) {
+        return new VmIoBackendListener(vmIoStatDAO, builder, vmId, pid);
     }
 
-    private static class VmIoBackendAction implements VmPollingAction {
-
-        private VmIoStatDAO dao;
+    private static class VmIoBackendListener implements VmUpdateListener {
+        private VmIoStatDAO vmIoStatDAO;
         private VmIoStatBuilder builder;
+        private String vmId;
+        private int pid;
 
-        private VmIoBackendAction(VmIoStatDAO dao, VmIoStatBuilder builder) {
-            this.dao = dao;
+
+        public VmIoBackendListener (VmIoStatDAO vmIoStatDAO, VmIoStatBuilder builder, String vmId, int pid) {
+            this.vmIoStatDAO = vmIoStatDAO;
             this.builder = builder;
+            this.vmId = vmId;
+            this.pid = pid;
         }
 
         @Override
-        public void run(String vmId, int pid) {
+        public void countersUpdated(VmUpdate update) {
             VmIoStat dataBuilt = builder.build(vmId, pid);
             if (dataBuilt != null) {
-                dao.putVmIoStat(dataBuilt);
+                vmIoStatDAO.putVmIoStat(dataBuilt);
             }
         }
-        
     }
 
     @Override
--- a/vm-io/agent/src/test/java/com/redhat/thermostat/vm/io/agent/internal/VmIoBackendTest.java	Thu Nov 10 15:23:19 2016 -0500
+++ b/vm-io/agent/src/test/java/com/redhat/thermostat/vm/io/agent/internal/VmIoBackendTest.java	Mon Nov 14 11:34:41 2016 -0500
@@ -67,15 +67,12 @@
 public class VmIoBackendTest {
 
     private VmIoBackend backend;
-    private ScheduledExecutorService executor;
     private VmIoStatDAO vmIoStatDao;
     private VmStatusListenerRegistrar registrar;
     private VmIoStatBuilder ioStatBuilder;
 
     @Before
     public void setup() {
-        Clock clock = mock(Clock.class);
-        executor = mock(ScheduledExecutorService.class);
         vmIoStatDao = mock(VmIoStatDAO.class);
 
         Version version = mock(Version.class);
@@ -85,14 +82,13 @@
 
         WriterID id = mock(WriterID.class);
         ioStatBuilder = mock(VmIoStatBuilder.class);
-        backend = new VmIoBackend(clock, executor, version, vmIoStatDao, ioStatBuilder, registrar, id);
+        backend = new VmIoBackend(version, vmIoStatDao, ioStatBuilder, registrar, id);
     }
 
     @Test
     public void testActivate() {
         backend.activate();
 
-        verify(executor).scheduleAtFixedRate(isA(Runnable.class), eq(0l), eq(1000l), eq(TimeUnit.MILLISECONDS));
         verify(registrar).register(backend);
         assertTrue(backend.isActive());
     }
@@ -112,7 +108,6 @@
         backend.activate();
         backend.deactivate();
 
-        verify(executor).shutdown();
         verify(registrar).unregister(backend);
         assertFalse(backend.isActive());
     }
@@ -129,41 +124,6 @@
     }
 
     @Test
-    public void testStart() {
-        // Setup Runnable mocks
-        final Set<Integer> pids = new HashSet<>();
-        pids.add(0);
-        pids.add(1);
-
-        VmIoStat stat0 = mock(VmIoStat.class);
-        VmIoStat stat1 = mock(VmIoStat.class);
-        when(ioStatBuilder.build("vm1", 0)).thenReturn(stat0);
-        when(ioStatBuilder.build("vm2", 1)).thenReturn(stat1);
-
-        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());
-
-        backend.vmStatusChanged(Status.VM_ACTIVE, "vm1", 0);
-        backend.vmStatusChanged(Status.VM_STARTED, "vm2", 1);
-
-        Runnable runnable = captor.getValue();
-        runnable.run();
-        verify(vmIoStatDao).putVmIoStat(stat0);
-        verify(vmIoStatDao).putVmIoStat(stat1);
-
-        backend.vmStatusChanged(Status.VM_STOPPED, "vm1", 0);
-        backend.vmStatusChanged(Status.VM_STOPPED, "vm2", 1);
-
-        runnable.run();
-
-        verifyNoMoreInteractions(vmIoStatDao);
-    }
-
-    @Test
     public void testOrderValue() {
         int orderValue = backend.getOrderValue();