From d16143ab60f0bb75d7d1666f1bd198fc651d8ab0 Mon Sep 17 00:00:00 2001 From: sourabh_sourabh Date: Mon, 17 Jun 2024 19:45:00 +0100 Subject: Handle high frequency (faster then DMI) of passthrough request in NCMP - Added pendingAcquireMaxCount property into appliaction.yml. - Added findbugs google annotations dependency. - Supressed "BC_UNCONFIRMED_CAST_OF_RETURN_VALUE" spotbugs low error for reactor.netty.resources.ConnectionProvider. Issue-ID: CPS-2262 Change-Id: Ie755e40282473933f2052fbe7654e7090bb9b337 Signed-off-by: sourabh_sourabh --- .../api/impl/config/DmiWebClientConfiguration.java | 122 ++++++++++++--------- .../api/impl/config/HttpClientConfiguration.java | 46 ++++---- 2 files changed, 97 insertions(+), 71 deletions(-) (limited to 'cps-ncmp-service/src/main/java/org/onap') diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/DmiWebClientConfiguration.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/DmiWebClientConfiguration.java index 08885a9e04..3a861a68b4 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/DmiWebClientConfiguration.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/DmiWebClientConfiguration.java @@ -20,9 +20,12 @@ package org.onap.cps.ncmp.api.impl.config; +import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; import io.netty.channel.ChannelOption; import io.netty.handler.timeout.ReadTimeoutHandler; import io.netty.handler.timeout.WriteTimeoutHandler; +import io.netty.resolver.DefaultAddressResolverGroup; +import java.time.Duration; import java.util.concurrent.TimeUnit; import lombok.RequiredArgsConstructor; import org.springframework.context.annotation.Bean; @@ -35,8 +38,9 @@ import reactor.netty.http.client.HttpClient; import reactor.netty.resources.ConnectionProvider; /** - * Configures and creates a WebClient bean that triggers an initialization (warmup) of the host name resolver and - * loads the necessary native libraries to avoid the extra time needed to load resources for first request. + * Configures and creates WebClient beans for various DMI services including data, model, and health check services. + * The configuration utilizes Netty-based HttpClient with custom connection settings, read and write timeouts, + * and initializes WebClient with these settings to ensure optimal performance and resource management. */ @Configuration @RequiredArgsConstructor @@ -44,82 +48,98 @@ public class DmiWebClientConfiguration { private final HttpClientConfiguration httpClientConfiguration; + private static final Duration DEFAULT_RESPONSE_TIMEOUT = Duration.ofSeconds(30); + /** - * Configures and create a WebClient bean for DMI data service. + * Configures and creates a WebClient bean for DMI data services. * - * @return a WebClient instance for data services. + * @return a WebClient instance configured for data services. */ @Bean public WebClient dataServicesWebClient() { - final HttpClientConfiguration.DataServices httpClientConfiguration - = this.httpClientConfiguration.getDataServices(); - - final HttpClient httpClient = createHttpClient("dataConnectionPool", - httpClientConfiguration.getMaximumConnectionsTotal(), - httpClientConfiguration.getConnectionTimeoutInSeconds(), - httpClientConfiguration.getReadTimeoutInSeconds(), - httpClientConfiguration.getWriteTimeoutInSeconds()); - return buildAndGetWebClient(httpClient, httpClientConfiguration.getMaximumInMemorySizeInMegabytes()); + final HttpClientConfiguration.DataServices dataServiceConfig = httpClientConfiguration.getDataServices(); + final ConnectionProvider dataServicesConnectionProvider + = getConnectionProvider(dataServiceConfig.getConnectionProviderName(), + dataServiceConfig.getMaximumConnectionsTotal(), dataServiceConfig.getPendingAcquireMaxCount()); + final HttpClient dataServicesHttpClient = createHttpClient(dataServiceConfig, dataServicesConnectionProvider); + return buildAndGetWebClient(dataServicesHttpClient, dataServiceConfig.getMaximumInMemorySizeInMegabytes()); } /** - * Configures and creates a WebClient bean for DMI model service. + * Configures and creates a WebClient bean for DMI model services. * - * @return a WebClient instance for model services. + * @return a WebClient instance configured for model services. */ @Bean public WebClient modelServicesWebClient() { - final HttpClientConfiguration.ModelServices httpClientConfiguration - = this.httpClientConfiguration.getModelServices(); - - final HttpClient httpClient = createHttpClient("modelConnectionPool", - httpClientConfiguration.getMaximumConnectionsTotal(), - httpClientConfiguration.getConnectionTimeoutInSeconds(), - httpClientConfiguration.getReadTimeoutInSeconds(), - httpClientConfiguration.getWriteTimeoutInSeconds()); - return buildAndGetWebClient(httpClient, httpClientConfiguration.getMaximumInMemorySizeInMegabytes()); + final HttpClientConfiguration.ModelServices modelServiceConfig = httpClientConfiguration.getModelServices(); + final ConnectionProvider modelServicesConnectionProvider + = getConnectionProvider(modelServiceConfig.getConnectionProviderName(), + modelServiceConfig.getMaximumConnectionsTotal(), + modelServiceConfig.getPendingAcquireMaxCount()); + final HttpClient modelServicesHttpClient + = createHttpClient(modelServiceConfig, modelServicesConnectionProvider); + return buildAndGetWebClient(modelServicesHttpClient, modelServiceConfig.getMaximumInMemorySizeInMegabytes()); } /** - * Configures and creates a WebClient bean for DMI health service. + * Configures and creates a WebClient bean for DMI health check services. * - * @return a WebClient instance for health checks. + * @return a WebClient instance configured for health check services. */ @Bean public WebClient healthChecksWebClient() { - final HttpClientConfiguration.HealthCheckServices httpClientConfiguration - = this.httpClientConfiguration.getHealthCheckServices(); + final HttpClientConfiguration.HealthCheckServices healthCheckServiceConfig + = httpClientConfiguration.getHealthCheckServices(); + final ConnectionProvider healthChecksConnectionProvider + = getConnectionProvider(healthCheckServiceConfig.getConnectionProviderName(), + healthCheckServiceConfig.getMaximumConnectionsTotal(), + healthCheckServiceConfig.getPendingAcquireMaxCount()); + final HttpClient healthChecksHttpClient + = createHttpClient(healthCheckServiceConfig, healthChecksConnectionProvider); + return buildAndGetWebClient(healthChecksHttpClient, + healthCheckServiceConfig.getMaximumInMemorySizeInMegabytes()); + } - final HttpClient httpClient = createHttpClient("healthConnectionPool", - httpClientConfiguration.getMaximumConnectionsTotal(), - httpClientConfiguration.getConnectionTimeoutInSeconds(), - httpClientConfiguration.getReadTimeoutInSeconds(), - httpClientConfiguration.getWriteTimeoutInSeconds()); - return buildAndGetWebClient(httpClient, httpClientConfiguration.getMaximumInMemorySizeInMegabytes()); + /** + * Provides a WebClient.Builder bean for creating WebClient instances. + * + * @return a WebClient.Builder instance. + */ + @Bean + public WebClient.Builder webClientBuilder() { + return WebClient.builder(); } - private static HttpClient createHttpClient(final String connectionProviderName, - final Integer maximumConnectionsTotal, - final Integer connectionTimeoutInSeconds, - final Integer readTimeoutInSeconds, - final Integer writeTimeoutInSeconds) { - final ConnectionProvider dmiWebClientConnectionProvider = ConnectionProvider.create(connectionProviderName, - maximumConnectionsTotal); + private static HttpClient createHttpClient(final HttpClientConfiguration.ServiceConfig serviceConfig, + final ConnectionProvider connectionProvider) { + return HttpClient.create(connectionProvider) + .responseTimeout(DEFAULT_RESPONSE_TIMEOUT) + .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, serviceConfig.getConnectionTimeoutInSeconds() * 1000) + .doOnConnected(connection -> connection.addHandlerLast(new ReadTimeoutHandler( + serviceConfig.getReadTimeoutInSeconds(), TimeUnit.SECONDS)).addHandlerLast( + new WriteTimeoutHandler(serviceConfig.getWriteTimeoutInSeconds(), TimeUnit.SECONDS))) + .resolver(DefaultAddressResolverGroup.INSTANCE) + .compress(true); + } - return HttpClient.create(dmiWebClientConnectionProvider) - .option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeoutInSeconds * 1000) - .doOnConnected(connection -> connection.addHandlerLast(new ReadTimeoutHandler(readTimeoutInSeconds, - TimeUnit.SECONDS)).addHandlerLast(new WriteTimeoutHandler(writeTimeoutInSeconds, - TimeUnit.SECONDS))); + @SuppressFBWarnings("BC_UNCONFIRMED_CAST_OF_RETURN_VALUE") + private static ConnectionProvider getConnectionProvider(final String connectionProviderName, + final int maximumConnectionsTotal, + final int pendingAcquireMaxCount) { + return ConnectionProvider.builder(connectionProviderName) + .maxConnections(maximumConnectionsTotal) + .pendingAcquireMaxCount(pendingAcquireMaxCount) + .build(); } - private static WebClient buildAndGetWebClient(final HttpClient httpClient, - final Integer maximumInMemorySizeInMegabytes) { - return WebClient.builder() + private WebClient buildAndGetWebClient(final HttpClient httpClient, + final int maximumInMemorySizeInMegabytes) { + return webClientBuilder() .defaultHeaders(header -> header.set(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)) .defaultHeaders(header -> header.set(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)) .clientConnector(new ReactorClientHttpConnector(httpClient)) - .codecs(configurer -> configurer.defaultCodecs().maxInMemorySize( - maximumInMemorySizeInMegabytes * 1024 * 1024)).build(); + .codecs(configurer -> configurer.defaultCodecs() + .maxInMemorySize(maximumInMemorySizeInMegabytes * 1024 * 1024)).build(); } } diff --git a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/HttpClientConfiguration.java b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/HttpClientConfiguration.java index 62432f6cae..0acbabbbaf 100644 --- a/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/HttpClientConfiguration.java +++ b/cps-ncmp-service/src/main/java/org/onap/cps/ncmp/api/impl/config/HttpClientConfiguration.java @@ -23,11 +23,11 @@ package org.onap.cps.ncmp.api.impl.config; import lombok.Getter; import lombok.Setter; import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.stereotype.Component; +import org.springframework.context.annotation.Configuration; @Getter @Setter -@Component +@Configuration @ConfigurationProperties(prefix = "ncmp.dmi.httpclient") public class HttpClientConfiguration { @@ -37,30 +37,36 @@ public class HttpClientConfiguration { @Getter @Setter - public static class DataServices { - private Integer maximumConnectionsTotal = 100; - private Integer connectionTimeoutInSeconds = 30; - private Integer readTimeoutInSeconds = 30; - private Integer writeTimeoutInSeconds = 30; - private Integer maximumInMemorySizeInMegabytes = 1; + public static class DataServices extends ServiceConfig { + private String connectionProviderName = "dataConnectionPool"; } @Getter @Setter - public static class ModelServices { - private Integer maximumConnectionsTotal = 100; - private Integer connectionTimeoutInSeconds = 30; - private Integer readTimeoutInSeconds = 30; - private Integer writeTimeoutInSeconds = 30; - private Integer maximumInMemorySizeInMegabytes = 1; + public static class ModelServices extends ServiceConfig { + private String connectionProviderName = "modelConnectionPool"; + } + + @Getter + @Setter + public static class HealthCheckServices extends ServiceConfig { + private String connectionProviderName = "healthConnectionPool"; + private int maximumConnectionsTotal = 10; + private int pendingAcquireMaxCount = 5; } + /** + * Base configuration properties for all services. + */ @Getter - public static class HealthCheckServices { - private final Integer maximumConnectionsTotal = 10; - private final Integer connectionTimeoutInSeconds = 30; - private final Integer readTimeoutInSeconds = 30; - private final Integer writeTimeoutInSeconds = 30; - private final Integer maximumInMemorySizeInMegabytes = 1; + @Setter + public static class ServiceConfig { + private String connectionProviderName = "cpsConnectionPool"; + private int maximumConnectionsTotal = 100; + private int pendingAcquireMaxCount = 50; + private Integer connectionTimeoutInSeconds = 30; + private long readTimeoutInSeconds = 30; + private long writeTimeoutInSeconds = 30; + private int maximumInMemorySizeInMegabytes = 1; } } -- cgit 1.2.3-korg