Skip to content

Commit 5fd970d

Browse files
authored
db, server: refactor host_view to prevent duplicate entries (#4796)
Signed-off-by: Abhishek Kumar <abhishek.mrt22@gmail.com>
1 parent 0d72886 commit 5fd970d

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-41510to41600.sql

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -419,3 +419,95 @@ DROP TABLE IF EXISTS `cloud`.`swift`;
419419
ALTER TABLE `cloud`.`snapshots` DROP FOREIGN KEY `fk_snapshots__s3_id` ;
420420
ALTER TABLE `cloud`.`snapshots` DROP COLUMN `s3_id` ;
421421
DROP TABLE IF EXISTS `cloud`.`s3`;
422+
423+
-- Re-create host view to prevent multiple entries for hosts with multiple tags
424+
DROP VIEW IF EXISTS `cloud`.`host_view`;
425+
CREATE VIEW `cloud`.`host_view` AS
426+
SELECT
427+
host.id,
428+
host.uuid,
429+
host.name,
430+
host.status,
431+
host.disconnected,
432+
host.type,
433+
host.private_ip_address,
434+
host.version,
435+
host.hypervisor_type,
436+
host.hypervisor_version,
437+
host.capabilities,
438+
host.last_ping,
439+
host.created,
440+
host.removed,
441+
host.resource_state,
442+
host.mgmt_server_id,
443+
host.cpu_sockets,
444+
host.cpus,
445+
host.speed,
446+
host.ram,
447+
cluster.id cluster_id,
448+
cluster.uuid cluster_uuid,
449+
cluster.name cluster_name,
450+
cluster.cluster_type,
451+
data_center.id data_center_id,
452+
data_center.uuid data_center_uuid,
453+
data_center.name data_center_name,
454+
data_center.networktype data_center_type,
455+
host_pod_ref.id pod_id,
456+
host_pod_ref.uuid pod_uuid,
457+
host_pod_ref.name pod_name,
458+
GROUP_CONCAT(DISTINCT(host_tags.tag)) AS tag,
459+
guest_os_category.id guest_os_category_id,
460+
guest_os_category.uuid guest_os_category_uuid,
461+
guest_os_category.name guest_os_category_name,
462+
mem_caps.used_capacity memory_used_capacity,
463+
mem_caps.reserved_capacity memory_reserved_capacity,
464+
cpu_caps.used_capacity cpu_used_capacity,
465+
cpu_caps.reserved_capacity cpu_reserved_capacity,
466+
async_job.id job_id,
467+
async_job.uuid job_uuid,
468+
async_job.job_status job_status,
469+
async_job.account_id job_account_id,
470+
oobm.enabled AS `oobm_enabled`,
471+
oobm.power_state AS `oobm_power_state`,
472+
ha_config.enabled AS `ha_enabled`,
473+
ha_config.ha_state AS `ha_state`,
474+
ha_config.provider AS `ha_provider`,
475+
`last_annotation_view`.`annotation` AS `annotation`,
476+
`last_annotation_view`.`created` AS `last_annotated`,
477+
`user`.`username` AS `username`
478+
FROM
479+
`cloud`.`host`
480+
LEFT JOIN
481+
`cloud`.`cluster` ON host.cluster_id = cluster.id
482+
LEFT JOIN
483+
`cloud`.`data_center` ON host.data_center_id = data_center.id
484+
LEFT JOIN
485+
`cloud`.`host_pod_ref` ON host.pod_id = host_pod_ref.id
486+
LEFT JOIN
487+
`cloud`.`host_details` ON host.id = host_details.host_id
488+
AND host_details.name = 'guest.os.category.id'
489+
LEFT JOIN
490+
`cloud`.`guest_os_category` ON guest_os_category.id = CONVERT ( host_details.value, UNSIGNED )
491+
LEFT JOIN
492+
`cloud`.`host_tags` ON host_tags.host_id = host.id
493+
LEFT JOIN
494+
`cloud`.`op_host_capacity` mem_caps ON host.id = mem_caps.host_id
495+
AND mem_caps.capacity_type = 0
496+
LEFT JOIN
497+
`cloud`.`op_host_capacity` cpu_caps ON host.id = cpu_caps.host_id
498+
AND cpu_caps.capacity_type = 1
499+
LEFT JOIN
500+
`cloud`.`async_job` ON async_job.instance_id = host.id
501+
AND async_job.instance_type = 'Host'
502+
AND async_job.job_status = 0
503+
LEFT JOIN
504+
`cloud`.`oobm` ON oobm.host_id = host.id
505+
left join
506+
`cloud`.`ha_config` ON ha_config.resource_id=host.id
507+
and ha_config.resource_type='Host'
508+
LEFT JOIN
509+
`cloud`.`last_annotation_view` ON `last_annotation_view`.`entity_uuid` = `host`.`uuid`
510+
LEFT JOIN
511+
`cloud`.`user` ON `user`.`uuid` = `last_annotation_view`.`user_uuid`
512+
GROUP BY
513+
`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
@@ -1891,18 +1891,10 @@ public static HostResponse newHostResponse(HostJoinVO vr, EnumSet<HostDetails> d
18911891
return s_hostJoinDao.newHostResponse(vr, details);
18921892
}
18931893

1894-
public static HostResponse fillHostDetails(HostResponse vrData, HostJoinVO vr) {
1895-
return s_hostJoinDao.setHostResponse(vrData, vr);
1896-
}
1897-
18981894
public static HostForMigrationResponse newHostForMigrationResponse(HostJoinVO vr, EnumSet<HostDetails> details) {
18991895
return s_hostJoinDao.newHostForMigrationResponse(vr, details);
19001896
}
19011897

1902-
public static HostForMigrationResponse fillHostForMigrationDetails(HostForMigrationResponse vrData, HostJoinVO vr) {
1903-
return s_hostJoinDao.setHostForMigrationResponse(vrData, vr);
1904-
}
1905-
19061898
public static List<HostJoinVO> newHostView(Host vr) {
19071899
return s_hostJoinDao.newHostView(vr);
19081900
}

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();
@@ -180,13 +193,7 @@ public HostResponse newHostResponse(HostJoinVO host, EnumSet<HostDetails> detail
180193

181194
String hostTags = host.getTag();
182195
hostResponse.setHostTags(hostTags);
183-
184-
hostResponse.setHaHost(false);
185-
String haTag = ApiDBUtils.getHaTag();
186-
if (StringUtils.isNotEmpty(haTag) && StringUtils.isNotEmpty(hostTags) &&
187-
haTag.equalsIgnoreCase(hostTags)) {
188-
hostResponse.setHaHost(true);
189-
}
196+
hostResponse.setHaHost(containsHostHATag(hostTags));
190197

191198
hostResponse.setHypervisorVersion(host.getHypervisorVersion());
192199

@@ -268,26 +275,6 @@ public HostResponse newHostResponse(HostJoinVO host, EnumSet<HostDetails> detail
268275
return hostResponse;
269276
}
270277

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

340327
String hostTags = host.getTag();
341328
hostResponse.setHostTags(hostTags);
342-
343-
hostResponse.setHaHost(false);
344-
String haTag = ApiDBUtils.getHaTag();
345-
if (StringUtils.isNotEmpty(haTag) && StringUtils.isNotEmpty(hostTags) &&
346-
haTag.equalsIgnoreCase(hostTags)) {
347-
hostResponse.setHaHost(true);
348-
}
329+
hostResponse.setHaHost(containsHostHATag(hostTags));
349330

350331
hostResponse.setHypervisorVersion(host.getHypervisorVersion());
351332

@@ -410,26 +391,6 @@ public HostForMigrationResponse newHostForMigrationResponse(HostJoinVO host, Enu
410391
return hostResponse;
411392
}
412393

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-
if (Boolean.FALSE.equals(response.getHaHost())) {
424-
String haTag = ApiDBUtils.getHaTag();
425-
if (StringUtils.isNotEmpty(haTag) && haTag.equalsIgnoreCase(tag)) {
426-
response.setHaHost(true);
427-
}
428-
}
429-
}
430-
return response;
431-
}
432-
433394
@Override
434395
public List<HostJoinVO> newHostView(Host host) {
435396
SearchCriteria<HostJoinVO> sc = hostIdSearch.create();

0 commit comments

Comments
 (0)