Skip to content

Commit 933931f

Browse files
author
gitlab
committed
Merge branch 'huangtian-ZSTAC-83646@@2' into '5.5.12'
<fix>[compute]: fix VM clone quota check fail See merge request zstackio/zstack!9480
2 parents e2e0f8f + ee4b6be commit 933931f

5 files changed

Lines changed: 97 additions & 20 deletions

File tree

compute/src/main/java/org/zstack/compute/allocator/QuotaAllocatorFlow.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import org.springframework.beans.factory.annotation.Configurable;
55
import org.zstack.compute.vm.VmQuotaOperator;
66
import org.zstack.header.allocator.AbstractHostAllocatorFlow;
7+
import org.zstack.header.errorcode.ErrorCode;
8+
import org.zstack.header.errorcode.OperationFailureException;
79
import org.zstack.header.identity.AccountConstant;
810
import org.zstack.identity.Account;
911
import org.zstack.identity.QuotaUtil;
@@ -51,6 +53,12 @@ public void allocate() {
5153
}
5254

5355
throwExceptionIfIAmTheFirstFlow();
56+
// skip checkquota if the operator is admin
57+
String currentAccountUuid = spec.getAccountUuid();
58+
if (currentAccountUuid != null && AccountConstant.isAdminPermission(currentAccountUuid)) {
59+
next(candidates);
60+
return;
61+
}
5462

5563
final String vmInstanceUuid = spec.getVmInstance().getUuid();
5664
final String accountUuid = Account.getAccountUuidOfResource(vmInstanceUuid);
@@ -60,21 +68,27 @@ public void allocate() {
6068
}
6169

6270
if (!spec.isFullAllocate()) {
63-
new VmQuotaOperator().checkVmCupAndMemoryCapacity(accountUuid,
71+
ErrorCode error = new VmQuotaOperator().checkVmCupAndMemoryCapacityWithResult(accountUuid,
6472
accountUuid,
6573
spec.getCpuCapacity(),
6674
spec.getMemoryCapacity(),
6775
new QuotaUtil().makeQuotaPairs(accountUuid));
76+
if (error != null) {
77+
throw new OperationFailureException(error);
78+
}
6879

6980
next(candidates);
7081
return;
7182
}
7283

73-
new VmQuotaOperator().checkVmInstanceQuota(
84+
ErrorCode error = new VmQuotaOperator().checkVmInstanceQuotaWithResult(
7485
accountUuid,
7586
accountUuid,
7687
vmInstanceUuid,
7788
new QuotaUtil().makeQuotaPairs(accountUuid));
89+
if (error != null) {
90+
throw new OperationFailureException(error);
91+
}
7892
next(candidates);
7993
}
8094
}

compute/src/main/java/org/zstack/compute/vm/VmQuotaOperator.java

