Skip to content

Commit

Permalink
Optimized to create only one http client per http upstream
Browse files Browse the repository at this point in the history
  • Loading branch information
santanusinha committed Aug 6, 2024
1 parent b546941 commit 0ba1be0
Show file tree
Hide file tree
Showing 20 changed files with 245 additions and 55 deletions.
3 changes: 2 additions & 1 deletion lombok.config
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
lombok.addLombokGeneratedAnnotation = true
lombok.extern.findbugs.addSuppressFBWarnings = true
lombok.extern.findbugs.addSuppressFBWarnings = true
lombok.accessors.chain = true
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@

<maven.compiler.version>3.8.0</maven.compiler.version>
<java.version>17</java.version>
<java.release.version>11</java.release.version>
<java.release.version>17</java.release.version>
<lombok.version>1.18.22</lombok.version>
<annotations.version>3.0.1u2</annotations.version>

Expand Down
8 changes: 1 addition & 7 deletions ranger-http-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,9 @@
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<artifactId>wiremock-standalone</artifactId>
<version>${wiremock.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;

import java.util.Objects;

@Slf4j
@Getter
Expand All @@ -38,12 +41,16 @@ public abstract class AbstractRangerHttpHubClient<T, R extends ServiceRegistry<T
extends AbstractRangerHubClient<T, R, D> {

private final HttpClientConfig clientConfig;

private final OkHttpClient httpClient;

@Builder.Default
private final ServiceNodeSelector<T> nodeSelector = new RandomServiceNodeSelector<>();

@Override
protected ServiceDataSource getDefaultDataSource() {
return new HttpServiceDataSource<>(clientConfig, getMapper(), RangerHttpUtils.httpClient(clientConfig));
return new HttpServiceDataSource<>(clientConfig, getMapper(), Objects.requireNonNullElseGet(getHttpClient(),
() -> RangerHttpUtils.httpClient(clientConfig)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class ShardedRangerHttpHubClient<T>
protected ServiceFinderFactory<T, MapBasedServiceRegistry<T>> getFinderFactory() {
return HttpShardedServiceFinderFactory.<T>builder()
.httpClientConfig(this.getClientConfig())
.httpClient(this.getHttpClient())
.nodeRefreshIntervalMs(getNodeRefreshTimeMs())
.deserializer(getDeserializer())
.shardSelector(shardSelector)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;

@Slf4j
@SuperBuilder
Expand All @@ -39,6 +40,7 @@ public class SimpleRangerHttpClient<T> extends AbstractRangerClient<T, ListBased
private final ObjectMapper mapper;
private final int nodeRefreshIntervalMs;
private final HttpClientConfig clientConfig;
private final OkHttpClient httpClient;
private final HTTPResponseDataDeserializer<T> deserializer;
@Builder.Default
private final ShardSelector<T, ListBasedServiceRegistry<T>> shardSelector = new ListShardSelector<>();
Expand All @@ -58,6 +60,7 @@ public void start() {
.withServiceName(serviceName)
.withNamespace(namespace)
.withObjectMapper(mapper)
.withHttpClient(httpClient)
.withNodeRefreshIntervalMs(nodeRefreshIntervalMs)
.withDeserializer(deserializer)
.withShardSelector(shardSelector)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ public class UnshardedRangerHttpHubClient<T>
protected ServiceFinderFactory<T, ListBasedServiceRegistry<T>> getFinderFactory() {
return HttpUnshardedServiceFinderFactory.<T>builder()
.httpClientConfig(this.getClientConfig())
.httpClient(this.getHttpClient())
.nodeRefreshIntervalMs(getNodeRefreshTimeMs())
.deserializer(getDeserializer())
.shardSelector(shardSelector)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,8 +26,10 @@ class ShardedRangerHttpClientTest extends BaseRangerHttpClientTest {

@Test
void testShardedHttpHubClient(){
val httpClientConfig = getHttpClientConfig();
val client = ShardedRangerHttpHubClient.<TestNodeData>builder()
.clientConfig(getHttpClientConfig())
.clientConfig(httpClientConfig)
.httpClient(RangerHttpUtils.httpClient(httpClientConfig))
.namespace("test-n")
.deserializer(this::read)
.mapper(getObjectMapper())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,9 +26,11 @@ class SimpleRangerHttpClientTest extends BaseRangerHttpClientTest{

@Test
void testSimpleHttpRangerClient(){
val httpClientConfig = getHttpClientConfig();
val client = SimpleRangerHttpClient.<TestNodeData>builder()
.clientConfig(getHttpClientConfig())
.clientConfig(httpClientConfig)
.mapper(getObjectMapper())
.httpClient(RangerHttpUtils.httpClient(httpClientConfig))
.deserializer(this::read)
.namespace("test-n")
.serviceName("test-s")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,8 +26,10 @@ class UnshardedRangerHttpClientTest extends BaseRangerHttpClientTest {

@Test
void testUnshardedRangerHubClient(){
val httpClientConfig = getHttpClientConfig();
val client = UnshardedRangerHttpHubClient.<TestNodeData>builder()
.clientConfig(getHttpClientConfig())
.clientConfig(httpClientConfig)
.httpClient(RangerHttpUtils.httpClient(httpClientConfig))
.namespace("test-n")
.deserializer(this::read)
.mapper(getObjectMapper())
Expand Down
8 changes: 1 addition & 7 deletions ranger-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,9 @@
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<artifactId>wiremock-standalone</artifactId>
<version>${wiremock.version}</version>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.appform.ranger</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.appform.ranger.http.config;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;
Expand All @@ -25,6 +26,7 @@
@Value
@Builder
@Jacksonized
@AllArgsConstructor
public class HttpClientConfig {
String host;
int port;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@
import io.appform.ranger.http.servicefinder.HttpShardedServiceFinderBuilder;
import lombok.Builder;
import lombok.val;
import okhttp3.OkHttpClient;

public class HttpShardedServiceFinderFactory <T> implements ServiceFinderFactory<T, MapBasedServiceRegistry<T>> {

private final HttpClientConfig clientConfig;
private final OkHttpClient httpClient;
private final ObjectMapper mapper;
private final HTTPResponseDataDeserializer<T> deserializer;
private final ShardSelector<T, MapBasedServiceRegistry<T>> shardSelector;
Expand All @@ -39,14 +41,15 @@ public class HttpShardedServiceFinderFactory <T> implements ServiceFinderFactory

@Builder
public HttpShardedServiceFinderFactory(
HttpClientConfig httpClientConfig,
HttpClientConfig httpClientConfig, OkHttpClient httpClient,
ObjectMapper mapper,
HTTPResponseDataDeserializer<T> deserializer,
ShardSelector<T, MapBasedServiceRegistry<T>> shardSelector,
ServiceNodeSelector<T> nodeSelector,
int nodeRefreshIntervalMs)
{
this.clientConfig = httpClientConfig;
this.httpClient = httpClient;
this.mapper = mapper;
this.deserializer = deserializer;
this.shardSelector = shardSelector;
Expand All @@ -58,6 +61,7 @@ public HttpShardedServiceFinderFactory(
public ServiceFinder<T, MapBasedServiceRegistry<T>> buildFinder(Service service) {
val serviceFinder = new HttpShardedServiceFinderBuilder<T>()
.withClientConfig(clientConfig)
.withHttpClient(httpClient)
.withObjectMapper(mapper)
.withDeserializer(deserializer)
.withNamespace(service.getNamespace())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import io.appform.ranger.http.servicefinder.HttpUnshardedServiceFinderBuilider;
import lombok.Builder;
import lombok.val;
import okhttp3.OkHttpClient;

public class HttpUnshardedServiceFinderFactory<T> implements ServiceFinderFactory<T, ListBasedServiceRegistry<T>> {

private final HttpClientConfig clientConfig;
private final ObjectMapper mapper;
private final OkHttpClient httpClient;
private final HTTPResponseDataDeserializer<T> deserializer;
private final ShardSelector<T, ListBasedServiceRegistry<T>> shardSelector;
private final ServiceNodeSelector<T> nodeSelector;
Expand All @@ -40,14 +42,15 @@ public class HttpUnshardedServiceFinderFactory<T> implements ServiceFinderFactor
@Builder
public HttpUnshardedServiceFinderFactory(
HttpClientConfig httpClientConfig,
ObjectMapper mapper,
ObjectMapper mapper, OkHttpClient httpClient,
HTTPResponseDataDeserializer<T> deserializer,
ShardSelector<T, ListBasedServiceRegistry<T>> shardSelector,
ServiceNodeSelector<T> nodeSelector,
int nodeRefreshIntervalMs)
{
this.clientConfig = httpClientConfig;
this.mapper = mapper;
this.httpClient = httpClient;
this.deserializer = deserializer;
this.shardSelector = shardSelector;
this.nodeSelector = nodeSelector;
Expand All @@ -59,6 +62,7 @@ public ServiceFinder<T, ListBasedServiceRegistry<T>> buildFinder(Service service
val serviceFinder = new HttpUnshardedServiceFinderBuilider<T>()
.withClientConfig(clientConfig)
.withObjectMapper(mapper)
.withHttpClient(httpClient)
.withDeserializer(deserializer)
.withNamespace(service.getNamespace())
.withServiceName(service.getServiceName())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.appform.ranger.http.config.HttpClientConfig;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import okhttp3.ConnectionPool;
import okhttp3.OkHttpClient;

Expand All @@ -11,8 +12,10 @@
* 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
Expand Down
90 changes: 59 additions & 31 deletions ranger-hub-server-bundle/pom.xml
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
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">
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">
<parent>
<artifactId>ranger</artifactId>
<groupId>io.appform.ranger</groupId>
<version>1.0-RC18</version>
<version>1.1-RC1</version>
</parent>
<build>
<plugins>
Expand All @@ -27,33 +27,61 @@
<dropwizard.version>2.0.23</dropwizard.version>
</properties>

<dependencies>
<dependency>
<groupId>io.appform.ranger</groupId>
<artifactId>ranger-http-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.appform.ranger</groupId>
<artifactId>ranger-zk-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.appform.ranger</groupId>
<artifactId>ranger-server-bundle</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.appform.ranger</groupId>
<artifactId>ranger-server-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<version>${dropwizard.version}</version>
<scope>provided</scope>
</dependency>
</dependencies>
<dependencies>
<dependency>
<groupId>io.appform.ranger</groupId>
<artifactId>ranger-http-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.appform.ranger</groupId>
<artifactId>ranger-zk-client</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.appform.ranger</groupId>
<artifactId>ranger-server-bundle</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.appform.ranger</groupId>
<artifactId>ranger-server-common</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-core</artifactId>
<!-- <scope>provided</scope>-->
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock-standalone</artifactId>
<version>${wiremock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.dropwizard</groupId>
<artifactId>dropwizard-testing</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- <dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>4.0.4</version>
<scope>test</scope>
</dependency>-->
</dependencies>

</project>
Loading

0 comments on commit 0ba1be0

Please sign in to comment.