bug 10489: improving listVM performance: Combined various queries to get Vm details into one single join, reduced the time required to 1/5

This commit is contained in:
Abhinandan Prateek 2011-06-30 09:55:35 +05:30
parent 1503bacc7c
commit f34252f2e0
6 changed files with 181 additions and 194 deletions

View File

@ -142,4 +142,9 @@ public class NicResponse extends BaseResponse {
public void setMacAddress(String macAddress) {
this.macAddress = macAddress;
}
@Override
public boolean equals(Object r){
return id==((NicResponse)r).id;
}
}

View File

@ -131,4 +131,9 @@ public class SecurityGroupResponse extends BaseResponse {
public void setJobStatus(Integer jobStatus) {
this.jobStatus = jobStatus;
}
@Override
public boolean equals(Object r){
return id==((SecurityGroupResponse)r).id;
}
}

View File

@ -22,6 +22,7 @@ import java.util.List;
import java.util.Map;
import com.cloud.agent.AgentManager;
import com.cloud.api.response.UserVmResponse;
import com.cloud.async.AsyncJobManager;
import com.cloud.async.AsyncJobVO;
import com.cloud.configuration.Config;
@ -61,7 +62,6 @@ import com.cloud.network.dao.IPAddressDao;
import com.cloud.network.dao.LoadBalancerDao;
import com.cloud.network.dao.NetworkDao;
import com.cloud.network.dao.NetworkRuleConfigDao;
import com.cloud.network.rules.PortForwardingRuleVO;
import com.cloud.network.security.SecurityGroup;
import com.cloud.network.security.SecurityGroupManager;
import com.cloud.network.security.SecurityGroupVO;
@ -594,5 +594,9 @@ public class ApiDBUtils {
public static List<String> findPortForwardingSourceCidrs(long id){
return _firewallCidrsDao.getSourceCidrs(id);
}
public static List<UserVmResponse> listVmDetails(UserVm userVm, boolean show_host){
return _userVmDao.listVmDetails(userVm, show_host);
}
}

View File

