Skip to content

Commit 46a80a5

Browse files
committed
bug 9192: multiple improvementes to listVms command.
1) No longer do multiple searches involving "domain" table; only one join with domain is being done. 2) Do join with domain table only when command is executed by domainAdmin 3) Added index for "path" field in "domain" table 4) No longer do joins with account table as account_id is already present in vm_instance table.
1 parent 2a4d0cf commit 46a80a5

7 files changed

Lines changed: 79 additions & 122 deletions

File tree

‎api/src/com/cloud/api/commands/ListVMsCmd.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ public class ListVMsCmd extends BaseListCmd {
4747
@Parameter(name=ApiConstants.DOMAIN_ID, type=CommandType.LONG, description="the domain ID. If used with the account parameter, lists virtual machines for the specified account in this domain.")
4848
private Long domainId;
4949

50-
@Parameter(name=ApiConstants.IS_RECURSIVE, type=CommandType.BOOLEAN, description="defaults to false, but if true, lists all vms from the parent specified by the domain id till leaves.")
50+
@Parameter(name=ApiConstants.IS_RECURSIVE, type=CommandType.BOOLEAN, description="Must be used with domainId parameter. Defaults to false, but if true, lists all vms from the parent specified by the domain id till leaves.")
5151
private Boolean recursive;
5252

5353
@Parameter(name=ApiConstants.GROUP_ID, type=CommandType.LONG, description="the group ID")

‎server/src/com/cloud/acl/DomainChecker.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ public boolean checkAccess(Account account, Domain domain) throws PermissionDeni
5353
throw new PermissionDeniedException(account + " is disabled.");
5454
}
5555

