Skip to content

Commit edecd7d

Browse files
committed
db, server: refactor host_view to prevent duplicate entries
Signed-off-by: Abhishek Kumar <[email protected]>
1 parent 3f84b9a commit edecd7d

File tree

5 files changed

+116
-84
lines changed

5 files changed

+116
-84
lines changed

engine/schema/src/main/resources/META-INF/db/schema-41500to41510.sql

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,95 @@
2323
UPDATE `cloud`.`guest_os` SET display_name='Fedora Linux (32 bit)' WHERE id=320;
2424
UPDATE `cloud`.`guest_os` SET display_name='Mandriva Linux (32 bit)' WHERE id=323;
2525
UPDATE `cloud`.`guest_os` SET display_name='OpenSUSE Linux (32 bit)' WHERE id=327;
26+
27+
-- Re-create host view to prevent multiple entries for hosts with multiple tags
28+
DROP VIEW IF EXISTS `cloud`.`host_view`;
29+
CREATE VIEW `cloud`.`host_view` AS
30+
SELECT
31+
host.id,
32+
host.uuid,
33+
host.name,
34+
host.status,
35+
host.disconnected,
36+
host.type,
37+
host.private_ip_address,
38+
host.version,
39+
host.hypervisor_type,
40+
host.hypervisor_version,
41+
host.capabilities,
42+
host.last_ping,
43+
host.created,
44+
host.removed,
45+
host.resource_state,
46+
host.mgmt_server_id,
47+
host.cpu_sockets,
48+
host.cpus,
49+
host.speed,
50+
host.ram,
51+
cluster.id cluster_id,
52+
cluster.uuid cluster_uuid,
53+
cluster.name cluster_name,
54+
cluster.cluster_type,
55+
data_center.id data_center_id,
56+
data_center.uuid data_center_uuid,
57+
data_center.name data_center_name,
58+
data_center.networktype data_center_type,
59+
host_pod_ref.id pod_id,
60+
host_pod_ref.uuid pod_uuid,
61+
host_pod_ref.name pod_name,
62+
GROUP_CONCAT(DISTINCT(host_tags.tag)) AS tag,
63+
guest_os_category.id guest_os_category_id,
64+
guest_os_category.uuid guest_os_category_uuid,
65+
guest_os_category.name guest_os_category_name,
66+
mem_caps.used_capacity memory_used_capacity,
67+
mem_caps.reserved_capacity memory_reserved_capacity,
68+
cpu_caps.used_capacity cpu_used_capacity,
69+
cpu_caps.reserved_capacity cpu_reserved_capacity,
70+
async_job.id job_id,
71+
async_job.uuid job_uuid,
72+
async_job.job_status job_status,
73+
async_job.account_id job_account_id,
74+
oobm.enabled AS `oobm_enabled`,
75+
oobm.power_state AS `oobm_power_state`,
76+
ha_config.enabled AS `ha_enabled`,
77+
ha_config.ha_state AS `ha_state`,
78+
ha_config.provider AS `ha_provider`,
79+
`last_annotation_view`.`annotation` AS `annotation`,
80+
`last_annotation_view`.`created` AS `last_annotated`,
81+
`user`.`username` AS `username`
82+
FROM
83+
`cloud`.`host`
84+
LEFT JOIN
85+
`cloud`.`cluster` ON host.cluster_id = cluster.id
86+
LEFT JOIN
87+
`cloud`.`data_center` ON host.data_center_id = data_center.id
88+
LEFT JOIN
89+
`cloud`.`host_pod_ref` ON host.pod_id = host_pod_ref.id
90+
LEFT JOIN
91+
`cloud`.`host_details` ON host.id = host_details.host_id
92+
AND host_details.name = 'guest.os.category.id'
93+
LEFT JOIN
94+
`cloud`.`guest_os_category` ON guest_os_category.id = CONVERT ( host_details.value, UNSIGNED )
95+
LEFT JOIN
96+
`cloud`.`host_tags` ON host_tags.host_id = host.id
97+
LEFT JOIN
98+
`cloud`.`op_host_capacity` mem_caps ON host.id = mem_caps.host_id
99+
AND mem_caps.capacity_type = 0
100+
LEFT JOIN
101+
`cloud`.`op_host_capacity` cpu_caps ON host.id = cpu_caps.host_id
102+
AND cpu_caps.capacity_type = 1
103+
LEFT JOIN
104+
`cloud`.`async_job` ON async_job.instance_id = host.id
105+
AND async_job.instance_type = 'Host'
106+
AND async_job.job_status = 0
107+
LEFT JOIN
108+
`cloud`.`oobm` ON oobm.host_id = host.id
109+
left join
110+
`cloud`.`ha_config` ON ha_config.resource_id=host.id
111+
and ha_config.resource_type='Host'
112+
LEFT JOIN
113+
`cloud`.`last_annotation_view` ON `last_annotation_view`.`entity_uuid` = `host`.`uuid`
114+
LEFT JOIN
115+
`cloud`.`user` ON `user`.`uuid` = `last_annotation_view`.`user_uuid`
116+
GROUP BY
117+
`host`.`id`;

