From 360731ba72fbbafb3189ea7722292f2be378dd14 Mon Sep 17 00:00:00 2001 From: LSmyrnaios Date: Thu, 14 Sep 2023 13:53:01 +0300 Subject: [PATCH] - Improve handling of the "NO_CONTENT" case, in "getAssignments"-endpoint. - Code optimization and polishing. --- .../services/UrlsServiceImpl.java | 32 +++++++------------ .../urls_controller/util/FileUtils.java | 2 +- 2 files changed, 13 insertions(+), 21 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 8da9238..259d8fe 100644 --- a/src/main/java/eu/openaire/urls_controller/services/UrlsServiceImpl.java +++ b/src/main/java/eu/openaire/urls_controller/services/UrlsServiceImpl.java @@ -183,34 +183,26 @@ public class UrlsServiceImpl implements UrlsService { assignments.add(assignment); }); } catch (EmptyResultDataAccessException erdae) { - errorMsg = "No results were returned for \"getAssignmentsQuery\":\n" + getAssignmentsQuery; String tmpErrMsg = dropCurrentAssignmentTable(); DatabaseConnector.databaseLock.unlock(); - if ( tmpErrMsg != null ) - errorMsg += "\n" + tmpErrMsg; - logger.warn(errorMsg); - return ResponseEntity.status(HttpStatus.NO_CONTENT).body(errorMsg); + errorMsg = "No results retrieved from the \"getAssignmentsQuery\" for worker with id: " + workerId + ". Will increase the \"maxAttempts\" to " + maxAttemptsPerRecordAtomic.incrementAndGet() + " for the next requests."; + logger.error(errorMsg); + if ( tmpErrMsg != null ) { + errorMsg += "\n" + tmpErrMsg; // The additional error-msg is already logged. + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMsg); + } else + return ResponseEntity.status(HttpStatus.NO_CONTENT).body(new AssignmentsResponse((long) -1, null)); } catch (Exception e) { - errorMsg = DatabaseConnector.handleQueryException("getAssignmentsQuery", getAssignmentsQuery, e); String tmpErrMsg = dropCurrentAssignmentTable(); DatabaseConnector.databaseLock.unlock(); + errorMsg = DatabaseConnector.handleQueryException("getAssignmentsQuery", getAssignmentsQuery, e); if ( tmpErrMsg != null ) errorMsg += "\n" + tmpErrMsg; return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMsg); } - int assignmentsSize = assignments.size(); - if ( assignmentsSize == 0 ) { - errorMsg = "No results retrieved from the \"findAssignmentsQuery\" for worker with id: " + workerId + ". Will increase the \"maxAttempts\" to " + maxAttemptsPerRecordAtomic.incrementAndGet() + " for the next requests."; - logger.error(errorMsg); - String tmpErrMsg = dropCurrentAssignmentTable(); - DatabaseConnector.databaseLock.unlock(); - if ( tmpErrMsg != null ) { - errorMsg += "\n" + tmpErrMsg; // The additional error-msg is already logged. - return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMsg); - } else - return ResponseEntity.status(HttpStatus.MULTI_STATUS).body(new AssignmentsResponse((long) -1, null)); - } else if ( assignmentsSize < assignmentsLimit ) + int assignmentsSize = assignments.size(); // It will not be zero here! As in case of no results, the "EmptyResultDataAccessException" is thrown and handled. + if ( assignmentsSize < assignmentsLimit ) logger.warn("The retrieved results were fewer (" + assignmentsSize + ") than the \"assignmentsLimit\" (" + assignmentsLimit + "), for worker with id: " + workerId + ". Will increase the \"maxAttempts\" to " + maxAttemptsPerRecordAtomic.incrementAndGet() + ", for the next requests."); logger.debug("Finished gathering " + assignmentsSize + " assignments for worker with id \"" + workerId + "\". Going to insert them into the \"assignment\" table and then return them to the worker."); @@ -423,7 +415,7 @@ public class UrlsServiceImpl implements UrlsService { private String createAndInitializeCurrentAssignmentsTable(String findAssignmentsQuery) { - final String createCurrentAssignmentsQuery = "create table " + DatabaseConnector.databaseName + ".current_assignment as \n" + findAssignmentsQuery; + String createCurrentAssignmentsQuery = "create table " + DatabaseConnector.databaseName + ".current_assignment as \n" + findAssignmentsQuery; final String computeCurrentAssignmentsStatsQuery = "COMPUTE STATS " + DatabaseConnector.databaseName + ".current_assignment"; try { @@ -451,7 +443,7 @@ public class UrlsServiceImpl implements UrlsService { private String dropCurrentAssignmentTable() { - String dropCurrentAssignmentsQuery = "DROP TABLE IF EXISTS " + DatabaseConnector.databaseName + ".current_assignment PURGE"; + final String dropCurrentAssignmentsQuery = "DROP TABLE IF EXISTS " + DatabaseConnector.databaseName + ".current_assignment PURGE"; try { jdbcTemplate.execute(dropCurrentAssignmentsQuery); return null; // All good. No error-message. diff --git a/src/main/java/eu/openaire/urls_controller/util/FileUtils.java b/src/main/java/eu/openaire/urls_controller/util/FileUtils.java index a226f81..bd5e571 100644 --- a/src/main/java/eu/openaire/urls_controller/util/FileUtils.java +++ b/src/main/java/eu/openaire/urls_controller/util/FileUtils.java @@ -153,7 +153,7 @@ public class FileUtils { logger.error((errorMsg += "The original table \"" + tableName + "\" must be created manually! Serious problems may appear otherwise!")); // TODO - Should we shutdown the service? It is highly unlikely that anyone will observe this error live (and fix it immediately to avoid other errors in the Service)..! } - // Here, the original-table exists in the DB, BUT without any data inside! This worker Report failed to handled! (some of its data could not be loaded to the database, and all previous data was lost). + // Here, the original-table exists in the DB, BUT without any data inside! This workerReport failed to be handled! (some of its data could not be loaded to the database, and all previous data was lost). return errorMsg; }