56-
if (!_domainDao.isChildDomain(account.getDomainId(), domain.getId())) {
56+
if (account.getType() == Account.ACCOUNT_TYPE_NORMAL) {
57+
if (account.getDomainId() != domain.getId()) {
58+
throw new PermissionDeniedException(account + " does not have permission to operate within " + domain);
59+
}
60+
} else if (!_domainDao.isChildDomain(account.getDomainId(), domain.getId())) {
5761
throw new PermissionDeniedException(account + " does not have permission to operate within " + domain);
5862
}
5963

‎server/src/com/cloud/api/ApiDBUtils.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ public static StorageStats getSecondaryStorageStatistics(long id){
299299
/////////////////////////////////////////////////////////////
300300

301301
public static Account findAccountById(Long accountId) {
302-
return _accountDao.findById(accountId);
302+
return _accountDao.findByIdIncludingRemoved(accountId);
303303
}
304304

305305
public static Account findAccountByIdIncludingRemoved(Long accountId) {

‎server/src/com/cloud/api/ApiResponseHelper.java‎

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -995,17 +995,10 @@ public IpForwardingRuleResponse createIpForwardingRuleResponse(StaticNatRule fwR
995995
public UserVmResponse createUserVmResponse(UserVm userVm) {
996996
UserVmResponse userVmResponse = new UserVmResponse();
997997
Account acct = ApiDBUtils.findAccountById(Long.valueOf(userVm.getAccountId()));
998-
// FIXME - this check should be done in searchForUserVm method in
999-
// ManagementServerImpl;
1000-
// otherwise the number of vms returned is not going to match pageSize
1001-
// request parameter
1002-
if ((acct != null) && (acct.getRemoved() == null)) {
998+
if (acct != null) {
1003999
userVmResponse.setAccountName(acct.getAccountName());
10041000
userVmResponse.setDomainId(acct.getDomainId());
10051001
userVmResponse.setDomainName(ApiDBUtils.findDomainById(acct.getDomainId()).getName());
1006-
} else {
1007-
return null; // the account has been deleted, skip this VM in the
1008-
// response
10091002
}
10101003

10111004
userVmResponse.setId(userVm.getId());

‎server/src/com/cloud/vm/UserVmManagerImpl.java‎

Lines changed: 65 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -2621,58 +2621,57 @@ public UserVm destroyVm(long vmId) throws ResourceUnavailableException, Concurre
26212621

26222622
@Override
26232623
public List<UserVmVO> searchForUserVMs(ListVMsCmd cmd) throws InvalidParameterValueException, PermissionDeniedException {
2624-
Account account = UserContext.current().getCaller();
2624+
Account caller = UserContext.current().getCaller();
26252625
Long domainId = cmd.getDomainId();
26262626
String accountName = cmd.getAccountName();
2627-
Long accountId = null;
26282627
Boolean isRecursive = cmd.isRecursive();
26292628
String hypervisor = cmd.getHypervisor();
2630-
List<DomainVO> domainsToSearchForVms = new ArrayList<DomainVO>();
2631-
boolean isAdmin = false;
2629+
Long accountId = null;
26322630
String path = null;
2633-
if ((account == null) || isAdmin(account.getType())) {
2634-
isAdmin = true;
2635-
if (domainId != null) {
2636-
if ((account != null) && !_domainDao.isChildDomain(account.getDomainId(), domainId)) {
2637-
throw new PermissionDeniedException("Invalid domain id (" + domainId + ") given, unable to list virtual machines.");
2638-
}
2631+
2632+
if (isRecursive != null && isRecursive && domainId == null){
2633+
throw new InvalidParameterValueException("Please enter a parent domain id for listing vms recursively");
2634+
}
2635+
2636+
if (domainId != null) {
2637+
//Verify if user is authorized to see instances belonging to the domain
2638+
DomainVO domain = _domainDao.findById(domainId);
2639+
if (domain == null) {
2640+
throw new InvalidParameterValueException("Domain id=" + domainId + " doesn't exist");
2641+
}
2642+
_accountMgr.checkAccess(caller, domain);
2643+
}
26392644

2640-
if (accountName != null) {
2641-
account = _accountDao.findActiveAccount(accountName, domainId);
2642-
if (account == null) {
2643-
throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId);
2644-
}
2645-
accountId = account.getId();
2645+
boolean isAdmin = false;
2646+
2647+
if (_accountMgr.isAdmin(caller.getType())) {
2648+
isAdmin = true;
2649+
if (accountName != null && domainId != null) {
2650+
caller = _accountDao.findActiveAccount(accountName, domainId);
2651+
if (caller == null) {
2652+
throw new InvalidParameterValueException("Unable to find account " + accountName + " in domain " + domainId);
26462653
}
2647-
}
2648-
if (account.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
2649-
DomainVO domain = _domainDao.findById(account.getDomainId());
2650-
if (domain != null) {
2654+
accountId = caller.getId();
2655+
}
2656+
2657+
if (caller.getType() == Account.ACCOUNT_TYPE_DOMAIN_ADMIN) {
2658+
if (isRecursive == null) {
2659+
DomainVO domain = _domainDao.findById(caller.getDomainId());
26512660
path = domain.getPath();
2652-
}
2661+
}
26532662
}
26542663
} else {
2655-
accountId = account.getId();
2656-
}
2657-
2658-
if(isRecursive == null) {
2659-
isRecursive = false;
2664+
accountId = caller.getId();
26602665
}
26612666

2662-
if(isRecursive && domainId != null) {
2663-
DomainVO parentDomain = _domainDao.findById(domainId);
2664-
if(parentDomain.getName().equals("ROOT")) {
2665-
domainsToSearchForVms.addAll(_domainDao.listAll());
2666-
return recursivelySearchForVms(cmd, path, isAdmin, domainsToSearchForVms, accountId);
2667-
}else {
2668-
domainsToSearchForVms.add(parentDomain);
2669-
domainsToSearchForVms.addAll(_domainDao.findAllChildren(parentDomain.getPath(), parentDomain.getId()));
2670-
return recursivelySearchForVms(cmd, path, isAdmin, domainsToSearchForVms, accountId);
2667+
if (isRecursive != null && isRecursive && isAdmin) {
2668+
if (isRecursive) {
2669+
DomainVO domain = _domainDao.findById(domainId);
2670+
path = domain.getPath();
2671+
domainId = null;
26712672
}
2672-
} else if(isRecursive && domainId == null){
2673-
throw new InvalidParameterValueException("Please enter a parent domain id for listing vms recursively");
2674-
}
2675-
2673+
}
2674+
26762675
Criteria c = new Criteria("id", Boolean.TRUE, cmd.getStartIndex(), cmd.getPageSizeVal());
26772676
c.addCriteria(Criteria.KEYWORD, cmd.getKeyword());
26782677
c.addCriteria(Criteria.ID, cmd.getId());
@@ -2683,19 +2682,22 @@ public List<UserVmVO> searchForUserVMs(ListVMsCmd cmd) throws InvalidParameterVa
26832682
c.addCriteria(Criteria.FOR_VIRTUAL_NETWORK, cmd.getForVirtualNetwork());
26842683
c.addCriteria(Criteria.NETWORKID, cmd.getNetworkId());
26852684

2685+
if (domainId != null) {
2686+
c.addCriteria(Criteria.DOMAINID, domainId);
2687+
}
2688+
26862689
if (path != null) {
26872690
c.addCriteria(Criteria.PATH, path);
26882691
}
26892692

26902693
if (HypervisorType.getType(hypervisor) != HypervisorType.None){
26912694
c.addCriteria(Criteria.HYPERVISOR, hypervisor);
2692-
}else if (hypervisor != null){
2695+
} else if (hypervisor != null){
26932696
throw new InvalidParameterValueException("Invalid HypervisorType " + hypervisor);
26942697
}
26952698

26962699
// ignore these search requests if it's not an admin
2697-
if (isAdmin == true) {
2698-
c.addCriteria(Criteria.DOMAINID, domainId);
2700+
if (isAdmin) {
26992701
c.addCriteria(Criteria.PODID, cmd.getPodId());
27002702
c.addCriteria(Criteria.HOSTID, cmd.getHostId());
27012703
c.addCriteria(Criteria.STORAGE_ID, cmd.getStorageId());
@@ -2709,60 +2711,14 @@ public List<UserVmVO> searchForUserVMs(ListVMsCmd cmd) throws InvalidParameterVa
27092711
return searchForUserVMs(c);
27102712
}
27112713

2712-
private List<UserVmVO> recursivelySearchForVms(ListVMsCmd cmd, String path, boolean isAdmin, List<DomainVO> domainToSearchWithin, Long accountId) {
2713-
2714-
List<UserVmVO> result = new ArrayList<UserVmVO>();
2715-
String hypervisor = cmd.getHypervisor();
2716-
for(DomainVO domain : domainToSearchWithin) {
2717-
2718-
Criteria c = new Criteria("id", Boolean.TRUE, cmd.getStartIndex(), cmd.getPageSizeVal());
2719-
c.addCriteria(Criteria.KEYWORD, cmd.getKeyword());
2720-
c.addCriteria(Criteria.ID, cmd.getId());
2721-
c.addCriteria(Criteria.NAME, cmd.getInstanceName());
2722-
c.addCriteria(Criteria.STATE, cmd.getState());
2723-
c.addCriteria(Criteria.DATACENTERID, cmd.getZoneId());
2724-
c.addCriteria(Criteria.GROUPID, cmd.getGroupId());
2725-
c.addCriteria(Criteria.FOR_VIRTUAL_NETWORK, cmd.getForVirtualNetwork());
2726-
c.addCriteria(Criteria.NETWORKID, cmd.getNetworkId());
2727-
2728-
if (path != null) {
2729-
c.addCriteria(Criteria.PATH, path);
2730-
}
2731-
2732-
// ignore these search requests if it's not an admin
2733-
if (isAdmin == true) {
2734-
c.addCriteria(Criteria.DOMAINID, domain.getId());
2735-
c.addCriteria(Criteria.PODID, cmd.getPodId());
2736-
c.addCriteria(Criteria.HOSTID, cmd.getHostId());
2737-
}
2738-
2739-
if (HypervisorType.getType(hypervisor) != HypervisorType.None){
2740-
c.addCriteria(Criteria.HYPERVISOR, hypervisor);
2741-
}else if (hypervisor != null){
2742-
throw new InvalidParameterValueException("Invalid HypervisorType " + hypervisor);
2743-
}
2744-
2745-
if (accountId != null) {
2746-
c.addCriteria(Criteria.ACCOUNTID, new Object[] {accountId});
2747-
}
2748-
c.addCriteria(Criteria.ISADMIN, isAdmin);
2749-
2750-
result.addAll(searchForUserVMs(c));
2751-
}
2752-
return result;
2753-
}
27542714

27552715
@Override
27562716
public List<UserVmVO> searchForUserVMs(Criteria c) {
27572717
Filter searchFilter = new Filter(UserVmVO.class, c.getOrderBy(), c.getAscending(), c.getOffset(), c.getLimit());
27582718

27592719
SearchBuilder<UserVmVO> sb = _vmDao.createSearchBuilder();
2760-
2761-
// some criteria matter for generating the join condition
27622720
Object[] accountIds = (Object[]) c.getCriteria(Criteria.ACCOUNTID);
27632721
Object domainId = c.getCriteria(Criteria.DOMAINID);
2764-
2765-
// get the rest of the criteria
27662722
Object id = c.getCriteria(Criteria.ID);
27672723
Object name = c.getCriteria(Criteria.NAME);
27682724
Object state = c.getCriteria(Criteria.STATE);
@@ -2793,21 +2749,14 @@ public List<UserVmVO> searchForUserVMs(Criteria c) {
27932749
sb.and("hypervisorType", sb.entity().getHypervisorType(), SearchCriteria.Op.EQ);
27942750
sb.and("hostIdEQ", sb.entity().getHostId(), SearchCriteria.Op.EQ);
27952751
sb.and("hostIdIN", sb.entity().getHostId(), SearchCriteria.Op.IN);
2752+
sb.and("domainId", sb.entity().getDomainId(), SearchCriteria.Op.EQ);
27962753

2797-
if (domainId != null || path != null) {
2798-
// if accountId isn't specified, we can do a domain match for the admin case
2754+
if (path != null) {
27992755
SearchBuilder<DomainVO> domainSearch = _domainDao.createSearchBuilder();
2800-
domainSearch.and("id", domainSearch.entity().getId(), SearchCriteria.Op.EQ);
28012756
domainSearch.and("path", domainSearch.entity().getPath(), SearchCriteria.Op.LIKE);
28022757
sb.join("domainSearch", domainSearch, sb.entity().getDomainId(), domainSearch.entity().getId(), JoinBuilder.JoinType.INNER);
28032758
}
28042759

2805-
if (storageId != null) {
2806-
SearchBuilder<VolumeVO> volumeSearch = _volsDao.createSearchBuilder();
2807-
volumeSearch.and("poolId", volumeSearch.entity().getPoolId(), SearchCriteria.Op.EQ);
2808-
sb.join("volumeSearch", volumeSearch, sb.entity().getId(), volumeSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER);
2809-
}
2810-
28112760
if (groupId != null && (Long)groupId == -1) {
28122761
SearchBuilder<InstanceGroupVMMapVO> vmSearch = _groupVMMapDao.createSearchBuilder();
28132762
vmSearch.and("instanceId", vmSearch.entity().getInstanceId(), SearchCriteria.Op.EQ);
@@ -2829,13 +2778,14 @@ public List<UserVmVO> searchForUserVMs(Criteria c) {
28292778
sb.join("nicSearch", nicSearch, sb.entity().getId(), nicSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER);
28302779
}
28312780

2832-
SearchBuilder<AccountVO> accountRemoved = _accountDao.createSearchBuilder();
2833-
accountRemoved.and("accountremoved", accountRemoved.entity().getRemoved(), SearchCriteria.Op.NULL);
2834-
sb.join("accountRemoved", accountRemoved, sb.entity().getAccountId(), accountRemoved.entity().getId(), JoinBuilder.JoinType.INNER);
2781+
if (storageId != null) {
2782+
SearchBuilder<VolumeVO> volumeSearch = _volsDao.createSearchBuilder();
2783+
volumeSearch.and("poolId", volumeSearch.entity().getPoolId(), SearchCriteria.Op.EQ);
2784+
sb.join("volumeSearch", volumeSearch, sb.entity().getId(), volumeSearch.entity().getInstanceId(), JoinBuilder.JoinType.INNER);
2785+
}
28352786

28362787
// populate the search criteria with the values passed in
2837-
SearchCriteria<UserVmVO> sc = sb.create();
2838-
2788+
SearchCriteria<UserVmVO> sc = sb.create();
28392789
if (groupId != null && (Long)groupId == -1){
28402790
sc.setJoinParameters("vmSearch", "instanceId", (Object)null);
28412791
} else if (groupId != null ) {
@@ -2855,6 +2805,7 @@ public List<UserVmVO> searchForUserVMs(Criteria c) {
28552805
if (id != null) {
28562806
sc.setParameters("id", id);
28572807
}
2808+
28582809
if (accountIds != null) {
28592810
if (accountIds.length == 1) {
28602811
if (accountIds[0] != null) {
@@ -2863,25 +2814,24 @@ public List<UserVmVO> searchForUserVMs(Criteria c) {
28632814
} else {
28642815
sc.setParameters("accountIdIN", accountIds);
28652816
}
2866-
} else if (domainId != null) {
2867-
sc.setJoinParameters("domainSearch", "id", domainId);
2817+
}
2818+
2819+
if (domainId != null) {
2820+
sc.setParameters("domainId", domainId);
28682821
}
28692822

28702823
if (path != null) {
28712824
sc.setJoinParameters("domainSearch", "path", path + "%");
28722825
}
28732826

2874-
if (storageId != null) {
2875-
sc.setJoinParameters("volumeSearch", "poolId", storageId);
2876-
}
2877-
28782827
if (networkId != null) {
28792828
sc.setJoinParameters("nicSearch", "networkId", networkId);
28802829
}
28812830

28822831
if (name != null) {
28832832
sc.setParameters("name", "%" + name + "%");
28842833
}
2834+
28852835
if (state != null) {
28862836
if (notState != null && (Boolean) notState == true) {
28872837
sc.setParameters("stateNEQ", state);
@@ -2893,14 +2843,16 @@ public List<UserVmVO> searchForUserVMs(Criteria c) {
28932843
if (hypervisor != null){
28942844
sc.setParameters("hypervisorType", hypervisor);
28952845
}
2846+
2847+
//Don't show Destroyed and Expunging vms to the end user
28962848
if ((isAdmin != null) && ((Boolean) isAdmin != true)) {
28972849
sc.setParameters("stateNIN", "Destroyed", "Expunging");
28982850
}
28992851

29002852
if (zone != null) {
29012853
sc.setParameters("dataCenterId", zone);
29022854

2903-
if(state == null) {
2855+
if (state == null) {
29042856
sc.setParameters("stateNEQ", "Destroyed");
29052857
}
29062858
}
@@ -2929,6 +2881,10 @@ public List<UserVmVO> searchForUserVMs(Criteria c) {
29292881
}
29302882
}
29312883
}
2884+
2885+
if (storageId != null) {
2886+
sc.setJoinParameters("volumeSearch", "poolId", storageId);
2887+
}
29322888

29332889
return _vmDao.search(sc, searchFilter);
29342890
}

‎setup/db/create-schema.sql‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,7 +967,8 @@ CREATE TABLE `cloud`.`domain` (
967967
`removed` datetime COMMENT 'date removed',
968968
`state` char(32) NOT NULL default 'Active' COMMENT 'state of the domain',
969969
PRIMARY KEY (`id`),
970-
UNIQUE (parent, name, removed)
970+
UNIQUE (parent, name, removed),
971+
INDEX `i_domain__path`(`path`)
971972
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
972973

973974
CREATE TABLE `cloud`.`account` (

‎setup/db/schema-222to224.sql‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,7 @@ ALTER TABLE `cloud`.`cluster` ADD INDEX `i_cluster__allocation_state`(`allocatio
5757
ALTER TABLE `cloud`.`host_pod_ref` ADD COLUMN `allocation_state` varchar(32) NOT NULL DEFAULT 'Enabled';
5858
ALTER TABLE `cloud`.`host_pod_ref` ADD INDEX `i_host_pod_ref__allocation_state`(`allocation_state`);
5959
ALTER TABLE `cloud`.`host` ADD COLUMN `allocation_state` varchar(32) NOT NULL DEFAULT 'Enabled';
60-
ALTER TABLE `cloud`.`host` ADD INDEX `i_host__allocation_state`(`allocation_state`);
60+
ALTER TABLE `cloud`.`host` ADD INDEX `i_host__allocation_state`(`allocation_state`);
61+
62+
ALTER TABLE `cloud`.`domain` ADD INDEX `i_domain__path`(`path`);
63+

0 commit comments

Comments
 (0)