tmf.core: Add failure causes to analyses
authorGeneviève Bastien <gbastien+lttng@versatic.net>
Thu, 27 Oct 2016 16:45:25 +0000 (12:45 -0400)
committerGenevieve Bastien <gbastien+lttng@versatic.net>
Mon, 30 Jan 2017 14:12:33 +0000 (09:12 -0500)
And state system analyses can fail when their event requests failed.

Change-Id: Id3bcaf88378cd16e8f0f7ac7aa4d2f0c7c574392
Signed-off-by: Geneviève Bastien <gbastien+lttng@versatic.net>
Reviewed-on: https://git.eclipse.org/r/84037
Reviewed-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Tested-by: Matthew Khouzam <matthew.khouzam@ericsson.com>
Reviewed-by: Hudson CI
tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/statesystem/StateSystemAnalysisModuleTest.java
tmf/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/analysis/TestStateSystemModule.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/analysis/IAnalysisModule.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/analysis/TmfAbstractAnalysisModule.java
tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/statesystem/TmfStateSystemAnalysisModule.java

index d2ff0b4301928574d7fe8602cc630984fa2f08b7..1ecee267bbf599a870b1f7d8adfb041dff495f0e 100644 (file)
@@ -306,4 +306,31 @@ public class StateSystemAnalysisModuleTest {
             module2.dispose();
         }
     }
+
+    /**
+     * Test that an analysis whose event request throws an exception is failed
+     * correctly
+     *
+     * @throws TmfAnalysisException
+     *             Propagates exceptions
+     */
+    @Test
+    public void testRequestFailure() throws TmfAnalysisException {
+        TestStateSystemModule module = new TestStateSystemModule();
+        module.setRequestAction(e -> {
+            throw new IllegalArgumentException("This exception is desired and part of the test");
+        });
+        try {
+            ITmfTrace trace = fTrace;
+            assertNotNull(trace);
+            module.setTrace(trace);
+
+            // Execute the module that should throw the exception
+            module.schedule();
+            assertFalse(module.waitForCompletion());
+
+        } finally {
+            module.dispose();
+        }
+    }
 }
index e94d28a68fda3286302cb9f8d9ef154ec546e222..25cb1428f65d30ec4b4f175efa2feca384c80046 100644 (file)
@@ -15,11 +15,17 @@ package org.eclipse.tracecompass.tmf.tests.stubs.analysis;
 import static org.eclipse.tracecompass.common.core.NonNullUtils.checkNotNull;
 
 import java.io.File;
+import java.util.function.Function;
 
+import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.NonNullByDefault;
 import org.eclipse.jdt.annotation.Nullable;
+import org.eclipse.tracecompass.common.core.NonNullUtils;
+import org.eclipse.tracecompass.tmf.core.event.ITmfEvent;
+import org.eclipse.tracecompass.tmf.core.request.ITmfEventRequest;
 import org.eclipse.tracecompass.tmf.core.statesystem.ITmfStateProvider;
 import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModule;
+import org.eclipse.tracecompass.tmf.core.timestamp.TmfTimeRange;
 
 /**
  * Test State System module
@@ -29,9 +35,10 @@ import org.eclipse.tracecompass.tmf.core.statesystem.TmfStateSystemAnalysisModul
 @NonNullByDefault
 public class TestStateSystemModule extends TmfStateSystemAnalysisModule {
 
-    private @Nullable TestStateSystemProvider fProvider = null;
-    private boolean fThrottleEvents = false;
     private final boolean fOnDisk;
+    private boolean fThrottleEvents = false;
+    private @Nullable TestStateSystemProvider fProvider = null;
+    private Function<ITmfEvent, ITmfEvent> fRequestAction = e -> e;
 
     /**
      * Constructor
@@ -103,4 +110,38 @@ public class TestStateSystemModule extends TmfStateSystemAnalysisModule {
         return super.getSsFile();
     }
 
+    /**
+     * Set a function that will be executed on an event and return an event.
+     * This action will be executed in the event request, before calling the
+     * parent's handleData method.
+     *
+     * @param requestAction
+     *            The function to execute on the event
+     */
+    public void setRequestAction(Function<ITmfEvent, ITmfEvent> requestAction) {
+        fRequestAction = requestAction;
+    }
+
+    @Override
+    protected @NonNull ITmfEventRequest createEventRequest(@NonNull ITmfStateProvider stateProvider, @NonNull TmfTimeRange timeRange, int nbRead) {
+        return new TestStateSystemRequest(stateProvider, timeRange, nbRead, fRequestAction);
+    }
+
+    private class TestStateSystemRequest extends StateSystemEventRequest {
+
+        private final Function<ITmfEvent, ITmfEvent> fAction;
+
+        public TestStateSystemRequest(ITmfStateProvider sp, TmfTimeRange timeRange, int index, Function<ITmfEvent, ITmfEvent> requestAction) {
+            super(sp, timeRange, index);
+            fAction = requestAction;
+        }
+
+        @Override
+        public void handleData(final ITmfEvent event) {
+            ITmfEvent ev = NonNullUtils.checkNotNull(fAction.apply(event));
+            super.handleData(ev);
+        }
+
+    }
+
 }
