Skip to content

Commit 5d60ce3

Browse files
committed
bug 12167: ResourceLimits: load limit related values from the cloud.configuration DB only on the management server start.
Reviewed by : Abhi.
1 parent a94cd0b commit 5d60ce3

1 file changed

Lines changed: 29 additions & 39 deletions

File tree

‎server/src/com/cloud/resourcelimit/ResourceLimitManagerImpl.java‎

100644100755
Lines changed: 29 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package com.cloud.resourcelimit;
1919

2020
import java.util.ArrayList;
21+
import java.util.EnumMap;
2122
import java.util.List;
2223
import java.util.Map;
2324
import java.util.Set;
@@ -115,7 +116,9 @@ public class ResourceLimitManagerImpl implements ResourceLimitService, Manager{
115116
protected SearchBuilder<ResourceCountVO> ResourceCountSearch;
116117
ScheduledExecutorService _rcExecutor;
117118
long _resourceCountCheckInterval=0;
118-
119+
Map<ResourceType, Long> accountResourceLimitMap = new EnumMap<ResourceType, Long>(ResourceType.class);
120+
Map<ResourceType, Long> projectResourceLimitMap = new EnumMap<ResourceType, Long>(ResourceType.class);
121+
119122
@Override
120123
public String getName() {
121124
return _name;
@@ -148,6 +151,19 @@ public boolean configure(final String name, final Map<String, Object> params) th
148151
if (_resourceCountCheckInterval > 0){
149152
_rcExecutor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("ResourceCountChecker"));
150153
}
154+
155+
projectResourceLimitMap.put(Resource.ResourceType.public_ip, Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectPublicIPs.key())));
156+
projectResourceLimitMap.put(Resource.ResourceType.snapshot, Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectSnapshots.key())));
157+
projectResourceLimitMap.put(Resource.ResourceType.template, Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectTemplates.key())));
158+
projectResourceLimitMap.put(Resource.ResourceType.user_vm, Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectUserVms.key())));
159+
projectResourceLimitMap.put(Resource.ResourceType.volume, Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectVolumes.key())));
160+
161+
accountResourceLimitMap.put(Resource.ResourceType.public_ip, Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountPublicIPs.key())));
162+
accountResourceLimitMap.put(Resource.ResourceType.snapshot, Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountSnapshots.key())));
163+
accountResourceLimitMap.put(Resource.ResourceType.template, Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountTemplates.key())));
164+
accountResourceLimitMap.put(Resource.ResourceType.user_vm, Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountUserVms.key())));
165+
accountResourceLimitMap.put(Resource.ResourceType.volume, Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountVolumes.key())));
166+
151167
return true;
152168
}
153169

@@ -183,50 +199,24 @@ public void decrementResourceCount(long accountId, ResourceType type, Long... de
183199

184200
@Override
185201
public long findCorrectResourceLimitForAccount(Account account, ResourceType type) {
186-
long max = -1;
187-
202+
203+
long max = -1; //if resource limit is not found, then we treat it as unlimited
188204
ResourceLimitVO limit = _resourceLimitDao.findByOwnerIdAndType(account.getId(), ResourceOwnerType.Account, type);
189205

190206
// Check if limit is configured for account
191207
if (limit != null) {
192208
max = limit.getMax().longValue();
193209
} else {
194-
// If the account has an no limit set, then return global default account limits
195-
try {
196-
if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
197-
if (type == Resource.ResourceType.public_ip) {
198-
max = Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectPublicIPs.key()));
199-
} else if (type == ResourceType.snapshot) {
200-
max = Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectSnapshots.key()));
201-
} else if (type == ResourceType.template) {
202-
max = Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectTemplates.key()));
203-
} else if (type == ResourceType.user_vm) {
204-
max = Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectUserVms.key()));
205-
} else if (type == ResourceType.volume) {
206-
max = Long.parseLong(_configDao.getValue(Config.DefaultMaxProjectVolumes.key()));
207-
} else {
208-
//if resource limit is not supported, then we treat it as unlimited
209-
return -1;
210-
}
211-
} else {
212-
if (type == Resource.ResourceType.public_ip) {
213-
max = Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountPublicIPs.key()));
214-
} else if (type == ResourceType.snapshot) {
215-
max = Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountSnapshots.key()));
216-
} else if (type == ResourceType.template) {
217-
max = Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountTemplates.key()));
218-
} else if (type == ResourceType.user_vm) {
219-
max = Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountUserVms.key()));
220-
} else if (type == ResourceType.volume) {
221-
max = Long.parseLong(_configDao.getValue(Config.DefaultMaxAccountVolumes.key()));
222-
} else {
223-
//if resource limit is not supported, then we treat it as unlimited
224-
return -1;
225-
}
226-
}
227-
} catch (NumberFormatException nfe) {
228-
s_logger.error("Invalid value is set for the default account limit.");
229-
}
210+
// If the account has an no limit set, then return global default account limits
211+
Long value = null;
212+
if (account.getType() == Account.ACCOUNT_TYPE_PROJECT) {
213+
value = projectResourceLimitMap.get(type);
214+
} else {
215+
value = accountResourceLimitMap.get(type);
216+
}
217+
if (value != null){
218+
return value;
219+
}
230220
}
231221

232222
return max;

0 commit comments

Comments
 (0)