Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Add Burst control capability for Applications #12162

Merged
merged 16 commits into from
Jan 5, 2024
113 changes: 78 additions & 35 deletions .github/workflows/main-forks.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
name: API Manager Build with Test - Forks
name: API Manager Build with Test

on:
push:
paths-ignore:
- '**/org.wso2.carbon.apimgt.admin.feature/src/main/resources/admin/**'
- '**/org.wso2.carbon.apimgt.publisher.feature/src/main/resources/publisher/**'
- '**/org.wso2.carbon.apimgt.store.feature/src/main/resources/devportal/**'

branches:
- master
pull_request:
branches:
- master
workflow_dispatch:

permissions:
contents: read # to fetch code (actions/checkout)
concurrency:
group: ${{ github.event.number }}
cancel-in-progress: true
jobs:
build-carbon:
runs-on: ubuntu-latest
Expand All @@ -23,11 +28,11 @@ jobs:
uses: actions/checkout@v3
with:
fetch-depth: '10'
- name: Set up JDK 1.8
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: 8.0.282+8
distribution: 'adopt'
java-version: 11.0.16+8
distribution: 'temurin'
- uses: actions/setup-node@v3
with:
node-version: '14.x'
Expand All @@ -37,24 +42,24 @@ jobs:
path: ~/.m2
key: ${{ runner.os }}-m2
restore-keys: ${{ runner.os }}-m2

- name: Build carbon-apimgt with Tests, skipping AspectJ
run: mvn clean install --file pom.xml -Dskip.aspectj=true

- name: Upload unit test coverage to Codecov
uses: codecov/codecov-action@v3
with:
flags: unit_tests

build-product:
runs-on: ubuntu-latest
strategy:
matrix:
include:
- id: 1
segment: apim-integration-tests-api-common,apim-integration-tests-api-change-endpoint,apim-integration-tests-api-product,apim-integration-tests-api-lifecycle,apim-integration-tests-api-lifecycle-2
segment: group1
- id: 2
segment: apim-email-secondary-userstore-tests,apim-CORS-tests,apim-publisher-tests
- id: 3
segment: apim-integration-tests-samples,apim-store-tests,apim-grant-type-token-tests,apim-integration-tests-graphql,admin-rest-api-tests,rest-api-tests,apim-mediation-tests,apim-integration-tests-without-restarts,apim-integration-tests-without-advance-throttling,apim-urlsafe-JWT-integration-tests
- id: 4
segment: apim-streaming-api-tests,apim-JWT-integration-tests,apim-integration-tests-external-idp,apim-integration-tests-workflow
- id: 5
segment: apim-websocket-tests,apim-integration-tests-application-sharing,apim-integration-tests-endpoint-security,apim-integration-emailusername-login
segment: group2
fail-fast: false
steps:
- name: Run hostname
Expand All @@ -67,11 +72,11 @@ jobs:
uses: actions/checkout@v3
with:
fetch-depth: '10'
- name: Set up JDK 1.8
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: 8.0.282+8
distribution: 'adopt'
java-version: 11.0.16+8
distribution: 'temurin'
- uses: actions/setup-node@v3
with:
node-version: '14.x'
Expand All @@ -91,14 +96,19 @@ jobs:
- name: Checkout Product-APIM
uses: actions/checkout@v3
with:
repository: wso2/product-apim
ref: refs/heads/master
repository: shilmyhasan/product-apim-1
ref: refs/heads/master-testnew
fetch-depth: '10'
path: product-apim
- name: Build product-apim with tests
run: mvn clean install -Dcarbon.apimgt.version=$(cat ../CARBON_APIMGT_VERSION_FILE) -fae --file product-apim/pom.xml
run: mvn clean install -Dcarbon.apimgt.version=$(cat ../CARBON_APIMGT_VERSION_FILE) -fae --file product-apim/pom.xml -DskipBenchMarkTest=true -DskipRestartTests=true
env:
PRODUCT_APIM_TESTS: ${{ matrix.segment }}
PRODUCT_APIM_TEST_GROUPS: ${{ matrix.segment }}
- name: Publish Test Report
if: always()
uses: malinthaprasan/action-surefire-report@v1
with:
report_paths: 'product-apim/modules/integration/tests-integration/tests-backend/target/surefire-reports/TEST-TestSuite.xml'
- name: Prepare upload testng report
if: always()
run: mv product-apim/modules/integration/tests-integration/tests-backend/target/surefire-reports/TEST-TestSuite.xml TEST-TestSuite_${{ matrix.id }}.xml
Expand All @@ -110,17 +120,50 @@ jobs:
path: |
TEST-TestSuite_${{ matrix.id }}.xml
if-no-files-found: warn