Lines changed: 56 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import org.zstack.core.db.SimpleQuery;
1010
import org.zstack.core.errorcode.ErrorFacade;
1111
import org.zstack.header.apimediator.ApiMessageInterceptionException;
12+
import org.zstack.header.errorcode.ErrorCode;
1213
import org.zstack.header.identity.APIChangeResourceOwnerMsg;
1314
import org.zstack.header.identity.AccountType;
1415
import org.zstack.header.identity.Quota;
@@ -151,12 +152,27 @@ private void checkTotalVMQuota(String currentAccountUuid,
151152
String vmInstanceUuid,
152153
long totalVmNumQuota,
153154
long totalVmNum) {
155+
ErrorCode error = checkTotalVMQuotaWithResult(currentAccountUuid,
156+
resourceTargetOwnerAccountUuid,
157+
vmInstanceUuid,
158+
totalVmNumQuota,
159+
totalVmNum);
160+
if (error != null) {
161+
throw new ApiMessageInterceptionException(error);
162+
}
163+
}
164+
165+
private ErrorCode checkTotalVMQuotaWithResult(String currentAccountUuid,
166+
String resourceTargetOwnerAccountUuid,
167+
String vmInstanceUuid,
168+
long totalVmNumQuota,
169+
long totalVmNum) {
154170
if (Q.New(VmInstanceVO.class)
155171
.eq(VmInstanceVO_.uuid, vmInstanceUuid)
156172
.notNull(VmInstanceVO_.lastHostUuid)
157173
.isExists()) {
158174
// Dirty hack - VM with last host UUID means existing VM.
159-
return;
175+
return null;
160176
}
161177

162178
QuotaUtil.QuotaCompareInfo quotaCompareInfo;
@@ -167,18 +183,17 @@ private void checkTotalVMQuota(String currentAccountUuid,
167183
quotaCompareInfo.quotaValue = totalVmNumQuota;
168184
quotaCompareInfo.currentUsed = totalVmNum;
169185
quotaCompareInfo.request = 1;
170-
new QuotaUtil().CheckQuota(quotaCompareInfo);
186+
return new QuotaUtil().checkQuotaAndReturn(quotaCompareInfo);
171187
}
172188

173189
@Transactional(readOnly = true)
174-
public void checkVmInstanceQuota(String currentAccountUuid,
175-
String resourceTargetOwnerAccountUuid,
176-
String vmInstanceUuid,
177-
Map<String, Quota.QuotaPair> pairs) {
190+
public ErrorCode checkVmInstanceQuotaWithResult(String currentAccountUuid,
191+
String resourceTargetOwnerAccountUuid,
192+
String vmInstanceUuid,
193+
Map<String, Quota.QuotaPair> pairs) {
178194
long vmNumQuota = pairs.get(VmQuotaConstant.VM_RUNNING_NUM).getValue();
179195

180196
VmQuotaUtil.VmQuota vmQuotaUsed = new VmQuotaUtil().getUsedVmCpuMemory(resourceTargetOwnerAccountUuid, null);
181-
//
182197
{
183198
QuotaUtil.QuotaCompareInfo quotaCompareInfo;
184199
quotaCompareInfo = new QuotaUtil.QuotaCompareInfo();
@@ -188,22 +203,38 @@ public void checkVmInstanceQuota(String currentAccountUuid,
188203
quotaCompareInfo.quotaValue = vmNumQuota;
189204
quotaCompareInfo.currentUsed = vmQuotaUsed.runningVmNum;
190205
quotaCompareInfo.request = 1;
191-
new QuotaUtil().CheckQuota(quotaCompareInfo);
206+
ErrorCode error = new QuotaUtil().checkQuotaAndReturn(quotaCompareInfo);
207+
if (error != null) {
208+
return error;
209+
}
192210
}
193-
//
194-
checkTotalVMQuota(currentAccountUuid,
211+
212+
ErrorCode error = checkTotalVMQuotaWithResult(currentAccountUuid,
195213
resourceTargetOwnerAccountUuid,
196214
vmInstanceUuid,
197215
pairs.get(VmQuotaConstant.VM_TOTAL_NUM).getValue(),
198216
vmQuotaUsed.totalVmNum);
199-
//
217+
if (error != null) {
218+
return error;
219+
}
220+
200221
VmInstanceVO vm = dbf.getEntityManager().find(VmInstanceVO.class, vmInstanceUuid);
201222

202-
checkVmCupAndMemoryCapacity(currentAccountUuid, resourceTargetOwnerAccountUuid, vm.getCpuNum(), vm.getMemorySize(), pairs);
223+
return checkVmCupAndMemoryCapacityWithResult(currentAccountUuid, resourceTargetOwnerAccountUuid, vm.getCpuNum(), vm.getMemorySize(), pairs);
224+
}
225+
226+
public void checkVmInstanceQuota(String currentAccountUuid,
227+
String resourceTargetOwnerAccountUuid,
228+
String vmInstanceUuid,
229+
Map<String, Quota.QuotaPair> pairs) {
230+
ErrorCode error = checkVmInstanceQuotaWithResult(currentAccountUuid, resourceTargetOwnerAccountUuid, vmInstanceUuid, pairs);
231+
if (error != null) {
232+
throw new ApiMessageInterceptionException(error);
233+
}
203234
}
204235

205236
@Transactional(readOnly = true)
206-
public void checkVmCupAndMemoryCapacity(String currentAccountUuid, String resourceTargetOwnerAccountUuid, long cpu, long memory, Map<String, Quota.QuotaPair> pairs) {
237+
public ErrorCode checkVmCupAndMemoryCapacityWithResult(String currentAccountUuid, String resourceTargetOwnerAccountUuid, long cpu, long memory, Map<String, Quota.QuotaPair> pairs) {
207238
VmQuotaUtil.VmQuota vmQuotaUsed = new VmQuotaUtil().getUsedVmCpuMemory(resourceTargetOwnerAccountUuid);
208239
long cpuNumQuota = pairs.get(VmQuotaConstant.VM_RUNNING_CPU_NUM).getValue();
209240
long memoryQuota = pairs.get(VmQuotaConstant.VM_RUNNING_MEMORY_SIZE).getValue();
@@ -217,7 +248,10 @@ public void checkVmCupAndMemoryCapacity(String currentAccountUuid, String resour
217248
quotaCompareInfo.quotaValue = cpuNumQuota;
218249
quotaCompareInfo.currentUsed = vmQuotaUsed.runningVmCpuNum;
219250
quotaCompareInfo.request = cpu;
220-
new QuotaUtil().CheckQuota(quotaCompareInfo);
251+
ErrorCode error = new QuotaUtil().checkQuotaAndReturn(quotaCompareInfo);
252+
if (error != null) {
253+
return error;
254+
}
221255
}
222256
{
223257
QuotaUtil.QuotaCompareInfo quotaCompareInfo;
@@ -228,7 +262,14 @@ public void checkVmCupAndMemoryCapacity(String currentAccountUuid, String resour
228262
quotaCompareInfo.quotaValue = memoryQuota;
229263
quotaCompareInfo.currentUsed = vmQuotaUsed.runningVmMemorySize;
230264
quotaCompareInfo.request = memory;
231-
new QuotaUtil().CheckQuota(quotaCompareInfo);
265+
return new QuotaUtil().checkQuotaAndReturn(quotaCompareInfo);
266+
}
267+
}
268+
269+
public void checkVmCupAndMemoryCapacity(String currentAccountUuid, String resourceTargetOwnerAccountUuid, long cpu, long memory, Map<String, Quota.QuotaPair> pairs) {
270+
ErrorCode error = checkVmCupAndMemoryCapacityWithResult(currentAccountUuid, resourceTargetOwnerAccountUuid, cpu, memory, pairs);
271+
if (error != null) {
272+
throw new ApiMessageInterceptionException(error);
232273
}
233274
}
234275

header/src/main/java/org/zstack/header/allocator/AllocateHostMsg.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class AllocateHostMsg extends NeedReplyMessage {
3131
private long oldMemoryCapacity = 0;
3232
private AllocationScene allocationScene;
3333
private String architecture;
34+
private String accountUuid;
3435

3536
public List<Set<String>> getOptionalPrimaryStorageUuids() {
3637
return optionalPrimaryStorageUuids;
@@ -211,4 +212,8 @@ public String getArchitecture() {
211212
public void setArchitecture(String architecture) {
212213
this.architecture = architecture;
213214
}
215+
216+
public String getAccountUuid() { return accountUuid; }
217+
218+
public void setAccountUuid(String accountUuid) { this.accountUuid = accountUuid; }
214219
}

header/src/main/java/org/zstack/header/allocator/HostAllocatorSpec.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public class HostAllocatorSpec {
3737
private long oldMemoryCapacity = 0;
3838
private AllocationScene allocationScene;
3939
private String architecture;
40+
private String accountUuid;
4041

4142
public AllocationScene getAllocationScene() {
4243
return allocationScene;
@@ -161,7 +162,13 @@ public List<String> getL3NetworkUuids() {
161162
}
162163
return l3NetworkUuids;
163164
}
165+
public String getAccountUuid() {
166+
return accountUuid;
167+
}
164168

169+
public void setAccountUuid(String accountUuid) {
170+
this.accountUuid = accountUuid;
171+
}
165172
public void setL3NetworkUuids(List<String> l3NetworkUuids) {
166173
this.l3NetworkUuids = l3NetworkUuids;
167174
}
@@ -250,6 +257,7 @@ public static HostAllocatorSpec fromAllocationMsg(AllocateHostMsg msg) {
250257
msg.getOptionalPrimaryStorageUuids().forEach(spec::addOptionalPrimaryStorageUuids);
251258
spec.setAllocationScene(msg.getAllocationScene());
252259
spec.setArchitecture(msg.getArchitecture());
260+
spec.setAccountUuid(msg.getAccountUuid());
253261
if (msg.getSystemTags() != null && !msg.getSystemTags().isEmpty()){
254262
spec.setSystemTags(new ArrayList<String>(msg.getSystemTags()));
255263
}

identity/src/main/java/org/zstack/identity/QuotaUtil.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,22 +72,31 @@ public String getResourceOwnerAccountUuid(String resourceUuid) {
7272
}
7373

7474
@Transactional(readOnly = true)
75-
public void CheckQuota(QuotaCompareInfo quotaCompareInfo) {
75+
public ErrorCode checkQuotaAndReturn(QuotaCompareInfo quotaCompareInfo) {
7676
logger.trace(String.format("dump quota QuotaCompareInfo: \n %s",
7777
JSONObjectUtil.toJsonString(quotaCompareInfo)));
7878
String accountName = Q.New(AccountVO.class)
7979
.select(AccountVO_.name)
8080
.eq(AccountVO_.uuid, quotaCompareInfo.resourceTargetOwnerAccountUuid)
8181
.findValue();
8282
if (quotaCompareInfo.currentUsed + quotaCompareInfo.request > quotaCompareInfo.quotaValue) {
83-
throw new ApiMessageInterceptionException(err(ORG_ZSTACK_IDENTITY_10002, IdentityErrors.QUOTA_EXCEEDING,
83+
return err(ORG_ZSTACK_IDENTITY_10002, IdentityErrors.QUOTA_EXCEEDING,
8484
"quota exceeding." +
8585
"The resource owner(or target resource owner) account[uuid: %s name: %s] exceeds a quota[name: %s, value: %s], " +
8686
"Current used:%s, Request:%s. Please contact the administrator.",
8787
quotaCompareInfo.resourceTargetOwnerAccountUuid, StringUtils.trimToEmpty(accountName),
8888
quotaCompareInfo.quotaName, quotaCompareInfo.quotaValue,
8989
quotaCompareInfo.currentUsed, quotaCompareInfo.request
90-
));
90+
);
91+
}
92+
93+
return null;
94+
}
95+
96+
public void CheckQuota(QuotaCompareInfo quotaCompareInfo) {
97+
ErrorCode error = checkQuotaAndReturn(quotaCompareInfo);
98+
if (error != null) {
99+
throw new ApiMessageInterceptionException(error);
91100
}
92101
}
93102

0 commit comments

Comments
 (0)