Skip to content

Commit

Permalink
1. Added a ranger-client module to contain the client creation abstra…
Browse files Browse the repository at this point in the history
…ctions and added http and zk clients respectively with required test cases. This will help end-clients not replicate the same boiler plate code again and again
  • Loading branch information
koushikr committed Oct 18, 2021
1 parent 77981ea commit 7fdeb8f
Show file tree
Hide file tree
Showing 25 changed files with 1,641 additions and 5 deletions.
4 changes: 3 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
<module>ranger-core</module>
<module>ranger-zookeeper</module>
<module>ranger-http</module>
<module>ranger-server</module>
<module>ranger-client</module>
<module>ranger-zk-client</module>
<module>ranger-http-client</module>
</modules>

<distributionManagement>
Expand Down
16 changes: 13 additions & 3 deletions ranger-server/pom.xml → ranger-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,24 @@
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>ranger-server</artifactId>
<artifactId>ranger-client</artifactId>

<dependencies>
<dependency>
<groupId>com.flipkart.ranger</groupId>
<artifactId>ranger-zookeeper</artifactId>
<artifactId>ranger-core</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.flipkart.ranger</groupId>
<artifactId>ranger-core</artifactId>
<version>${project.version}</version>
<type>test-jar</type>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
</dependencies>

</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
/*
* 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.client;

import com.fasterxml.jackson.databind.ObjectMapper;
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.*;
import com.google.common.base.Preconditions;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

import java.util.List;
import java.util.Optional;

@Slf4j
@Getter
public abstract class AbstractRangerHubClient<T, C extends Criteria<T>, R extends ServiceRegistry<T>, D extends Deserializer<T>> implements RangerHubClient<T, C> {

private final String namespace;
private final ObjectMapper mapper;
private final C criteria;
private final D deserializer;

private int nodeRefreshTimeMs;
private ServiceFinderHub<T, C, R> hub;

public AbstractRangerHubClient(
String namespace,
ObjectMapper mapper,
int nodeRefreshTimeMs,
C criteria,
D deserializer
){
this.namespace = namespace;
this.mapper = mapper;
this.nodeRefreshTimeMs = nodeRefreshTimeMs;
this.criteria = criteria;
this.deserializer = deserializer;
}

public void start(){
Preconditions.checkNotNull(mapper, "Mapper can't be null");
Preconditions.checkNotNull(namespace, "namespace can't be null");
Preconditions.checkNotNull(deserializer, "deserializer can't be null");

if (this.nodeRefreshTimeMs < RangerClientConstants.MINIMUM_REFRESH_TIME) {
log.warn("Node info update interval too low: {} ms. Has been upgraded to {} ms ",
this.nodeRefreshTimeMs,
RangerClientConstants.MINIMUM_REFRESH_TIME);
}
this.nodeRefreshTimeMs = Math.max(RangerClientConstants.MINIMUM_REFRESH_TIME, this.nodeRefreshTimeMs);
this.hub = buildHub();
this.hub.start();
}

public void stop(){
hub.stop();
}

public Optional<ServiceNode<T>> getNode(
final Service service
){
return getNode(service, criteria);
}

public Optional<List<ServiceNode<T>>> getAllNodes(
final Service service
){
return getAllNodes(service, criteria);
}

public Optional<ServiceNode<T>> getNode(
final Service service,
final C criteria
){
val optionalFinder = this.getHub().finder(service);
return optionalFinder.map(trcServiceFinder -> trcServiceFinder.get(criteria));
}

public Optional<List<ServiceNode<T>>> getAllNodes(
final Service service,
final C criteria
){
val optionalFinder = this.getHub().finder(service);
return optionalFinder.map(trcServiceFinder -> trcServiceFinder.getAll(criteria));
}

protected abstract ServiceFinderHub<T, C, R> buildHub();

protected abstract ServiceDataSource buildServiceDataSource();

protected abstract ServiceFinderFactory<T,C, R> buildFinderFactory();

}

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

package com.flipkart.ranger.client;

import com.flipkart.ranger.core.model.Criteria;
import com.flipkart.ranger.core.model.Deserializer;
import com.flipkart.ranger.core.model.ServiceNode;

import java.util.List;
import java.util.Optional;

public interface RangerClient<T, C extends Criteria<T>> {

void start();

void stop();

Optional<ServiceNode<T>> getNode();

Optional<ServiceNode<T>> getNode(final C criteria);

Optional<List<ServiceNode<T>>> getAllNodes();

Optional<List<ServiceNode<T>>> getAllNodes(final C criteria);
}
Original file line number Diff line number Diff line change
@@ -0,0 +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.client;

public class RangerClientConstants {

private RangerClientConstants(){}

public static final int CONNECTION_RETRY_TIME = 5000;
public static final int MINIMUM_REFRESH_TIME = 5000;
}
Original file line number Diff line number Diff line change
@@ -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.client;

import com.flipkart.ranger.core.model.Criteria;
import com.flipkart.ranger.core.model.Service;
import com.flipkart.ranger.core.model.ServiceNode;

import java.util.List;
import java.util.Optional;

public interface RangerHubClient<T, C extends Criteria<T>> {

void start();

void stop();

Optional<ServiceNode<T>> getNode(final Service service);

Optional<ServiceNode<T>> getNode(final Service service, final C criteria);

Optional<List<ServiceNode<T>>> getAllNodes(final Service service);

Optional<List<ServiceNode<T>>> getAllNodes(final Service service, final C criteria);

}
Loading

0 comments on commit 7fdeb8f

Please sign in to comment.