Skip to content
Draft
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 @@ -16,6 +16,7 @@
// under the License.
package org.apache.cloudstack.context;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Stack;
Expand Down Expand Up @@ -65,6 +66,7 @@ protected Stack<CallContext> initialValue() {
private final Map<Object, Object> context = new HashMap<Object, Object>();
private Project project;
private String apiName;
private final RequestEntityCache requestEntityCache = new RequestEntityCache(Duration.ofSeconds(60));

static EntityManager s_entityMgr;

Expand Down Expand Up @@ -423,4 +425,8 @@ public String toString() {
.append("]")
.toString();
}

public RequestEntityCache getRequestEntityCache() {
return requestEntityCache;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package org.apache.cloudstack.context;

import java.time.Duration;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Supplier;

public final class RequestEntityCache {
private static final class Key {
final Class<?> type;
final long id;
Key(Class<?> type, long id) { this.type = type; this.id = id; }
@Override public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Key)) return false;
Key k = (Key) o; return id == k.id && type.equals(k.type);
}
@Override public int hashCode() { return 31 * type.hashCode() + Long.hashCode(id); }
}
private static final class Entry {
final Object value; final long expireAtNanos; // 0 = never expires
Entry(Object v, long exp) { value = v; expireAtNanos = exp; }
}

private final ConcurrentHashMap<Key, Entry> map = new ConcurrentHashMap<>();
private final long ttlNanos; // 0 = no TTL

public RequestEntityCache(Duration ttl) { this.ttlNanos = ttl == null ? 0 : ttl.toNanos(); }

@SuppressWarnings("unchecked")
public <T> T get(Class<T> type, long id, Supplier<T> loader) {
final long now = System.nanoTime();
final Key key = new Key(type, id);
Entry e = map.get(key);
if (e != null && (e.expireAtNanos == 0 || now < e.expireAtNanos)) {
return (T) e.value;
}
T loaded = loader.get();
long exp = ttlNanos == 0 ? 0 : now + ttlNanos;
if (loaded != null) map.put(key, new Entry(loaded, exp));
return loaded;
}

public void clear() { map.clear(); }
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
import javax.inject.Inject;

import com.cloud.network.Network;

import org.apache.cloudstack.context.CallContext;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;

Expand Down Expand Up @@ -215,7 +217,8 @@ private static void publishUsageEvent(String usageEventType, Long accountId, Lon
}

Account account = s_accountDao.findById(accountId);
DataCenterVO dc = s_dcDao.findById(zoneId);
DataCenterVO dc = CallContext.current().getRequestEntityCache().get(DataCenterVO.class, zoneId,
() -> s_dcDao.findById(zoneId));

