You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airavata.apache.org by sm...@apache.org on 2017/12/11 19:56:16 UTC

[airavata] 11/13: Using regular client instead of ThriftClientPool

This is an automated email from the ASF dual-hosted git repository.

smarru pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/airavata.git

commit f51745f19215fafaa81cec95990459c629629dfd
Author: Sachin Kariyattin <sa...@gmail.com>
AuthorDate: Tue Dec 5 14:44:51 2017 -0500

    Using regular client instead of ThriftClientPool
---
 .../handlers/GroupManagerServiceHandler.java       |  48 +++---
 .../service/profile/utils/ThriftClientPool.java    | 176 ---------------------
 2 files changed, 23 insertions(+), 201 deletions(-)

diff --git a/airavata-services/profile-service/profile-service-server/src/main/java/org/apache/airavata/service/profile/handlers/GroupManagerServiceHandler.java b/airavata-services/profile-service/profile-service-server/src/main/java/org/apache/airavata/service/profile/handlers/GroupManagerServiceHandler.java
index 7a57f52..208c59e 100644
--- a/airavata-services/profile-service/profile-service-server/src/main/java/org/apache/airavata/service/profile/handlers/GroupManagerServiceHandler.java
+++ b/airavata-services/profile-service/profile-service-server/src/main/java/org/apache/airavata/service/profile/handlers/GroupManagerServiceHandler.java
@@ -1,5 +1,6 @@
 package org.apache.airavata.service.profile.handlers;
 
+import org.apache.airavata.common.exception.ApplicationSettingsException;
 import org.apache.airavata.common.utils.Constants;
 import org.apache.airavata.common.utils.ServerSettings;
 import org.apache.airavata.model.error.AuthorizationException;
@@ -7,12 +8,12 @@ import org.apache.airavata.model.group.GroupModel;
 import org.apache.airavata.model.security.AuthzToken;
 import org.apache.airavata.service.profile.groupmanager.cpi.GroupManagerService;
 import org.apache.airavata.service.profile.groupmanager.cpi.exception.GroupManagerServiceException;
-import org.apache.airavata.service.profile.utils.ThriftClientPool;
 import org.apache.airavata.service.security.interceptor.SecurityCheck;
+import org.apache.airavata.sharing.registry.client.SharingRegistryServiceClientFactory;
 import org.apache.airavata.sharing.registry.models.GroupType;
+import org.apache.airavata.sharing.registry.models.SharingRegistryException;
 import org.apache.airavata.sharing.registry.models.UserGroup;
 import org.apache.airavata.sharing.registry.service.cpi.SharingRegistryService;
