Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimization to create only one HTTP client per upstream #41

Merged
merged 3 commits into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ target
*.iws
out
dep
.java-version
.java-version
stage.yml
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 @@ -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
Expand All @@ -37,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());
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 @@ -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;

/**
*
*/
Expand All @@ -36,17 +33,9 @@ public class HttpNodeDataStoreConnector<T> implements NodeDataStoreConnector<T>

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;
}
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 @@ -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;

/**
*
Expand All @@ -43,10 +43,11 @@ public class HttpNodeDataSource<T, D extends HTTPResponseDataDeserializer<T>> 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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
*
Expand All @@ -30,6 +34,7 @@ public class HttpShardedServiceFinderBuilder<T> extends SimpleShardedServiceFind

private HttpClientConfig clientConfig;
private ObjectMapper mapper;
private OkHttpClient httpClient;

public HttpShardedServiceFinderBuilder<T> withClientConfig(final HttpClientConfig clientConfig) {
this.clientConfig = clientConfig;
Expand All @@ -41,14 +46,21 @@ public HttpShardedServiceFinderBuilder<T> withObjectMapper(final ObjectMapper ma
return this;
}

public HttpShardedServiceFinderBuilder<T> withHttpClient(final OkHttpClient httpClient){
this.httpClient = httpClient;
return this;
}

@Override
public SimpleShardedServiceFinder<T> build() {
return buildFinder();
}

@Override
protected NodeDataSource<T, HTTPResponseDataDeserializer<T>> dataSource(Service service) {
return new HttpNodeDataSource<>(service, clientConfig, mapper);
return new HttpNodeDataSource<>(service, clientConfig, mapper,
Objects.requireNonNullElseGet(httpClient,
() -> RangerHttpUtils.httpClient(clientConfig)));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>
extends SimpleUnshardedServiceFinderBuilder<T, HttpUnshardedServiceFinderBuilider<T>, HTTPResponseDataDeserializer<T>> {

private HttpClientConfig clientConfig;
private ObjectMapper mapper;
private OkHttpClient httpClient;

public HttpUnshardedServiceFinderBuilider<T> withClientConfig(final HttpClientConfig clientConfig) {
this.clientConfig = clientConfig;
Expand All @@ -39,14 +44,21 @@ public HttpUnshardedServiceFinderBuilider<T> withObjectMapper(final ObjectMapper
return this;
}

public HttpUnshardedServiceFinderBuilider<T> withHttpClient(final OkHttpClient httpClient) {
this.httpClient = httpClient;
return this;
}

@Override
public SimpleUnshardedServiceFinder<T> build() {
return buildFinder();
}

@Override
protected NodeDataSource<T, HTTPResponseDataDeserializer<T>> dataSource(Service service) {
return new HttpNodeDataSource<>(service, clientConfig, mapper);
return new HttpNodeDataSource<>(service, clientConfig, mapper,
Objects.requireNonNullElseGet(httpClient,
() -> RangerHttpUtils.httpClient(clientConfig)));
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -34,8 +35,8 @@
@Slf4j
public class HttpServiceDataSource<T> extends HttpNodeDataStoreConnector<T> implements ServiceDataSource {

public HttpServiceDataSource(HttpClientConfig config, ObjectMapper mapper) {
super(config, mapper);
public HttpServiceDataSource(HttpClientConfig config, ObjectMapper mapper, OkHttpClient httpClient) {
super(config, mapper, httpClient);
}

@Override
Expand Down
Loading
Loading