Skip to content

Commit d9ebb71

Browse files
author
abhishek
committed
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
1 parent 4d2e126 commit d9ebb71

4 files changed

Lines changed: 39 additions & 17 deletions

File tree

‎core/src/com/cloud/certificate/dao/CertificateDaoImpl.java‎

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,33 +24,33 @@ public CertificateDaoImpl(){
2424
}
2525

2626
@Override
27-
public Long persistCustomCertToDb(String certPath){
28-
29-
String certStr = null;
30-
byte[] buffer = new byte[(int) new File(certPath).length()];
27+
public Long persistCustomCertToDb(String certPath){
3128
BufferedInputStream f = null;
32-
try
29+
String certStr = null;
30+
try
3331
{
32+
byte[] buffer = new byte[(int) new File(certPath).length()];
3433
f = new BufferedInputStream(new FileInputStream(certPath));
3534
f.read(buffer);
35+
certStr = new String(buffer);
36+
CertificateVO certRec = new CertificateVO();
37+
certRec.setCertificate(certStr);
38+
this.persist(certRec);
39+
return certRec.getId();
3640
} catch (FileNotFoundException e) {
3741
s_logger.warn("Unable to read the certificate: "+e);
3842
return new Long(0);
3943
} catch (IOException e) {
4044
s_logger.warn("Unable to read the certificate: "+e);
4145
return new Long(0);
42-
}
46+
} catch (Exception e){
47+
s_logger.warn("Unable to read the certificate: "+e);
48+
return new Long(0);
49+
}
4350
finally
4451
{
4552
if (f != null)
4653
try { f.close(); } catch (IOException ignored) { }
4754
}
48-
certStr = new String(buffer);
49-
50-
CertificateVO certRec = new CertificateVO();
51-
certRec.setCertificate(certStr);
52-
this.persist(certRec);
53-
54-
return certRec.getId();
5555
}
5656
}

‎core/src/com/cloud/host/dao/HostDao.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,9 @@ public interface HostDao extends GenericDao<HostVO, Long> {
137137

138138
void loadDetails(HostVO host);
139139

140-
HostVO findConsoleProxyHost(String name, Type type);
140+
HostVO findConsoleProxyHost(String name, Type type);
141+
142+
List<HostVO> listAllConsoleProxyHosts(Type type);
141143

142144

143145
}

‎core/src/com/cloud/host/dao/HostDaoImpl.java‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
8080
protected final SearchBuilder<HostVO> MaintenanceCountSearch;
8181
protected final SearchBuilder<HostVO> ClusterSearch;
8282
protected final SearchBuilder<HostVO> ConsoleProxyHostSearch;
83+
protected final SearchBuilder<HostVO> ConsoleProxyHostsListSearch;
8384

8485
protected final Attribute _statusAttr;
8586
protected final Attribute _msIdAttr;
@@ -160,6 +161,10 @@ public HostDaoImpl() {
160161
ConsoleProxyHostSearch.and("name", ConsoleProxyHostSearch.entity().getName(), SearchCriteria.Op.EQ);
161162
ConsoleProxyHostSearch.and("type", ConsoleProxyHostSearch.entity().getType(), SearchCriteria.Op.EQ);
162163
ConsoleProxyHostSearch.done();
164+
165+
ConsoleProxyHostsListSearch = createSearchBuilder();
166+
ConsoleProxyHostsListSearch.and("type", ConsoleProxyHostsListSearch.entity().getType(), SearchCriteria.Op.EQ);
167+
ConsoleProxyHostsListSearch.done();
163168

164169
PodSearch = createSearchBuilder();
165170
PodSearch.and("pod", PodSearch.entity().getPodId(), SearchCriteria.Op.EQ);
@@ -175,7 +180,7 @@ public HostDaoImpl() {
175180

176181
NameLikeSearch = createSearchBuilder();
177182
NameLikeSearch.and("name", NameLikeSearch.entity().getName(), SearchCriteria.Op.LIKE);
178-
NameLikeSearch.done();
183+
NameLikeSearch.done();
179184

180185
SequenceSearch = createSearchBuilder();
181186
SequenceSearch.and("id", SequenceSearch.entity().getId(), SearchCriteria.Op.EQ);
@@ -461,6 +466,13 @@ public HostVO findConsoleProxyHost(String name, Type type) {
461466
else
462467
return hostList.get(0);
463468
}
469+
470+
@Override
471+
public List<HostVO> listAllConsoleProxyHosts(Type type) {
472+
SearchCriteria<HostVO> sc = ConsoleProxyHostSearch.create();
473+
sc.setParameters("type", type);
474+
return listBy(sc);
475+
}
464476

465477
public List<HostVO> listByHostPod(long podId) {
466478
SearchCriteria<HostVO> sc = PodSearch.create("pod", podId);

‎server/src/com/cloud/server/ManagementServerImpl.java‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5880,13 +5880,21 @@ public boolean uploadCertificate(UploadCustomCertificateCmd cmd) throws Resource
58805880
//certficate uploaded to db successfully
58815881
//get a list of all Console proxies from the cp table
58825882
List<ConsoleProxyVO> cpList = _consoleProxyDao.listAll();
5883+
//get a list of all hosts in host table for type cp
5884+
List<HostVO> cpHosts = _hostDao.listAllConsoleProxyHosts(com.cloud.host.Host.Type.ConsoleProxy);
5885+
//create a hashmap for fast lookup
5886+
Map<String,Long> hostNameToHostIdMap = new HashMap<String, Long>();
5887+
for(HostVO cpHost : cpHosts){
5888+
hostNameToHostIdMap.put(cpHost.getName(), cpHost.getId());
5889+
}
5890+
58835891
for(ConsoleProxyVO cp : cpList)
58845892
{
5885-
HostVO cpHost = _hostDao.findConsoleProxyHost(cp.getName(), com.cloud.host.Host.Type.ConsoleProxy);
5893+
Long cpHostId = hostNameToHostIdMap.get(cp.getName());//there will always be a cphost for a cpvm
58865894
//now send a command to each console proxy
58875895
UpdateCertificateCommand certCmd = new UpdateCertificateCommand(_certDao.findById(certVOId).getCertificate());
58885896
try {
5889-
Answer updateCertAns = _agentMgr.send(cpHost.getId(), certCmd);
5897+
Answer updateCertAns = _agentMgr.send(cpHostId, certCmd);
58905898
if(updateCertAns.getResult() == true)
58915899
{
58925900
//we have the cert copied over on cpvm

0 commit comments

Comments
 (0)