You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@knox.apache.org by GitBox <gi...@apache.org> on 2020/07/14 14:12:58 UTC

[GitHub] [knox] moresandeep commented on a change in pull request #361: KNOX-2399 - Implemented ZookeeperTokenStateService

moresandeep commented on a change in pull request #361:
URL: https://github.com/apache/knox/pull/361#discussion_r454357167



##########
File path: gateway-server/src/main/java/org/apache/knox/gateway/services/factory/AbstractServiceFactory.java
##########
@@ -37,7 +41,8 @@
 
   private static final GatewayMessages LOG = MessagesFactory.get(GatewayMessages.class);
   private static final String IMPLEMENTATION_PARAM_NAME = "impl";
-  private static final String EMPTY_DEFAULT_IMPLEMENTATION = "";
+  protected static final String EMPTY_DEFAULT_IMPLEMENTATION = "";
+  private final Map<ServiceCacheKey, Service> serviceCache = new HashMap<>(); //to avoid creating the same service with the same implementation more than once

Review comment:
       This code runs during init right then what would be the scenario where there would be multiple implementation of same service?

##########
File path: gateway-server/src/main/java/org/apache/knox/gateway/services/factory/AbstractServiceFactory.java
##########
@@ -47,22 +52,38 @@ public Service create(GatewayServices gatewayServices, ServiceType serviceType,
   @Override
   public Service create(GatewayServices gatewayServices, ServiceType serviceType, GatewayConfig gatewayConfig, Map<String, String> options, String implementation)
       throws ServiceLifecycleException {
-    Service service = null;
-    if (getServiceType() == serviceType) {
-      service = createService(gatewayServices, serviceType, gatewayConfig, options, implementation);
-      if (service == null && StringUtils.isNotBlank(implementation)) {
-        // no known service implementation created, try to create the custom one
-        try {
-          service = Service.class.cast(Class.forName(implementation).newInstance());
-          logServiceUsage(implementation, serviceType);
-        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
-          throw new ServiceLifecycleException("Errror while instantiating " + serviceType.getShortName() + " service implementation " + implementation, e);
+    Service service = fetchFromCache(serviceType, implementation);
+    if (service == null) {
+      if (getServiceType() == serviceType) {
+        service = createService(gatewayServices, serviceType, gatewayConfig, options, implementation);
+        if (service == null && StringUtils.isNotBlank(implementation)) {
+          // no known service implementation created, try to create the custom one
+          try {
+            service = Service.class.cast(Class.forName(implementation).newInstance());

Review comment:
       This looks awkward, this can be changed to `(Service) Class.forName(implementation).newInstance();`

##########
File path: gateway-server/src/main/java/org/apache/knox/gateway/services/factory/AbstractServiceFactory.java
##########
@@ -47,22 +52,38 @@ public Service create(GatewayServices gatewayServices, ServiceType serviceType,
   @Override
   public Service create(GatewayServices gatewayServices, ServiceType serviceType, GatewayConfig gatewayConfig, Map<String, String> options, String implementation)
       throws ServiceLifecycleException {
-    Service service = null;
-    if (getServiceType() == serviceType) {
-      service = createService(gatewayServices, serviceType, gatewayConfig, options, implementation);
-      if (service == null && StringUtils.isNotBlank(implementation)) {
-        // no known service implementation created, try to create the custom one
-        try {
-          service = Service.class.cast(Class.forName(implementation).newInstance());
-          logServiceUsage(implementation, serviceType);
-        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
-          throw new ServiceLifecycleException("Errror while instantiating " + serviceType.getShortName() + " service implementation " + implementation, e);
+    Service service = fetchFromCache(serviceType, implementation);
+    if (service == null) {
+      if (getServiceType() == serviceType) {
+        service = createService(gatewayServices, serviceType, gatewayConfig, options, implementation);
+        if (service == null && StringUtils.isNotBlank(implementation)) {

Review comment:
       I think we should have a fallback option in case `implementation` is missing - a meaningful default perhaps?

##########
File path: gateway-server/src/main/java/org/apache/knox/gateway/services/factory/AbstractServiceFactory.java
##########
@@ -37,7 +41,8 @@
 
   private static final GatewayMessages LOG = MessagesFactory.get(GatewayMessages.class);
   private static final String IMPLEMENTATION_PARAM_NAME = "impl";
-  private static final String EMPTY_DEFAULT_IMPLEMENTATION = "";
+  protected static final String EMPTY_DEFAULT_IMPLEMENTATION = "";
+  private final Map<ServiceCacheKey, Service> serviceCache = new HashMap<>(); //to avoid creating the same service with the same implementation more than once

Review comment:
       Might be better to use `ConcurrentHashMap` just because we are using it for Caching.

##########
File path: gateway-server/src/test/java/org/apache/knox/gateway/services/factory/ServiceFactoryTest.java
##########
@@ -52,7 +52,7 @@
 
   protected final GatewayServices gatewayServices = EasyMock.createNiceMock(GatewayServices.class);
   protected final GatewayConfig gatewayConfig = EasyMock.createNiceMock(GatewayConfig.class);
-  protected final Map<String, String> options = Collections.emptyMap();
+  protected final Map<String, String> options = new HashMap<>();

Review comment:
       Any reason why we need a mutable map?

##########
File path: gateway-server/src/main/java/org/apache/knox/gateway/services/security/impl/ZookeeperRemoteAliasService.java
##########
@@ -190,13 +190,18 @@ private static void checkPathsExist(final RemoteConfigurationRegistryClient remo
      *
      * @param clusterName
      *            cluster name
-     * @return List of all the aliases
+     * @return List of all the aliases (an empty list, in case there is no alias for the given cluster)
      */
     @Override
     public List<String> getAliasesForCluster(final String clusterName) throws AliasServiceException {
-        final List<String> localAliases = localAliasService.getAliasesForCluster(clusterName);
+        final List<String> localAliases = localAliasService == null ? Collections.emptyList() : localAliasService.getAliasesForCluster(clusterName);
         if (localAliases == null || localAliases.isEmpty()) {
-          return remoteClient == null ? new ArrayList<>() : remoteClient.listChildEntries(buildClusterEntryName(clusterName));
+          if (remoteClient != null) {
+            final List<String> remoteAliases = remoteClient.listChildEntries(buildClusterEntryName(clusterName));
+            return remoteAliases == null ? new ArrayList<>() : remoteAliases;

Review comment:
       `Collections.emptyList() ` might be better here too like you did above.

##########
File path: gateway-server/src/main/java/org/apache/knox/gateway/services/factory/AbstractServiceFactory.java
##########
@@ -47,22 +52,38 @@ public Service create(GatewayServices gatewayServices, ServiceType serviceType,
   @Override
   public Service create(GatewayServices gatewayServices, ServiceType serviceType, GatewayConfig gatewayConfig, Map<String, String> options, String implementation)
       throws ServiceLifecycleException {
-    Service service = null;
-    if (getServiceType() == serviceType) {
-      service = createService(gatewayServices, serviceType, gatewayConfig, options, implementation);
-      if (service == null && StringUtils.isNotBlank(implementation)) {
-        // no known service implementation created, try to create the custom one
-        try {
-          service = Service.class.cast(Class.forName(implementation).newInstance());
-          logServiceUsage(implementation, serviceType);
-        } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
-          throw new ServiceLifecycleException("Errror while instantiating " + serviceType.getShortName() + " service implementation " + implementation, e);
+    Service service = fetchFromCache(serviceType, implementation);
+    if (service == null) {
+      if (getServiceType() == serviceType) {
+        service = createService(gatewayServices, serviceType, gatewayConfig, options, implementation);
+        if (service == null && StringUtils.isNotBlank(implementation)) {
+          // no known service implementation created, try to create the custom one
+          try {
+            service = Service.class.cast(Class.forName(implementation).newInstance());
+            logServiceUsage(implementation, serviceType);
+          } catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
+            throw new ServiceLifecycleException("Errror while instantiating " + serviceType.getShortName() + " service implementation " + implementation, e);

Review comment:
       Typo in Error :)

##########
File path: gateway-server/src/main/java/org/apache/knox/gateway/services/factory/AliasServiceFactory.java
##########
@@ -54,7 +57,8 @@ protected Service createService(GatewayServices gatewayServices, ServiceType ser
       } else if (matchesImplementation(implementation, RemoteAliasService.class)) {
         service = new RemoteAliasService(defaultAliasService, getMasterService(gatewayServices));
       } else if (matchesImplementation(implementation, ZookeeperRemoteAliasService.class)) {
-        service = new ZookeeperRemoteAliasServiceProvider().newInstance(defaultAliasService, getMasterService(gatewayServices));
+        final boolean useLocalAliasService = options == null ? true : Boolean.valueOf(options.getOrDefault(USE_LOCAL_ALIAS_IN_ZK_IMPL_PARAM_NAME, "true"));
+        service = new ZookeeperRemoteAliasServiceProvider().newInstance(useLocalAliasService ? defaultAliasService : null, getMasterService(gatewayServices));

Review comment:
       Does this mean the default behavior is to store tokens in ZK?

##########
File path: gateway-server/src/main/java/org/apache/knox/gateway/services/security/impl/ZookeeperRemoteAliasService.java
##########
@@ -190,13 +190,18 @@ private static void checkPathsExist(final RemoteConfigurationRegistryClient remo
      *
      * @param clusterName
      *            cluster name
-     * @return List of all the aliases
+     * @return List of all the aliases (an empty list, in case there is no alias for the given cluster)
      */
     @Override
     public List<String> getAliasesForCluster(final String clusterName) throws AliasServiceException {
-        final List<String> localAliases = localAliasService.getAliasesForCluster(clusterName);
+        final List<String> localAliases = localAliasService == null ? Collections.emptyList() : localAliasService.getAliasesForCluster(clusterName);
         if (localAliases == null || localAliases.isEmpty()) {
-          return remoteClient == null ? new ArrayList<>() : remoteClient.listChildEntries(buildClusterEntryName(clusterName));
+          if (remoteClient != null) {
+            final List<String> remoteAliases = remoteClient.listChildEntries(buildClusterEntryName(clusterName));
+            return remoteAliases == null ? new ArrayList<>() : remoteAliases;
+          } else {
+            return new ArrayList<>();

Review comment:
       `Collections.emptyList() ` might be better here too like you did above. Unless this is mutable list.




----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
users@infra.apache.org