Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 55 additions & 38 deletions engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
import com.cloud.org.Managed;
import com.cloud.resource.ResourceState;
import com.cloud.utils.DateUtil;
import com.cloud.utils.StringUtils;
import com.cloud.utils.db.Attribute;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.Filter;
Expand All @@ -83,13 +84,13 @@
private static final Logger status_logger = Logger.getLogger(Status.class);
private static final Logger state_logger = Logger.getLogger(ResourceState.class);

private static final String LIST_HOST_IDS_BY_COMPUTETAGS = "SELECT filtered.host_id, COUNT(filtered.tag) AS tag_count "
+ "FROM (SELECT host_id, tag, is_tag_a_rule FROM host_tags GROUP BY host_id,tag) AS filtered "
+ "WHERE tag IN(%s) AND is_tag_a_rule = 0 "
private static final String LIST_HOST_IDS_BY_HOST_TAGS = "SELECT filtered.host_id, COUNT(filtered.tag) AS tag_count "
+ "FROM (SELECT host_id, tag, is_tag_a_rule FROM host_tags GROUP BY host_id,tag,is_tag_a_rule) AS filtered "
+ "WHERE tag IN (%s) AND (is_tag_a_rule = 0 OR is_tag_a_rule IS NULL) "
+ "GROUP BY host_id "
+ "HAVING tag_count = %s ";
private static final String SEPARATOR = ",";
private static final String LIST_CLUSTERID_FOR_HOST_TAG = "select distinct cluster_id from host join ( %s ) AS selected_hosts ON host.id = selected_hosts.host_id";
private static final String LIST_CLUSTER_IDS_FOR_HOST_TAGS = "select distinct cluster_id from host join ( %s ) AS selected_hosts ON host.id = selected_hosts.host_id";
private static final String GET_HOSTS_OF_ACTIVE_VMS = "select h.id " +
"from vm_instance vm " +
"join host h on (vm.host_id=h.id) " +
Expand Down Expand Up @@ -785,6 +786,15 @@

@Override
public List<HostVO> listByHostTag(Host.Type type, Long clusterId, Long podId, long dcId, String hostTag) {
return listHostsWithOrWithoutHostTags(type, clusterId, podId, dcId, hostTag, true);
}

Check warning on line 790 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L789-L790

Added lines #L789 - L790 were not covered by tests

private List<HostVO> listHostsWithOrWithoutHostTags(Host.Type type, Long clusterId, Long podId, long dcId, String hostTags, boolean withHostTags) {

Check warning on line 792 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L792

Added line #L792 was not covered by tests
if (StringUtils.isEmpty(hostTags)) {
s_logger.debug("Host tags not specified, to list hosts");
return new ArrayList();

Check warning on line 795 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L794-L795

Added lines #L794 - L795 were not covered by tests
}

SearchBuilder<HostVO> hostSearch = createSearchBuilder();
HostVO entity = hostSearch.entity();
hostSearch.and("type", entity.getType(), SearchCriteria.Op.EQ);
Expand All @@ -795,7 +805,9 @@
hostSearch.and("resourceState", entity.getResourceState(), SearchCriteria.Op.EQ);

SearchCriteria<HostVO> sc = hostSearch.create();
sc.setParameters("type", type.toString());
if (type != null) {
sc.setParameters("type", type.toString());

Check warning on line 809 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L809

Added line #L809 was not covered by tests
}
if (podId != null) {
sc.setParameters("pod", podId);
}
Expand All @@ -806,27 +818,38 @@
sc.setParameters("status", Status.Up.toString());
sc.setParameters("resourceState", ResourceState.Enabled.toString());

List<HostVO> tmpHosts = listBy(sc);
List<HostVO> correctHostsByHostTags = new ArrayList();
List<Long> hostIdsByComputeOffTags = findHostByComputeOfferings(hostTag);
List<HostVO> upAndEnabledHosts = listBy(sc);

Check warning on line 821 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L821

Added line #L821 was not covered by tests
if (CollectionUtils.isEmpty(upAndEnabledHosts)) {
return new ArrayList();

Check warning on line 823 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L823

Added line #L823 was not covered by tests
}

tmpHosts.forEach((host) -> { if(hostIdsByComputeOffTags.contains(host.getId())) correctHostsByHostTags.add(host);});
List<Long> hostIdsByHostTags = findHostIdsByHostTags(hostTags);

Check warning on line 826 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L826

Added line #L826 was not covered by tests
if (CollectionUtils.isEmpty(hostIdsByHostTags)) {
return withHostTags ? new ArrayList() : upAndEnabledHosts;
}

return correctHostsByHostTags;
if (withHostTags) {
List<HostVO> upAndEnabledHostsWithHostTags = new ArrayList();

Check warning on line 832 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L832

Added line #L832 was not covered by tests
upAndEnabledHosts.forEach((host) -> { if (hostIdsByHostTags.contains(host.getId())) upAndEnabledHostsWithHostTags.add(host);});
return upAndEnabledHostsWithHostTags;

Check warning on line 834 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L834

Added line #L834 was not covered by tests
} else {
List<HostVO> upAndEnabledHostsWithoutHostTags = new ArrayList();

Check warning on line 836 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L836

Added line #L836 was not covered by tests
upAndEnabledHosts.forEach((host) -> { if (!hostIdsByHostTags.contains(host.getId())) upAndEnabledHostsWithoutHostTags.add(host);});
return upAndEnabledHostsWithoutHostTags;

Check warning on line 838 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L838

Added line #L838 was not covered by tests
}
}

