Skip to content

Commit 8cf48ed

Browse files
committed
CLOUDSTACK-4179: [Performance Testing] Time taken for Deploy VM async job to complete is considerably higher
The time increased due to the newly added dedicated resources feature. During regular VM deployment, all dedicated resources are put in avoid list so that they are not considered for deployment. Now the way to compute the list of dedicated resources is not optimal and performance deteriorates in an environment having lot of pods, clusters and hosts as the logic is to query db. for each suc resource. The fix is to optimize the logic not to loop through all resources but get the list of each resource type in a single query. Conflicts: server/src/com/cloud/deploy/DeploymentPlanningManagerImpl.java
1 parent 2100789 commit 8cf48ed

10 files changed

Lines changed: 122 additions & 64 deletions

File tree

api/src/com/cloud/deploy/DeploymentPlanner.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,13 @@ public void addPod(long podId) {
191191
_podIds.add(podId);
192192
}
193193

194+
public void addPodList(Collection<Long> podList) {
195+
if (_podIds == null) {
196+
_podIds = new HashSet<Long>();
197+
}
198+
_podIds.addAll(podList);
199+
}
200+
194201
public void addCluster(long clusterId) {
195202
if (_clusterIds == null) {
196203
_clusterIds = new HashSet<Long>();

engine/schema/src/com/cloud/dc/dao/ClusterDao.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,5 @@ public interface ClusterDao extends GenericDao<ClusterVO, Long> {
3535
List<Long> listDisabledClusters(long zoneId, Long podId);
3636
List<Long> listClustersWithDisabledPods(long zoneId);
3737
List<ClusterVO> listClustersByDcId(long zoneId);
38+
List<Long> listAllCusters(long zoneId);
3839
}

engine/schema/src/com/cloud/dc/dao/ClusterDaoImpl.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@ public class ClusterDaoImpl extends GenericDaoBase<ClusterVO, Long> implements C
5454
protected final SearchBuilder<ClusterVO> ZoneHyTypeSearch;
5555
protected final SearchBuilder<ClusterVO> ZoneClusterSearch;
5656

57+
protected GenericSearchBuilder<ClusterVO, Long> ClusterIdSearch;
58+
5759
private static final String GET_POD_CLUSTER_MAP_PREFIX = "SELECT pod_id, id FROM cloud.cluster WHERE cluster.id IN( ";
5860
private static final String GET_POD_CLUSTER_MAP_SUFFIX = " )";
5961
@Inject
@@ -90,6 +92,11 @@ public ClusterDaoImpl() {
9092
ZoneClusterSearch = createSearchBuilder();
9193
ZoneClusterSearch.and("dataCenterId", ZoneClusterSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
9294
ZoneClusterSearch.done();
95+
96+
ClusterIdSearch = createSearchBuilder(Long.class);
97+
ClusterIdSearch.selectField(ClusterIdSearch.entity().getId());
98+
ClusterIdSearch.and("dataCenterId", ClusterIdSearch.entity().getDataCenterId(), Op.EQ);
99+
ClusterIdSearch.done();
93100
}
94101

95102
@Override
@@ -168,11 +175,11 @@ public Map<Long, List<Long>> getPodClusterIdMap(List<Long> clusterIds){
168175
while (rs.next()) {
169176
Long podId = rs.getLong(1);
170177
Long clusterIdInPod = rs.getLong(2);
171-
if(result.containsKey(podId)){
178+
if (result.containsKey(podId)) {
172179
List<Long> clusterList = result.get(podId);
173180
clusterList.add(clusterIdInPod);
174181
result.put(podId, clusterList);
175-
}else{
182+
} else {
176183
List<Long> clusterList = new ArrayList<Long>();
177184
clusterList.add(clusterIdInPod);
178185
result.put(podId, clusterList);
@@ -191,13 +198,12 @@ public List<Long> listDisabledClusters(long zoneId, Long podId) {
191198
GenericSearchBuilder<ClusterVO, Long> clusterIdSearch = createSearchBuilder(Long.class);
192199
clusterIdSearch.selectField(clusterIdSearch.entity().getId());
193200
clusterIdSearch.and("dataCenterId", clusterIdSearch.entity().getDataCenterId(), Op.EQ);
194-
if(podId != null){
201+
if (podId != null) {
195202
clusterIdSearch.and("podId", clusterIdSearch.entity().getPodId(), Op.EQ);
196203
}
197204
clusterIdSearch.and("allocationState", clusterIdSearch.entity().getAllocationState(), Op.EQ);
198205
clusterIdSearch.done();
199206

200-
201207
SearchCriteria<Long> sc = clusterIdSearch.create();
202208
sc.addAnd("dataCenterId", SearchCriteria.Op.EQ, zoneId);
203209
if (podId != null) {
@@ -250,4 +256,10 @@ public boolean remove(Long id) {
250256
return result;
251257
}
252258

259+
@Override
260+
public List<Long> listAllCusters(long zoneId) {
261+
SearchCriteria<Long> sc = ClusterIdSearch.create();
262+
sc.setParameters("dataCenterId", zoneId);
263+
return customSearch(sc, null);
264+
}
253265
}

engine/schema/src/com/cloud/dc/dao/HostPodDao.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@
2424
import com.cloud.vm.VirtualMachine;
2525

2626
public interface HostPodDao extends GenericDao<HostPodVO, Long> {
27-
public List<HostPodVO> listByDataCenterId(long id);
27+
public List<HostPodVO> listByDataCenterId(long id);
2828

2929
public HostPodVO findByName(String name, long dcId);
30-
31-
public HashMap<Long, List<Object>> getCurrentPodCidrSubnets(long zoneId, long podIdToSkip);
30+
31+
public HashMap<Long, List<Object>> getCurrentPodCidrSubnets(long zoneId, long podIdToSkip);
3232

3333
public List<Long> listDisabledPods(long zoneId);
3434

35+
public List<Long> listAllPods(long zoneId);
3536
}

engine/schema/src/com/cloud/dc/dao/HostPodDaoImpl.java

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ public class HostPodDaoImpl extends GenericDaoBase<HostPodVO, Long> implements H
4444

4545
protected SearchBuilder<HostPodVO> DataCenterAndNameSearch;
4646
protected SearchBuilder<HostPodVO> DataCenterIdSearch;
47+
protected GenericSearchBuilder<HostPodVO, Long> PodIdSearch;
4748

4849
public HostPodDaoImpl() {
4950
DataCenterAndNameSearch = createSearchBuilder();
@@ -54,6 +55,12 @@ public HostPodDaoImpl() {
5455
DataCenterIdSearch = createSearchBuilder();
5556
DataCenterIdSearch.and("dcId", DataCenterIdSearch.entity().getDataCenterId(), SearchCriteria.Op.EQ);
5657
DataCenterIdSearch.done();
58+
59+
PodIdSearch = createSearchBuilder(Long.class);
60+
PodIdSearch.selectField(PodIdSearch.entity().getId());
61+
PodIdSearch.and("dataCenterId", PodIdSearch.entity().getDataCenterId(), Op.EQ);
62+
PodIdSearch.and("allocationState", PodIdSearch.entity().getAllocationState(), Op.EQ);
63+
PodIdSearch.done();
5764
}
5865

5966
@Override
@@ -118,17 +125,16 @@ public boolean remove(Long id) {
118125

119126
@Override
120127
public List<Long> listDisabledPods(long zoneId) {
121-
GenericSearchBuilder<HostPodVO, Long> podIdSearch = createSearchBuilder(Long.class);
122-
podIdSearch.selectField(podIdSearch.entity().getId());
123-
podIdSearch.and("dataCenterId", podIdSearch.entity().getDataCenterId(), Op.EQ);
124-
podIdSearch.and("allocationState", podIdSearch.entity().getAllocationState(), Op.EQ);
125-
podIdSearch.done();
126-
127-
128-
SearchCriteria<Long> sc = podIdSearch.create();
128+
SearchCriteria<Long> sc = PodIdSearch.create();
129129
sc.addAnd("dataCenterId", SearchCriteria.Op.EQ, zoneId);
130130
sc.addAnd("allocationState", SearchCriteria.Op.EQ, Grouping.AllocationState.Disabled);
131131
return customSearch(sc, null);
132132
}
133133

134+
@Override
135+
public List<Long> listAllPods(long zoneId) {
136+
SearchCriteria<Long> sc = PodIdSearch.create();
137+
sc.addAnd("dataCenterId", SearchCriteria.Op.EQ, zoneId);
138+
return customSearch(sc, null);
139+
}
134140
}

engine/schema/src/com/cloud/host/dao/HostDao.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public interface HostDao extends GenericDao<HostVO, Long>, StateDao<Status, Stat
4343
*/
4444
void markHostsAsDisconnected(long msId, long lastPing);
4545

46-
List<HostVO> findLostHosts(long timeout);
46+
List<HostVO> findLostHosts(long timeout);
4747

4848
List<HostVO> findAndUpdateDirectAgentToLoad(long lastPingSecondsAfter, Long limit, long managementServerId);
4949

@@ -61,15 +61,14 @@ public interface HostDao extends GenericDao<HostVO, Long>, StateDao<Status, Stat
6161

6262
long countRoutingHostsByDataCenter(long dcId);
6363

64-
List<HostVO> findAndUpdateApplianceToLoad(long lastPingSecondsAfter, long managementServerId);
64+
List<HostVO> findAndUpdateApplianceToLoad(long lastPingSecondsAfter, long managementServerId);
6565

6666
boolean updateResourceState(ResourceState oldState, ResourceState.Event event, ResourceState newState, Host vo);
6767

68-
HostVO findByGuid(String guid);
69-
70-
HostVO findByTypeNameAndZoneId(long zoneId, String name, Host.Type type);
71-
List<HostVO> findHypervisorHostInCluster(long clusterId);
68+
HostVO findByGuid(String guid);
7269

70+
HostVO findByTypeNameAndZoneId(long zoneId, String name, Host.Type type);
71+
List<HostVO> findHypervisorHostInCluster(long clusterId);
7372

7473
/**
7574
* @param type
@@ -86,4 +85,6 @@ public interface HostDao extends GenericDao<HostVO, Long>, StateDao<Status, Stat
8685
List<HostVO> findByClusterId(Long clusterId);
8786

8887
List<HostVO> listByDataCenterId(long id);
88+
89+
List<Long> listAllHosts(long zoneId);
8990
}

engine/schema/src/com/cloud/host/dao/HostDaoImpl.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ public class HostDaoImpl extends GenericDaoBase<HostVO, Long> implements HostDao
105105
protected SearchBuilder<HostVO> ManagedRoutingServersSearch;
106106
protected SearchBuilder<HostVO> SecondaryStorageVMSearch;
107107

108-
108+
protected GenericSearchBuilder<HostVO, Long> HostIdSearch;
109109
protected GenericSearchBuilder<HostVO, Long> HostsInStatusSearch;
110110
protected GenericSearchBuilder<HostVO, Long> CountRoutingByDc;
111111
protected SearchBuilder<HostTransferMapVO> HostTransferSearch;
@@ -319,7 +319,6 @@ public void init() {
319319
CountRoutingByDc.and("dc", CountRoutingByDc.entity().getDataCenterId(), SearchCriteria.Op.EQ);
320320
CountRoutingByDc.and("type", CountRoutingByDc.entity().getType(), SearchCriteria.Op.EQ);
321321
CountRoutingByDc.and("status", CountRoutingByDc.entity().getStatus(), SearchCriteria.Op.EQ);
322-
323322
CountRoutingByDc.done();
324323

325324
ManagedDirectConnectSearch = createSearchBuilder();
@@ -370,6 +369,11 @@ public void init() {
370369
HostsInClusterSearch.and("server", HostsInClusterSearch.entity().getManagementServerId(), SearchCriteria.Op.NNULL);
371370
HostsInClusterSearch.done();
372371

372+
HostIdSearch = createSearchBuilder(Long.class);
373+
HostIdSearch.selectField(HostIdSearch.entity().getId());
374+
HostIdSearch.and("dataCenterId", HostIdSearch.entity().getDataCenterId(), Op.EQ);
375+
HostIdSearch.done();
376+
373377
_statusAttr = _allAttributes.get("status");
374378
_msIdAttr = _allAttributes.get("managementServerId");
375379
_pingTimeAttr = _allAttributes.get("lastPinged");
@@ -1027,4 +1031,10 @@ public List<HostVO> findHypervisorHostInCluster(long clusterId) {
10271031
return listBy(sc);
10281032
}
10291033

1034+
@Override
1035+
public List<Long> listAllHosts(long zoneId) {
1036+
SearchCriteria<Long> sc = HostIdSearch.create();
1037+
sc.addAnd("dataCenterId", SearchCriteria.Op.EQ, zoneId);
1038+
return customSearch(sc, null);
1039+
}
10301040
}

server/src/com/cloud/dc/dao/DedicatedResourceDao.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,10 @@ public interface DedicatedResourceDao extends GenericDao<DedicatedResourceVO, Lo
4646
List<DedicatedResourceVO> listByDomainId(Long domainId);
4747

4848
List<DedicatedResourceVO> listZonesNotInDomainIds(List<Long> domainIds);
49+
50+
List<Long> listAllPods();
51+
52+
List<Long> listAllClusters();
53+
54+
List<Long> listAllHosts();
4955
}

server/src/com/cloud/dc/dao/DedicatedResourceDaoImpl.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,14 @@
2323
import org.springframework.stereotype.Component;
2424

2525
import com.cloud.dc.DedicatedResourceVO;
26+
import com.cloud.dc.HostPodVO;
2627
import com.cloud.utils.Pair;
2728
import com.cloud.utils.db.DB;
2829
import com.cloud.utils.db.GenericDaoBase;
30+
import com.cloud.utils.db.GenericSearchBuilder;
2931
import com.cloud.utils.db.SearchBuilder;
3032
import com.cloud.utils.db.SearchCriteria;
33+
import com.cloud.utils.db.SearchCriteria.Func;
3134
import com.cloud.utils.db.SearchCriteria.Op;
3235
import com.cloud.utils.db.Transaction;
3336

@@ -59,6 +62,10 @@ public class DedicatedResourceDaoImpl extends GenericDaoBase<DedicatedResourceVO
5962

6063
protected SearchBuilder<DedicatedResourceVO> ZoneByDomainIdsSearch;
6164

65+
protected GenericSearchBuilder<DedicatedResourceVO, Long> ListPodsSearch;
66+
protected GenericSearchBuilder<DedicatedResourceVO, Long> ListClustersSearch;
67+
protected GenericSearchBuilder<DedicatedResourceVO, Long> ListHostsSearch;
68+
6269
protected DedicatedResourceDaoImpl() {
6370
PodSearch = createSearchBuilder();
6471
PodSearch.and("podId", PodSearch.entity().getPodId(), SearchCriteria.Op.EQ);
@@ -169,6 +176,21 @@ protected DedicatedResourceDaoImpl() {
169176
ZoneByDomainIdsSearch.and("zoneId", ZoneByDomainIdsSearch.entity().getDataCenterId(), SearchCriteria.Op.NNULL);
170177
ZoneByDomainIdsSearch.and("domainId", ZoneByDomainIdsSearch.entity().getDomainId(), SearchCriteria.Op.NIN);
171178
ZoneByDomainIdsSearch.done();
179+
180+
ListPodsSearch = createSearchBuilder(Long.class);
181+
ListPodsSearch.select(null, Func.DISTINCT, ListPodsSearch.entity().getPodId());
182+
ListPodsSearch.and("podId", ListPodsSearch.entity().getPodId(), Op.NNULL);
183+
ListPodsSearch.done();
184+
185+
ListClustersSearch = createSearchBuilder(Long.class);
186+
ListClustersSearch.select(null, Func.DISTINCT, ListClustersSearch.entity().getClusterId());
187+
ListClustersSearch.and("clusterId", ListClustersSearch.entity().getClusterId(), Op.NNULL);
188+
ListClustersSearch.done();
189+
190+
ListHostsSearch = createSearchBuilder(Long.class);
191+
ListHostsSearch.select(null, Func.DISTINCT, ListHostsSearch.entity().getHostId());
192+
ListHostsSearch.and("hostId", ListHostsSearch.entity().getHostId(), Op.NNULL);
193+
ListHostsSearch.done();
172194
}
173195

174196
@Override
@@ -301,4 +323,22 @@ public boolean remove(Long id) {
301323
txn.commit();
302324
return result;
303325
}
326+
327+
@Override
328+
public List<Long> listAllPods() {
329+
SearchCriteria<Long> sc = ListPodsSearch.create();
330+
return customSearch(sc, null);
331+
}
332+
333+
@Override
334+
public List<Long> listAllClusters() {
335+
SearchCriteria<Long> sc = ListClustersSearch.create();
336+
return customSearch(sc, null);
337+
}
338+
339+
@Override
340+
public List<Long> listAllHosts() {
341+
SearchCriteria<Long> sc = ListHostsSearch.create();
342+
return customSearch(sc, null);
343+
}
304344
}

server/src/com/cloud/deploy/DeploymentPlanningManagerImpl.java

Lines changed: 15 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,8 @@
4444
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
4545
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
4646
import org.apache.cloudstack.utils.identity.ManagementServerNode;
47-
4847
import org.apache.log4j.Logger;
4948

50-
51-
52-
53-
54-
5549
import com.cloud.capacity.CapacityManager;
5650
import com.cloud.capacity.dao.CapacityDao;
5751
import com.cloud.configuration.Config;
@@ -61,7 +55,6 @@
6155
import com.cloud.dc.DataCenter;
6256
import com.cloud.dc.DataCenterVO;
6357
import com.cloud.dc.DedicatedResourceVO;
64-
import com.cloud.dc.HostPodVO;
6558
import com.cloud.dc.Pod;
6659
import com.cloud.dc.dao.ClusterDao;
6760
import com.cloud.dc.dao.DataCenterDao;
@@ -100,8 +93,6 @@
10093
import com.cloud.utils.component.Manager;
10194
import com.cloud.utils.component.ManagerBase;
10295
import com.cloud.utils.db.DB;
103-
import com.cloud.utils.db.JoinBuilder;
104-
import com.cloud.utils.db.SearchBuilder;
10596
import com.cloud.utils.db.SearchCriteria;
10697
import com.cloud.utils.db.Transaction;
10798
import com.cloud.utils.exception.CloudRuntimeException;
@@ -466,39 +457,22 @@ private void checkForNonDedicatedResources(VirtualMachineProfile vmProfile, Data
466457
DedicatedResourceVO dedicatedZone = _dedicatedDao.findByZoneId(dc.getId());
467458
if (dedicatedZone != null && dedicatedZone.getDomainId() != null) {
468459
throw new CloudRuntimeException("Failed to deploy VM. Zone " + dc.getName() + " is dedicated . Please use Explicit Dedication Affinity Group");
469-
}
470-
471-
List<HostPodVO> podsInDc = _podDao.listByDataCenterId(dc.getId());
472-
for (HostPodVO pod : podsInDc) {
473-
DedicatedResourceVO dedicatedPod = _dedicatedDao.findByPodId(pod.getId());
474-
if (dedicatedPod != null) {
475-
avoids.addPod(dedicatedPod.getPodId());
476-
if (s_logger.isDebugEnabled()) {
477-
s_logger.debug("Cannot use this dedicated pod " + pod.getName() + ".");
478-
}
479-
}
480-
}
460+
}
481461

482-
List<ClusterVO> clusterInDc = _clusterDao.listClustersByDcId(dc.getId());
483-
for (ClusterVO cluster : clusterInDc) {
484-
DedicatedResourceVO dedicatedCluster = _dedicatedDao.findByClusterId(cluster.getId());
485-
if (dedicatedCluster != null) {
486-
avoids.addCluster(dedicatedCluster.getClusterId());
487-
if (s_logger.isDebugEnabled()) {
488-
s_logger.debug("Cannot use this dedicated Cluster " + cluster.getName() + ".");
489-
}
490-
}
491-
}
492-
List<HostVO> hostInDc = _hostDao.listByDataCenterId(dc.getId());
493-
for (HostVO host : hostInDc) {
494-
DedicatedResourceVO dedicatedHost = _dedicatedDao.findByHostId(host.getId());
495-
if (dedicatedHost != null) {
496-
avoids.addHost(dedicatedHost.getHostId());
497-
if (s_logger.isDebugEnabled()) {
498-
s_logger.debug("Cannot use this dedicated host " + host.getName() + ".");
499-
}
500-
}
501-
}
462+
List<Long> allPodsInDc = _podDao.listAllPods(dc.getId());
463+
List<Long> allDedicatedPods = _dedicatedDao.listAllPods();
464+
allPodsInDc.retainAll(allDedicatedPods);
465+
avoids.addPodList(allPodsInDc);
466+
467+
List<Long> allClustersInDc = _clusterDao.listAllCusters(dc.getId());
468+
List<Long> allDedicatedClusters = _dedicatedDao.listAllClusters();
469+
allClustersInDc.retainAll(allDedicatedClusters);
470+
avoids.addClusterList(allClustersInDc);
471+
472+
List<Long> allHostsInDc = _hostDao.listAllHosts(dc.getId());
473+
List<Long> allDedicatedHosts = _dedicatedDao.listAllHosts();
474+
allHostsInDc.retainAll(allDedicatedHosts);
475+
avoids.addHostList(allHostsInDc);
502476
}
503477
}
504478

0 commit comments

Comments
 (0)