// if account has been deleted, this might be called during cleanup of resources and results in null pointer
if (account == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@

import com.cloud.resource.ResourceState;
import org.apache.cloudstack.ca.CAManager;
import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.framework.config.ConfigDepot;
import org.apache.cloudstack.framework.config.ConfigKey;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
Expand Down Expand Up @@ -573,7 +574,8 @@ public SocketChannel connectToPeer(final long hostId, final SocketChannel prevCh
@Override
protected AgentAttache getAttache(final Long hostId) throws AgentUnavailableException {
assert hostId != null : "Who didn't check their id value?";
final HostVO host = _hostDao.findById(hostId);
final HostVO host = CallContext.current().getRequestEntityCache()
.get(HostVO.class, hostId, () -> _hostDao.findById(hostId));
if (host == null) {
throw new AgentUnavailableException("Can't find the host ", hostId);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,9 @@ public void orchestrateStart(final String vmUuid, final Map<VirtualMachineProfil

startedVm = vm;
logger.debug("Start completed for VM {}", vm);
final Host vmHost = _hostDao.findById(destHostId);
long finalDestHostId = destHostId;
final Host vmHost = CallContext.current().getRequestEntityCache().get(HostVO.class, finalDestHostId,
() -> _hostDao.findById(finalDestHostId));
if (vmHost != null && (VirtualMachine.Type.ConsoleProxy.equals(vm.getType()) ||
VirtualMachine.Type.SecondaryStorageVm.equals(vm.getType())) && caManager.canProvisionCertificates()) {
for (int retries = 3; retries > 0; retries--) {
Expand Down Expand Up @@ -1766,7 +1768,8 @@ private void addToNetworkNameMap(long networkId, long dataCenterId, Map<Long, St
NetworkVO networkVO = _networkDao.findById(networkId);
Account acc = accountDao.findById(networkVO.getAccountId());
Domain domain = domainDao.findById(networkVO.getDomainId());
DataCenter zone = _dcDao.findById(dataCenterId);
DataCenter zone = CallContext.current().getRequestEntityCache()
.get(DataCenterVO.class, dataCenterId, () -> _dcDao.findById(dataCenterId));
if (Objects.isNull(zone)) {
throw new CloudRuntimeException(String.format("Failed to find zone with ID: %s", dataCenterId));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

import javax.inject.Inject;

import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.framework.config.ConfigKey;
import org.apache.cloudstack.framework.config.ConfigKey.Scope;
import org.apache.cloudstack.framework.config.ScopedConfigStorage;
Expand Down Expand Up @@ -197,7 +198,8 @@ private String getCpuMemoryOvercommitRatio(String name) {

@Override
public Pair<Scope, Long> getParentScope(long id) {
Cluster cluster = clusterDao.findById(id);
Cluster cluster = CallContext.current().getRequestEntityCache().get(ClusterVO.class, id,
() -> clusterDao.findById(id));
if (cluster == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,62 +16,61 @@
// under the License.
package org.apache.cloudstack.storage.allocator;

import com.cloud.api.query.dao.StoragePoolJoinDao;
import com.cloud.dc.dao.HostPodDao;
import com.cloud.exception.StorageUnavailableException;
import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
import com.cloud.storage.ScopeType;
import com.cloud.storage.StoragePoolStatus;
import org.apache.cloudstack.storage.datastore.db.StoragePoolDetailVO;
import org.apache.cloudstack.storage.datastore.db.StoragePoolDetailsDao;
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
import org.apache.commons.collections.CollectionUtils;
import java.math.BigDecimal;
import java.security.SecureRandom;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.cloud.utils.Pair;
import javax.inject.Inject;
import javax.naming.ConfigurationException;

import org.apache.cloudstack.context.CallContext;
import org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
import org.apache.cloudstack.engine.subsystem.api.storage.StoragePoolAllocator;
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
import org.apache.cloudstack.storage.datastore.db.PrimaryDataStoreDao;
import org.apache.cloudstack.storage.datastore.db.StoragePoolDetailVO;
import org.apache.cloudstack.storage.datastore.db.StoragePoolDetailsDao;
import org.apache.cloudstack.storage.datastore.db.StoragePoolVO;
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.cloud.api.query.dao.StoragePoolJoinDao;
import com.cloud.capacity.Capacity;
import com.cloud.capacity.dao.CapacityDao;
import com.cloud.dc.ClusterVO;
import com.cloud.dc.dao.ClusterDao;
import com.cloud.deploy.DeploymentPlan;
import com.cloud.deploy.DeploymentPlanner.ExcludeList;
import com.cloud.exception.StorageUnavailableException;
import com.cloud.host.HostVO;
import com.cloud.host.dao.HostDao;
import com.cloud.hypervisor.Hypervisor.HypervisorType;
import com.cloud.storage.ScopeType;
import com.cloud.storage.Storage;
import com.cloud.storage.StorageManager;
import com.cloud.storage.StoragePool;
import com.cloud.storage.StoragePoolStatus;
import com.cloud.storage.StorageUtil;
import com.cloud.storage.Volume;
import com.cloud.storage.dao.VolumeDao;
import com.cloud.user.Account;
import com.cloud.utils.NumbersUtil;
import com.cloud.utils.Pair;
import com.cloud.utils.component.AdapterBase;
import com.cloud.vm.DiskProfile;
import com.cloud.vm.VirtualMachineProfile;

import org.apache.cloudstack.engine.orchestration.service.VolumeOrchestrationService;

import javax.inject.Inject;
import javax.naming.ConfigurationException;
import java.math.BigDecimal;
import java.security.SecureRandom;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public abstract class AbstractStoragePoolAllocator extends AdapterBase implements StoragePoolAllocator {

protected BigDecimal storageOverprovisioningFactor = new BigDecimal(1);
Expand All @@ -88,8 +87,6 @@ public abstract class AbstractStoragePoolAllocator extends AdapterBase implement
@Inject private StoragePoolDetailsDao storagePoolDetailsDao;
@Inject
protected HostDao hostDao;
@Inject
protected HostPodDao podDao;

/**
* make sure shuffled lists of Pools are really shuffled
Expand Down Expand Up @@ -304,7 +301,8 @@ protected boolean filter(ExcludeList avoid, StoragePool pool, DiskProfile dskCh,

Long clusterId = pool.getClusterId();
if (clusterId != null) {
ClusterVO cluster = clusterDao.findById(clusterId);
ClusterVO cluster = CallContext.current().getRequestEntityCache()
.get(ClusterVO.class, clusterId, () -> clusterDao.findById(clusterId));
if (!(cluster.getHypervisorType() == dskCh.getHypervisorType())) {
if (logger.isDebugEnabled()) {
logger.debug("StoragePool's Cluster does not have required hypervisorType, skipping this pool");
Expand All @@ -329,7 +327,8 @@ protected boolean filter(ExcludeList avoid, StoragePool pool, DiskProfile dskCh,
}

if (plan.getHostId() != null) {
HostVO plannedHost = hostDao.findById(plan.getHostId());
HostVO plannedHost = CallContext.current().getRequestEntityCache()
.get(HostVO.class, plan.getHostId(), () -> hostDao.findById(plan.getHostId()));
if (!storageMgr.checkIfHostAndStoragePoolHasCommonStorageAccessGroups(plannedHost, pool)) {
if (logger.isDebugEnabled()) {
logger.debug(String.format("StoragePool %s and host %s does not have matching storage access groups", pool, plannedHost));
Expand Down
Loading
Loading