Skip to content

Commit

Permalink
0) Renamed UnshardedClusterInfo into UnshardedFinder and introduced a…
Browse files Browse the repository at this point in the history
… ListBasedServiceRegistry along with the MapBased one

1) Added a criteria interface in the ShardSelector to keep it seperate from the T in ServiceNode and trickled the changes upstream to whereever ShardSelector is getting built.
2) Added test cases to the new criteria interface
3) Added Http serivceNodeProvider and serviceFinders, along with the HttpServiceFinderHub
4) Added tests for Http interfaces as well.
5) Added license to the missing files
6) Fixed sonar issues
  • Loading branch information
koushikr committed Oct 18, 2021
1 parent 459e774 commit 4fcfe36
Show file tree
Hide file tree
Showing 132 changed files with 3,065 additions and 736 deletions.
11 changes: 8 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,18 +50,23 @@
<name>Tushar Naik</name>
<email>[email protected]</email>
</developer>
<developer>
<id>koushikr</id>
<name>Koushik Ramachandra</name>
<email>[email protected]</email>
</developer>
</developers>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<guava.version>30.1.1-jre</guava.version>
<curator.version>4.0.1</curator.version>
<curator.version>4.2.0</curator.version>
<jackson.version>2.12.5</jackson.version>
<slf4j.version>1.7.32</slf4j.version>

<maven.compiler.version>3.8.0</maven.compiler.version>
<java.version>1.8</java.version>
<lombok.version>1.18.20</lombok.version>
<lombok.version>1.18.16</lombok.version>

<junit.version>4.13.2</junit.version>
<awaitility.version>4.1.0</awaitility.version>
Expand All @@ -78,7 +83,7 @@
<dependency>
<groupId>com.google.code.findbugs</groupId>
<artifactId>annotations</artifactId>
<version>3.0.1u2</version>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2015 Flipkart Internet Pvt. Ltd.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,7 +16,9 @@

package com.flipkart.ranger.core.finder;

import com.flipkart.ranger.core.finder.signals.ScheduledRegistryUpdateSignal;
import com.flipkart.ranger.core.finder.nodeselector.RandomServiceNodeSelector;
import com.flipkart.ranger.core.finder.serviceregistry.ServiceRegistryUpdater;
import com.flipkart.ranger.core.finder.serviceregistry.signal.ScheduledRegistryUpdateSignal;
import com.flipkart.ranger.core.model.*;
import com.flipkart.ranger.core.signals.Signal;
import com.google.common.base.Preconditions;
Expand All @@ -31,20 +33,22 @@
import java.util.function.Consumer;

