Sahina Bose has uploaded a new change for review.

Change subject: engine:Sync gluster hooks
......................................................................

engine:Sync gluster hooks

This patch adds a periodic job to sync gluster hooks
on servers in the cluster with the ones stores in database.

Every execution of the job
- Retrieves the list of hooks from each server in the cluster
- Compares the list with the saved data in database
- If different, inserts or updated the database
- Changes conflict status, if any status, content or missing
  conflict detected.

Refactored GlusterManager to GlusterSyncManager and to extend
from GlusterJobsManager.

GlusterJobsManager will now schedule each of the jobs.

Change-Id: I2766edd4e810677a200cf5c45d334cabef5f2924
Signed-off-by: Sahina Bose <sab...@redhat.com>
---
M backend/manager/dbscripts/upgrade/pre_upgrade/0000_config.sql
M 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/InitBackendServicesOnStartupBean.java
A 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterHookManager.java
A 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterJobsManager.java
R 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterSyncManager.java
A 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GlusterHookManagerTest.java
R 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GlusterSyncManagerTest.java
M 
backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/config/ConfigValues.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/gluster/GlusterHooksDao.java
M 
backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/gluster/GlusterHooksDaoDbFacadeImpl.java
10 files changed, 601 insertions(+), 125 deletions(-)


  git pull ssh://gerrit.ovirt.org:29418/ovirt-engine refs/changes/43/13943/1

diff --git a/backend/manager/dbscripts/upgrade/pre_upgrade/0000_config.sql 
b/backend/manager/dbscripts/upgrade/pre_upgrade/0000_config.sql
index b55c6b3..d1e98ed 100644
--- a/backend/manager/dbscripts/upgrade/pre_upgrade/0000_config.sql
+++ b/backend/manager/dbscripts/upgrade/pre_upgrade/0000_config.sql
@@ -135,6 +135,10 @@
 select 
fn_db_add_config_value('GlusterVolumeOptionGroupVirtValue','virt','general');
 select 
fn_db_add_config_value('GlusterVolumeOptionOwnerUserVirtValue','36','general');
 select 
fn_db_add_config_value('GlusterVolumeOptionOwnerGroupVirtValue','36','general');
+-- Gluster refresh rates (in seconds)
+select fn_db_add_config_value('GlusterRefreshRateLight', '5', 'general');
+select fn_db_add_config_value('GlusterRefreshRateHeavy', '300', 'general');
+select fn_db_add_config_value('GlusterRefreshRateHooks', '600', 'general');
 select 
fn_db_add_config_value('GuestToolsSetupIsoPrefix','RHEV-toolsSetup_','general');
 select fn_db_add_config_value('HardwareInfoEnabled','false','3.0');
 select fn_db_add_config_value('HardwareInfoEnabled','false','3.1');
@@ -514,9 +518,7 @@
 select fn_db_add_config_value('AutoRecoveryAllowedTypes','{\"storage 
domains\":\"true\",\"hosts\":\"true\"}','general');
 -- Client console mode settings (Auto, Native, Plugin)
 select fn_db_add_config_value('ClientConsoleModeDefault','Auto','general');
--- Gluster refresh rates (in seconds)
-select fn_db_add_config_value('GlusterRefreshRateLight', '5', 'general');
-select fn_db_add_config_value('GlusterRefreshRateHeavy', '300', 'general');
+
 
 select fn_db_add_config_value('LogMaxPhysicalMemoryUsedThresholdInPercentage', 
'95', 'general');
 select fn_db_add_config_value('LogMaxCpuUsedThresholdInPercentage', '95', 
'general');
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/InitBackendServicesOnStartupBean.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/InitBackendServicesOnStartupBean.java
index 789cf38..22c0feb 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/InitBackendServicesOnStartupBean.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/InitBackendServicesOnStartupBean.java
@@ -6,7 +6,7 @@
 import javax.ejb.Singleton;
 import javax.ejb.Startup;
 
-import org.ovirt.engine.core.bll.gluster.GlusterManager;
+import org.ovirt.engine.core.bll.gluster.GlusterJobsManager;
 import org.ovirt.engine.core.bll.network.MacPoolManager;
 import org.ovirt.engine.core.bll.storage.StoragePoolStatusHandler;
 import org.ovirt.engine.core.common.config.Config;
@@ -53,7 +53,8 @@
         });
         StoragePoolStatusHandler.Init();
 
