Skip to content
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

[POC] Computation debug mode #117

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@
<dependency>
<groupId>com.powsybl</groupId>
<artifactId>powsybl-ws-commons</artifactId>
<version>1.19.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -62,6 +62,7 @@ public ResponseEntity<UUID> 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) {

Expand All @@ -73,7 +74,8 @@ public ResponseEntity<UUID> 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String> message) {
return DynamicSimulationResultContext.fromMessage(message, objectMapper);
Expand Down Expand Up @@ -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);
}

Expand All @@ -205,7 +191,7 @@ public CompletableFuture<DynamicSimulationResult> getCompletableFuture(DynamicSi
eventModelsSupplier,
outputVariablesSupplier,
runContext.getVariantId() != null ? runContext.getVariantId() : VariantManagerConstants.INITIAL_VARIANT_ID,
getComputationManager(),
runContext.getComputationManager(),
parameters,
runContext.getReportNode());
}
Expand All @@ -225,9 +211,11 @@ public Consumer<Message<String>> consumeCancel() {
@Override
protected void clean(AbstractResultContext<DynamicSimulationRunContext> resultContext) {
super.clean(resultContext);
// clean working directory
Path workDir = resultContext.getRunContext().getWorkDir();
removeWorkingDirectory(workDir);
if (!resultContext.getRunContext().isDebug()) {
// clean dump directory
Path dumpDir = getDumpDir(resultContext.getRunContext().getDynamicSimulationParameters());
removeDirectory(dumpDir);
}
}

// --- Dump file related methods --- //
Expand Down Expand Up @@ -265,27 +253,15 @@ 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) {
private void removeDirectory(Path dir) {
if (dir != null) {
try {
FileUtil.removeDir(workDir);
FileUtil.removeDir(dir);
} catch (IOException e) {
LOGGER.error(String.format("%s: Error occurred while cleaning working directory at %s", getComputationType(), workDir.toAbsolutePath()), e);
LOGGER.error(String.format("%s: Error occurred while cleaning directory at %s", getComputationType(), dir.toAbsolutePath()), e);
}
} else {
LOGGER.info("{}: No working directory to clean", getComputationType());
LOGGER.info("{}: No directory to clean", getComputationType());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -55,6 +56,7 @@ public static DynamicSimulationResultContext fromMessage(Message<String> 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)
Expand All @@ -64,6 +66,7 @@ public static DynamicSimulationResultContext fromMessage(Message<String> 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -31,8 +30,6 @@ public class DynamicSimulationRunContext extends AbstractComputationRunContext<D

// --- Fields which are enriched in worker service --- //

private Path workDir;

private List<DynamicModelConfig> dynamicModelContent;

private List<EventModelConfig> eventModelContent;
Expand All @@ -43,8 +40,8 @@ public class DynamicSimulationRunContext extends AbstractComputationRunContext<D

@Builder
public DynamicSimulationRunContext(UUID networkUuid, String variantId, String receiver, String provider, String mapping,
ReportInfos reportInfos, String userId, DynamicSimulationParametersInfos parameters) {
super(networkUuid, variantId, receiver, reportInfos, userId, provider, parameters);
ReportInfos reportInfos, String userId, DynamicSimulationParametersInfos parameters, boolean debug) {
super(networkUuid, variantId, receiver, reportInfos, userId, provider, parameters, debug);
this.mapping = mapping;
}
}
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public interface ParametersService {
DynamicSimulationParameters getDynamicSimulationParameters(byte[] dynamicParams, String provider, DynamicSimulationParametersInfos inputParameters);

DynamicSimulationRunContext createRunContext(UUID networkUuid, String variantId, String receiver, String provider, String mapping,
ReportInfos reportContext, String userId, DynamicSimulationParametersInfos parameters);
ReportInfos reportContext, String userId, DynamicSimulationParametersInfos parameters, Boolean debug);

List<DynamicModelConfig> getDynamicModel(InputMapping inputMapping, Network network);
}
Original file line number Diff line number Diff line change
Expand Up @@ -140,14 +140,15 @@ 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)
.receiver(receiver)
.reportInfos(reportInfos)
.userId(userId)
.parameters(parameters)
.debug(Optional.ofNullable(debug).orElse(false))
.build();

// set provider for run context
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,7 @@ public void setUp() throws IOException {
}

@After
@Override
public void tearDown() throws Exception {
super.tearDown();

OutputDestination output = getOutputDestination();
List<String> destinations = List.of(dsFailedDestination, dsResultDestination, dsStoppedDestination, dsCancelFailedDestination);

Expand All @@ -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);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}
}
Loading