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
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);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so we order by runid and assume that means the first one is the longest running.
I think this will work, but there is the issue of re-joining the cluster and time skews (this is about milliseconds. I don´t think these will be issues for this functionality but just giving a heads-up. Maybe someone else knows??

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In such cases would the runid field get updated? If that is the case then another management server will acquire the lock for this task

List<ManagementServerHostVO> msHosts = listBy(sc, filter);
return CollectionUtils.isNotEmpty(msHosts) ? msHosts.get(0) : null;
}

}
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");
Comment thread
nvazquez marked this conversation as resolved.
Resolved
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