@Override
public List<HostVO> listAllUpAndEnabledNonHAHosts(Type type, Long clusterId, Long podId, long dcId, String haTag) {
if (StringUtils.isNotEmpty(haTag)) {
return listHostsWithOrWithoutHostTags(type, clusterId, podId, dcId, haTag, false);

Check warning on line 845 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L845

Added line #L845 was not covered by tests
}

SearchBuilder<HostTagVO> hostTagSearch = _hostTagsDao.createSearchBuilder();
hostTagSearch.and();
hostTagSearch.op("isTagARule", hostTagSearch.entity().getIsTagARule(), Op.EQ);
hostTagSearch.or("tagDoesNotExist", hostTagSearch.entity().getIsTagARule(), Op.NULL);
hostTagSearch.cp();
if (haTag != null && !haTag.isEmpty()) {
hostTagSearch.and().op("tag", hostTagSearch.entity().getTag(), SearchCriteria.Op.NEQ);
hostTagSearch.or("tagNull", hostTagSearch.entity().getTag(), SearchCriteria.Op.NULL);
hostTagSearch.cp();
}

SearchBuilder<HostVO> hostSearch = createSearchBuilder();

Expand All @@ -837,18 +860,12 @@
hostSearch.and("status", hostSearch.entity().getStatus(), SearchCriteria.Op.EQ);
hostSearch.and("resourceState", hostSearch.entity().getResourceState(), SearchCriteria.Op.EQ);


hostSearch.join("hostTagSearch", hostTagSearch, hostSearch.entity().getId(), hostTagSearch.entity().getHostId(), JoinBuilder.JoinType.LEFTOUTER);


SearchCriteria<HostVO> sc = hostSearch.create();

sc.setJoinParameters("hostTagSearch", "isTagARule", false);

if (haTag != null && !haTag.isEmpty()) {
sc.setJoinParameters("hostTagSearch", "tag", haTag);
}

if (type != null) {
sc.setParameters("type", type);
}
Expand Down Expand Up @@ -1300,19 +1317,19 @@
}

