From cfe206b32cb7d22403187241b56bff9e01efd6b6 Mon Sep 17 00:00:00 2001 From: Ace Date: Fri, 4 Dec 2020 07:26:29 +0530 Subject: [PATCH] correcting task random candidate election logic --- .../service/HeartBeatManagementServiceImpl.java | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/service/HeartBeatManagementServiceImpl.java b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/service/HeartBeatManagementServiceImpl.java index cb9fbb8568..6fe0d3a858 100644 --- a/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/service/HeartBeatManagementServiceImpl.java +++ b/components/heartbeat-management/io.entgra.server.bootup.heartbeat.beacon/src/main/java/io/entgra/server/bootup/heartbeat/beacon/service/HeartBeatManagementServiceImpl.java @@ -208,12 +208,17 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic Map servers = heartBeatDAO.getActiveServerDetails(elapsedTimeInSeconds); if (servers != null && !servers.isEmpty()) { ElectedCandidate presentCandidate = heartBeatDAO.retrieveCandidate(); - if (presentCandidate != null && - presentCandidate.getTimeOfElection().before(new Timestamp(System.currentTimeMillis() - - TimeUnit.SECONDS.toMillis(elapsedTimeInSeconds)))) { - heartBeatDAO.purgeCandidates(); + if (presentCandidate != null) { + //if candidate is older than stipulated elapsed-time, purge and re-elect + if (presentCandidate.getTimeOfElection().before(new Timestamp(System.currentTimeMillis() + - TimeUnit.SECONDS.toMillis(elapsedTimeInSeconds)))) { + heartBeatDAO.purgeCandidates(); + electCandidate(servers); + } + } else { + //first time execution, elect if not present + electCandidate(servers); } - electCandidate(servers); HeartBeatBeaconDAOFactory.commitTransaction(); } } catch (HeartBeatDAOException e) {