diff --git a/pom.xml b/pom.xml index 5e6d95f0..7573ca59 100644 --- a/pom.xml +++ b/pom.xml @@ -50,18 +50,23 @@ Tushar Naik tushar.naik@flipkart.com + + koushikr + Koushik Ramachandra + rkoushik.14@gmail.com + UTF-8 30.1.1-jre - 4.0.1 + 4.2.0 2.12.5 1.7.32 3.8.0 1.8 - 1.18.20 + 1.18.16 4.13.2 4.1.0 @@ -78,7 +83,7 @@ com.google.code.findbugs annotations - 3.0.1u2 + 3.0.1 com.google.guava diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/BaseServiceFinderBuilder.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/BaseServiceFinderBuilder.java index b9af724a..a83df33c 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/BaseServiceFinderBuilder.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/BaseServiceFinderBuilder.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. *

* Licensed under the Apache License, Version 2.0 (the "License"); @@ -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; @@ -31,20 +33,22 @@ import java.util.function.Consumer; @Slf4j +@SuppressWarnings("unchecked") public abstract class BaseServiceFinderBuilder < T, R extends ServiceRegistry, - F extends ServiceFinder, - B extends BaseServiceFinderBuilder, - D extends Deserializer> { + F extends ServiceFinder, + B extends BaseServiceFinderBuilder, + D extends Deserializer, + C extends Criteria> { protected String namespace; protected String serviceName; protected int nodeRefreshIntervalMs; protected boolean disablePushUpdaters; protected D deserializer; - protected ShardSelector shardSelector; + protected ShardSelector shardSelector; protected ServiceNodeSelector nodeSelector = new RandomServiceNodeSelector<>(); protected final List> additionalRefreshSignals = new ArrayList<>(); protected final List> startSignalHandlers = Lists.newArrayList(); @@ -65,7 +69,7 @@ public B withDeserializer(D deserializer) { return (B)this; } - public B withShardSelector(ShardSelector shardSelector) { + public B withShardSelector(ShardSelector shardSelector) { this.shardSelector = shardSelector; return (B)this; } @@ -150,7 +154,7 @@ protected F buildFinder() { log.debug("Added additional signal handlers"); } - val updater = new ServiceRegistryUpdater(registry, nodeDataSource, signalGenerators, deserializer); + val updater = new ServiceRegistryUpdater<>(registry, nodeDataSource, signalGenerators, deserializer); finder.getStartSignal() .registerConsumers(startSignalHandlers) .registerConsumer(x -> nodeDataSource.start()) @@ -173,7 +177,7 @@ protected List> implementationSpecificRefreshSignals(Service service, protected abstract F buildFinder( Service service, - ShardSelector shardSelector, + ShardSelector shardSelector, ServiceNodeSelector nodeSelector); } diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/ServiceFinder.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/ServiceFinder.java index d6004f9f..0ab7ab8a 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/ServiceFinder.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/ServiceFinder.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. *

* Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,11 +16,8 @@ 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; @@ -28,10 +25,10 @@ import java.util.List; @Slf4j -public abstract class ServiceFinder> { +public abstract class ServiceFinder, ServiceRegistryType extends ServiceRegistry> { @Getter private final ServiceRegistryType serviceRegistry; - private final ShardSelector shardSelector; + private final ShardSelector shardSelector; private final ServiceNodeSelector nodeSelector; @Getter private final ExternalTriggeredSignal startSignal = new ExternalTriggeredSignal<>(() -> null, Collections.emptyList()); @@ -40,14 +37,14 @@ public abstract class ServiceFinder shardSelector, + ShardSelector shardSelector, ServiceNodeSelector nodeSelector) { this.serviceRegistry = serviceRegistry; this.shardSelector = shardSelector; this.nodeSelector = nodeSelector; } - public ServiceNode get(T criteria) { + public ServiceNode get(C criteria) { List> nodes = shardSelector.nodes(criteria, serviceRegistry); if (null == nodes || nodes.isEmpty()) { return null; @@ -55,7 +52,7 @@ public ServiceNode get(T criteria) { return nodeSelector.select(nodes); } - public List> getAll(T criteria) { + public List> getAll(C criteria) { return shardSelector.nodes(criteria, serviceRegistry); } diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/sharded/SimpleShardedServiceFinder.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/SimpleShardedServiceFinder.java similarity index 70% rename from ranger-core/src/main/java/com/flipkart/ranger/core/finder/sharded/SimpleShardedServiceFinder.java rename to ranger-core/src/main/java/com/flipkart/ranger/core/finder/SimpleShardedServiceFinder.java index 5c0405c3..44e66eb5 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/sharded/SimpleShardedServiceFinder.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/SimpleShardedServiceFinder.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -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 extends ServiceFinder> { +public class SimpleShardedServiceFinder> extends ServiceFinder> { public SimpleShardedServiceFinder(MapBasedServiceRegistry serviceRegistry, - ShardSelector> shardSelector, + ShardSelector> shardSelector, ServiceNodeSelector nodeSelector) { super(serviceRegistry, shardSelector, nodeSelector); } diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/sharded/SimpleShardedServiceFinderBuilder.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/SimpleShardedServiceFinderBuilder.java similarity index 63% rename from ranger-core/src/main/java/com/flipkart/ranger/core/finder/sharded/SimpleShardedServiceFinderBuilder.java rename to ranger-core/src/main/java/com/flipkart/ranger/core/finder/SimpleShardedServiceFinderBuilder.java index 25a2a596..a6aafb65 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/sharded/SimpleShardedServiceFinderBuilder.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/SimpleShardedServiceFinderBuilder.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. *

* Licensed under the Apache License, Version 2.0 (the "License"); @@ -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, D extends Deserializer> - extends BaseServiceFinderBuilder, SimpleShardedServiceFinder, B, D> { +public abstract class SimpleShardedServiceFinderBuilder, D extends Deserializer, C extends Criteria> + extends BaseServiceFinderBuilder, SimpleShardedServiceFinder, B, D, C> { @Override - protected SimpleShardedServiceFinder buildFinder( + protected SimpleShardedServiceFinder buildFinder( Service service, - ShardSelector> shardSelector, - ServiceNodeSelector nodeSelector) { + ShardSelector> shardSelector, + ServiceNodeSelector nodeSelector + ) { if (null == shardSelector) { shardSelector = new MatchingShardSelector<>(); } final MapBasedServiceRegistry serviceRegistry = new MapBasedServiceRegistry<>(service); return new SimpleShardedServiceFinder<>(serviceRegistry, shardSelector, nodeSelector); } - } diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/unsharded/UnshardedClusterFinder.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/SimpleUnshardedServiceFinder.java similarity index 57% rename from ranger-core/src/main/java/com/flipkart/ranger/core/finder/unsharded/UnshardedClusterFinder.java rename to ranger-core/src/main/java/com/flipkart/ranger/core/finder/SimpleUnshardedServiceFinder.java index be55c096..783a7a1c 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/unsharded/UnshardedClusterFinder.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/SimpleUnshardedServiceFinder.java @@ -1,12 +1,12 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. @@ -14,16 +14,17 @@ * 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 { - public UnshardedClusterFinder(UnshardedClusterServiceRegistry serviceRegistry, - ShardSelector shardSelector, - ServiceNodeSelector nodeSelector) { +public class SimpleUnshardedServiceFinder> extends ServiceFinder> { + public SimpleUnshardedServiceFinder(ListBasedServiceRegistry serviceRegistry, + ShardSelector> shardSelector, + ServiceNodeSelector nodeSelector) { super(serviceRegistry, shardSelector, nodeSelector); } } diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/SimpleUnshardedServiceFinderBuilder.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/SimpleUnshardedServiceFinderBuilder.java new file mode 100644 index 00000000..7917b705 --- /dev/null +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/SimpleUnshardedServiceFinderBuilder.java @@ -0,0 +1,37 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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, D extends Deserializer, C extends Criteria> + extends BaseServiceFinderBuilder, SimpleUnshardedServiceFinder, B, D, C> { + + @Override + protected SimpleUnshardedServiceFinder buildFinder( + Service service, + ShardSelector> shardSelector, + ServiceNodeSelector nodeSelector + ) { + if (null == shardSelector) { + shardSelector = new ListShardSelector<>(); + } + final ListBasedServiceRegistry serviceRegistry = new ListBasedServiceRegistry<>(service); + return new SimpleUnshardedServiceFinder<>(serviceRegistry, shardSelector, nodeSelector); + } +} \ No newline at end of file diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/RandomServiceNodeSelector.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/nodeselector/RandomServiceNodeSelector.java similarity index 94% rename from ranger-core/src/main/java/com/flipkart/ranger/core/finder/RandomServiceNodeSelector.java rename to ranger-core/src/main/java/com/flipkart/ranger/core/finder/nodeselector/RandomServiceNodeSelector.java index 3dc91885..6db1c034 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/RandomServiceNodeSelector.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/nodeselector/RandomServiceNodeSelector.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -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; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/RoundRobinServiceNodeSelector.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/nodeselector/RoundRobinServiceNodeSelector.java similarity index 95% rename from ranger-core/src/main/java/com/flipkart/ranger/core/finder/RoundRobinServiceNodeSelector.java rename to ranger-core/src/main/java/com/flipkart/ranger/core/finder/nodeselector/RoundRobinServiceNodeSelector.java index 62ae215d..99b81d14 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/RoundRobinServiceNodeSelector.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/nodeselector/RoundRobinServiceNodeSelector.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -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; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/unsharded/UnshardedClusterServiceRegistry.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/serviceregistry/ListBasedServiceRegistry.java similarity index 67% rename from ranger-core/src/main/java/com/flipkart/ranger/core/finder/unsharded/UnshardedClusterServiceRegistry.java rename to ranger-core/src/main/java/com/flipkart/ranger/core/finder/serviceregistry/ListBasedServiceRegistry.java index d5fe01af..c0e7f0ab 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/unsharded/UnshardedClusterServiceRegistry.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/serviceregistry/ListBasedServiceRegistry.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -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 { - private AtomicReference>> nodes +public class ListBasedServiceRegistry extends ServiceRegistry { + private final AtomicReference>> nodes = new AtomicReference<>(); - public UnshardedClusterServiceRegistry(Service service) { + public ListBasedServiceRegistry(Service service) { super(service); } - public List> nodes() { - return nodes.get(); + public List> nodeList() { + List> nodes = this.nodes.get(); + return null == nodes ? Collections.emptyList() : nodes; } @Override - public void updateNodes(List> serviceNodes) { + public void updateNodes(List> serviceNodes) { nodes.set(ImmutableList.copyOf(serviceNodes)); } } diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/sharded/MapBasedServiceRegistry.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/serviceregistry/MapBasedServiceRegistry.java similarity index 76% rename from ranger-core/src/main/java/com/flipkart/ranger/core/finder/sharded/MapBasedServiceRegistry.java rename to ranger-core/src/main/java/com/flipkart/ranger/core/finder/serviceregistry/MapBasedServiceRegistry.java index 116b411e..afaf6fb3 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/sharded/MapBasedServiceRegistry.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/serviceregistry/MapBasedServiceRegistry.java @@ -1,20 +1,19 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - *

+ * * 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 - *

+ * * http://www.apache.org/licenses/LICENSE-2.0 - *

+ * * 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.sharded; +package com.flipkart.ranger.core.finder.serviceregistry; import com.flipkart.ranger.core.model.Service; import com.flipkart.ranger.core.model.ServiceNode; @@ -23,14 +22,15 @@ import com.google.common.collect.ImmutableListMultimap; import com.google.common.collect.ListMultimap; +import java.util.ArrayList; +import java.util.Collections; import java.util.List; import java.util.concurrent.atomic.AtomicReference; public class MapBasedServiceRegistry extends ServiceRegistry { - private AtomicReference>> nodes = new AtomicReference<>(); + private final AtomicReference>> nodes = new AtomicReference<>(); - public MapBasedServiceRegistry( - Service service) { + public MapBasedServiceRegistry(Service service) { super(service); } @@ -39,6 +39,12 @@ public ListMultimap> nodes() { return null == nodeList ? ImmutableListMultimap.of() : nodeList; } + @Override + public List> nodeList() { + final ListMultimap> nodeList = nodes.get(); + return null == nodeList ? Collections.emptyList() : new ArrayList<>(nodeList.values()); + } + @Override public void updateNodes(List> nodes) { ListMultimap> serviceNodes = ArrayListMultimap.create(); diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/ServiceRegistryUpdater.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/serviceregistry/ServiceRegistryUpdater.java similarity index 89% rename from ranger-core/src/main/java/com/flipkart/ranger/core/finder/ServiceRegistryUpdater.java rename to ranger-core/src/main/java/com/flipkart/ranger/core/finder/serviceregistry/ServiceRegistryUpdater.java index c348036f..5a5a2634 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/ServiceRegistryUpdater.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/serviceregistry/ServiceRegistryUpdater.java @@ -1,20 +1,19 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - *

+ * * 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 - *

+ * * http://www.apache.org/licenses/LICENSE-2.0 - *

+ * * 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; +package com.flipkart.ranger.core.finder.serviceregistry; import com.flipkart.ranger.core.model.Deserializer; import com.flipkart.ranger.core.model.NodeDataSource; @@ -44,13 +43,13 @@ public class ServiceRegistryUpdater> { private final NodeDataSource nodeDataSource; private final D deserializer; - private Lock checkLock = new ReentrantLock(); - private Condition checkCondition = checkLock.newCondition(); + private final Lock checkLock = new ReentrantLock(); + private final Condition checkCondition = checkLock.newCondition(); private boolean checkForUpdate = false; private Future queryThreadFuture; - private ExecutorService executorService = Executors.newFixedThreadPool(1); - private AtomicBoolean initialRefreshCompleted = new AtomicBoolean(false); + private final ExecutorService executorService = Executors.newFixedThreadPool(1); + private final AtomicBoolean initialRefreshCompleted = new AtomicBoolean(false); public ServiceRegistryUpdater( ServiceRegistry serviceRegistry, @@ -126,7 +125,7 @@ private Void queryExecutor() { } } - private void updateRegistry() { + private void updateRegistry() throws InterruptedException { log.debug("Checking for updates on data source for service: {}", serviceRegistry.getService().getServiceName()); if(!nodeDataSource.isActive()) { @@ -134,11 +133,11 @@ private void updateRegistry() { serviceRegistry.getService().getServiceName()); return; } - val nodes = nodeDataSource.refresh(deserializer).orElse(null); - if (null != nodes) { - log.debug("Updating nodelist of size: {} for [{}]", nodes.size(), + val nodeList = nodeDataSource.refresh(deserializer); + if (nodeList.isPresent()) { + log.debug("Updating nodelist of size: {} for [{}]", nodeList.get().size(), serviceRegistry.getService().getServiceName()); - serviceRegistry.updateNodes(nodes); + serviceRegistry.updateNodes(nodeList.get()); initialRefreshCompleted.compareAndSet(false, true); } else { diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/serviceregistry/signal/ScheduledRegistryUpdateSignal.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/serviceregistry/signal/ScheduledRegistryUpdateSignal.java new file mode 100644 index 00000000..c728f923 --- /dev/null +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/serviceregistry/signal/ScheduledRegistryUpdateSignal.java @@ -0,0 +1,36 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.serviceregistry.signal; + +import com.flipkart.ranger.core.model.Service; +import com.flipkart.ranger.core.signals.ScheduledSignal; +import lombok.extern.slf4j.Slf4j; + +import java.util.Collections; + +/** + * + */ +@Slf4j +public class ScheduledRegistryUpdateSignal extends ScheduledSignal { + + public ScheduledRegistryUpdateSignal( + Service service, + long refreshIntervalMillis) { + super(service, () -> null, Collections.emptyList(), refreshIntervalMillis); + } + +} diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/shardselector/ListShardSelector.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/shardselector/ListShardSelector.java new file mode 100644 index 00000000..d72fa6ae --- /dev/null +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/shardselector/ListShardSelector.java @@ -0,0 +1,37 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.shardselector; + +import com.flipkart.ranger.core.finder.serviceregistry.ListBasedServiceRegistry; +import com.flipkart.ranger.core.model.Criteria; +import com.flipkart.ranger.core.model.ServiceNode; +import com.flipkart.ranger.core.model.ShardSelector; + +import java.util.List; +import java.util.stream.Collectors; + +public class ListShardSelector> implements ShardSelector> { + + @Override + public List> nodes(C criteria, ListBasedServiceRegistry serviceRegistry) { + if(null == criteria){ + return serviceRegistry.nodeList(); + } + return serviceRegistry.nodeList().stream().filter(node -> criteria.apply(node.getNodeData())).collect(Collectors.toList()); + } + + +} diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/shardselector/MatchingShardSelector.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/shardselector/MatchingShardSelector.java new file mode 100644 index 00000000..f94b2363 --- /dev/null +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/shardselector/MatchingShardSelector.java @@ -0,0 +1,40 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.shardselector; + +import com.flipkart.ranger.core.finder.serviceregistry.MapBasedServiceRegistry; +import com.flipkart.ranger.core.model.Criteria; +import com.flipkart.ranger.core.model.ServiceNode; +import com.flipkart.ranger.core.model.ShardSelector; + +import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; + +public class MatchingShardSelector> implements ShardSelector> { + + @Override + public List> nodes(C criteria, MapBasedServiceRegistry serviceRegistry) { + if(null == criteria) return serviceRegistry.nodeList(); + return serviceRegistry.nodes() + .entries() + .stream() + .filter(e -> criteria.apply(e.getKey())) + .map(Map.Entry::getValue) + .collect(Collectors.toList()); + } +} diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/sharded/MatchingShardSelector.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/shardselector/NoopShardSelector.java similarity index 62% rename from ranger-core/src/main/java/com/flipkart/ranger/core/finder/sharded/MatchingShardSelector.java rename to ranger-core/src/main/java/com/flipkart/ranger/core/finder/shardselector/NoopShardSelector.java index 5dc212ae..41d3c8fa 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/sharded/MatchingShardSelector.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/shardselector/NoopShardSelector.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,18 +13,18 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +package com.flipkart.ranger.core.finder.shardselector; -package com.flipkart.ranger.core.finder.sharded; - -import com.flipkart.ranger.core.model.ShardSelector; +import com.flipkart.ranger.core.finder.serviceregistry.ListBasedServiceRegistry; +import com.flipkart.ranger.core.model.Criteria; import com.flipkart.ranger.core.model.ServiceNode; +import com.flipkart.ranger.core.model.ShardSelector; import java.util.List; -public class MatchingShardSelector implements ShardSelector> { - +public class NoopShardSelector> implements ShardSelector> { @Override - public List> nodes(T criteria, MapBasedServiceRegistry serviceRegistry) { - return serviceRegistry.nodes().get(criteria); + public List> nodes(C criteria, ListBasedServiceRegistry serviceRegistry) { + return serviceRegistry.nodeList(); } } diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/signals/ScheduledRegistryUpdateSignal.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/signals/ScheduledRegistryUpdateSignal.java deleted file mode 100644 index 63ae6289..00000000 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/signals/ScheduledRegistryUpdateSignal.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.flipkart.ranger.core.finder.signals; - -import com.flipkart.ranger.core.model.Service; -import com.flipkart.ranger.core.signals.ScheduledSignal; -import lombok.extern.slf4j.Slf4j; - -import java.util.Collections; - -/** - * - */ -@Slf4j -public class ScheduledRegistryUpdateSignal extends ScheduledSignal { - - public ScheduledRegistryUpdateSignal( - Service service, - long refreshIntervalMillis) { - super(service, () -> null, Collections.emptyList(), refreshIntervalMillis); - } - -} diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/unsharded/UnshardedFinderBuilder.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finder/unsharded/UnshardedFinderBuilder.java deleted file mode 100644 index b9af94aa..00000000 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/unsharded/UnshardedFinderBuilder.java +++ /dev/null @@ -1,37 +0,0 @@ -/** - * Copyright 2015 Flipkart Internet Pvt. Ltd. - *

- * 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 - *

- * http://www.apache.org/licenses/LICENSE-2.0 - *

- * 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; - -import com.flipkart.ranger.core.model.Deserializer; -import com.flipkart.ranger.core.model.Service; -import com.flipkart.ranger.core.model.ShardSelector; -import com.flipkart.ranger.core.finder.BaseServiceFinderBuilder; -import com.flipkart.ranger.core.model.ServiceNodeSelector; - -public abstract class UnshardedFinderBuilder, D extends Deserializer> - extends BaseServiceFinderBuilder { - - @Override - protected UnshardedClusterFinder buildFinder( - Service service, - ShardSelector shardSelector, - ServiceNodeSelector nodeSelector) { - final UnshardedClusterServiceRegistry unshardedClusterServiceRegistry - = new UnshardedClusterServiceRegistry(service); - return new UnshardedClusterFinder(unshardedClusterServiceRegistry, new NoOpShardSelector(), nodeSelector); - } -} diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/ServiceDataSource.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/ServiceDataSource.java index 7a014377..dc579606 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/ServiceDataSource.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/ServiceDataSource.java @@ -1,3 +1,18 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.finderhub; import com.flipkart.ranger.core.model.Service; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/ServiceFinderFactory.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/ServiceFinderFactory.java index 5c89b2ca..d8b9c52e 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/ServiceFinderFactory.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/ServiceFinderFactory.java @@ -1,13 +1,30 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.finderhub; import com.flipkart.ranger.core.finder.ServiceFinder; +import com.flipkart.ranger.core.model.Criteria; import com.flipkart.ranger.core.model.Service; import com.flipkart.ranger.core.model.ServiceRegistry; /** * */ -public interface ServiceFinderFactory> { +public interface ServiceFinderFactory, R extends ServiceRegistry> { + + ServiceFinder buildFinder(final Service service); - ServiceFinder buildFinder(final Service service); } diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/ServiceFinderHub.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/ServiceFinderHub.java index aceeb761..d47298dc 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/ServiceFinderHub.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/ServiceFinderHub.java @@ -1,6 +1,22 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.finderhub; import com.flipkart.ranger.core.finder.ServiceFinder; +import com.flipkart.ranger.core.model.Criteria; import com.flipkart.ranger.core.model.Service; import com.flipkart.ranger.core.model.ServiceRegistry; import com.flipkart.ranger.core.signals.ExternalTriggeredSignal; @@ -26,12 +42,12 @@ * */ @Slf4j -public class ServiceFinderHub> { - private final AtomicReference>> finders = new AtomicReference<>(new HashMap<>()); +public class ServiceFinderHub, R extends ServiceRegistry> { + private final AtomicReference>> finders = new AtomicReference<>(new HashMap<>()); private final Lock updateLock = new ReentrantLock(); private final Condition updateCond = updateLock.newCondition(); private boolean updateAvailable = false; - private ExecutorService executorService = Executors.newFixedThreadPool(1); + private final ExecutorService executorService = Executors.newFixedThreadPool(1); @Getter private final ExternalTriggeredSignal startSignal @@ -42,15 +58,16 @@ public class ServiceFinderHub> { private final List> refreshSignals = new ArrayList<>(); + @Getter private final ServiceDataSource serviceDataSource; - private final ServiceFinderFactory finderFactory; + private final ServiceFinderFactory finderFactory; - private AtomicBoolean alreadyUpdating = new AtomicBoolean(false); + private final AtomicBoolean alreadyUpdating = new AtomicBoolean(false); private Future monitorFuture = null; public ServiceFinderHub( ServiceDataSource serviceDataSource, - ServiceFinderFactory finderFactory) { + ServiceFinderFactory finderFactory) { this.serviceDataSource = serviceDataSource; this.finderFactory = finderFactory; this.refreshSignals.add(new ScheduledSignal<>("service-hub-updater", @@ -59,7 +76,7 @@ public ServiceFinderHub( 10_000)); } - public Optional> finder(final Service service) { + public Optional> finder(final Service service) { return Optional.ofNullable(finders.get().get(service)); } @@ -126,10 +143,10 @@ private void updateRegistry() { return; } alreadyUpdating.set(true); - final Map> updatedFinders = new HashMap<>(); + final Map> updatedFinders = new HashMap<>(); try { final Collection services = serviceDataSource.services(); - final Map> knownServiceFinders = finders.get(); + final Map> knownServiceFinders = finders.get(); val newFinders = services.stream() .filter(service -> !knownServiceFinders.containsKey(service)) .collect(Collectors.toMap(Function.identity(), finderFactory::buildFinder)); diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ServiceFinderHubBuilder.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/ServiceFinderHubBuilder.java similarity index 53% rename from ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ServiceFinderHubBuilder.java rename to ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/ServiceFinderHubBuilder.java index 91d28c3b..1b883e3b 100644 --- a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ServiceFinderHubBuilder.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/ServiceFinderHubBuilder.java @@ -1,8 +1,21 @@ -package com.flipkart.ranger.zookeeper.zk; +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.finderhub; -import com.flipkart.ranger.core.finderhub.ServiceDataSource; -import com.flipkart.ranger.core.finderhub.ServiceFinderFactory; -import com.flipkart.ranger.core.finderhub.ServiceFinderHub; +import com.flipkart.ranger.core.model.Criteria; import com.flipkart.ranger.core.model.ServiceRegistry; import com.flipkart.ranger.core.signals.ScheduledSignal; import com.flipkart.ranger.core.signals.Signal; @@ -17,47 +30,49 @@ /** * */ -public abstract class ServiceFinderHubBuilder> { +public abstract class ServiceFinderHubBuilder, R extends ServiceRegistry> { private ServiceDataSource serviceDataSource; - private ServiceFinderFactory serviceFinderFactory; + private ServiceFinderFactory serviceFinderFactory; private long refreshFrequencyMs = 10_000; - private List> extraStartSignalConsumers = new ArrayList<>(); - private List> extraStopSignalConsumers = new ArrayList<>(); - private List> extraRefreshSignals = new ArrayList<>(); + private final List> extraStartSignalConsumers = new ArrayList<>(); + private final List> extraStopSignalConsumers = new ArrayList<>(); + private final List> extraRefreshSignals = new ArrayList<>(); - public ServiceFinderHubBuilder withServiceDataSource(ServiceDataSource serviceDataSource) { + public ServiceFinderHubBuilder withServiceDataSource(ServiceDataSource serviceDataSource) { this.serviceDataSource = serviceDataSource; return this; } - public ServiceFinderHubBuilder withServiceFinderFactory(ServiceFinderFactory serviceFinderFactory) { + public ServiceFinderHubBuilder withServiceFinderFactory(ServiceFinderFactory serviceFinderFactory) { this.serviceFinderFactory = serviceFinderFactory; return this; } - - public ServiceFinderHubBuilder withRefreshFrequencyMs(long refreshFrequencyMs) { + + public ServiceFinderHubBuilder withRefreshFrequencyMs(long refreshFrequencyMs) { this.refreshFrequencyMs = refreshFrequencyMs; return this; } - public ServiceFinderHubBuilder withExtraStartSignalConsumer(Consumer consumer) { + public ServiceFinderHubBuilder withExtraStartSignalConsumer(Consumer consumer) { this.extraStartSignalConsumers.add(consumer); return this; } - public ServiceFinderHubBuilder withExtraStopSignalConsumer(Consumer consumer) { + public ServiceFinderHubBuilder withExtraStopSignalConsumer(Consumer consumer) { this.extraStopSignalConsumers.add(consumer); return this; } - public ServiceFinderHubBuilder withExtraRefreshSignal(Signal extraRefreshSignal) { + public ServiceFinderHubBuilder withExtraRefreshSignal(Signal extraRefreshSignal) { this.extraRefreshSignals.add(extraRefreshSignal); return this; } - public ServiceFinderHub build() { + public ServiceFinderHub build() { + preBuild(); Preconditions.checkNotNull(serviceDataSource, "Provide a non-null service data source"); Preconditions.checkNotNull(serviceFinderFactory, "Provide a non-null service finder factory"); + val hub = new ServiceFinderHub<>(serviceDataSource, serviceFinderFactory); final ScheduledSignal refreshSignal = new ScheduledSignal<>("service-hub-refresh-timer", () -> null, @@ -74,9 +89,11 @@ public ServiceFinderHub build() { .registerConsumers(extraStopSignalConsumers) .registerConsumer(x -> refreshSignal.stop()) .registerConsumer(x -> serviceDataSource.stop()); + postBuild(hub); return hub; } protected abstract void preBuild(); - protected abstract void postBuild(ServiceFinderHub serviceFinderHub); + + protected abstract void postBuild(ServiceFinderHub serviceFinderHub); } diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/StaticDataSource.java b/ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/StaticDataSource.java new file mode 100644 index 00000000..11ebe0e6 --- /dev/null +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/finderhub/StaticDataSource.java @@ -0,0 +1,49 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.finderhub; + +import com.flipkart.ranger.core.model.Service; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +import java.util.Collection; +import java.util.List; + +/* +A static data source to be used when we know the services beforehand and don't have to fetch from a source. + */ + +@Slf4j +@AllArgsConstructor +public class StaticDataSource implements ServiceDataSource{ + + private final List services; + + @Override + public Collection services() throws Exception { + return services; + } + + @Override + public void start() { + + } + + @Override + public void stop() { + + } +} diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/HealthChecker.java b/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/HealthChecker.java index 70d6cbb1..3d7dbd3b 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/HealthChecker.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/HealthChecker.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. *

* Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.flipkart.ranger.core.healthcheck; import lombok.extern.slf4j.Slf4j; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/Healthcheck.java b/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/Healthcheck.java index ab266709..100372f0 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/Healthcheck.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/Healthcheck.java @@ -1,12 +1,12 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. @@ -26,5 +26,5 @@ public interface Healthcheck { * * @return health status */ - public HealthcheckStatus check(); + HealthcheckStatus check(); } diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/HealthcheckResult.java b/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/HealthcheckResult.java index f77f1efd..e7b38167 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/HealthcheckResult.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/HealthcheckResult.java @@ -1,3 +1,18 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.healthcheck; import lombok.Builder; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/HealthcheckStatus.java b/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/HealthcheckStatus.java index 22ba575a..4b84fa68 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/HealthcheckStatus.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/HealthcheckStatus.java @@ -1,19 +1,18 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.healthcheck; public enum HealthcheckStatus { diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/Healthchecks.java b/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/Healthchecks.java index 42c24505..a07cc271 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/Healthchecks.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/healthcheck/Healthchecks.java @@ -1,3 +1,18 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.healthcheck; import java.io.File; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/HealthService.java b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/HealthService.java index 02fa9802..3c79b04b 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/HealthService.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/HealthService.java @@ -1,12 +1,12 @@ -/** - * Copyright 2016 Flipkart Internet Pvt. Ltd. - * +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/ServiceHealthAggregator.java b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/ServiceHealthAggregator.java index 094e43fc..2468a633 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/ServiceHealthAggregator.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/ServiceHealthAggregator.java @@ -1,5 +1,5 @@ -/** - * Copyright 2016 Flipkart Internet Pvt. Ltd. +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. *

* Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,10 +16,10 @@ package com.flipkart.ranger.core.healthservice; -import com.flipkart.ranger.core.healthservice.monitor.IsolatedHealthMonitor; -import com.flipkart.ranger.core.healthservice.monitor.Monitor; import com.flipkart.ranger.core.healthcheck.Healthcheck; import com.flipkart.ranger.core.healthcheck.HealthcheckStatus; +import com.flipkart.ranger.core.healthservice.monitor.IsolatedHealthMonitor; +import com.flipkart.ranger.core.healthservice.monitor.Monitor; import com.google.common.collect.Lists; import lombok.extern.slf4j.Slf4j; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/TimeEntity.java b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/TimeEntity.java index ffdadefb..cde306cd 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/TimeEntity.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/TimeEntity.java @@ -1,19 +1,18 @@ -/** - * Copyright 2016 Flipkart Internet Pvt. Ltd. - * +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.healthservice; import com.google.common.base.MoreObjects; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/IsolatedHealthMonitor.java b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/IsolatedHealthMonitor.java index 19511975..d3bf3bf6 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/IsolatedHealthMonitor.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/IsolatedHealthMonitor.java @@ -1,12 +1,12 @@ -/** - * Copyright 2016 Flipkart Internet Pvt. Ltd. - * +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. @@ -16,10 +16,10 @@ package com.flipkart.ranger.core.healthservice.monitor; +import com.flipkart.ranger.core.healthservice.TimeEntity; import com.flipkart.ranger.core.healthservice.monitor.sample.CountMonitor; import com.flipkart.ranger.core.healthservice.monitor.sample.PingCheckMonitor; import com.flipkart.ranger.core.healthservice.monitor.sample.RotationStatusMonitor; -import com.flipkart.ranger.core.healthservice.TimeEntity; import java.util.Date; import java.util.concurrent.atomic.AtomicBoolean; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/Monitor.java b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/Monitor.java index c74b31e0..2f2ab700 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/Monitor.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/Monitor.java @@ -1,12 +1,12 @@ -/** - * Copyright 2016 Flipkart Internet Pvt. Ltd. - * +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/Monitors.java b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/Monitors.java index b4578a16..769ae8cd 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/Monitors.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/Monitors.java @@ -1,19 +1,18 @@ -/** - * Copyright 2016 Flipkart Internet Pvt. Ltd. - * +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.healthservice.monitor; import com.flipkart.ranger.core.healthcheck.HealthcheckStatus; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/RollingWindowHealthQueue.java b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/RollingWindowHealthQueue.java index 3fdb0b27..6a0aab1d 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/RollingWindowHealthQueue.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/RollingWindowHealthQueue.java @@ -1,12 +1,12 @@ -/** - * Copyright 2016 Flipkart Internet Pvt. Ltd. - * +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/sample/CountMonitor.java b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/sample/CountMonitor.java index 2529bfa5..de3e1551 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/sample/CountMonitor.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/sample/CountMonitor.java @@ -1,24 +1,23 @@ -/** - * Copyright 2016 Flipkart Internet Pvt. Ltd. - * +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.healthservice.monitor.sample; -import com.flipkart.ranger.core.healthservice.monitor.IsolatedHealthMonitor; import com.flipkart.ranger.core.healthcheck.HealthcheckStatus; import com.flipkart.ranger.core.healthservice.TimeEntity; +import com.flipkart.ranger.core.healthservice.monitor.IsolatedHealthMonitor; /** * A monitor that can be used as a counting monitor to check if any countable entity breaches a threashhold diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/sample/DiskSpaceMonitor.java b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/sample/DiskSpaceMonitor.java index 59088729..10cca535 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/sample/DiskSpaceMonitor.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/sample/DiskSpaceMonitor.java @@ -1,19 +1,18 @@ -/** - * Copyright 2016 Flipkart Internet Pvt. Ltd. - * +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.healthservice.monitor.sample; import com.flipkart.ranger.core.healthservice.TimeEntity; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/sample/PingCheckMonitor.java b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/sample/PingCheckMonitor.java index 92fdb7c1..b9385320 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/sample/PingCheckMonitor.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/sample/PingCheckMonitor.java @@ -1,19 +1,18 @@ -/** - * Copyright 2016 Flipkart Internet Pvt. Ltd. - * +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.healthservice.monitor.sample; import com.flipkart.ranger.core.healthcheck.HealthcheckStatus; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/sample/RotationStatusMonitor.java b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/sample/RotationStatusMonitor.java index 5a2f9039..655eadc0 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/sample/RotationStatusMonitor.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/healthservice/monitor/sample/RotationStatusMonitor.java @@ -1,5 +1,23 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.healthservice.monitor.sample; + /** - * Copyright 2016 Flipkart Internet Pvt. Ltd. + * Copyright 2015 Flipkart Internet Pvt. Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,9 +31,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -package com.flipkart.ranger.core.healthservice.monitor.sample; - import com.flipkart.ranger.core.healthcheck.HealthcheckStatus; import com.flipkart.ranger.core.healthservice.TimeEntity; import com.flipkart.ranger.core.healthservice.monitor.IsolatedHealthMonitor; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/unsharded/UnshardedClusterInfo.java b/ranger-core/src/main/java/com/flipkart/ranger/core/model/Criteria.java similarity index 69% rename from ranger-core/src/main/java/com/flipkart/ranger/core/finder/unsharded/UnshardedClusterInfo.java rename to ranger-core/src/main/java/com/flipkart/ranger/core/model/Criteria.java index 93d45fce..a9baf215 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/unsharded/UnshardedClusterInfo.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/model/Criteria.java @@ -1,29 +1,23 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.model; -package com.flipkart.ranger.core.finder.unsharded; +@FunctionalInterface +public interface Criteria { -public class UnshardedClusterInfo { - @Override - public int hashCode() { - return 0; - } + boolean apply(T nodeData); - @Override - public boolean equals(Object obj) { - return super.equals(obj); - } } diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/model/Deserializer.java b/ranger-core/src/main/java/com/flipkart/ranger/core/model/Deserializer.java index c4acf561..74aebe5e 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/model/Deserializer.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/model/Deserializer.java @@ -1,19 +1,18 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.model; public interface Deserializer { diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/model/NodeDataSink.java b/ranger-core/src/main/java/com/flipkart/ranger/core/model/NodeDataSink.java index 92684451..2b6b8ac8 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/model/NodeDataSink.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/model/NodeDataSink.java @@ -1,3 +1,18 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.model; /** diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/model/NodeDataSource.java b/ranger-core/src/main/java/com/flipkart/ranger/core/model/NodeDataSource.java index e11ccac9..79f75a8d 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/model/NodeDataSource.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/model/NodeDataSource.java @@ -1,3 +1,18 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.model; import java.util.List; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/model/NodeDataStoreConnector.java b/ranger-core/src/main/java/com/flipkart/ranger/core/model/NodeDataStoreConnector.java index e2cab33a..1b60442b 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/model/NodeDataStoreConnector.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/model/NodeDataStoreConnector.java @@ -1,3 +1,18 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.model; /** diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/model/Serializer.java b/ranger-core/src/main/java/com/flipkart/ranger/core/model/Serializer.java index c4e8d401..ab37c6ad 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/model/Serializer.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/model/Serializer.java @@ -1,12 +1,12 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/model/Service.java b/ranger-core/src/main/java/com/flipkart/ranger/core/model/Service.java index ec3e7fe0..a9f77786 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/model/Service.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/model/Service.java @@ -1,19 +1,18 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.model; import lombok.AccessLevel; @@ -23,8 +22,10 @@ @Data @AllArgsConstructor(access = AccessLevel.PUBLIC) public class Service { - private final String namespace; - private final String serviceName; + private String namespace; + private String serviceName; + + public Service(){} public String name() { return String.format("%s/%s", namespace, serviceName); diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/model/ServiceNode.java b/ranger-core/src/main/java/com/flipkart/ranger/core/model/ServiceNode.java index a0563f31..383c6a36 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/model/ServiceNode.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/model/ServiceNode.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -12,8 +12,8 @@ * 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.model; import com.flipkart.ranger.core.healthcheck.HealthcheckStatus; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/model/ServiceNodeSelector.java b/ranger-core/src/main/java/com/flipkart/ranger/core/model/ServiceNodeSelector.java index d655b7ff..786c03c4 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/model/ServiceNodeSelector.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/model/ServiceNodeSelector.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. *

* Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,7 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.flipkart.ranger.core.model; import java.util.List; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/model/ServiceRegistry.java b/ranger-core/src/main/java/com/flipkart/ranger/core/model/ServiceRegistry.java index 09209aaf..60bd8041 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/model/ServiceRegistry.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/model/ServiceRegistry.java @@ -1,19 +1,18 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.model; import lombok.AccessLevel; @@ -27,5 +26,7 @@ public abstract class ServiceRegistry { private final Service service; + public abstract List> nodeList(); + public abstract void updateNodes(List> nodes); } diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/model/ShardSelector.java b/ranger-core/src/main/java/com/flipkart/ranger/core/model/ShardSelector.java index 7cea8c84..4053fe07 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/model/ShardSelector.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/model/ShardSelector.java @@ -1,23 +1,24 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.model; import java.util.List; -public interface ShardSelector> { - List> nodes(T criteria, ServiceRegistryType serviceRegistry); +public interface ShardSelector, ServiceRegistryType extends ServiceRegistry> { + + List> nodes(C criteria, ServiceRegistryType serviceRegistry); + } diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/serviceprovider/BaseServiceProviderBuilder.java b/ranger-core/src/main/java/com/flipkart/ranger/core/serviceprovider/BaseServiceProviderBuilder.java index 38db70af..ff6c0597 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/serviceprovider/BaseServiceProviderBuilder.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/serviceprovider/BaseServiceProviderBuilder.java @@ -1,12 +1,12 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - *

+ *

* 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 - *

+ *

* http://www.apache.org/licenses/LICENSE-2.0 - *

+ *

* 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. @@ -22,7 +22,10 @@ import com.flipkart.ranger.core.healthservice.HealthService; import com.flipkart.ranger.core.healthservice.ServiceHealthAggregator; import com.flipkart.ranger.core.healthservice.monitor.IsolatedHealthMonitor; -import com.flipkart.ranger.core.model.*; +import com.flipkart.ranger.core.model.NodeDataSink; +import com.flipkart.ranger.core.model.Serializer; +import com.flipkart.ranger.core.model.Service; +import com.flipkart.ranger.core.model.ServiceNode; import com.flipkart.ranger.core.signals.ScheduledSignal; import com.flipkart.ranger.core.signals.Signal; import com.google.common.base.Preconditions; @@ -48,11 +51,11 @@ public abstract class BaseServiceProviderBuilder healthchecks = Lists.newArrayList(); protected NodeDataSink nodeDataSource = null; + protected List healthchecks = Lists.newArrayList(); protected final List> startSignalHandlers = Lists.newArrayList(); protected final List> stopSignalHandlers = Lists.newArrayList(); - protected final List> extraRefreshSignals = Lists.newArrayList(); + protected final List> additionalRefreshSignals = Lists.newArrayList(); /* list of isolated monitors */ private List isolatedMonitors = Lists.newArrayList(); @@ -141,13 +144,13 @@ public B withStopSignalHandlers(List> stopSignalHandlers) { return (B)this; } - public B withExtraRefreshSignal(Signal extraRefreshSignal) { - this.extraRefreshSignals.add(extraRefreshSignal); + public B withadditionalRefreshSignal(Signal additionalRefreshSignal) { + this.additionalRefreshSignals.add(additionalRefreshSignal); return (B)this; } - public B withExtraRefreshSignals(List> extraRefreshSignals) { - this.extraRefreshSignals.addAll(extraRefreshSignals); + public B withAdditionalRefreshSignals(List> additionalRefreshSignals) { + this.additionalRefreshSignals.addAll(additionalRefreshSignals); return (B)this; } @@ -175,21 +178,25 @@ protected final ServiceProvider buildProvider() { for (IsolatedHealthMonitor isolatedMonitor : isolatedMonitors) { serviceHealthAggregator.addIsolatedMonitor(isolatedMonitor); } + healthchecks.add(serviceHealthAggregator); final Service service = new Service(namespace, serviceName); + final NodeDataSink usableNodeDataSource = dataSink(service); + final ScheduledSignal healthcheckUpdateSignalGenerator = new ScheduledSignal<>( service, new HealthChecker(healthchecks, staleUpdateThresholdMs), Collections.emptyList(), - healthUpdateIntervalMs); - final NodeDataSink usableNodeDataSource = dataSink(service); + healthUpdateIntervalMs + ); + final List healthServices = Collections.singletonList(serviceHealthAggregator); final List> signalGenerators = ImmutableList.>builder() .add(healthcheckUpdateSignalGenerator) - .addAll(extraRefreshSignals) + .addAll(additionalRefreshSignals) .build(); final ServiceProvider serviceProvider = new ServiceProvider<>(service, new ServiceNode<>(hostname, port, nodeData), diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/serviceprovider/ServiceProvider.java b/ranger-core/src/main/java/com/flipkart/ranger/core/serviceprovider/ServiceProvider.java index 2c19997e..b2f381f0 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/serviceprovider/ServiceProvider.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/serviceprovider/ServiceProvider.java @@ -1,12 +1,12 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. @@ -16,10 +16,13 @@ package com.flipkart.ranger.core.serviceprovider; -import com.flipkart.ranger.core.model.*; +import com.flipkart.ranger.core.healthcheck.HealthcheckResult; +import com.flipkart.ranger.core.model.NodeDataSink; +import com.flipkart.ranger.core.model.Serializer; +import com.flipkart.ranger.core.model.Service; +import com.flipkart.ranger.core.model.ServiceNode; import com.flipkart.ranger.core.signals.ExternalTriggeredSignal; import com.flipkart.ranger.core.signals.Signal; -import com.flipkart.ranger.core.healthcheck.HealthcheckResult; import lombok.Getter; import lombok.extern.slf4j.Slf4j; @@ -32,6 +35,7 @@ public class ServiceProvider> { private final Service service; private final ServiceNode serviceNode; private final S serializer; + @Getter private final NodeDataSink dataSink; @Getter private final ExternalTriggeredSignal startSignal = new ExternalTriggeredSignal<>(() -> null, Collections.emptyList()); diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/signals/ExternalTriggeredSignal.java b/ranger-core/src/main/java/com/flipkart/ranger/core/signals/ExternalTriggeredSignal.java index 2dc41acd..350a2005 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/signals/ExternalTriggeredSignal.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/signals/ExternalTriggeredSignal.java @@ -1,3 +1,18 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.signals; import java.util.List; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/signals/ScheduledSignal.java b/ranger-core/src/main/java/com/flipkart/ranger/core/signals/ScheduledSignal.java index 71b39886..ab6b93dd 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/signals/ScheduledSignal.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/signals/ScheduledSignal.java @@ -1,3 +1,18 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.signals; import com.flipkart.ranger.core.model.Service; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/signals/Signal.java b/ranger-core/src/main/java/com/flipkart/ranger/core/signals/Signal.java index 354b3d4a..cb03ff6f 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/signals/Signal.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/signals/Signal.java @@ -1,3 +1,18 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.signals; import lombok.val; diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/util/Exceptions.java b/ranger-core/src/main/java/com/flipkart/ranger/core/util/Exceptions.java index 69f75f2f..d93d167e 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/util/Exceptions.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/util/Exceptions.java @@ -1,3 +1,18 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.util; /** diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/util/FinderUtils.java b/ranger-core/src/main/java/com/flipkart/ranger/core/util/FinderUtils.java index 204dd4b5..54fa9545 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/util/FinderUtils.java +++ b/ranger-core/src/main/java/com/flipkart/ranger/core/util/FinderUtils.java @@ -1,3 +1,18 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.util; import com.flipkart.ranger.core.healthcheck.HealthcheckStatus; diff --git a/ranger-core/src/test/java/com/flipkart/ranger/core/TestUtils.java b/ranger-core/src/test/java/com/flipkart/ranger/core/TestUtils.java new file mode 100644 index 00000000..cde529a6 --- /dev/null +++ b/ranger-core/src/test/java/com/flipkart/ranger/core/TestUtils.java @@ -0,0 +1,29 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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; + +import java.util.concurrent.TimeUnit; + +import static org.awaitility.Awaitility.await; + +/** + * + */ +public class TestUtils { + public static void sleepForSeconds(int numSeconds) { + await().pollDelay(numSeconds, TimeUnit.SECONDS).until(() -> true); + } +} diff --git a/ranger-core/src/test/java/com/flipkart/ranger/core/finder/SimpleShardFinderTest.java b/ranger-core/src/test/java/com/flipkart/ranger/core/finder/SimpleShardFinderTest.java new file mode 100644 index 00000000..404d92fa --- /dev/null +++ b/ranger-core/src/test/java/com/flipkart/ranger/core/finder/SimpleShardFinderTest.java @@ -0,0 +1,54 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.nodeselector.RoundRobinServiceNodeSelector; +import com.flipkart.ranger.core.finder.serviceregistry.MapBasedServiceRegistry; +import com.flipkart.ranger.core.model.Criteria; +import com.flipkart.ranger.core.model.ServiceNode; +import com.flipkart.ranger.core.model.ShardSelector; +import com.flipkart.ranger.core.units.TestNodeData; +import com.flipkart.ranger.core.utils.CriteriaUtils; +import com.flipkart.ranger.core.utils.RegistryTestUtils; +import com.google.common.collect.Lists; +import lombok.val; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class SimpleShardFinderTest { + + static class TestSimpleShardSelector> implements ShardSelector>{ + + @Override + public List> nodes(C criteria, MapBasedServiceRegistry serviceRegistry) { + return Lists.newArrayList(); + } + } + + @Test + public void testSimpleShardedFinder(){ + final MapBasedServiceRegistry serviceRegistry = RegistryTestUtils.getServiceRegistry(); + final TestSimpleShardSelector> shardSelector = new TestSimpleShardSelector<>(); + final RoundRobinServiceNodeSelector roundRobinServiceNodeSelector = new RoundRobinServiceNodeSelector<>(); + val simpleShardedFinder = new SimpleShardedServiceFinder<>( + serviceRegistry, shardSelector, roundRobinServiceNodeSelector); + val testNodeDataServiceNode = simpleShardedFinder.get( + CriteriaUtils.getCriteria(2)); + Assert.assertNull(testNodeDataServiceNode); + } +} diff --git a/ranger-core/src/test/java/com/flipkart/ranger/core/finder/UnshardedClusterFinderTest.java b/ranger-core/src/test/java/com/flipkart/ranger/core/finder/UnshardedClusterFinderTest.java new file mode 100644 index 00000000..ad35be35 --- /dev/null +++ b/ranger-core/src/test/java/com/flipkart/ranger/core/finder/UnshardedClusterFinderTest.java @@ -0,0 +1,53 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.Criteria; +import com.flipkart.ranger.core.model.ServiceNode; +import com.flipkart.ranger.core.model.ServiceNodeSelector; +import com.flipkart.ranger.core.units.TestNodeData; +import com.flipkart.ranger.core.utils.CriteriaUtils; +import com.flipkart.ranger.core.utils.RegistryTestUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class UnshardedClusterFinderTest { + + static class TestUnshardedNodeSelector implements ServiceNodeSelector{ + + @Override + public ServiceNode select(List> serviceNodes) { + return serviceNodes.get(0); + } + } + @Test + public void unshardedClusterFinder(){ + final ListBasedServiceRegistry unshardedRegistry = RegistryTestUtils.getUnshardedRegistry(); + final ListShardSelector> shardSelector = new ListShardSelector<>(); + SimpleUnshardedServiceFinder> simpleUnshardedServiceFinder = new SimpleUnshardedServiceFinder<>( + unshardedRegistry, + shardSelector, + new TestUnshardedNodeSelector() + ); + final ServiceNode serviceNode = simpleUnshardedServiceFinder.get(CriteriaUtils.getCriteria(1)); + Assert.assertNotNull(serviceNode); + Assert.assertEquals("localhost-1", serviceNode.getHost()); + } +} diff --git a/ranger-core/src/test/java/com/flipkart/ranger/core/finder/nodeselector/RoundRobinServiceNodeSelectorTest.java b/ranger-core/src/test/java/com/flipkart/ranger/core/finder/nodeselector/RoundRobinServiceNodeSelectorTest.java new file mode 100644 index 00000000..70563099 --- /dev/null +++ b/ranger-core/src/test/java/com/flipkart/ranger/core/finder/nodeselector/RoundRobinServiceNodeSelectorTest.java @@ -0,0 +1,41 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.nodeselector; + +import com.flipkart.ranger.core.model.ServiceNode; +import com.flipkart.ranger.core.units.TestNodeData; +import com.google.common.collect.Lists; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class RoundRobinServiceNodeSelectorTest { + @Test + public void testRandomNodeSelector(){ + final RoundRobinServiceNodeSelector roundRobinSelector = new RoundRobinServiceNodeSelector(); + List> serviceNodes = Lists.newArrayList(); + serviceNodes.add(new ServiceNode<>("localhost-1", 9000, TestNodeData.builder().nodeId(1).build())); + serviceNodes.add(new ServiceNode<>("localhost-2", 9001, TestNodeData.builder().nodeId(2).build())); + serviceNodes.add(new ServiceNode<>("localhost-3", 9002, TestNodeData.builder().nodeId(3).build())); + ServiceNode select = roundRobinSelector.select(serviceNodes); + Assert.assertTrue(select.getHost().equalsIgnoreCase("localhost-2")); + select = roundRobinSelector.select(serviceNodes); + Assert.assertTrue(select.getHost().equalsIgnoreCase("localhost-3")); + select = roundRobinSelector.select(serviceNodes); + Assert.assertTrue(select.getHost().equalsIgnoreCase("localhost-1")); + } +} diff --git a/ranger-core/src/test/java/com/flipkart/ranger/core/finder/serviceregistry/MapBasedServiceRegistryTest.java b/ranger-core/src/test/java/com/flipkart/ranger/core/finder/serviceregistry/MapBasedServiceRegistryTest.java new file mode 100644 index 00000000..effc6f41 --- /dev/null +++ b/ranger-core/src/test/java/com/flipkart/ranger/core/finder/serviceregistry/MapBasedServiceRegistryTest.java @@ -0,0 +1,40 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.serviceregistry; + +import com.flipkart.ranger.core.finder.shardselector.MatchingShardSelector; +import com.flipkart.ranger.core.model.Criteria; +import com.flipkart.ranger.core.units.TestNodeData; +import com.flipkart.ranger.core.utils.CriteriaUtils; +import com.flipkart.ranger.core.utils.RegistryTestUtils; +import lombok.val; +import org.junit.Assert; +import org.junit.Test; + +public class MapBasedServiceRegistryTest { + + @Test + public void testMapBasedServiceRegistryWithMatchingShardSelector(){ + final MapBasedServiceRegistry serviceRegistry = RegistryTestUtils.getServiceRegistry(); + Assert.assertTrue(null != serviceRegistry.nodes() && !serviceRegistry.nodes().isEmpty()); + final MatchingShardSelector> matchingShardSelector = new MatchingShardSelector<>(); + val nodes = matchingShardSelector.nodes( + CriteriaUtils.getCriteria(1), serviceRegistry); + Assert.assertFalse(nodes.isEmpty()); + Assert.assertEquals("localhost-1", nodes.get(0).getHost()); + } + +} diff --git a/ranger-core/src/test/java/com/flipkart/ranger/core/finder/shardselector/ListShardSelectorTest.java b/ranger-core/src/test/java/com/flipkart/ranger/core/finder/shardselector/ListShardSelectorTest.java new file mode 100644 index 00000000..1f747c24 --- /dev/null +++ b/ranger-core/src/test/java/com/flipkart/ranger/core/finder/shardselector/ListShardSelectorTest.java @@ -0,0 +1,39 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.shardselector; + +import com.flipkart.ranger.core.finder.serviceregistry.ListBasedServiceRegistry; +import com.flipkart.ranger.core.model.Criteria; +import com.flipkart.ranger.core.model.ServiceNode; +import com.flipkart.ranger.core.units.TestNodeData; +import com.flipkart.ranger.core.utils.CriteriaUtils; +import com.flipkart.ranger.core.utils.RegistryTestUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class ListShardSelectorTest { + + @Test + public void testNoOpShardSelector(){ + final ListBasedServiceRegistry serviceRegistry = RegistryTestUtils.getUnshardedRegistry(); + final ListShardSelector> shardSelector = new ListShardSelector<>(); + final List> nodes = shardSelector.nodes(CriteriaUtils.getCriteria(1), serviceRegistry); + Assert.assertFalse(nodes.isEmpty()); + Assert.assertEquals("localhost-1", nodes.get(0).getHost()); + } +} diff --git a/ranger-core/src/test/java/com/flipkart/ranger/core/finder/shardselector/MatchingShardSelectorTest.java b/ranger-core/src/test/java/com/flipkart/ranger/core/finder/shardselector/MatchingShardSelectorTest.java new file mode 100644 index 00000000..ce53b3f9 --- /dev/null +++ b/ranger-core/src/test/java/com/flipkart/ranger/core/finder/shardselector/MatchingShardSelectorTest.java @@ -0,0 +1,46 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.shardselector; + +import com.flipkart.ranger.core.finder.serviceregistry.MapBasedServiceRegistry; +import com.flipkart.ranger.core.model.Criteria; +import com.flipkart.ranger.core.model.ServiceNode; +import com.flipkart.ranger.core.units.TestNodeData; +import com.flipkart.ranger.core.utils.RegistryTestUtils; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class MatchingShardSelectorTest { + + private static final class TestCriteria implements Criteria{ + @Override + public boolean apply(TestNodeData nodeData) { + return nodeData.getNodeId() == 1; + } + } + + @Test + public void testMatchingShardSelector(){ + final MapBasedServiceRegistry serviceRegistry = RegistryTestUtils.getServiceRegistry(); + final MatchingShardSelector> shardSelector = new MatchingShardSelector<>(); + final List> nodes = shardSelector.nodes( + new TestCriteria(), serviceRegistry); + Assert.assertFalse(nodes.isEmpty()); + Assert.assertEquals("localhost-1", nodes.get(0).getHost()); + } +} diff --git a/ranger-core/src/test/java/com/flipkart/ranger/core/serviceprovider/ServiceProviderTest.java b/ranger-core/src/test/java/com/flipkart/ranger/core/serviceprovider/ServiceProviderTest.java new file mode 100644 index 00000000..50e470a7 --- /dev/null +++ b/ranger-core/src/test/java/com/flipkart/ranger/core/serviceprovider/ServiceProviderTest.java @@ -0,0 +1,136 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.serviceprovider; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.flipkart.ranger.core.healthcheck.Healthchecks; +import com.flipkart.ranger.core.model.NodeDataSink; +import com.flipkart.ranger.core.model.Serializer; +import com.flipkart.ranger.core.model.Service; +import com.flipkart.ranger.core.model.ServiceNode; +import com.flipkart.ranger.core.units.TestNodeData; +import org.junit.Assert; +import org.junit.Test; + +public class ServiceProviderTest { + + static TestNodeData testNodeData = null; + + interface TestSerializer extends Serializer { + byte[] serialize(final ServiceNode node); + } + + static class TestSerializerImpl implements TestSerializer{ + private ObjectMapper objectMapper; + + public TestSerializerImpl(){ + objectMapper = new ObjectMapper(); + } + + @Override + public byte[] serialize(ServiceNode node) { + try{ + return objectMapper.writeValueAsBytes(node); + }catch (JsonProcessingException jpe){ + return null; + } + } + } + + static class TestNodeDataSink> implements NodeDataSink{ + + public TestNodeDataSink(){ + } + + @Override + public void updateState(S serializer, ServiceNode serviceNode) { + testNodeData = (TestNodeData) serviceNode.getNodeData(); + } + + @Override + public void start() { + + } + + @Override + public void ensureConnected() { + + } + + @Override + public void stop() { + + } + + @Override + public boolean isActive() { + return true; + } + } + + public class TestServiceProviderBuilder extends BaseServiceProviderBuilder, TestSerializer> { + + @Override + public ServiceProvider> build() { + return super.buildProvider(); + } + + @Override + protected NodeDataSink> dataSink(Service service) { + return new TestNodeDataSink<>(); + } + } + + @Test(expected = NullPointerException.class) + public void testInvalidServiceProvider(){ + new TestServiceProviderBuilder() + .withServiceName("test-service") + .withNamespace("test") + .withHostname("localhost-1") + .withPort(9000) + .build(); + } + + @Test(expected = IllegalArgumentException.class) + public void testInvalidServiceProviderNoHealthCheck(){ + new TestServiceProviderBuilder() + .withServiceName("test-service") + .withNamespace("test") + .withHostname("localhost-1") + .withPort(9000) + .withSerializer(new TestSerializerImpl()) + .build(); + } + + @Test + public void testBuildServiceProvider(){ + ServiceProvider> testProvider = new TestServiceProviderBuilder() + .withServiceName("test-service") + .withNamespace("test") + .withHostname("localhost-1") + .withPort(9000) + .withSerializer(new TestSerializerImpl()) + .withNodeData(TestNodeData.builder().nodeId(1).build()) + .withHealthcheck(Healthchecks.defaultHealthyCheck()) + .withHealthUpdateIntervalMs(1000) + .build(); + testProvider.start(); + Assert.assertNotNull(testNodeData); + Assert.assertEquals(1, testNodeData.getNodeId()); + } + +} diff --git a/ranger-core/src/test/java/com/flipkart/ranger/core/units/TestNodeData.java b/ranger-core/src/test/java/com/flipkart/ranger/core/units/TestNodeData.java new file mode 100644 index 00000000..c38eaa19 --- /dev/null +++ b/ranger-core/src/test/java/com/flipkart/ranger/core/units/TestNodeData.java @@ -0,0 +1,47 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.units; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@AllArgsConstructor +@NoArgsConstructor +@Builder +public class TestNodeData { + @JsonProperty + private int nodeId; + + @Override + public boolean equals(Object obj){ + if(this == obj) + return true; + if(obj == null || obj.getClass()!= this.getClass()) + return false; + TestNodeData that = (TestNodeData) obj; + return (that.nodeId == this.nodeId); + } + + @Override + public int hashCode(){ + return this.nodeId; + } + +} diff --git a/ranger-core/src/test/java/com/flipkart/ranger/core/utils/CriteriaUtils.java b/ranger-core/src/test/java/com/flipkart/ranger/core/utils/CriteriaUtils.java new file mode 100644 index 00000000..6231dce6 --- /dev/null +++ b/ranger-core/src/test/java/com/flipkart/ranger/core/utils/CriteriaUtils.java @@ -0,0 +1,26 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.utils; + +import com.flipkart.ranger.core.model.Criteria; +import com.flipkart.ranger.core.units.TestNodeData; + +public class CriteriaUtils { + + public static Criteria getCriteria(int shardId){ + return nodeData -> nodeData.getNodeId() == shardId; + } +} diff --git a/ranger-core/src/test/java/com/flipkart/ranger/core/utils/RegistryTestUtils.java b/ranger-core/src/test/java/com/flipkart/ranger/core/utils/RegistryTestUtils.java new file mode 100644 index 00000000..5ea19537 --- /dev/null +++ b/ranger-core/src/test/java/com/flipkart/ranger/core/utils/RegistryTestUtils.java @@ -0,0 +1,50 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.utils; + +import com.flipkart.ranger.core.finder.serviceregistry.ListBasedServiceRegistry; +import com.flipkart.ranger.core.finder.serviceregistry.MapBasedServiceRegistry; +import com.flipkart.ranger.core.model.Service; +import com.flipkart.ranger.core.model.ServiceNode; +import com.flipkart.ranger.core.units.TestNodeData; +import com.google.common.collect.Lists; + +import java.util.List; + +public class RegistryTestUtils { + + public static MapBasedServiceRegistry getServiceRegistry(){ + final Service service = new Service("test", "test-service"); + final MapBasedServiceRegistry serviceRegistry = new MapBasedServiceRegistry<>(service); + List> serviceNodes = Lists.newArrayList(); + serviceNodes.add(new ServiceNode<>("localhost-1", 9000, TestNodeData.builder().nodeId(1).build())); + serviceNodes.add(new ServiceNode<>("localhost-2", 9001, TestNodeData.builder().nodeId(2).build())); + serviceNodes.add(new ServiceNode<>("localhost-3", 9002, TestNodeData.builder().nodeId(3).build())); + serviceRegistry.updateNodes(serviceNodes); + return serviceRegistry; + } + + public static ListBasedServiceRegistry getUnshardedRegistry(){ + final Service service = new Service("test", "test-service"); + final ListBasedServiceRegistry serviceRegistry = new ListBasedServiceRegistry<>(service); + List> serviceNodes = Lists.newArrayList(); + serviceNodes.add(new ServiceNode<>("localhost-1", 9000, TestNodeData.builder().nodeId(1).build())); + serviceNodes.add(new ServiceNode<>("localhost-2", 9001, TestNodeData.builder().nodeId(2).build())); + serviceNodes.add(new ServiceNode<>("localhost-3", 9002, TestNodeData.builder().nodeId(3).build())); + serviceRegistry.updateNodes(serviceNodes); + return serviceRegistry; + } +} diff --git a/ranger-core/src/test/java/com/flipkart/ranger/core/utils/TestUtils.java b/ranger-core/src/test/java/com/flipkart/ranger/core/utils/TestUtils.java deleted file mode 100644 index cb98cc62..00000000 --- a/ranger-core/src/test/java/com/flipkart/ranger/core/utils/TestUtils.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.flipkart.ranger.core.utils; - -import java.util.concurrent.TimeUnit; - -import static org.awaitility.Awaitility.await; - -/** - * - */ -public class TestUtils { - public static void sleepForSeconds(int numSeconds) { - await().pollDelay(numSeconds, TimeUnit.SECONDS).until(() -> true); - } -} diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/HttpServiceFinderBuilders.java b/ranger-http/src/main/java/com/flipkart/ranger/http/HttpServiceFinderBuilders.java new file mode 100644 index 00000000..f578937d --- /dev/null +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/HttpServiceFinderBuilders.java @@ -0,0 +1,35 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.http; + +import com.flipkart.ranger.core.model.Criteria; +import com.flipkart.ranger.http.servicefinder.HttpShardedServiceFinderBuilder; +import com.flipkart.ranger.http.servicefinder.HttpUnshardedServiceFinderBuilider; + +public class HttpServiceFinderBuilders { + + private void HttpServiceProviderBuilders(){ + throw new InstantiationError("Must not instantiate this class"); + } + + public static > HttpShardedServiceFinderBuilder httpShardedServiceFinderBuilder(){ + return new HttpShardedServiceFinderBuilder<>(); + } + + public static > HttpUnshardedServiceFinderBuilider httpUnshardedServiceFinderBuilider(){ + return new HttpUnshardedServiceFinderBuilider<>(); + } +} diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/HttpServiceProviderBuilders.java b/ranger-http/src/main/java/com/flipkart/ranger/http/HttpServiceProviderBuilders.java new file mode 100644 index 00000000..605b3b36 --- /dev/null +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/HttpServiceProviderBuilders.java @@ -0,0 +1,29 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.http; + +import com.flipkart.ranger.http.serviceprovider.HttpShardedServiceProviderBuilder; + +public class HttpServiceProviderBuilders { + + private HttpServiceProviderBuilders() { + throw new InstantiationError("Must not instantiate this class"); + } + + public static HttpShardedServiceProviderBuilder httpServiceProviderBuilder() { + return new HttpShardedServiceProviderBuilder<>(); + } +} diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/HttpShardedServiceFinderBuilder.java b/ranger-http/src/main/java/com/flipkart/ranger/http/HttpShardedServiceFinderBuilder.java deleted file mode 100644 index 042f56e2..00000000 --- a/ranger-http/src/main/java/com/flipkart/ranger/http/HttpShardedServiceFinderBuilder.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.flipkart.ranger.http; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.flipkart.ranger.core.finder.sharded.SimpleShardedServiceFinder; -import com.flipkart.ranger.core.finder.sharded.SimpleShardedServiceFinderBuilder; -import com.flipkart.ranger.core.model.NodeDataSource; -import com.flipkart.ranger.core.model.Service; -import com.flipkart.ranger.http.config.HttpClientConfig; -import com.flipkart.ranger.http.serde.HTTPResponseDataDeserializer; - -/** - * - */ -public class HttpShardedServiceFinderBuilder> extends SimpleShardedServiceFinderBuilder, D> { - - private HttpClientConfig clientConfig; - - public HttpShardedServiceFinderBuilder withClientConfig(final HttpClientConfig clientConfig) { - this.clientConfig = clientConfig; - return this; - } - - @Override - public SimpleShardedServiceFinder build() { - return buildFinder(); - } - - @Override - protected NodeDataSource dataSource(Service service) { - return new HttpNodeDataSource<>(service, clientConfig, new ObjectMapper()); - } -} diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/HttpNodeDataStoreConnector.java b/ranger-http/src/main/java/com/flipkart/ranger/http/common/HttpNodeDataStoreConnector.java similarity index 68% rename from ranger-http/src/main/java/com/flipkart/ranger/http/HttpNodeDataStoreConnector.java rename to ranger-http/src/main/java/com/flipkart/ranger/http/common/HttpNodeDataStoreConnector.java index ee0e61be..4cb3bd7e 100644 --- a/ranger-http/src/main/java/com/flipkart/ranger/http/HttpNodeDataStoreConnector.java +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/common/HttpNodeDataStoreConnector.java @@ -1,15 +1,28 @@ -package com.flipkart.ranger.http; +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.http.common; import com.fasterxml.jackson.databind.ObjectMapper; import com.flipkart.ranger.core.model.NodeDataStoreConnector; -import com.flipkart.ranger.core.model.Service; import com.flipkart.ranger.http.config.HttpClientConfig; import lombok.extern.slf4j.Slf4j; import okhttp3.ConnectionPool; import okhttp3.OkHttpClient; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicBoolean; /** * @@ -17,17 +30,13 @@ @Slf4j public class HttpNodeDataStoreConnector implements NodeDataStoreConnector { - protected final Service service; protected final HttpClientConfig config; - protected final OkHttpClient httpClient; protected final ObjectMapper mapper; - protected final AtomicBoolean firstCall = new AtomicBoolean(false); + protected final OkHttpClient httpClient; public HttpNodeDataStoreConnector( - Service service, final HttpClientConfig config, ObjectMapper mapper) { - this.service = service; this.httpClient = new OkHttpClient.Builder() .callTimeout(config.getOperationTimeoutMs() == 0 ? 3000 @@ -39,7 +48,7 @@ public HttpNodeDataStoreConnector( .connectionPool(new ConnectionPool(1, 30, TimeUnit.SECONDS)) .build(); this.config = config; - this.mapper = mapper; + this.mapper = null != mapper ? mapper : new ObjectMapper(); } diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/config/HttpClientConfig.java b/ranger-http/src/main/java/com/flipkart/ranger/http/config/HttpClientConfig.java index ebac969d..bf1f2f4d 100644 --- a/ranger-http/src/main/java/com/flipkart/ranger/http/config/HttpClientConfig.java +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/config/HttpClientConfig.java @@ -1,19 +1,35 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.http.config; -import lombok.Builder; -import lombok.Data; +import lombok.*; /** * */ @Data +@AllArgsConstructor @Builder +@ToString +@NoArgsConstructor public class HttpClientConfig { private String host; private int port; private boolean secure; private long connectionTimeoutMs; private long operationTimeoutMs; - private String nodesRequestPath; - + private long refreshIntervalMillis; } diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/model/ServiceDataSourceResponse.java b/ranger-http/src/main/java/com/flipkart/ranger/http/model/ServiceDataSourceResponse.java new file mode 100644 index 00000000..59964417 --- /dev/null +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/model/ServiceDataSourceResponse.java @@ -0,0 +1,31 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.http.model; + +import com.flipkart.ranger.core.model.Service; +import lombok.*; + +import java.util.List; + +@AllArgsConstructor +@NoArgsConstructor +@Getter +@Setter +@Builder +public class ServiceDataSourceResponse { + private boolean success; + private List data; +} diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/model/ServiceNodesRequest.java b/ranger-http/src/main/java/com/flipkart/ranger/http/model/ServiceNodesRequest.java deleted file mode 100644 index 548560a9..00000000 --- a/ranger-http/src/main/java/com/flipkart/ranger/http/model/ServiceNodesRequest.java +++ /dev/null @@ -1,18 +0,0 @@ -package com.flipkart.ranger.http.model; - -import com.fasterxml.jackson.annotation.JsonProperty; -import lombok.Builder; -import lombok.Data; - -/** - * - */ -@Data -public class ServiceNodesRequest { - private final String serviceName; - - @Builder - public ServiceNodesRequest(@JsonProperty("serviceName") String serviceName) { - this.serviceName = serviceName; - } -} diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/model/ServiceNodesResponse.java b/ranger-http/src/main/java/com/flipkart/ranger/http/model/ServiceNodesResponse.java index 2de6cc0d..0d713cff 100644 --- a/ranger-http/src/main/java/com/flipkart/ranger/http/model/ServiceNodesResponse.java +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/model/ServiceNodesResponse.java @@ -1,10 +1,24 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.http.model; import com.fasterxml.jackson.annotation.JsonProperty; import com.flipkart.ranger.core.model.ServiceNode; import lombok.Builder; import lombok.Data; -import lombok.Singular; import java.util.List; @@ -12,15 +26,15 @@ * */ @Data -public class ServiceNodesResponse { +public class ServiceNodesResponse { private final boolean success; - final List> nodes; + final List> data; @Builder public ServiceNodesResponse( @JsonProperty("success") boolean success, - @JsonProperty("nodes") @Singular List> nodes) { + @JsonProperty("data") List> data) { this.success = success; - this.nodes = nodes; + this.data = data; } } diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/model/ServiceRegistrationResponse.java b/ranger-http/src/main/java/com/flipkart/ranger/http/model/ServiceRegistrationResponse.java new file mode 100644 index 00000000..897d760e --- /dev/null +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/model/ServiceRegistrationResponse.java @@ -0,0 +1,32 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.http.model; + +import com.fasterxml.jackson.annotation.JsonProperty; +import lombok.Builder; +import lombok.Data; + +@Data +public class ServiceRegistrationResponse { + private final boolean success; + + @Builder + public ServiceRegistrationResponse( + @JsonProperty("success") boolean success + ) { + this.success = success; + } +} diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/serde/HTTPResponseDataDeserializer.java b/ranger-http/src/main/java/com/flipkart/ranger/http/serde/HTTPResponseDataDeserializer.java index 3ece5323..df018a0c 100644 --- a/ranger-http/src/main/java/com/flipkart/ranger/http/serde/HTTPResponseDataDeserializer.java +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/serde/HTTPResponseDataDeserializer.java @@ -1,14 +1,27 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.http.serde; import com.flipkart.ranger.core.model.Deserializer; -import com.flipkart.ranger.core.model.ServiceNode; - -import java.util.Collection; +import com.flipkart.ranger.http.model.ServiceNodesResponse; /** * */ @FunctionalInterface public interface HTTPResponseDataDeserializer extends Deserializer { - Collection> deserialize(byte []data); + ServiceNodesResponse deserialize(byte []data); } diff --git a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/unsharded/NoOpShardSelector.java b/ranger-http/src/main/java/com/flipkart/ranger/http/serde/HttpRequestDataSerializer.java similarity index 56% rename from ranger-core/src/main/java/com/flipkart/ranger/core/finder/unsharded/NoOpShardSelector.java rename to ranger-http/src/main/java/com/flipkart/ranger/http/serde/HttpRequestDataSerializer.java index 3718eb50..9a1d5184 100644 --- a/ranger-core/src/main/java/com/flipkart/ranger/core/finder/unsharded/NoOpShardSelector.java +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/serde/HttpRequestDataSerializer.java @@ -1,30 +1,26 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.http.serde; -package com.flipkart.ranger.core.finder.unsharded; - -import com.flipkart.ranger.core.model.ShardSelector; +import com.flipkart.ranger.core.model.Serializer; import com.flipkart.ranger.core.model.ServiceNode; -import java.util.List; +@FunctionalInterface +public interface HttpRequestDataSerializer extends Serializer { + + byte[] serialize(final ServiceNode node); -public class NoOpShardSelector implements ShardSelector { - @Override - public List> nodes(UnshardedClusterInfo criteria, - UnshardedClusterServiceRegistry serviceRegistry) { - return serviceRegistry.nodes(); - } } diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/serde/JacksonHTTPResponseDataDeserializer.java b/ranger-http/src/main/java/com/flipkart/ranger/http/serde/JacksonHTTPResponseDataDeserializer.java deleted file mode 100644 index 64ff3600..00000000 --- a/ranger-http/src/main/java/com/flipkart/ranger/http/serde/JacksonHTTPResponseDataDeserializer.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.flipkart.ranger.http.serde; - -import com.fasterxml.jackson.core.type.TypeReference; -import com.fasterxml.jackson.databind.ObjectMapper; -import com.flipkart.ranger.core.model.ServiceNode; -import com.flipkart.ranger.http.model.ServiceNodesResponse; - -import java.io.IOException; -import java.util.Collection; -import java.util.Collections; - -/** - * - */ -public class JacksonHTTPResponseDataDeserializer implements HTTPResponseDataDeserializer { - private final ObjectMapper mapper; - - public JacksonHTTPResponseDataDeserializer(ObjectMapper mapper) { - this.mapper = mapper; - } - - @Override - public Collection> deserialize(byte[] data) { - try { - final ServiceNodesResponse response - = mapper.readValue(data, new TypeReference>() {}); - return response.isSuccess() ? response.getNodes() : Collections.emptyList(); - } - catch (IOException e) { - throw new IllegalArgumentException(e); - } - } -} diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/HttpNodeDataSource.java b/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinder/HttpNodeDataSource.java similarity index 54% rename from ranger-http/src/main/java/com/flipkart/ranger/http/HttpNodeDataSource.java rename to ranger-http/src/main/java/com/flipkart/ranger/http/servicefinder/HttpNodeDataSource.java index 9ec4d2d7..7ca0bfc0 100644 --- a/ranger-http/src/main/java/com/flipkart/ranger/http/HttpNodeDataSource.java +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinder/HttpNodeDataSource.java @@ -1,4 +1,19 @@ -package com.flipkart.ranger.http; +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.http.servicefinder; import com.fasterxml.jackson.databind.ObjectMapper; import com.flipkart.ranger.core.model.NodeDataSource; @@ -6,8 +21,11 @@ import com.flipkart.ranger.core.model.ServiceNode; import com.flipkart.ranger.core.util.Exceptions; import com.flipkart.ranger.core.util.FinderUtils; +import com.flipkart.ranger.http.common.HttpNodeDataStoreConnector; import com.flipkart.ranger.http.config.HttpClientConfig; +import com.flipkart.ranger.http.model.ServiceNodesResponse; import com.flipkart.ranger.http.serde.HTTPResponseDataDeserializer; +import com.google.common.base.Preconditions; import lombok.extern.slf4j.Slf4j; import lombok.val; import okhttp3.HttpUrl; @@ -23,53 +41,60 @@ */ @Slf4j public class HttpNodeDataSource> extends HttpNodeDataStoreConnector implements NodeDataSource { + + private final Service service; + public HttpNodeDataSource( Service service, final HttpClientConfig config, ObjectMapper mapper) { - super(service, config, mapper); + super(config, mapper); + this.service = service; } - @Override public Optional>> refresh(D deserializer) { + Preconditions.checkNotNull(config, "client config has not been set for node data"); + Preconditions.checkNotNull(mapper, "mapper has not been set for node data"); val httpUrl = new HttpUrl.Builder() .scheme(config.isSecure() ? "https" : "http") .host(config.getHost()) .port(config.getPort() == 0 - ? defaultPort() - : config.getPort()) - .encodedPath(String.format("/ranger/nodes/v1/%s/%s", service.getNamespace(), service.getServiceName())) + ? defaultPort() + : config.getPort()) + .encodedPath(String.format("/v1/ranger/nodes/%s/%s", service.getNamespace(), service.getServiceName())) .build(); val request = new Request.Builder() .url(httpUrl) .get() .build(); + ServiceNodesResponse serviceNodesResponse = null; try (val response = httpClient.newCall(request).execute()) { if (response.isSuccessful()) { try (final ResponseBody body = response.body()) { if (null == body) { log.warn("HTTP call to {} returned empty body", httpUrl); - } - else { + } else { final byte[] bytes = body.bytes(); - return Optional.of(FinderUtils.filterValidNodes( - service, - deserializer.deserialize(bytes), - healthcheckZombieCheckThresholdTime(service))); + serviceNodesResponse = deserializer.deserialize(bytes); } } + } else { + log.warn("HTTP call to {} returned: {}", httpUrl, response.code()); } - else { - log.warn("HTTP call to {} returned: {}", httpUrl.toString(), response.code()); - } - } - catch (IOException e) { + } catch (IOException e) { Exceptions.illegalState("Error fetching data from server: " + httpUrl, e); } - log.error("No data returned from server: " + httpUrl); + + if (null != serviceNodesResponse && null != serviceNodesResponse.getData() && + !serviceNodesResponse.getData().isEmpty()) { + return Optional.of(FinderUtils.filterValidNodes( + service, + serviceNodesResponse.getData(), + healthcheckZombieCheckThresholdTime(service))); + } return Optional.empty(); } diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinder/HttpShardedServiceFinderBuilder.java b/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinder/HttpShardedServiceFinderBuilder.java new file mode 100644 index 00000000..bdb3b487 --- /dev/null +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinder/HttpShardedServiceFinderBuilder.java @@ -0,0 +1,55 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.http.servicefinder; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.flipkart.ranger.core.finder.SimpleShardedServiceFinder; +import com.flipkart.ranger.core.finder.SimpleShardedServiceFinderBuilder; +import com.flipkart.ranger.core.model.Criteria; +import com.flipkart.ranger.core.model.NodeDataSource; +import com.flipkart.ranger.core.model.Service; +import com.flipkart.ranger.http.config.HttpClientConfig; +import com.flipkart.ranger.http.serde.HTTPResponseDataDeserializer; + +/** + * + */ +public class HttpShardedServiceFinderBuilder> extends SimpleShardedServiceFinderBuilder, HTTPResponseDataDeserializer, C> { + + private HttpClientConfig clientConfig; + private ObjectMapper mapper; + + public HttpShardedServiceFinderBuilder withClientConfig(final HttpClientConfig clientConfig) { + this.clientConfig = clientConfig; + return this; + } + + public HttpShardedServiceFinderBuilder withObjectMapper(final ObjectMapper mapper){ + this.mapper = mapper; + return this; + } + + @Override + public SimpleShardedServiceFinder build() { + return buildFinder(); + } + + @Override + protected NodeDataSource> dataSource(Service service) { + return new HttpNodeDataSource<>(service, clientConfig, mapper); + } + +} diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinder/HttpUnshardedServiceFinderBuilider.java b/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinder/HttpUnshardedServiceFinderBuilider.java new file mode 100644 index 00000000..fbc1ece8 --- /dev/null +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinder/HttpUnshardedServiceFinderBuilider.java @@ -0,0 +1,54 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.http.servicefinder; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.flipkart.ranger.core.finder.SimpleUnshardedServiceFinder; +import com.flipkart.ranger.core.finder.SimpleUnshardedServiceFinderBuilder; +import com.flipkart.ranger.core.model.Criteria; +import com.flipkart.ranger.core.model.NodeDataSource; +import com.flipkart.ranger.core.model.Service; +import com.flipkart.ranger.http.config.HttpClientConfig; +import com.flipkart.ranger.http.serde.HTTPResponseDataDeserializer; + +public class HttpUnshardedServiceFinderBuilider> + extends SimpleUnshardedServiceFinderBuilder, HTTPResponseDataDeserializer, C> { + + private HttpClientConfig clientConfig; + private ObjectMapper mapper; + + public HttpUnshardedServiceFinderBuilider withClientConfig(final HttpClientConfig clientConfig) { + this.clientConfig = clientConfig; + return this; + } + + public HttpUnshardedServiceFinderBuilider withObjectMapper(final ObjectMapper mapper) { + this.mapper = mapper; + return this; + } + + @Override + public SimpleUnshardedServiceFinder build() { + return buildFinder(); + } + + @Override + protected NodeDataSource> dataSource(Service service) { + return new HttpNodeDataSource<>(service, clientConfig, mapper); + } + +} + diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinderhub/HttpServiceDataSource.java b/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinderhub/HttpServiceDataSource.java new file mode 100644 index 00000000..cb0af3f7 --- /dev/null +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinderhub/HttpServiceDataSource.java @@ -0,0 +1,89 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.http.servicefinderhub; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.flipkart.ranger.core.finderhub.ServiceDataSource; +import com.flipkart.ranger.core.model.Service; +import com.flipkart.ranger.core.util.Exceptions; +import com.flipkart.ranger.http.common.HttpNodeDataStoreConnector; +import com.flipkart.ranger.http.config.HttpClientConfig; +import com.flipkart.ranger.http.model.ServiceDataSourceResponse; +import com.google.common.base.Preconditions; +import lombok.extern.slf4j.Slf4j; +import lombok.val; +import okhttp3.HttpUrl; +import okhttp3.Request; +import okhttp3.ResponseBody; + +import java.io.IOException; +import java.util.Collection; +import java.util.Collections; + +@Slf4j +public class HttpServiceDataSource extends HttpNodeDataStoreConnector implements ServiceDataSource { + + public HttpServiceDataSource(HttpClientConfig config, ObjectMapper mapper) { + super(config, mapper); + } + + @Override + public Collection services() { + Preconditions.checkNotNull(config, "client config has not been set for node data"); + Preconditions.checkNotNull(mapper, "mapper has not been set for node data"); + + val httpUrl = new HttpUrl.Builder() + .scheme(config.isSecure() + ? "https" + : "http") + .host(config.getHost()) + .port(config.getPort() == 0 + ? defaultPort() + : config.getPort()) + .encodedPath("/v1/ranger/services") + .build(); + val request = new Request.Builder() + .url(httpUrl) + .get() + .build(); + + try (val response = httpClient.newCall(request).execute()) { + if (response.isSuccessful()) { + try (final ResponseBody body = response.body()) { + if (null == body) { + log.warn("HTTP call to {} returned empty body", httpUrl); + } + else { + final byte[] bytes = body.bytes(); + val serviceDataSourceResponse = mapper.readValue(bytes, ServiceDataSourceResponse.class); + if(serviceDataSourceResponse.isSuccess()){ + return serviceDataSourceResponse.getData(); + } + } + } + } + else { + log.warn("HTTP call to {} returned: {}", httpUrl, response.code()); + } + } + catch (IOException e) { + Exceptions.illegalState("Error fetching data from server: " + httpUrl, e); + } + + log.error("No data returned from server: " + httpUrl); + return Collections.emptyList(); + } +} diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinderhub/HttpServiceFinderHubBuilder.java b/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinderhub/HttpServiceFinderHubBuilder.java new file mode 100644 index 00000000..2c100288 --- /dev/null +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinderhub/HttpServiceFinderHubBuilder.java @@ -0,0 +1,36 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.http.servicefinderhub; + +import com.flipkart.ranger.core.finderhub.ServiceFinderHub; +import com.flipkart.ranger.core.finderhub.ServiceFinderHubBuilder; +import com.flipkart.ranger.core.model.Criteria; +import com.flipkart.ranger.core.model.ServiceRegistry; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class HttpServiceFinderHubBuilder, R extends ServiceRegistry> extends ServiceFinderHubBuilder { + + @Override + protected void preBuild() { + log.info("No pre-build actions necessary"); + } + + @Override + protected void postBuild(ServiceFinderHub serviceFinderHub) { + log.info("No post build actions necessary"); + } +} diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinderhub/HttpShardedServiceFinderFactory.java b/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinderhub/HttpShardedServiceFinderFactory.java new file mode 100644 index 00000000..c5f7c340 --- /dev/null +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinderhub/HttpShardedServiceFinderFactory.java @@ -0,0 +1,75 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.http.servicefinderhub; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.flipkart.ranger.core.finder.ServiceFinder; +import com.flipkart.ranger.core.finder.serviceregistry.MapBasedServiceRegistry; +import com.flipkart.ranger.core.finderhub.ServiceFinderFactory; +import com.flipkart.ranger.core.model.Criteria; +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.http.config.HttpClientConfig; +import com.flipkart.ranger.http.serde.HTTPResponseDataDeserializer; +import com.flipkart.ranger.http.servicefinder.HttpShardedServiceFinderBuilder; +import lombok.Builder; +import lombok.Getter; +import lombok.val; + +@Getter +public class HttpShardedServiceFinderFactory > implements ServiceFinderFactory> { + + private final HttpClientConfig clientConfig; + private final ObjectMapper mapper; + private final HTTPResponseDataDeserializer deserializer; + private final ShardSelector> shardSelector; + private final ServiceNodeSelector nodeSelector; + private final int nodeRefreshIntervalMs; + + @Builder + public HttpShardedServiceFinderFactory( + HttpClientConfig httpClientConfig, + ObjectMapper mapper, + HTTPResponseDataDeserializer deserializer, + ShardSelector> shardSelector, + ServiceNodeSelector nodeSelector, + int nodeRefreshIntervalMs) + { + this.clientConfig = httpClientConfig; + this.mapper = mapper; + this.deserializer = deserializer; + this.shardSelector = shardSelector; + this.nodeSelector = nodeSelector; + this.nodeRefreshIntervalMs = nodeRefreshIntervalMs; + } + + @Override + public ServiceFinder> buildFinder(Service service) { + val serviceFinder = new HttpShardedServiceFinderBuilder() + .withClientConfig(clientConfig) + .withObjectMapper(mapper) + .withDeserializer(deserializer) + .withNamespace(service.getNamespace()) + .withServiceName(service.getServiceName()) + .withNodeRefreshIntervalMs(nodeRefreshIntervalMs) + .withShardSelector(shardSelector) + .withNodeSelector(nodeSelector) + .build(); + serviceFinder.start(); + return serviceFinder; + } +} diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinderhub/HttpUnshardedServiceFinderFactory.java b/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinderhub/HttpUnshardedServiceFinderFactory.java new file mode 100644 index 00000000..eeb63c76 --- /dev/null +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/servicefinderhub/HttpUnshardedServiceFinderFactory.java @@ -0,0 +1,75 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.http.servicefinderhub; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.flipkart.ranger.core.finder.ServiceFinder; +import com.flipkart.ranger.core.finder.serviceregistry.ListBasedServiceRegistry; +import com.flipkart.ranger.core.finderhub.ServiceFinderFactory; +import com.flipkart.ranger.core.model.Criteria; +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.http.config.HttpClientConfig; +import com.flipkart.ranger.http.serde.HTTPResponseDataDeserializer; +import com.flipkart.ranger.http.servicefinder.HttpUnshardedServiceFinderBuilider; +import lombok.Builder; +import lombok.Getter; +import lombok.val; + +@Getter +public class HttpUnshardedServiceFinderFactory> implements ServiceFinderFactory> { + + private final HttpClientConfig clientConfig; + private final ObjectMapper mapper; + private final HTTPResponseDataDeserializer deserializer; + private final ShardSelector> shardSelector; + private final ServiceNodeSelector nodeSelector; + private final int nodeRefreshIntervalMs; + + @Builder + public HttpUnshardedServiceFinderFactory( + HttpClientConfig httpClientConfig, + ObjectMapper mapper, + HTTPResponseDataDeserializer deserializer, + ShardSelector> shardSelector, + ServiceNodeSelector nodeSelector, + int nodeRefreshIntervalMs) + { + this.clientConfig = httpClientConfig; + this.mapper = mapper; + this.deserializer = deserializer; + this.shardSelector = shardSelector; + this.nodeSelector = nodeSelector; + this.nodeRefreshIntervalMs = nodeRefreshIntervalMs; + } + + @Override + public ServiceFinder> buildFinder(Service service) { + val serviceFinder = new HttpUnshardedServiceFinderBuilider() + .withClientConfig(clientConfig) + .withObjectMapper(mapper) + .withDeserializer(deserializer) + .withNamespace(service.getNamespace()) + .withServiceName(service.getServiceName()) + .withNodeRefreshIntervalMs(nodeRefreshIntervalMs) + .withShardSelector(shardSelector) + .withNodeSelector(nodeSelector) + .build(); + serviceFinder.start(); + return serviceFinder; + } +} diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/serviceprovider/HttpNodeDataSink.java b/ranger-http/src/main/java/com/flipkart/ranger/http/serviceprovider/HttpNodeDataSink.java new file mode 100644 index 00000000..92263bf8 --- /dev/null +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/serviceprovider/HttpNodeDataSink.java @@ -0,0 +1,98 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.http.serviceprovider; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.flipkart.ranger.core.model.NodeDataSink; +import com.flipkart.ranger.core.model.Service; +import com.flipkart.ranger.core.model.ServiceNode; +import com.flipkart.ranger.core.util.Exceptions; +import com.flipkart.ranger.http.common.HttpNodeDataStoreConnector; +import com.flipkart.ranger.http.config.HttpClientConfig; +import com.flipkart.ranger.http.model.ServiceRegistrationResponse; +import com.flipkart.ranger.http.serde.HttpRequestDataSerializer; +import com.google.common.base.Preconditions; +import lombok.extern.slf4j.Slf4j; +import lombok.val; +import okhttp3.HttpUrl; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.ResponseBody; + +import java.io.IOException; +import java.util.Optional; + +@Slf4j +public class HttpNodeDataSink> extends HttpNodeDataStoreConnector implements NodeDataSink { + + private final Service service; + + public HttpNodeDataSink(Service service, HttpClientConfig config, ObjectMapper mapper) { + super(config, mapper); + this.service = service; + } + + @Override + public void updateState(S serializer, ServiceNode serviceNode) { + Preconditions.checkNotNull(config, "client config has not been set for node data"); + Preconditions.checkNotNull(mapper, "mapper has not been set for node data"); + + val httpUrl = new HttpUrl.Builder() + .scheme(config.isSecure() + ? "https" + : "http") + .host(config.getHost()) + .port(config.getPort() == 0 + ? defaultPort() + : config.getPort()) + .encodedPath(String.format("/v1/ranger/nodes/add/%s/%s", service.getNamespace(), service.getServiceName())) + .build(); + val requestBody = RequestBody.create(serializer.serialize(serviceNode)); + val serviceRegistrationResponse = registerService(httpUrl, requestBody); + if(!serviceRegistrationResponse.isPresent() || !serviceRegistrationResponse.get().isSuccess()){ + Exceptions.illegalState("Error updating state on the server for nodedata: " + httpUrl); + } + } + + private Optional registerService(HttpUrl httpUrl, RequestBody requestBody){ + val request = new Request.Builder() + .url(httpUrl) + .post(requestBody) + .build(); + try (val response = httpClient.newCall(request).execute()) { + if (response.isSuccessful()) { + try (final ResponseBody body = response.body()) { + if (null == body) { + log.warn("HTTP call to {} returned empty body", httpUrl); + } + else { + final byte[] bytes = body.bytes(); + return Optional.of( + mapper.readValue(bytes, ServiceRegistrationResponse.class) + ); + } + } + } + else { + log.warn("HTTP call to {} has returned: {}", httpUrl, response.code()); + } + } + catch (IOException e) { + Exceptions.illegalState("Error updating state on the server: " + httpUrl, e); + } + return Optional.empty(); + } +} diff --git a/ranger-http/src/main/java/com/flipkart/ranger/http/serviceprovider/HttpShardedServiceProviderBuilder.java b/ranger-http/src/main/java/com/flipkart/ranger/http/serviceprovider/HttpShardedServiceProviderBuilder.java new file mode 100644 index 00000000..64160015 --- /dev/null +++ b/ranger-http/src/main/java/com/flipkart/ranger/http/serviceprovider/HttpShardedServiceProviderBuilder.java @@ -0,0 +1,52 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.http.serviceprovider; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.flipkart.ranger.core.model.NodeDataSink; +import com.flipkart.ranger.core.model.Service; +import com.flipkart.ranger.core.serviceprovider.BaseServiceProviderBuilder; +import com.flipkart.ranger.core.serviceprovider.ServiceProvider; +import com.flipkart.ranger.http.config.HttpClientConfig; +import com.flipkart.ranger.http.serde.HttpRequestDataSerializer; +import lombok.extern.slf4j.Slf4j; + +@Slf4j +public class HttpShardedServiceProviderBuilder extends BaseServiceProviderBuilder, HttpRequestDataSerializer> { + + private HttpClientConfig clientConfig; + private ObjectMapper mapper; + + public HttpShardedServiceProviderBuilder withClientConfiguration(final HttpClientConfig clientConfig) { + this.clientConfig = clientConfig; + return this; + } + + public HttpShardedServiceProviderBuilder withObjectMapper(final ObjectMapper mapper){ + this.mapper = mapper; + return this; + } + + @Override + public ServiceProvider> build() { + return super.buildProvider(); + } + + @Override + protected NodeDataSink> dataSink(Service service) { + return new HttpNodeDataSink<>(service, clientConfig, mapper); + } +} diff --git a/ranger-http/src/test/java/com/flipkart/ranger/http/ResourceHelper.java b/ranger-http/src/test/java/com/flipkart/ranger/http/ResourceHelper.java new file mode 100644 index 00000000..94dcacf0 --- /dev/null +++ b/ranger-http/src/test/java/com/flipkart/ranger/http/ResourceHelper.java @@ -0,0 +1,44 @@ +/** + * Copyright 2015 Flipkart Internet Pvt. Ltd. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.http; + +import com.fasterxml.jackson.databind.ObjectMapper; +import lombok.SneakyThrows; + +import java.io.BufferedReader; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.stream.Collectors; + +public class ResourceHelper { + + private static final ObjectMapper objectMapper = new ObjectMapper(); + + public static String getResource(String path) { + final InputStream data = ResourceHelper.class.getClassLoader().getResourceAsStream(path); + return new BufferedReader( + new InputStreamReader(data)) + .lines() + .collect(Collectors.joining("\n")); + } + + @SneakyThrows + public static T getResource(String path, Class klass) { + final String data = getResource(path); + return objectMapper.readValue(data, klass); + } + +} diff --git a/ranger-http/src/test/java/com/flipkart/ranger/http/common/HttpNodeDataStoreConnectorTest.java b/ranger-http/src/test/java/com/flipkart/ranger/http/common/HttpNodeDataStoreConnectorTest.java new file mode 100644 index 00000000..caf6d5fc --- /dev/null +++ b/ranger-http/src/test/java/com/flipkart/ranger/http/common/HttpNodeDataStoreConnectorTest.java @@ -0,0 +1,36 @@ +/** + * Copyright 2015 Flipkart Internet Pvt. Ltd. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.http.common; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.flipkart.ranger.http.config.HttpClientConfig; +import org.junit.Assert; +import org.junit.Test; + +public class HttpNodeDataStoreConnectorTest { + + @Test + public void testHttpNodeDataStoreConnector(){ + final ObjectMapper objectMapper = new ObjectMapper(); + final HttpClientConfig httpClientConfig = HttpClientConfig.builder() + .host("localhost-1") + .port(80) + .build(); + HttpNodeDataStoreConnector httpNodeDataStoreConnector = new HttpNodeDataStoreConnector(httpClientConfig, objectMapper); + Assert.assertNotNull(httpNodeDataStoreConnector); + Assert.assertTrue(httpNodeDataStoreConnector.isActive()); + } +} diff --git a/ranger-http/src/test/java/com/flipkart/ranger/http/config/HttpClientConfigTest.java b/ranger-http/src/test/java/com/flipkart/ranger/http/config/HttpClientConfigTest.java new file mode 100644 index 00000000..336fb925 --- /dev/null +++ b/ranger-http/src/test/java/com/flipkart/ranger/http/config/HttpClientConfigTest.java @@ -0,0 +1,32 @@ +/** + * Copyright 2015 Flipkart Internet Pvt. Ltd. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.http.config; + +import com.flipkart.ranger.http.ResourceHelper; +import org.junit.Assert; +import org.junit.Test; + +public class HttpClientConfigTest { + + @Test + public void testHttpClientConfig(){ + HttpClientConfig resource = ResourceHelper.getResource("fixtures/httpClientConfig.json", HttpClientConfig.class); + Assert.assertEquals("localhost-1", resource.getHost()); + Assert.assertEquals(80, resource.getPort()); + Assert.assertEquals(10, resource.getConnectionTimeoutMs()); + Assert.assertEquals(10, resource.getOperationTimeoutMs()); + } +} diff --git a/ranger-http/src/test/java/com/flipkart/ranger/http/model/ServiceNodeResponseTest.java b/ranger-http/src/test/java/com/flipkart/ranger/http/model/ServiceNodeResponseTest.java new file mode 100644 index 00000000..8c1f6f40 --- /dev/null +++ b/ranger-http/src/test/java/com/flipkart/ranger/http/model/ServiceNodeResponseTest.java @@ -0,0 +1,45 @@ +/** + * Copyright 2015 Flipkart Internet Pvt. Ltd. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.http.model; + +import com.flipkart.ranger.core.model.ServiceNode; +import com.flipkart.ranger.http.ResourceHelper; +import lombok.*; +import org.junit.Assert; +import org.junit.Test; + +public class ServiceNodeResponseTest { + + @AllArgsConstructor + @NoArgsConstructor + @Getter + @Setter + static class TestNodeInfo{ + private int shardId; + private String region; + } + + @Test + public void testServiceNodesResponse(){ + val serviceNodesResponse = ResourceHelper.getResource("fixtures/serviceNodesResponse.json", ServiceNodesResponse.class); + Assert.assertNotNull(serviceNodesResponse); + Assert.assertFalse(serviceNodesResponse.getData().isEmpty()); + Assert.assertNotNull(((ServiceNode) serviceNodesResponse.getData().get(0)).getNodeData()); + Assert.assertNotNull(((ServiceNode) serviceNodesResponse.getData().get(1)).getNodeData()); + Assert.assertEquals(((ServiceNode) serviceNodesResponse.getData().get(0)).getHost(), "localhost-1"); + Assert.assertEquals(((ServiceNode) serviceNodesResponse.getData().get(1)).getHost(), "localhost-2"); + } +} diff --git a/ranger-http/src/test/java/com/flipkart/ranger/http/model/ServiceRegistrationResponseTest.java b/ranger-http/src/test/java/com/flipkart/ranger/http/model/ServiceRegistrationResponseTest.java new file mode 100644 index 00000000..42025c66 --- /dev/null +++ b/ranger-http/src/test/java/com/flipkart/ranger/http/model/ServiceRegistrationResponseTest.java @@ -0,0 +1,30 @@ +/** + * Copyright 2015 Flipkart Internet Pvt. Ltd. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.http.model; + +import com.flipkart.ranger.http.ResourceHelper; +import org.junit.Assert; +import org.junit.Test; + +public class ServiceRegistrationResponseTest { + + @Test + public void testServiceRegistrationResponse(){ + ServiceRegistrationResponse resource = ResourceHelper.getResource("fixtures/serviceResponse.json", ServiceRegistrationResponse.class); + Assert.assertNotNull(resource); + Assert.assertTrue(resource.isSuccess()); + } +} diff --git a/ranger-http/src/test/java/com/flipkart/ranger/http/HttpShardedServiceFinderBuilderTest.java b/ranger-http/src/test/java/com/flipkart/ranger/http/servicefinder/HttpShardedServiceFinderBuilderTest.java similarity index 65% rename from ranger-http/src/test/java/com/flipkart/ranger/http/HttpShardedServiceFinderBuilderTest.java rename to ranger-http/src/test/java/com/flipkart/ranger/http/servicefinder/HttpShardedServiceFinderBuilderTest.java index 7415aaa4..e01acac9 100644 --- a/ranger-http/src/test/java/com/flipkart/ranger/http/HttpShardedServiceFinderBuilderTest.java +++ b/ranger-http/src/test/java/com/flipkart/ranger/http/servicefinder/HttpShardedServiceFinderBuilderTest.java @@ -1,16 +1,32 @@ -package com.flipkart.ranger.http; +/** + * Copyright 2015 Flipkart Internet Pvt. Ltd. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.http.servicefinder; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.flipkart.ranger.core.finder.sharded.SimpleShardedServiceFinder; +import com.flipkart.ranger.core.TestUtils; +import com.flipkart.ranger.core.finder.SimpleShardedServiceFinder; import com.flipkart.ranger.core.healthcheck.HealthcheckStatus; +import com.flipkart.ranger.core.model.Criteria; import com.flipkart.ranger.core.model.ServiceNode; -import com.flipkart.ranger.core.utils.TestUtils; import com.flipkart.ranger.http.config.HttpClientConfig; import com.flipkart.ranger.http.model.ServiceNodesResponse; -import com.flipkart.ranger.http.serde.HTTPResponseDataDeserializer; import com.github.tomakehurst.wiremock.junit.WireMockRule; +import com.google.common.collect.Lists; import lombok.Data; import lombok.val; import org.junit.Assert; @@ -18,9 +34,6 @@ import org.junit.Test; import java.io.IOException; -import java.util.Collections; -import java.util.Map; -import java.util.stream.Collectors; import static com.github.tomakehurst.wiremock.client.WireMock.*; @@ -51,10 +64,10 @@ public void testFinder() throws Exception { node.setLastUpdatedTimeStamp(System.currentTimeMillis()); val payload = MAPPER.writeValueAsBytes( ServiceNodesResponse.builder() - .node(node) + .data(Lists.newArrayList(node)) .success(true) .build()); - server.stubFor(get(urlEqualTo("/ranger/nodes/v1/testns/test")) + server.stubFor(get(urlEqualTo("/v1/ranger/nodes/testns/test")) .willReturn(aResponse() .withBody(payload) .withStatus(200))); @@ -64,31 +77,25 @@ public void testFinder() throws Exception { .connectionTimeoutMs(30_000) .operationTimeoutMs(30_000) .build(); - final SimpleShardedServiceFinder finder = new HttpShardedServiceFinderBuilder>() + + final SimpleShardedServiceFinder> finder = new HttpShardedServiceFinderBuilder>() .withClientConfig(clientConfig) .withNamespace("testns") .withServiceName("test") + .withObjectMapper(MAPPER) .withDeserializer(data -> { - final ServiceNodesResponse response; try { - response = MAPPER.readValue(data, new TypeReference>() {}); + return MAPPER.readValue(data, new TypeReference>() {}); } catch (IOException e) { throw new IllegalArgumentException(e); } - return response.isSuccess() ? response.getNodes() : Collections.emptyList(); }) - .withShardSelector((criteria, registry) -> registry - .nodes() - .entries() - .stream() - .filter(e -> e.getKey().getName().equals(criteria.getName())) - .map(Map.Entry::getValue) - .collect(Collectors.toList())) + .withShardSelector((criteria, registry) -> registry.nodeList()) .build(); finder.start(); TestUtils.sleepForSeconds(3); - Assert.assertNotNull(finder.get(testNode)); + Assert.assertNotNull(finder.get(nodeData -> true)); } } \ No newline at end of file diff --git a/ranger-http/src/test/java/com/flipkart/ranger/http/servicefinderhub/HttpServiceDataSourceTest.java b/ranger-http/src/test/java/com/flipkart/ranger/http/servicefinderhub/HttpServiceDataSourceTest.java new file mode 100644 index 00000000..d5420f68 --- /dev/null +++ b/ranger-http/src/test/java/com/flipkart/ranger/http/servicefinderhub/HttpServiceDataSourceTest.java @@ -0,0 +1,85 @@ +/** + * Copyright 2015 Flipkart Internet Pvt. Ltd. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.http.servicefinderhub; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.flipkart.ranger.core.model.Service; +import com.flipkart.ranger.http.config.HttpClientConfig; +import com.flipkart.ranger.http.model.ServiceDataSourceResponse; +import com.github.tomakehurst.wiremock.junit.WireMockRule; +import com.google.common.collect.Lists; +import lombok.Builder; +import lombok.Data; +import lombok.val; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; + +import java.io.IOException; +import java.util.Collection; + +import static com.github.tomakehurst.wiremock.client.WireMock.*; + +public class HttpServiceDataSourceTest { + + @Data + private static final class TestNodeData { + private final String farmId; + + @Builder + public TestNodeData(@JsonProperty("farmId") String farmId) { + this.farmId = farmId; + } + } + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + @Rule + public WireMockRule server = new WireMockRule(8888); + + @Test + public void testServiceDataSource() throws IOException { + val responseObj = ServiceDataSourceResponse.builder() + .success(true) + .data(Lists.newArrayList( + new Service("test-n", "test-s"), + new Service("test-n", "test-s1"), + new Service("test-n", "test-s2") + )) + .build(); + val response = MAPPER.writeValueAsBytes(responseObj); + server.stubFor(get(urlEqualTo("/v1/ranger/services")) + .willReturn(aResponse() + .withBody(response) + .withStatus(200))); + val clientConfig = HttpClientConfig.builder() + .host("127.0.0.1") + .port(server.port()) + .connectionTimeoutMs(30_000) + .operationTimeoutMs(30_000) + .build(); + HttpServiceDataSource httpServiceDataSource = new HttpServiceDataSource<>(clientConfig, MAPPER); + Collection services = httpServiceDataSource.services(); + Assert.assertNotNull(services); + Assert.assertFalse(services.isEmpty()); + Assert.assertEquals(3, services.size()); + Assert.assertFalse(services.stream().noneMatch(each -> each.getServiceName().equalsIgnoreCase("test-s"))); + Assert.assertFalse(services.stream().noneMatch(each -> each.getServiceName().equalsIgnoreCase("test-s1"))); + Assert.assertFalse(services.stream().noneMatch(each -> each.getServiceName().equalsIgnoreCase("test-s2"))); + } + +} diff --git a/ranger-http/src/test/java/com/flipkart/ranger/http/serviceprovider/HttpShardedServiceProviderBuilderTest.java b/ranger-http/src/test/java/com/flipkart/ranger/http/serviceprovider/HttpShardedServiceProviderBuilderTest.java new file mode 100644 index 00000000..3c325024 --- /dev/null +++ b/ranger-http/src/test/java/com/flipkart/ranger/http/serviceprovider/HttpShardedServiceProviderBuilderTest.java @@ -0,0 +1,87 @@ +/** + * Copyright 2015 Flipkart Internet Pvt. Ltd. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.http.serviceprovider; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.flipkart.ranger.core.healthcheck.Healthchecks; +import com.flipkart.ranger.core.model.ServiceNode; +import com.flipkart.ranger.core.serviceprovider.ServiceProvider; +import com.flipkart.ranger.http.config.HttpClientConfig; +import com.flipkart.ranger.http.model.ServiceRegistrationResponse; +import com.flipkart.ranger.http.serde.HttpRequestDataSerializer; +import com.github.tomakehurst.wiremock.junit.WireMockRule; +import lombok.Builder; +import lombok.Data; +import lombok.val; +import org.junit.Rule; +import org.junit.Test; + +import static com.github.tomakehurst.wiremock.client.WireMock.*; + +public class HttpShardedServiceProviderBuilderTest { + + @Data + private static final class TestNodeData { + private final String farmId; + + @Builder + public TestNodeData(@JsonProperty("farmId") String farmId) { + this.farmId = farmId; + } + } + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + @Rule + public WireMockRule server = new WireMockRule(8888); + + @Test + public void testProvider() throws Exception { + TestNodeData nm5NodeData = TestNodeData.builder().farmId("nm5").build(); + ServiceNode testNode = new ServiceNode<>("localhost-1", 80, nm5NodeData); + val response = MAPPER.writeValueAsBytes( + ServiceRegistrationResponse.builder() + .success(true) + .build()); + byte[] requestBytes = MAPPER.writeValueAsBytes(testNode); + server.stubFor(post(urlEqualTo("/v1/ranger/nodes/add/testns/test")) + .withRequestBody(binaryEqualTo(requestBytes)) + .willReturn(aResponse() + .withBody(response) + .withStatus(200))); + val clientConfig = HttpClientConfig.builder() + .host("127.0.0.1") + .port(server.port()) + .connectionTimeoutMs(30_000) + .operationTimeoutMs(30_000) + .build(); + ServiceProvider> serviceProvider = new HttpShardedServiceProviderBuilder() + .withNamespace("testns") + .withServiceName("test") + .withHostname("localhost-1") + .withPort(80) + .withHealthcheck(Healthchecks.defaultHealthyCheck()) + .withHealthUpdateIntervalMs(1000) + .withObjectMapper(MAPPER) + .withClientConfiguration(clientConfig) + .withNodeData(nm5NodeData) + .withSerializer(node -> requestBytes) + .build(); + serviceProvider.start(); + } + +} diff --git a/ranger-http/src/test/resources/fixtures/httpClientConfig.json b/ranger-http/src/test/resources/fixtures/httpClientConfig.json new file mode 100644 index 00000000..3855f309 --- /dev/null +++ b/ranger-http/src/test/resources/fixtures/httpClientConfig.json @@ -0,0 +1,8 @@ +{ + "host" : "localhost-1", + "port" : 80, + "secure" : false, + "connectionTimeoutMs" : 10, + "operationTimeoutMs" : 10, + "refreshIntervalMillis" : 1000 +} \ No newline at end of file diff --git a/ranger-http/src/test/resources/fixtures/serviceNodesResponse.json b/ranger-http/src/test/resources/fixtures/serviceNodesResponse.json new file mode 100644 index 00000000..7ac55a8e --- /dev/null +++ b/ranger-http/src/test/resources/fixtures/serviceNodesResponse.json @@ -0,0 +1,23 @@ +{ + "success" : true, + "data" : [ + { + "host" : "localhost-1", + "port" : 80, + "nodeData" : { + "shardId" : 1, + "region" : "r1" + }, + "healthcheckStatus" : "healthy" + }, + { + "host" : "localhost-2", + "port" : 90, + "nodeData" : { + "shardId" : 2, + "region" : "r2" + }, + "healthcheckStatus" : "healthy" + } + ] +} \ No newline at end of file diff --git a/ranger-http/src/test/resources/fixtures/serviceResponse.json b/ranger-http/src/test/resources/fixtures/serviceResponse.json new file mode 100644 index 00000000..753c708a --- /dev/null +++ b/ranger-http/src/test/resources/fixtures/serviceResponse.json @@ -0,0 +1,3 @@ +{ + "success" : true +} \ No newline at end of file diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/ServiceFinderBuilders.java b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/ServiceFinderBuilders.java index 33c2dbc3..179068c5 100644 --- a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/ServiceFinderBuilders.java +++ b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/ServiceFinderBuilders.java @@ -1,23 +1,23 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.zookeeper; -import com.flipkart.ranger.zookeeper.zk.ZkSimpleShardedServiceFinderBuilder; -import com.flipkart.ranger.zookeeper.zk.ZkUnshardedFinderBuilder; +import com.flipkart.ranger.core.model.Criteria; +import com.flipkart.ranger.zookeeper.servicefinder.ZkSimpleShardedServiceFinderBuilder; +import com.flipkart.ranger.zookeeper.servicefinder.ZkSimpleUnshardedServiceFinderBuilder; public class ServiceFinderBuilders { @@ -25,11 +25,11 @@ private ServiceFinderBuilders() { throw new InstantiationError("Must not instantiate this class"); } - public static ZkSimpleShardedServiceFinderBuilder shardedFinderBuilder() { + public static > ZkSimpleShardedServiceFinderBuilder shardedFinderBuilder() { return new ZkSimpleShardedServiceFinderBuilder<>(); } - public static ZkUnshardedFinderBuilder unshardedFinderBuilder() { - return new ZkUnshardedFinderBuilder(); + public static > ZkSimpleUnshardedServiceFinderBuilder unshardedFinderBuilder() { + return new ZkSimpleUnshardedServiceFinderBuilder<>(); } } diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/ServiceProviderBuilders.java b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/ServiceProviderBuilders.java index 9feac3c4..e83abfbd 100644 --- a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/ServiceProviderBuilders.java +++ b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/ServiceProviderBuilders.java @@ -1,12 +1,12 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. @@ -16,8 +16,7 @@ package com.flipkart.ranger.zookeeper; -import com.flipkart.ranger.zookeeper.zk.ZkServiceProviderBuilder; -import com.flipkart.ranger.core.finder.unsharded.UnshardedClusterInfo; +import com.flipkart.ranger.zookeeper.serviceprovider.ZkServiceProviderBuilder; public class ServiceProviderBuilders { @@ -29,7 +28,7 @@ public static ZkServiceProviderBuilder shardedServiceProviderBuilder() { return new ZkServiceProviderBuilder<>(); } - public static ZkServiceProviderBuilder unshardedServiceProviderBuilder() { + public static ZkServiceProviderBuilder unshardedServiceProviderBuilder() { return new ZkServiceProviderBuilder<>(); } } diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkNodeDataStoreConnector.java b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/common/ZkNodeDataStoreConnector.java similarity index 85% rename from ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkNodeDataStoreConnector.java rename to ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/common/ZkNodeDataStoreConnector.java index 2ad0c2f4..79261dee 100644 --- a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkNodeDataStoreConnector.java +++ b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/common/ZkNodeDataStoreConnector.java @@ -1,4 +1,19 @@ -package com.flipkart.ranger.zookeeper.zk; +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.zookeeper.common; import com.flipkart.ranger.core.model.NodeDataStoreConnector; import com.flipkart.ranger.core.model.Service; diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/serde/ZkNodeDataDeserializer.java b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/serde/ZkNodeDataDeserializer.java index 11b22821..2990943a 100644 --- a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/serde/ZkNodeDataDeserializer.java +++ b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/serde/ZkNodeDataDeserializer.java @@ -1,3 +1,18 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.zookeeper.serde; import com.flipkart.ranger.core.model.Deserializer; diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/serde/ZkNodeDataSerializer.java b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/serde/ZkNodeDataSerializer.java index 182f001b..3a2db411 100644 --- a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/serde/ZkNodeDataSerializer.java +++ b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/serde/ZkNodeDataSerializer.java @@ -1,3 +1,18 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.zookeeper.serde; import com.flipkart.ranger.core.model.Serializer; diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkNodeDataSource.java b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinder/ZkNodeDataSource.java similarity index 83% rename from ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkNodeDataSource.java rename to ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinder/ZkNodeDataSource.java index 9d701053..d2258dfc 100644 --- a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkNodeDataSource.java +++ b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinder/ZkNodeDataSource.java @@ -1,9 +1,25 @@ -package com.flipkart.ranger.zookeeper.zk; +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.zookeeper.servicefinder; import com.flipkart.ranger.core.model.NodeDataSource; import com.flipkart.ranger.core.model.Service; import com.flipkart.ranger.core.model.ServiceNode; import com.flipkart.ranger.core.util.FinderUtils; +import com.flipkart.ranger.zookeeper.common.ZkNodeDataStoreConnector; import com.flipkart.ranger.zookeeper.serde.ZkNodeDataDeserializer; import com.flipkart.ranger.zookeeper.util.PathBuilder; import com.google.common.base.Preconditions; diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkSimpleShardedServiceFinderBuilder.java b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinder/ZkSimpleShardedServiceFinderBuilder.java similarity index 59% rename from ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkSimpleShardedServiceFinderBuilder.java rename to ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinder/ZkSimpleShardedServiceFinderBuilder.java index bfb6345d..fa055fe0 100644 --- a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkSimpleShardedServiceFinderBuilder.java +++ b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinder/ZkSimpleShardedServiceFinderBuilder.java @@ -1,11 +1,28 @@ -package com.flipkart.ranger.zookeeper.zk; +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.zookeeper.servicefinder; -import com.flipkart.ranger.core.finder.sharded.SimpleShardedServiceFinder; -import com.flipkart.ranger.core.finder.sharded.SimpleShardedServiceFinderBuilder; +import com.flipkart.ranger.core.finder.SimpleShardedServiceFinder; +import com.flipkart.ranger.core.finder.SimpleShardedServiceFinderBuilder; +import com.flipkart.ranger.core.model.Criteria; import com.flipkart.ranger.core.model.NodeDataSource; import com.flipkart.ranger.core.model.Service; import com.flipkart.ranger.core.signals.Signal; import com.flipkart.ranger.zookeeper.serde.ZkNodeDataDeserializer; +import com.flipkart.ranger.zookeeper.servicefinder.signals.ZkWatcherRegistryUpdateSignal; import com.google.common.base.Preconditions; import lombok.extern.slf4j.Slf4j; import org.apache.curator.framework.CuratorFramework; @@ -19,23 +36,22 @@ * */ @Slf4j -public class ZkSimpleShardedServiceFinderBuilder extends SimpleShardedServiceFinderBuilder, ZkNodeDataDeserializer> { +public class ZkSimpleShardedServiceFinderBuilder> extends SimpleShardedServiceFinderBuilder, ZkNodeDataDeserializer, C> { protected CuratorFramework curatorFramework; protected String connectionString; - - public ZkSimpleShardedServiceFinderBuilder withCuratorFramework(CuratorFramework curatorFramework) { + public ZkSimpleShardedServiceFinderBuilder withCuratorFramework(CuratorFramework curatorFramework) { this.curatorFramework = curatorFramework; return this; } - public ZkSimpleShardedServiceFinderBuilder withConnectionString(final String connectionString) { + public ZkSimpleShardedServiceFinderBuilder withConnectionString(final String connectionString) { this.connectionString = connectionString; return this; } @Override - public SimpleShardedServiceFinder build() { + public SimpleShardedServiceFinder build() { boolean curatorProvided = curatorFramework != null; if (!curatorProvided) { Preconditions.checkNotNull(connectionString); diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkUnshardedFinderBuilder.java b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinder/ZkSimpleUnshardedServiceFinderBuilder.java similarity index 52% rename from ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkUnshardedFinderBuilder.java rename to ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinder/ZkSimpleUnshardedServiceFinderBuilder.java index 1e0259d3..640ebb68 100644 --- a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkUnshardedFinderBuilder.java +++ b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinder/ZkSimpleUnshardedServiceFinderBuilder.java @@ -1,12 +1,28 @@ -package com.flipkart.ranger.zookeeper.zk; +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.zookeeper.servicefinder; -import com.flipkart.ranger.core.finder.unsharded.UnshardedClusterFinder; -import com.flipkart.ranger.core.finder.unsharded.UnshardedClusterInfo; -import com.flipkart.ranger.core.finder.unsharded.UnshardedFinderBuilder; +import com.flipkart.ranger.core.finder.SimpleUnshardedServiceFinder; +import com.flipkart.ranger.core.finder.SimpleUnshardedServiceFinderBuilder; +import com.flipkart.ranger.core.model.Criteria; import com.flipkart.ranger.core.model.NodeDataSource; import com.flipkart.ranger.core.model.Service; import com.flipkart.ranger.core.signals.Signal; import com.flipkart.ranger.zookeeper.serde.ZkNodeDataDeserializer; +import com.flipkart.ranger.zookeeper.servicefinder.signals.ZkWatcherRegistryUpdateSignal; import com.google.common.base.Preconditions; import lombok.extern.slf4j.Slf4j; import org.apache.curator.framework.CuratorFramework; @@ -20,22 +36,23 @@ * */ @Slf4j -public class ZkUnshardedFinderBuilder extends UnshardedFinderBuilder> { +public class ZkSimpleUnshardedServiceFinderBuilder> + extends SimpleUnshardedServiceFinderBuilder, ZkNodeDataDeserializer, C> { private CuratorFramework curatorFramework; private String connectionString; - public ZkUnshardedFinderBuilder withCuratorFramework(CuratorFramework curatorFramework) { + public ZkSimpleUnshardedServiceFinderBuilder withCuratorFramework(CuratorFramework curatorFramework) { this.curatorFramework = curatorFramework; return this; } - public ZkUnshardedFinderBuilder withConnectionString(final String connectionString) { + public ZkSimpleUnshardedServiceFinderBuilder withConnectionString(final String connectionString) { this.connectionString = connectionString; return this; } @Override - public UnshardedClusterFinder build() { + public SimpleUnshardedServiceFinder build() { boolean curatorProvided = curatorFramework != null; if (!curatorProvided) { Preconditions.checkNotNull(connectionString); @@ -50,15 +67,15 @@ public UnshardedClusterFinder build() { } @Override - protected NodeDataSource> dataSource( + protected NodeDataSource> dataSource( Service service) { return new ZkNodeDataSource<>(service, curatorFramework); } @Override - protected List> implementationSpecificRefreshSignals( - final Service service, final NodeDataSource> nodeDataSource) { + protected List> implementationSpecificRefreshSignals( + final Service service, final NodeDataSource> nodeDataSource) { if (!disablePushUpdaters) { return Collections.singletonList( new ZkWatcherRegistryUpdateSignal<>(service, nodeDataSource, curatorFramework)); diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkWatcherRegistryUpdateSignal.java b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinder/signals/ZkWatcherRegistryUpdateSignal.java similarity index 75% rename from ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkWatcherRegistryUpdateSignal.java rename to ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinder/signals/ZkWatcherRegistryUpdateSignal.java index e4d9c3dc..8b5c5659 100644 --- a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkWatcherRegistryUpdateSignal.java +++ b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinder/signals/ZkWatcherRegistryUpdateSignal.java @@ -1,9 +1,24 @@ -package com.flipkart.ranger.zookeeper.zk; +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.zookeeper.servicefinder.signals; import com.flipkart.ranger.core.model.NodeDataStoreConnector; import com.flipkart.ranger.core.model.Service; -import com.flipkart.ranger.zookeeper.util.PathBuilder; import com.flipkart.ranger.core.signals.Signal; +import com.flipkart.ranger.zookeeper.util.PathBuilder; import lombok.EqualsAndHashCode; import lombok.ToString; import lombok.extern.slf4j.Slf4j; diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinderhub/ZKUnshardedServiceFinderFactory.java b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinderhub/ZKUnshardedServiceFinderFactory.java new file mode 100644 index 00000000..8539998b --- /dev/null +++ b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinderhub/ZKUnshardedServiceFinderFactory.java @@ -0,0 +1,78 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.zookeeper.servicefinderhub; + +import com.flipkart.ranger.core.finder.SimpleUnshardedServiceFinder; +import com.flipkart.ranger.core.finder.serviceregistry.ListBasedServiceRegistry; +import com.flipkart.ranger.core.finderhub.ServiceFinderFactory; +import com.flipkart.ranger.core.model.Criteria; +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.zookeeper.serde.ZkNodeDataDeserializer; +import com.flipkart.ranger.zookeeper.servicefinder.ZkSimpleUnshardedServiceFinderBuilder; +import lombok.Builder; +import lombok.Getter; +import lombok.val; +import org.apache.curator.framework.CuratorFramework; + + +@Getter +public class ZKUnshardedServiceFinderFactory> implements ServiceFinderFactory>{ + + private final CuratorFramework curatorFramework; + private final String connectionString; + private final int nodeRefreshIntervalMs; + private final boolean disablePushUpdaters; + private final ZkNodeDataDeserializer deserializer; + private final ShardSelector> shardSelector; + private final ServiceNodeSelector nodeSelector; + + @Builder + public ZKUnshardedServiceFinderFactory( + CuratorFramework curatorFramework, + String connectionString, + int nodeRefreshIntervalMs, + boolean disablePushUpdaters, + ZkNodeDataDeserializer deserializer, + ShardSelector> shardSelector, + ServiceNodeSelector nodeSelector) { + this.curatorFramework = curatorFramework; + this.connectionString = connectionString; + this.nodeRefreshIntervalMs = nodeRefreshIntervalMs; + this.disablePushUpdaters = disablePushUpdaters; + this.deserializer = deserializer; + this.shardSelector = shardSelector; + this.nodeSelector = nodeSelector; + } + + @Override + public SimpleUnshardedServiceFinder buildFinder(Service service) { + val finder = new ZkSimpleUnshardedServiceFinderBuilder() + .withDeserializer(deserializer) + .withNamespace(service.getNamespace()) + .withServiceName(service.getServiceName()) + .withNodeRefreshIntervalMs(nodeRefreshIntervalMs) + .withDisableWatchers(disablePushUpdaters) + .withShardSelector(shardSelector) + .withNodeSelector(nodeSelector) + .withConnectionString(connectionString) + .withCuratorFramework(curatorFramework) + .build(); + finder.start(); + return finder; + } +} diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinderhub/ZkServiceDataSource.java b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinderhub/ZkServiceDataSource.java new file mode 100644 index 00000000..e13fcaf1 --- /dev/null +++ b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinderhub/ZkServiceDataSource.java @@ -0,0 +1,86 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.zookeeper.servicefinderhub; + +import com.flipkart.ranger.core.finderhub.ServiceDataSource; +import com.flipkart.ranger.core.model.Service; +import com.google.common.base.Preconditions; +import lombok.extern.slf4j.Slf4j; +import org.apache.curator.framework.CuratorFramework; +import org.apache.curator.framework.CuratorFrameworkFactory; +import org.apache.curator.retry.ExponentialBackoffRetry; + +import java.util.Collection; +import java.util.List; +import java.util.stream.Collectors; + +/** + * + */ +@Slf4j +public class ZkServiceDataSource implements ServiceDataSource { + + private final String namespace; + private String connectionString; + private CuratorFramework curatorFramework; + private boolean curatorProvided; + + public ZkServiceDataSource(String namespace, + String connectionString, + CuratorFramework curatorFramework){ + this.namespace = namespace; + this.connectionString = connectionString; + this.curatorFramework = curatorFramework; + } + + @Override + public Collection services() throws Exception { + final List children = curatorFramework.getChildren() + .forPath("/"); + return children.stream() + .map(child -> new Service(namespace, child)) + .collect(Collectors.toSet()); + } + + @Override + public void start() { + if(null == curatorFramework){ + Preconditions.checkNotNull(connectionString); + log.info("Building custom curator framework"); + curatorFramework = CuratorFrameworkFactory.builder() + .namespace(namespace) + .connectString(connectionString) + .retryPolicy(new ExponentialBackoffRetry(1000, 100)) + .build(); + curatorFramework.start(); + curatorProvided = false; + } + try { + curatorFramework.blockUntilConnected(); + } + catch (InterruptedException e) { + log.error("Curator block interrupted", e); + } + log.info("Service data source started. Curator state is: {}", curatorFramework.getState().name()); + } + + @Override + public void stop() { + log.info("Service data stopped"); + if(!curatorProvided) curatorFramework.close(); + log.info("Service data source stopped"); + } +} diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkServiceFinderHubBuilder.java b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinderhub/ZkServiceFinderHubBuilder.java similarity index 52% rename from ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkServiceFinderHubBuilder.java rename to ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinderhub/ZkServiceFinderHubBuilder.java index 9fa692b6..aead107e 100644 --- a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkServiceFinderHubBuilder.java +++ b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinderhub/ZkServiceFinderHubBuilder.java @@ -1,6 +1,23 @@ -package com.flipkart.ranger.zookeeper.zk; +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.zookeeper.servicefinderhub; import com.flipkart.ranger.core.finderhub.ServiceFinderHub; +import com.flipkart.ranger.core.finderhub.ServiceFinderHubBuilder; +import com.flipkart.ranger.core.model.Criteria; import com.flipkart.ranger.core.model.ServiceRegistry; import com.google.common.base.Preconditions; import lombok.extern.slf4j.Slf4j; @@ -12,22 +29,22 @@ * */ @Slf4j -public class ZkServiceFinderHubBuilder> extends ServiceFinderHubBuilder { +public class ZkServiceFinderHubBuilder, R extends ServiceRegistry> extends ServiceFinderHubBuilder { private String namespace; private CuratorFramework curatorFramework; private String connectionString; - public ZkServiceFinderHubBuilder withNamespace(final String namespace) { + public ZkServiceFinderHubBuilder withNamespace(final String namespace) { this.namespace = namespace; return this; } - public ZkServiceFinderHubBuilder withCuratorFramework(CuratorFramework curatorFramework) { + public ZkServiceFinderHubBuilder withCuratorFramework(CuratorFramework curatorFramework) { this.curatorFramework = curatorFramework; return this; } - public ZkServiceFinderHubBuilder withConnectionString(final String connectionString) { + public ZkServiceFinderHubBuilder withConnectionString(final String connectionString) { this.connectionString = connectionString; return this; } @@ -48,7 +65,7 @@ protected void preBuild() { } @Override - protected void postBuild(ServiceFinderHub serviceFinderHub) { + protected void postBuild(ServiceFinderHub serviceFinderHub) { log.debug("No post build steps necessary"); } } diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkShardedServiceFinderFactory.java b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinderhub/ZkShardedServiceFinderFactory.java similarity index 58% rename from ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkShardedServiceFinderFactory.java rename to ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinderhub/ZkShardedServiceFinderFactory.java index f6feca8d..d36981ff 100644 --- a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkShardedServiceFinderFactory.java +++ b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/servicefinderhub/ZkShardedServiceFinderFactory.java @@ -1,12 +1,29 @@ -package com.flipkart.ranger.zookeeper.zk; +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.zookeeper.servicefinderhub; -import com.flipkart.ranger.core.finder.sharded.MapBasedServiceRegistry; -import com.flipkart.ranger.core.finder.sharded.SimpleShardedServiceFinder; +import com.flipkart.ranger.core.finder.SimpleShardedServiceFinder; +import com.flipkart.ranger.core.finder.serviceregistry.MapBasedServiceRegistry; import com.flipkart.ranger.core.finderhub.ServiceFinderFactory; +import com.flipkart.ranger.core.model.Criteria; 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.zookeeper.serde.ZkNodeDataDeserializer; +import com.flipkart.ranger.zookeeper.servicefinder.ZkSimpleShardedServiceFinderBuilder; import lombok.Builder; import lombok.val; import org.apache.curator.framework.CuratorFramework; @@ -14,13 +31,13 @@ /** * */ -public class ZkShardedServiceFinderFactory implements ServiceFinderFactory> { +public class ZkShardedServiceFinderFactory> implements ServiceFinderFactory> { private final CuratorFramework curatorFramework; private final String connectionString; private final int nodeRefreshIntervalMs; private final boolean disablePushUpdaters; private final ZkNodeDataDeserializer deserializer; - private final ShardSelector> shardSelector; + private final ShardSelector> shardSelector; private final ServiceNodeSelector nodeSelector; @Builder @@ -30,7 +47,7 @@ public ZkShardedServiceFinderFactory( int nodeRefreshIntervalMs, boolean disablePushUpdaters, ZkNodeDataDeserializer deserializer, - ShardSelector> shardSelector, + ShardSelector> shardSelector, ServiceNodeSelector nodeSelector) { this.curatorFramework = curatorFramework; this.connectionString = connectionString; @@ -42,8 +59,8 @@ public ZkShardedServiceFinderFactory( } @Override - public SimpleShardedServiceFinder buildFinder(Service service) { - val finder = new ZkSimpleShardedServiceFinderBuilder() + public SimpleShardedServiceFinder buildFinder(Service service) { + val finder = new ZkSimpleShardedServiceFinderBuilder() .withDeserializer(deserializer) .withNamespace(service.getNamespace()) .withServiceName(service.getServiceName()) diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkNodeDataSink.java b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/serviceprovider/ZkNodeDataSink.java similarity index 79% rename from ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkNodeDataSink.java rename to ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/serviceprovider/ZkNodeDataSink.java index 3e5d27ab..3792e731 100644 --- a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkNodeDataSink.java +++ b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/serviceprovider/ZkNodeDataSink.java @@ -1,9 +1,25 @@ -package com.flipkart.ranger.zookeeper.zk; +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.zookeeper.serviceprovider; import com.flipkart.ranger.core.model.NodeDataSink; import com.flipkart.ranger.core.model.Service; import com.flipkart.ranger.core.model.ServiceNode; import com.flipkart.ranger.core.util.Exceptions; +import com.flipkart.ranger.zookeeper.common.ZkNodeDataStoreConnector; import com.flipkart.ranger.zookeeper.serde.ZkNodeDataSerializer; import com.flipkart.ranger.zookeeper.util.PathBuilder; import com.google.common.base.Preconditions; diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkServiceProviderBuilder.java b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/serviceprovider/ZkServiceProviderBuilder.java similarity index 74% rename from ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkServiceProviderBuilder.java rename to ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/serviceprovider/ZkServiceProviderBuilder.java index cb6a47f8..1a838324 100644 --- a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkServiceProviderBuilder.java +++ b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/serviceprovider/ZkServiceProviderBuilder.java @@ -1,4 +1,19 @@ -package com.flipkart.ranger.zookeeper.zk; +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

+ * 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 + *

+ * http://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.zookeeper.serviceprovider; import com.flipkart.ranger.core.model.NodeDataSink; import com.flipkart.ranger.core.model.Service; diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/util/PathBuilder.java b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/util/PathBuilder.java index 02542579..3474c749 100644 --- a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/util/PathBuilder.java +++ b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/util/PathBuilder.java @@ -1,12 +1,12 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. diff --git a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkServiceDataSource.java b/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkServiceDataSource.java deleted file mode 100644 index 3f3cfd00..00000000 --- a/ranger-zookeeper/src/main/java/com/flipkart/ranger/zookeeper/zk/ZkServiceDataSource.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.flipkart.ranger.zookeeper.zk; - -import com.flipkart.ranger.core.finderhub.ServiceDataSource; -import com.flipkart.ranger.core.model.Service; -import lombok.extern.slf4j.Slf4j; -import org.apache.curator.framework.CuratorFramework; - -import java.util.Collection; -import java.util.List; -import java.util.stream.Collectors; - -/** - * - */ -@Slf4j -public class ZkServiceDataSource implements ServiceDataSource { - - private final String namespace; - private final CuratorFramework curatorFramework; - - public ZkServiceDataSource(String namespace, CuratorFramework curatorFramework) { - this.namespace = namespace; - this.curatorFramework = curatorFramework; - } - - @Override - public Collection services() throws Exception { - final List children = curatorFramework.getChildren() - .forPath("/"); - return children.stream() - .map(child -> new Service(namespace, child)) - .collect(Collectors.toSet()); - } - - @Override - public void start() { - try { - curatorFramework.blockUntilConnected(); - } - catch (InterruptedException e) { - log.error("Curator block interrupted", e); - } - log.info("Service data source started. Curator state is: {}", curatorFramework.getState().name()); - } - - @Override - public void stop() { - log.info("Service data source stopped"); - } -} diff --git a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/ServiceHealthAggregatorTest.java b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/ServiceHealthAggregatorTest.java index 6f52e2eb..bc217329 100644 --- a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/ServiceHealthAggregatorTest.java +++ b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/ServiceHealthAggregatorTest.java @@ -1,27 +1,26 @@ -/** - * Copyright 2016 Flipkart Internet Pvt. Ltd. - * +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.zookeeper.healthservice; +import com.flipkart.ranger.core.TestUtils; import com.flipkart.ranger.core.healthcheck.HealthcheckStatus; import com.flipkart.ranger.core.healthservice.ServiceHealthAggregator; import com.flipkart.ranger.core.healthservice.TimeEntity; import com.flipkart.ranger.core.healthservice.monitor.IsolatedHealthMonitor; import com.flipkart.ranger.core.healthservice.monitor.Monitor; -import com.flipkart.ranger.core.utils.TestUtils; import org.junit.After; import org.junit.Assert; import org.junit.Before; diff --git a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/ServiceProviderIntegrationTest.java b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/ServiceProviderIntegrationTest.java index 64ed9401..3219233f 100644 --- a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/ServiceProviderIntegrationTest.java +++ b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/ServiceProviderIntegrationTest.java @@ -1,12 +1,12 @@ -/** - * Copyright 2016 Flipkart Internet Pvt. Ltd. - * +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. @@ -19,14 +19,14 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.flipkart.ranger.core.finder.unsharded.UnshardedClusterFinder; -import com.flipkart.ranger.core.finder.unsharded.UnshardedClusterInfo; +import com.flipkart.ranger.core.TestUtils; +import com.flipkart.ranger.core.finder.SimpleUnshardedServiceFinder; import com.flipkart.ranger.core.healthcheck.Healthchecks; import com.flipkart.ranger.core.healthservice.TimeEntity; import com.flipkart.ranger.core.healthservice.monitor.sample.RotationStatusMonitor; +import com.flipkart.ranger.core.model.Criteria; import com.flipkart.ranger.core.model.ServiceNode; import com.flipkart.ranger.core.util.Exceptions; -import com.flipkart.ranger.core.utils.TestUtils; import com.flipkart.ranger.zookeeper.ServiceFinderBuilders; import com.flipkart.ranger.zookeeper.ServiceProviderBuilders; import lombok.val; @@ -50,8 +50,19 @@ public class ServiceProviderIntegrationTest { private TestingCluster testingCluster; private ObjectMapper objectMapper; - UnshardedClusterFinder serviceFinder; + SimpleUnshardedServiceFinder serviceFinder; + private static final class UnshardedClusterInfo { + @Override + public int hashCode() { + return 0; + } + + @Override + public boolean equals(Object obj) { + return super.equals(obj); + } + } @Before public void startTestCluster() throws Exception { objectMapper = new ObjectMapper(); @@ -65,7 +76,7 @@ public void startTestCluster() throws Exception { registerService("localhost-4", 9000, 2, anotherFile); - serviceFinder = ServiceFinderBuilders.unshardedFinderBuilder() + serviceFinder = ServiceFinderBuilders.>unshardedFinderBuilder() .withConnectionString(testingCluster.getConnectString()) .withNamespace("test") .withServiceName("test-service") diff --git a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/monitor/RollingWindowHealthQueueTest.java b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/monitor/RollingWindowHealthQueueTest.java index 065cc7a3..34b58db5 100644 --- a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/monitor/RollingWindowHealthQueueTest.java +++ b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/monitor/RollingWindowHealthQueueTest.java @@ -1,19 +1,18 @@ -/** - * Copyright 2016 Flipkart Internet Pvt. Ltd. - * +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.zookeeper.healthservice.monitor; import com.flipkart.ranger.core.healthcheck.HealthcheckStatus; diff --git a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/monitor/sample/DiskSpaceMonitorTest.java b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/monitor/sample/DiskSpaceMonitorTest.java index 54ea08d5..be3c5e07 100644 --- a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/monitor/sample/DiskSpaceMonitorTest.java +++ b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/monitor/sample/DiskSpaceMonitorTest.java @@ -1,12 +1,12 @@ -/** - * Copyright 2016 Flipkart Internet Pvt. Ltd. - * +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. diff --git a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/monitor/sample/PingCheckMonitorTest.java b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/monitor/sample/PingCheckMonitorTest.java index 2a4cb14f..d0e0a9cc 100644 --- a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/monitor/sample/PingCheckMonitorTest.java +++ b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/monitor/sample/PingCheckMonitorTest.java @@ -1,19 +1,18 @@ -/** - * Copyright 2016 Flipkart Internet Pvt. Ltd. - * +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.zookeeper.healthservice.monitor.sample; import com.flipkart.ranger.core.healthcheck.HealthcheckStatus; diff --git a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/monitor/sample/RotationStatusMonitorTest.java b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/monitor/sample/RotationStatusMonitorTest.java index 22457cbb..63c4264a 100644 --- a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/monitor/sample/RotationStatusMonitorTest.java +++ b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/healthservice/monitor/sample/RotationStatusMonitorTest.java @@ -1,19 +1,18 @@ -/** - * Copyright 2016 Flipkart Internet Pvt. Ltd. - * +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.zookeeper.healthservice.monitor.sample; import com.flipkart.ranger.core.healthcheck.HealthcheckStatus; diff --git a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/CustomShardSelectorTest.java b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/CustomShardSelectorTest.java index 36587d52..d489e6b3 100644 --- a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/CustomShardSelectorTest.java +++ b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/CustomShardSelectorTest.java @@ -1,27 +1,27 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.zookeeper.model; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.flipkart.ranger.core.finder.sharded.MapBasedServiceRegistry; -import com.flipkart.ranger.core.finder.sharded.SimpleShardedServiceFinder; +import com.flipkart.ranger.core.finder.SimpleShardedServiceFinder; +import com.flipkart.ranger.core.finder.serviceregistry.MapBasedServiceRegistry; import com.flipkart.ranger.core.healthcheck.Healthchecks; +import com.flipkart.ranger.core.model.Criteria; import com.flipkart.ranger.core.model.ServiceNode; import com.flipkart.ranger.core.model.ShardSelector; import com.flipkart.ranger.core.serviceprovider.ServiceProvider; @@ -40,6 +40,7 @@ import java.util.List; import java.util.Map; + @Slf4j public class CustomShardSelectorTest { private TestingCluster testingCluster; @@ -111,16 +112,20 @@ public int hashCode() { result = 31 * result + b; return result; } + + private static Criteria getCriteria(int a, int b){ + return nodeData -> nodeData.getA() == a && nodeData.getB() == b; + } } - private static final class TestShardSelector implements ShardSelector> { + private static final class TestShardSelector implements ShardSelector, MapBasedServiceRegistry> { @Override - public List> nodes(TestShardInfo criteria, MapBasedServiceRegistry serviceRegistry) { + public List> nodes(Criteria criteria, MapBasedServiceRegistry serviceRegistry) { List> nodes = Lists.newArrayList(); for(Map.Entry> entry : serviceRegistry.nodes().entries()) { TestShardInfo shardInfo = entry.getKey(); - if((shardInfo.getA() + shardInfo.getB()) == (criteria.getA() + criteria.getB())) { + if(criteria.apply(shardInfo)){ nodes.add(entry.getValue()); } } @@ -130,7 +135,7 @@ public List> nodes(TestShardInfo criteria, MapBasedSe @Test public void testBasicDiscovery() throws Exception { - SimpleShardedServiceFinder serviceFinder = ServiceFinderBuilders.shardedFinderBuilder() + SimpleShardedServiceFinder> serviceFinder = ServiceFinderBuilders.>shardedFinderBuilder() .withConnectionString(testingCluster.getConnectString()) .withNamespace("test") .withServiceName("test-service") @@ -147,18 +152,18 @@ public void testBasicDiscovery() throws Exception { .build(); serviceFinder.start(); { - ServiceNode node = serviceFinder.get(new TestShardInfo(1, 10)); + ServiceNode node = serviceFinder.get(TestShardInfo.getCriteria(1, 10)); Assert.assertNull(node); } { - ServiceNode node = serviceFinder.get(new TestShardInfo(1, 2)); + ServiceNode node = serviceFinder.get(TestShardInfo.getCriteria(1, 2)); Assert.assertNotNull(node); Assert.assertEquals(new TestShardInfo(1, 2), node.getNodeData()); } serviceFinder.stop(); } - private void registerService(String host, int port, int a, int b) throws Exception { + private void registerService(String host, int port, int a, int b) { final ServiceProvider> serviceProvider = ServiceProviderBuilders.shardedServiceProviderBuilder() .withConnectionString(testingCluster.getConnectString()) .withNamespace("test") diff --git a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/ServiceNoProviderTest.java b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/ServiceNoProviderTest.java index 0bb56679..ebee0623 100644 --- a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/ServiceNoProviderTest.java +++ b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/ServiceNoProviderTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. *

* Licensed under the Apache License, Version 2.0 (the "License"); @@ -13,13 +13,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - package com.flipkart.ranger.zookeeper.model; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.flipkart.ranger.core.finder.RoundRobinServiceNodeSelector; -import com.flipkart.ranger.core.finder.sharded.SimpleShardedServiceFinder; +import com.flipkart.ranger.core.finder.SimpleShardedServiceFinder; +import com.flipkart.ranger.core.finder.nodeselector.RoundRobinServiceNodeSelector; +import com.flipkart.ranger.core.model.Criteria; import com.flipkart.ranger.core.model.ServiceNode; import com.flipkart.ranger.zookeeper.ServiceFinderBuilders; import lombok.val; @@ -93,11 +93,16 @@ public boolean equals(Object o) { public int hashCode() { return shardId; } + + private static Criteria getCriteria(int shardId){ + return nodeData -> shardId == nodeData.getShardId(); + } } + @Test public void testBasicDiscovery() throws Exception { - SimpleShardedServiceFinder serviceFinder = ServiceFinderBuilders.shardedFinderBuilder() + SimpleShardedServiceFinder> serviceFinder = ServiceFinderBuilders.>shardedFinderBuilder() .withConnectionString(testingCluster.getConnectString()) .withNamespace("test") .withServiceName("test-service") @@ -114,15 +119,15 @@ public void testBasicDiscovery() throws Exception { }) .build(); serviceFinder.start(); - ServiceNode node = serviceFinder.get(new TestShardInfo(1)); + ServiceNode node = serviceFinder.get(TestShardInfo.getCriteria(1)); Assert.assertNull(node); serviceFinder.stop(); } @Test - public void testBasicDiscoveryRR() throws Exception { - val serviceFinder = ServiceFinderBuilders.shardedFinderBuilder() + public void testBasicDiscoveryRR() { + val serviceFinder = ServiceFinderBuilders.>shardedFinderBuilder() .withConnectionString(testingCluster.getConnectString()) .withNamespace("test") .withServiceName("test-service") @@ -140,7 +145,7 @@ public void testBasicDiscoveryRR() throws Exception { }) .build(); serviceFinder.start(); - ServiceNode node = serviceFinder.get(new TestShardInfo(1)); + ServiceNode node = serviceFinder.get(TestShardInfo.getCriteria(1)); Assert.assertNull(node); serviceFinder.stop(); } diff --git a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/ServiceProviderExtCuratorTest.java b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/ServiceProviderExtCuratorTest.java index 5c8b1183..68fbb4f6 100644 --- a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/ServiceProviderExtCuratorTest.java +++ b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/ServiceProviderExtCuratorTest.java @@ -1,26 +1,26 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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.zookeeper.model; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.flipkart.ranger.core.finder.sharded.SimpleShardedServiceFinder; +import com.flipkart.ranger.core.finder.SimpleShardedServiceFinder; import com.flipkart.ranger.core.healthcheck.Healthchecks; +import com.flipkart.ranger.core.model.Criteria; import com.flipkart.ranger.core.model.ServiceNode; import com.flipkart.ranger.core.serviceprovider.ServiceProvider; import com.flipkart.ranger.zookeeper.ServiceFinderBuilders; @@ -108,11 +108,16 @@ public boolean equals(Object o) { public int hashCode() { return shardId; } + + private static Criteria getCriteria(int shardId){ + return nodeData -> nodeData.getShardId() == shardId; + } + } @Test - public void testBasicDiscovery() throws Exception { - SimpleShardedServiceFinder serviceFinder = ServiceFinderBuilders.shardedFinderBuilder() + public void testBasicDiscovery() { + SimpleShardedServiceFinder> serviceFinder = ServiceFinderBuilders.>shardedFinderBuilder() .withCuratorFramework(curatorFramework) .withNamespace("test") .withServiceName("test-service") @@ -129,25 +134,25 @@ public void testBasicDiscovery() throws Exception { .build(); serviceFinder.start(); { - ServiceNode node = serviceFinder.get(new TestShardInfo(1)); + ServiceNode node = serviceFinder.get(TestShardInfo.getCriteria(1)); Assert.assertNotNull(node); Assert.assertEquals(1, node.getNodeData().getShardId()); } { - ServiceNode node = serviceFinder.get(new TestShardInfo(1)); + ServiceNode node = serviceFinder.get(TestShardInfo.getCriteria(1)); Assert.assertNotNull(node); Assert.assertEquals(1, node.getNodeData().getShardId()); } long startTime = System.currentTimeMillis(); for(long i = 0; i <1000000; i++) { - ServiceNode node = serviceFinder.get(new TestShardInfo(2)); + ServiceNode node = serviceFinder.get(TestShardInfo.getCriteria(2)); Assert.assertNotNull(node); Assert.assertEquals(2, node.getNodeData().getShardId()); } log.info("PERF::RandomSelector::Took (ms):" + (System.currentTimeMillis() - startTime)); { - ServiceNode node = serviceFinder.get(new TestShardInfo(99)); + ServiceNode node = serviceFinder.get(TestShardInfo.getCriteria(99)); Assert.assertNull(node); } serviceFinder.stop(); diff --git a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/ServiceProviderHealthcheckTest.java b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/ServiceProviderHealthcheckTest.java index 3e31e1e9..bbeef818 100644 --- a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/ServiceProviderHealthcheckTest.java +++ b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/ServiceProviderHealthcheckTest.java @@ -1,12 +1,12 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - *

+ *

* 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 - *

+ *

* http://www.apache.org/licenses/LICENSE-2.0 - *

+ *

* 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. @@ -19,12 +19,13 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.flipkart.ranger.core.finder.sharded.SimpleShardedServiceFinder; +import com.flipkart.ranger.core.TestUtils; +import com.flipkart.ranger.core.finder.SimpleShardedServiceFinder; import com.flipkart.ranger.core.healthcheck.Healthcheck; import com.flipkart.ranger.core.healthcheck.HealthcheckStatus; +import com.flipkart.ranger.core.model.Criteria; import com.flipkart.ranger.core.model.ServiceNode; import com.flipkart.ranger.core.serviceprovider.ServiceProvider; -import com.flipkart.ranger.core.utils.TestUtils; import com.flipkart.ranger.zookeeper.ServiceFinderBuilders; import com.flipkart.ranger.zookeeper.ServiceProviderBuilders; import com.flipkart.ranger.zookeeper.serde.ZkNodeDataSerializer; @@ -95,11 +96,15 @@ public boolean equals(Object o) { public int hashCode() { return shardId; } + + private static Criteria getCriteria(int shardId){ + return nodeData -> nodeData.getShardId() == shardId; + } } @Test - public void testBasicDiscovery() throws Exception { - SimpleShardedServiceFinder serviceFinder = ServiceFinderBuilders.shardedFinderBuilder() + public void testBasicDiscovery() { + SimpleShardedServiceFinder> serviceFinder = ServiceFinderBuilders.>shardedFinderBuilder() .withConnectionString(testingCluster.getConnectString()) .withNamespace("test") .withServiceName("test-service") @@ -116,13 +121,13 @@ public void testBasicDiscovery() throws Exception { .withNodeRefreshIntervalMs(1000) .build(); serviceFinder.start(); - ServiceNode node = serviceFinder.get(new TestShardInfo(1)); + ServiceNode node = serviceFinder.get(TestShardInfo.getCriteria(1)); Assert.assertNotNull(node); Assert.assertEquals("localhost-1", node.getHost()); TestServiceProvider testServiceProvider = serviceProviders.get(node.getHost()); testServiceProvider.oor(); TestUtils.sleepForSeconds(6); - Assert.assertNull(serviceFinder.get(new TestShardInfo(1))); + Assert.assertNull(serviceFinder.get(TestShardInfo.getCriteria(1))); serviceFinder.stop(); } diff --git a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/ServiceProviderTest.java b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/ServiceProviderTest.java index 23432125..55041fd0 100644 --- a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/ServiceProviderTest.java +++ b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/ServiceProviderTest.java @@ -1,4 +1,4 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. *

* Licensed under the Apache License, Version 2.0 (the "License"); @@ -19,9 +19,10 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.flipkart.ranger.core.finder.RoundRobinServiceNodeSelector; -import com.flipkart.ranger.core.finder.sharded.SimpleShardedServiceFinder; +import com.flipkart.ranger.core.finder.SimpleShardedServiceFinder; +import com.flipkart.ranger.core.finder.nodeselector.RoundRobinServiceNodeSelector; import com.flipkart.ranger.core.healthcheck.Healthchecks; +import com.flipkart.ranger.core.model.Criteria; import com.flipkart.ranger.core.model.ServiceNode; import com.flipkart.ranger.core.serviceprovider.ServiceProvider; import com.flipkart.ranger.zookeeper.ServiceFinderBuilders; @@ -106,11 +107,15 @@ public boolean equals(Object o) { public int hashCode() { return shardId; } + + private static Criteria getCriteria(int shardId){ + return nodeData -> nodeData.getShardId() == shardId; + } } @Test public void testBasicDiscovery() throws Exception { - SimpleShardedServiceFinder serviceFinder = ServiceFinderBuilders.shardedFinderBuilder() + SimpleShardedServiceFinder> serviceFinder = ServiceFinderBuilders.>shardedFinderBuilder() .withConnectionString(testingCluster.getConnectString()) .withNamespace("test") .withServiceName("test-service") @@ -126,24 +131,24 @@ public void testBasicDiscovery() throws Exception { .build(); serviceFinder.start(); { - ServiceNode node = serviceFinder.get(new TestShardInfo(1)); + ServiceNode node = serviceFinder.get(TestShardInfo.getCriteria(1)); Assert.assertNotNull(node); Assert.assertEquals(1, node.getNodeData().getShardId()); } { - ServiceNode node = serviceFinder.get(new TestShardInfo(1)); + ServiceNode node = serviceFinder.get(TestShardInfo.getCriteria(1)); Assert.assertNotNull(node); Assert.assertEquals(1, node.getNodeData().getShardId()); } long startTime = System.currentTimeMillis(); for (long i = 0; i < 1000000; i++) { - ServiceNode node = serviceFinder.get(new TestShardInfo(2)); + ServiceNode node = serviceFinder.get(TestShardInfo.getCriteria(2)); Assert.assertNotNull(node); Assert.assertEquals(2, node.getNodeData().getShardId()); } log.info("PERF::RandomSelector::Took (ms):" + (System.currentTimeMillis() - startTime)); { - ServiceNode node = serviceFinder.get(new TestShardInfo(99)); + ServiceNode node = serviceFinder.get(TestShardInfo.getCriteria(99)); Assert.assertNull(node); } serviceFinder.stop(); @@ -152,8 +157,8 @@ public void testBasicDiscovery() throws Exception { @Test public void testBasicDiscoveryRR() throws Exception { - SimpleShardedServiceFinder serviceFinder - = ServiceFinderBuilders.shardedFinderBuilder() + SimpleShardedServiceFinder> serviceFinder + = ServiceFinderBuilders.>shardedFinderBuilder() .withConnectionString(testingCluster.getConnectString()) .withNamespace("test") .withServiceName("test-service") @@ -172,24 +177,24 @@ public void testBasicDiscoveryRR() throws Exception { .build(); serviceFinder.start(); { - ServiceNode node = serviceFinder.get(new TestShardInfo(1)); + ServiceNode node = serviceFinder.get(TestShardInfo.getCriteria(1)); Assert.assertNotNull(node); Assert.assertEquals(1, node.getNodeData().getShardId()); } { - ServiceNode node = serviceFinder.get(new TestShardInfo(1)); + ServiceNode node = serviceFinder.get(TestShardInfo.getCriteria(1)); Assert.assertNotNull(node); Assert.assertEquals(1, node.getNodeData().getShardId()); } long startTime = System.currentTimeMillis(); for (long i = 0; i < 1000000; i++) { - ServiceNode node = serviceFinder.get(new TestShardInfo(2)); + ServiceNode node = serviceFinder.get(TestShardInfo.getCriteria(2)); Assert.assertNotNull(node); Assert.assertEquals(2, node.getNodeData().getShardId()); } log.info("PERF::RoundRobinSelector::Took (ms):" + (System.currentTimeMillis() - startTime)); { - ServiceNode node = serviceFinder.get(new TestShardInfo(99)); + ServiceNode node = serviceFinder.get(TestShardInfo.getCriteria(99)); Assert.assertNull(node); } serviceFinder.stop(); @@ -197,8 +202,9 @@ public void testBasicDiscoveryRR() throws Exception { } @Test - public void testVisibility() throws Exception { - SimpleShardedServiceFinder serviceFinder = ServiceFinderBuilders.shardedFinderBuilder() + public void testVisibility() { + SimpleShardedServiceFinder> serviceFinder = ServiceFinderBuilders. + >shardedFinderBuilder() .withConnectionString(testingCluster.getConnectString()) .withNamespace("test") .withServiceName("test-service") @@ -215,7 +221,7 @@ public void testVisibility() throws Exception { }) .build(); serviceFinder.start(); - List> all = serviceFinder.getAll(new TestShardInfo(1)); + List> all = serviceFinder.getAll(TestShardInfo.getCriteria(1)); log.info("Testing ServiceFinder.getAll()"); for (ServiceNode serviceNode : all) { log.info("node = " + serviceNode.getHost() + ":" + serviceNode.getPort() + " " + serviceNode.getHealthcheckStatus() + " " + serviceNode @@ -225,7 +231,7 @@ public void testVisibility() throws Exception { serviceFinder.stop(); } - private void registerService(String host, int port, int shardId) throws Exception { + private void registerService(String host, int port, int shardId) { final ServiceProvider> serviceProvider = ServiceProviderBuilders.shardedServiceProviderBuilder() .withConnectionString(testingCluster.getConnectString()) .withNamespace("test") diff --git a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/SimpleServiceProviderTest.java b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/SimpleServiceProviderTest.java index 71d163bb..e875c186 100644 --- a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/SimpleServiceProviderTest.java +++ b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/model/SimpleServiceProviderTest.java @@ -1,12 +1,12 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. @@ -19,9 +19,9 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.flipkart.ranger.core.finder.unsharded.UnshardedClusterFinder; -import com.flipkart.ranger.core.finder.unsharded.UnshardedClusterInfo; +import com.flipkart.ranger.core.finder.SimpleUnshardedServiceFinder; import com.flipkart.ranger.core.healthcheck.Healthchecks; +import com.flipkart.ranger.core.model.Criteria; import com.flipkart.ranger.core.model.ServiceNode; import com.flipkart.ranger.zookeeper.ServiceFinderBuilders; import com.flipkart.ranger.zookeeper.ServiceProviderBuilders; @@ -58,23 +58,36 @@ public void stopTestCluster() throws Exception { } } + private static class UnshardedInfo { + + @Override + public int hashCode() { + return 0; + } + + @Override + public boolean equals(Object obj) { + return super.equals(obj); + } + } + @Test - public void testBasicDiscovery() throws Exception { - UnshardedClusterFinder serviceFinder = ServiceFinderBuilders.unshardedFinderBuilder() + public void testBasicDiscovery() { + SimpleUnshardedServiceFinder> serviceFinder = ServiceFinderBuilders.>unshardedFinderBuilder() .withConnectionString(testingCluster.getConnectString()) .withNamespace("test") .withServiceName("test-service") + .withDisableWatchers() .withDeserializer(data -> { try { return objectMapper.readValue(data, - new TypeReference>() { + new TypeReference>() { }); } catch (IOException e) { e.printStackTrace(); } return null; }) - .withDisableWatchers() .build(); serviceFinder.start(); { @@ -95,8 +108,8 @@ public void testBasicDiscovery() throws Exception { //while (true); } - private void registerService(String host, int port, int shardId) throws Exception { - val serviceProvider = ServiceProviderBuilders.unshardedServiceProviderBuilder() + private void registerService(String host, int port, int shardId) { + val serviceProvider = ServiceProviderBuilders.unshardedServiceProviderBuilder() .withConnectionString(testingCluster.getConnectString()) .withNamespace("test") .withServiceName("test-service") diff --git a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/servicehub/ServiceHubTest.java b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/servicehub/ServiceHubTest.java index 6292b505..e563d2e2 100644 --- a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/servicehub/ServiceHubTest.java +++ b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/servicehub/ServiceHubTest.java @@ -1,20 +1,36 @@ +/* + * Copyright 2015 Flipkart Internet Pvt. Ltd. + * + * 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 + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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.zookeeper.servicehub; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; -import com.flipkart.ranger.core.finder.sharded.MapBasedServiceRegistry; +import com.flipkart.ranger.core.TestUtils; +import com.flipkart.ranger.core.finder.serviceregistry.MapBasedServiceRegistry; import com.flipkart.ranger.core.healthcheck.HealthcheckResult; import com.flipkart.ranger.core.healthcheck.HealthcheckStatus; +import com.flipkart.ranger.core.model.Criteria; import com.flipkart.ranger.core.model.Service; import com.flipkart.ranger.core.model.ServiceNode; import com.flipkart.ranger.core.signals.ExternalTriggeredSignal; import com.flipkart.ranger.core.util.Exceptions; -import com.flipkart.ranger.core.utils.TestUtils; import com.flipkart.ranger.zookeeper.ServiceProviderBuilders; -import com.flipkart.ranger.zookeeper.zk.ZkServiceDataSource; -import com.flipkart.ranger.zookeeper.zk.ZkShardedServiceFinderFactory; -import com.flipkart.ranger.zookeeper.zk.ZkServiceFinderHubBuilder; +import com.flipkart.ranger.zookeeper.servicefinderhub.ZkServiceDataSource; +import com.flipkart.ranger.zookeeper.servicefinderhub.ZkServiceFinderHubBuilder; +import com.flipkart.ranger.zookeeper.servicefinderhub.ZkShardedServiceFinderFactory; import lombok.Data; import lombok.extern.slf4j.Slf4j; import lombok.val; @@ -68,14 +84,14 @@ public void stopTestCluster() throws Exception { } @Test - public void testHub() throws InterruptedException { + public void testHub() { ExternalTriggeredSignal refreshHubSignal = new ExternalTriggeredSignal<>(() -> null, Collections.emptyList()); - val hub = new ZkServiceFinderHubBuilder>() + val hub = new ZkServiceFinderHubBuilder, MapBasedServiceRegistry>() .withCuratorFramework(curatorFramework) .withNamespace("test") .withRefreshFrequencyMs(1000) - .withServiceDataSource(new ZkServiceDataSource("test", curatorFramework)) - .withServiceFinderFactory(ZkShardedServiceFinderFactory.builder() + .withServiceDataSource(new ZkServiceDataSource("test", testingCluster.getConnectString(), curatorFramework)) + .withServiceFinderFactory(ZkShardedServiceFinderFactory.>builder() .curatorFramework(curatorFramework) .deserializer(this::read) .build()) @@ -96,7 +112,7 @@ public void testHub() throws InterruptedException { .withSerializer(this::write) .withNodeData(new TestShardInfo("prod")) .withHealthcheck(() -> HealthcheckStatus.healthy) - .withExtraRefreshSignal(refreshProviderSignal) + .withadditionalRefreshSignal(refreshProviderSignal) .withCuratorFramework(curatorFramework) .build(); provider1.start(); @@ -106,7 +122,7 @@ public void testHub() throws InterruptedException { TestUtils.sleepForSeconds(3); val node = hub.finder(new Service(NAMESPACE, "s1")) - .map(finder -> finder.get(new TestShardInfo("prod"))) + .map(finder -> finder.get(nodeData -> nodeData.getEnvironment().equalsIgnoreCase("prod"))) .orElse(null); Assert.assertNotNull(node); hub.stop(); diff --git a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/serviceprovider/BaseServiceProviderBuilderTest.java b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/serviceprovider/BaseServiceProviderBuilderTest.java index 110f9dd7..06bb8f49 100644 --- a/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/serviceprovider/BaseServiceProviderBuilderTest.java +++ b/ranger-zookeeper/src/test/java/com/flipkart/ranger/zookeeper/serviceprovider/BaseServiceProviderBuilderTest.java @@ -1,12 +1,12 @@ -/** +/* * Copyright 2015 Flipkart Internet Pvt. Ltd. - * + *

* 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 - * + *

* http://www.apache.org/licenses/LICENSE-2.0 - * + *

* 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. @@ -52,7 +52,7 @@ public void stopTestCluster() throws Exception { } @Test - public void testbuilder() throws Exception { + public void testbuilder() { final String host = "localhost"; final int port = 9000; Exception exception = null; @@ -95,5 +95,6 @@ public void testbuilder() throws Exception { .withPort(port) .withHealthUpdateIntervalMs(1000) .build(); + serviceProvider.start(); } } \ No newline at end of file