Skip to content

Commit 1693c48

Browse files
committed
tmf: Propagate analysis failure of dependent analysis to parent
fixes #301 Signed-off-by: Bernd Hufmann <[email protected]>
1 parent 4cca791 commit 1693c48

File tree

5 files changed

+108
-6
lines changed

5 files changed

+108
-6
lines changed

tmf/org.eclipse.tracecompass.tmf.core.tests/src/org/eclipse/tracecompass/tmf/core/tests/analysis/AnalysisModuleTest.java

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ public void testDependentAnalyses() {
331331
final TestAnalysis depModule = new TestAnalysis() {
332332

333333
@Override
334-
protected boolean executeAnalysis(IProgressMonitor monitor) {
334+
protected boolean executeAnalysis(IProgressMonitor monitor) throws TmfAnalysisException {
335335
try {
336336
Thread.sleep(1000);
337337
} catch (InterruptedException e) {
@@ -460,6 +460,70 @@ protected Iterable<IAnalysisModule> getDependentAnalyses() {
460460

461461
}
462462

463+
/**
464+
* Test that the failure of the dependent analysis is propagated to the
465+
* parent analysis by the {@link TmfAbstractAnalysisModule}
466+
*/
467+
@Test
468+
public void testFailedDependentAnalyses() {
469+
470+
ITmfTrace trace = TmfTestTrace.A_TEST_10K.getTrace();
471+
int paramAndResult = 5;
472+
473+
/* Setup the dependent module */
474+
final String suffix = " dep";
475+
final TestAnalysis depModule = new TestAnalysis() {
476+
477+
@Override
478+
protected boolean executeAnalysis(IProgressMonitor monitor) throws TmfAnalysisException {
479+
try {
480+
Thread.sleep(1000);
481+
} catch (InterruptedException e) {
482+
return false;
483+
}
484+
return super.executeAnalysis(monitor);
485+
}
486+
487+
};
488+
depModule.setName(MODULE_GENERIC_NAME + suffix);
489+
depModule.setId(MODULE_GENERIC_ID + suffix);
490+
depModule.addParameter(TestAnalysis.PARAM_TEST);
491+
depModule.setParameter(TestAnalysis.PARAM_TEST, 1999);
492+
493+
/* Prepare the main analysis with a dependent analysis */
494+
TestAnalysis module = new TestAnalysis() {
495+
496+
@Override
497+
protected Iterable<IAnalysisModule> getDependentAnalyses() {
498+
Set<IAnalysisModule> modules = new HashSet<>();
499+
modules.add(depModule);
500+
return modules;
501+
}
502+
503+
};
504+
505+
module.setName(MODULE_GENERIC_NAME);
506+
module.setId(MODULE_GENERIC_ID);
507+
module.addParameter(TestAnalysis.PARAM_TEST);
508+
module.setParameter(TestAnalysis.PARAM_TEST, paramAndResult);
509+
510+
try {
511+
assertTrue(depModule.setTrace(trace));
512+
assertTrue(module.setTrace(trace));
513+
} catch (TmfAnalysisException e) {
514+
fail(e.getMessage());
515+
}
516+
517+
module.schedule();
518+
519+
/* Verify that failure of the dependent analysis is propagated to the parent */
520+
assertFalse(module.waitForCompletion());
521+
522+
module.dispose();
523+
depModule.dispose();
524+
trace.dispose();
525+
}
526+
463527
/**
464528
* Test configurable analysis
465529
*

tmf/org.eclipse.tracecompass.tmf.core.tests/stubs/org/eclipse/tracecompass/tmf/tests/stubs/analysis/TestAnalysis.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import org.eclipse.core.runtime.IProgressMonitor;
1818
import org.eclipse.jdt.annotation.NonNull;
1919
import org.eclipse.tracecompass.tmf.core.analysis.TmfAbstractAnalysisModule;
20+
import org.eclipse.tracecompass.tmf.core.exceptions.TmfAnalysisException;
2021
import org.eclipse.tracecompass.tmf.core.trace.ITmfTrace;
2122

2223
/**
@@ -44,7 +45,7 @@ public boolean canExecute(ITmfTrace trace) {
4445
}
4546

4647
@Override
47-
protected boolean executeAnalysis(final IProgressMonitor monitor) {
48+
protected boolean executeAnalysis(final IProgressMonitor monitor) throws TmfAnalysisException {
4849
Object parameter = getParameter(PARAM_TEST);
4950
if (!(parameter instanceof Integer)) {
5051
throw new RuntimeException("The parameter should be set");
@@ -64,6 +65,8 @@ protected boolean executeAnalysis(final IProgressMonitor monitor) {
6465
}
6566
}
6667
return !monitor.isCanceled();
68+
} else if (integer == 1999) {
69+
throw new TmfAnalysisException("Failure");
6770
}
6871
return true;
6972
}

tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/analysis/IAnalysisModule.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,6 +286,16 @@ default void fail(@NonNull Throwable cause) {
286286
// Do nothing by default.
287287
}
288288

289+
/**
290+
* Gets the error cause in case of failure
291+
*
292+
* @return the error cause
293+
* @since 10.1
294+
*/
295+
default @Nullable Throwable getFailureCause() {
296+
return null;
297+
}
298+
289299
// -----------------------------------------------------
290300
// Utilities
291301
// -----------------------------------------------------

tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/analysis/TmfAbstractAnalysisModule.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,14 @@ public final void fail(Throwable cause) {
299299
onFail();
300300
}
301301

302+
/**
303+
* @since 10.1
304+
*/
305+
@Override
306+
public @Nullable Throwable getFailureCause() {
307+
return fFailureCause;
308+
}
309+
302310
/**
303311
* Method executed when the analysis has failed, so that analysis can
304312
* rectify their state. For instance, if the analysis had not been
@@ -383,7 +391,7 @@ private void execute(final ITmfTrace trace) {
383391
* Actual analysis will be run on a separate thread
384392
*/
385393
String jobName = checkNotNull(NLS.bind(Messages.TmfAbstractAnalysisModule_RunningAnalysis, getName()));
386-
fJob = new Job(jobName) {
394+
Job job = new Job(jobName) {
387395
@Override
388396
protected @Nullable IStatus run(final @Nullable IProgressMonitor monitor) {
389397
try (FlowScopeLog jobLog = new FlowScopeLogBuilder(LOGGER, Level.FINE, "TmfAbstractAnalysis:executing").setParentScope(analysisLog).build()) { //$NON-NLS-1$
@@ -393,11 +401,16 @@ private void execute(final ITmfTrace trace) {
393401
TmfCoreTracer.traceAnalysis(TmfAbstractAnalysisModule.this.getId(), TmfAbstractAnalysisModule.this.getTrace(), "started"); //$NON-NLS-1$
394402
fAnalysisCancelled = !executeAnalysis(mon);
395403
for (IAnalysisModule module : dependentAnalyses) {
396-
module.waitForCompletion(mon);
404+
boolean isModuleCancelled = !module.waitForCompletion(mon);
405+
if (isModuleCancelled) {
406+
Throwable cause = module.getFailureCause();
407+
if (cause != null) {
408+
throw new TmfAnalysisException("Dependent analysis '" + module.getName() + "' failed.", cause); //$NON-NLS-1$ //$NON-NLS-2$
409+
}
410+
fAnalysisCancelled |= isModuleCancelled;
411+
}
397412
}
398413
TmfCoreTracer.traceAnalysis(TmfAbstractAnalysisModule.this.getId(), TmfAbstractAnalysisModule.this.getTrace(), "finished"); //$NON-NLS-1$
399-
} catch (TmfAnalysisException e) {
400-
Activator.logError("Error executing analysis with trace " + trace.getName(), e); //$NON-NLS-1$
401414
} catch (OperationCanceledException e) {
402415
// Analysis was canceled
403416
} catch (Exception e) {
@@ -428,6 +441,7 @@ protected void canceling() {
428441
}
429442

430443
};
444+
fJob = job;
431445
fJob.schedule();
432446
}
433447
}

tmf/org.eclipse.tracecompass.tmf.core/src/org/eclipse/tracecompass/tmf/core/exceptions/TmfAnalysisException.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,15 @@ public TmfAnalysisException(Throwable cause) {
5656
super(cause);
5757
}
5858

59+
/**
60+
* Constructs a new exception with a message and with the specified cause.
61+
*
62+
* @param cause
63+
* The cause
64+
* @since 10.1
65+
*/
66+
public TmfAnalysisException(String message, Throwable cause) {
67+
super(message, cause);
68+
}
69+
5970
}

0 commit comments

Comments
 (0)