show-report:
if: ${{ always() && !cancelled() }}
needs: build-product
runs-on: 'ubuntu-latest'
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
with:
flags: integration_tests
run-benchmark-test:
runs-on: ubuntu-latest
steps:
- uses: actions/download-artifact@v2
- name: Run hostname
run: hostname
- name: Fix host entry
run: sudo echo "127.0.0.1 $(hostname)" | sudo tee -a /etc/hosts
- name: Show hosts
run: cat /etc/hosts
- name: Checkout master
uses: actions/checkout@v3
with:
path: reports
- name: Publish Test Report
if: always()
uses: scacap/action-surefire-report@v1
fetch-depth: '10'
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
report_paths: 'reports/**/TEST-*.xml'
java-version: 11.0.16+8
distribution: 'temurin'
- uses: actions/setup-node@v3
with:
node-version: '14.x'
- name: Cache maven packages
uses: actions/cache@v3
with:
path: ~/.m2
key: ${{ runner.os }}-m2
restore-keys: ${{ runner.os }}-m2
- name: Build carbon-apimgt without tests, skip AspectJ, skip portal builds
run: mvn clean install --file pom.xml -Dmaven.test.skip=true -Dskip.aspectj=true -Dnpm.build.skip=true
- name: Get carbon.apimgt.version
run: mvn help:evaluate -Dexpression=project.version -q -DforceStdout > ../CARBON_APIMGT_VERSION_FILE
- name: Print carbon.apimgt.version
run: |
echo $(cat ../CARBON_APIMGT_VERSION_FILE)
- name: Checkout Product-APIM
uses: actions/checkout@v3
with:
repository: shilmyhasan/product-apim-1
ref: refs/heads/master-testnew
fetch-depth: '10'
path: product-apim
- name: Build product-apim with tests
run: mvn clean install -Dcarbon.apimgt.version=$(cat ../CARBON_APIMGT_VERSION_FILE) -fae --file product-apim/pom.xml -DskipIntegrationTests
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.wso2.carbon.apimgt.api.model.policy;

public class ApplicationPolicy extends Policy {
private int rateLimitCount;
private String rateLimitTimeUnit;
private int applicationId;

private byte[] customAttributes;
Expand All @@ -43,10 +45,24 @@
this.customAttributes = customAttributes;
}

public String getRateLimitTimeUnit() {
return rateLimitTimeUnit;

Check warning on line 49 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/policy/ApplicationPolicy.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/policy/ApplicationPolicy.java#L49

Added line #L49 was not covered by tests
}
public void setRateLimitTimeUnit(String rateLimitTimeUnit) {
this.rateLimitTimeUnit = rateLimitTimeUnit;
}

Check warning on line 53 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/policy/ApplicationPolicy.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/policy/ApplicationPolicy.java#L52-L53

Added lines #L52 - L53 were not covered by tests
public int getRateLimitCount() {
return rateLimitCount;

Check warning on line 55 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/policy/ApplicationPolicy.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/policy/ApplicationPolicy.java#L55

Added line #L55 was not covered by tests
}
public void setRateLimitCount(int rateLimitCount) {
this.rateLimitCount = rateLimitCount;
}

Check warning on line 59 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/policy/ApplicationPolicy.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/policy/ApplicationPolicy.java#L58-L59

Added lines #L58 - L59 were not covered by tests

