Skip to content

Commit ffc0d51

Browse files
committed
Merge branch '4.20' of https://github.com/apache/cloudstack
2 parents 193d6ef + 255a45c commit ffc0d51

File tree

8 files changed

+126
-107
lines changed

8 files changed

+126
-107
lines changed

plugins/host-allocators/random/src/main/java/com/cloud/agent/manager/allocator/impl/RandomAllocator.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,15 +94,17 @@ private List<Host> findSuitableHosts(VirtualMachineProfile vmProfile, Deployment
9494
return suitableHosts;
9595
}
9696
String offeringHostTag = offering.getHostTag();
97+
9798
VMTemplateVO template = (VMTemplateVO)vmProfile.getTemplate();
9899
String templateTag = template.getTemplateTag();
99100
String hostTag = null;
100-
if (ObjectUtils.anyNull(offeringHostTag, templateTag)) {
101-
hostTag = offeringHostTag;
102-
hostTag = hostTag == null ? templateTag : String.format("%s, %s", hostTag, templateTag);
103-
logger.debug(String.format("Looking for hosts in dc [%s], pod [%s], cluster [%s] and complying with host tag(s): [%s]", dcId, podId, clusterId, hostTag));
101+
if (ObjectUtils.anyNotNull(offeringHostTag, templateTag)) {
102+
hostTag = ObjectUtils.allNotNull(offeringHostTag, templateTag) ?
103+
String.format("%s, %s", offeringHostTag, templateTag) :
104+
ObjectUtils.firstNonNull(offeringHostTag, templateTag);
105+
logger.debug("Looking for hosts in dc [{}], pod [{}], cluster [{}] and complying with host tag(s): [{}]", dcId, podId, clusterId, hostTag);
104106
} else {
105-
logger.debug("Looking for hosts in dc: " + dcId + " pod:" + podId + " cluster:" + clusterId);
107+
logger.debug("Looking for hosts in dc: {} pod: {} cluster: {}", dcId , podId, clusterId);
106108
}
107109
if (hosts != null) {
108110
// retain all computing hosts, regardless of whether they support routing...it's random after all

server/src/main/java/com/cloud/agent/manager/allocator/impl/FirstFitAllocator.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import javax.inject.Inject;
2626
import javax.naming.ConfigurationException;
2727

28-
import com.cloud.utils.exception.CloudRuntimeException;
2928
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
3029
import org.apache.cloudstack.utils.reflectiontostringbuilderutils.ReflectionToStringBuilderUtils;
3130
import org.springframework.stereotype.Component;
@@ -130,8 +129,8 @@ public List<Host> allocateTo(VirtualMachineProfile vmProfile, DeploymentPlan pla
130129
// FirstFitAllocator should be used for user VMs only since it won't care whether the host is capable of routing or not
131130
return new ArrayList<>();
132131
}
133-
134-
logger.debug("Looking for hosts in zone [{}], pod [{}], cluster [{}]", dcId, podId, clusterId);
132+
String paramAsStringToLog = String.format("zone [%s], pod [%s], cluster [%s]", dcId, podId, clusterId);
133+
logger.debug("Looking for hosts in {}", paramAsStringToLog);
135134

136135
String hostTagOnOffering = offering.getHostTag();
137136
String hostTagOnTemplate = template.getTemplateTag();
@@ -203,8 +202,8 @@ public List<Host> allocateTo(VirtualMachineProfile vmProfile, DeploymentPlan pla
203202

204203

205204
if (clusterHosts.isEmpty()) {
206-
logger.error("No suitable host found for vm [{}] with tags [{}].", vmProfile, hostTagOnOffering);
207-
throw new CloudRuntimeException(String.format("No suitable host found for vm [%s].", vmProfile));
205+
logger.warn("No suitable host found for VM [{}] with tags {} in {}.", vmProfile, hostTagOnOffering, paramAsStringToLog);
206+
return null;
208207
}
209208
// add all hosts that we are not considering to the avoid list
210209
List<HostVO> allhostsInCluster = _hostDao.listAllUpAndEnabledNonHAHosts(type, clusterId, podId, dcId, null);

server/src/main/java/com/cloud/network/IpAddressManagerImpl.java

Lines changed: 55 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,36 +1038,48 @@ public List<IPAddressVO> listAvailablePublicIps(final long dcId, final Long podI
10381038
@Override
10391039
public void markPublicIpAsAllocated(final IPAddressVO addr) {
10401040
synchronized (allocatedLock) {
1041-
Transaction.execute(new TransactionCallbackNoReturn() {
1041+
Transaction.execute(new TransactionCallbackWithExceptionNoReturn<CloudRuntimeException>() {
10421042
@Override
10431043
public void doInTransactionWithoutResult(TransactionStatus status) {
10441044
Account owner = _accountMgr.getAccount(addr.getAllocatedToAccountId());
1045-
if (_ipAddressDao.lockRow(addr.getId(), true) != null) {
1046-
final IPAddressVO userIp = _ipAddressDao.findById(addr.getId());
1047-
if (userIp.getState() == IpAddress.State.Allocating || addr.getState() == IpAddress.State.Free || addr.getState() == IpAddress.State.Reserved) {
1048-
boolean shouldUpdateIpResourceCount = checkIfIpResourceCountShouldBeUpdated(addr);
1049-
addr.setState(IpAddress.State.Allocated);
1050-
if (_ipAddressDao.update(addr.getId(), addr)) {
1051-
// Save usage event
1052-
if (owner.getAccountId() != Account.ACCOUNT_ID_SYSTEM) {
1053-
VlanVO vlan = _vlanDao.findById(addr.getVlanId());
1054-
String guestType = vlan.getVlanType().toString();
1055-
if (!isIpDedicated(addr)) {
1056-
final boolean usageHidden = isUsageHidden(addr);
1057-
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_ASSIGN, owner.getId(), addr.getDataCenterId(), addr.getId(),
1058-
addr.getAddress().toString(), addr.isSourceNat(), guestType, addr.getSystem(), usageHidden,
1059-
addr.getClass().getName(), addr.getUuid());
1060-
}
1061-
if (shouldUpdateIpResourceCount) {
1062-
_resourceLimitMgr.incrementResourceCount(owner.getId(), ResourceType.public_ip);
1063-
}
1064-
}
1065-
} else {
1066-
logger.error("Failed to mark public IP as allocated: {}", addr);
1045+
final IPAddressVO userIp = _ipAddressDao.lockRow(addr.getId(), true);
1046+
if (userIp == null) {
1047+
logger.error(String.format("Failed to acquire row lock to mark public IP as allocated with ID [%s] and address [%s]", addr.getId(), addr.getAddress()));
1048+
return;
1049+
}
1050+
1051+
List<IpAddress.State> expectedIpAddressStates = List.of(IpAddress.State.Allocating, IpAddress.State.Free, IpAddress.State.Reserved);
1052+
if (!expectedIpAddressStates.contains(userIp.getState())) {
1053+
logger.debug(String.format("Not marking public IP with ID [%s] and address [%s] as allocated, since it is in the [%s] state.", addr.getId(), addr.getAddress(), userIp.getState()));
1054+
return;
1055+
}
1056+
1057+
boolean shouldUpdateIpResourceCount = checkIfIpResourceCountShouldBeUpdated(addr);
1058+
addr.setState(IpAddress.State.Allocated);
1059+
boolean updatedIpAddress = _ipAddressDao.update(addr.getId(), addr);
1060+
if (!updatedIpAddress) {
1061+
logger.error(String.format("Failed to mark public IP as allocated with ID [%s] and address [%s]", addr.getId(), addr.getAddress()));
1062+
return;
1063+
}
1064+
1065+
if (owner.getAccountId() != Account.ACCOUNT_ID_SYSTEM) {
1066+
if (shouldUpdateIpResourceCount) {
1067+
try (CheckedReservation publicIpReservation = new CheckedReservation(owner, ResourceType.public_ip, 1L, reservationDao, _resourceLimitMgr)) {
1068+
_resourceLimitMgr.incrementResourceCount(owner.getId(), ResourceType.public_ip);
1069+
} catch (Exception e) {
1070+
_ipAddressDao.unassignIpAddress(addr.getId());
1071+
throw new CloudRuntimeException(e);
10671072
}
10681073
}
1069-
} else {
1070-
logger.error("Failed to acquire row lock to mark public IP as allocated: {}", addr);
1074+
1075+
VlanVO vlan = _vlanDao.findById(addr.getVlanId());
1076+
String guestType = vlan.getVlanType().toString();
1077+
if (!isIpDedicated(addr)) {
1078+
final boolean usageHidden = isUsageHidden(addr);
1079+
UsageEventUtils.publishUsageEvent(EventTypes.EVENT_NET_IP_ASSIGN, owner.getId(), addr.getDataCenterId(), addr.getId(),
1080+
addr.getAddress().toString(), addr.isSourceNat(), guestType, addr.getSystem(), usageHidden,
1081+
addr.getClass().getName(), addr.getUuid());
1082+
}
10711083
}
10721084
}
10731085
});
@@ -1553,27 +1565,31 @@ public IPAddressVO associateIPToGuestNetwork(long ipId, long networkId, boolean
15531565

15541566
boolean isSourceNat = isSourceNatAvailableForNetwork(owner, ipToAssoc, network);
15551567

1556-
logger.debug("Associating ip " + ipToAssoc + " to network " + network);
1568+
logger.debug(String.format("Associating IP [%s] to network [%s].", ipToAssoc, network));
15571569

15581570
boolean success = false;
15591571
IPAddressVO ip = null;
1560-
try (CheckedReservation publicIpReservation = new CheckedReservation(owner, ResourceType.public_ip, 1l, reservationDao, _resourceLimitMgr)) {
1561-
ip = _ipAddressDao.findById(ipId);
1562-
//update ip address with networkId
1563-
ip.setAssociatedWithNetworkId(networkId);
1564-
ip.setSourceNat(isSourceNat);
1565-
_ipAddressDao.update(ipId, ip);
1566-
1567-
success = applyIpAssociations(network, false);
1572+
try {
1573+
Pair<IPAddressVO, Boolean> updatedIpAddress = Transaction.execute((TransactionCallbackWithException<Pair<IPAddressVO, Boolean>, Exception>) status -> {
1574+
IPAddressVO ipAddress = _ipAddressDao.findById(ipId);
1575+
ipAddress.setAssociatedWithNetworkId(networkId);
1576+
ipAddress.setSourceNat(isSourceNat);
1577+
_ipAddressDao.update(ipId, ipAddress);
1578+
return new Pair<>(_ipAddressDao.findById(ipId), applyIpAssociations(network, false));
1579+
});
1580+
1581+
ip = updatedIpAddress.first();
1582+
success = updatedIpAddress.second();
15681583
if (success) {
1569-
logger.debug("Successfully associated ip address " + ip.getAddress().addr() + " to network " + network);
1584+
logger.debug(String.format("Successfully associated IP address [%s] to network [%s]", ip.getAddress().addr(), network));
15701585
} else {
1571-
logger.warn("Failed to associate ip address " + ip.getAddress().addr() + " to network " + network);
1586+
logger.warn(String.format("Failed to associate IP address [%s] to network [%s]", ip.getAddress().addr(), network));
15721587
}
1573-
return _ipAddressDao.findById(ipId);
1588+
return ip;
15741589
} catch (Exception e) {
1575-
logger.error(String.format("Failed to associate ip address %s to network %s", ipToAssoc, network), e);
1576-
throw new CloudRuntimeException(String.format("Failed to associate ip address %s to network %s", ipToAssoc, network), e);
1590+
String errorMessage = String.format("Failed to associate IP address [%s] to network [%s]", ipToAssoc, network);
1591+
logger.error(errorMessage, e);
1592+
throw new CloudRuntimeException(errorMessage, e);
15771593
} finally {
15781594
if (!success && releaseOnFailure) {
15791595
if (ip != null) {

server/src/main/java/com/cloud/network/vpc/VpcManagerImpl.java

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
import com.cloud.dc.Vlan;
5151
import com.cloud.network.dao.NsxProviderDao;
5252
import com.cloud.network.element.NsxProviderVO;
53-
import com.cloud.resourcelimit.CheckedReservation;
5453
import com.google.common.collect.Sets;
5554
import org.apache.cloudstack.acl.ControlledEntity.ACLType;
5655
import org.apache.cloudstack.alert.AlertService;
@@ -3206,32 +3205,27 @@ public IpAddress associateIPToVpc(final long ipId, final long vpcId) throws Reso
32063205
// check permissions
32073206
_accountMgr.checkAccess(caller, null, false, owner, vpc);
32083207

3209-
logger.debug("Associating ip " + ipToAssoc + " to vpc " + vpc);
3208+
logger.debug(String.format("Associating IP [%s] to VPC [%s]", ipToAssoc, vpc));
32103209

32113210
final boolean isSourceNatFinal = isSrcNatIpRequired(vpc.getVpcOfferingId()) && getExistingSourceNatInVpc(vpc.getAccountId(), vpcId, false) == null;
3212-
try (CheckedReservation publicIpReservation = new CheckedReservation(owner, ResourceType.public_ip, 1l, reservationDao, _resourceLimitMgr)) {
3213-
Transaction.execute(new TransactionCallbackNoReturn() {
3214-
@Override
3215-
public void doInTransactionWithoutResult(final TransactionStatus status) {
3211+
try {
3212+
IPAddressVO updatedIpAddress = Transaction.execute((TransactionCallbackWithException<IPAddressVO, CloudRuntimeException>) status -> {
32163213
final IPAddressVO ip = _ipAddressDao.findById(ipId);
3217-
// update ip address with networkId
32183214
ip.setVpcId(vpcId);
32193215
ip.setSourceNat(isSourceNatFinal);
3220-
32213216
_ipAddressDao.update(ipId, ip);
3222-
3223-
// mark ip as allocated
32243217
_ipAddrMgr.markPublicIpAsAllocated(ip);
3225-
}
3218+
return _ipAddressDao.findById(ipId);
32263219
});
3220+
3221+
logger.debug(String.format("Successfully assigned IP [%s] to VPC [%s]", ipToAssoc, vpc));
3222+
CallContext.current().putContextParameter(IpAddress.class, ipToAssoc.getUuid());
3223+
return updatedIpAddress;
32273224
} catch (Exception e) {
3228-
logger.error("Failed to associate ip " + ipToAssoc + " to vpc " + vpc, e);
3229-
throw new CloudRuntimeException("Failed to associate ip " + ipToAssoc + " to vpc " + vpc, e);
3225+
String errorMessage = String.format("Failed to associate IP address [%s] to VPC [%s]", ipToAssoc, vpc);
3226+
logger.error(errorMessage, e);
3227+
throw new CloudRuntimeException(errorMessage, e);
32303228
}
3231-
3232-
logger.debug("Successfully assigned ip " + ipToAssoc + " to vpc " + vpc);
3233-
CallContext.current().putContextParameter(IpAddress.class, ipToAssoc.getUuid());
3234-
return _ipAddressDao.findById(ipId);
32353229
}
32363230

32373231
@Override

0 commit comments

Comments
 (0)