<commit_before>package com.cloud.vm.dao;

import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
import com.cloud.server.ResourceTag.ResourceObjectType;
import com.cloud.tags.dao.ResourceTagDao;
import com.cloud.utils.DateUtil;
import com.cloud.utils.Pair;
import com.cloud.utils.db.Attribute;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.GenericSearchBuilder;
import com.cloud.utils.db.JoinBuilder;
import com.cloud.utils.db.JoinBuilder.JoinType;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.SearchCriteria.Func;
import com.cloud.utils.db.SearchCriteria.Op;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.db.TransactionCallback;
import com.cloud.utils.db.TransactionCallbackNoReturn;
import com.cloud.utils.db.TransactionLegacy;
import com.cloud.utils.db.TransactionStatus;
import com.cloud.utils.db.UpdateBuilder;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.NicVO;
import com.cloud.vm.VMInstanceVO;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachine.Event;
import com.cloud.vm.VirtualMachine.State;
import com.cloud.vm.VirtualMachine.Type;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implements VMInstanceDao {

    private static final Logger s_logger = LoggerFactory.getLogger(VMInstanceDaoImpl.class);

    private static final int MAX_CONSECUTIVE_SAME_STATE_UPDATE_COUNT = 3;
    private static final String ORDER_CLUSTERS_NUMBER_OF_VMS_FOR_ACCOUNT_PART1 = "SELECT host.cluster_id, SUM(IF(vm.state IN ('Starting', 'Running') AND vm.account_id = ?, 1, 0)) " +
            "FROM `cloud`.`host` host LEFT JOIN `cloud`.`vm_instance` vm ON host.id = vm.host_id WHERE ";
    private static final String ORDER_CLUSTERS_NUMBER_OF_VMS_FOR_ACCOUNT_PART2 = " AND host.type = 'Routing' AND host.removed is null GROUP BY host.cluster_id " +
            "ORDER BY 2 ASC ";
    private static final String ORDER_PODS_NUMBER_OF_VMS_FOR_ACCOUNT = "SELECT pod.id, SUM(IF(vm.state IN ('Starting', 'Running') AND vm.account_id = ?, 1, 0)) FROM `cloud`.`" +
            "host_pod_ref` pod LEFT JOIN `cloud`.`vm_instance` vm ON pod.id = vm.pod_id WHERE pod.data_center_id = ? AND pod.removed is null "
            + " GROUP BY pod.id ORDER BY 2 ASC ";
    private static final String ORDER_HOSTS_NUMBER_OF_VMS_FOR_ACCOUNT =
            "SELECT host.id, SUM(IF(vm.state='Running' AND vm.account_id = ?, 1, 0)) FROM `cloud`.`host` host LEFT JOIN `cloud`.`vm_instance` vm ON host.id = vm.host_id " +
                    "WHERE host.data_center_id = ? AND host.type = 'Routing' AND host.removed is null ";
    private static final String ORDER_HOSTS_NUMBER_OF_VMS_FOR_ACCOUNT_PART2 = " GROUP BY host.id ORDER BY 2 ASC ";
    private static final String COUNT_VMS_BASED_ON_VGPU_TYPES1 =
            "SELECT pci, type, SUM(vmcount) FROM (SELECT MAX(IF(offering.name = 'pciDevice',value,'')) AS pci, MAX(IF(offering.name = 'vgpuType', value,'')) " +
                    "AS type, COUNT(DISTINCT vm.id) AS vmcount FROM service_offering_details offering INNER JOIN vm_instance vm ON offering.service_offering_id = vm" +
                    ".service_offering_id " +
                    "INNER JOIN `cloud`.`host` ON vm.host_id = host.id WHERE vm.state = 'Running' AND host.data_center_id = ? ";
    private static final String COUNT_VMS_BASED_ON_VGPU_TYPES2 =
            "GROUP BY offering.service_offering_id) results GROUP BY pci, type";

    private SearchBuilder<VMInstanceVO> _vmClusterSearch;
    private SearchBuilder<VMInstanceVO> _lhvmClusterSearch;
    private SearchBuilder<VMInstanceVO> _allFieldsSearch;
    private SearchBuilder<VMInstanceVO> _zoneTemplateNonExpungedSearch;
    private SearchBuilder<VMInstanceVO> _nameLikeSearch;
    private SearchBuilder<VMInstanceVO> _stateChangeSearch;
    private SearchBuilder<VMInstanceVO> _transitionSearch;
    private SearchBuilder<VMInstanceVO> _typesSearch;
    private SearchBuilder<VMInstanceVO> _idTypesSearch;
    private SearchBuilder<VMInstanceVO> _hostIdTypesSearch;
    private SearchBuilder<VMInstanceVO> _hostIdStatesSearch;
    private SearchBuilder<VMInstanceVO> _hostIdUpTypesSearch;<commit_msg>Also counting starting VMs when ordering hosts for deployment dispersion.<commit_after>package com.cloud.vm.dao;

import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
import com.cloud.server.ResourceTag.ResourceObjectType;
import com.cloud.tags.dao.ResourceTagDao;
import com.cloud.utils.DateUtil;
import com.cloud.utils.Pair;
import com.cloud.utils.db.Attribute;
import com.cloud.utils.db.DB;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.GenericSearchBuilder;
import com.cloud.utils.db.JoinBuilder;
import com.cloud.utils.db.JoinBuilder.JoinType;
import com.cloud.utils.db.SearchBuilder;
import com.cloud.utils.db.SearchCriteria;
import com.cloud.utils.db.SearchCriteria.Func;
import com.cloud.utils.db.SearchCriteria.Op;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.db.TransactionCallback;
import com.cloud.utils.db.TransactionCallbackNoReturn;
import com.cloud.utils.db.TransactionLegacy;
import com.cloud.utils.db.TransactionStatus;
import com.cloud.utils.db.UpdateBuilder;
import com.cloud.utils.exception.CloudRuntimeException;
import com.cloud.vm.NicVO;
import com.cloud.vm.VMInstanceVO;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachine.Event;
import com.cloud.vm.VirtualMachine.State;
import com.cloud.vm.VirtualMachine.Type;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class VMInstanceDaoImpl extends GenericDaoBase<VMInstanceVO, Long> implements VMInstanceDao {

    private static final Logger s_logger = LoggerFactory.getLogger(VMInstanceDaoImpl.class);

    private static final int MAX_CONSECUTIVE_SAME_STATE_UPDATE_COUNT = 3;
    private static final String ORDER_CLUSTERS_NUMBER_OF_VMS_FOR_ACCOUNT_PART1 = "SELECT host.cluster_id, SUM(IF(vm.state IN ('Starting', 'Running') AND vm.account_id = ?, 1, 0)) " +
            "FROM `cloud`.`host` host LEFT JOIN `cloud`.`vm_instance` vm ON host.id = vm.host_id WHERE ";
    private static final String ORDER_CLUSTERS_NUMBER_OF_VMS_FOR_ACCOUNT_PART2 = " AND host.type = 'Routing' AND host.removed is null GROUP BY host.cluster_id " +
            "ORDER BY 2 ASC ";
    private static final String ORDER_PODS_NUMBER_OF_VMS_FOR_ACCOUNT = "SELECT pod.id, SUM(IF(vm.state IN ('Starting', 'Running') AND vm.account_id = ?, 1, 0)) FROM `cloud`.`" +
            "host_pod_ref` pod LEFT JOIN `cloud`.`vm_instance` vm ON pod.id = vm.pod_id WHERE pod.data_center_id = ? AND pod.removed is null "
            + " GROUP BY pod.id ORDER BY 2 ASC ";
    private static final String ORDER_HOSTS_NUMBER_OF_VMS_FOR_ACCOUNT =
            "SELECT host.id, SUM(IF(vm.state IN ('Starting', 'Running') AND vm.account_id = ?, 1, 0)) FROM `cloud`.`host` host LEFT JOIN `cloud`.`vm_instance` vm ON host.id = vm.host_id " +
                    "WHERE host.data_center_id = ? AND host.type = 'Routing' AND host.removed is null ";
    private static final String ORDER_HOSTS_NUMBER_OF_VMS_FOR_ACCOUNT_PART2 = " GROUP BY host.id ORDER BY 2 ASC ";
    private static final String COUNT_VMS_BASED_ON_VGPU_TYPES1 =
            "SELECT pci, type, SUM(vmcount) FROM (SELECT MAX(IF(offering.name = 'pciDevice',value,'')) AS pci, MAX(IF(offering.name = 'vgpuType', value,'')) " +
                    "AS type, COUNT(DISTINCT vm.id) AS vmcount FROM service_offering_details offering INNER JOIN vm_instance vm ON offering.service_offering_id = vm" +
                    ".service_offering_id " +
                    "INNER JOIN `cloud`.`host` ON vm.host_id = host.id WHERE vm.state = 'Running' AND host.data_center_id = ? ";
    private static final String COUNT_VMS_BASED_ON_VGPU_TYPES2 =
            "GROUP BY offering.service_offering_id) results GROUP BY pci, type";

    private SearchBuilder<VMInstanceVO> _vmClusterSearch;
    private SearchBuilder<VMInstanceVO> _lhvmClusterSearch;
    private SearchBuilder<VMInstanceVO> _allFieldsSearch;
    private SearchBuilder<VMInstanceVO> _zoneTemplateNonExpungedSearch;
    private SearchBuilder<VMInstanceVO> _nameLikeSearch;
    private SearchBuilder<VMInstanceVO> _stateChangeSearch;
    private SearchBuilder<VMInstanceVO> _transitionSearch;
    private SearchBuilder<VMInstanceVO> _typesSearch;
    private SearchBuilder<VMInstanceVO> _idTypesSearch;
    private SearchBuilder<VMInstanceVO> _hostIdTypesSearch;
    private SearchBuilder<VMInstanceVO> _hostIdStatesSearch;
    private SearchBuilder<VMInstanceVO> _hostIdUpTypesSearch;
{'fraction_non_alphanumeric': 0.09758946585961559, 'fraction_numerical': 0.003610491664011893, 'mean_word_length': 4.659855769230769, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6205743', 'n_tokens_mistral': 2992, 'n_tokens_neox': 2921, 'n_words': 669}