tmf.core: Add failure causes to analyses
[deliverable/tracecompass.git] / tmf / org.eclipse.tracecompass.tmf.core / src / org / eclipse / tracecompass / tmf / core / statesystem / TmfStateSystemAnalysisModule.java
index b9b0ad08a953e63927e33fbcc6015140ec04d633..4a688bd97770f4cf4dd0a9777a1451327a045acf 100644 (file)
@@ -47,6 +47,8 @@ import org.eclipse.tracecompass.tmf.core.trace.TmfTraceManager;
 import org.eclipse.tracecompass.tmf.core.trace.TmfTraceUtils;
 import org.eclipse.tracecompass.tmf.core.trace.experiment.TmfExperiment;
 
+import com.google.common.annotations.VisibleForTesting;
+
 /**
  * Abstract analysis module to generate a state system. It is a base class that
  * can be used as a shortcut by analysis who just need to build a single state
@@ -66,15 +68,15 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
     private final CountDownLatch fInitialized = new CountDownLatch(1);
     private final Object fRequestSyncObj = new Object();
 
-    @Nullable private ITmfStateSystemBuilder fStateSystem;
-    @Nullable private ITmfStateProvider fStateProvider;
-    @Nullable private IStateHistoryBackend fHtBackend;
-    @Nullable private ITmfEventRequest fRequest;
-    @Nullable private TmfTimeRange fTimeRange = null;
+    private @Nullable ITmfStateSystemBuilder fStateSystem;
+    private @Nullable ITmfEventRequest fRequest;
+    private @Nullable TmfTimeRange fTimeRange = null;
 
     private int fNbRead = 0;
     private boolean fInitializationSucceeded;
 
+    private volatile @Nullable ITmfStateProvider fStateProvider;
+
     /**
      * State system backend types
      *
@@ -172,11 +174,31 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
         return fInitializationSucceeded;
     }
 
+    /**
+     * @since 2.0
+     */
+    @Override
+    public boolean isQueryable(long ts) {
+        /* Return true if there is no state provider available (the analysis is not being built) */
+        ITmfStateProvider provider = fStateProvider;
+        if (provider == null) {
+            return true;
+        }
+        return ts <= provider.getLatestSafeTime();
+    }
+
     // ------------------------------------------------------------------------
     // TmfAbstractAnalysisModule
     // ------------------------------------------------------------------------
 
-    private @Nullable File getSsFile() {
+    /**
+     * Get the file where to save the results of the analysis
+     *
+     * @return The file to save the results in
+     * @since 2.3
+     */
+    @VisibleForTesting
+    protected @Nullable File getSsFile() {
         ITmfTrace trace = getTrace();
         if (trace == null) {
             return null;
@@ -198,7 +220,6 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
             /* Get the state system according to backend */
             StateSystemBackendType backend = getBackendType();
 
-
             ITmfTrace trace = getTrace();
             if (trace == null) {
                 // Analysis was cancelled in the meantime
@@ -288,7 +309,6 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
             try {
                 IStateHistoryBackend backend = StateHistoryBackendFactory.createHistoryTreeBackendExistingFile(
                         id, htFile, version);
-                fHtBackend = backend;
                 fStateSystem = StateSystemFactory.newStateSystem(backend, false);
                 analysisReady(true);
                 return;
@@ -307,7 +327,6 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
         try {
             IStateHistoryBackend backend = StateHistoryBackendFactory.createHistoryTreeBackendNewFile(
                     id, htFile, provider.getVersion(), provider.getStartTime(), QUEUE_SIZE);
-            fHtBackend = backend;
             fStateSystem = StateSystemFactory.newStateSystem(backend);
             provider.assignTargetStateSystem(fStateSystem);
             build(provider);
@@ -388,7 +407,6 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
         provider.assignTargetStateSystem(realSS);
 
         /* 7 */
-        fHtBackend = partialBackend;
         fStateSystem = realSS;
 
         build(provider);
@@ -401,7 +419,6 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
      */
     private void createNullHistory(String id, ITmfStateProvider provider) {
         IStateHistoryBackend backend = StateHistoryBackendFactory.createNullBackend(id);
-        fHtBackend = backend;
         fStateSystem = StateSystemFactory.newStateSystem(backend);
         provider.assignTargetStateSystem(fStateSystem);
         build(provider);
@@ -414,7 +431,6 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
      */
     private void createInMemoryHistory(String id, ITmfStateProvider provider) {
         IStateHistoryBackend backend = StateHistoryBackendFactory.createInMemoryBackend(id, provider.getStartTime());
-        fHtBackend = backend;
         fStateSystem = StateSystemFactory.newStateSystem(backend);
         provider.assignTargetStateSystem(fStateSystem);
         build(provider);
@@ -425,13 +441,14 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
         if (provider != null) {
             provider.dispose();
         }
-        if (deleteFiles && (fHtBackend != null)) {
-            fHtBackend.removeFiles();
+        fStateProvider = null;
+        if (deleteFiles && (fStateSystem != null)) {
+            fStateSystem.removeFiles();
         }
     }
 
     private void build(ITmfStateProvider provider) {
-        if ((fStateSystem == null) || (fHtBackend == null)) {
+        if (fStateSystem == null) {
             throw new IllegalArgumentException();
         }
 
@@ -449,6 +466,7 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
         fStateProvider = provider;
         synchronized (fRequestSyncObj) {
             startRequest();
+            request = fRequest;
         }
 
         /*
@@ -462,24 +480,49 @@ 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,
                     index,
                     ITmfEventRequest.ALL_DATA,
-                    ITmfEventRequest.ExecutionType.BACKGROUND);
+                    ITmfEventRequest.ExecutionType.BACKGROUND,
+                    TmfStateSystemAnalysisModule.this.getDependencyLevel());
             this.sci = sp;
             trace = sci.getTrace();
 
@@ -524,9 +567,7 @@ public abstract class TmfStateSystemAnalysisModule extends TmfAbstractAnalysisMo
         @Override
         public void handleCancel() {
             super.handleCancel();
-            if (isCompleteTrace(trace)) {
-                disposeProvider(true);
-            }
+            disposeProvider(true);
         }
 
         @Override
@@ -583,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.027493 seconds and 5 git commands to generate.