Skip to content

Commit

Permalink
Merge pull request #12474 from msm1992/master-2946
Browse files Browse the repository at this point in the history
Make ThrottleDataHolder a singleton object
  • Loading branch information
msm1992 committed Jun 27, 2024
2 parents 0f156d4 + 3758c3c commit 5418cc4
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public class ServiceReferenceHolder {

private ConfigurationContextService cfgCtxService;
private APIManagerConfigurationService amConfigService;
public ThrottleDataHolder throttleDataHolder;
private final ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
private ThrottleProperties throttleProperties;
private ConfigurationContext axis2ConfigurationContext;
private TracingService tracingService;
Expand Down Expand Up @@ -100,9 +100,7 @@ public class ServiceReferenceHolder {

private Set<String> activeTenants = new ConcurrentSkipListSet<>();
private JedisPool redisPool;
public void setThrottleDataHolder(ThrottleDataHolder throttleDataHolder) {
this.throttleDataHolder = throttleDataHolder;
}

public ThrottleDataHolder getThrottleDataHolder() {
return throttleDataHolder;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,11 @@ public void completedServerStartup() {
// This prevents errors in an All in one setup caused by the ThrottleDataPublisher trying to connect to the
// event receiver, before the event receiver has been started on completion of server startup.
ServiceReferenceHolder.getInstance().setThrottleDataPublisher(new ThrottleDataPublisher());
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
APIThrottleDataServiceImpl throttleDataServiceImpl =
new APIThrottleDataServiceImpl(throttleDataHolder);
APIThrottleDataServiceImpl throttleDataServiceImpl = new APIThrottleDataServiceImpl();
CacheInvalidationService cacheInvalidationService = new CacheInvalidationServiceImpl();
// Register APIThrottleDataService so that ThrottleData maps are available to other components.
ServiceReferenceHolder.getInstance().setCacheInvalidationService(cacheInvalidationService);
ServiceReferenceHolder.getInstance().setAPIThrottleDataService(throttleDataServiceImpl);
ServiceReferenceHolder.getInstance().setThrottleDataHolder(throttleDataHolder);
ServiceReferenceHolder.getInstance().setRevokedTokenService(new RevokedTokenDataImpl());
SubscriptionsDataService subscriptionsDataService = new SubscriptionsDataServiceImpl();
ServiceReferenceHolder.getInstance().setSubscriptionsDataService(subscriptionsDataService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,7 @@
*/
public class APIThrottleDataServiceImpl implements APIThrottleDataService {

private ThrottleDataHolder throttleDataHolder;

public APIThrottleDataServiceImpl(ThrottleDataHolder throttleDataHolder) {

this.throttleDataHolder = throttleDataHolder;
}
private ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

@Override
public void addThrottledApiConditions(String resourceKey, String name,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ public void addThrottledAPIKey(String key, Long value){
throttledAPIKeysMap.put(key,value);
}

private static final ThrottleDataHolder instance = new ThrottleDataHolder();

private ThrottleDataHolder() {

}

public static ThrottleDataHolder getInstance() {
return instance;
}

public void addThrottledApiConditions(String key, String conditionKey, List<ConditionDto> conditionValue) {

Map<String, List<ConditionDto>> conditionMap;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ public void init() {

@Test
public void testDoNotThrottleWhenMsgIsAResponseAndAuthCtxNotAvailable() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);

Expand All @@ -133,7 +133,7 @@ public void testDoNotThrottleWhenMsgIsAResponseAndAuthCtxNotAvailable() {

@Test
public void testSubscriptionLevelThrottlingInitWhenThrottleCtxIsNull() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);

Expand All @@ -145,7 +145,7 @@ public void testSubscriptionLevelThrottlingInitWhenThrottleCtxIsNull() {

@Test
public void testSubscriptionLevelThrottlingInitialization() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);

Expand All @@ -160,7 +160,7 @@ public void testSubscriptionLevelThrottlingInitialization() {

@Test
public void testMsgThrottleOutWhenBlockingConditionsAreSatisfied() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);

Expand All @@ -181,7 +181,7 @@ public void testMsgThrottleOutWhenBlockingConditionsAreSatisfied() {

@Test
public void testMsgThrottleContinueWhenAPITierIsNotAvailable() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);

Expand All @@ -196,7 +196,7 @@ public void testMsgThrottleContinueWhenAPITierIsNotAvailable() {

@Test
public void testMsgDoContinueWhenAllThrottlingLevelsAreNotThrolled() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
ServiceReferenceHolder.getInstance().setThrottleDataPublisher(new ThrottleDataPublisher());
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
Expand All @@ -214,7 +214,7 @@ public void testMsgDoContinueWhenAllThrottlingLevelsAreNotThrolled() {

@Test
public void testMsgDoThrottleWhenUserLevelThrottlingIsTriggerred() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
Expand All @@ -233,7 +233,7 @@ public void testMsgDoThrottleWhenUserLevelThrottlingIsTriggerred() {

@Test
public void testMsgThrottleOutWhenAPILevelIsThrottled() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
Expand Down Expand Up @@ -261,7 +261,7 @@ public void testMsgThrottleOutWhenAPILevelIsThrottled() {

@Test
public void testMsgThrottleOutWhenResourceLevelIsThrottled() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
Expand Down Expand Up @@ -289,7 +289,7 @@ public void testMsgThrottleOutWhenResourceLevelIsThrottled() {

@Test
public void testMsgThrottleOutWhenSubscriptionLevelIsThrottledAndStopOnQuotaReachIsEnabled() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
Expand All @@ -311,12 +311,13 @@ public void testMsgThrottleOutWhenSubscriptionLevelIsThrottledAndStopOnQuotaReac
//Should throttle out and discontinue message flow, when subscription level is throttled out
//and stop on quota reach is enabled
Assert.assertFalse(throttleHandler.handleRequest(messageContext));
throttleDataHolder.removeThrottleData(subscriptionLevelThrottleKey);

}

@Test
public void testMsgContinueWhenSubscriptionLevelIsThrottledAndStopOnQuotaReachIsDisabled() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
Expand All @@ -340,11 +341,12 @@ public void testMsgContinueWhenSubscriptionLevelIsThrottledAndStopOnQuotaReachIs
//Though subscription level is throttled out, should continue the message flow, if stop on quota reach is
//disabled
Assert.assertTrue(throttleHandler.handleRequest(messageContext));
throttleDataHolder.removeThrottleData(subscriptionLevelThrottleKey);
}

@Test
public void testMsgThrottleOutWhenApplicationLevelIsThrottled() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
Expand All @@ -365,11 +367,12 @@ public void testMsgThrottleOutWhenApplicationLevelIsThrottled() {

//Should discontinue message flow, when application level is throttled
Assert.assertFalse(throttleHandler.handleRequest(messageContext));
throttleDataHolder.removeThrottleData(applicationLevelThrottleKey);
}

@Test
public void testMsgThrottleOutWhenProductionHardThrottlingLimitsThrottled() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator,
accessInformation);
Expand Down Expand Up @@ -399,7 +402,7 @@ public void testMsgThrottleOutWhenProductionHardThrottlingLimitsThrottled() {

@Test
public void testMsgThrottleOutWhenSandBoxHardThrottlingLimitsThrottled() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator,
accessInformation);
Expand Down Expand Up @@ -438,7 +441,7 @@ public void testMsgThrottleOutWhenSandBoxHardThrottlingLimitsThrottled() {

@Test
public void testMsgThrottleOutWhenCustomThrottlingLimitExceeded() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator,
accessInformation);
Expand All @@ -465,11 +468,13 @@ public void testMsgThrottleOutWhenCustomThrottlingLimitExceeded() {
Assert.assertFalse(throttleHandler.handleRequest(messageContext));
throttleDataHolder.removeKeyTemplate("testKeyTemplate");
Assert.assertTrue(throttleHandler.handleRequest(messageContext));
throttleDataHolder.removeThrottleData("testKeyTemplate");
throttleDataHolder.removeKeyTemplate("$user");
}

@Test
public void testMsgThrottleOutWhenHittingSubscriptionLevelSpike() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator,
accessInformation);
Expand Down Expand Up @@ -500,7 +505,7 @@ public void testMsgThrottleOutWhenHittingSubscriptionLevelSpike() {

@Test
public void testHandleResponse() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
Expand All @@ -509,7 +514,7 @@ public void testHandleResponse() {

@Test
public void testCheckForStaledThrottleData() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
ServiceReferenceHolder.getInstance().setThrottleDataPublisher(new ThrottleDataPublisher());
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator,
accessInformation);
Expand All @@ -533,11 +538,12 @@ public void testCheckForStaledThrottleData() {
throttleDataHolder.addKeyTemplate("testKeyTemplate", "testKeyTemplateValue");
throttleDataHolder.addThrottleData("testKeyTemplate", System.currentTimeMillis() - 10000);
Assert.assertTrue(throttleHandler.handleRequest(messageContext));
throttleDataHolder.removeThrottleData("testKeyTemplate");
}

@Test
public void testMsgThrottleOutWithUserBlockingConditions() {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, throttleDataHolder, throttleEvaluator);
MessageContext messageContext = TestUtils.getMessageContextWithAuthContext(apiContext, apiVersion);
Expand Down Expand Up @@ -586,7 +592,7 @@ public void testMsgThrottleOutWithUserBlockingConditions() {
@Test
public void testHandleRequestForGraphQLSubscriptions() {

ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, new ThrottleDataHolder(),
ThrottleHandler throttleHandler = new ThrottlingHandlerWrapper(timer, ThrottleDataHolder.getInstance(),
throttleEvaluator, accessInformation);
Axis2MessageContext messageContext = Mockito.mock(Axis2MessageContext.class);
org.apache.axis2.context.MessageContext axis2MessageContext =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public void testServiceReferenceHolder() {

serviceReferenceHolder.getThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = Mockito.mock(ThrottleDataHolder.class);
serviceReferenceHolder.setThrottleDataHolder(throttleDataHolder);

ConfigurationContextService cfgCtxService = Mockito.mock(ConfigurationContextService.class);
serviceReferenceHolder.setConfigurationContextService(cfgCtxService);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,23 @@ public class ThrottleDataHolderTest {
public void addThrottleDataFromMap() throws Exception {
Map<String,Long> map = new HashMap<>();
map.put("/api/1.0.0",System.currentTimeMillis());
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
throttleDataHolder.addThrottleDataFromMap(map);
throttleDataHolder.removeThrottleData("/api/1.0.0");
}


@Test
public void removeThrottledAPIKey() throws Exception {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
throttleDataHolder.addThrottledAPIKey("/api/1.0.0",System.currentTimeMillis());
throttleDataHolder.removeThrottledAPIKey("/api/1.0.0");
}


@Test
public void addBlockingCondition() throws Exception {
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
throttleDataHolder.addAPIBlockingCondition("/api1/1.0.0","enabled");
throttleDataHolder.removeAPIBlockingCondition("/api1/1.0.0");
throttleDataHolder.addApplicationBlockingCondition("admin:DefaultApplication","enabled");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public void run() throws Exception {
eventHubConfigurationDto.setPassword("admin".toCharArray());
eventHubConfigurationDto.setEnabled(true);
eventHubConfigurationDto.setServiceUrl("http://localhost:18083/internal/data/v1");
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
BlockingConditionRetriever blockingConditionRetriever =
new BlockingConditionRetrieverWrapper(eventHubConfigurationDto, throttleDataHolder);
blockingConditionRetriever.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public void run() throws Exception {
eventHubConfigurationDto.setPassword("admin".toCharArray());
eventHubConfigurationDto.setEnabled(true);
eventHubConfigurationDto.setServiceUrl("http://localhost:18084/internal/data/v1");
ThrottleDataHolder throttleDataHolder = new ThrottleDataHolder();
ThrottleDataHolder throttleDataHolder = ThrottleDataHolder.getInstance();
KeyTemplateRetriever keyTemplateRetriever = new KeyTemplateRetrieverWrapper(eventHubConfigurationDto,
throttleDataHolder);
keyTemplateRetriever.run();
Expand Down

0 comments on commit 5418cc4

Please sign in to comment.