SecurityAnalysisExecutionHandlers { public static ExecutionHandler<SecurityAnalysisResultWithLog> distributedWithLog(SecurityAnalysisExecutionInput input, int subtaskCount) { Preconditions.checkArgument(subtaskCount >= 1, TASK_COUNT_ERROR_MESSAGE, subtaskCount); return new SecurityAnalysisExecutionHandler<>(workingDir -> readResultsWithLogs(workingDir, subtaskCount), (workingDir, options) -> distributedWithLogsOptions(workingDir, options, subtaskCount), (workingDir, cause) -> generateExceptionWithLogs(workingDir, cause, subtaskCount), subtaskCount, input); } private  SecurityAnalysisExecutionHandlers(); static ExecutionHandler<SecurityAnalysisResult> forwarded(SecurityAnalysisExecutionInput input); static ExecutionHandler<SecurityAnalysisResult> forwarded(SecurityAnalysisExecutionInput input, Integer forwardedTaskCount); static ExecutionHandler<SecurityAnalysisResultWithLog> forwardedWithLogs(SecurityAnalysisExecutionInput input); static ExecutionHandler<SecurityAnalysisResultWithLog> forwardedWithLogs(SecurityAnalysisExecutionInput input, Integer forwardedTaskCount); static ExecutionHandler<SecurityAnalysisResult> distributed(SecurityAnalysisExecutionInput input, int subtaskCount); static ExecutionHandler<SecurityAnalysisResultWithLog> distributedWithLog(SecurityAnalysisExecutionInput input, int subtaskCount); static SecurityAnalysisResult readSingleResult(Path workingDir); static SecurityAnalysisResultWithLog readSingleResultWithLogs(Path workingDir); static void forwardedOptions(Path workingDir, SecurityAnalysisCommandOptions options, Integer taskCount); static void forwardedWithLogsOptions(Path workingDir, SecurityAnalysisCommandOptions options, Integer taskCount); static void distributedOptions(Path workingDir, SecurityAnalysisCommandOptions options, int taskCount); static void distributedWithLogsOptions(Path workingDir, SecurityAnalysisCommandOptions options, int taskCount); static Path getOutputPathForTask(Path workingDir, int taskIndex); static SecurityAnalysisResult readResults(Path workingDir, int subtaskCount); static SecurityAnalysisResultWithLog readResultsWithLogs(Path workingDir, int subtaskCount); static Path getLogPathForTask(Path workingDir, int taskNumber); static Path getLogPath(Path workingDir);  }
@Test public void distributedBeforeWithLog() throws IOException { SecurityAnalysisExecutionInput input = new SecurityAnalysisExecutionInput() .setParameters(new SecurityAnalysisParameters()) .setNetworkVariant(EurostagTutorialExample1Factory.create(), VariantManagerConstants.INITIAL_VARIANT_ID) .setContingenciesSource(ByteSource.wrap("contingencies definition".getBytes(StandardCharsets.UTF_8))); ExecutionHandler<SecurityAnalysisResultWithLog> handler = SecurityAnalysisExecutionHandlers.distributedWithLog(input, 3); List<CommandExecution> commandExecutions = handler.before(workingDir); SimpleCommand command = (SimpleCommand) commandExecutions.get(0).getCommand(); List<String> args = command.getArgs(0); assertThat(command.getArgs(0).subList(1, args.size())) .containsExactlyInAnyOrder("--case-file=/work/network.xiidm", "--parameters-file=/work/parameters.json", "--output-file=/work/task_0_result.json", "--output-format=JSON", "--contingencies-file=/work/contingencies.groovy", "--task=1/3", "--log-file=/work/logs_0.zip"); assertThat(command.getArgs(1).subList(1, args.size())) .containsExactlyInAnyOrder("--case-file=/work/network.xiidm", "--parameters-file=/work/parameters.json", "--output-file=/work/task_1_result.json", "--output-format=JSON", "--contingencies-file=/work/contingencies.groovy", "--task=2/3", "--log-file=/work/logs_1.zip"); }
@Test public void distributedAfterWithLogs() throws IOException { JsonSecurityAnalysisResultExporter exporter = new JsonSecurityAnalysisResultExporter(); Set<String> expectedLogs = ImmutableSet.of("logs_0.zip", "security-analysis-task_0.out", "security-analysis-task_0.err", "logs_1.zip", "security-analysis-task_1.out", "security-analysis-task_1.err"); for (String logFileName : expectedLogs) { Files.write(workingDir.resolve(logFileName), "logs".getBytes(StandardCharsets.UTF_8)); } SecurityAnalysisExecutionInput input = new SecurityAnalysisExecutionInput(); ExecutionHandler<SecurityAnalysisResultWithLog> handler2 = SecurityAnalysisExecutionHandlers.distributedWithLog(input, 2); try { handler2.after(workingDir, new DefaultExecutionReport()); fail(); } catch (ComputationException ce) { assertEquals("logs", ce.getErrLogs().get("security-analysis-task_0.err")); assertEquals("logs", ce.getErrLogs().get("security-analysis-task_1.err")); assertEquals("logs", ce.getOutLogs().get("security-analysis-task_0.out")); assertEquals("logs", ce.getOutLogs().get("security-analysis-task_1.out")); } try { Command cmd = Mockito.mock(Command.class); handler2.after(workingDir, new DefaultExecutionReport(Collections.singletonList(new ExecutionError(cmd, 0, 42)))); fail(); } catch (Exception e) { assertTrue(e instanceof ComputationException); } try (Writer writer = Files.newBufferedWriter(workingDir.resolve("task_0_result.json"))) { exporter.export(resultForContingency("c1"), writer); } try (Writer writer = Files.newBufferedWriter(workingDir.resolve("task_1_result.json"))) { exporter.export(resultForContingency("c2"), writer); } ExecutionHandler<SecurityAnalysisResultWithLog> handler = SecurityAnalysisExecutionHandlers.distributedWithLog(input, 2); SecurityAnalysisResultWithLog resultWithLog = handler.after(workingDir, new DefaultExecutionReport()); SecurityAnalysisResult result = resultWithLog.getResult(); assertNotNull(result); assertTrue(result.getPreContingencyResult().isComputationOk()); assertTrue(result.getPreContingencyResult().getLimitViolations().isEmpty()); assertEquals(2, result.getPostContingencyResults().size()); assertEquals("c1", result.getPostContingencyResults().get(0).getContingency().getId()); assertEquals("c2", result.getPostContingencyResults().get(1).getContingency().getId()); byte[] logBytes = resultWithLog.getLogBytes() .orElseThrow(AssertionError::new); Set<String> foundNames = getFileNamesFromZip(logBytes); assertEquals(expectedLogs, foundNames); }