From 9ecdd486f860ea154f664e6e159bb6e3ac7b490b Mon Sep 17 00:00:00 2001 From: "amalka.subasinghe" Date: Thu, 24 Aug 2023 16:31:42 +0530 Subject: [PATCH] imrpoved heart beat to handle cluster formation changed --- .../pom.xml | 1 + .../internal/HeartBeatBeaconComponent.java | 5 ++++- .../beacon/internal/HeartBeatExecutor.java | 19 ++++++++++++++++++- ...terFormationChangedNotifierRepository.java | 5 ++--- .../HeartBeatManagementServiceImpl.java | 8 ++++++++ 5 files changed, 33 insertions(+), 5 deletions(-) diff --git a/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/pom.xml b/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/pom.xml index 83e9788b9d..6cdcf4027f 100644 --- a/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/pom.xml +++ b/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/pom.xml @@ -70,6 +70,7 @@ !io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.internal, io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.* + * diff --git a/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/internal/HeartBeatBeaconComponent.java b/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/internal/HeartBeatBeaconComponent.java index a526e216fb..356f5d4996 100644 --- a/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/internal/HeartBeatBeaconComponent.java +++ b/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/internal/HeartBeatBeaconComponent.java @@ -28,6 +28,7 @@ import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.HeartBea import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.osgi.service.component.ComponentContext; +import org.wso2.carbon.core.ServerStartupObserver; import org.wso2.carbon.ndatasource.core.DataSourceService; import java.util.List; @@ -73,7 +74,9 @@ public class HeartBeatBeaconComponent { clusterFormationChangedNotifierRepository); //Setting up executors to notify heart beat status */ - HeartBeatExecutor.setUpNotifiers(HeartBeatBeaconUtils.getServerDetails()); + HeartBeatExecutor heartBeatExecutor = new HeartBeatExecutor(); + componentContext.getBundleContext().registerService( + ServerStartupObserver.class.getName(), heartBeatExecutor, null); } diff --git a/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/internal/HeartBeatExecutor.java b/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/internal/HeartBeatExecutor.java index bd19a185a8..bcadf4afe7 100644 --- a/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/internal/HeartBeatExecutor.java +++ b/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/internal/HeartBeatExecutor.java @@ -26,13 +26,16 @@ import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.dto.ServerContex import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.exception.HeartBeatManagementException; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.wso2.carbon.core.ServerStartupObserver; import java.io.IOException; +import java.net.SocketException; +import java.net.UnknownHostException; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; import java.util.concurrent.TimeUnit; -public class HeartBeatExecutor { +public class HeartBeatExecutor implements ServerStartupObserver { private static Log log = LogFactory.getLog(HeartBeatExecutor.class); private static final int DEFAULT__NOTIFIER_INTERVAL = 5; @@ -43,6 +46,20 @@ public class HeartBeatExecutor { CONFIG = HeartBeatBeaconConfig.getInstance(); } + @Override + public void completingServerStartup() { + + } + + @Override + public void completedServerStartup() { + try { + setUpNotifiers(HeartBeatBeaconUtils.getServerDetails()); + } catch (HeartBeatBeaconConfigurationException | UnknownHostException | SocketException e) { + throw new RuntimeException(e); + } + } + static void setUpNotifiers(ServerContext ctx) throws HeartBeatBeaconConfigurationException { ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor(); diff --git a/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/service/ClusterFormationChangedNotifierRepository.java b/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/service/ClusterFormationChangedNotifierRepository.java index 9c711366ae..bb0c7726de 100644 --- a/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/service/ClusterFormationChangedNotifierRepository.java +++ b/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/service/ClusterFormationChangedNotifierRepository.java @@ -15,9 +15,8 @@ * specific language governing permissions and limitations * under the License. */ -package io.entgra.device.mgt.core.device.mgt.core.push.notification.mgt; +package io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service; -import io.entgra.device.mgt.core.server.bootup.heartbeat.beacon.service.ClusterFormationChangedNotifier; import org.apache.commons.lang.StringUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -41,7 +40,7 @@ public class ClusterFormationChangedNotifierRepository { public void addNotifier(String className) { try { if (!StringUtils.isEmpty(className)) { - Class clz = ClassLoader.getSystemClassLoader()forName(className); + Class clz = Class.forName(className); ClusterFormationChangedNotifier notifier = (ClusterFormationChangedNotifier) clz.newInstance(); notifiers.put(notifier.getType(), notifier); } diff --git a/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/service/HeartBeatManagementServiceImpl.java b/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/service/HeartBeatManagementServiceImpl.java index 7a31a6ebcc..6c737c0b2f 100644 --- a/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/service/HeartBeatManagementServiceImpl.java +++ b/components/heartbeat-management/io.entgra.device.mgt.core.server.bootup.heartbeat.beacon/src/main/java/io/entgra/device/mgt/core/server/bootup/heartbeat/beacon/service/HeartBeatManagementServiceImpl.java @@ -235,6 +235,7 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic } } else { //first time execution, elect if not present + heartBeatDAO.purgeCandidates(); electCandidate(servers); } HeartBeatBeaconDAOFactory.commitTransaction(); @@ -268,6 +269,10 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic String serverUUID = HeartBeatBeaconDataHolder.getInstance().getLocalServerUUID(); ServerContext serverContext = servers.get(serverUUID); + if (log.isDebugEnabled()) { + log.debug("HashIndex (previous, current) : " + lastHashIndex + ", " + serverContext.getIndex()); + log.debug("ActiveServerCount (previous, current) : " + lastActiveCount + ", " + servers.size()); + } // cluster change can be identified, either by changing hash index or changing active server count if ((lastHashIndex != serverContext.getIndex()) || (lastActiveCount != servers.size())) { lastHashIndex = serverContext.getIndex(); @@ -280,6 +285,9 @@ public class HeartBeatManagementServiceImpl implements HeartBeatManagementServic Runnable r = new Runnable() { @Override public void run() { + if (log.isDebugEnabled()) { + log.debug("notify cluster formation changed : " + notifier.getType()); + } notifier.notifyClusterFormationChanged(lastHashIndex, lastActiveCount); } };