changeset 1637:c9f9c00e3cbb

Add failfast to query result handlers review-thread: http://icedtea.classpath.org/pipermail/thermostat/2014-December/012284.html reviewed-by: jerboaa
author Mario Torre <neugens.limasoftware@gmail.com>
date Mon, 15 Dec 2014 13:23:00 +0100
parents 24a440f57aed
children 461cef9a84c4
files storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/BeanAdapter.java storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/ResultHandler.java storage/core/src/test/java/com/redhat/thermostat/storage/core/experimental/statement/BeanAdapterTest.java thread/client-common/src/main/java/com/redhat/thermostat/thread/client/common/collector/impl/ThreadMXBeanCollector.java thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/ThreadTableController.java thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/ThreadTimelineController.java thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoImpl.java
diffstat 7 files changed, 70 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/BeanAdapter.java	Fri Dec 12 13:39:35 2014 -0700
+++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/BeanAdapter.java	Mon Dec 15 13:23:00 2014 +0100
@@ -158,10 +158,11 @@
         }
 
         Cursor<T> cursor = prepared.executeQuery();
-        while (cursor.hasNext()) {
+        boolean needMoreResults = true;
+        while (cursor.hasNext() && needMoreResults) {
             T result = cursor.next();
             try {
-                handler.onResult(result);
+                needMoreResults = handler.onResult(result);
 
             } catch (Throwable t) {
                 logger.log(Level.SEVERE, "Exception executing results", t);
--- a/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/ResultHandler.java	Fri Dec 12 13:39:35 2014 -0700
+++ b/storage/core/src/main/java/com/redhat/thermostat/storage/core/experimental/statement/ResultHandler.java	Mon Dec 15 13:23:00 2014 +0100
@@ -40,5 +40,13 @@
  *
  */
 public interface ResultHandler<T> {
-    void onResult(T result);
+    /**
+     * The return value indicates whether or not the statement engine should
+     * continue returning results as long as they are available. If the handler
+     * return {@code false}, the engine will not invoke this method anymore;
+     * it is still possible that the statement engine has more results already
+     * loaded in memory as a result of caching or other optimisation, however
+     * no more interaction with this handler will be performed.
+     */
+    boolean onResult(T result);
 }
--- a/storage/core/src/test/java/com/redhat/thermostat/storage/core/experimental/statement/BeanAdapterTest.java	Fri Dec 12 13:39:35 2014 -0700
+++ b/storage/core/src/test/java/com/redhat/thermostat/storage/core/experimental/statement/BeanAdapterTest.java	Mon Dec 15 13:23:00 2014 +0100
@@ -53,6 +53,7 @@
 import static org.junit.Assert.assertTrue;
 import static org.mockito.Matchers.any;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.verifyNoMoreInteractions;
 import static org.mockito.Mockito.when;
@@ -185,8 +186,9 @@
         final boolean [] called = new boolean[1];
         ResultHandler<SampleBean> handler = new ResultHandler<SampleBean>() {
             @Override
-            public void onResult(SampleBean result) {
+            public boolean onResult(SampleBean result) {
                 called[0] = true;
+                return true;
             }
         };
 
@@ -227,8 +229,9 @@
         final boolean [] called = new boolean[1];
         ResultHandler<SampleBean> handler = new ResultHandler<SampleBean>() {
             @Override
-            public void onResult(SampleBean result) {
+            public boolean onResult(SampleBean result) {
                 called[0] = true;
+                return true;
             }
         };
 
@@ -241,4 +244,42 @@
 
         assertTrue(called[0]);
     }
+
+    @Test
+    public void testSkipResultsOnHandlerRequest() throws Exception {
+        when(storage.prepareStatement(any(StatementDescriptor.class))).thenReturn(query);
+        Cursor cursor = mock(Cursor.class);
+
+        final SampleBean sample0 = mock(SampleBean.class);
+        final SampleBean sample1 = mock(SampleBean.class);
+        final SampleBean sample2 = mock(SampleBean.class);
+
+        when(query.executeQuery()).thenReturn(cursor);
+        when(cursor.hasNext()).thenReturn(true).thenReturn(true).thenReturn(true);
+        when(cursor.next()).thenReturn(sample0).thenReturn(sample1).thenReturn(sample2);
+
+        BeanAdapterBuilder<SampleBean> builder =
+                new BeanAdapterBuilder<>(SampleBean.class, queries);
+
+        BeanAdapter<SampleBean> adapter = builder.build();
+        Query range = adapter.getQuery(RANGE);
+        QueryValues values = range.createValues();
+        values.set(new Id("0"), "fluff");
+        values.set(new Id("1"), 10l);
+        values.set(new Id("2"), 20l);
+        values.set(new Id("3"), 2);
+
+        ResultHandler<SampleBean> handler = new ResultHandler<SampleBean>() {
+            @Override
+            public boolean onResult(SampleBean result) {
+                if (result.equals(sample1)) {
+                    return false;
+                }
+                return true;
+            }
+        };
+        adapter.query(values, handler, storage);
+
+        verify(cursor, times(2)).next();
+    }
 }
--- a/thread/client-common/src/main/java/com/redhat/thermostat/thread/client/common/collector/impl/ThreadMXBeanCollector.java	Fri Dec 12 13:39:35 2014 -0700
+++ b/thread/client-common/src/main/java/com/redhat/thermostat/thread/client/common/collector/impl/ThreadMXBeanCollector.java	Mon Dec 15 13:23:00 2014 +0100
@@ -174,8 +174,9 @@
         threadDao.getThreadStates(ref, session,
                                   new ResultHandler<ThreadState>() {
                                       @Override
-                                      public void onResult(ThreadState result) {
+                                      public boolean onResult(ThreadState result) {
                                           timestamps[1] = result.getTimeStamp();
+                                          return false;
                                       }
                                   },
                                   FULL_RANGE, FIRST, ThreadDao.Sort.DESCENDING);
@@ -183,8 +184,9 @@
         threadDao.getThreadStates(ref, session,
                                   new ResultHandler<ThreadState>() {
                                       @Override
-                                      public void onResult(ThreadState result) {
+                                      public boolean onResult(ThreadState result) {
                                           timestamps[0] = result.getTimeStamp();
+                                          return false;
                                       }
                                   },
                                   FULL_RANGE, FIRST, ThreadDao.Sort.ASCENDING);
@@ -224,11 +226,6 @@
         postAndWait(harvester);
     }
 
-//    @Override
-//    public ThreadContentionSample getLatestContentionSample(ThreadHeader thread) {
-//        return threadDao.getLatestContentionSample(thread);
-//    }
-
     private boolean postAndWait(Request harvester) {
         final CountDownLatch latch = new CountDownLatch(1);
         final boolean[] result = new boolean[1];
--- a/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/ThreadTableController.java	Fri Dec 12 13:39:35 2014 -0700
+++ b/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/ThreadTableController.java	Mon Dec 15 13:23:00 2014 +0100
@@ -124,7 +124,7 @@
         }
 
         @Override
-        public void onResult(ThreadState thread) {
+        public boolean onResult(ThreadState thread) {
 
             ThreadInfo key = new ThreadInfo();
             key.setName(thread.getName());
@@ -165,6 +165,8 @@
             bean.setStopTimeStamp(thread.getTimeStamp());
 
             threadTableView.display(bean);
+
+            return true;
         }
     }
 
--- a/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/ThreadTimelineController.java	Fri Dec 12 13:39:35 2014 -0700
+++ b/thread/client-controllers/src/main/java/com/redhat/thermostat/thread/client/controller/impl/ThreadTimelineController.java	Mon Dec 15 13:23:00 2014 +0100
@@ -147,7 +147,7 @@
         }
 
         @Override
-        public void onResult(ThreadState state) {
+        public boolean onResult(ThreadState state) {
 
             key.setName(state.getName());
             key.setId(state.getId());
@@ -160,6 +160,8 @@
 
             TimelineProbe probe = TimelineFactory.createTimelineProbe(state);
             view.addProbe(info, probe);
+
+            return true;
         }
     }
 }
--- a/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoImpl.java	Fri Dec 12 13:39:35 2014 -0700
+++ b/thread/collector/src/main/java/com/redhat/thermostat/thread/dao/impl/ThreadDaoImpl.java	Mon Dec 15 13:23:00 2014 +0100
@@ -55,18 +55,16 @@
 import com.redhat.thermostat.storage.core.experimental.statement.ResultHandler;
 import com.redhat.thermostat.storage.model.Pojo;
 import com.redhat.thermostat.thread.dao.ThreadDao;
+import com.redhat.thermostat.thread.dao.impl.statement.SessionQueries;
 import com.redhat.thermostat.thread.dao.impl.statement.StateQueries;
-import com.redhat.thermostat.thread.dao.impl.statement.SessionQueries;
 import com.redhat.thermostat.thread.dao.impl.statement.SummaryQuery;
 import com.redhat.thermostat.thread.model.SessionID;
-import com.redhat.thermostat.thread.model.ThreadContentionSample;
 import com.redhat.thermostat.thread.model.ThreadHarvestingStatus;
 import com.redhat.thermostat.thread.model.ThreadSession;
 import com.redhat.thermostat.thread.model.ThreadState;
 import com.redhat.thermostat.thread.model.ThreadSummary;
 import com.redhat.thermostat.thread.model.VmDeadLockData;
 import java.util.ArrayList;
-import java.util.Collections;
 import java.util.List;
 import java.util.logging.Level;
 import java.util.logging.Logger;
@@ -198,8 +196,9 @@
         try {
             ThreadSummaryAdapter.query(values, new ResultHandler<ThreadSummary>() {
                 @Override
-                public void onResult(ThreadSummary result) {
+                public boolean onResult(ThreadSummary result) {
                     results.add(result);
+                    return true;
                 }
             }, storage);
 
@@ -232,8 +231,9 @@
         try {
             ThreadSessionAdapter.query(values, new ResultHandler<ThreadSession>() {
                 @Override
-                public void onResult(ThreadSession result) {
+                public boolean onResult(ThreadSession result) {
                     results.add(result);
+                    return true;
                 }
             }, storage);