server/src/main/java/com/cloud/api/ApiDBUtils.java

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1870,18 +1870,10 @@ public static HostResponse newHostResponse(HostJoinVO vr, EnumSet<HostDetails> d
18701870
return s_hostJoinDao.newHostResponse(vr, details);
18711871
}
18721872

1873-
public static HostResponse fillHostDetails(HostResponse vrData, HostJoinVO vr) {
1874-
return s_hostJoinDao.setHostResponse(vrData, vr);
1875-
}
1876-
18771873
public static HostForMigrationResponse newHostForMigrationResponse(HostJoinVO vr, EnumSet<HostDetails> details) {
18781874
return s_hostJoinDao.newHostForMigrationResponse(vr, details);
18791875
}
18801876

1881-
public static HostForMigrationResponse fillHostForMigrationDetails(HostForMigrationResponse vrData, HostJoinVO vr) {
1882-
return s_hostJoinDao.setHostForMigrationResponse(vrData, vr);
1883-
}
1884-
18851877
public static List<HostJoinVO> newHostView(Host vr) {
18861878
return s_hostJoinDao.newHostView(vr);
18871879
}

server/src/main/java/com/cloud/api/query/ViewResponseHelper.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -242,14 +242,7 @@ public static List<HostResponse> createHostResponse(EnumSet<HostDetails> details
242242
Hashtable<Long, HostResponse> vrDataList = new Hashtable<Long, HostResponse>();
243243
// Initialise the vrdatalist with the input data
244244
for (HostJoinVO vr : hosts) {
245-
HostResponse vrData = vrDataList.get(vr.getId());
246-
if (vrData == null) {
247-
// first time encountering this vm
248-
vrData = ApiDBUtils.newHostResponse(vr, details);
249-
} else {
250-
// update tags
251-
vrData = ApiDBUtils.fillHostDetails(vrData, vr);
252-
}
245+
HostResponse vrData = ApiDBUtils.newHostResponse(vr, details);
253246
vrDataList.put(vr.getId(), vrData);
254247
}
255248
return new ArrayList<HostResponse>(vrDataList.values());
@@ -259,14 +252,7 @@ public static List<HostForMigrationResponse> createHostForMigrationResponse(Enum
259252
Hashtable<Long, HostForMigrationResponse> vrDataList = new Hashtable<Long, HostForMigrationResponse>();
260253
// Initialise the vrdatalist with the input data
261254
for (HostJoinVO vr : hosts) {
262-
HostForMigrationResponse vrData = vrDataList.get(vr.getId());
263-
if (vrData == null) {
264-
// first time encountering this vm
265-
vrData = ApiDBUtils.newHostForMigrationResponse(vr, details);
266-
} else {
267-
// update tags
268-
vrData = ApiDBUtils.fillHostForMigrationDetails(vrData, vr);
269-
}
255+
HostForMigrationResponse vrData = ApiDBUtils.newHostForMigrationResponse(vr, details);
270256
vrDataList.put(vr.getId(), vrData);
271257
}
272258
return new ArrayList<HostForMigrationResponse>(vrDataList.values());

server/src/main/java/com/cloud/api/query/dao/HostJoinDao.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,8 @@ public interface HostJoinDao extends GenericDao<HostJoinVO, Long> {
3131

3232
HostResponse newHostResponse(HostJoinVO host, EnumSet<HostDetails> details);
3333

34-
HostResponse setHostResponse(HostResponse response, HostJoinVO host);
35-
3634
HostForMigrationResponse newHostForMigrationResponse(HostJoinVO host, EnumSet<HostDetails> details);
3735

38-
HostForMigrationResponse setHostForMigrationResponse(HostForMigrationResponse response, HostJoinVO host);
39-
4036
List<HostJoinVO> newHostView(Host group);
4137

4238
List<HostJoinVO> searchByIds(Long... ids);

server/src/main/java/com/cloud/api/query/dao/HostJoinDaoImpl.java

Lines changed: 22 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.text.DecimalFormat;
2020
import java.util.ArrayList;
21+
import java.util.Arrays;
2122
import java.util.Date;
2223
import java.util.EnumSet;
2324
import java.util.Iterator;
@@ -26,16 +27,18 @@
2627

2728
import javax.inject.Inject;
2829

29-
import org.apache.log4j.Logger;
30-
import org.springframework.stereotype.Component;
31-
3230
import org.apache.cloudstack.api.ApiConstants.HostDetails;
3331
import org.apache.cloudstack.api.response.GpuResponse;
3432
import org.apache.cloudstack.api.response.HostForMigrationResponse;
3533
import org.apache.cloudstack.api.response.HostResponse;
3634
import org.apache.cloudstack.api.response.VgpuResponse;
3735
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
36+
import org.apache.cloudstack.ha.HAResource;
37+
import org.apache.cloudstack.ha.dao.HAConfigDao;
3838
import org.apache.cloudstack.outofbandmanagement.dao.OutOfBandManagementDao;
39+
import org.apache.commons.lang3.StringUtils;
40+
import org.apache.log4j.Logger;
41+
import org.springframework.stereotype.Component;
3942

4043
import com.cloud.api.ApiDBUtils;
4144
import com.cloud.api.query.vo.HostJoinVO;
@@ -52,9 +55,6 @@
5255
import com.cloud.utils.db.SearchBuilder;
5356
import com.cloud.utils.db.SearchCriteria;
5457

55-
import org.apache.cloudstack.ha.HAResource;
56-
import org.apache.cloudstack.ha.dao.HAConfigDao;
57-
5858
@Component
5959
public class HostJoinDaoImpl extends GenericDaoBase<HostJoinVO, Long> implements HostJoinDao {
6060
public static final Logger s_logger = Logger.getLogger(HostJoinDaoImpl.class);
@@ -94,6 +94,18 @@ protected HostJoinDaoImpl() {
9494
this._count = "select count(distinct id) from host_view WHERE ";
9595
}
9696

97+
private boolean containsHostHATag(final String tags) {
98+
boolean result = false;
99+
String haTag = ApiDBUtils.getHaTag();
100+
if (StringUtils.isNotEmpty(haTag) && StringUtils.isNotEmpty(tags)) {
101+
List<String> tagsList = Arrays.asList(tags.split(","));
102+
if (tagsList.contains(haTag)) {
103+
result = true;
104+
}
105+
}
106+
return result;
107+
}
108+
97109
@Override
98110
public HostResponse newHostResponse(HostJoinVO host, EnumSet<HostDetails> details) {
99111
HostResponse hostResponse = new HostResponse();
@@ -178,18 +190,8 @@ public HostResponse newHostResponse(HostJoinVO host, EnumSet<HostDetails> detail
178190
hostResponse.setMemoryAllocatedPercentage(memoryAllocatedPercentage);
179191

180192
String hostTags = host.getTag();
181-
hostResponse.setHostTags(host.getTag());
182-
183-
String haTag = ApiDBUtils.getHaTag();
184-
if (haTag != null && !haTag.isEmpty() && hostTags != null && !hostTags.isEmpty()) {
185-
if (haTag.equalsIgnoreCase(hostTags)) {
186-
hostResponse.setHaHost(true);
187-
} else {
188-
hostResponse.setHaHost(false);
189-
}
190-
} else {
191-
hostResponse.setHaHost(false);
192-
}
193+
hostResponse.setHostTags(hostTags);
194+
hostResponse.setHaHost(containsHostHATag(hostTags));
193195

194196
hostResponse.setHypervisorVersion(host.getHypervisorVersion());
195197

@@ -271,19 +273,6 @@ public HostResponse newHostResponse(HostJoinVO host, EnumSet<HostDetails> detail
271273
return hostResponse;
272274
}
273275

274-
@Override
275-
public HostResponse setHostResponse(HostResponse response, HostJoinVO host) {
276-
String tag = host.getTag();
277-
if (tag != null) {
278-
if (response.getHostTags() != null && response.getHostTags().length() > 0) {
279-
response.setHostTags(response.getHostTags() + "," + tag);
280-
} else {
281-
response.setHostTags(tag);
282-
}
283-
}
284-
return response;
285-
}
286-
287276
@Override
288277
public HostForMigrationResponse newHostForMigrationResponse(HostJoinVO host, EnumSet<HostDetails> details) {
289278
HostForMigrationResponse hostResponse = new HostForMigrationResponse();
@@ -334,18 +323,8 @@ public HostForMigrationResponse newHostForMigrationResponse(HostJoinVO host, Enu
334323
hostResponse.setMemoryAllocatedBytes(mem);
335324

336325
String hostTags = host.getTag();
337-
hostResponse.setHostTags(host.getTag());
338-
339-
String haTag = ApiDBUtils.getHaTag();
340-
if (haTag != null && !haTag.isEmpty() && hostTags != null && !hostTags.isEmpty()) {
341-
if (haTag.equalsIgnoreCase(hostTags)) {
342-
hostResponse.setHaHost(true);
343-
} else {
344-
hostResponse.setHaHost(false);
345-
}
346-
} else {
347-
hostResponse.setHaHost(false);
348-
}
326+
hostResponse.setHostTags(hostTags);
327+
hostResponse.setHaHost(containsHostHATag(hostTags));
349328

350329
hostResponse.setHypervisorVersion(host.getHypervisorVersion());
351330

@@ -410,19 +389,6 @@ public HostForMigrationResponse newHostForMigrationResponse(HostJoinVO host, Enu
410389
return hostResponse;
411390
}
412391

413-
@Override
414-
public HostForMigrationResponse setHostForMigrationResponse(HostForMigrationResponse response, HostJoinVO host) {
415-
String tag = host.getTag();
416-
if (tag != null) {
417-
if (response.getHostTags() != null && response.getHostTags().length() > 0) {
418-
response.setHostTags(response.getHostTags() + "," + tag);
419-
} else {
420-
response.setHostTags(tag);
421-
}
422-
}
423-
return response;
424-
}
425-
426392
@Override
427393
public List<HostJoinVO> newHostView(Host host) {
428394
SearchCriteria<HostJoinVO> sc = hostIdSearch.create();

0 commit comments

Comments
 (0)