-
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
base: main
Are you sure you want to change the base?
Changes from 2 commits
dfa0f28
1538ab0
61bf49e
3a7c583
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/** | ||
* 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, Throwable cause) { | ||
super(message, cause); | ||
} | ||
} |
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; | ||
|
@@ -94,24 +92,26 @@ protected void cleanResultsAndPublishCancel(UUID resultUuid, String receiver) { | |
} | ||
} | ||
|
||
private boolean cancelAsync(CancelContext cancelContext) { | ||
private void cancelAsync(CancelContext cancelContext) { | ||
lockRunAndCancel.lock(); | ||
boolean isCanceled = false; | ||
try { | ||
cancelComputationRequests.put(cancelContext.resultUuid(), cancelContext); | ||
|
||
// find the completableFuture associated with result uuid | ||
CompletableFuture<R> future = futures.get(cancelContext.resultUuid()); | ||
if (future != null) { | ||
isCanceled = future.cancel(true); // cancel computation in progress | ||
if (isCanceled) { | ||
if (future == null) { | ||
cleanResultsAndPublishCancel(cancelContext.resultUuid(), cancelContext.receiver()); | ||
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. this will be run on all but one server who is doing the computation ? 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. it looks like it's already the case after this method returns...? 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. before we did publishCancelFail but now we do cleanResults and publishStop 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. removed |
||
} else { | ||
boolean isCanceled = future.cancel(true); // cancel computation in progress | ||
if (future.isDone() || isCanceled) { | ||
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. Not sure about adding this clean when the computation is done 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. removed |
||
cleanResultsAndPublishCancel(cancelContext.resultUuid(), cancelContext.receiver()); | ||
} else { | ||
notificationService.publishCancelFailed(cancelContext.resultUuid(), cancelContext.receiver(), getComputationType(), cancelContext.userId()); | ||
} | ||
} | ||
} finally { | ||
lockRunAndCancel.unlock(); | ||
} | ||
return isCanceled; | ||
} | ||
|
||
protected abstract AbstractResultContext<C> fromMessage(Message<String> message); | ||
|
@@ -143,15 +143,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); | ||
} | ||
|
@@ -178,10 +175,7 @@ protected void handleNonCancellationException(AbstractResultContext<C> resultCon | |
public Consumer<Message<String>> consumeCancel() { | ||
return message -> { | ||
CancelContext cancelContext = CancelContext.fromMessage(message); | ||
boolean isCancelled = cancelAsync(cancelContext); | ||
if (!isCancelled) { | ||
notificationService.publishCancelFailed(cancelContext.resultUuid(), cancelContext.receiver(), getComputationType(), cancelContext.userId()); | ||
} | ||
cancelAsync(cancelContext); | ||
}; | ||
} | ||
|
||
|
@@ -192,11 +186,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 +194,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 +212,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; | ||
} | ||
|
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