Visitar URL original
Move Api rate limit configurations from xml file to global · gitqueue/cloudstack@0b1e3a4 · GitHub
Skip to content

Commit 0b1e3a4

Browse files
committed
Move Api rate limit configurations from xml file to global
configuration, and also modify listCapabilitiesCmd to also return api limit interval and max for UI consumption.
1 parent 6d952a1 commit 0b1e3a4

7 files changed

Lines changed: 58 additions & 16 deletions

File tree

‎api/src/org/apache/cloudstack/api/command/user/config/ListCapabilitiesCmd.java‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ public void execute(){
5252
response.setProjectInviteRequired((Boolean)capabilities.get("projectInviteRequired"));
5353
response.setAllowUsersCreateProjects((Boolean)capabilities.get("allowusercreateprojects"));
5454
response.setDiskOffMaxSize((Long)capabilities.get("customDiskOffMaxSize"));
55+
response.setApiLimitInterval((Integer)capabilities.get("apiLimitInterval"));
56+
response.setApiLimitMax((Integer)capabilities.get("apiLimitMax"));
5557
response.setObjectName("capability");
5658
response.setResponseName(getCommandName());
5759
this.setResponseObject(response);

‎api/src/org/apache/cloudstack/api/response/CapabilitiesResponse.java‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ public class CapabilitiesResponse extends BaseResponse {
4646
"create disk from disk offering with custom size")
4747
private Long diskOffMaxSize;
4848

49+
@SerializedName("apilimitinterval") @Param(description="time interval (in seconds) to reset api count")
50+
private Integer apiLimitInterval;
51+
52+
@SerializedName("apilimitmax") @Param(description="Max allowed number of api requests within the specified interval")
53+
private Integer apiLimitMax;
54+
4955

5056
public void setSecurityGroupsEnabled(boolean securityGroupsEnabled) {
5157
this.securityGroupsEnabled = securityGroupsEnabled;
@@ -75,4 +81,13 @@ public void setDiskOffMaxSize(Long diskOffMaxSize) {
7581
this.diskOffMaxSize = diskOffMaxSize;
7682
}
7783

84+
public void setApiLimitInterval(Integer apiLimitInterval) {
85+
this.apiLimitInterval = apiLimitInterval;
86+
}
87+
88+
public void setApiLimitMax(Integer apiLimitMax) {
89+
this.apiLimitMax = apiLimitMax;
90+
}
91+
92+
7893
}

‎plugins/api/rate-limit/src/org/apache/cloudstack/ratelimit/ApiRateLimitServiceImpl.java‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,13 @@
2929
import org.apache.log4j.Logger;
3030

3131
import org.apache.cloudstack.acl.APIChecker;
32+
import org.apache.cloudstack.api.ApiConstants.LDAPParams;
3233
import org.apache.cloudstack.api.command.admin.ratelimit.ResetApiLimitCmd;
3334
import org.apache.cloudstack.api.command.user.ratelimit.GetApiLimitCmd;
3435
import org.apache.cloudstack.api.response.ApiLimitResponse;
3536

37+
import com.cloud.configuration.Config;
38+
import com.cloud.configuration.dao.ConfigurationDao;
3639
import com.cloud.exception.PermissionDeniedException;
3740
import com.cloud.exception.RequestLimitException;
3841
import com.cloud.user.Account;
@@ -61,29 +64,29 @@ public class ApiRateLimitServiceImpl extends AdapterBase implements APIChecker,
6164
@Inject
6265
AccountService _accountService;
6366

67+
@Inject
68+
ConfigurationDao _configDao;
69+
6470
@Override
6571
public boolean configure(String name, Map<String, Object> params) throws ConfigurationException {
6672
super.configure(name, params);
6773

6874
if (_store == null) {
69-
// not configured yet, note that since this class is both adapter
70-
// and pluggableService, so this method
71-
// may be invoked twice in ComponentLocator.
7275
// get global configured duration and max values
73-
Object duration = params.get("api.throttling.interval");
76+
String duration = _configDao.getValue(Config.ApiLimitInterval.key());
7477
if (duration != null) {
75-
timeToLive = Integer.parseInt((String) duration);
78+
timeToLive = Integer.parseInt(duration);
7679
}
77-
Object maxReqs = params.get("api.throttling.max");
80+
String maxReqs = _configDao.getValue(Config.ApiLimitMax.key());
7881
if (maxReqs != null) {
79-
maxAllowed = Integer.parseInt((String) maxReqs);
82+
maxAllowed = Integer.parseInt(maxReqs);
8083
}
8184
// create limit store
8285
EhcacheLimitStore cacheStore = new EhcacheLimitStore();
8386
int maxElements = 10000;
84-
Object cachesize = params.get("api.throttling.cachesize");
87+
String cachesize = _configDao.getValue(Config.ApiLimitCacheSize.key());
8588
if ( cachesize != null ){
86-
maxElements = Integer.parseInt((String)cachesize);
89+
maxElements = Integer.parseInt(cachesize);
8790
}
8891
CacheManager cm = CacheManager.create();
8992
Cache cache = new Cache("api-limit-cache", maxElements, false, false, timeToLive, timeToLive);

‎plugins/api/rate-limit/test/org/apache/cloudstack/ratelimit/ApiRateLimitTest.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
import org.junit.BeforeClass;
3030
import org.junit.Test;
3131

32+
import com.cloud.configuration.Config;
33+
import com.cloud.configuration.dao.ConfigurationDao;
3234
import com.cloud.exception.RequestLimitException;
3335
import com.cloud.user.Account;
3436
import com.cloud.user.AccountService;
@@ -43,12 +45,18 @@ public class ApiRateLimitTest {
4345

4446
static ApiRateLimitServiceImpl _limitService = new ApiRateLimitServiceImpl();
4547
static AccountService _accountService = mock(AccountService.class);
48+
static ConfigurationDao _configDao = mock(ConfigurationDao.class);
4649
private static long acctIdSeq = 5L;
4750
private static Account testAccount;
4851

4952
@BeforeClass
5053
public static void setUp() throws ConfigurationException {
5154

55+
when(_configDao.getValue(Config.ApiLimitInterval.key())).thenReturn(null);
56+
when(_configDao.getValue(Config.ApiLimitMax.key())).thenReturn(null);
57+
when(_configDao.getValue(Config.ApiLimitCacheSize.key())).thenReturn(null);
58+
_limitService._configDao = _configDao;
59+
5260
_limitService.configure("ApiRateLimitTest", Collections.<String, Object> emptyMap());
5361

5462
_limitService._accountService = _accountService;

‎server/src/com/cloud/configuration/Config.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,12 +358,16 @@ public enum Config {
358358
ConcurrentSnapshotsThresholdPerHost("Advanced", ManagementServer.class, Long.class, "concurrent.snapshots.threshold.perhost",
359359
null, "Limits number of snapshots that can be handled by the host concurrently; default is NULL - unlimited", null),
360360
NetworkIPv6SearchRetryMax("Network", ManagementServer.class, Integer.class, "network.ipv6.search.retry.max", "10000", "The maximum number of retrying times to search for an available IPv6 address in the table", null),
361-
361+
362362
ExternalBaremetalSystemUrl("Advanced", ManagementServer.class, String.class, "external.baremetal.system.url", null, "url of external baremetal system that CloudStack will talk to", null),
363363
ExternalBaremetalResourceClassName("Advanced", ManagementServer.class, String.class, "external,baremetal.resource.classname", null, "class name for handling external baremetal resource", null),
364364
EnableBaremetalSecurityGroupAgentEcho("Advanced", ManagementServer.class, Boolean.class, "enable.baremetal.securitygroup.agent.echo", "false", "After starting provision process, periodcially echo security agent installed in the template. Treat provisioning as success only if echo successfully", null),
365365
IntervalToEchoBaremetalSecurityGroupAgent("Advanced", ManagementServer.class, Integer.class, "interval.baremetal.securitygroup.agent.echo", "10", "Interval to echo baremetal security group agent, in seconds", null),
366-
TimeoutToEchoBaremetalSecurityGroupAgent("Advanced", ManagementServer.class, Integer.class, "timeout.baremetal.securitygroup.agent.echo", "3600", "Timeout to echo baremetal security group agent, in seconds, the provisioning process will be treated as a failure", null);
366+
TimeoutToEchoBaremetalSecurityGroupAgent("Advanced", ManagementServer.class, Integer.class, "timeout.baremetal.securitygroup.agent.echo", "3600", "Timeout to echo baremetal security group agent, in seconds, the provisioning process will be treated as a failure", null),
367+
368+
ApiLimitInterval("Advanced", ManagementServer.class, Integer.class, "api.throttling.interval", "1", "Time interval (in seconds) to reset API count", null),
369+
ApiLimitMax("Advanced", ManagementServer.class, Integer.class, "api.throttling.max", "25", "Max allowed number of APIs within fixed interval", null),
370+
ApiLimitCacheSize("Advanced", ManagementServer.class, Integer.class, "api.throttling.cachesize", "50000", "Account based API count cache size", null);
367371

368372
private final String _category;
369373
private final Class<?> _componentClass;

‎server/src/com/cloud/server/ManagementServerImpl.java‎

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
// to you under the Apache License, Version 2.0 (the
66
// "License"); you may not use this file except in compliance
77
// with the License. You may obtain a copy of the License at
8-
//
8+
//
99
// http://www.apache.org/licenses/LICENSE-2.0
1010
//
1111
// Unless required by applicable law or agreed to in writing,
@@ -375,7 +375,7 @@ public class ManagementServerImpl extends ManagerBase implements ManagementServe
375375
@Inject
376376
S3Manager _s3Mgr;
377377

378-
/*
378+
/*
379379
@Inject
380380
ComponentContext _forceContextRef; // create a dependency to ComponentContext so that it can be loaded beforehead
381381
@@ -417,14 +417,14 @@ public boolean configure(String name, Map<String, Object> params)
417417
for (String id : availableIds) {
418418
_availableIdsMap.put(id, true);
419419
}
420-
420+
421421
return true;
422422
}
423-
423+
424424
@Override
425425
public boolean start() {
426426
s_logger.info("Startup CloudStack management server...");
427-
427+
428428
enableAdminUser("password");
429429
return true;
430430
}
@@ -2187,6 +2187,10 @@ public Map<String, Object> listCapabilities(ListCapabilitiesCmd cmd) {
21872187

21882188
String userPublicTemplateEnabled = _configs.get(Config.AllowPublicUserTemplates.key());
21892189

2190+
// add some parameters UI needs to handle API throttling
2191+
Integer apiLimitInterval = Integer.valueOf(_configDao.getValue(Config.ApiLimitInterval.key()));
2192+
Integer apiLimitMax = Integer.valueOf(_configDao.getValue(Config.ApiLimitMax.key()));
2193+
21902194
capabilities.put("securityGroupsEnabled", securityGroupsEnabled);
21912195
capabilities
21922196
.put("userPublicTemplateEnabled", (userPublicTemplateEnabled == null || userPublicTemplateEnabled.equals("false") ? false : true));
@@ -2195,6 +2199,8 @@ public Map<String, Object> listCapabilities(ListCapabilitiesCmd cmd) {
21952199
capabilities.put("projectInviteRequired", _projectMgr.projectInviteRequired());
21962200
capabilities.put("allowusercreateprojects", _projectMgr.allowUserToCreateProject());
21972201
capabilities.put("customDiskOffMaxSize", diskOffMaxSize);
2202+
capabilities.put("apiLimitInterval", apiLimitInterval);
2203+
capabilities.put("apiLimitMax", apiLimitMax);
21982204

21992205
return capabilities;
22002206
}

‎setup/db/db/schema-40to410.sql‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,10 @@ UPDATE `cloud`.`counter` set uuid=id WHERE uuid is NULL;
146146
UPDATE `cloud`.`conditions` set uuid=id WHERE uuid is NULL;
147147

148148
INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Advanced', 'DEFAULT', 'management-server', '"detail.batch.query.size"', '2000', 'Default entity detail batch query size for listing');
149+
INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Advanced', 'DEFAULT', 'management-server', '"api.throttling.interval"', '1', 'Time interval (in seconds) to reset API count');
150+
INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Advanced', 'DEFAULT', 'management-server', '"api.throttling.max"', '25', 'Max allowed number of APIs within fixed interval');
151+
INSERT IGNORE INTO `cloud`.`configuration` VALUES ('Advanced', 'DEFAULT', 'management-server', '"api.throttling.cachesize"', '50000', 'Account based API count cache size');
152+
149153

150154
-- DB views for list api
151155

0 commit comments

Comments
 (0)