Skip to content

Commit a3b439c

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

File tree

5 files changed

+109
-82
lines changed

5 files changed

+109
-82
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
@@ -1887,18 +1887,10 @@ public static HostResponse newHostResponse(HostJoinVO vr, EnumSet<HostDetails> d
18871887
return s_hostJoinDao.newHostResponse(vr, details);
18881888
}
18891889

1890-
public static HostResponse fillHostDetails(HostResponse vrData, HostJoinVO vr) {
1891-
return s_hostJoinDao.setHostResponse(vrData, vr);
1892-
}
1893-
18941890
public static HostForMigrationResponse newHostForMigrationResponse(HostJoinVO vr, EnumSet<HostDetails> details) {
18951891
return s_hostJoinDao.newHostForMigrationResponse(vr, details);
18961892
}
18971893

1898-
public static HostForMigrationResponse fillHostForMigrationDetails(HostForMigrationResponse vrData, HostJoinVO vr) {
1899-
return s_hostJoinDao.setHostForMigrationResponse(vrData, vr);
1900-
}
1901-
19021894
public static List<HostJoinVO> newHostView(Host vr) {
19031895
return s_hostJoinDao.newHostView(vr);
19041896
}

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: 15 additions & 54 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;
@@ -93,6 +94,18 @@ protected HostJoinDaoImpl() {
9394
this._count = "select count(distinct id) from host_view WHERE ";
9495
}
9596

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+
96109
@Override
97110
public HostResponse newHostResponse(HostJoinVO host, EnumSet<HostDetails> details) {
98111
HostResponse hostResponse = new HostResponse();
@@ -178,13 +191,7 @@ public HostResponse newHostResponse(HostJoinVO host, EnumSet<HostDetails> detail
178191

179192
String hostTags = host.getTag();
180193
hostResponse.setHostTags(hostTags);
181-
182-
hostResponse.setHaHost(false);
183-
String haTag = ApiDBUtils.getHaTag();
184-
if (StringUtils.isNotEmpty(haTag) && StringUtils.isNotEmpty(hostTags) &&
185-
haTag.equalsIgnoreCase(hostTags)) {
186-
hostResponse.setHaHost(true);
187-
}
194+
hostResponse.setHaHost(containsHostHATag(hostTags));
188195

189196
hostResponse.setHypervisorVersion(host.getHypervisorVersion());
190197

@@ -266,26 +273,6 @@ public HostResponse newHostResponse(HostJoinVO host, EnumSet<HostDetails> detail
266273
return hostResponse;
267274
}
268275

269-
@Override
270-
public HostResponse setHostResponse(HostResponse response, HostJoinVO host) {
271-
String tag = host.getTag();
272-
if (StringUtils.isNotEmpty(tag)) {
273-
if (StringUtils.isNotEmpty(response.getHostTags())) {
274-
response.setHostTags(response.getHostTags() + "," + tag);
275-
} else {
276-
response.setHostTags(tag);
277-
}
278-
279-
if (Boolean.FALSE.equals(response.getHaHost())) {
280-
String haTag = ApiDBUtils.getHaTag();
281-
if (StringUtils.isNotEmpty(haTag) && haTag.equalsIgnoreCase(tag)) {
282-
response.setHaHost(true);
283-
}
284-
}
285-
}
286-
return response;
287-
}
288-
289276
@Override
290277
public HostForMigrationResponse newHostForMigrationResponse(HostJoinVO host, EnumSet<HostDetails> details) {
291278
HostForMigrationResponse hostResponse = new HostForMigrationResponse();
@@ -337,13 +324,7 @@ public HostForMigrationResponse newHostForMigrationResponse(HostJoinVO host, Enu
337324

338325
String hostTags = host.getTag();
339326
hostResponse.setHostTags(hostTags);
340-
341-
hostResponse.setHaHost(false);
342-
String haTag = ApiDBUtils.getHaTag();
343-
if (StringUtils.isNotEmpty(haTag) && StringUtils.isNotEmpty(hostTags) &&
344-
haTag.equalsIgnoreCase(hostTags)) {
345-
hostResponse.setHaHost(true);
346-
}
327+
hostResponse.setHaHost(containsHostHATag(hostTags));
347328

348329
hostResponse.setHypervisorVersion(host.getHypervisorVersion());
349330

@@ -408,26 +389,6 @@ public HostForMigrationResponse newHostForMigrationResponse(HostJoinVO host, Enu
408389
return hostResponse;
409390
}
410391

411-
@Override
412-
public HostForMigrationResponse setHostForMigrationResponse(HostForMigrationResponse response, HostJoinVO host) {
413-
String tag = host.getTag();
414-
if (tag != null) {
415-
if (response.getHostTags() != null && response.getHostTags().length() > 0) {
416-
response.setHostTags(response.getHostTags() + "," + tag);
417-
} else {
418-
response.setHostTags(tag);
419-
}
420-
421-
if (Boolean.FALSE.equals(response.getHaHost())) {
422-
String haTag = ApiDBUtils.getHaTag();
423-
if (StringUtils.isNotEmpty(haTag) && haTag.equalsIgnoreCase(tag)) {
424-
response.setHaHost(true);
425-
}
426-
}
427-
}
428-
return response;
429-
}
430-
431392
@Override
432393
public List<HostJoinVO> newHostView(Host host) {
433394
SearchCriteria<HostJoinVO> sc = hostIdSearch.create();

0 commit comments

Comments
 (0)