@Slf4j
@SuppressWarnings("unchecked")
public abstract class BaseServiceFinderBuilder
<
T,
R extends ServiceRegistry<T>,
F extends ServiceFinder<T, R>,
B extends BaseServiceFinderBuilder<T, R, F, B, D>,
D extends Deserializer<T>> {
F extends ServiceFinder<T, C, R>,
B extends BaseServiceFinderBuilder<T, R, F, B, D, C>,
D extends Deserializer<T>,
C extends Criteria<T>> {

protected String namespace;
protected String serviceName;
protected int nodeRefreshIntervalMs;
protected boolean disablePushUpdaters;
protected D deserializer;
protected ShardSelector<T, R> shardSelector;
protected ShardSelector<T, C, R> shardSelector;
protected ServiceNodeSelector<T> nodeSelector = new RandomServiceNodeSelector<>();
protected final List<Signal<T>> additionalRefreshSignals = new ArrayList<>();
protected final List<Consumer<Void>> startSignalHandlers = Lists.newArrayList();
Expand All @@ -65,7 +69,7 @@ public B withDeserializer(D deserializer) {
return (B)this;
}

public B withShardSelector(ShardSelector<T, R> shardSelector) {
public B withShardSelector(ShardSelector<T, C, R> shardSelector) {
this.shardSelector = shardSelector;
return (B)this;
}
Expand Down Expand Up @@ -150,7 +154,7 @@ protected F buildFinder() {
log.debug("Added additional signal handlers");
}

val updater = new ServiceRegistryUpdater<T, D>(registry, nodeDataSource, signalGenerators, deserializer);
val updater = new ServiceRegistryUpdater<>(registry, nodeDataSource, signalGenerators, deserializer);
finder.getStartSignal()
.registerConsumers(startSignalHandlers)
.registerConsumer(x -> nodeDataSource.start())
Expand All @@ -173,7 +177,7 @@ protected List<Signal<T>> implementationSpecificRefreshSignals(Service service,

protected abstract F buildFinder(
Service service,
ShardSelector<T, R> shardSelector,
ShardSelector<T, C, R> shardSelector,
ServiceNodeSelector<T> nodeSelector);

}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2015 Flipkart Internet Pvt. Ltd.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -16,22 +16,19 @@

package com.flipkart.ranger.core.finder;

import com.flipkart.ranger.core.model.ServiceNode;
import com.flipkart.ranger.core.model.ServiceRegistry;
import com.flipkart.ranger.core.model.ShardSelector;
import com.flipkart.ranger.core.model.*;
import com.flipkart.ranger.core.signals.ExternalTriggeredSignal;
import com.flipkart.ranger.core.model.ServiceNodeSelector;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.Collections;
import java.util.List;

@Slf4j
public abstract class ServiceFinder<T, ServiceRegistryType extends ServiceRegistry<T>> {
public abstract class ServiceFinder<T, C extends Criteria<T>, ServiceRegistryType extends ServiceRegistry<T>> {
@Getter
private final ServiceRegistryType serviceRegistry;
private final ShardSelector<T, ServiceRegistryType> shardSelector;
private final ShardSelector<T, C, ServiceRegistryType> shardSelector;
private final ServiceNodeSelector<T> nodeSelector;
@Getter
private final ExternalTriggeredSignal<Void> startSignal = new ExternalTriggeredSignal<>(() -> null, Collections.emptyList());
Expand All @@ -40,22 +37,22 @@ public abstract class ServiceFinder<T, ServiceRegistryType extends ServiceRegist

protected ServiceFinder(
ServiceRegistryType serviceRegistry,
ShardSelector<T, ServiceRegistryType> shardSelector,
ShardSelector<T, C, ServiceRegistryType> shardSelector,
ServiceNodeSelector<T> nodeSelector) {
this.serviceRegistry = serviceRegistry;
this.shardSelector = shardSelector;
this.nodeSelector = nodeSelector;
}

public ServiceNode<T> get(T criteria) {
public ServiceNode<T> get(C criteria) {
List<ServiceNode<T>> nodes = shardSelector.nodes(criteria, serviceRegistry);
if (null == nodes || nodes.isEmpty()) {
return null;
}
return nodeSelector.select(nodes);
}

public List<ServiceNode<T>> getAll(T criteria) {
public List<ServiceNode<T>> getAll(C criteria) {
return shardSelector.nodes(criteria, serviceRegistry);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2015 Flipkart Internet Pvt. Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -14,15 +14,16 @@
* limitations under the License.
*/

package com.flipkart.ranger.core.finder.sharded;
package com.flipkart.ranger.core.finder;

import com.flipkart.ranger.core.model.ShardSelector;
import com.flipkart.ranger.core.finder.ServiceFinder;
import com.flipkart.ranger.core.finder.serviceregistry.MapBasedServiceRegistry;
import com.flipkart.ranger.core.model.Criteria;
import com.flipkart.ranger.core.model.ServiceNodeSelector;
import com.flipkart.ranger.core.model.ShardSelector;

public class SimpleShardedServiceFinder<T> extends ServiceFinder<T, MapBasedServiceRegistry<T>> {
public class SimpleShardedServiceFinder<T, C extends Criteria<T>> extends ServiceFinder<T, C, MapBasedServiceRegistry<T>> {
public SimpleShardedServiceFinder(MapBasedServiceRegistry<T> serviceRegistry,
ShardSelector<T, MapBasedServiceRegistry<T>> shardSelector,
ShardSelector<T, C, MapBasedServiceRegistry<T>> shardSelector,
ServiceNodeSelector<T> nodeSelector) {
super(serviceRegistry, shardSelector, nodeSelector);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2015 Flipkart Internet Pvt. Ltd.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -14,27 +14,25 @@
* limitations under the License.
*/

package com.flipkart.ranger.core.finder.sharded;
package com.flipkart.ranger.core.finder;

import com.flipkart.ranger.core.finder.BaseServiceFinderBuilder;
import com.flipkart.ranger.core.model.Deserializer;
import com.flipkart.ranger.core.model.Service;
import com.flipkart.ranger.core.model.ServiceNodeSelector;
import com.flipkart.ranger.core.model.ShardSelector;
import com.flipkart.ranger.core.finder.serviceregistry.MapBasedServiceRegistry;
import com.flipkart.ranger.core.finder.shardselector.MatchingShardSelector;
import com.flipkart.ranger.core.model.*;

public abstract class SimpleShardedServiceFinderBuilder<T, B extends SimpleShardedServiceFinderBuilder<T,B, D>, D extends Deserializer<T>>
extends BaseServiceFinderBuilder<T, MapBasedServiceRegistry<T>, SimpleShardedServiceFinder<T>, B, D> {
public abstract class SimpleShardedServiceFinderBuilder<T, B extends SimpleShardedServiceFinderBuilder<T,B, D, C>, D extends Deserializer<T>, C extends Criteria<T>>
extends BaseServiceFinderBuilder<T, MapBasedServiceRegistry<T>, SimpleShardedServiceFinder<T, C>, B, D, C> {

@Override
protected SimpleShardedServiceFinder<T> buildFinder(
protected SimpleShardedServiceFinder<T, C> buildFinder(
Service service,
ShardSelector<T, MapBasedServiceRegistry<T>> shardSelector,
ServiceNodeSelector<T> nodeSelector) {
ShardSelector<T, C, MapBasedServiceRegistry<T>> shardSelector,
ServiceNodeSelector<T> nodeSelector
) {
if (null == shardSelector) {
shardSelector = new MatchingShardSelector<>();
}
final MapBasedServiceRegistry<T> serviceRegistry = new MapBasedServiceRegistry<>(service);
return new SimpleShardedServiceFinder<>(serviceRegistry, shardSelector, nodeSelector);
}

}
Original file line number Diff line number Diff line change
@@ -1,29 +1,30 @@
/**
/*
* Copyright 2015 Flipkart Internet Pvt. Ltd.
*
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.flipkart.ranger.core.finder.unsharded;
package com.flipkart.ranger.core.finder;

import com.flipkart.ranger.core.finder.ServiceFinder;
import com.flipkart.ranger.core.finder.serviceregistry.ListBasedServiceRegistry;
import com.flipkart.ranger.core.model.Criteria;
import com.flipkart.ranger.core.model.ServiceNodeSelector;
import com.flipkart.ranger.core.model.ShardSelector;

public class UnshardedClusterFinder extends ServiceFinder<UnshardedClusterInfo, UnshardedClusterServiceRegistry> {
public UnshardedClusterFinder(UnshardedClusterServiceRegistry serviceRegistry,
ShardSelector<UnshardedClusterInfo, UnshardedClusterServiceRegistry> shardSelector,
ServiceNodeSelector<UnshardedClusterInfo> nodeSelector) {
public class SimpleUnshardedServiceFinder<T, C extends Criteria<T>> extends ServiceFinder<T, C, ListBasedServiceRegistry<T>> {
public SimpleUnshardedServiceFinder(ListBasedServiceRegistry<T> serviceRegistry,
ShardSelector<T, C, ListBasedServiceRegistry<T>> shardSelector,
ServiceNodeSelector<T> nodeSelector) {
super(serviceRegistry, shardSelector, nodeSelector);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Copyright 2015 Flipkart Internet Pvt. Ltd.
* <p>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.flipkart.ranger.core.finder;

import com.flipkart.ranger.core.finder.serviceregistry.ListBasedServiceRegistry;
import com.flipkart.ranger.core.finder.shardselector.ListShardSelector;
import com.flipkart.ranger.core.model.*;

public abstract class SimpleUnshardedServiceFinderBuilder<T, B extends SimpleUnshardedServiceFinderBuilder<T, B, D, C>, D extends Deserializer<T>, C extends Criteria<T>>
extends BaseServiceFinderBuilder<T, ListBasedServiceRegistry<T>, SimpleUnshardedServiceFinder<T, C>, B, D, C> {

@Override
protected SimpleUnshardedServiceFinder<T, C> buildFinder(
Service service,
ShardSelector<T, C, ListBasedServiceRegistry<T>> shardSelector,
ServiceNodeSelector<T> nodeSelector
) {
if (null == shardSelector) {
shardSelector = new ListShardSelector<>();
}
final ListBasedServiceRegistry<T> serviceRegistry = new ListBasedServiceRegistry<>(service);
return new SimpleUnshardedServiceFinder<>(serviceRegistry, shardSelector, nodeSelector);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2015 Flipkart Internet Pvt. Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,8 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.flipkart.ranger.core.finder;
package com.flipkart.ranger.core.finder.nodeselector;

import com.flipkart.ranger.core.model.ServiceNode;
import com.flipkart.ranger.core.model.ServiceNodeSelector;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2015 Flipkart Internet Pvt. Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,8 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.flipkart.ranger.core.finder;
package com.flipkart.ranger.core.finder.nodeselector;

import com.flipkart.ranger.core.model.ServiceNode;
import com.flipkart.ranger.core.model.ServiceNodeSelector;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/**
/*
* Copyright 2015 Flipkart Internet Pvt. Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
Expand All @@ -13,31 +13,32 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package com.flipkart.ranger.core.finder.unsharded;
package com.flipkart.ranger.core.finder.serviceregistry;

import com.flipkart.ranger.core.model.Service;
import com.flipkart.ranger.core.model.ServiceNode;
import com.flipkart.ranger.core.model.ServiceRegistry;
import com.google.common.collect.ImmutableList;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

public class UnshardedClusterServiceRegistry extends ServiceRegistry<UnshardedClusterInfo> {
private AtomicReference<List<ServiceNode<UnshardedClusterInfo>>> nodes
public class ListBasedServiceRegistry<T> extends ServiceRegistry<T> {
private final AtomicReference<List<ServiceNode<T>>> nodes
= new AtomicReference<>();

public UnshardedClusterServiceRegistry(Service service) {
public ListBasedServiceRegistry(Service service) {
super(service);
}

public List<ServiceNode<UnshardedClusterInfo>> nodes() {
return nodes.get();
public List<ServiceNode<T>> nodeList() {
List<ServiceNode<T>> nodes = this.nodes.get();
return null == nodes ? Collections.emptyList() : nodes;
}

@Override
public void updateNodes(List<ServiceNode<UnshardedClusterInfo>> serviceNodes) {
public void updateNodes(List<ServiceNode<T>> serviceNodes) {
nodes.set(ImmutableList.copyOf(serviceNodes));
}
}
Loading

0 comments on commit 4fcfe36

Please sign in to comment.