index c44a8a254ee2ef72348b958d0bce5956f4cf645f..ac71802d51f8719ce88e7b64cd206bc900b22860 100644 (file)
@@ -245,6 +245,16 @@ public interface IAnalysisModule extends ITmfComponent, IAnalysisRequirementProv
      */
     void cancel();
 
+    /**
+     * Makes the analysis fail with a cause
+     *
+     * @param cause The cause of the failure
+     * @since 2.3
+     */
+    default void fail(@NonNull Throwable cause) {
+        // Do nothing by default.
+    }
+
     // -----------------------------------------------------
     // Utilities
     // -----------------------------------------------------
index 638edc9857b892e01ce200a97b6e3dee22af4b05..8540fb35d126a3f94d24db39a37580873fcc2d87 100644 (file)
@@ -74,6 +74,8 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent
 
     private boolean fAnalysisCancelled = false;
 
+    private @Nullable Throwable fFailureCause = null;
+
     @Override
     public boolean isAutomatic() {
         return fAutomatic;
@@ -278,6 +280,14 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent
         }
     }
 
+    /**
+     * @since 2.3
+     */
+    @Override
+    public final void fail(Throwable cause) {
+        fFailureCause = cause;
+    }
+
     @Override
     public void dispose() {
         super.dispose();
@@ -425,7 +435,7 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent
         } catch (InterruptedException e) {
             Activator.logError("Error while waiting for module completion", e); //$NON-NLS-1$
         }
-        return !fAnalysisCancelled;
+        return (!fAnalysisCancelled && fFailureCause == null);
     }
 
     @Override
@@ -440,7 +450,7 @@ public abstract class TmfAbstractAnalysisModule extends TmfComponent
         } catch (InterruptedException e) {
             Activator.logError("Error while waiting for module completion", e); //$NON-NLS-1$
         }
-        return !fAnalysisCancelled;
+        return (!fAnalysisCancelled && fFailureCause == null);
     }
 
     /**
index 32dd6ac9ac33ee44c6ce375008654d259a3018ff..4a688bd97770f4cf4dd0a9777a1451327a045acf 100644 (file)
@@ -466,6 +466,7 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
         fStateProvider = provider;
         synchronized (fRequestSyncObj) {
             startRequest();
+            request = fRequest;
         }
 
         /*
@@ -479,18 +480,42 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
          * progress monitor displays that it is running).
          */
         try {
-            if (fRequest != null) {
-                fRequest.waitForCompletion();
+            if (request != null) {
+                request.waitForCompletion();
+                if (request.isFailed()) {
+                    Throwable failureCause = request.getFailureCause();
+                    if (failureCause != null) {
+                        fail(failureCause);
+                    } else {
+                        fail(new RuntimeException("Event request failed without a cause")); //$NON-NLS-1$
+                    }
+                }
             }
         } catch (InterruptedException e) {
-             e.printStackTrace();
+             fail(e);
         }
     }
 
-    private class StateSystemEventRequest extends TmfEventRequest {
+    /**
+     * A request to build a state system from a state provider
+     *
+     * @since 2.3
+     */
+    @VisibleForTesting
+    protected class StateSystemEventRequest extends TmfEventRequest {
         private final ITmfStateProvider sci;
         private final ITmfTrace trace;
 
+        /**
+         * Constructor
+         *
+         * @param sp
+         *            The state provider used to build the state system
+         * @param timeRange
+         *            The requested time range for the request
+         * @param index
+         *            The event number at which to start the request
+         */
         public StateSystemEventRequest(ITmfStateProvider sp, TmfTimeRange timeRange, int index) {
             super(ITmfEvent.class,
                     timeRange,
@@ -599,11 +624,28 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
         if (stateProvider == null || timeRange == null) {
             return;
         }
-        ITmfEventRequest request = new StateSystemEventRequest(stateProvider, timeRange, fNbRead);
+        ITmfEventRequest request = createEventRequest(stateProvider, timeRange, fNbRead);
         stateProvider.getTrace().sendRequest(request);
         fRequest = request;
     }
 
+    /**
+     * Create a new event request
+     *
+     * @param stateProvider
+     *            The state provider used to build the state system
+     * @param timeRange
+     *            The requested time range for the request
+     * @param nbRead
+     *            The event number at which to start the request
+     * @return A new event request
+     * @since 2.3
+     */
+    @VisibleForTesting
+    protected ITmfEventRequest createEventRequest(ITmfStateProvider stateProvider, TmfTimeRange timeRange, int nbRead) {
+        return new StateSystemEventRequest(stateProvider, timeRange, nbRead);
+    }
+
     private static boolean isCompleteTrace(ITmfTrace trace) {
         return !(trace instanceof ITmfTraceCompleteness) || ((ITmfTraceCompleteness) trace).isComplete();
     }
This page took 0.030044 seconds and 5 git commands to generate.