@Override
public List<Long> listClustersByHostTag(String computeOfferingTags) {
public List<Long> listClustersByHostTag(String hostTags) {

Check warning on line 1320 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L1320

Added line #L1320 was not covered by tests
TransactionLegacy txn = TransactionLegacy.currentTxn();
String sql = this.LIST_CLUSTERID_FOR_HOST_TAG;
String selectStmtToListClusterIdsByHostTags = this.LIST_CLUSTER_IDS_FOR_HOST_TAGS;

Check warning on line 1322 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L1322

Added line #L1322 was not covered by tests
PreparedStatement pstmt = null;
List<Long> result = new ArrayList();
List<String> tags = Arrays.asList(computeOfferingTags.split(this.SEPARATOR));
String subselect = getHostIdsByComputeTags(tags);
sql = String.format(sql, subselect);
List<String> tags = Arrays.asList(hostTags.split(this.SEPARATOR));
String selectStmtToListHostIdsByHostTags = getSelectStmtToListHostIdsByHostTags(tags);
selectStmtToListClusterIdsByHostTags = String.format(selectStmtToListClusterIdsByHostTags, selectStmtToListHostIdsByHostTags);

Check warning on line 1327 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L1325-L1327

Added lines #L1325 - L1327 were not covered by tests

try {
pstmt = txn.prepareStatement(sql);
pstmt = txn.prepareStatement(selectStmtToListClusterIdsByHostTags);

Check warning on line 1330 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L1330

Added line #L1330 was not covered by tests

for(int i = 0; i < tags.size(); i++){
for (int i = 0; i < tags.size(); i++){
pstmt.setString(i+1, tags.get(i));
}

Expand All @@ -1323,20 +1340,20 @@
pstmt.close();
return result;
} catch (SQLException e) {
throw new CloudRuntimeException("DB Exception on: " + sql, e);
throw new CloudRuntimeException("DB Exception on: " + selectStmtToListClusterIdsByHostTags, e);

Check warning on line 1343 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L1343

Added line #L1343 was not covered by tests
}
}

private List<Long> findHostByComputeOfferings(String computeOfferingTags){
private List<Long> findHostIdsByHostTags(String hostTags){

Check warning on line 1347 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L1347

Added line #L1347 was not covered by tests
TransactionLegacy txn = TransactionLegacy.currentTxn();
PreparedStatement pstmt = null;
List<Long> result = new ArrayList();
List<String> tags = Arrays.asList(computeOfferingTags.split(this.SEPARATOR));
String select = getHostIdsByComputeTags(tags);
List<String> tags = Arrays.asList(hostTags.split(this.SEPARATOR));
String selectStmtToListHostIdsByHostTags = getSelectStmtToListHostIdsByHostTags(tags);

Check warning on line 1352 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L1351-L1352

Added lines #L1351 - L1352 were not covered by tests
try {
pstmt = txn.prepareStatement(select);
pstmt = txn.prepareStatement(selectStmtToListHostIdsByHostTags);

Check warning on line 1354 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L1354

Added line #L1354 was not covered by tests

for(int i = 0; i < tags.size(); i++){
for (int i = 0; i < tags.size(); i++){
pstmt.setString(i+1, tags.get(i));
}

Expand All @@ -1347,7 +1364,7 @@
pstmt.close();
return result;
} catch (SQLException e) {
throw new CloudRuntimeException("DB Exception on: " + select, e);
throw new CloudRuntimeException("DB Exception on: " + selectStmtToListHostIdsByHostTags, e);

Check warning on line 1367 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L1367

Added line #L1367 was not covered by tests
}
}

Expand All @@ -1372,10 +1389,10 @@
return new ArrayList<>(result);
}

private String getHostIdsByComputeTags(List<String> offeringTags){
private String getSelectStmtToListHostIdsByHostTags(List<String> hostTags){

Check warning on line 1392 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L1392

Added line #L1392 was not covered by tests
List<String> questionMarks = new ArrayList();
offeringTags.forEach((tag) -> { questionMarks.add("?"); });
return String.format(this.LIST_HOST_IDS_BY_COMPUTETAGS, String.join(",", questionMarks),questionMarks.size());
hostTags.forEach((tag) -> { questionMarks.add("?"); });
return String.format(this.LIST_HOST_IDS_BY_HOST_TAGS, String.join(",", questionMarks), questionMarks.size());

Check warning on line 1395 in engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java

View check run for this annotation

Codecov / codecov/patch

engine/schema/src/main/java/com/cloud/host/dao/HostDaoImpl.java#L1394-L1395

Added lines #L1394 - L1395 were not covered by tests
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1919,7 +1919,7 @@ private Pair<List<HostVO>, Integer> searchForServers(final Long startIndex, fina

final String haTag = _haMgr.getHaTag();
SearchBuilder<HostTagVO> hostTagSearch = null;
if (haHosts != null && haTag != null && !haTag.isEmpty()) {
if (haHosts != null && StringUtils.isNotEmpty(haTag)) {
hostTagSearch = _hostTagsDao.createSearchBuilder();
if ((Boolean)haHosts) {
hostTagSearch.and().op("tag", hostTagSearch.entity().getTag(), SearchCriteria.Op.EQ);
Expand Down Expand Up @@ -1980,7 +1980,7 @@ private Pair<List<HostVO>, Integer> searchForServers(final Long startIndex, fina
sc.setParameters("resourceState", resourceState);
}

if (haHosts != null && haTag != null && !haTag.isEmpty()) {
if (haHosts != null && StringUtils.isNotEmpty(haTag)) {
sc.setJoinParameters("hostTagSearch", "tag", haTag);
}

Expand Down
2 changes: 0 additions & 2 deletions server/src/test/java/com/cloud/vm/FirstFitPlannerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,6 @@ public void checkClusterListBasedOnHostTag() throws InsufficientServerCapacityEx
}

private List<Long> initializeForClusterListBasedOnHostTag(ServiceOffering offering) {


when(offering.getHostTag()).thenReturn("hosttag1");
initializeForClusterThresholdDisabled();
List<Long> matchingClusters = new ArrayList<>();
Expand Down