diff --git a/.gitignore b/.gitignore index 9eabb5af..e02ba096 100644 --- a/.gitignore +++ b/.gitignore @@ -6,4 +6,5 @@ target *.iws out dep -.java-version \ No newline at end of file +.java-version +stage.yml diff --git a/lombok.config b/lombok.config index d76b04d5..0d6c2570 100644 --- a/lombok.config +++ b/lombok.config @@ -1,2 +1,3 @@ lombok.addLombokGeneratedAnnotation = true -lombok.extern.findbugs.addSuppressFBWarnings = true \ No newline at end of file +lombok.extern.findbugs.addSuppressFBWarnings = true +lombok.accessors.chain = true diff --git a/pom.xml b/pom.xml index c3a75300..5bd6472c 100644 --- a/pom.xml +++ b/pom.xml @@ -85,7 +85,7 @@ 3.8.0 17 - 11 + 17 1.18.22 3.0.1u2 diff --git a/ranger-http-client/pom.xml b/ranger-http-client/pom.xml index 286dda72..1068efa0 100644 --- a/ranger-http-client/pom.xml +++ b/ranger-http-client/pom.xml @@ -24,15 +24,9 @@ org.wiremock - wiremock + wiremock-standalone ${wiremock.version} test - - - commons-fileupload - commons-fileupload - - diff --git a/ranger-http-client/src/main/java/io/appform/ranger/client/http/AbstractRangerHttpHubClient.java b/ranger-http-client/src/main/java/io/appform/ranger/client/http/AbstractRangerHttpHubClient.java index 5dd8d92a..eef30766 100644 --- a/ranger-http-client/src/main/java/io/appform/ranger/client/http/AbstractRangerHttpHubClient.java +++ b/ranger-http-client/src/main/java/io/appform/ranger/client/http/AbstractRangerHttpHubClient.java @@ -25,10 +25,14 @@ import io.appform.ranger.http.serde.HTTPResponseDataDeserializer; import io.appform.ranger.http.servicefinderhub.HttpServiceDataSource; import io.appform.ranger.http.servicefinderhub.HttpServiceFinderHubBuilder; +import io.appform.ranger.http.utils.RangerHttpUtils; import lombok.Builder; import lombok.Getter; import lombok.experimental.SuperBuilder; import lombok.extern.slf4j.Slf4j; +import okhttp3.OkHttpClient; + +import java.util.Objects; @Slf4j @Getter @@ -37,12 +41,16 @@ public abstract class AbstractRangerHttpHubClient { private final HttpClientConfig clientConfig; + + private final OkHttpClient httpClient; + @Builder.Default private final ServiceNodeSelector nodeSelector = new RandomServiceNodeSelector<>(); @Override protected ServiceDataSource getDefaultDataSource() { - return new HttpServiceDataSource<>(clientConfig, getMapper()); + return new HttpServiceDataSource<>(clientConfig, getMapper(), Objects.requireNonNullElseGet(getHttpClient(), + () -> RangerHttpUtils.httpClient(clientConfig))); } @Override diff --git a/ranger-http-client/src/main/java/io/appform/ranger/client/http/ShardedRangerHttpHubClient.java b/ranger-http-client/src/main/java/io/appform/ranger/client/http/ShardedRangerHttpHubClient.java index ecc69e68..04389d23 100644 --- a/ranger-http-client/src/main/java/io/appform/ranger/client/http/ShardedRangerHttpHubClient.java +++ b/ranger-http-client/src/main/java/io/appform/ranger/client/http/ShardedRangerHttpHubClient.java @@ -37,6 +37,7 @@ public class ShardedRangerHttpHubClient protected ServiceFinderFactory> getFinderFactory() { return HttpShardedServiceFinderFactory.builder() .httpClientConfig(this.getClientConfig()) + .httpClient(this.getHttpClient()) .nodeRefreshIntervalMs(getNodeRefreshTimeMs()) .deserializer(getDeserializer()) .shardSelector(shardSelector) diff --git a/ranger-http-client/src/main/java/io/appform/ranger/client/http/SimpleRangerHttpClient.java b/ranger-http-client/src/main/java/io/appform/ranger/client/http/SimpleRangerHttpClient.java index 021f027c..bfcdf67f 100644 --- a/ranger-http-client/src/main/java/io/appform/ranger/client/http/SimpleRangerHttpClient.java +++ b/ranger-http-client/src/main/java/io/appform/ranger/client/http/SimpleRangerHttpClient.java @@ -29,6 +29,7 @@ import lombok.Getter; import lombok.experimental.SuperBuilder; import lombok.extern.slf4j.Slf4j; +import okhttp3.OkHttpClient; @Slf4j @SuperBuilder @@ -39,6 +40,7 @@ public class SimpleRangerHttpClient extends AbstractRangerClient deserializer; @Builder.Default private final ShardSelector> shardSelector = new ListShardSelector<>(); @@ -58,6 +60,7 @@ public void start() { .withServiceName(serviceName) .withNamespace(namespace) .withObjectMapper(mapper) + .withHttpClient(httpClient) .withNodeRefreshIntervalMs(nodeRefreshIntervalMs) .withDeserializer(deserializer) .withShardSelector(shardSelector) diff --git a/ranger-http-client/src/main/java/io/appform/ranger/client/http/UnshardedRangerHttpHubClient.java b/ranger-http-client/src/main/java/io/appform/ranger/client/http/UnshardedRangerHttpHubClient.java index 2bd3f83e..74ee5291 100644 --- a/ranger-http-client/src/main/java/io/appform/ranger/client/http/UnshardedRangerHttpHubClient.java +++ b/ranger-http-client/src/main/java/io/appform/ranger/client/http/UnshardedRangerHttpHubClient.java @@ -37,6 +37,7 @@ public class UnshardedRangerHttpHubClient protected ServiceFinderFactory> getFinderFactory() { return HttpUnshardedServiceFinderFactory.builder() .httpClientConfig(this.getClientConfig()) + .httpClient(this.getHttpClient()) .nodeRefreshIntervalMs(getNodeRefreshTimeMs()) .deserializer(getDeserializer()) .shardSelector(shardSelector) diff --git a/ranger-http-client/src/test/java/io/appform/ranger/client/http/ShardedRangerHttpClientTest.java b/ranger-http-client/src/test/java/io/appform/ranger/client/http/ShardedRangerHttpClientTest.java index 1e09e1e6..9078dea0 100644 --- a/ranger-http-client/src/test/java/io/appform/ranger/client/http/ShardedRangerHttpClientTest.java +++ b/ranger-http-client/src/test/java/io/appform/ranger/client/http/ShardedRangerHttpClientTest.java @@ -17,6 +17,7 @@ import io.appform.ranger.core.units.TestNodeData; import io.appform.ranger.core.utils.RangerTestUtils; +import io.appform.ranger.http.utils.RangerHttpUtils; import lombok.val; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -25,8 +26,10 @@ class ShardedRangerHttpClientTest extends BaseRangerHttpClientTest { @Test void testShardedHttpHubClient(){ + val httpClientConfig = getHttpClientConfig(); val client = ShardedRangerHttpHubClient.builder() - .clientConfig(getHttpClientConfig()) + .clientConfig(httpClientConfig) + .httpClient(RangerHttpUtils.httpClient(httpClientConfig)) .namespace("test-n") .deserializer(this::read) .mapper(getObjectMapper()) diff --git a/ranger-http-client/src/test/java/io/appform/ranger/client/http/SimpleRangerHttpClientTest.java b/ranger-http-client/src/test/java/io/appform/ranger/client/http/SimpleRangerHttpClientTest.java index e379f272..a1d69d4f 100644 --- a/ranger-http-client/src/test/java/io/appform/ranger/client/http/SimpleRangerHttpClientTest.java +++ b/ranger-http-client/src/test/java/io/appform/ranger/client/http/SimpleRangerHttpClientTest.java @@ -17,6 +17,7 @@ import io.appform.ranger.core.units.TestNodeData; import io.appform.ranger.core.utils.RangerTestUtils; +import io.appform.ranger.http.utils.RangerHttpUtils; import lombok.val; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -25,9 +26,11 @@ class SimpleRangerHttpClientTest extends BaseRangerHttpClientTest{ @Test void testSimpleHttpRangerClient(){ + val httpClientConfig = getHttpClientConfig(); val client = SimpleRangerHttpClient.builder() - .clientConfig(getHttpClientConfig()) + .clientConfig(httpClientConfig) .mapper(getObjectMapper()) + .httpClient(RangerHttpUtils.httpClient(httpClientConfig)) .deserializer(this::read) .namespace("test-n") .serviceName("test-s") diff --git a/ranger-http-client/src/test/java/io/appform/ranger/client/http/UnshardedRangerHttpClientTest.java b/ranger-http-client/src/test/java/io/appform/ranger/client/http/UnshardedRangerHttpClientTest.java index a3ced34c..7b22bd95 100644 --- a/ranger-http-client/src/test/java/io/appform/ranger/client/http/UnshardedRangerHttpClientTest.java +++ b/ranger-http-client/src/test/java/io/appform/ranger/client/http/UnshardedRangerHttpClientTest.java @@ -17,6 +17,7 @@ import io.appform.ranger.core.units.TestNodeData; import io.appform.ranger.core.utils.RangerTestUtils; +import io.appform.ranger.http.utils.RangerHttpUtils; import lombok.val; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -25,8 +26,10 @@ class UnshardedRangerHttpClientTest extends BaseRangerHttpClientTest { @Test void testUnshardedRangerHubClient(){ + val httpClientConfig = getHttpClientConfig(); val client = UnshardedRangerHttpHubClient.builder() - .clientConfig(getHttpClientConfig()) + .clientConfig(httpClientConfig) + .httpClient(RangerHttpUtils.httpClient(httpClientConfig)) .namespace("test-n") .deserializer(this::read) .mapper(getObjectMapper()) diff --git a/ranger-http/pom.xml b/ranger-http/pom.xml index 5356c651..66063983 100644 --- a/ranger-http/pom.xml +++ b/ranger-http/pom.xml @@ -29,15 +29,9 @@ org.wiremock - wiremock + wiremock-standalone ${wiremock.version} test - - - commons-fileupload - commons-fileupload - - io.appform.ranger diff --git a/ranger-http/src/main/java/io/appform/ranger/http/common/HttpNodeDataStoreConnector.java b/ranger-http/src/main/java/io/appform/ranger/http/common/HttpNodeDataStoreConnector.java index f9c51528..2d0f52c9 100644 --- a/ranger-http/src/main/java/io/appform/ranger/http/common/HttpNodeDataStoreConnector.java +++ b/ranger-http/src/main/java/io/appform/ranger/http/common/HttpNodeDataStoreConnector.java @@ -19,11 +19,8 @@ import io.appform.ranger.core.model.NodeDataStoreConnector; import io.appform.ranger.http.config.HttpClientConfig; import lombok.extern.slf4j.Slf4j; -import okhttp3.ConnectionPool; import okhttp3.OkHttpClient; -import java.util.concurrent.TimeUnit; - /** * */ @@ -36,17 +33,9 @@ public class HttpNodeDataStoreConnector implements NodeDataStoreConnector public HttpNodeDataStoreConnector( final HttpClientConfig config, - final ObjectMapper mapper) { - this.httpClient = new OkHttpClient.Builder() - .callTimeout(config.getOperationTimeoutMs() == 0 - ? 3000 - : config.getOperationTimeoutMs(), TimeUnit.MILLISECONDS) - .connectTimeout(config.getConnectionTimeoutMs() == 0 - ? 3000 - : config.getConnectionTimeoutMs(), TimeUnit.MILLISECONDS) - .followRedirects(true) - .connectionPool(new ConnectionPool(1, 30, TimeUnit.SECONDS)) - .build(); + final ObjectMapper mapper, + final OkHttpClient httpClient) { + this.httpClient = httpClient; this.config = config; this.mapper = mapper; } diff --git a/ranger-http/src/main/java/io/appform/ranger/http/config/HttpClientConfig.java b/ranger-http/src/main/java/io/appform/ranger/http/config/HttpClientConfig.java index b24d762f..268ef0d0 100644 --- a/ranger-http/src/main/java/io/appform/ranger/http/config/HttpClientConfig.java +++ b/ranger-http/src/main/java/io/appform/ranger/http/config/HttpClientConfig.java @@ -15,6 +15,7 @@ */ package io.appform.ranger.http.config; +import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Value; import lombok.extern.jackson.Jacksonized; @@ -25,6 +26,7 @@ @Value @Builder @Jacksonized +@AllArgsConstructor public class HttpClientConfig { String host; int port; diff --git a/ranger-http/src/main/java/io/appform/ranger/http/servicefinder/HttpNodeDataSource.java b/ranger-http/src/main/java/io/appform/ranger/http/servicefinder/HttpNodeDataSource.java index 1eaba0ab..58ae4bee 100644 --- a/ranger-http/src/main/java/io/appform/ranger/http/servicefinder/HttpNodeDataSource.java +++ b/ranger-http/src/main/java/io/appform/ranger/http/servicefinder/HttpNodeDataSource.java @@ -24,15 +24,15 @@ import io.appform.ranger.http.common.HttpNodeDataStoreConnector; import io.appform.ranger.http.config.HttpClientConfig; import io.appform.ranger.http.serde.HTTPResponseDataDeserializer; -import java.util.Optional; import lombok.extern.slf4j.Slf4j; import lombok.val; import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; import okhttp3.Request; import java.io.IOException; -import java.util.Collections; import java.util.List; +import java.util.Optional; /** * @@ -43,10 +43,11 @@ public class HttpNodeDataSource> ex private final Service service; public HttpNodeDataSource( - Service service, + final Service service, final HttpClientConfig config, - ObjectMapper mapper) { - super(config, mapper); + final ObjectMapper mapper, + final OkHttpClient httpClient) { + super(config, mapper, httpClient); this.service = service; } diff --git a/ranger-http/src/main/java/io/appform/ranger/http/servicefinder/HttpShardedServiceFinderBuilder.java b/ranger-http/src/main/java/io/appform/ranger/http/servicefinder/HttpShardedServiceFinderBuilder.java index 26ab07d2..74280b7c 100644 --- a/ranger-http/src/main/java/io/appform/ranger/http/servicefinder/HttpShardedServiceFinderBuilder.java +++ b/ranger-http/src/main/java/io/appform/ranger/http/servicefinder/HttpShardedServiceFinderBuilder.java @@ -22,6 +22,10 @@ import io.appform.ranger.core.model.Service; import io.appform.ranger.http.config.HttpClientConfig; import io.appform.ranger.http.serde.HTTPResponseDataDeserializer; +import io.appform.ranger.http.utils.RangerHttpUtils; +import okhttp3.OkHttpClient; + +import java.util.Objects; /** * @@ -30,6 +34,7 @@ public class HttpShardedServiceFinderBuilder extends SimpleShardedServiceFind private HttpClientConfig clientConfig; private ObjectMapper mapper; + private OkHttpClient httpClient; public HttpShardedServiceFinderBuilder withClientConfig(final HttpClientConfig clientConfig) { this.clientConfig = clientConfig; @@ -41,6 +46,11 @@ public HttpShardedServiceFinderBuilder withObjectMapper(final ObjectMapper ma return this; } + public HttpShardedServiceFinderBuilder withHttpClient(final OkHttpClient httpClient){ + this.httpClient = httpClient; + return this; + } + @Override public SimpleShardedServiceFinder build() { return buildFinder(); @@ -48,7 +58,9 @@ public SimpleShardedServiceFinder build() { @Override protected NodeDataSource> dataSource(Service service) { - return new HttpNodeDataSource<>(service, clientConfig, mapper); + return new HttpNodeDataSource<>(service, clientConfig, mapper, + Objects.requireNonNullElseGet(httpClient, + () -> RangerHttpUtils.httpClient(clientConfig))); } } diff --git a/ranger-http/src/main/java/io/appform/ranger/http/servicefinder/HttpUnshardedServiceFinderBuilider.java b/ranger-http/src/main/java/io/appform/ranger/http/servicefinder/HttpUnshardedServiceFinderBuilider.java index efdf1f2f..3dffcf2a 100644 --- a/ranger-http/src/main/java/io/appform/ranger/http/servicefinder/HttpUnshardedServiceFinderBuilider.java +++ b/ranger-http/src/main/java/io/appform/ranger/http/servicefinder/HttpUnshardedServiceFinderBuilider.java @@ -22,12 +22,17 @@ import io.appform.ranger.core.model.Service; import io.appform.ranger.http.config.HttpClientConfig; import io.appform.ranger.http.serde.HTTPResponseDataDeserializer; +import io.appform.ranger.http.utils.RangerHttpUtils; +import okhttp3.OkHttpClient; + +import java.util.Objects; public class HttpUnshardedServiceFinderBuilider extends SimpleUnshardedServiceFinderBuilder, HTTPResponseDataDeserializer> { private HttpClientConfig clientConfig; private ObjectMapper mapper; + private OkHttpClient httpClient; public HttpUnshardedServiceFinderBuilider withClientConfig(final HttpClientConfig clientConfig) { this.clientConfig = clientConfig; @@ -39,6 +44,11 @@ public HttpUnshardedServiceFinderBuilider withObjectMapper(final ObjectMapper return this; } + public HttpUnshardedServiceFinderBuilider withHttpClient(final OkHttpClient httpClient) { + this.httpClient = httpClient; + return this; + } + @Override public SimpleUnshardedServiceFinder build() { return buildFinder(); @@ -46,7 +56,9 @@ public SimpleUnshardedServiceFinder build() { @Override protected NodeDataSource> dataSource(Service service) { - return new HttpNodeDataSource<>(service, clientConfig, mapper); + return new HttpNodeDataSource<>(service, clientConfig, mapper, + Objects.requireNonNullElseGet(httpClient, + () -> RangerHttpUtils.httpClient(clientConfig))); } } diff --git a/ranger-http/src/main/java/io/appform/ranger/http/servicefinderhub/HttpServiceDataSource.java b/ranger-http/src/main/java/io/appform/ranger/http/servicefinderhub/HttpServiceDataSource.java index 978eca5d..d6e9f28e 100644 --- a/ranger-http/src/main/java/io/appform/ranger/http/servicefinderhub/HttpServiceDataSource.java +++ b/ranger-http/src/main/java/io/appform/ranger/http/servicefinderhub/HttpServiceDataSource.java @@ -25,6 +25,7 @@ import lombok.extern.slf4j.Slf4j; import lombok.val; import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; import okhttp3.Request; import java.io.IOException; @@ -34,8 +35,8 @@ @Slf4j public class HttpServiceDataSource extends HttpNodeDataStoreConnector implements ServiceDataSource { - public HttpServiceDataSource(HttpClientConfig config, ObjectMapper mapper) { - super(config, mapper); + public HttpServiceDataSource(HttpClientConfig config, ObjectMapper mapper, OkHttpClient httpClient) { + super(config, mapper, httpClient); } @Override diff --git a/ranger-http/src/main/java/io/appform/ranger/http/servicefinderhub/HttpShardedServiceFinderFactory.java b/ranger-http/src/main/java/io/appform/ranger/http/servicefinderhub/HttpShardedServiceFinderFactory.java index 129568ba..4c70d451 100644 --- a/ranger-http/src/main/java/io/appform/ranger/http/servicefinderhub/HttpShardedServiceFinderFactory.java +++ b/ranger-http/src/main/java/io/appform/ranger/http/servicefinderhub/HttpShardedServiceFinderFactory.java @@ -27,10 +27,12 @@ import io.appform.ranger.http.servicefinder.HttpShardedServiceFinderBuilder; import lombok.Builder; import lombok.val; +import okhttp3.OkHttpClient; public class HttpShardedServiceFinderFactory implements ServiceFinderFactory> { private final HttpClientConfig clientConfig; + private final OkHttpClient httpClient; private final ObjectMapper mapper; private final HTTPResponseDataDeserializer deserializer; private final ShardSelector> shardSelector; @@ -39,7 +41,7 @@ public class HttpShardedServiceFinderFactory implements ServiceFinderFactory @Builder public HttpShardedServiceFinderFactory( - HttpClientConfig httpClientConfig, + HttpClientConfig httpClientConfig, OkHttpClient httpClient, ObjectMapper mapper, HTTPResponseDataDeserializer deserializer, ShardSelector> shardSelector, @@ -47,6 +49,7 @@ public HttpShardedServiceFinderFactory( int nodeRefreshIntervalMs) { this.clientConfig = httpClientConfig; + this.httpClient = httpClient; this.mapper = mapper; this.deserializer = deserializer; this.shardSelector = shardSelector; @@ -58,6 +61,7 @@ public HttpShardedServiceFinderFactory( public ServiceFinder> buildFinder(Service service) { val serviceFinder = new HttpShardedServiceFinderBuilder() .withClientConfig(clientConfig) + .withHttpClient(httpClient) .withObjectMapper(mapper) .withDeserializer(deserializer) .withNamespace(service.getNamespace()) diff --git a/ranger-http/src/main/java/io/appform/ranger/http/servicefinderhub/HttpUnshardedServiceFinderFactory.java b/ranger-http/src/main/java/io/appform/ranger/http/servicefinderhub/HttpUnshardedServiceFinderFactory.java index e85b7719..6347b108 100644 --- a/ranger-http/src/main/java/io/appform/ranger/http/servicefinderhub/HttpUnshardedServiceFinderFactory.java +++ b/ranger-http/src/main/java/io/appform/ranger/http/servicefinderhub/HttpUnshardedServiceFinderFactory.java @@ -27,11 +27,13 @@ import io.appform.ranger.http.servicefinder.HttpUnshardedServiceFinderBuilider; import lombok.Builder; import lombok.val; +import okhttp3.OkHttpClient; public class HttpUnshardedServiceFinderFactory implements ServiceFinderFactory> { private final HttpClientConfig clientConfig; private final ObjectMapper mapper; + private final OkHttpClient httpClient; private final HTTPResponseDataDeserializer deserializer; private final ShardSelector> shardSelector; private final ServiceNodeSelector nodeSelector; @@ -40,7 +42,7 @@ public class HttpUnshardedServiceFinderFactory implements ServiceFinderFactor @Builder public HttpUnshardedServiceFinderFactory( HttpClientConfig httpClientConfig, - ObjectMapper mapper, + ObjectMapper mapper, OkHttpClient httpClient, HTTPResponseDataDeserializer deserializer, ShardSelector> shardSelector, ServiceNodeSelector nodeSelector, @@ -48,6 +50,7 @@ public HttpUnshardedServiceFinderFactory( { this.clientConfig = httpClientConfig; this.mapper = mapper; + this.httpClient = httpClient; this.deserializer = deserializer; this.shardSelector = shardSelector; this.nodeSelector = nodeSelector; @@ -59,6 +62,7 @@ public ServiceFinder> buildFinder(Service service val serviceFinder = new HttpUnshardedServiceFinderBuilider() .withClientConfig(clientConfig) .withObjectMapper(mapper) + .withHttpClient(httpClient) .withDeserializer(deserializer) .withNamespace(service.getNamespace()) .withServiceName(service.getServiceName()) diff --git a/ranger-http/src/main/java/io/appform/ranger/http/serviceprovider/HttpNodeDataSink.java b/ranger-http/src/main/java/io/appform/ranger/http/serviceprovider/HttpNodeDataSink.java index 47ea2c98..23553528 100644 --- a/ranger-http/src/main/java/io/appform/ranger/http/serviceprovider/HttpNodeDataSink.java +++ b/ranger-http/src/main/java/io/appform/ranger/http/serviceprovider/HttpNodeDataSink.java @@ -29,6 +29,7 @@ import lombok.extern.slf4j.Slf4j; import lombok.val; import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.RequestBody; @@ -40,8 +41,8 @@ public class HttpNodeDataSink> extends private final Service service; - public HttpNodeDataSink(Service service, HttpClientConfig config, ObjectMapper mapper) { - super(config, mapper); + public HttpNodeDataSink(Service service, HttpClientConfig config, ObjectMapper mapper, OkHttpClient httpClient) { + super(config, mapper, httpClient); this.service = service; } diff --git a/ranger-http/src/main/java/io/appform/ranger/http/serviceprovider/HttpShardedServiceProviderBuilder.java b/ranger-http/src/main/java/io/appform/ranger/http/serviceprovider/HttpShardedServiceProviderBuilder.java index 98d5a404..402cbdfa 100644 --- a/ranger-http/src/main/java/io/appform/ranger/http/serviceprovider/HttpShardedServiceProviderBuilder.java +++ b/ranger-http/src/main/java/io/appform/ranger/http/serviceprovider/HttpShardedServiceProviderBuilder.java @@ -22,13 +22,18 @@ import io.appform.ranger.core.serviceprovider.ServiceProvider; import io.appform.ranger.http.config.HttpClientConfig; import io.appform.ranger.http.serde.HttpRequestDataSerializer; +import io.appform.ranger.http.utils.RangerHttpUtils; import lombok.extern.slf4j.Slf4j; +import okhttp3.OkHttpClient; + +import java.util.Objects; @Slf4j public class HttpShardedServiceProviderBuilder extends BaseServiceProviderBuilder, HttpRequestDataSerializer> { private HttpClientConfig clientConfig; private ObjectMapper mapper; + private OkHttpClient httpClient; public HttpShardedServiceProviderBuilder withClientConfiguration(final HttpClientConfig clientConfig) { this.clientConfig = clientConfig; @@ -40,6 +45,11 @@ public HttpShardedServiceProviderBuilder withObjectMapper(final ObjectMapper return this; } + public HttpShardedServiceProviderBuilder withHttpClient(final OkHttpClient httpClient) { + this.httpClient = httpClient; + return this; + } + @Override public ServiceProvider> build() { return super.buildProvider(); @@ -47,6 +57,8 @@ public ServiceProvider> build() { @Override protected NodeDataSink> dataSink(Service service) { - return new HttpNodeDataSink<>(service, clientConfig, mapper); + return new HttpNodeDataSink<>(service, clientConfig, mapper, + Objects.requireNonNullElseGet(httpClient, + () -> RangerHttpUtils.httpClient(clientConfig))); } } diff --git a/ranger-http/src/main/java/io/appform/ranger/http/utils/RangerHttpUtils.java b/ranger-http/src/main/java/io/appform/ranger/http/utils/RangerHttpUtils.java new file mode 100644 index 00000000..1b9836a2 --- /dev/null +++ b/ranger-http/src/main/java/io/appform/ranger/http/utils/RangerHttpUtils.java @@ -0,0 +1,30 @@ +package io.appform.ranger.http.utils; + +import io.appform.ranger.http.config.HttpClientConfig; +import lombok.experimental.UtilityClass; +import lombok.extern.slf4j.Slf4j; +import okhttp3.ConnectionPool; +import okhttp3.OkHttpClient; + +import java.util.concurrent.TimeUnit; + +/** + * Set of utilities for http client + */ +@UtilityClass +@Slf4j +public class RangerHttpUtils { + public static OkHttpClient httpClient(final HttpClientConfig config) { + log.info("Creating http client for {}:{}", config.getHost(), config.getPort()); + return new OkHttpClient.Builder() + .callTimeout(config.getOperationTimeoutMs() == 0 + ? 3000 + : config.getOperationTimeoutMs(), TimeUnit.MILLISECONDS) + .connectTimeout(config.getConnectionTimeoutMs() == 0 + ? 3000 + : config.getConnectionTimeoutMs(), TimeUnit.MILLISECONDS) + .followRedirects(true) + .connectionPool(new ConnectionPool(1, 30, TimeUnit.SECONDS)) + .build(); + } +} diff --git a/ranger-http/src/test/java/io/appform/ranger/http/common/HttpNodeDataStoreConnectorTest.java b/ranger-http/src/test/java/io/appform/ranger/http/common/HttpNodeDataStoreConnectorTest.java index 07e76767..79512b4b 100644 --- a/ranger-http/src/test/java/io/appform/ranger/http/common/HttpNodeDataStoreConnectorTest.java +++ b/ranger-http/src/test/java/io/appform/ranger/http/common/HttpNodeDataStoreConnectorTest.java @@ -17,6 +17,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import io.appform.ranger.http.config.HttpClientConfig; +import io.appform.ranger.http.utils.RangerHttpUtils; import lombok.val; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -30,7 +31,8 @@ void testHttpNodeDataStoreConnector(){ .host("localhost-1") .port(80) .build(); - val httpNodeDataStoreConnector = new HttpNodeDataStoreConnector<>(httpClientConfig, objectMapper); + val httpNodeDataStoreConnector = new HttpNodeDataStoreConnector<>(httpClientConfig, objectMapper, + RangerHttpUtils.httpClient(httpClientConfig)); Assertions.assertNotNull(httpNodeDataStoreConnector); Assertions.assertTrue(httpNodeDataStoreConnector.isActive()); } diff --git a/ranger-http/src/test/java/io/appform/ranger/http/servicefinderhub/HttpServiceDataSourceTest.java b/ranger-http/src/test/java/io/appform/ranger/http/servicefinderhub/HttpServiceDataSourceTest.java index 1996364f..9b2c3f05 100644 --- a/ranger-http/src/test/java/io/appform/ranger/http/servicefinderhub/HttpServiceDataSourceTest.java +++ b/ranger-http/src/test/java/io/appform/ranger/http/servicefinderhub/HttpServiceDataSourceTest.java @@ -22,6 +22,7 @@ import io.appform.ranger.core.utils.RangerTestUtils; import io.appform.ranger.http.config.HttpClientConfig; import io.appform.ranger.http.model.ServiceDataSourceResponse; +import io.appform.ranger.http.utils.RangerHttpUtils; import lombok.val; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -54,7 +55,7 @@ void testServiceDataSource(WireMockRuntimeInfo wireMockRuntimeInfo) throws IOExc .connectionTimeoutMs(30_000) .operationTimeoutMs(30_000) .build(); - val httpServiceDataSource = new HttpServiceDataSource<>(clientConfig, MAPPER); + val httpServiceDataSource = new HttpServiceDataSource<>(clientConfig, MAPPER, RangerHttpUtils.httpClient(clientConfig)); val services = httpServiceDataSource.services(); Assertions.assertNotNull(services); Assertions.assertFalse(services.isEmpty()); diff --git a/ranger-hub-server-bundle/pom.xml b/ranger-hub-server-bundle/pom.xml index d3efc881..75e32d34 100644 --- a/ranger-hub-server-bundle/pom.xml +++ b/ranger-hub-server-bundle/pom.xml @@ -1,11 +1,11 @@ + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> ranger io.appform.ranger - 1.0-RC18 + 1.1-RC1 @@ -27,33 +27,55 @@ 2.0.23 - - - io.appform.ranger - ranger-http-client - ${project.version} - - - io.appform.ranger - ranger-zk-client - ${project.version} - - - io.appform.ranger - ranger-server-bundle - ${project.version} - - - io.appform.ranger - ranger-server-common - ${project.version} - - - io.dropwizard - dropwizard-core - ${dropwizard.version} - provided - - + + + io.appform.ranger + ranger-http-client + ${project.version} + + + io.appform.ranger + ranger-zk-client + ${project.version} + + + io.appform.ranger + ranger-server-bundle + ${project.version} + + + io.appform.ranger + ranger-server-common + ${project.version} + + + io.dropwizard + dropwizard-core + + + + org.mockito + mockito-core + ${mockito.version} + test + + + org.wiremock + wiremock-standalone + ${wiremock.version} + test + + + io.dropwizard + dropwizard-testing + test + + + junit + junit + + + + \ No newline at end of file diff --git a/ranger-hub-server-bundle/src/main/java/io/appform/ranger/hub/server/bundle/RangerHubServerBundle.java b/ranger-hub-server-bundle/src/main/java/io/appform/ranger/hub/server/bundle/RangerHubServerBundle.java index 5ce111f0..045c36e3 100644 --- a/ranger-hub-server-bundle/src/main/java/io/appform/ranger/hub/server/bundle/RangerHubServerBundle.java +++ b/ranger-hub-server-bundle/src/main/java/io/appform/ranger/hub/server/bundle/RangerHubServerBundle.java @@ -27,13 +27,16 @@ import io.appform.ranger.core.signals.Signal; import io.appform.ranger.http.config.HttpClientConfig; import io.appform.ranger.http.model.ServiceNodesResponse; +import io.appform.ranger.http.utils.RangerHttpUtils; import io.appform.ranger.hub.server.bundle.configuration.*; import io.appform.ranger.hub.server.bundle.healthcheck.RangerHealthCheck; import io.appform.ranger.hub.server.bundle.lifecycle.CuratorLifecycle; import io.appform.ranger.server.bundle.RangerServerBundle; import io.dropwizard.Configuration; -import lombok.*; +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; import lombok.extern.slf4j.Slf4j; +import lombok.val; import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.retry.RetryForever; @@ -117,6 +120,7 @@ private RangerHubClient> getHttpH .namespace(namespace) .mapper(getMapper()) .clientConfig(httpClientConfig) + .httpClient(RangerHttpUtils.httpClient(httpClientConfig)) .nodeRefreshTimeMs(httpConfiguration.getNodeRefreshTimeMs()) .deserializer(data -> { try { diff --git a/ranger-hub-server-bundle/src/test/java/io/appform/ranger/hub/server/bundle/RangerHubServerBundleTest.java b/ranger-hub-server-bundle/src/test/java/io/appform/ranger/hub/server/bundle/RangerHubServerBundleTest.java new file mode 100644 index 00000000..ab48a7fe --- /dev/null +++ b/ranger-hub-server-bundle/src/test/java/io/appform/ranger/hub/server/bundle/RangerHubServerBundleTest.java @@ -0,0 +1,129 @@ +package io.appform.ranger.hub.server.bundle; + +import com.codahale.metrics.health.HealthCheckRegistry; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; +import com.github.tomakehurst.wiremock.junit5.WireMockTest; +import com.google.common.collect.ImmutableList; +import io.appform.ranger.client.RangerHubClient; +import io.appform.ranger.common.server.ShardInfo; +import io.appform.ranger.core.healthcheck.HealthcheckStatus; +import io.appform.ranger.core.model.Service; +import io.appform.ranger.core.model.ServiceNode; +import io.appform.ranger.http.config.HttpClientConfig; +import io.appform.ranger.http.model.ServiceDataSourceResponse; +import io.appform.ranger.http.model.ServiceNodesResponse; +import io.appform.ranger.hub.server.bundle.configuration.RangerHttpUpstreamConfiguration; +import io.appform.ranger.hub.server.bundle.configuration.RangerServerConfiguration; +import io.appform.ranger.hub.server.bundle.configuration.RangerUpstreamConfiguration; +import io.dropwizard.Configuration; +import io.dropwizard.jersey.DropwizardResourceConfig; +import io.dropwizard.jersey.setup.JerseyEnvironment; +import io.dropwizard.lifecycle.setup.LifecycleEnvironment; +import io.dropwizard.setup.AdminEnvironment; +import io.dropwizard.setup.Bootstrap; +import io.dropwizard.setup.Environment; +import lombok.Getter; +import lombok.SneakyThrows; +import lombok.val; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.List; +import java.util.stream.Collectors; +import java.util.stream.IntStream; + +import static com.github.tomakehurst.wiremock.client.WireMock.*; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +/** + * + */ +@WireMockTest +class RangerHubServerBundleTest { + + @Test + @SneakyThrows + void test(final WireMockRuntimeInfo wm) { + @Getter + class TestConfig extends Configuration { + private final RangerServerConfiguration upstreams = RangerServerConfiguration.builder() + .namespace("test") + .upstreams(ImmutableList.builder() + .add(new RangerHttpUpstreamConfiguration() + .setHttpClientConfigs(List.of(HttpClientConfig.builder() + .host("localhost") + .port(wm.getHttpPort()) + .build()))) + .build()) + .build(); + + } + final TestConfig testConfig = new TestConfig(); + final HealthCheckRegistry healthChecks = mock(HealthCheckRegistry.class); + final JerseyEnvironment jerseyEnvironment = mock(JerseyEnvironment.class); + final LifecycleEnvironment lifecycleEnvironment = mock(LifecycleEnvironment.class); + final Environment environment = mock(Environment.class); + final AdminEnvironment adminEnvironment = mock(AdminEnvironment.class); + final Bootstrap bootstrap = mock(Bootstrap.class); + final ObjectMapper mapper = new ObjectMapper(); + + when(jerseyEnvironment.getResourceConfig()).thenReturn(new DropwizardResourceConfig()); + when(environment.jersey()).thenReturn(jerseyEnvironment); + when(environment.lifecycle()).thenReturn(lifecycleEnvironment); + when(environment.healthChecks()).thenReturn(healthChecks); + when(environment.admin()).thenReturn(adminEnvironment); + when(environment.getObjectMapper()).thenReturn(mapper); + when(bootstrap.getHealthCheckRegistry()).thenReturn(mock(HealthCheckRegistry.class)); + + val bundle = new RangerHubServerBundle() { + + @Override + protected RangerServerConfiguration getRangerConfiguration(TestConfig configuration) { + return testConfig.getUpstreams(); + } + }; + val services = IntStream.rangeClosed(1, 10) + .mapToObj(i -> Service.builder() + .namespace("test") + .serviceName("service-" + i) + .build()) + .collect(Collectors.toUnmodifiableSet()); + stubFor(get("/ranger/services/v1") + .willReturn(okJson(environment.getObjectMapper() + .writeValueAsString(ServiceDataSourceResponse.builder() + .data(services) + .build())))); + stubFor(any(urlMatching("/ranger/nodes/v1/test/service-[0-9]+")) + .willReturn(okJson(mapper.writeValueAsString( + ServiceNodesResponse.builder() + .data(IntStream.rangeClosed(1, 5) + .mapToObj(i -> ServiceNode.builder() + .host("host-" + i) + .port(5000) + .nodeData(ShardInfo.builder() + .environment("blah") + .region("reg") + .build()) + .healthcheckStatus(HealthcheckStatus.healthy) + .lastUpdatedTimeStamp(System.currentTimeMillis()) + .build()) + .toList()) + .build())))); + + bundle.initialize(bootstrap); + bundle.run(testConfig, environment); + bundle.getHubs().forEach(RangerHubClient::start); + IntStream.rangeClosed(1,10) + .forEach(i -> { + val nodes = bundle.getHubs() + .stream() + .flatMap(hub -> hub.getAllNodes(Service.builder().namespace("test").serviceName("service-" + i).build()).stream()) + .toList(); + Assertions.assertEquals(5, nodes.size()); + }); + + } + +} \ No newline at end of file diff --git a/ranger-server-bundle/pom.xml b/ranger-server-bundle/pom.xml index 56a04a42..9bea4a16 100644 --- a/ranger-server-bundle/pom.xml +++ b/ranger-server-bundle/pom.xml @@ -33,6 +33,12 @@ ${mockito.version} test + + org.wiremock + wiremock-standalone + ${wiremock.version} + test + io.appform.ranger ranger-client diff --git a/ranger-server/pom.xml b/ranger-server/pom.xml index 1b6f2538..deb708c8 100644 --- a/ranger-server/pom.xml +++ b/ranger-server/pom.xml @@ -6,7 +6,7 @@ io.appform.ranger ranger - 1.0-RC18 + 1.1-RC1 ranger-server