-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: let consumeRun throw to let message broker handle failures #91
Open
TheMaskedTurtle
wants to merge
4
commits into
main
Choose a base branch
from
feat/rabbit-error-handling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
dfa0f28
feat: let consume run throw to let message broker handle failures
TheMaskedTurtle 1538ab0
feat: improve handling of cancellation
TheMaskedTurtle 61bf49e
fix: corrections after review
TheMaskedTurtle 3a7c583
fix: improve cancellation tests
TheMaskedTurtle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
src/main/java/com/powsybl/ws/commons/computation/ComputationException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
/** | ||
* Copyright (c) 2024, RTE (http://www.rte-france.com) | ||
* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. | ||
*/ | ||
package com.powsybl.ws.commons.computation; | ||
|
||
/** | ||
* @author Joris Mancini <joris.mancini_externe at rte-france.com> | ||
*/ | ||
public class ComputationException extends RuntimeException { | ||
public ComputationException(String message) { | ||
super(message); | ||
} | ||
|
||
public ComputationException(String message, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ | |
import com.powsybl.iidm.network.VariantManagerConstants; | ||
import com.powsybl.network.store.client.NetworkStoreService; | ||
import com.powsybl.network.store.client.PreloadingStrategy; | ||
import com.powsybl.ws.commons.computation.ComputationException; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
@@ -23,10 +24,7 @@ | |
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.UUID; | ||
import java.util.concurrent.CancellationException; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.concurrent.*; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.concurrent.locks.Lock; | ||
import java.util.concurrent.locks.ReentrantLock; | ||
|
@@ -143,15 +141,12 @@ public Consumer<Message<String>> consumeRun() { | |
sendResultMessage(resultContext, result); | ||
LOGGER.info("{} complete (resultUuid='{}')", getComputationType(), resultContext.getResultUuid()); | ||
} | ||
} catch (InterruptedException e) { | ||
Thread.currentThread().interrupt(); | ||
} catch (CancellationException e) { | ||
LOGGER.info("Computation was interrupted"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better to add context (resultUuid) to this log just like above it ? |
||
} catch (Exception e) { | ||
if (!(e instanceof CancellationException)) { | ||
LOGGER.error(NotificationService.getFailedMessage(getComputationType()), e); | ||
publishFail(resultContext, e.getMessage()); | ||
resultService.delete(resultContext.getResultUuid()); | ||
this.handleNonCancellationException(resultContext, e, rootReporter); | ||
} | ||
resultService.delete(resultContext.getResultUuid()); | ||
this.handleNonCancellationException(resultContext, e, rootReporter); | ||
throw new ComputationException(String.format("%s: %s", NotificationService.getFailedMessage(getComputationType()), e.getMessage()), e.getCause()); | ||
} finally { | ||
clean(resultContext); | ||
} | ||
|
@@ -192,11 +187,6 @@ protected void sendResultMessage(AbstractResultContext<C> resultContext, R ignor | |
resultContext.getRunContext().getUserId(), null); | ||
} | ||
|
||
protected void publishFail(AbstractResultContext<C> resultContext, String message) { | ||
notificationService.publishFail(resultContext.getResultUuid(), resultContext.getRunContext().getReceiver(), | ||
message, resultContext.getRunContext().getUserId(), getComputationType(), null); | ||
} | ||
|
||
/** | ||
* Do some extra task before running the computation, e.g. print log or init extra data for the run context | ||
* @param ignoredRunContext This context may be used for further computation in overriding classes | ||
|
@@ -205,7 +195,7 @@ protected void preRun(C ignoredRunContext) { | |
LOGGER.info("Run {} computation...", getComputationType()); | ||
} | ||
|
||
protected R run(C runContext, UUID resultUuid, AtomicReference<ReportNode> rootReporter) throws Exception { | ||
protected R run(C runContext, UUID resultUuid, AtomicReference<ReportNode> rootReporter) { | ||
String provider = runContext.getProvider(); | ||
ReportNode reportNode = ReportNode.NO_OP; | ||
|
||
|
@@ -223,7 +213,7 @@ protected R run(C runContext, UUID resultUuid, AtomicReference<ReportNode> rootR | |
|
||
preRun(runContext); | ||
CompletableFuture<R> future = runAsync(runContext, provider, resultUuid); | ||
R result = future == null ? null : observer.observeRun("run", runContext, future::get); | ||
R result = future == null ? null : observer.observeRun("run", runContext, future::join); | ||
postRun(runContext, rootReporter, result); | ||
return result; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably better to add the constructor without cause right now
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done