- Improve handling of the "NO_CONTENT" case, in "getAssignments"-endpoint.

- Code optimization and polishing.
This commit is contained in:
Lampros Smyrnaios 2023-09-14 13:53:01 +03:00
parent b4f91f188e
commit 360731ba72
2 changed files with 13 additions and 21 deletions

View File

@ -183,34 +183,26 @@ public class UrlsServiceImpl implements UrlsService {
assignments.add(assignment); assignments.add(assignment);
}); });
} catch (EmptyResultDataAccessException erdae) { } catch (EmptyResultDataAccessException erdae) {
errorMsg = "No results were returned for \"getAssignmentsQuery\":\n" + getAssignmentsQuery;
String tmpErrMsg = dropCurrentAssignmentTable(); String tmpErrMsg = dropCurrentAssignmentTable();
DatabaseConnector.databaseLock.unlock(); DatabaseConnector.databaseLock.unlock();
if ( tmpErrMsg != null ) errorMsg = "No results retrieved from the \"getAssignmentsQuery\" for worker with id: " + workerId + ". Will increase the \"maxAttempts\" to " + maxAttemptsPerRecordAtomic.incrementAndGet() + " for the next requests.";
errorMsg += "\n" + tmpErrMsg; logger.error(errorMsg);
logger.warn(errorMsg); if ( tmpErrMsg != null ) {
return ResponseEntity.status(HttpStatus.NO_CONTENT).body(errorMsg); 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) { } catch (Exception e) {
errorMsg = DatabaseConnector.handleQueryException("getAssignmentsQuery", getAssignmentsQuery, e);
String tmpErrMsg = dropCurrentAssignmentTable(); String tmpErrMsg = dropCurrentAssignmentTable();
DatabaseConnector.databaseLock.unlock(); DatabaseConnector.databaseLock.unlock();
errorMsg = DatabaseConnector.handleQueryException("getAssignmentsQuery", getAssignmentsQuery, e);
if ( tmpErrMsg != null ) if ( tmpErrMsg != null )
errorMsg += "\n" + tmpErrMsg; errorMsg += "\n" + tmpErrMsg;
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMsg); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(errorMsg);
} }
int assignmentsSize = assignments.size(); int assignmentsSize = assignments.size(); // It will not be zero here! As in case of no results, the "EmptyResultDataAccessException" is thrown and handled.
if ( assignmentsSize == 0 ) { if ( assignmentsSize < assignmentsLimit )
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 )
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.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."); 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) 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"; final String computeCurrentAssignmentsStatsQuery = "COMPUTE STATS " + DatabaseConnector.databaseName + ".current_assignment";
try { try {
@ -451,7 +443,7 @@ public class UrlsServiceImpl implements UrlsService {
private String dropCurrentAssignmentTable() { 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 { try {
jdbcTemplate.execute(dropCurrentAssignmentsQuery); jdbcTemplate.execute(dropCurrentAssignmentsQuery);
return null; // All good. No error-message. return null; // All good. No error-message.

View File

@ -153,7 +153,7 @@ public class FileUtils {
logger.error((errorMsg += "The original table \"" + tableName + "\" must be created manually! Serious problems may appear otherwise!")); 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)..! // 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; return errorMsg;
} }