@Override
public String toString() {
return "ApplicationPolicy [policyName=" + getPolicyName()
+ ", applicationId =" + applicationId + ", description=" + getDescription() + ", defaultQuotaPolicy="
+ getDefaultQuotaPolicy() + "]";
+ getDefaultQuotaPolicy() + ", rateLimitCount=" + getRateLimitCount() + ", rateLimitTimeUnit="
+ getRateLimitTimeUnit() + "]";

Check warning on line 66 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/policy/ApplicationPolicy.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/policy/ApplicationPolicy.java#L65-L66

Added lines #L65 - L66 were not covered by tests
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,28 @@
*/
public class ApplicationPolicy extends Policy {

private int rateLimitCount = -1;
private String rateLimitTimeUnit = null;

Check warning on line 27 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/ApplicationPolicy.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/ApplicationPolicy.java#L26-L27

Added lines #L26 - L27 were not covered by tests

@Override
public String getCacheKey() {

return POLICY_TYPE.APPLICATION + DELEM_PERIOD + super.getCacheKey();
}

public String getRateLimitTimeUnit() {
return rateLimitTimeUnit;

Check warning on line 36 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/ApplicationPolicy.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/ApplicationPolicy.java#L36

Added line #L36 was not covered by tests
}

public void setRateLimitTimeUnit(String rateLimitTimeUnit) {
this.rateLimitTimeUnit = rateLimitTimeUnit;
}

Check warning on line 41 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/ApplicationPolicy.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/ApplicationPolicy.java#L40-L41

Added lines #L40 - L41 were not covered by tests

public int getRateLimitCount() {
return rateLimitCount;

Check warning on line 44 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/ApplicationPolicy.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/ApplicationPolicy.java#L44

Added line #L44 was not covered by tests
}

public void setRateLimitCount(int rateLimitCount) {
this.rateLimitCount = rateLimitCount;
}

Check warning on line 49 in components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/ApplicationPolicy.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.api/src/main/java/org/wso2/carbon/apimgt/api/model/subscription/ApplicationPolicy.java#L48-L49

Added lines #L48 - L49 were not covered by tests
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public enum Throttling implements FaultSubCategory {
BLOCKED,
CUSTOM_POLICY_LIMIT_EXCEEDED,
BURST_CONTROL_LIMIT_EXCEEDED,
APPLICATION_BURST_CONTROL_LIMIT_EXCEEDED,
QUERY_TOO_DEEP,
QUERY_TOO_COMPLEX,
OTHER
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@
return FaultSubCategories.Throttling.CUSTOM_POLICY_LIMIT_EXCEEDED;
case APIThrottleConstants.SUBSCRIPTION_BURST_THROTTLE_OUT_ERROR_CODE:
return FaultSubCategories.Throttling.BURST_CONTROL_LIMIT_EXCEEDED;
case APIThrottleConstants.APPLICATION_BURST_THROTTLE_OUT_ERROR_CODE:
return FaultSubCategories.Throttling.APPLICATION_BURST_CONTROL_LIMIT_EXCEEDED;

Check warning on line 113 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/FaultCodeClassifier.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/analytics/FaultCodeClassifier.java#L113

Added line #L113 was not covered by tests
case APIThrottleConstants.GRAPHQL_QUERY_TOO_DEEP:
case WebSocketApiConstants.FrameErrorConstants.GRAPHQL_QUERY_TOO_DEEP:
return FaultSubCategories.Throttling.QUERY_TOO_DEEP;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
private String subscriber;
private List<String> throttlingDataList;
private int spikeArrestLimit;
private int applicationSpikesArrestLimit;
private String subscriberTenantDomain;
private String spikeArrestUnit;
private boolean stopOnQuotaReach;
Expand Down Expand Up @@ -180,6 +181,23 @@
this.consumerKey = consumerKey;
}

public String getApplicationSpikesArrestUnit() {
return applicationSpikesArrestUnit;

Check warning on line 185 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/AuthenticationContext.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/AuthenticationContext.java#L185

Added line #L185 was not covered by tests
}

public void setApplicationSpikesArrestUnit(String applicationSpikesArrestUnit) {
this.applicationSpikesArrestUnit = applicationSpikesArrestUnit;
}

private String applicationSpikesArrestUnit;

public int getApplicationSpikesArrestLimit(){
return applicationSpikesArrestLimit;
}
public void setApplicationSpikesArrestLimit(int applicationSpikesArrestLimit) {
this.applicationSpikesArrestLimit = applicationSpikesArrestLimit;
}

public int getSpikeArrestLimit() {
return spikeArrestLimit;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@
authContext.setSubscriberTenantDomain(info.getSubscriberTenantDomain());
authContext.setSpikeArrestLimit(info.getSpikeArrestLimit());
authContext.setSpikeArrestUnit(info.getSpikeArrestUnit());
authContext.setApplicationSpikesArrestLimit(info.getApplicationSpikeArrestLimit());
authContext.setApplicationSpikesArrestUnit(info.getApplicationSpikeArrestUnit());

Check warning on line 356 in components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/oauth/OAuthAuthenticator.java

View check run for this annotation

Codecov / codecov/patch

components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/handlers/security/oauth/OAuthAuthenticator.java#L355-L356

Added lines #L355 - L356 were not covered by tests
authContext.setStopOnQuotaReach(info.isStopOnQuotaReach());
authContext.setIsContentAware(info.isContentAware());
APISecurityUtils.setAuthenticationContext(synCtx, authContext, securityContextHeader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class APIThrottleConstants {
public static final int APPLICATION_THROTTLE_OUT_ERROR_CODE = 900803;
public static final int SUBSCRIPTION_THROTTLE_OUT_ERROR_CODE = 900804;
public static final int SUBSCRIPTION_BURST_THROTTLE_OUT_ERROR_CODE = 900807;
public static final int APPLICATION_BURST_THROTTLE_OUT_ERROR_CODE = 900809;
public static final int BLOCKED_ERROR_CODE = 900805;
public static final int CUSTOM_POLICY_THROTTLE_OUT_ERROR_CODE = 900806;
public static final int CONNECTIONS_COUNT_THROTTLE_OUT_ERROR_CODE = 900808;
Expand All @@ -47,6 +48,8 @@ public class APIThrottleConstants {
public static final String HARD_THROTTLING_CONFIGURATION = "hard_throttling_limits";
public static final String PRODUCTION_HARD_LIMIT = "PRODUCTION_HARD_LIMIT";
public static final String SUBSCRIPTION_BURST_LIMIT = "SUBSCRIPTION_BURST_LIMIT";
public static final String APPLICATION_BURST_LIMIT = "APPLICATION_BURST_LIMIT";
public static final String APPLICATION_BURST_LIMIT_EXCEEDED = "APPLICATION_BURST_LIMIT_EXCEED";
public static final String SANDBOX_HARD_LIMIT = "SANDBOX_HARD_LIMIT";
public static final String THROTTLED_OUT_REASON = APIConstants.THROTTLE_OUT_REASON_KEY;
public static final String THROTTLED_NEXT_ACCESS_TIMESTAMP = "NEXT_ACCESS_TIME";
Expand Down
Loading
Loading