-        GlusterManager.getInstance().init();
+        GlusterJobsManager.init();
+
         try {
             log.info("Init VM custom properties utilities");
             VmPropertiesUtils.getInstance().init();
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterHookManager.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterHookManager.java
new file mode 100644
index 0000000..448c61a
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterHookManager.java
@@ -0,0 +1,253 @@
+package org.ovirt.engine.core.bll.gluster;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.concurrent.Callable;
+
+import org.ovirt.engine.core.common.businessentities.VDS;
+import org.ovirt.engine.core.common.businessentities.VDSGroup;
+import org.ovirt.engine.core.common.businessentities.gluster.GlusterHookEntity;
+import org.ovirt.engine.core.common.businessentities.gluster.GlusterHookStage;
+import org.ovirt.engine.core.common.businessentities.gluster.GlusterServerHook;
+import org.ovirt.engine.core.common.utils.ObjectUtils;
+import org.ovirt.engine.core.common.utils.Pair;
+import org.ovirt.engine.core.common.vdscommands.VDSCommandType;
+import org.ovirt.engine.core.common.vdscommands.VDSReturnValue;
+import org.ovirt.engine.core.common.vdscommands.VdsIdVDSCommandParametersBase;
+import org.ovirt.engine.core.compat.Guid;
+import org.ovirt.engine.core.utils.log.Log;
+import org.ovirt.engine.core.utils.log.LogFactory;
+import org.ovirt.engine.core.utils.threadpool.ThreadPoolUtil;
+import org.ovirt.engine.core.utils.timer.OnTimerMethodAnnotation;
+
+public class GlusterHookManager extends GlusterJobsManager {
+    private static final Log log = LogFactory.getLog(GlusterHookManager.class);
+
+    private static final GlusterHookManager instance = new 
GlusterHookManager();
+
+    public static GlusterHookManager getInstance() {
+       return instance;
+    }
+
+    @OnTimerMethodAnnotation("refreshHooks")
+    public void refreshHooks() {
+        log.debug("Refreshing hooks list");
+        List<VDSGroup> clusters = getClusterDao().getAll();
+
+        for (VDSGroup cluster : clusters) {
+            List<VDS> upServers = 
getClusterUtils().getAllUpServers(cluster.getId());
+
+            List<Callable<Pair<VDS, VDSReturnValue>>> taskList = new 
ArrayList<Callable<Pair<VDS, VDSReturnValue>>>();
+            for (final VDS upServer : upServers) {
+                taskList.add(new Callable<Pair<VDS, VDSReturnValue>>() {
+                    @Override
+                    public Pair<VDS, VDSReturnValue> call() throws Exception {
+                        VDSReturnValue returnValue 
=runVdsCommand(VDSCommandType.GlusterHooksList,
+                                new 
VdsIdVDSCommandParametersBase(upServer.getId()));
+                        return new Pair<VDS, VDSReturnValue>(upServer, 
returnValue);
+                    }
+                });
+            }
+            List<Pair<VDS, VDSReturnValue>> pairResults = 
ThreadPoolUtil.invokeAll(taskList);
+
+            addOrUpdateHooks(cluster.getId(), pairResults);
+        }
+    }
+
+    private void addOrUpdateHooks(Guid clusterId,  List<Pair<VDS, 
VDSReturnValue>> pairResults ) {
+        List<GlusterHookEntity> existingHooks = 
getHooksDao().getByClusterId(clusterId);
+        Map<HookKey, GlusterHookEntity> existingHookMap = new 
HashMap<HookKey,GlusterHookEntity>();
+        for (GlusterHookEntity hook: existingHooks) {
+            existingHookMap.put(new 
HookKey(hook.getGlusterCommand(),hook.getStage().name(), hook.getName()), hook);
+        }
+
+        List<HookKey> returnedHookKeyList = new ArrayList<HookKey>();
+        Map<HookKey, GlusterHookEntity> newHookMap = new 
HashMap<HookKey,GlusterHookEntity>();
+        List<GlusterServerHook> newServerHooks = new 
ArrayList<GlusterServerHook>();
+        List<GlusterServerHook> updatedServerHooks = new 
ArrayList<GlusterServerHook>();
+        Map<HookKey, GlusterHookEntity> hooksWithConflictChanges = new 
HashMap<HookKey,GlusterHookEntity>();
+
+        for (Pair<VDS, VDSReturnValue> pairResult : pairResults) {
+            VDS server = pairResult.getFirst();
+            List<GlusterHookEntity> returnedHooks = (List<GlusterHookEntity>) 
pairResult.getSecond().getReturnValue();
+
+            for (GlusterHookEntity retHook: returnedHooks) {
+                HookKey key= 
HookKey.valueOf(retHook.getGlusterCommand(),retHook.getStage(),retHook.getName());
+                GlusterHookEntity existingHook = existingHookMap.get(key);
+
+                addToReturnedHookKeyList(returnedHookKeyList, retHook);
+
+                if (existingHook != null) {
+                    GlusterServerHook serverHook = 
getServerHookForServer(existingHook, server);
+                    // if gsh null, insert serverhook
+                    if (serverHook == null) {
+                        log.infoFormat("Adding new server hook {0} in server 
{1} ", key,server);
+                        newServerHooks.add(buildServerHook(server.getId(), 
existingHook.getId(), retHook));
+                    } else {
+                    // if not null, compare to see if update required
+                        if 
(!(serverHook.getChecksum().equals(retHook.getChecksum()) && 
serverHook.getContentType().equals(retHook.getContentType())
+                             && 
serverHook.getStatus().equals(retHook.getStatus()))) {
+                            serverHook.setChecksum(retHook.getChecksum());
+                            
serverHook.setContentType(retHook.getContentType());
+                            serverHook.setStatus(retHook.getStatus());
+                            updatedServerHooks.add(serverHook);
+                        }
+
+                    }
+                    Integer oldConflictValue = 
existingHook.getConflictStatus();
+                    existingHook = setConflict(existingHook, retHook);
+                    if 
(!oldConflictValue.equals(existingHook.getConflictStatus())) {
+                        log.debugFormat("Conflict change detected for hook {0} 
in server {1} ", key,server);
+                        if (hooksWithConflictChanges.containsKey(key)) {
+                            
existingHook.setConflictStatus(existingHook.getConflictStatus() | 
hooksWithConflictChanges.get(key).getConflictStatus());
+                        }
+                        hooksWithConflictChanges.put(key, existingHook);
+                    }
+                } else {
+                    log.infoFormat("Adding new hook {0} in server {1} ", 
key,server);
+                    existingHook = newHookMap.get(key);
+                    if (existingHook == null) {
+                        existingHook = retHook;
+                        existingHook.setId(Guid.NewGuid());
+                    }
+                    GlusterServerHook gsh = buildServerHook(server.getId(), 
existingHook.getId(), retHook);
+                    existingHook.getServerHooks().add(gsh);
+                    existingHook = setConflict(existingHook, retHook);
+                    newHookMap.put(key, existingHook);
+                }
+            }
+        }
+
+        //Save new hooks
+        for (GlusterHookEntity hook: newHookMap.values()) {
+            getHooksDao().save(hook);
+        }
+
+        //Add new server hooks
+        for (GlusterServerHook serverHook: newServerHooks) {
+            getHooksDao().saveGlusterServerHook(serverHook);
+        }
+
+        //Update existing server hooks
+        for (GlusterServerHook serverHook: updatedServerHooks) {
+            getHooksDao().updateGlusterServerHook(serverHook);
+        }
+
+        //Update conflict status for existing hooks
+        for (GlusterHookEntity hook: hooksWithConflictChanges.values()) {
+            getHooksDao().updateGlusterHookConflictStatus(hook.getId(), 
hook.getConflictStatus());
+        }
+
+        //Update missing conflicts for hooks found only in db
+        Set<HookKey> hooksOnlyInDB = new 
HashSet<HookKey>(existingHookMap.keySet());
+        hooksOnlyInDB.removeAll(returnedHookKeyList);
+
+        for (HookKey key: hooksOnlyInDB) {
+            GlusterHookEntity hook = existingHookMap.get(key);
+            hook.addMissingConflict();
+            getHooksDao().updateGlusterHookConflictStatus(hook.getId(), 
hook.getConflictStatus());
+        }
+
+
+    }
+
+    private void addToReturnedHookKeyList(List<HookKey> returnedHookKeyList, 
GlusterHookEntity retHook) {
+        HookKey returnedHookKey = HookKey.valueOf(retHook.getGlusterCommand(), 
retHook.getStage(), retHook.getName());
+        if (!returnedHookKeyList.contains(returnedHookKey))
+            returnedHookKeyList.add(returnedHookKey);
+
+    }
+
+    private GlusterHookEntity setConflict(GlusterHookEntity hook, 
GlusterHookEntity returnedHook) {
+        //reinitialize conflict status as we are going to calculate conflicts 
again.
+        hook.setConflictStatus(0);
+        if (!hook.getChecksum().equals(returnedHook.getChecksum())) {
+            hook.addContentConflict();
+        }
+        if (!hook.getContentType().equals(returnedHook.getContentType())) {
+            hook.addContentConflict();
+        }
+        if (!hook.getStatus().equals(returnedHook.getStatus())) {
+            hook.addStatusConflict();
+        }
+        return hook;
+    }
+
+    private GlusterServerHook buildServerHook(Guid serverId, Guid hookId, 
GlusterHookEntity returnedHook) {
+        GlusterServerHook serverHook = new GlusterServerHook();
+        serverHook.setHookId(hookId);
+        serverHook.setServerId(serverId);
+        serverHook.setStatus(returnedHook.getStatus());
+        serverHook.setContentType(returnedHook.getContentType());
+        serverHook.setChecksum(returnedHook.getChecksum());
+        return serverHook;
+    }
+
+    private GlusterServerHook getServerHookForServer(GlusterHookEntity hook, 
VDS server) {
+        if (hook.getServerHooks() == null) {
+            
hook.setServerHooks(getHooksDao().getGlusterServerHooks(hook.getId()));
+        }
+        for (GlusterServerHook serverHook: hook.getServerHooks()) {
+            if (serverHook.getServerId().equals(server.getId())) {
+                return serverHook;
+            }
+        }
+        return null;
+    }
+
+
+    private static class HookKey {
+        private final String command;
+        private final String stage;
+        private final String name;
+
+        HookKey(String command, String stage, String name) {
+            this.command = command;
+            this.stage = stage;
+            this.name = name;
+        }
+
+        public static HookKey valueOf(String glusterCommand, GlusterHookStage 
stage, String name) {
+            return new HookKey(glusterCommand, stage.toString(), name);
+        }
+
+        @Override
+        public int hashCode() {
+            final int prime = 31;
+            int result = 1;
+            result = prime * result + ((command == null) ? 0 : 
command.hashCode());
+            result = prime * result + ((name == null) ? 0 : name.hashCode());
+            result = prime * result + ((stage == null) ? 0 : stage.hashCode());
+            return result;
+        }
+
+        @Override
+        public boolean equals(Object obj) {
+            if (this == obj)
+                return true;
+            if (obj == null)
+                return false;
+            if (getClass() != obj.getClass())
+                return false;
+            HookKey other = (HookKey) obj;
+            return ObjectUtils.objectsEqual(command, other.command)
+                  && ObjectUtils.objectsEqual(name, other.name)
+                  && ObjectUtils.objectsEqual(stage, other.stage);
+
+        }
+
+        @Override
+        public String toString() {
+            return command + "-" + stage + "-" + name;
+        }
+
+
+    }
+
+
+
+}
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterJobsManager.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterJobsManager.java
new file mode 100644
index 0000000..6561f87
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterJobsManager.java
@@ -0,0 +1,149 @@
+package org.ovirt.engine.core.bll.gluster;
+
+import java.util.List;
+import java.util.concurrent.TimeUnit;
+
+import org.ovirt.engine.core.bll.Backend;
+import org.ovirt.engine.core.bll.utils.ClusterUtils;
+import org.ovirt.engine.core.common.businessentities.VDS;
+import org.ovirt.engine.core.common.businessentities.gluster.GlusterServerInfo;
+import org.ovirt.engine.core.common.config.Config;
+import org.ovirt.engine.core.common.config.ConfigValues;
+import org.ovirt.engine.core.common.mode.ApplicationMode;
+import org.ovirt.engine.core.common.vdscommands.VDSCommandType;
+import org.ovirt.engine.core.common.vdscommands.VDSParametersBase;
+import org.ovirt.engine.core.common.vdscommands.VDSReturnValue;
+import org.ovirt.engine.core.common.vdscommands.VdsIdVDSCommandParametersBase;
+import org.ovirt.engine.core.dal.dbbroker.DbFacade;
+import org.ovirt.engine.core.dao.VdsDAO;
+import org.ovirt.engine.core.dao.VdsDynamicDAO;
+import org.ovirt.engine.core.dao.VdsGroupDAO;
+import org.ovirt.engine.core.dao.VdsStaticDAO;
+import org.ovirt.engine.core.dao.VdsStatisticsDAO;
+import org.ovirt.engine.core.dao.gluster.GlusterBrickDao;
+import org.ovirt.engine.core.dao.gluster.GlusterHooksDao;
+import org.ovirt.engine.core.dao.gluster.GlusterOptionDao;
+import org.ovirt.engine.core.dao.gluster.GlusterVolumeDao;
+import org.ovirt.engine.core.dao.network.InterfaceDao;
+import org.ovirt.engine.core.utils.log.Log;
+import org.ovirt.engine.core.utils.log.LogFactory;
+import org.ovirt.engine.core.utils.timer.SchedulerUtil;
+import org.ovirt.engine.core.utils.timer.SchedulerUtilQuartzImpl;
+
+public class GlusterJobsManager {
+
+    private static final Log log = LogFactory.getLog(GlusterJobsManager.class);
+
+    public static void init() {
+        if (!glusterModeSupported()) {
+            log.debug("Gluster mode not supported. Will not schedule jobs for 
refreshing Gluster data.");
+            return;
+        }
+
+        log.debug("Initializing Gluster Jobs Manager");
+
+        SchedulerUtil scheduler = SchedulerUtilQuartzImpl.getInstance();
+
+        scheduler.scheduleAFixedDelayJob(GlusterSyncManager.getInstance(),
+                "refreshLightWeightData",
+                new Class[0],
+                new Object[0],
+                getGlusterRefreshRateLight(),
+                getGlusterRefreshRateLight(),
+                TimeUnit.SECONDS);
+
+        scheduler.scheduleAFixedDelayJob(GlusterSyncManager.getInstance(),
+                "refreshHeavyWeightData",
+                new Class[0],
+                new Object[0],
+                getGlusterRefreshRateHeavy(),
+                getGlusterRefreshRateHeavy(),
+                TimeUnit.SECONDS);
+
+        scheduler.scheduleAFixedDelayJob(GlusterHookManager.getInstance(),
+                "refreshHooks",
+                new Class[0],
+                new Object[0],
+                getGlusterRefreshRateHooks(),
+                getGlusterRefreshRateHooks(),
+                TimeUnit.SECONDS);
+
+    }
+
+    private static boolean glusterModeSupported() {
+        Integer appMode = Config.<Integer> 
GetValue(ConfigValues.ApplicationMode);
+        return ((appMode & ApplicationMode.GlusterOnly.getValue()) > 0);
+    }
+
+    private static int getGlusterRefreshRateLight() {
+        return Config.<Integer> GetValue(ConfigValues.GlusterRefreshRateLight);
+    }
+
+    private static int getGlusterRefreshRateHeavy() {
+        return Config.<Integer> GetValue(ConfigValues.GlusterRefreshRateHeavy);
+    }
+
+    private static int getGlusterRefreshRateHooks() {
+        return Config.<Integer> GetValue(ConfigValues.GlusterRefreshRateHooks);
+    }
+
+    @SuppressWarnings("unchecked")
+    protected List<GlusterServerInfo> fetchServers(VDS upServer) {
+        VDSReturnValue result =
+                Backend.getInstance()
+                        .getResourceManager()
+                        .RunVdsCommand(VDSCommandType.GlusterServersList,
+                                new 
VdsIdVDSCommandParametersBase(upServer.getId()));
+
+        return result.getSucceeded() ? (List<GlusterServerInfo>) 
result.getReturnValue() : null;
+    }
+
+    protected VDSReturnValue runVdsCommand(VDSCommandType commandType, 
VDSParametersBase params) {
+        return 
Backend.getInstance().getResourceManager().RunVdsCommand(commandType, params);
+    }
+
+    protected ClusterUtils getClusterUtils() {
+        return ClusterUtils.getInstance();
+    }
+
+    protected VdsStatisticsDAO getVdsStatisticsDao() {
+        return DbFacade.getInstance().getVdsStatisticsDao();
+    }
+
+    protected VdsStaticDAO getVdsStaticDao() {
+        return DbFacade.getInstance().getVdsStaticDao();
+    }
+
+    protected VdsDynamicDAO getVdsDynamicDao() {
+        return DbFacade.getInstance().getVdsDynamicDao();
+    }
+
+    protected InterfaceDao getInterfaceDao() {
+        return DbFacade.getInstance().getInterfaceDao();
+    }
+
+    protected VdsGroupDAO getClusterDao() {
+        return DbFacade.getInstance().getVdsGroupDao();
+    }
+
+    protected VdsDAO getVdsDao() {
+        return DbFacade.getInstance().getVdsDao();
+    }
+
+    protected GlusterVolumeDao getVolumeDao() {
+        return DbFacade.getInstance().getGlusterVolumeDao();
+    }
+
+    protected GlusterOptionDao getOptionDao() {
+        return DbFacade.getInstance().getGlusterOptionDao();
+    }
+
+    protected GlusterBrickDao getBrickDao() {
+        return DbFacade.getInstance().getGlusterBrickDao();
+    }
+
+    protected GlusterHooksDao getHooksDao() {
+        return DbFacade.getInstance().getGlusterHooksDao();
+    }
+
+}
diff --git 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterManager.java
 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterSyncManager.java
similarity index 89%
rename from 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterManager.java
rename to 
backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterSyncManager.java
index a6924c0..db4cc53 100644
--- 
a/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterManager.java
+++ 
b/backend/manager/modules/bll/src/main/java/org/ovirt/engine/core/bll/gluster/GlusterSyncManager.java
@@ -8,12 +8,10 @@
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
-import java.util.concurrent.TimeUnit;
 
 import org.ovirt.engine.core.bll.Backend;
 import org.ovirt.engine.core.bll.LockMessagesMatchUtil;
 import org.ovirt.engine.core.bll.job.ExecutionHandler;
-import org.ovirt.engine.core.bll.utils.ClusterUtils;
 import org.ovirt.engine.core.common.AuditLogType;
 import org.ovirt.engine.core.common.action.SetNonOperationalVdsParameters;
 import org.ovirt.engine.core.common.action.VdcActionType;
@@ -30,43 +28,26 @@
 import 
org.ovirt.engine.core.common.businessentities.gluster.GlusterVolumeOptionEntity;
 import org.ovirt.engine.core.common.businessentities.gluster.TransportType;
 import 
org.ovirt.engine.core.common.businessentities.network.VdsNetworkInterface;
-import org.ovirt.engine.core.common.config.Config;
-import org.ovirt.engine.core.common.config.ConfigValues;
 import org.ovirt.engine.core.common.constants.gluster.GlusterConstants;
 import org.ovirt.engine.core.common.locks.LockingGroup;
-import org.ovirt.engine.core.common.mode.ApplicationMode;
 import org.ovirt.engine.core.common.utils.ListUtils;
 import org.ovirt.engine.core.common.utils.ObjectUtils;
 import org.ovirt.engine.core.common.utils.gluster.GlusterCoreUtil;
 import org.ovirt.engine.core.common.vdscommands.RemoveVdsVDSCommandParameters;
 import org.ovirt.engine.core.common.vdscommands.VDSCommandType;
-import org.ovirt.engine.core.common.vdscommands.VDSParametersBase;
 import org.ovirt.engine.core.common.vdscommands.VDSReturnValue;
-import org.ovirt.engine.core.common.vdscommands.VdsIdVDSCommandParametersBase;
 import 
org.ovirt.engine.core.common.vdscommands.gluster.GlusterVolumeAdvancedDetailsVDSParameters;
 import 
org.ovirt.engine.core.common.vdscommands.gluster.GlusterVolumesListVDSParameters;
 import org.ovirt.engine.core.compat.Guid;
 import org.ovirt.engine.core.dal.VdcBllMessages;
-import org.ovirt.engine.core.dal.dbbroker.DbFacade;
 import 
org.ovirt.engine.core.dal.dbbroker.auditloghandling.gluster.GlusterAuditLogUtil;
-import org.ovirt.engine.core.dao.VdsDAO;
-import org.ovirt.engine.core.dao.VdsDynamicDAO;
-import org.ovirt.engine.core.dao.VdsGroupDAO;
-import org.ovirt.engine.core.dao.VdsStaticDAO;
-import org.ovirt.engine.core.dao.VdsStatisticsDAO;
-import org.ovirt.engine.core.dao.gluster.GlusterBrickDao;
-import org.ovirt.engine.core.dao.gluster.GlusterOptionDao;
 import org.ovirt.engine.core.dao.gluster.GlusterDBUtils;
-import org.ovirt.engine.core.dao.gluster.GlusterVolumeDao;
-import org.ovirt.engine.core.dao.network.InterfaceDao;
 import org.ovirt.engine.core.utils.lock.EngineLock;
 import org.ovirt.engine.core.utils.lock.LockManager;
 import org.ovirt.engine.core.utils.lock.LockManagerFactory;
 import org.ovirt.engine.core.utils.log.Log;
 import org.ovirt.engine.core.utils.log.LogFactory;
 import org.ovirt.engine.core.utils.timer.OnTimerMethodAnnotation;
-import org.ovirt.engine.core.utils.timer.SchedulerUtil;
-import org.ovirt.engine.core.utils.timer.SchedulerUtilQuartzImpl;
 import org.ovirt.engine.core.utils.transaction.TransactionMethod;
 import org.ovirt.engine.core.utils.transaction.TransactionSupport;
 
@@ -75,16 +56,16 @@
  * GlusterFS. This helps to make sure that any changes done on Gluster servers 
using the Gluster CLI are propagated to
  * engine as well.
  */
-public class GlusterManager {
-    private final Log log = LogFactory.getLog(GlusterManager.class);
+public class GlusterSyncManager extends GlusterJobsManager {
+    private final Log log = LogFactory.getLog(GlusterSyncManager.class);
     private final LockManager lockManager = 
LockManagerFactory.getLockManager();
     private GlusterAuditLogUtil logUtil = GlusterAuditLogUtil.getInstance();
-    private static final GlusterManager instance = new GlusterManager();
+    private static final GlusterSyncManager instance = new 
GlusterSyncManager();
 
-    private GlusterManager() {
+    private GlusterSyncManager() {
     }
 
-    public static GlusterManager getInstance() {
+    public static GlusterSyncManager getInstance() {
         return instance;
     }
 
@@ -95,37 +76,6 @@
         this.logUtil = logUtil;
     }
 
-    public void init() {
-        if (!glusterModeSupported()) {
-            log.debug("Gluster mode not supported. Will not schedule jobs for 
refreshing Gluster data.");
-            return;
-        }
-
-        log.debug("Initializing Gluster Manager");
-
-        SchedulerUtil scheduler = SchedulerUtilQuartzImpl.getInstance();
-
-        scheduler.scheduleAFixedDelayJob(this,
-                "refreshLightWeightData",
-                new Class[0],
-                new Object[0],
-                getGlusterRefreshRateLight(),
-                getGlusterRefreshRateLight(),
-                TimeUnit.SECONDS);
-
-        scheduler.scheduleAFixedDelayJob(this,
-                "refreshHeavyWeightData",
-                new Class[0],
-                new Object[0],
-                getGlusterRefreshRateHeavy(),
-                getGlusterRefreshRateHeavy(),
-                TimeUnit.SECONDS);
-    }
-
-    private boolean glusterModeSupported() {
-        Integer appMode = Config.<Integer> 
GetValue(ConfigValues.ApplicationMode);
-        return ((appMode & ApplicationMode.GlusterOnly.getValue()) > 0);
-    }
 
     /**
      * Acquires a lock on the cluster with given id and locking group {@link 
LockingGroup#GLUSTER}
@@ -263,10 +213,6 @@
         });
     }
 
-    protected VDSReturnValue runVdsCommand(VDSCommandType commandType, 
VDSParametersBase params) {
-        return 
Backend.getInstance().getResourceManager().RunVdsCommand(commandType, params);
-    }
-
     /**
      * We need to be particularly careful about what servers we remove from 
the DB. A newly added (bootstrapped) server
      * gets peer probed after it's first reboot, and we don't want to 
accidentally remove such legitimate servers just
@@ -384,17 +330,6 @@
         
Backend.getInstance().runInternalAction(VdcActionType.SetNonOperationalVds,
                 nonOpParams,
                 ExecutionHandler.createInternalJobContext());
-    }
-
-    @SuppressWarnings("unchecked")
-    protected List<GlusterServerInfo> fetchServers(VDS upServer) {
-        VDSReturnValue result =
-                Backend.getInstance()
-                        .getResourceManager()
-                        .RunVdsCommand(VDSCommandType.GlusterServersList,
-                                new 
VdsIdVDSCommandParametersBase(upServer.getId()));
-
-        return result.getSucceeded() ? (List<GlusterServerInfo>) 
result.getReturnValue() : null;
     }
 
     /**
@@ -878,51 +813,4 @@
         getVdsDynamicDao().remove(server.getId());
     }
 
-    protected ClusterUtils getClusterUtils() {
-        return ClusterUtils.getInstance();
-    }
-
-    protected VdsStatisticsDAO getVdsStatisticsDao() {
-        return DbFacade.getInstance().getVdsStatisticsDao();
-    }
-
-    protected VdsStaticDAO getVdsStaticDao() {
-        return DbFacade.getInstance().getVdsStaticDao();
-    }
-
-    protected VdsDynamicDAO getVdsDynamicDao() {
-        return DbFacade.getInstance().getVdsDynamicDao();
-    }
-
-    protected InterfaceDao getInterfaceDao() {
-        return DbFacade.getInstance().getInterfaceDao();
-    }
-
-    protected VdsGroupDAO getClusterDao() {
-        return DbFacade.getInstance().getVdsGroupDao();
-    }
-
-    protected VdsDAO getVdsDao() {
-        return DbFacade.getInstance().getVdsDao();
-    }
-
-    protected GlusterVolumeDao getVolumeDao() {
-        return DbFacade.getInstance().getGlusterVolumeDao();
-    }
-
-    protected GlusterOptionDao getOptionDao() {
-        return DbFacade.getInstance().getGlusterOptionDao();
-    }
-
-    protected GlusterBrickDao getBrickDao() {
-        return DbFacade.getInstance().getGlusterBrickDao();
-    }
-
-    private int getGlusterRefreshRateLight() {
-        return Config.<Integer> GetValue(ConfigValues.GlusterRefreshRateLight);
-    }
-
-    private int getGlusterRefreshRateHeavy() {
-        return Config.<Integer> GetValue(ConfigValues.GlusterRefreshRateHeavy);
-    }
 }
diff --git 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GlusterHookManagerTest.java
 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GlusterHookManagerTest.java
new file mode 100644
index 0000000..bad137f
--- /dev/null
+++ 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GlusterHookManagerTest.java
@@ -0,0 +1,169 @@
+package org.ovirt.engine.core.bll.gluster;
+
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.argThat;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.times;
+import static org.ovirt.engine.core.utils.MockConfigRule.mockConfig;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.ArgumentMatcher;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.mockito.runners.MockitoJUnitRunner;
+import org.ovirt.engine.core.bll.utils.ClusterUtils;
+import org.ovirt.engine.core.common.businessentities.VDS;
+import org.ovirt.engine.core.common.businessentities.VDSGroup;
+import org.ovirt.engine.core.common.businessentities.VDSStatus;
+import org.ovirt.engine.core.common.businessentities.VdsDynamic;
+import org.ovirt.engine.core.common.businessentities.VdsStatic;
+import org.ovirt.engine.core.common.businessentities.VdsStatistics;
+import 
org.ovirt.engine.core.common.businessentities.gluster.GlusterHookContentType;
+import org.ovirt.engine.core.common.businessentities.gluster.GlusterHookEntity;
+import org.ovirt.engine.core.common.businessentities.gluster.GlusterHookStatus;
+import org.ovirt.engine.core.common.config.ConfigValues;
+import org.ovirt.engine.core.common.vdscommands.VDSCommandType;
+import org.ovirt.engine.core.common.vdscommands.VDSParametersBase;
+import org.ovirt.engine.core.common.vdscommands.VDSReturnValue;
+import org.ovirt.engine.core.common.vdscommands.VdsIdVDSCommandParametersBase;
+import org.ovirt.engine.core.compat.Guid;
+import org.ovirt.engine.core.dao.VdsGroupDAO;
+import org.ovirt.engine.core.dao.gluster.GlusterHooksDao;
+import org.ovirt.engine.core.utils.MockConfigRule;
+
+@RunWith(MockitoJUnitRunner.class)
+public class GlusterHookManagerTest {
+    private static final Guid[] SERVER_GUIDS = {new 
Guid("11111111-1111-1111-1111-111111111111"),
+                                         new 
Guid("22222222-2222-2222-2222-222222222222"),
+                                         new 
Guid("33333333-3333-3333-3333-333333333333")};
+    private static final Guid[] CLUSTER_GUIDS = {new 
Guid("CC111111-1111-1111-1111-111111111111")};
+
+    @Mock
+    private ClusterUtils clusterUtils;
+
+    @Mock
+    private GlusterHooksDao hooksDao;
+
+    @Mock
+    private VdsGroupDAO clusterDao;
+
+    private GlusterHookManager hookManager;
+
+    @ClassRule
+    public static MockConfigRule mcr = new MockConfigRule(
+            mockConfig(ConfigValues.DefaultMinThreadPoolSize,10),
+            mockConfig(ConfigValues.DefaultMaxThreadPoolSize, 20));
+
+    @Before
+    public void init() {
+        MockitoAnnotations.initMocks(this);
+    }
+
+    private void mockDaos() {
+
+        doReturn(clusterDao).when(hookManager).getClusterDao();
+        doReturn(hooksDao).when(hookManager).getHooksDao();
+
+        
doReturn(Collections.singletonList(createCluster())).when(clusterDao).getAll();
+        doReturn(getHooksList(2, 
true)).when(hooksDao).getByClusterId(CLUSTER_GUIDS[0]);
+    }
+
+
+    private void initMocks() {
+
+        hookManager = Mockito.spy(GlusterHookManager.getInstance());
+        doReturn(clusterUtils).when(hookManager).getClusterUtils();
+        
doReturn(getServers()).when(clusterUtils).getAllUpServers(any(Guid.class));
+        
doReturn(getHooksListVDSReturnVal()).when(hookManager).runVdsCommand(eq(VDSCommandType.GlusterHooksList),
+                argThat(anyUpServer()));
+        mockDaos();
+    }
+
+    private Object getHooksListVDSReturnVal() {
+        VDSReturnValue vdsRetValue = new VDSReturnValue();
+        vdsRetValue.setSucceeded(true);
+        vdsRetValue.setReturnValue(getHooksList(3, false));
+        return vdsRetValue;
+    }
+
+    private List<GlusterHookEntity> getHooksList(int listCount, boolean 
setIds) {
+        List<GlusterHookEntity> hookList = new ArrayList<GlusterHookEntity>();
+        for (int i=0; i<= listCount; i++) {
+            hookList.add(getHook("create", "hook-" + i, setIds));
+        }
+        return hookList;
+    }
+
+    private GlusterHookEntity getHook(String command, String name, boolean 
setIds) {
+        GlusterHookEntity hook = new GlusterHookEntity();
+        hook.setChecksum("234230--090934");
+        hook.setContentType(GlusterHookContentType.TEXT);
+        hook.setStatus(GlusterHookStatus.ENABLED);
+        hook.setGlusterCommand(command);
+        hook.setStage("PRE");
+        hook.setName(name);
+        if (setIds) {
+            hook.setId(Guid.NewGuid());
+            hook.setClusterId(CLUSTER_GUIDS[0]);
+        }
+        return hook;
+    }
+
+    private ArgumentMatcher<VDSParametersBase> anyUpServer() {
+        return new ArgumentMatcher<VDSParametersBase>() {
+
+            @Override
+            public boolean matches(Object argument) {
+                if (!(argument instanceof VdsIdVDSCommandParametersBase)) {
+                    return false;
+                }
+
+                return true;
+            }
+        };
+    }
+
+    private List<VDS> getServers() {
+       List<VDS> vdsList = new ArrayList<VDS>();
+       vdsList.add(createServer(SERVER_GUIDS[0], "HOST-0"));
+       vdsList.add(createServer(SERVER_GUIDS[1], "HOST-1"));
+       return vdsList;
+    }
+
+    private VDS createServer(Guid serverId, String hostname) {
+        VdsStatic vdsStatic = new VdsStatic();
+        vdsStatic.setId(serverId);
+        vdsStatic.setHostName(hostname);
+        VdsDynamic vdsDynamic = new VdsDynamic();
+        vdsDynamic.setstatus(VDSStatus.Up);
+        return new VDS(vdsStatic, vdsDynamic, new VdsStatistics());
+    }
+
+    private VDSGroup createCluster() {
+        VDSGroup cluster = new VDSGroup();
+        cluster.setId(CLUSTER_GUIDS[0]);
+        cluster.setname("cluster");
+        cluster.setGlusterService(true);
+        cluster.setVirtService(false);
+        return cluster;
+    }
+
+    @Test
+    public void syncHooks() {
+        initMocks();
+        hookManager.refreshHooks();
+        Mockito.verify(hooksDao, times(1)).save(any(GlusterHookEntity.class));
+        //Mockito.verify(hooksDao, 
times(1)).saveGlusterServerHook(any(GlusterServerHook.class));
+    }
+
+
+}
diff --git 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GlusterManagerTest.java
 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GlusterSyncManagerTest.java
similarity index 99%
rename from 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GlusterManagerTest.java
rename to 
backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GlusterSyncManagerTest.java
index fde740b..d5521d0 100644
--- 
a/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GlusterManagerTest.java
+++ 
b/backend/manager/modules/bll/src/test/java/org/ovirt/engine/core/bll/gluster/GlusterSyncManagerTest.java
@@ -67,7 +67,7 @@
 import org.ovirt.engine.core.utils.MockEJBStrategyRule;
 
 @RunWith(MockitoJUnitRunner.class)
-public class GlusterManagerTest {
+public class GlusterSyncManagerTest {
 
     private static final String REPL_VOL_NAME = "repl-vol";
 
@@ -84,7 +84,7 @@
     @ClassRule
     public static MockEJBStrategyRule ejbRule = new MockEJBStrategyRule();
 
-    private GlusterManager glusterManager;
+    private GlusterSyncManager glusterManager;
     private GlusterAuditLogUtil logUtil;
 
     private static final String OPTION_AUTH_ALLOW = "auth.allow";
@@ -211,7 +211,7 @@
     @SuppressWarnings("unchecked")
     private void setupMocks() throws Exception {
         logUtil = Mockito.spy(GlusterAuditLogUtil.getInstance());
-        glusterManager = Mockito.spy(GlusterManager.getInstance());
+        glusterManager = Mockito.spy(GlusterSyncManager.getInstance());
         glusterManager.setLogUtil(logUtil);
         mockDaos();
 
diff --git 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/config/ConfigValues.java
 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/config/ConfigValues.java
index 467b9ab..125bb96 100644
--- 
a/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/config/ConfigValues.java
+++ 
b/backend/manager/modules/common/src/main/java/org/ovirt/engine/core/common/config/ConfigValues.java
@@ -1347,6 +1347,12 @@
     @DefaultValueAttribute("false")
     MigrationNetworkEnabled(502),
 
+
+    @TypeConverterAttribute(Integer.class)
+    @DefaultValueAttribute("600")
+    GlusterRefreshRateHooks(503),
+
+
     Invalid(65535);
 
     private int intValue;
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/gluster/GlusterHooksDao.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/gluster/GlusterHooksDao.java
index 23d2a6e..ac19f31 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/gluster/GlusterHooksDao.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/gluster/GlusterHooksDao.java
@@ -38,6 +38,8 @@
 
     public GlusterHookEntity getGlusterHook(Guid clusterId, String 
glusterCommand, GlusterHookStage stage, String hookName);
 
+    public List<GlusterServerHook> getGlusterServerHooks(Guid hookId);
+
     public GlusterServerHook getGlusterServerHook(Guid hookId, Guid serverId);
 
     public List<GlusterHookEntity> getByClusterId(Guid clusterId);
diff --git 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/gluster/GlusterHooksDaoDbFacadeImpl.java
 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/gluster/GlusterHooksDaoDbFacadeImpl.java
index 48de214..22e334e 100644
--- 
a/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/gluster/GlusterHooksDaoDbFacadeImpl.java
+++ 
b/backend/manager/modules/dal/src/main/java/org/ovirt/engine/core/dao/gluster/GlusterHooksDaoDbFacadeImpl.java
@@ -75,6 +75,12 @@
         return glusterHook;
     }
 
+    public List<GlusterServerHook> getGlusterServerHooks(Guid hookId) {
+        List<GlusterServerHook> serverHooks = 
getCallsHandler().executeReadList("GetGlusterServerHooksById", 
glusterServerHookRowMapper,
+                createIdParameterMapper(hookId));
+        return serverHooks;
+    }
+
     @Override
     public GlusterServerHook getGlusterServerHook(Guid hookId, Guid serverId) {
         GlusterServerHook serverHook = 
getCallsHandler().executeRead("GetGlusterServerHook",


--
To view, visit http://gerrit.ovirt.org/13943
To unsubscribe, visit http://gerrit.ovirt.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I2766edd4e810677a200cf5c45d334cabef5f2924
Gerrit-PatchSet: 1
Gerrit-Project: ovirt-engine
Gerrit-Branch: master
Gerrit-Owner: Sahina Bose <sab...@redhat.com>
_______________________________________________
Engine-patches mailing list
Engine-patches@ovirt.org
http://lists.ovirt.org/mailman/listinfo/engine-patches

Reply via email to