- Avoid sending "cancelShutdown" requests to already shutDown Workers.

- Optimize performance of the code running right before the "postShutdownOrCancelRequestToWorker".
- Show which Workers have already shutdown and as a result a "postShutdownOrCancelRequestToWorker" will not be performed on them.
This commit is contained in:
Lampros Smyrnaios 2023-05-29 13:41:37 +03:00
parent f9c6bad768
commit 54685bbe9a
1 changed files with 27 additions and 16 deletions

View File

@ -35,24 +35,29 @@ public class ShutdownController {
if ( responseEntity != null ) if ( responseEntity != null )
return responseEntity; return responseEntity;
String finalMsg = ""; String endingMsg;
if ( shouldShutdownService ) if ( shouldShutdownService ) {
finalMsg = "The controller has already received a \"shutdownService\" (which was not canceled afterwards)."; endingMsg = "The controller has already received a \"shutdownService\" (which was not canceled afterwards).";
else { logger.info(initMsg + endingMsg);
} else {
shouldShutdownService = true; shouldShutdownService = true;
// Send "shutdownWorker" requests to all Workers. endingMsg = "The service will shutdown, after finishing current work.";
logger.info(initMsg + endingMsg);
// Send "shutdownWorker" requests to all active Workers.
for ( String workerId : UrlsController.workersInfoMap.keySet() ) { for ( String workerId : UrlsController.workersInfoMap.keySet() ) {
if ( ! UrlsController.workersInfoMap.get(workerId).getHasShutdown() ) WorkerInfo workerInfo = UrlsController.workersInfoMap.get(workerId);
shutdownService.postShutdownOrCancelRequestToWorker(workerId, UrlsController.workersInfoMap.get(workerId).getWorkerIP(), false); if ( ! workerInfo.getHasShutdown() ) // A worker may have shutdown on its own (by sending it a shutDown request manually), so it will have told the Controller when it shut down. In case of a Worker-crash, the Controller will not know about it.
shutdownService.postShutdownOrCancelRequestToWorker(workerId, workerInfo.getWorkerIP(), false);
else
logger.warn("Will not post ShutdownRequest to Worker \"" + workerId + "\", since is it has already shutdown.");
} }
// That's it for now. The workers may take some hours to finish their work (including delivering the full-text files). // That's it for now. The workers may take some hours to finish their work (including delivering the full-text files).
// A scheduler monitors the shutdown of the workers. Once all worker have shutdown, the Controller shuts down as well. // A scheduler monitors the shutdown of the workers. Once all worker have shutdown, the Controller shuts down as well.
} }
finalMsg += "The service will shutdown, after finishing current work."; return ResponseEntity.ok().body(endingMsg + "\n");
logger.info(initMsg + finalMsg);
return ResponseEntity.ok().body(finalMsg + "\n");
} }
@ -65,13 +70,19 @@ public class ShutdownController {
return responseEntity; return responseEntity;
shouldShutdownService = false; shouldShutdownService = false;
// Send "cancelShutdownWorker" requests to all Workers. String endingMsg = "Any previous \"shutdownService\"-request is canceled.";
for ( String workerId : UrlsController.workersInfoMap.keySet() ) logger.info(initMsg + endingMsg);
shutdownService.postShutdownOrCancelRequestToWorker(workerId, UrlsController.workersInfoMap.get(workerId).getWorkerIP(), true);
String finalMsg = "Any previous \"shutdownService\"-request is canceled."; // Send "cancelShutdownWorker" requests to all active Workers.
logger.info(initMsg + finalMsg); for ( String workerId : UrlsController.workersInfoMap.keySet() ) {
return ResponseEntity.ok().body(finalMsg + "\n"); WorkerInfo workerInfo = UrlsController.workersInfoMap.get(workerId);
if ( ! workerInfo.getHasShutdown() ) // A worker may have shutdown on its own (by sending it a shutDown request manually), so it will have told the Controller when it shut down. In case of a Worker-crash, the Controller will not know about it.
shutdownService.postShutdownOrCancelRequestToWorker(workerId, workerInfo.getWorkerIP(), true);
else
logger.warn("Will not post CancelShutdownRequest to Worker \"" + workerId + "\", since is it has already shutdown.");
}
return ResponseEntity.ok().body(endingMsg + "\n");
} }