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

Update for concurrent computations #56

Merged
merged 3 commits into from
Dec 7, 2023
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/**
* Copyright (c) 2023, 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.shortcircuit.server.service;

import com.powsybl.computation.ComputationManager;
import com.powsybl.computation.local.LocalComputationManager;
import jakarta.annotation.PostConstruct;
import jakarta.annotation.PreDestroy;
import lombok.Getter;
import lombok.SneakyThrows;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
* @author David Braquart <david.braquart at rte-france.com>
*/
@Service
@Getter
public class ShortCircuitExecutionService {

private ExecutorService executorService;

private ComputationManager computationManager;

@SneakyThrows
@PostConstruct
private void postConstruct() {
executorService = Executors.newCachedThreadPool();
computationManager = new LocalComputationManager(getExecutorService());
}

@PreDestroy
private void preDestroy() {
executorService.shutdown();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import com.powsybl.commons.PowsyblException;
import com.powsybl.commons.reporter.Reporter;
import com.powsybl.commons.reporter.ReporterModel;
import com.powsybl.computation.local.LocalComputationManager;
import com.powsybl.iidm.network.*;
import com.powsybl.iidm.network.extensions.IdentifiableShortCircuit;
import com.powsybl.network.store.client.NetworkStoreService;
Expand Down Expand Up @@ -51,27 +50,29 @@ public class ShortCircuitWorkerService {
private static final String SHORTCIRCUIT_ALL_BUSES_DEFAULT_TYPE_REPORT = "AllBusesShortCircuitAnalysis";
private static final String SHORTCIRCUIT_ONE_BUS_DEFAULT_TYPE_REPORT = "OneBusShortCircuitAnalysis";

private NetworkStoreService networkStoreService;
private ReportService reportService;
private ShortCircuitAnalysisResultRepository resultRepository;
private NotificationService notificationService;
private ObjectMapper objectMapper;
private final NetworkStoreService networkStoreService;
private final ReportService reportService;
private final ShortCircuitAnalysisResultRepository resultRepository;
private final NotificationService notificationService;
private final ShortCircuitExecutionService shortCircuitExecutionService;
private final ObjectMapper objectMapper;
private final Collection<AbstractReportMapper> reportMappers;

private Map<UUID, CompletableFuture<ShortCircuitAnalysisResult>> futures = new ConcurrentHashMap<>();
private final Map<UUID, CompletableFuture<ShortCircuitAnalysisResult>> futures = new ConcurrentHashMap<>();

private Map<UUID, ShortCircuitCancelContext> cancelComputationRequests = new ConcurrentHashMap<>();
private final Map<UUID, ShortCircuitCancelContext> cancelComputationRequests = new ConcurrentHashMap<>();

private Set<UUID> runRequests = Sets.newConcurrentHashSet();
private final Set<UUID> runRequests = Sets.newConcurrentHashSet();

private final Lock lockRunAndCancelShortCircuitAnalysis = new ReentrantLock();

@Autowired
public ShortCircuitWorkerService(NetworkStoreService networkStoreService, ReportService reportService,
public ShortCircuitWorkerService(NetworkStoreService networkStoreService, ReportService reportService, ShortCircuitExecutionService shortCircuitExecutionService,
NotificationService notificationService, ShortCircuitAnalysisResultRepository resultRepository,
ObjectMapper objectMapper, Collection<AbstractReportMapper> reportMappers) {
this.networkStoreService = Objects.requireNonNull(networkStoreService);
this.reportService = Objects.requireNonNull(reportService);
this.shortCircuitExecutionService = Objects.requireNonNull(shortCircuitExecutionService);
this.notificationService = Objects.requireNonNull(notificationService);
this.resultRepository = Objects.requireNonNull(resultRepository);
this.objectMapper = Objects.requireNonNull(objectMapper);
Expand Down Expand Up @@ -182,7 +183,7 @@ private CompletableFuture<ShortCircuitAnalysisResult> runShortCircuitAnalysisAsy
network,
faults,
context.getParameters(),
LocalComputationManager.getDefault(),
shortCircuitExecutionService.getComputationManager(),
List.of(),
reporter);
if (resultUuid != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ void testLogsMappersIsCalled() throws Exception {
final AbstractReportMapper reportMapperMocked = Mockito.mock(AbstractReportMapper.class);
final NetworkStoreService networkStoreServiceMocked = Mockito.mock(NetworkStoreService.class);
final ReportService reportServiceMocked = Mockito.mock(ReportService.class);
final ShortCircuitExecutionService shortCircuitExecutionService = Mockito.mock(ShortCircuitExecutionService.class);
final NotificationService notificationServiceMocked = Mockito.mock(NotificationService.class);
final ShortCircuitAnalysisResultRepository resultRepositoryMocked = Mockito.mock(ShortCircuitAnalysisResultRepository.class);
final ObjectMapper objectMapperMocked = Mockito.mock(ObjectMapper.class);
Expand Down Expand Up @@ -77,7 +78,7 @@ void testLogsMappersIsCalled() throws Exception {
Mockito.when(networkMocked.getBusView()).thenReturn(busViewMocked);
Mockito.when(busViewMocked.getBusStream()).thenAnswer(invocation -> Stream.empty());
Mockito.when(reportMapperMocked.processReporter(any(Reporter.class))).thenReturn(reporter);
final ShortCircuitWorkerService workerService = new ShortCircuitWorkerService(networkStoreServiceMocked, reportServiceMocked, notificationServiceMocked, resultRepositoryMocked, objectMapperMocked, List.of(reportMapperMocked));
final ShortCircuitWorkerService workerService = new ShortCircuitWorkerService(networkStoreServiceMocked, reportServiceMocked, shortCircuitExecutionService, notificationServiceMocked, resultRepositoryMocked, objectMapperMocked, List.of(reportMapperMocked));
workerService.consumeRun().accept(message);
shortCircuitAnalysisMockedStatic.verify(ShortCircuitAnalysis::find, atLeastOnce());
Mockito.verify(reportMapperMocked, times(1)).processReporter(any(ReporterModel.class));
Expand Down
Loading