From a1ed72f1a155f37cdb0e93f2fa44bda4ee7838d0 Mon Sep 17 00:00:00 2001 From: Thang PHAM Date: Mon, 2 Dec 2024 18:05:02 +0100 Subject: [PATCH 1/6] Computation debug mode Signed-off-by: Thang PHAM --- pom.xml | 1 + .../DynamicSimulationController.java | 8 +-- .../DynamicSimulationWorkerService.java | 50 +------------------ .../contexts/DynamicSimulationRunContext.java | 7 +-- .../service/parameters/ParametersService.java | 2 +- .../impl/ParametersServiceImpl.java | 3 +- ...stractDynamicSimulationControllerTest.java | 5 +- .../server/controller/AbstractDynawoTest.java | 6 --- 8 files changed, 14 insertions(+), 68 deletions(-) diff --git a/pom.xml b/pom.xml index 4208cbf..d19fe4b 100644 --- a/pom.xml +++ b/pom.xml @@ -156,6 +156,7 @@ com.powsybl powsybl-ws-commons + 1.17.0-SNAPSHOT org.liquibase diff --git a/src/main/java/org/gridsuite/ds/server/controller/DynamicSimulationController.java b/src/main/java/org/gridsuite/ds/server/controller/DynamicSimulationController.java index a2d1a55..fd51dcf 100644 --- a/src/main/java/org/gridsuite/ds/server/controller/DynamicSimulationController.java +++ b/src/main/java/org/gridsuite/ds/server/controller/DynamicSimulationController.java @@ -6,13 +6,13 @@ */ package org.gridsuite.ds.server.controller; +import com.powsybl.ws.commons.computation.dto.ReportInfos; import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Parameter; import io.swagger.v3.oas.annotations.responses.ApiResponse; import io.swagger.v3.oas.annotations.responses.ApiResponses; import io.swagger.v3.oas.annotations.tags.Tag; import org.apache.commons.collections4.CollectionUtils; -import com.powsybl.ws.commons.computation.dto.ReportInfos; import org.gridsuite.ds.server.dto.DynamicSimulationParametersInfos; import org.gridsuite.ds.server.dto.DynamicSimulationStatus; import org.gridsuite.ds.server.service.DynamicSimulationResultService; @@ -26,8 +26,8 @@ import java.util.List; import java.util.UUID; -import static org.gridsuite.ds.server.DynamicSimulationApi.API_VERSION; import static com.powsybl.ws.commons.computation.service.NotificationService.HEADER_USER_ID; +import static org.gridsuite.ds.server.DynamicSimulationApi.API_VERSION; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; import static org.springframework.http.MediaType.TEXT_PLAIN_VALUE; @@ -62,6 +62,7 @@ public ResponseEntity run(@PathVariable("networkUuid") UUID networkUuid, @RequestParam(name = "reporterId", required = false) String reportName, @RequestParam(name = "reportType", required = false, defaultValue = "DynamicSimulation") String reportType, @RequestParam(name = "provider", required = false) String provider, + @RequestParam(name = "debug", required = false) Boolean debug, @RequestBody DynamicSimulationParametersInfos parameters, @RequestHeader(HEADER_USER_ID) String userId) { @@ -73,7 +74,8 @@ public ResponseEntity run(@PathVariable("networkUuid") UUID networkUuid, mappingName, ReportInfos.builder().reportUuid(reportId).reporterId(reportName).computationType(reportType).build(), userId, - parameters); + parameters, + debug); UUID resultUuid = dynamicSimulationService.runAndSaveResult(dynamicSimulationRunContext); return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(resultUuid); diff --git a/src/main/java/org/gridsuite/ds/server/service/DynamicSimulationWorkerService.java b/src/main/java/org/gridsuite/ds/server/service/DynamicSimulationWorkerService.java index c77cb8e..506cf11 100644 --- a/src/main/java/org/gridsuite/ds/server/service/DynamicSimulationWorkerService.java +++ b/src/main/java/org/gridsuite/ds/server/service/DynamicSimulationWorkerService.java @@ -10,7 +10,6 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.powsybl.commons.PowsyblException; import com.powsybl.commons.io.FileUtil; -import com.powsybl.computation.ComputationManager; import com.powsybl.dynamicsimulation.*; import com.powsybl.dynamicsimulation.groovy.GroovyExtension; import com.powsybl.dynamicsimulation.groovy.GroovyOutputVariablesSupplier; @@ -86,15 +85,6 @@ public DynamicSimulationWorkerService(NetworkStoreService networkStoreService, this.parametersService = Objects.requireNonNull(parametersService); } - /** - * Use this method to mock with DockerLocalComputationManager in case of integration tests with test container - * - * @return a computation manager - */ - public ComputationManager getComputationManager() { - return executionService.getComputationManager(); - } - @Override protected DynamicSimulationResultContext fromMessage(Message message) { return DynamicSimulationResultContext.fromMessage(message, objectMapper); @@ -175,12 +165,8 @@ public void preRun(DynamicSimulationRunContext runContext) { runContext.setEventModelContent(eventModel); runContext.setCurveContent(curveModel); - // create a working folder for this run - Path workDir; - workDir = createWorkingDirectory(); - runContext.setWorkDir(workDir); - // enrich dump parameters + Path workDir = runContext.getComputationManager().getLocalDir(); setupDumpParameters(workDir, parameters); } @@ -205,7 +191,7 @@ public CompletableFuture getCompletableFuture(DynamicSi eventModelsSupplier, outputVariablesSupplier, runContext.getVariantId() != null ? runContext.getVariantId() : VariantManagerConstants.INITIAL_VARIANT_ID, - getComputationManager(), + runContext.getComputationManager(), parameters, runContext.getReportNode()); } @@ -222,14 +208,6 @@ public Consumer> consumeCancel() { return super.consumeCancel(); } - @Override - protected void clean(AbstractResultContext resultContext) { - super.clean(resultContext); - // clean working directory - Path workDir = resultContext.getRunContext().getWorkDir(); - removeWorkingDirectory(workDir); - } - // --- Dump file related methods --- // private void setupDumpParameters(Path workDir, DynamicSimulationParameters parameters) { @@ -264,28 +242,4 @@ private byte[] zipDumpFile(Path dumpDir) { } return outputState; } - - private Path createWorkingDirectory() { - Path workDir; - Path localDir = getComputationManager().getLocalDir(); - try { - workDir = Files.createTempDirectory(localDir, "dynamic_simulation_"); - } catch (IOException e) { - throw new DynamicSimulationException(DUMP_FILE_ERROR, String.format("Error occurred while creating a working directory inside the local directory %s", - localDir.toAbsolutePath())); - } - return workDir; - } - - private void removeWorkingDirectory(Path workDir) { - if (workDir != null) { - try { - FileUtil.removeDir(workDir); - } catch (IOException e) { - LOGGER.error(String.format("%s: Error occurred while cleaning working directory at %s", getComputationType(), workDir.toAbsolutePath()), e); - } - } else { - LOGGER.info("{}: No working directory to clean", getComputationType()); - } - } } diff --git a/src/main/java/org/gridsuite/ds/server/service/contexts/DynamicSimulationRunContext.java b/src/main/java/org/gridsuite/ds/server/service/contexts/DynamicSimulationRunContext.java index fbe38f0..49dfec0 100644 --- a/src/main/java/org/gridsuite/ds/server/service/contexts/DynamicSimulationRunContext.java +++ b/src/main/java/org/gridsuite/ds/server/service/contexts/DynamicSimulationRunContext.java @@ -16,7 +16,6 @@ import lombok.Setter; import org.gridsuite.ds.server.dto.DynamicSimulationParametersInfos; -import java.nio.file.Path; import java.util.List; import java.util.UUID; @@ -31,8 +30,6 @@ public class DynamicSimulationRunContext extends AbstractComputationRunContext dynamicModelContent; private List eventModelContent; @@ -43,8 +40,8 @@ public class DynamicSimulationRunContext extends AbstractComputationRunContext getDynamicModel(InputMapping inputMapping, Network network); } diff --git a/src/main/java/org/gridsuite/ds/server/service/parameters/impl/ParametersServiceImpl.java b/src/main/java/org/gridsuite/ds/server/service/parameters/impl/ParametersServiceImpl.java index aef2e48..28def57 100644 --- a/src/main/java/org/gridsuite/ds/server/service/parameters/impl/ParametersServiceImpl.java +++ b/src/main/java/org/gridsuite/ds/server/service/parameters/impl/ParametersServiceImpl.java @@ -140,7 +140,7 @@ public DynamicSimulationParameters getDynamicSimulationParameters(byte[] dynamic @Override public DynamicSimulationRunContext createRunContext(UUID networkUuid, String variantId, String receiver, String provider, String mapping, - ReportInfos reportInfos, String userId, DynamicSimulationParametersInfos parameters) { + ReportInfos reportInfos, String userId, DynamicSimulationParametersInfos parameters, Boolean debug) { DynamicSimulationRunContext runContext = DynamicSimulationRunContext.builder() .networkUuid(networkUuid) .variantId(variantId) @@ -148,6 +148,7 @@ public DynamicSimulationRunContext createRunContext(UUID networkUuid, String var .reportInfos(reportInfos) .userId(userId) .parameters(parameters) + .debug(Optional.ofNullable(debug).orElse(false)) .build(); // set provider for run context diff --git a/src/test/java/org/gridsuite/ds/server/controller/AbstractDynamicSimulationControllerTest.java b/src/test/java/org/gridsuite/ds/server/controller/AbstractDynamicSimulationControllerTest.java index 630ab51..b221524 100644 --- a/src/test/java/org/gridsuite/ds/server/controller/AbstractDynamicSimulationControllerTest.java +++ b/src/test/java/org/gridsuite/ds/server/controller/AbstractDynamicSimulationControllerTest.java @@ -80,10 +80,7 @@ public void setUp() throws IOException { } @After - @Override public void tearDown() throws Exception { - super.tearDown(); - OutputDestination output = getOutputDestination(); List destinations = List.of(dsFailedDestination, dsResultDestination, dsStoppedDestination, dsCancelFailedDestination); @@ -104,7 +101,7 @@ public void tearDown() throws Exception { private void initDynamicSimulationWorkerServiceSpy() { // setup spy bean - when(dynamicSimulationWorkerService.getComputationManager()).thenReturn(computationManager); + when(dynamicSimulationWorkerService.createComputationManager()).thenReturn(computationManager); } } diff --git a/src/test/java/org/gridsuite/ds/server/controller/AbstractDynawoTest.java b/src/test/java/org/gridsuite/ds/server/controller/AbstractDynawoTest.java index b7b645d..a7e537a 100644 --- a/src/test/java/org/gridsuite/ds/server/controller/AbstractDynawoTest.java +++ b/src/test/java/org/gridsuite/ds/server/controller/AbstractDynawoTest.java @@ -10,7 +10,6 @@ import com.powsybl.computation.ComputationManager; import com.powsybl.computation.local.test.ComputationDockerConfig; import com.powsybl.computation.local.test.DockerLocalComputationManager; -import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.rules.TemporaryFolder; @@ -43,9 +42,4 @@ public void setUp() throws IOException { Path localDir = tempFolder.getRoot().toPath(); computationManager = new DockerLocalComputationManager(localDir, dockerDir, config); } - - @After - public void tearDown() throws Exception { - computationManager.close(); - } } From e5a7f0ba2a6f764c8d83fe72a49834c81b303b5e Mon Sep 17 00:00:00 2001 From: Thang PHAM Date: Mon, 2 Dec 2024 18:40:07 +0100 Subject: [PATCH 2/6] rectify Signed-off-by: Thang PHAM --- .../DynamicSimulationWorkerService.java | 22 +++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/src/main/java/org/gridsuite/ds/server/service/DynamicSimulationWorkerService.java b/src/main/java/org/gridsuite/ds/server/service/DynamicSimulationWorkerService.java index 506cf11..15494ee 100644 --- a/src/main/java/org/gridsuite/ds/server/service/DynamicSimulationWorkerService.java +++ b/src/main/java/org/gridsuite/ds/server/service/DynamicSimulationWorkerService.java @@ -208,6 +208,16 @@ public Consumer> consumeCancel() { return super.consumeCancel(); } + @Override + protected void clean(AbstractResultContext resultContext) { + super.clean(resultContext); + if (!resultContext.getRunContext().isDebug()) { + // clean dump directory + Path dumpDir = getDumpDir(resultContext.getRunContext().getDynamicSimulationParameters()); + removeDirectory(dumpDir); + } + } + // --- Dump file related methods --- // private void setupDumpParameters(Path workDir, DynamicSimulationParameters parameters) { @@ -242,4 +252,16 @@ private byte[] zipDumpFile(Path dumpDir) { } return outputState; } + + private void removeDirectory(Path dir) { + if (dir != null) { + try { + FileUtil.removeDir(dir); + } catch (IOException e) { + LOGGER.error(String.format("%s: Error occurred while cleaning directory at %s", getComputationType(), dir.toAbsolutePath()), e); + } + } else { + LOGGER.info("{}: No directory to clean", getComputationType()); + } + } } From cead7957e1f2981f290aaec83d359161d1838bc0 Mon Sep 17 00:00:00 2001 From: Thang PHAM Date: Mon, 2 Dec 2024 20:02:29 +0100 Subject: [PATCH 3/6] Decode debug header Signed-off-by: Thang PHAM --- .../service/contexts/DynamicSimulationResultContext.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/main/java/org/gridsuite/ds/server/service/contexts/DynamicSimulationResultContext.java b/src/main/java/org/gridsuite/ds/server/service/contexts/DynamicSimulationResultContext.java index 7cd4888..c8b02a8 100644 --- a/src/main/java/org/gridsuite/ds/server/service/contexts/DynamicSimulationResultContext.java +++ b/src/main/java/org/gridsuite/ds/server/service/contexts/DynamicSimulationResultContext.java @@ -17,6 +17,7 @@ import java.io.UncheckedIOException; import java.util.Map; import java.util.Objects; +import java.util.Optional; import java.util.UUID; import static com.powsybl.ws.commons.computation.service.NotificationService.*; @@ -55,6 +56,7 @@ public static DynamicSimulationResultContext fromMessage(Message message String reporterId = (String) headers.get(REPORTER_ID_HEADER); String reportType = (String) headers.get(REPORT_TYPE_HEADER); String userId = (String) headers.get(HEADER_USER_ID); + Boolean debug = (Boolean) headers.get(DEBUG_HEADER); DynamicSimulationRunContext runContext = DynamicSimulationRunContext.builder() .networkUuid(networkUuid) @@ -64,6 +66,7 @@ public static DynamicSimulationResultContext fromMessage(Message message .reportInfos(ReportInfos.builder().reportUuid(reportUuid).reporterId(reporterId).computationType(reportType).build()) .userId(userId) .parameters(parametersInfos) + .debug(Optional.ofNullable(debug).orElse(false)) .build(); // specific headers for dynamic simulation From 5c60287842f9bd1890a61d946ac88f0c6631907c Mon Sep 17 00:00:00 2001 From: Thang PHAM Date: Mon, 2 Dec 2024 20:07:05 +0100 Subject: [PATCH 4/6] Clean notification classes Signed-off-by: Thang PHAM --- .../notification/NotificationService.java | 25 --------- .../impl/NotificationServiceImpl.java | 52 ------------------- 2 files changed, 77 deletions(-) delete mode 100644 src/main/java/org/gridsuite/ds/server/service/notification/NotificationService.java delete mode 100644 src/main/java/org/gridsuite/ds/server/service/notification/impl/NotificationServiceImpl.java diff --git a/src/main/java/org/gridsuite/ds/server/service/notification/NotificationService.java b/src/main/java/org/gridsuite/ds/server/service/notification/NotificationService.java deleted file mode 100644 index 8d68da5..0000000 --- a/src/main/java/org/gridsuite/ds/server/service/notification/NotificationService.java +++ /dev/null @@ -1,25 +0,0 @@ -/** - * Copyright (c) 2022, 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 org.gridsuite.ds.server.service.notification; - -import org.springframework.messaging.Message; - -/** - * @author Thang PHAM - */ -public interface NotificationService { - - String FAIL_MESSAGE = "Dynamic simulation has failed"; - - void emitRunDynamicSimulationMessage(Message message); - - void emitResultDynamicSimulationMessage(Message message); - - void emitCancelDynamicSimulationMessage(Message message); - - void emitFailDynamicSimulationMessage(Message message); -} diff --git a/src/main/java/org/gridsuite/ds/server/service/notification/impl/NotificationServiceImpl.java b/src/main/java/org/gridsuite/ds/server/service/notification/impl/NotificationServiceImpl.java deleted file mode 100644 index 86b9ff7..0000000 --- a/src/main/java/org/gridsuite/ds/server/service/notification/impl/NotificationServiceImpl.java +++ /dev/null @@ -1,52 +0,0 @@ -/** - * Copyright (c) 2022, 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 org.gridsuite.ds.server.service.notification.impl; - -import org.gridsuite.ds.server.service.notification.NotificationService; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.cloud.stream.function.StreamBridge; -import org.springframework.messaging.Message; -import org.springframework.stereotype.Service; - -/** - * @author Thang PHAM - */ -@Service -public class NotificationServiceImpl implements NotificationService { - private static final String CATEGORY_BROKER_OUTPUT = NotificationService.class.getName() + ".output-broker-messages"; - private static final Logger OUTPUT_MESSAGE_LOGGER = LoggerFactory.getLogger(CATEGORY_BROKER_OUTPUT); - - @Autowired - private StreamBridge publisher; - - private void sendMessage(Message message, String bindingName) { - NotificationServiceImpl.OUTPUT_MESSAGE_LOGGER.debug("Sending message : {}", message); - publisher.send(bindingName, message); - } - - @Override - public void emitRunDynamicSimulationMessage(Message message) { - sendMessage(message, "publishRun-out-0"); - } - - @Override - public void emitResultDynamicSimulationMessage(Message message) { - sendMessage(message, "publishResult-out-0"); - } - - @Override - public void emitCancelDynamicSimulationMessage(Message message) { - sendMessage(message, "publishCancel-out-0"); - } - - @Override - public void emitFailDynamicSimulationMessage(Message message) { - sendMessage(message, "publishFailed-out-0"); - } -} From ec14484f3e4b955b4db1144e0ba6786a94be07e4 Mon Sep 17 00:00:00 2001 From: Thang PHAM <117309322+thangqp@users.noreply.github.com> Date: Tue, 17 Dec 2024 09:29:45 +0100 Subject: [PATCH 5/6] Update powsybl-ws-commons to 1.19.0-SNAPSHOT --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index d19fe4b..081bd87 100644 --- a/pom.xml +++ b/pom.xml @@ -156,7 +156,7 @@ com.powsybl powsybl-ws-commons - 1.17.0-SNAPSHOT + 1.19.0-SNAPSHOT org.liquibase From acec2c7626c1e646119f52f18f85993175134526 Mon Sep 17 00:00:00 2001 From: Thang PHAM Date: Tue, 7 Jan 2025 11:14:38 +0100 Subject: [PATCH 6/6] update snapshot ws-commons Signed-off-by: Thang PHAM --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 081bd87..ac4ce9b 100644 --- a/pom.xml +++ b/pom.xml @@ -156,7 +156,7 @@ com.powsybl powsybl-ws-commons - 1.19.0-SNAPSHOT + 1.20.0-SNAPSHOT org.liquibase