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

Server to Server Replication #53

Merged
merged 5 commits into from
Dec 19, 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
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,15 @@
import io.appform.ranger.core.finderhub.ServiceFinderFactory;
import io.appform.ranger.core.finderhub.ServiceFinderHub;
import io.appform.ranger.core.model.*;

import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;
import java.util.stream.Collectors;

import io.appform.ranger.core.util.FinderUtils;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;

@Slf4j
@Getter
@SuperBuilder
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,8 @@ List<ServiceNode<T>> getAllNodes(
final Service service,
final Predicate<T> criteria,
final ShardSelector<T, R> shardSelector);

default boolean isReplicationSource() {
return false;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@ public class ServiceFinderHub<T, R extends ServiceRegistry<T>> {

public ServiceFinderHub(
ServiceDataSource serviceDataSource,
ServiceFinderFactory<T, R> finderFactory
) {
ServiceFinderFactory<T, R> finderFactory) {
this(serviceDataSource, finderFactory,
HubConstants.SERVICE_REFRESH_TIMEOUT_MS, HubConstants.HUB_START_TIMEOUT_MS, Set.of());
HubConstants.SERVICE_REFRESH_TIMEOUT_MS, HubConstants.HUB_START_TIMEOUT_MS, Set.of());
}

public ServiceFinderHub(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
import io.appform.ranger.core.model.ServiceRegistry;
import io.appform.ranger.core.signals.ScheduledSignal;
import io.appform.ranger.core.signals.Signal;

import java.util.*;

import lombok.val;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.function.Consumer;

/**
Expand All @@ -40,6 +43,7 @@ public abstract class ServiceFinderHubBuilder<T, R extends ServiceRegistry<T>> {
private final List<Signal<Void>> extraRefreshSignals = new ArrayList<>();
private long serviceRefreshTimeoutMs = HubConstants.SERVICE_REFRESH_TIMEOUT_MS;
private long hubStartTimeoutMs = HubConstants.HUB_START_TIMEOUT_MS;

private Set<String> excludedServices = new HashSet<>();

public ServiceFinderHubBuilder<T, R> withServiceDataSource(ServiceDataSource serviceDataSource) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import io.appform.ranger.core.model.*;
import io.appform.ranger.core.units.TestNodeData;
import io.appform.ranger.core.utils.RangerTestUtils;
import java.util.Set;
import lombok.val;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
Expand All @@ -36,6 +35,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;

class ServiceFinderHubTest {

Expand Down Expand Up @@ -93,17 +93,15 @@ void testDelayedServiceAddition() {
.withServiceName(service.getServiceName())
.withDeserializer(new Deserializer<TestNodeData>() {})
.withSleepDuration(5)
.build(), 1_000, 5_000, Set.of()
);
.build(), 1_000, 5_000, Set.of());
Assertions.assertThrows(IllegalStateException.class, delayedHub::start);
val serviceFinderHub = new ServiceFinderHub<>(new DynamicDataSource(Lists.newArrayList(new Service("NS", "SERVICE"))),
service -> new TestServiceFinderBuilder()
.withNamespace(service.getNamespace())
.withServiceName(service.getServiceName())
.withDeserializer(new Deserializer<TestNodeData>() {})
.withSleepDuration(1)
.build(), 5_000, 5_000, Set.of()
);
.build(), 5_000, 5_000, Set.of());
serviceFinderHub.start();
Assertions.assertTrue(serviceFinderHub.finder(new Service("NS", "SERVICE")).isPresent());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"iterations" : 4,
"threads" : 1,
"forks" : 3,
"mean_ops" : 812476.3197574528
"mean_ops" : 764043.6230133567
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"iterations" : 4,
"threads" : 1,
"forks" : 3,
"mean_ops" : 592802.8071907263
"mean_ops" : 584064.863102317
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@
import io.appform.ranger.core.finderhub.ServiceFinderHub;
import io.appform.ranger.core.model.ServiceNodeSelector;
import io.appform.ranger.core.model.ServiceRegistry;
import io.appform.ranger.drove.common.DroveCommunicator;
import io.appform.ranger.drove.config.DroveUpstreamConfig;
import io.appform.ranger.drove.serde.DroveResponseDataDeserializer;
import io.appform.ranger.drove.servicefinderhub.DroveServiceDataSource;
import io.appform.ranger.drove.servicefinderhub.DroveServiceFinderHubBuilder;
import io.appform.ranger.drove.common.DroveCommunicator;
import lombok.Builder;
import lombok.Getter;
import lombok.experimental.SuperBuilder;
Expand All @@ -34,29 +34,30 @@
@Slf4j
@Getter
@SuperBuilder
public abstract class AbstractRangerDroveHubClient<T, R extends ServiceRegistry<T>, D extends DroveResponseDataDeserializer<T>>
public abstract class AbstractRangerDroveHubClient<T, R extends ServiceRegistry<T>,
D extends DroveResponseDataDeserializer<T>>
extends AbstractRangerHubClient<T, R, D> {

private final DroveUpstreamConfig clientConfig;
private final DroveCommunicator droveCommunicator;
private final DroveUpstreamConfig clientConfig;
private final DroveCommunicator droveCommunicator;

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

@Override
protected ServiceDataSource getDefaultDataSource() {
return new DroveServiceDataSource<>(clientConfig, getMapper(), getNamespace(), droveCommunicator);
}
@Override
protected ServiceDataSource getDefaultDataSource() {
return new DroveServiceDataSource<>(clientConfig, getMapper(), getNamespace(), droveCommunicator);
}

@Override
protected ServiceFinderHub<T, R> buildHub() {
return new DroveServiceFinderHubBuilder<T, R>()
.withServiceDataSource(getServiceDataSource())
.withServiceFinderFactory(getFinderFactory())
.withRefreshFrequencyMs(getNodeRefreshTimeMs())
.withHubStartTimeout(getHubStartTimeoutMs())
.withServiceRefreshTimeout(getServiceRefreshTimeoutMs())
.withExcludedServices(getExcludedServices())
.build();
}
@Override
protected ServiceFinderHub<T, R> buildHub() {
return new DroveServiceFinderHubBuilder<T, R>()
.withServiceDataSource(getServiceDataSource())
.withServiceFinderFactory(getFinderFactory())
.withRefreshFrequencyMs(getNodeRefreshTimeMs())
.withHubStartTimeout(getHubStartTimeoutMs())
.withServiceRefreshTimeout(getServiceRefreshTimeoutMs())
.withExcludedServices(getExcludedServices())
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,9 @@ protected ServiceFinderHub<T, R> buildHub() {
.withExcludedServices(getExcludedServices())
.build();
}

@Override
public boolean isReplicationSource() {
return clientConfig.isReplicationSource();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public void prepareHttpMocks() throws Exception {
ServiceNodesResponse.<TestNodeData>builder()
.data(Lists.newArrayList(node))
.build());
wireMockExtension.stubFor(get(urlEqualTo("/ranger/nodes/v1/test-n/test-s"))
wireMockExtension.stubFor(get(urlPathEqualTo("/ranger/nodes/v1/test-n/test-s"))
.willReturn(aResponse()
.withBody(payload)
.withStatus(200)));
Expand All @@ -72,7 +72,7 @@ public void prepareHttpMocks() throws Exception {
))
.build();
val response = objectMapper.writeValueAsBytes(responseObj);
wireMockExtension.stubFor(get(urlEqualTo("/ranger/services/v1"))
wireMockExtension.stubFor(get(urlPathEqualTo("/ranger/services/v1"))
.willReturn(aResponse()
.withBody(response)
.withStatus(200)));
Expand Down
6 changes: 6 additions & 0 deletions ranger-http/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,5 +55,11 @@
<version>${project.version}</version>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ public class HttpClientConfig {
long connectionTimeoutMs;
long operationTimeoutMs;
long refreshIntervalMillis;
boolean replicationSource;
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public Set<Service> services() {
.host(config.getHost())
.port(config.getPort() == 0 ? defaultPort() : config.getPort())
.encodedPath("/ranger/services/v1")
.addQueryParameter("skipDataFromReplicationSources", Objects.toString(config.isReplicationSource()))
.build();
val request = new Request.Builder()
.url(httpUrl)
Expand Down Expand Up @@ -109,6 +110,7 @@ public List<ServiceNode<T>> listNodes(
.host(config.getHost())
.port(config.getPort() == 0 ? defaultPort() : config.getPort())
.encodedPath(url)
.addQueryParameter("skipDataFromReplicationSources", Objects.toString(config.isReplicationSource()))
.build();
val request = new Request.Builder()
.url(httpUrl)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ void testFinder(WireMockRuntimeInfo wireMockRuntimeInfo) throws Exception {
ServiceNodesResponse.<NodeData>builder()
.data(Collections.singletonList(node))
.build());
stubFor(get(urlEqualTo("/ranger/nodes/v1/testns/test"))
stubFor(get(urlPathEqualTo("/ranger/nodes/v1/testns/test"))
.willReturn(aResponse()
.withBody(payload)
.withStatus(200)));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,68 @@
class HttpServiceDataSourceTest {

private static final ObjectMapper MAPPER = new ObjectMapper();

@Test
void testServiceDataSource(WireMockRuntimeInfo wireMockRuntimeInfo) throws IOException {
val responseObj = ServiceDataSourceResponse.builder()
val responseObjReplicated = ServiceDataSourceResponse.builder()
.data(Sets.newHashSet(
RangerTestUtils.getService("test-n", "test-s"),
RangerTestUtils.getService("test-n", "test-s1"),
RangerTestUtils.getService("test-n", "test-s2")
))
.build();
val response = MAPPER.writeValueAsBytes(responseObj);
stubFor(get(urlEqualTo("/ranger/services/v1"))
stubFor(get(urlEqualTo("/ranger/services/v1?skipDataFromReplicationSources=false"))
.willReturn(aResponse()
.withBody(response)
.withBody(MAPPER.writeValueAsBytes(responseObjReplicated))
.withStatus(200)));
val clientConfig = HttpClientConfig.builder()
.host("127.0.0.1")
.port(wireMockRuntimeInfo.getHttpPort())
.connectionTimeoutMs(30_000)
.operationTimeoutMs(30_000)
val responseObjReplicationSkipped = ServiceDataSourceResponse.builder()
.data(Sets.newHashSet(
RangerTestUtils.getService("test-n", "test-s"),
RangerTestUtils.getService("test-n", "test-s1")))
.build();
val httpServiceDataSource = new HttpServiceDataSource<>(clientConfig, RangerHttpUtils.httpClient(clientConfig, MAPPER));
val services = httpServiceDataSource.services();
Assertions.assertNotNull(services);
Assertions.assertFalse(services.isEmpty());
Assertions.assertEquals(3, services.size());
Assertions.assertFalse(services.stream().noneMatch(each -> each.getServiceName().equalsIgnoreCase("test-s")));
Assertions.assertFalse(services.stream().noneMatch(each -> each.getServiceName().equalsIgnoreCase("test-s1")));
Assertions.assertFalse(services.stream().noneMatch(each -> each.getServiceName().equalsIgnoreCase("test-s2")));
stubFor(get(urlEqualTo("/ranger/services/v1?skipDataFromReplicationSources=true"))
.willReturn(aResponse()
.withBody(MAPPER.writeValueAsBytes(responseObjReplicationSkipped))
.withStatus(200)));
{
val clientConfig = HttpClientConfig.builder()
.host("127.0.0.1")
.port(wireMockRuntimeInfo.getHttpPort())
.connectionTimeoutMs(30_000)
.operationTimeoutMs(30_000)
.build();
val httpServiceDataSource = new HttpServiceDataSource<>(clientConfig,
RangerHttpUtils.httpClient(clientConfig, MAPPER));
val services = httpServiceDataSource.services();
Assertions.assertNotNull(services);
Assertions.assertFalse(services.isEmpty());
Assertions.assertEquals(3, services.size());
Assertions.assertFalse(services.stream()
.noneMatch(each -> each.getServiceName().equalsIgnoreCase("test-s")));
Assertions.assertFalse(services.stream()
.noneMatch(each -> each.getServiceName().equalsIgnoreCase("test-s1")));
Assertions.assertFalse(services.stream()
.noneMatch(each -> each.getServiceName().equalsIgnoreCase("test-s2")));
}
{ //Here we set skip replication data to false. so query param is set
val clientConfig = HttpClientConfig.builder()
.host("127.0.0.1")
.port(wireMockRuntimeInfo.getHttpPort())
.connectionTimeoutMs(30_000)
.operationTimeoutMs(30_000)
.replicationSource(true)
.build();
val httpServiceDataSource = new HttpServiceDataSource<>(clientConfig,
RangerHttpUtils.httpClient(clientConfig, MAPPER));
val services = httpServiceDataSource.services();
Assertions.assertNotNull(services);
Assertions.assertFalse(services.isEmpty());
Assertions.assertEquals(2, services.size());
Assertions.assertFalse(services.stream()
.noneMatch(each -> each.getServiceName().equalsIgnoreCase("test-s")));
Assertions.assertFalse(services.stream()
.noneMatch(each -> each.getServiceName().equalsIgnoreCase("test-s1")));
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void testProvider(WireMockRuntimeInfo wireMockRuntimeInfo) throws Exception {
)
.build());
byte[] requestBytes = MAPPER.writeValueAsBytes(testNode);
stubFor(post(urlEqualTo("/ranger/nodes/v1/add/testns/test"))
stubFor(post(urlPathEqualTo("/ranger/nodes/v1/add/testns/test"))
.withRequestBody(binaryEqualTo(requestBytes))
.willReturn(aResponse()
.withBody(response)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,16 @@
@JsonIgnoreProperties(ignoreUnknown = true)
public class RangerHttpUpstreamConfiguration extends RangerUpstreamConfiguration {

@NotEmpty
@Valid
private List<HttpClientConfig> httpClientConfigs;
@NotEmpty
@Valid
private List<HttpClientConfig> httpClientConfigs;

public RangerHttpUpstreamConfiguration() {
super(BackendType.HTTP);
}

@Override
public <T> T accept(RangerConfigurationVisitor<T> visitor) {
return visitor.visit(this);
}
@Override
public <T> T accept(RangerConfigurationVisitor<T> visitor) {
return visitor.visit(this);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ protected RangerServerConfiguration getRangerConfiguration(TestConfig configurat
.serviceName("service-" + i)
.build())
.collect(Collectors.toUnmodifiableSet());
stubFor(get("/ranger/services/v1")
stubFor(get(urlPathEqualTo("/ranger/services/v1"))
.willReturn(okJson(environment.getObjectMapper()
.writeValueAsString(ServiceDataSourceResponse.builder()
.data(services)
.build()))));
stubFor(any(urlMatching("/ranger/nodes/v1/test/service-[0-9]+"))
stubFor(any(urlPathMatching("/ranger/nodes/v1/test/service-[0-9]+"))
.willReturn(okJson(mapper.writeValueAsString(
ServiceNodesResponse.builder()
.data(IntStream.rangeClosed(1, 5)
Expand Down
Loading
Loading