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 cb9fbb85683..6fe0d3a8589 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) {