-import org.apache.commons.pool.impl.GenericObjectPool;
 import org.apache.thrift.TException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -24,21 +25,8 @@ public class GroupManagerServiceHandler implements GroupManagerService.Iface {
 
     private static final Logger logger = LoggerFactory.getLogger(GroupManagerServiceHandler.class);
 
-    private ThriftClientPool<SharingRegistryService.Client> sharingClientPool;
-
     public GroupManagerServiceHandler() {
-        GenericObjectPool.Config poolConfig = new GenericObjectPool.Config();
-        poolConfig.maxActive = 100;
-        poolConfig.minIdle = 5;
-        poolConfig.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;
-        poolConfig.testOnBorrow = true;
-        poolConfig.testWhileIdle = true;
-        poolConfig.numTestsPerEvictionRun = 10;
-        poolConfig.maxWait = 3000;
-
-        sharingClientPool = new ThriftClientPool<>(
-                tProtocol -> new SharingRegistryService.Client(tProtocol), poolConfig, ServerSettings.getSharingRegistryHost(),
-                Integer.parseInt(ServerSettings.getSharingRegistryPort()));
+
     }
 
     @Override
@@ -46,7 +34,7 @@ public class GroupManagerServiceHandler implements GroupManagerService.Iface {
     public String createGroup(AuthzToken authzToken, GroupModel groupModel) throws GroupManagerServiceException, AuthorizationException, TException {
         try {
             //TODO Validations for authorization
-            SharingRegistryService.Client sharingClient = sharingClientPool.getResource();
+            SharingRegistryService.Client sharingClient = getSharingRegistryServiceClient();
 
             UserGroup sharingUserGroup = new UserGroup();
             sharingUserGroup.setGroupId(UUID.randomUUID().toString());
@@ -73,7 +61,7 @@ public class GroupManagerServiceHandler implements GroupManagerService.Iface {
     public boolean updateGroup(AuthzToken authzToken, GroupModel groupModel) throws GroupManagerServiceException, AuthorizationException, TException {
         try {
             //TODO Validations for authorization
-            SharingRegistryService.Client sharingClient = sharingClientPool.getResource();
+            SharingRegistryService.Client sharingClient = getSharingRegistryServiceClient();
 
             UserGroup sharingUserGroup = new UserGroup();
             sharingUserGroup.setGroupId(groupModel.getId());
@@ -100,7 +88,7 @@ public class GroupManagerServiceHandler implements GroupManagerService.Iface {
     public boolean deleteGroup(AuthzToken authzToken, String groupId, String ownerId) throws GroupManagerServiceException, AuthorizationException, TException {
         try {
             //TODO Validations for authorization
-            SharingRegistryService.Client sharingClient = sharingClientPool.getResource();
+            SharingRegistryService.Client sharingClient = getSharingRegistryServiceClient();
 
             sharingClient.deleteGroup(authzToken.getClaimsMap().get(Constants.GATEWAY_ID), groupId);
             return true;
@@ -118,7 +106,7 @@ public class GroupManagerServiceHandler implements GroupManagerService.Iface {
     @SecurityCheck
     public GroupModel getGroup(AuthzToken authzToken, String groupId) throws GroupManagerServiceException, AuthorizationException, TException {
         try {
-            SharingRegistryService.Client sharingClient = sharingClientPool.getResource();
+            SharingRegistryService.Client sharingClient = getSharingRegistryServiceClient();
             UserGroup userGroup = sharingClient.getGroup(authzToken.getClaimsMap().get(Constants.GATEWAY_ID), groupId);
 
             GroupModel groupModel = new GroupModel();
@@ -161,7 +149,7 @@ public class GroupManagerServiceHandler implements GroupManagerService.Iface {
     @SecurityCheck
     public boolean transferGroupOwnership(AuthzToken authzToken, String groupId, String newOwnerId) throws GroupManagerServiceException, AuthorizationException, TException {
        try{
-           SharingRegistryService.Client sharingClient = sharingClientPool.getResource();
+           SharingRegistryService.Client sharingClient = getSharingRegistryServiceClient();
            return sharingClient.transferGroupOwnership(authzToken.getClaimsMap().get(Constants.GATEWAY_ID), groupId, newOwnerId);
        }
        catch (Exception e) {
@@ -178,7 +166,7 @@ public class GroupManagerServiceHandler implements GroupManagerService.Iface {
     @SecurityCheck
     public boolean addGroupAdmins(AuthzToken authzToken, String groupId, List<String> adminIds) throws GroupManagerServiceException, AuthorizationException, TException {
         try {
-            SharingRegistryService.Client sharingClient = sharingClientPool.getResource();
+            SharingRegistryService.Client sharingClient = getSharingRegistryServiceClient();
             return sharingClient.addGroupAdmins(authzToken.getClaimsMap().get(Constants.GATEWAY_ID), groupId, adminIds);
         }
         catch (Exception e) {
@@ -194,7 +182,7 @@ public class GroupManagerServiceHandler implements GroupManagerService.Iface {
     @SecurityCheck
     public boolean removeGroupAdmins(AuthzToken authzToken, String groupId, List<String> adminIds) throws GroupManagerServiceException, AuthorizationException, TException {
         try {
-            SharingRegistryService.Client sharingClient = sharingClientPool.getResource();
+            SharingRegistryService.Client sharingClient = getSharingRegistryServiceClient();
             return sharingClient.removeGroupAdmins(authzToken.getClaimsMap().get(Constants.GATEWAY_ID), groupId, adminIds);
         }
         catch (Exception e) {
@@ -210,7 +198,7 @@ public class GroupManagerServiceHandler implements GroupManagerService.Iface {
     @SecurityCheck
     public boolean hasAdminAccess(AuthzToken authzToken, String groupId, String adminId) throws GroupManagerServiceException, AuthorizationException, TException {
         try {
-            SharingRegistryService.Client sharingClient = sharingClientPool.getResource();
+            SharingRegistryService.Client sharingClient = getSharingRegistryServiceClient();
             return sharingClient.hasAdminAccess(authzToken.getClaimsMap().get(Constants.GATEWAY_ID), groupId, adminId);
         }
         catch (Exception e) {
@@ -226,7 +214,7 @@ public class GroupManagerServiceHandler implements GroupManagerService.Iface {
     @SecurityCheck
     public boolean hasOwnerAccess(AuthzToken authzToken, String groupId, String ownerId) throws GroupManagerServiceException, AuthorizationException, TException {
         try {
-            SharingRegistryService.Client sharingClient = sharingClientPool.getResource();
+            SharingRegistryService.Client sharingClient = getSharingRegistryServiceClient();
             return sharingClient.hasOwnerAccess(authzToken.getClaimsMap().get(Constants.GATEWAY_ID), groupId, ownerId);
         }
         catch (Exception e) {
@@ -237,4 +225,14 @@ public class GroupManagerServiceHandler implements GroupManagerService.Iface {
             throw exception;
         }
     }
+
+    private SharingRegistryService.Client getSharingRegistryServiceClient() throws TException, ApplicationSettingsException {
+        final int serverPort = Integer.parseInt(ServerSettings.getSharingRegistryPort());
+        final String serverHost = ServerSettings.getSharingRegistryHost();
+        try {
+            return SharingRegistryServiceClientFactory.createSharingRegistryClient(serverHost, serverPort);
+        } catch (SharingRegistryException e) {
+            throw new TException("Unable to create sharing registry client...", e);
+        }
+    }
 }
diff --git a/airavata-services/profile-service/profile-service-server/src/main/java/org/apache/airavata/service/profile/utils/ThriftClientPool.java b/airavata-services/profile-service/profile-service-server/src/main/java/org/apache/airavata/service/profile/utils/ThriftClientPool.java
deleted file mode 100644
index 1adf9b7..0000000
--- a/airavata-services/profile-service/profile-service-server/src/main/java/org/apache/airavata/service/profile/utils/ThriftClientPool.java
+++ /dev/null
@@ -1,176 +0,0 @@
-/**
- *
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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 org.apache.airavata.service.profile.utils;
-
-import org.apache.commons.pool.BasePoolableObjectFactory;
-import org.apache.commons.pool.impl.GenericObjectPool;
-import org.apache.thrift.TServiceClient;
-import org.apache.thrift.protocol.TBinaryProtocol;
-import org.apache.thrift.protocol.TProtocol;
-import org.apache.thrift.transport.TSocket;
-import org.apache.thrift.transport.TTransport;
-import org.apache.thrift.transport.TTransportException;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-
-public class ThriftClientPool<T extends TServiceClient> implements
-        AutoCloseable {
-
-    private static final Logger LOGGER = LoggerFactory
-            .getLogger(ThriftClientPool.class);
-
-    private final GenericObjectPool internalPool;
-
-    public ThriftClientPool(ClientFactory<T> clientFactory,
-                            GenericObjectPool.Config poolConfig, String host, int port) {
-        this(clientFactory, new BinaryOverSocketProtocolFactory(host, port),
-                poolConfig);
-    }
-
-    public ThriftClientPool(ClientFactory<T> clientFactory,
-                            ProtocolFactory protocolFactory, GenericObjectPool.Config poolConfig) {
-        this.internalPool = new GenericObjectPool(new ThriftClientFactory(
-                clientFactory, protocolFactory), poolConfig);
-    }
-
-    class ThriftClientFactory extends BasePoolableObjectFactory {
-
-        private ClientFactory<T> clientFactory;
-        private ProtocolFactory protocolFactory;
-
-        public ThriftClientFactory(ClientFactory<T> clientFactory,
-                                   ProtocolFactory protocolFactory) {
-            this.clientFactory = clientFactory;
-            this.protocolFactory = protocolFactory;
-        }
-
-        @Override
-        public T makeObject() throws Exception {
-            try {
-                TProtocol protocol = protocolFactory.make();
-                return clientFactory.make(protocol);
-            } catch (Exception e) {
-                LOGGER.warn(e.getMessage(), e);
-                throw new ThriftClientException(
-                        "Can not make a new object for pool", e);
-            }
-        }
-
-        public void destroyObject(T obj) throws Exception {
-            if (obj.getOutputProtocol().getTransport().isOpen()) {
-                obj.getOutputProtocol().getTransport().close();
-            }
-            if (obj.getInputProtocol().getTransport().isOpen()) {
-                obj.getInputProtocol().getTransport().close();
-            }
-        }
-    }
-
-    public static interface ClientFactory<T> {
-
-        T make(TProtocol tProtocol);
-    }
-
-    public static interface ProtocolFactory {
-
-        TProtocol make();
-    }
-
-    public static class BinaryOverSocketProtocolFactory implements
-            ProtocolFactory {
-
-        private String host;
-        private int port;
-
-        public BinaryOverSocketProtocolFactory(String host, int port) {
-            this.host = host;
-            this.port = port;
-        }
-
-        public TProtocol make() {
-            TTransport transport = new TSocket(host, port);
-            try {
-                transport.open();
-            } catch (TTransportException e) {
-                LOGGER.warn(e.getMessage(), e);
-                throw new ThriftClientException("Can not make protocol", e);
-            }
-            return new TBinaryProtocol(transport);
-        }
-    }
-
-    public static class ThriftClientException extends RuntimeException {
-
-        // Fucking Eclipse
-        private static final long serialVersionUID = -2275296727467192665L;
-
-        public ThriftClientException(String message, Exception e) {
-            super(message, e);
-        }
-
-    }
-
-    public T getResource() {
-        try {
-            return (T) internalPool.borrowObject();
-        } catch (Exception e) {
-            throw new ThriftClientException(
-                    "Could not get a resource from the pool", e);
-        }
-    }
-
-    public void returnResourceObject(T resource) {
-        try {
-            internalPool.returnObject(resource);
-        } catch (Exception e) {
-            throw new ThriftClientException(
-                    "Could not return the resource to the pool", e);
-        }
-    }
-
-    public void returnBrokenResource(T resource) {
-        returnBrokenResourceObject(resource);
-    }
-
-    public void returnResource(T resource) {
-        returnResourceObject(resource);
-    }
-
-    protected void returnBrokenResourceObject(T resource) {
-        try {
-            internalPool.invalidateObject(resource);
-        } catch (Exception e) {
-            throw new ThriftClientException(
-                    "Could not return the resource to the pool", e);
-        }
-    }
-
-    public void destroy() {
-        close();
-    }
-
-    public void close() {
-        try {
-            internalPool.close();
-        } catch (Exception e) {
-            throw new ThriftClientException("Could not destroy the pool", e);
-        }
-    }
-}
\ No newline at end of file

-- 
To stop receiving notification emails like this one, please contact
"commits@airavata.apache.org" <co...@airavata.apache.org>.