@ -1047,209 +1047,33 @@ public class ApiResponseHelper implements ResponseGenerator {
@Override
public List<UserVmResponse> createUserVmResponse(String objectName, UserVm... userVms) {
Account caller = UserContext.current().getCaller();
Map<Long, DataCenter> dataCenters = new HashMap<Long, DataCenter>();
Map<Long, Host> hosts = new HashMap<Long, Host>();
Map<Long, VMTemplateVO> templates = new HashMap<Long, VMTemplateVO>();
Map<Long, ServiceOffering> serviceOfferings = new HashMap<Long, ServiceOffering>();
Map<Long, Network> networks = new HashMap<Long, Network>();
List<UserVmResponse> vmResponses = new ArrayList<UserVmResponse>();
DecimalFormat decimalFormat = new DecimalFormat("#.##");
for (UserVm userVm : userVms) {
UserVmResponse userVmResponse = new UserVmResponse();
Account acct = ApiDBUtils.findAccountById(Long.valueOf(userVm.getAccountId()));
if (acct != null) {
userVmResponse.setAccountName(acct.getAccountName());
userVmResponse.setDomainId(acct.getDomainId());
userVmResponse.setDomainName(ApiDBUtils.findDomainById(acct.getDomainId()).getName());
}
List<UserVmResponse> rsps = ApiDBUtils.listVmDetails(userVm, (((caller == null) || (caller.getType() == Account.ACCOUNT_TYPE_ADMIN)) && (userVm.getHostId() != null)));
for (UserVmResponse userVmResponse: rsps){
// stats calculation
String cpuUsed = null;
VmStats vmStats = ApiDBUtils.getVmStatistics(userVm.getId());
if (vmStats != null) {
float cpuUtil = (float) vmStats.getCPUUtilization();
cpuUsed = decimalFormat.format(cpuUtil) + "%";
userVmResponse.setCpuUsed(cpuUsed);
userVmResponse.setId(userVm.getId());
userVmResponse.setName(userVm.getHostName());
userVmResponse.setCreated(userVm.getCreated());
Double networkKbRead = Double.valueOf(vmStats.getNetworkReadKBs());
userVmResponse.setNetworkKbsRead(networkKbRead.longValue());
if (userVm.getState() != null) {
userVmResponse.setState(userVm.getState().toString());
}
userVmResponse.setHaEnable(userVm.isHaEnabled());
if (userVm.getDisplayName() != null) {
userVmResponse.setDisplayName(userVm.getDisplayName());
} else {
userVmResponse.setDisplayName(userVm.getHostName());
}
InstanceGroupVO group = ApiDBUtils.findInstanceGroupForVM(userVm.getId());
if (group != null) {
userVmResponse.setGroup(group.getName());
userVmResponse.setGroupId(group.getId());
}
// Data Center Info
DataCenter zone = dataCenters.get(userVm.getDataCenterIdToDeployIn());
if (zone == null) {
zone = ApiDBUtils.findZoneById(userVm.getDataCenterIdToDeployIn());
dataCenters.put(zone.getId(), zone);
}
userVmResponse.setZoneId(zone.getId());
userVmResponse.setZoneName(zone.getName());
// if user is an admin, display host id
if (((caller == null) || (caller.getType() == Account.ACCOUNT_TYPE_ADMIN)) && (userVm.getHostId() != null)) {
Host host = hosts.get(userVm.getHostId());
if (host == null) {
host = ApiDBUtils.findHostById(userVm.getHostId());
hosts.put(host.getId(), host);
Double networkKbWrite = Double.valueOf(vmStats.getNetworkWriteKBs());
userVmResponse.setNetworkKbsWrite(networkKbWrite.longValue());
}
userVmResponse.setHostId(host.getId());
userVmResponse.setHostName(host.getName());
vmResponses.add(userVmResponse);
}
if (userVm.getHypervisorType() != null) {
userVmResponse.setHypervisor(userVm.getHypervisorType().toString());
}
// Template Info
VMTemplateVO template = templates.get(userVm.getTemplateId());
if (template == null) {
template = ApiDBUtils.findTemplateById(userVm.getTemplateId());
if (template != null) {
templates.put(template.getId(), template);
}
}
if (template != null) {
userVmResponse.setTemplateId(userVm.getTemplateId());
userVmResponse.setTemplateName(template.getName());
userVmResponse.setTemplateDisplayText(template.getDisplayText());
userVmResponse.setPasswordEnabled(template.getEnablePassword());
} else {
userVmResponse.setTemplateId(-1L);
userVmResponse.setTemplateName("ISO Boot");
userVmResponse.setTemplateDisplayText("ISO Boot");
userVmResponse.setPasswordEnabled(false);
}
if (userVm.getPassword() != null) {
userVmResponse.setPassword(userVm.getPassword());
}
// ISO Info
VMTemplateVO iso = templates.get(userVm.getIsoId());
if (iso == null) {
iso = ApiDBUtils.findTemplateById(userVm.getIsoId());
if (iso != null) {
templates.put(iso.getId(), iso);
}
}
if (iso != null) {
userVmResponse.setIsoId(iso.getId());
userVmResponse.setIsoName(iso.getName());
}
// Service Offering Info
ServiceOffering offering = serviceOfferings.get(userVm.getServiceOfferingId());
if (offering == null) {
offering = ApiDBUtils.findServiceOfferingById(userVm.getServiceOfferingId());
serviceOfferings.put(offering.getId(), offering);
}
userVmResponse.setServiceOfferingId(offering.getId());
userVmResponse.setServiceOfferingName(offering.getName());
userVmResponse.setCpuNumber(offering.getCpu());
userVmResponse.setCpuSpeed(offering.getSpeed());
userVmResponse.setMemory(offering.getRamSize());
VolumeVO rootVolume = ApiDBUtils.findRootVolume(userVm.getId());
if (rootVolume != null) {
userVmResponse.setRootDeviceId(rootVolume.getDeviceId());
String rootDeviceType = "Not created";
if (rootVolume.getPoolId() != null) {
StoragePoolVO storagePool = ApiDBUtils.findStoragePoolById(rootVolume.getPoolId());
rootDeviceType = storagePool.getPoolType().toString();
}
userVmResponse.setRootDeviceType(rootDeviceType);
}
// stats calculation
DecimalFormat decimalFormat = new DecimalFormat("#.##");
String cpuUsed = null;
VmStats vmStats = ApiDBUtils.getVmStatistics(userVm.getId());
if (vmStats != null) {
float cpuUtil = (float) vmStats.getCPUUtilization();
cpuUsed = decimalFormat.format(cpuUtil) + "%";
userVmResponse.setCpuUsed(cpuUsed);
Double networkKbRead = Double.valueOf(vmStats.getNetworkReadKBs());
userVmResponse.setNetworkKbsRead(networkKbRead.longValue());
Double networkKbWrite = Double.valueOf(vmStats.getNetworkWriteKBs());
userVmResponse.setNetworkKbsWrite(networkKbWrite.longValue());
}
userVmResponse.setGuestOsId(userVm.getGuestOSId());
// security groups - list only when zone is security group enabled
if (zone.isSecurityGroupEnabled()) {
List<SecurityGroupVO> securityGroups = ApiDBUtils.getSecurityGroupsForVm(userVm.getId());
List<SecurityGroupResponse> securityGroupResponse = new ArrayList<SecurityGroupResponse>();
for (SecurityGroupVO grp : securityGroups) {
SecurityGroupResponse resp = new SecurityGroupResponse();
resp.setId(grp.getId());
resp.setName(grp.getName());
resp.setDescription(grp.getDescription());
resp.setObjectName("securitygroup");
securityGroupResponse.add(resp);
}
userVmResponse.setSecurityGroupList(securityGroupResponse);
}
List<NicProfile> nicProfiles = ApiDBUtils.getNics(userVm);
List<NicResponse> nicResponses = new ArrayList<NicResponse>();
for (NicProfile singleNicProfile : nicProfiles) {
NicResponse nicResponse = new NicResponse();
nicResponse.setId(singleNicProfile.getId());
nicResponse.setIpaddress(singleNicProfile.getIp4Address());
nicResponse.setGateway(singleNicProfile.getGateway());
nicResponse.setNetmask(singleNicProfile.getNetmask());
nicResponse.setNetworkid(singleNicProfile.getNetworkId());
nicResponse.setMacAddress(singleNicProfile.getMacAddress());
if (acct.getType() == Account.ACCOUNT_TYPE_ADMIN) {
if (singleNicProfile.getBroadCastUri() != null) {
nicResponse.setBroadcastUri(singleNicProfile.getBroadCastUri().toString());
}
if (singleNicProfile.getIsolationUri() != null) {
nicResponse.setIsolationUri(singleNicProfile.getIsolationUri().toString());
}
}
// Long networkId = singleNicProfile.getNetworkId();
Network network = networks.get(singleNicProfile.getNetworkId());
if (network == null) {
network = ApiDBUtils.findNetworkById(singleNicProfile.getNetworkId());
networks.put(singleNicProfile.getNetworkId(), network);
}
nicResponse.setTrafficType(network.getTrafficType().toString());
nicResponse.setType(network.getGuestType().toString());
nicResponse.setIsDefault(singleNicProfile.isDefaultNic());
nicResponse.setObjectName("nic");
nicResponses.add(nicResponse);
}
userVmResponse.setNics(nicResponses);
userVmResponse.setObjectName(objectName);
vmResponses.add(userVmResponse);
}
return vmResponses;
}
@Override
public DomainRouterResponse createDomainRouterResponse(VirtualRouter router) {
Map<Long, ServiceOffering> serviceOfferings = new HashMap<Long, ServiceOffering>();

View File

@ -20,9 +20,10 @@ package com.cloud.vm.dao;
import java.util.Date;
import java.util.List;
import com.cloud.api.response.UserVmResponse;
import com.cloud.uservm.UserVm;
import com.cloud.utils.db.GenericDao;
import com.cloud.vm.UserVmVO;
import com.cloud.vm.VirtualMachine;
import com.cloud.vm.VirtualMachine.State;
public interface UserVmDao extends GenericDao<UserVmVO, Long> {
@ -69,4 +70,6 @@ public interface UserVmDao extends GenericDao<UserVmVO, Long> {
List<Long> listPodIdsHavingVmsforAccount(long zoneId, long accountId);
public Long countAllocatedVMsForAccount(long accountId);
List<UserVmResponse> listVmDetails(UserVm userVm, boolean show_host);
}

View File

@ -22,13 +22,22 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.ejb.Local;
import org.apache.log4j.Logger;
import com.cloud.api.ApiDBUtils;
import com.cloud.api.response.NicResponse;
import com.cloud.api.response.SecurityGroupResponse;
import com.cloud.api.response.UserVmResponse;
import com.cloud.user.Account;
import com.cloud.uservm.UserVm;
import com.cloud.utils.component.ComponentLocator;
import com.cloud.utils.db.Attribute;
import com.cloud.utils.db.GenericDaoBase;
@ -70,6 +79,26 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
private static final String LIST_PODS_HAVING_VMS_FOR_ACCOUNT = "SELECT pod_id FROM cloud.vm_instance WHERE data_center_id = ? AND account_id = ? AND pod_id IS NOT NULL AND state = 'Running' OR state = 'Stopped' " +
"GROUP BY pod_id HAVING count(id) > 0 ORDER BY count(id) DESC";
private static final String VM_DETAILS = "select account.account_name, account.type, domain.name, instance_group_vm_map.group_id, instance_group_vm_map.id," +
"data_center.id, data_center.name, data_center.is_security_group_enabled, host.id, host.name, vm_template.id, vm_template.name, vm_template.display_text, " +
"vm_template.enable_password, service_offering.id, disk_offering.name, " +
"service_offering.cpu, service_offering.speed, service_offering.ram_size, volumes.device_id, volumes.volume_type, security_group.id, security_group.name, " +
"security_group.description, nics.id, nics.ip4_address, nics.gateway, nics.network_id, nics.netmask, nics.mac_address, nics.broadcast_uri, nics.isolation_uri, " +
"networks.traffic_type, networks.guest_type, networks.is_default from vm_instance " +
"left join account on vm_instance.account_id=account.id " +
"left join domain on vm_instance.domain_id=domain.id " +
"left join instance_group_vm_map on vm_instance.id=instance_group_vm_map.instance_id " +
"left join data_center on vm_instance.data_center_id=data_center.id " +
"left join host on vm_instance.host_id=host.id " +
"left join vm_template on vm_instance.vm_template_id=vm_template.id " +
"left join service_offering on vm_instance.service_offering_id=service_offering.id " +
"left join disk_offering on vm_instance.service_offering_id=disk_offering.id " +
"left join volumes on vm_instance.id=volumes.instance_id " +
"left join security_group_vm_map on vm_instance.id=security_group_vm_map.instance_id " +
"left join security_group on security_group_vm_map.security_group_id=security_group.id " +
"left join nics on vm_instance.id=nics.instance_id " +
"left join networks on nics.network_id=networks.id " +
"where vm_instance.id=?";
protected final UserVmDetailsDaoImpl _detailsDao = ComponentLocator.inject(UserVmDetailsDaoImpl.class);
@ -310,6 +339,123 @@ public class UserVmDaoImpl extends GenericDaoBase<UserVmVO, Long> implements Use
throw new CloudRuntimeException("Caught: " + LIST_PODS_HAVING_VMS_FOR_ACCOUNT, e);
}
}
@Override
public List<UserVmResponse> listVmDetails(UserVm userVm, boolean show_host){
Transaction txn = Transaction.currentTxn();
PreparedStatement pstmt = null;
List<UserVmResponse> result = new ArrayList<UserVmResponse>();
try {
String sql = VM_DETAILS;
pstmt = txn.prepareAutoCloseStatement(sql);
pstmt.setLong(1, userVm.getId());
ResultSet rs = pstmt.executeQuery();
boolean is_data_center_security_group_enabled=false;
Set<SecurityGroupResponse> securityGroupResponse = new HashSet<SecurityGroupResponse>();
Set<NicResponse> nicResponses = new HashSet<NicResponse>();
UserVmResponse userVmResponse = null;
while (rs.next()) {
if (userVmResponse==null){
userVmResponse=new UserVmResponse();
userVmResponse.setId(userVm.getId());
userVmResponse.setName(userVm.getDisplayName());
userVmResponse.setCreated(userVm.getCreated());
userVmResponse.setGuestOsId(userVm.getGuestOSId());
userVmResponse.setHaEnable(userVm.isHaEnabled());
if (userVm.getDisplayName() != null) {
userVmResponse.setDisplayName(userVm.getDisplayName());
} else {
userVmResponse.setDisplayName(userVm.getHostName());
}
//account.account_name, account.type, domain.name, instance_group_vm_map.group_id, instance_group_vm_map.id,"
userVmResponse.setAccountName(rs.getString("account.account_name"));
userVmResponse.setDomainId(userVm.getDomainId());
userVmResponse.setDomainName(rs.getString("domain.name"));
userVmResponse.setGroup(rs.getString("instance_group_vm_map.group_id"));
userVmResponse.setGroupId(rs.getLong("instance_group_vm_map.id"));
//"data_center.id, data_center.name, host.id, host.name, vm_template.id, vm_template.name, vm_template.display_text, vm_template.enable_password,
userVmResponse.setZoneId(rs.getLong("data_center.id"));
userVmResponse.setZoneName(rs.getString("data_center.name"));
if (show_host){
userVmResponse.setHostId(rs.getLong("host.id"));
userVmResponse.setHostName(rs.getString("host.name"));
}
userVmResponse.setTemplateId(rs.getLong("vm_template.id"));
userVmResponse.setTemplateName(rs.getString("vm_template.name"));
userVmResponse.setTemplateDisplayText(rs.getString("vm_template.display_text"));
userVmResponse.setPasswordEnabled(rs.getBoolean("vm_template.enable_password"));
//service_offering.id, disk_offering.name, "
//"service_offering.cpu, service_offering.speed, service_offering.ram_size,
userVmResponse.setServiceOfferingId(rs.getLong("service_offering.id"));
userVmResponse.setServiceOfferingName(rs.getString("disk_offering.name"));
userVmResponse.setCpuNumber(rs.getInt("service_offering.cpu"));
userVmResponse.setCpuSpeed(rs.getInt("service_offering.speed"));
userVmResponse.setMemory(rs.getInt("service_offering.ram_size"));
// volumes.device_id, volumes.volume_type,
userVmResponse.setRootDeviceId(rs.getLong("volumes.device_id"));
userVmResponse.setRootDeviceType(rs.getString("volumes.volume_type"));
is_data_center_security_group_enabled = rs.getBoolean("data_center.is_security_group_enabled");
result.add(userVmResponse);
}
//security_group.id, security_group.name, security_group.description, , data_center.is_security_group_enabled
if (is_data_center_security_group_enabled){
SecurityGroupResponse resp = new SecurityGroupResponse();
resp.setId(rs.getLong("security_group.id"));
resp.setName("security_group.name");
resp.setDescription("security_group.description");
resp.setObjectName("securitygroup");
securityGroupResponse.add(resp);
}
//nics.id, nics.ip4_address, nics.gateway, nics.network_id, nics.netmask, nics. mac_address, nics.broadcast_uri, nics.isolation_uri, " +
//"networks.traffic_type, networks.guest_type, networks.is_default from vm_instance, "
NicResponse nicResponse = new NicResponse();
nicResponse.setId(rs.getLong("nics.id"));
nicResponse.setIpaddress(rs.getString("nics.ip4_address"));
nicResponse.setGateway(rs.getString("nics.gateway"));
nicResponse.setNetmask(rs.getString("nics.netmask"));
nicResponse.setNetworkid(rs.getLong("nics.network_id"));
nicResponse.setMacAddress(rs.getString("nics.mac_address"));
int account_type = rs.getInt("account.type");
if (account_type == Account.ACCOUNT_TYPE_ADMIN) {
nicResponse.setBroadcastUri(rs.getString("nics.broadcast_uri"));
nicResponse.setIsolationUri(rs.getString("nics.isolation_uri"));
}
nicResponse.setTrafficType(rs.getString("networks.traffic_type"));
nicResponse.setType(rs.getString("networks.guest_type"));
nicResponse.setIsDefault(rs.getBoolean("networks.is_default"));
nicResponse.setObjectName("nic");
nicResponses.add(nicResponse);
}
userVmResponse.setSecurityGroupList(new ArrayList(securityGroupResponse));
userVmResponse.setNics(new ArrayList(nicResponses));
return result;
} catch (SQLException e) {
throw new CloudRuntimeException("DB Exception on: " + VM_DETAILS, e);
} catch (Throwable e) {
throw new CloudRuntimeException("Caught: " + VM_DETAILS, e);
}
}
@Override
public Long countAllocatedVMsForAccount(long accountId) {