From 6108dc820031ddef1724caa835990e2079e59f55 Mon Sep 17 00:00:00 2001 From: kaanaktas Date: Mon, 3 Jun 2024 14:43:01 +0100 Subject: [PATCH] implement resilience bulkhead patterns --- .../src/main/resources/application.properties | 4 - .../configuration/Resilience4jConfig.java | 77 +------------------ .../endpoint/CaseDefinitionController.java | 2 - .../endpoint/ControllerExceptionHandler.java | 8 -- .../rest/endpoint/DisplayApiController.java | 3 +- .../configuration/Resilience4jConfigTest.java | 2 +- 6 files changed, 3 insertions(+), 93 deletions(-) diff --git a/application/src/main/resources/application.properties b/application/src/main/resources/application.properties index f36899a03f..6cd58384f4 100644 --- a/application/src/main/resources/application.properties +++ b/application/src/main/resources/application.properties @@ -154,9 +154,5 @@ testing-support-endpoints.enabled=${TESTING_SUPPORT_ENDPOINTS_ENABLED:false} spring.servlet.multipart.max-file-size=2MB spring.servlet.multipart.max-request-size=2MB -ratelimiter.default.limitForPeriod=${RATE_LIMITER_DEFAULT_LIMIT_FOR_PERIOD:30} -ratelimiter.default.limitRefreshPeriod=${RATE_LIMITER_DEFAULT_LIMIT_REFRESH_PERIOD:1} -ratelimiter.default.timeoutDuration=${RATE_LIMITER_DEFAULT_TIME_OUT_DURATION:0} - bulkhead.default.maxConcurrentCalls=${BULKHEAD_DEFAULT_MAX_CONCURRENT_CALLS:6} bulkhead.default.maxWaitDuration=${BULKHEAD_DEFAULT_MAX_WAIT_DURATION:500} diff --git a/rest-api/src/main/java/uk/gov/hmcts/ccd/definition/store/rest/configuration/Resilience4jConfig.java b/rest-api/src/main/java/uk/gov/hmcts/ccd/definition/store/rest/configuration/Resilience4jConfig.java index 76d072c8ee..903787bfea 100644 --- a/rest-api/src/main/java/uk/gov/hmcts/ccd/definition/store/rest/configuration/Resilience4jConfig.java +++ b/rest-api/src/main/java/uk/gov/hmcts/ccd/definition/store/rest/configuration/Resilience4jConfig.java @@ -3,9 +3,6 @@ import io.github.resilience4j.bulkhead.Bulkhead; import io.github.resilience4j.bulkhead.BulkheadConfig; import io.github.resilience4j.bulkhead.BulkheadRegistry; -import io.github.resilience4j.ratelimiter.RateLimiter; -import io.github.resilience4j.ratelimiter.RateLimiterConfig; -import io.github.resilience4j.ratelimiter.RateLimiterRegistry; import lombok.Getter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; @@ -17,19 +14,6 @@ @Configuration @Slf4j public class Resilience4jConfig { - - @Getter - @Value("${ratelimiter.default.limitForPeriod}") - private int ratelimiterDefaultLimitForPeriod; - - @Getter - @Value("${ratelimiter.default.limitRefreshPeriod}") - private int ratelimiterDefaultLimitRefreshPeriod; - - @Getter - @Value("${ratelimiter.default.timeoutDuration}") - private int ratelimiterDefaultTimeoutDuration; - @Getter @Value("${bulkhead.default.maxConcurrentCalls}") private int bulkheadDefaultMaxConcurrentCalls; @@ -38,66 +22,16 @@ public class Resilience4jConfig { @Value("${bulkhead.default.maxWaitDuration}") private int bulkheadDefaultMaxWaitDuration; - public static final String RATE_LIMITER_CASE_TYPE = "case_type"; - public static final String RATE_LIMITER_JURISDICTIONS = "jurisdictions"; - public static final String RATE_LIMITER_EVENT_TRIGGERS = "event-triggers"; - public static final String BULKHEAD_CASE_TYPE = "case_type"; public static final String BULKHEAD_JURISDICTIONS = "jurisdictions"; public static final String BULKHEAD_EVENT_TRIGGERS = "event-triggers"; - private final RateLimiterRegistry rateLimiterRegistry; private final BulkheadRegistry bulkheadRegistry; - public Resilience4jConfig(RateLimiterRegistry rateLimiterRegistry, BulkheadRegistry bulkheadRegistry) { - this.rateLimiterRegistry = rateLimiterRegistry; + public Resilience4jConfig(BulkheadRegistry bulkheadRegistry) { this.bulkheadRegistry = bulkheadRegistry; } - @Bean - public RateLimiter caseTypeRateLimiter() { - RateLimiter rateLimiter = createRateLimiter(RATE_LIMITER_CASE_TYPE, - getRatelimiterDefaultLimitForPeriod(), - getRatelimiterDefaultLimitRefreshPeriod(), - getRatelimiterDefaultTimeoutDuration()); - initiateRateLimiterEvent(rateLimiter); - - return rateLimiter; - } - - @Bean - public RateLimiter eventTriggersRateLimiter() { - RateLimiter rateLimiter = createRateLimiter(RATE_LIMITER_EVENT_TRIGGERS, - getRatelimiterDefaultLimitForPeriod(), - getRatelimiterDefaultLimitRefreshPeriod(), - getRatelimiterDefaultTimeoutDuration()); - initiateRateLimiterEvent(rateLimiter); - - return rateLimiter; - } - - @Bean - public RateLimiter jurisdictionRateLimiter() { - RateLimiter rateLimiter = createRateLimiter(RATE_LIMITER_JURISDICTIONS, - getRatelimiterDefaultLimitForPeriod(), - getRatelimiterDefaultLimitRefreshPeriod(), - getRatelimiterDefaultTimeoutDuration()); - initiateRateLimiterEvent(rateLimiter); - - return rateLimiter; - } - - private RateLimiter createRateLimiter(final String name, int limitForPeriod, int limitRefreshPeriod, - int timeoutDuration) { - RateLimiterConfig config = RateLimiterConfig.custom() - .timeoutDuration(Duration.ofSeconds(timeoutDuration)) - .limitRefreshPeriod(Duration.ofSeconds(limitRefreshPeriod)) - .limitForPeriod(limitForPeriod) - .build(); - - return rateLimiterRegistry.rateLimiter(name, config); - } - @Bean public Bulkhead caseTypeBulkhead() { Bulkhead bulkhead = createBulkhead(BULKHEAD_CASE_TYPE, @@ -138,19 +72,10 @@ private Bulkhead createBulkhead(final String name, int bulkheadDefaultMaxConcurr return bulkheadRegistry.bulkhead(name, config); } - private void initiateRateLimiterEvent(RateLimiter rateLimiter) { - RateLimiter.EventPublisher eventPublisher = rateLimiter - .getEventPublisher(); - - eventPublisher.onSuccess(event -> log.info(event.toString())); - eventPublisher.onFailure(event -> log.info(event.toString())); - } - private void initiateBulkheadEvent(Bulkhead bulkhead) { Bulkhead.EventPublisher eventPublisher = bulkhead .getEventPublisher(); - // eventPublisher.onCallPermitted(event -> log.info(event.toString())); eventPublisher.onCallRejected(event -> log.error(event.toString())); } } diff --git a/rest-api/src/main/java/uk/gov/hmcts/ccd/definition/store/rest/endpoint/CaseDefinitionController.java b/rest-api/src/main/java/uk/gov/hmcts/ccd/definition/store/rest/endpoint/CaseDefinitionController.java index 985627ce3f..67626b4b1b 100644 --- a/rest-api/src/main/java/uk/gov/hmcts/ccd/definition/store/rest/endpoint/CaseDefinitionController.java +++ b/rest-api/src/main/java/uk/gov/hmcts/ccd/definition/store/rest/endpoint/CaseDefinitionController.java @@ -60,7 +60,6 @@ public CaseDefinitionController(CaseTypeService caseTypeService, JurisdictionSer @ApiResponse(code = 200, message = "Unexpected error") }) @Bulkhead(name = Resilience4jConfig.BULKHEAD_CASE_TYPE) - //@RateLimiter(name = Resilience4jConfig.RATE_LIMITER_CASE_TYPE) public CaseType dataCaseTypeIdGet( @ApiParam(value = "Case Type ID", required = true) @PathVariable("id") String id) { return caseTypeService.findByCaseTypeId(id).orElseThrow(() -> new NotFoundException(id)); @@ -130,7 +129,6 @@ public List dataJurisdictionsJurisdictionIdCaseTypeGet( @ApiResponse(code = 200, message = "List of jurisdictions") }) @Bulkhead(name = Resilience4jConfig.BULKHEAD_JURISDICTIONS) - //@RateLimiter(name = Resilience4jConfig.RATE_LIMITER_JURISDICTIONS) public List findJurisdictions( @ApiParam(value = "list of jurisdiction references") @RequestParam("ids") Optional> idsOptional) { diff --git a/rest-api/src/main/java/uk/gov/hmcts/ccd/definition/store/rest/endpoint/ControllerExceptionHandler.java b/rest-api/src/main/java/uk/gov/hmcts/ccd/definition/store/rest/endpoint/ControllerExceptionHandler.java index 035bd00da9..ce9e66db2d 100644 --- a/rest-api/src/main/java/uk/gov/hmcts/ccd/definition/store/rest/endpoint/ControllerExceptionHandler.java +++ b/rest-api/src/main/java/uk/gov/hmcts/ccd/definition/store/rest/endpoint/ControllerExceptionHandler.java @@ -2,7 +2,6 @@ import com.google.common.collect.ImmutableMap; import io.github.resilience4j.bulkhead.BulkheadFullException; -import io.github.resilience4j.ratelimiter.RequestNotPermitted; import org.hibernate.exception.ConstraintViolationException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -123,13 +122,6 @@ Map elasticSearchInitialisationException(ElasticSearchInitialisa return getMessage(e, "ElasticSearch initialisation exception: %s"); } - @ExceptionHandler({RequestNotPermitted.class}) - @ResponseStatus(HttpStatus.TOO_MANY_REQUESTS) - public Map handleRequestNotPermitted(RequestNotPermitted e) { - log(e); - return getMessage(e, "Request is not permitted: %s"); - } - @ExceptionHandler({BulkheadFullException.class}) @ResponseStatus(HttpStatus.BANDWIDTH_LIMIT_EXCEEDED) public Map handleRequestNotPermitted(BulkheadFullException e) { diff --git a/rest-api/src/main/java/uk/gov/hmcts/ccd/definition/store/rest/endpoint/DisplayApiController.java b/rest-api/src/main/java/uk/gov/hmcts/ccd/definition/store/rest/endpoint/DisplayApiController.java index 45787cdd48..ea3849bd6e 100644 --- a/rest-api/src/main/java/uk/gov/hmcts/ccd/definition/store/rest/endpoint/DisplayApiController.java +++ b/rest-api/src/main/java/uk/gov/hmcts/ccd/definition/store/rest/endpoint/DisplayApiController.java @@ -100,8 +100,7 @@ public CaseTabCollection displayTabStructureIdGet( @ApiResponse(code = 200, message = "A Case Wizard Page Collection"), @ApiResponse(code = 200, message = "Unexpected error") }) - @Bulkhead(name = Resilience4jConfig.BULKHEAD_CASE_TYPE) - //@RateLimiter(name = Resilience4jConfig.RATE_LIMITER_EVENT_TRIGGERS) + @Bulkhead(name = Resilience4jConfig.BULKHEAD_EVENT_TRIGGERS) public WizardPageCollection displayWizardPageStructureIdGet( @ApiParam(value = "Case Type ID", required = true) @PathVariable("ctid") String caseTypeId, @ApiParam(value = "Event Reference", required = true) @PathVariable("etid") String eventReference) { diff --git a/rest-api/src/test/java/uk/gov/hmcts/ccd/definition/store/rest/configuration/Resilience4jConfigTest.java b/rest-api/src/test/java/uk/gov/hmcts/ccd/definition/store/rest/configuration/Resilience4jConfigTest.java index cc4c70b1b9..610915887d 100644 --- a/rest-api/src/test/java/uk/gov/hmcts/ccd/definition/store/rest/configuration/Resilience4jConfigTest.java +++ b/rest-api/src/test/java/uk/gov/hmcts/ccd/definition/store/rest/configuration/Resilience4jConfigTest.java @@ -52,7 +52,7 @@ public void setUp() { when(bulkhead.getEventPublisher()).thenReturn(bulkheadEventPublisher); when(bulkheadRegistry.bulkhead(anyString(), any(BulkheadConfig.class))).thenReturn(bulkhead); - resilience4jConfig = new Resilience4jConfig(rateLimiterRegistry, bulkheadRegistry); + resilience4jConfig = new Resilience4jConfig(bulkheadRegistry); resilience4jConfig = spy(resilience4jConfig); doReturn(10).when(resilience4jConfig).getRatelimiterDefaultLimitForPeriod();