Skip to content

Commit

Permalink
Merge branch 'apache:main' into Refactoring-SwiftUtilTest
Browse files Browse the repository at this point in the history
  • Loading branch information
gzhao9 authored Oct 15, 2023
2 parents d198cad + 55bef2b commit 196ac6c
Show file tree
Hide file tree
Showing 18 changed files with 274 additions and 105 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ public class DeployVMCmd extends BaseAsyncCreateCustomIdCmd implements SecurityG
@Parameter(name = ApiConstants.DISPLAY_NAME, type = CommandType.STRING, description = "an optional user generated name for the virtual machine")
private String displayName;

@Parameter(name=ApiConstants.PASSWORD, type=CommandType.STRING, description="The password of the virtual machine. If null, a random password will be generated for the VM.",
since="4.19.0.0")
protected String password;

//Owner information
@Parameter(name = ApiConstants.ACCOUNT, type = CommandType.STRING, description = "an optional account for the virtual machine. Must be used with domainId.")
private String accountName;
Expand Down Expand Up @@ -464,6 +468,10 @@ public Long getZoneId() {
return zoneId;
}

public String getPassword() {
return password;
}

public List<Long> getNetworkIds() {
if (MapUtils.isNotEmpty(vAppNetworks)) {
if (CollectionUtils.isNotEmpty(networkIds) || ipAddress != null || getIp6Address() != null || MapUtils.isNotEmpty(ipToNetworkList)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// under the License.
package org.apache.cloudstack.api.command.user.vm;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import org.apache.cloudstack.acl.SecurityChecker.AccessType;
Expand Down Expand Up @@ -56,8 +57,7 @@ public class ResetVMPasswordCmd extends BaseAsyncCmd implements UserCmd {
required=true, description="The ID of the virtual machine")
private Long id;

// unexposed parameter needed for serializing/deserializing the command
@Parameter(name=ApiConstants.PASSWORD, type=CommandType.STRING, expose=false)
@Parameter(name=ApiConstants.PASSWORD, type=CommandType.STRING, description="The new password of the virtual machine. If null, a random password will be generated for the VM.", since="4.19.0")
protected String password;


Expand Down Expand Up @@ -118,7 +118,14 @@ public Long getApiResourceId() {

@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException {
password = _mgr.generateRandomPassword();
password = getPassword();
UserVm vm = _responseGenerator.findUserVmById(getId());
if (StringUtils.isBlank(password)) {
password = _mgr.generateRandomPassword();
s_logger.debug(String.format("Resetting VM [%s] password to a randomly generated password.", vm.getUuid()));
} else {
s_logger.debug(String.format("Resetting VM [%s] password to password defined by user.", vm.getUuid()));
}
CallContext.current().setEventDetails("Vm Id: " + getId());
UserVm result = _userVmService.resetVMPassword(this, password);
if (result != null){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ public class NetworkResponse extends BaseResponseWithAssociatedNetwork implement
@Param(description = "the domain name of the network owner")
private String domain;

@SerializedName(ApiConstants.DOMAIN_PATH)
@Param(description = "path of the Domain the network belongs to", since = "4.19.0.0")
private String domainPath;

@SerializedName("isdefault")
@Param(description = "true if network is default, false otherwise")
private Boolean isDefault;
Expand Down Expand Up @@ -420,6 +424,10 @@ public void setDomainName(String domain) {
this.domain = domain;
}

public void setDomainPath(String domainPath) {
this.domainPath = domainPath;
}

public void setNetworkOfferingAvailability(String networkOfferingAvailability) {
this.networkOfferingAvailability = networkOfferingAvailability;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,5 +164,5 @@ public interface VMInstanceDao extends GenericDao<VMInstanceVO, Long>, StateDao<

void updateSystemVmTemplateId(long templateId, Hypervisor.HypervisorType hypervisorType);

List<VMInstanceVO> listByHostOrLastHostOrHostPod(long hostId, long podId);
List<VMInstanceVO> listByHostOrLastHostOrHostPod(List<Long> hostIds, long podId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -989,19 +989,19 @@ public void updateSystemVmTemplateId(long templateId, Hypervisor.HypervisorType
}

@Override
public List<VMInstanceVO> listByHostOrLastHostOrHostPod(long hostId, long podId) {
public List<VMInstanceVO> listByHostOrLastHostOrHostPod(List<Long> hostIds, long podId) {
SearchBuilder<VMInstanceVO> sb = createSearchBuilder();
sb.or().op("hostId", sb.entity().getHostId(), Op.EQ);
sb.or("lastHostId", sb.entity().getLastHostId(), Op.EQ);
sb.and().op("hostIdNull", sb.entity().getHostId(), SearchCriteria.Op.NULL);
sb.and().op("hostId", sb.entity().getHostId(), Op.IN);
sb.or("lastHostId", sb.entity().getLastHostId(), Op.IN);
sb.or().op("hostIdNull", sb.entity().getHostId(), SearchCriteria.Op.NULL);
sb.and("lastHostIdNull", sb.entity().getHostId(), SearchCriteria.Op.NULL);
sb.and("podId", sb.entity().getPodIdToDeployIn(), Op.EQ);
sb.cp();
sb.cp();
sb.done();
SearchCriteria<VMInstanceVO> sc = sb.create();
sc.setParameters("hostId", String.valueOf(hostId));
sc.setParameters("lastHostId", String.valueOf(hostId));
sc.setParameters("hostId", hostIds.toArray());
sc.setParameters("lastHostId", hostIds.toArray());
sc.setParameters("podId", String.valueOf(podId));
return listBy(sc);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,6 @@ public interface ManagementServerHostDao extends GenericDao<ManagementServerHost
List<Long> listOrphanMsids();

ManagementServerHostVO findOneInUpState(Filter filter);

ManagementServerHostVO findOneByLongestRuntime();
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.TimeZone;


import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;

import com.cloud.cluster.ClusterInvalidSessionException;
Expand Down Expand Up @@ -204,6 +205,7 @@ protected ManagementServerHostDaoImpl() {

StateSearch = createSearchBuilder();
StateSearch.and("state", StateSearch.entity().getState(), SearchCriteria.Op.IN);
StateSearch.and("runid", StateSearch.entity().getRunid(), SearchCriteria.Op.GT);
StateSearch.done();
}

Expand Down Expand Up @@ -272,4 +274,14 @@ public ManagementServerHostVO findOneInUpState(Filter filter) {
return null;
}

@Override
public ManagementServerHostVO findOneByLongestRuntime() {
SearchCriteria<ManagementServerHostVO> sc = StateSearch.create();
sc.setParameters("state", ManagementServerHost.State.Up);
sc.setParameters("runid", 0);
Filter filter = new Filter(ManagementServerHostVO.class, "runid", true, 0L, 1L);
List<ManagementServerHostVO> msHosts = listBy(sc, filter);
return CollectionUtils.isNotEmpty(msHosts) ? msHosts.get(0) : null;
}

}
4 changes: 4 additions & 0 deletions server/src/main/java/com/cloud/api/ApiResponseHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2572,6 +2572,10 @@ public NetworkResponse createNetworkResponse(ResponseView view, Network network)
Domain domain = ApiDBUtils.findDomainById(domainNetworkDetails.first());
if (domain != null) {
response.setDomainId(domain.getUuid());

StringBuilder domainPath = new StringBuilder("ROOT");
(domainPath.append(domain.getPath())).deleteCharAt(domainPath.length() - 1);
response.setDomainPath(domainPath.toString());
}
}
response.setSubdomainAccess(domainNetworkDetails.second());
Expand Down
12 changes: 11 additions & 1 deletion server/src/main/java/com/cloud/api/query/QueryManagerImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@
import com.cloud.exception.InvalidParameterValueException;
import com.cloud.exception.PermissionDeniedException;
import com.cloud.ha.HighAvailabilityManager;
import com.cloud.host.Host;
import com.cloud.hypervisor.Hypervisor;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.network.RouterHealthCheckResult;
Expand Down Expand Up @@ -1090,7 +1091,12 @@ private Pair<List<UserVmJoinVO>, Integer> searchForUserVMsInternal(ListVMsCmd cm
sb.and("stateNIN", sb.entity().getState(), SearchCriteria.Op.NIN);
sb.and("dataCenterId", sb.entity().getDataCenterId(), SearchCriteria.Op.EQ);
sb.and("podId", sb.entity().getPodId(), SearchCriteria.Op.EQ);
sb.and("clusterId", sb.entity().getClusterId(), SearchCriteria.Op.EQ);
if (clusterId != null) {
sb.and().op("clusterId", sb.entity().getClusterId(), SearchCriteria.Op.EQ);
sb.or("clusterHostId", sb.entity().getHostId(), Op.IN);
sb.or("clusterLastHostId", sb.entity().getLastHostId(), Op.IN);
sb.cp();
}
sb.and("hypervisorType", sb.entity().getHypervisorType(), SearchCriteria.Op.EQ);
sb.and("hostIdEQ", sb.entity().getHostId(), SearchCriteria.Op.EQ);
sb.and("templateId", sb.entity().getTemplateId(), SearchCriteria.Op.EQ);
Expand Down Expand Up @@ -1286,6 +1292,10 @@ private Pair<List<UserVmJoinVO>, Integer> searchForUserVMsInternal(ListVMsCmd cm

if (clusterId != null) {
sc.setParameters("clusterId", clusterId);
List<HostJoinVO> hosts = _hostJoinDao.findByClusterId((Long)clusterId, Host.Type.Routing);
List<Long> hostIds = hosts.stream().map(HostJoinVO::getId).collect(Collectors.toList());
sc.setParameters("clusterHostId", hostIds.toArray());
sc.setParameters("clusterLastHostId", hostIds.toArray());
}

if (hostId != null) {
Expand Down
12 changes: 6 additions & 6 deletions server/src/main/java/com/cloud/deploy/FirstFitPlanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public class FirstFitPlanner extends AdapterBase implements DeploymentClusterPla

protected String allocationAlgorithm = "random";
protected String globalDeploymentPlanner = "FirstFitPlanner";
protected String[] implicitHostTags;
protected String[] implicitHostTags = new String[0];

@Override
public List<Long> orderClusters(VirtualMachineProfile vmProfile, DeploymentPlan plan, ExcludeList avoid) throws InsufficientServerCapacityException {
Expand Down Expand Up @@ -214,11 +214,11 @@ private void reorderClustersBasedOnImplicitTags(List<Long> clusterList, int requ
Long uniqueTags;
for (Long clusterId : clusterList) {
uniqueTags = (long) 0;
List<Long> hostList = capacityDao.listHostsWithEnoughCapacity(requiredCpu, requiredRam, clusterId, Host.Type.Routing.toString());
if (!hostList.isEmpty() && implicitHostTags.length > 0) {
uniqueTags = new Long(hostTagsDao.getDistinctImplicitHostTags(hostList, implicitHostTags).size());
uniqueTags = uniqueTags + getHostsByCapability(hostList, Host.HOST_UEFI_ENABLE);
}
List<Long> hostList = capacityDao.listHostsWithEnoughCapacity(requiredCpu, requiredRam, clusterId, Host.Type.Routing.toString());
if (!hostList.isEmpty() && implicitHostTags.length > 0) {
uniqueTags = new Long(hostTagsDao.getDistinctImplicitHostTags(hostList, implicitHostTags).size());
uniqueTags = uniqueTags + getHostsByCapability(hostList, Host.HOST_UEFI_ENABLE);
}
UniqueTagsInClusterMap.put(clusterId, uniqueTags);
}
Collections.sort(clusterList, new Comparator<Long>() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1964,7 +1964,7 @@ private void checkAutoScaleVmGroupName(String groupName) {
private boolean startNewVM(long vmId) {
try {
CallContext.current().setEventDetails("Vm Id: " + vmId);
userVmMgr.startVirtualMachine(vmId, null, null, null);
userVmMgr.startVirtualMachine(vmId, null, new HashMap<>(), null);
} catch (final ResourceUnavailableException ex) {
s_logger.warn("Exception: ", ex);
throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, ex.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
import javax.inject.Inject;
import javax.naming.ConfigurationException;

import com.cloud.cluster.ManagementServerHostVO;
import com.cloud.cluster.dao.ManagementServerHostDao;
import com.cloud.utils.db.GlobalLock;
import org.apache.cloudstack.acl.SecurityChecker.AccessType;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.engine.subsystem.api.storage.ObjectInDataStoreStateMachine;
Expand All @@ -44,6 +47,8 @@
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
import org.apache.cloudstack.user.ResourceReservation;
import org.apache.cloudstack.utils.identity.ManagementServerNode;
import org.apache.commons.collections.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

Expand Down Expand Up @@ -162,6 +167,8 @@ public class ResourceLimitManagerImpl extends ManagerBase implements ResourceLim
private VpcDao _vpcDao;
@Inject
private VlanDao _vlanDao;
@Inject
private ManagementServerHostDao managementServerHostDao;

protected GenericSearchBuilder<TemplateDataStoreVO, SumCount> templateSizeSearch;
protected GenericSearchBuilder<SnapshotDataStoreVO, SumCount> snapshotSizeSearch;
Expand Down Expand Up @@ -1171,6 +1178,26 @@ public ResourceCountCheckTask() {

@Override
protected void runInContext() {
GlobalLock lock = GlobalLock.getInternLock("ResourceCheckTask");
try {
if (lock.lock(30)) {
try {
ManagementServerHostVO msHost = managementServerHostDao.findOneByLongestRuntime();
if (msHost == null || (msHost.getMsid() != ManagementServerNode.getManagementServerId())) {
s_logger.trace("Skipping the resource counters recalculation task on this management server");
return;
}
runResourceCheckTaskInternal();
} finally {
lock.unlock();
}
}
} finally {
lock.releaseRef();
}
}

private void runResourceCheckTaskInternal() {
s_logger.info("Started resource counters recalculation periodic task.");
List<DomainVO> domains;
List<AccountVO> accounts;
Expand All @@ -1192,16 +1219,20 @@ protected void runInContext() {
}

for (ResourceType type : ResourceType.values()) {
recalculateDomainResourceCountInContext(Domain.ROOT_DOMAIN, type);
for (Domain domain : domains) {
recalculateDomainResourceCount(domain.getId(), type);
if (CollectionUtils.isEmpty(domains)) {
recalculateDomainResourceCountInContext(Domain.ROOT_DOMAIN, type);
} else {
for (Domain domain : domains) {
recalculateDomainResourceCount(domain.getId(), type);
}
}

// run through the accounts in the root domain
for (AccountVO account : accounts) {
recalculateAccountResourceCountInContext(account.getId(), type);
}
}
s_logger.info("Finished resource counters recalculation periodic task.");
}

private void recalculateDomainResourceCountInContext(long domainId, ResourceType type) {
Expand Down
Loading

0 comments on commit 196ac6c

Please sign in to comment.