From c98e8df323df97a914c8319ec2c78fbd48d140fa Mon Sep 17 00:00:00 2001 From: LSmyrnaios Date: Wed, 13 Sep 2023 16:27:18 +0300 Subject: [PATCH] Move the "getRenamedWorkerReport"-code in its own method. --- .../services/UrlsServiceImpl.java | 55 ++++++++++--------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/src/main/java/eu/openaire/urls_controller/services/UrlsServiceImpl.java b/src/main/java/eu/openaire/urls_controller/services/UrlsServiceImpl.java index da81187..b5844db 100644 --- a/src/main/java/eu/openaire/urls_controller/services/UrlsServiceImpl.java +++ b/src/main/java/eu/openaire/urls_controller/services/UrlsServiceImpl.java @@ -515,30 +515,7 @@ public class UrlsServiceImpl implements UrlsService { // Rename the worker-report to indicate success or failure. String workerReportBaseName = this.workerReportsDirPath + File.separator + workerId + File.separator + workerId + "_assignments_" + assignmentRequestCounter + "_report"; File workerReport = new File(workerReportBaseName + ".json"); - File renamedWorkerReport = null; - boolean wasWorkerReportRenamed = true; - try { - // Check if the workerReport does not exist under this name, as it may have been renamed previously to "failed" and now is the 2nd try. - if ( !workerReport.isFile() ) { - // Then this is the 2nd try and the report has already been renamed to "failed" the 1st time. - workerReport = new File(workerReportBaseName + "_failed.json"); - if ( !workerReport.isFile() ) { // In this case, an error exists, since this file was deleted before its time. - logger.error("The workerReport file \"" + workerReport.getAbsolutePath() + "\" does not exist!"); - wasWorkerReportRenamed = false; // Do not proceed on renaming the non-existing file. - // TODO - Do we need additional handling? This report may be either successful or failed but the file was deleted. - } - } - if ( wasWorkerReportRenamed ) { // Only if the file exists. - renamedWorkerReport = new File(workerReportBaseName + ((errorMsg == null) ? "_successful.json" : "_failed.json")); - if ( !workerReport.renameTo(renamedWorkerReport) ) { - logger.warn("There was a problem when renaming the workerReport: " + workerReport.getName()); - wasWorkerReportRenamed = false; - } - } - } catch (Exception e) { - logger.error("There was a problem when renaming the workerReport: " + workerReport.getName(), e); - wasWorkerReportRenamed = false; - } + File renamedWorkerReport = getRenamedWorkerReport(workerReportBaseName, workerReport, errorMsg); // It may return null. // Get the IP of this worker. WorkerInfo workerInfo = UrlsController.workersInfoMap.get(workerId); @@ -558,7 +535,7 @@ public class UrlsServiceImpl implements UrlsService { logger.error("HTTP-Connection problem with the submission of the \"postReportResultToWorker\" of worker \"" + workerId + "\" and assignments_" + assignmentRequestCounter + "! Error-code was: " + responseCode); return false; } else if ( errorMsg == null ) // If the worker was notified, then go delete the successful workerReport. - fileUtils.deleteFile(wasWorkerReportRenamed ? renamedWorkerReport.getAbsolutePath() : workerReport.getAbsolutePath()); + fileUtils.deleteFile((renamedWorkerReport != null) ? renamedWorkerReport.getAbsolutePath() : workerReport.getAbsolutePath()); return true; } catch (HttpServerErrorException hsee) { logger.error("The Worker \"" + workerId + "\" failed to handle the \"postReportResultToWorker\", of assignments_" + assignmentRequestCounter + ": " + hsee.getMessage()); @@ -580,6 +557,34 @@ public class UrlsServiceImpl implements UrlsService { } + private static File getRenamedWorkerReport(String workerReportBaseName, File workerReport, String errorMsg) + { + File renamedWorkerReport = null; + try { + // Check if the workerReport does not exist under this name, as it may have been renamed previously to "failed" and now is the 2nd try. + if ( !workerReport.isFile() ) { + // Then this is the 2nd try and the report has already been renamed to "failed" the 1st time. + workerReport = new File(workerReportBaseName + "_failed.json"); + if ( !workerReport.isFile() ) { // In this case, an error exists, since this file was deleted before its time. + logger.error("The workerReport file \"" + workerReport.getAbsolutePath() + "\" does not exist!"); + // TODO - Do we need additional handling? This report may be either successful or failed but the file was deleted. + return null; // Do not proceed on renaming the non-existing file. + } + } + // Only if the file exists, proceed to renaming it. + renamedWorkerReport = new File(workerReportBaseName + ((errorMsg == null) ? "_successful.json" : "_failed.json")); + if ( !workerReport.renameTo(renamedWorkerReport) ) { + logger.warn("There was a problem when renaming the workerReport: " + workerReport.getName()); + return null; + } + } catch (Exception e) { + logger.error("There was a problem when renaming the workerReport: " + workerReport.getName(), e); + return null; + } + return renamedWorkerReport; + } + + // The "batchExecute" does not work in this Impala-Database, so this is a "giant-query" solution. // Note: this causes an "Out of memory"-ERROR in the current version of the Impala JDBC driver. If a later version is provided, then this code should be tested. private static PreparedStatement constructLargeInsertQuery(Connection con, String baseInsertQuery, int dataSize, int numParamsPerRow) throws RuntimeException {