From d9ebb7147c456d6f67f5fdfd00df20b2d6e539f1 Mon Sep 17 00:00:00 2001 From: abhishek Date: Tue, 26 Oct 2010 17:35:14 -0700 Subject: [PATCH] improving efficiency by reducing the number of db lookups, whilst updating the custom certificates across multiple console proxies when the command is issued. Now, we use in memory hashmaps for hosts to get the hostids for cphosts, which eliminates the need to make a db lookup for every cpvm that needs to be rebooted --- .../certificate/dao/CertificateDaoImpl.java | 26 +++++++++---------- core/src/com/cloud/host/dao/HostDao.java | 4 ++- core/src/com/cloud/host/dao/HostDaoImpl.java | 14 +++++++++- .../cloud/server/ManagementServerImpl.java | 12 +++++++-- 4 files changed, 39 insertions(+), 17 deletions(-) diff --git a/core/src/com/cloud/certificate/dao/CertificateDaoImpl.java b/core/src/com/cloud/certificate/dao/CertificateDaoImpl.java index 8ff6c918af5..6c2b4d846d8 100644 --- a/core/src/com/cloud/certificate/dao/CertificateDaoImpl.java +++ b/core/src/com/cloud/certificate/dao/CertificateDaoImpl.java @@ -24,33 +24,33 @@ public class CertificateDaoImpl extends GenericDaoBase imp } @Override - public Long persistCustomCertToDb(String certPath){ - - String certStr = null; - byte[] buffer = new byte[(int) new File(certPath).length()]; + public Long persistCustomCertToDb(String certPath){ BufferedInputStream f = null; - try + String certStr = null; + try { + byte[] buffer = new byte[(int) new File(certPath).length()]; f = new BufferedInputStream(new FileInputStream(certPath)); f.read(buffer); + certStr = new String(buffer); + CertificateVO certRec = new CertificateVO(); + certRec.setCertificate(certStr); + this.persist(certRec); + return certRec.getId(); } catch (FileNotFoundException e) { s_logger.warn("Unable to read the certificate: "+e); return new Long(0); } catch (IOException e) { s_logger.warn("Unable to read the certificate: "+e); return new Long(0); - } + } catch (Exception e){ + s_logger.warn("Unable to read the certificate: "+e); + return new Long(0); + } finally { if (f != null) try { f.close(); } catch (IOException ignored) { } } - certStr = new String(buffer); - - CertificateVO certRec = new CertificateVO(); - certRec.setCertificate(certStr); - this.persist(certRec); - - return certRec.getId(); } } diff --git a/core/src/com/cloud/host/dao/HostDao.java b/core/src/com/cloud/host/dao/HostDao.java index b68d7755245..7f0258aa74e 100644 --- a/core/src/com/cloud/host/dao/HostDao.java +++ b/core/src/com/cloud/host/dao/HostDao.java @@ -137,7 +137,9 @@ public interface HostDao extends GenericDao { void loadDetails(HostVO host); - HostVO findConsoleProxyHost(String name, Type type); + HostVO findConsoleProxyHost(String name, Type type); + + List listAllConsoleProxyHosts(Type type); } diff --git a/core/src/com/cloud/host/dao/HostDaoImpl.java b/core/src/com/cloud/host/dao/HostDaoImpl.java index 3acc97db6a5..952cfc500e5 100644 --- a/core/src/com/cloud/host/dao/HostDaoImpl.java +++ b/core/src/com/cloud/host/dao/HostDaoImpl.java @@ -80,6 +80,7 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao protected final SearchBuilder MaintenanceCountSearch; protected final SearchBuilder ClusterSearch; protected final SearchBuilder ConsoleProxyHostSearch; + protected final SearchBuilder ConsoleProxyHostsListSearch; protected final Attribute _statusAttr; protected final Attribute _msIdAttr; @@ -160,6 +161,10 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao ConsoleProxyHostSearch.and("name", ConsoleProxyHostSearch.entity().getName(), SearchCriteria.Op.EQ); ConsoleProxyHostSearch.and("type", ConsoleProxyHostSearch.entity().getType(), SearchCriteria.Op.EQ); ConsoleProxyHostSearch.done(); + + ConsoleProxyHostsListSearch = createSearchBuilder(); + ConsoleProxyHostsListSearch.and("type", ConsoleProxyHostsListSearch.entity().getType(), SearchCriteria.Op.EQ); + ConsoleProxyHostsListSearch.done(); PodSearch = createSearchBuilder(); PodSearch.and("pod", PodSearch.entity().getPodId(), SearchCriteria.Op.EQ); @@ -175,7 +180,7 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao NameLikeSearch = createSearchBuilder(); NameLikeSearch.and("name", NameLikeSearch.entity().getName(), SearchCriteria.Op.LIKE); - NameLikeSearch.done(); + NameLikeSearch.done(); SequenceSearch = createSearchBuilder(); SequenceSearch.and("id", SequenceSearch.entity().getId(), SearchCriteria.Op.EQ); @@ -461,6 +466,13 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao else return hostList.get(0); } + + @Override + public List listAllConsoleProxyHosts(Type type) { + SearchCriteria sc = ConsoleProxyHostSearch.create(); + sc.setParameters("type", type); + return listBy(sc); + } public List listByHostPod(long podId) { SearchCriteria sc = PodSearch.create("pod", podId); diff --git a/server/src/com/cloud/server/ManagementServerImpl.java b/server/src/com/cloud/server/ManagementServerImpl.java index d350e1aa43a..1c2bc1f5ba1 100755 --- a/server/src/com/cloud/server/ManagementServerImpl.java +++ b/server/src/com/cloud/server/ManagementServerImpl.java @@ -5880,13 +5880,21 @@ public class ManagementServerImpl implements ManagementServer { //certficate uploaded to db successfully //get a list of all Console proxies from the cp table List cpList = _consoleProxyDao.listAll(); + //get a list of all hosts in host table for type cp + List cpHosts = _hostDao.listAllConsoleProxyHosts(com.cloud.host.Host.Type.ConsoleProxy); + //create a hashmap for fast lookup + Map hostNameToHostIdMap = new HashMap(); + for(HostVO cpHost : cpHosts){ + hostNameToHostIdMap.put(cpHost.getName(), cpHost.getId()); + } + for(ConsoleProxyVO cp : cpList) { - HostVO cpHost = _hostDao.findConsoleProxyHost(cp.getName(), com.cloud.host.Host.Type.ConsoleProxy); + Long cpHostId = hostNameToHostIdMap.get(cp.getName());//there will always be a cphost for a cpvm //now send a command to each console proxy UpdateCertificateCommand certCmd = new UpdateCertificateCommand(_certDao.findById(certVOId).getCertificate()); try { - Answer updateCertAns = _agentMgr.send(cpHost.getId(), certCmd); + Answer updateCertAns = _agentMgr.send(cpHostId, certCmd); if(updateCertAns.getResult() == true) { //we have the cert copied over on cpvm