improving deployment policy validation, using Java concurrency

Project: http://git-wip-us.apache.org/repos/asf/incubator-stratos/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-stratos/commit/3a54eff3
Tree: http://git-wip-us.apache.org/repos/asf/incubator-stratos/tree/3a54eff3
Diff: http://git-wip-us.apache.org/repos/asf/incubator-stratos/diff/3a54eff3

Branch: refs/heads/master
Commit: 3a54eff331a3808d7de0a3ffdddde37ec17fa219
Parents: ab73ebd
Author: Nirmal Fernando <[email protected]>
Authored: Mon Mar 31 20:28:31 2014 +0530
Committer: Nirmal Fernando <[email protected]>
Committed: Mon Mar 31 20:28:31 2014 +0530

----------------------------------------------------------------------
 .../concurrent/PartitionValidatorCallable.java  | 88 ++++++++++++++++++++
 .../impl/CloudControllerServiceImpl.java        | 67 ++++++---------
 .../runtime/FasterLookUpDataHolder.java         | 17 +++-
 3 files changed, 128 insertions(+), 44 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/3a54eff3/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/concurrent/PartitionValidatorCallable.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/concurrent/PartitionValidatorCallable.java
 
b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/concurrent/PartitionValidatorCallable.java
new file mode 100644
index 0000000..2ae5274
--- /dev/null
+++ 
b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/concurrent/PartitionValidatorCallable.java
@@ -0,0 +1,88 @@
+/*
+ * 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.stratos.cloud.controller.concurrent;
+
+import java.util.concurrent.Callable;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.cloud.controller.deployment.partition.Partition;
+import 
org.apache.stratos.cloud.controller.exception.InvalidIaasProviderException;
+import org.apache.stratos.cloud.controller.exception.InvalidPartitionException;
+import org.apache.stratos.cloud.controller.interfaces.Iaas;
+import org.apache.stratos.cloud.controller.pojo.Cartridge;
+import org.apache.stratos.cloud.controller.pojo.IaasProvider;
+import org.apache.stratos.cloud.controller.util.CloudControllerUtil;
+import 
org.apache.stratos.cloud.controller.validate.interfaces.PartitionValidator;
+
+public class PartitionValidatorCallable implements Callable<IaasProvider> {
+       
+       private static final Log log = 
LogFactory.getLog(PartitionValidatorCallable.class);
+       private Partition partition;
+       private Cartridge cartridge;
+       
+       public PartitionValidatorCallable(Partition partition, Cartridge 
cartridge) {
+               this.partition = partition;
+               this.cartridge = cartridge;
+       }
+
+       @Override
+       public IaasProvider call() throws Exception {
+               String provider = partition.getProvider();
+        IaasProvider iaasProvider = cartridge.getIaasProvider(provider);
+
+        if (iaasProvider == null) {
+            String msg =
+                         "Invalid Partition - " + partition.toString() +
+                                 ". Cause: Iaas Provider is null for Provider: 
" + provider;
+            log.error(msg);
+            throw new InvalidPartitionException(msg);
+        }
+
+        Iaas iaas = iaasProvider.getIaas();
+        
+        if (iaas == null) {
+            
+            try {
+                iaas = CloudControllerUtil.getIaas(iaasProvider);
+            } catch (InvalidIaasProviderException e) {
+                String msg =
+                        "Invalid Partition - " + partition.toString() +
+                        ". Cause: Unable to build Iaas of this IaasProvider 
[Provider] : " + provider+". "+e.getMessage();
+                log.error(msg, e);
+                throw new InvalidPartitionException(msg, e);
+            }
+            
+        }
+        
+        PartitionValidator validator = iaas.getPartitionValidator();
+        validator.setIaasProvider(iaasProvider);
+        IaasProvider updatedIaasProvider =
+                                           
validator.validate(partition.getId(),
+                                                              
CloudControllerUtil.toJavaUtilProperties(partition.getProperties()));
+        
+        if (log.isDebugEnabled()) {
+               log.debug("Partition "+partition.toString()+ " is validated 
successfully "
+                               + "against the Cartridge: 
"+cartridge.getType());
+        }
+        
+        return updatedIaasProvider;
+       }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/3a54eff3/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/impl/CloudControllerServiceImpl.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/impl/CloudControllerServiceImpl.java
 
b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/impl/CloudControllerServiceImpl.java
index adff085..05d98cb 100644
--- 
a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/impl/CloudControllerServiceImpl.java
+++ 
b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/impl/CloudControllerServiceImpl.java
@@ -20,6 +20,7 @@ package org.apache.stratos.cloud.controller.impl;
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
+import 
org.apache.stratos.cloud.controller.concurrent.PartitionValidatorCallable;
 import org.apache.stratos.cloud.controller.concurrent.ThreadExecutor;
 import org.apache.stratos.cloud.controller.deployment.partition.Partition;
 import org.apache.stratos.cloud.controller.exception.*;
@@ -46,7 +47,9 @@ import 
org.wso2.carbon.registry.core.exceptions.RegistryException;
 
 import java.util.*;
 import java.util.Properties;
+import java.util.concurrent.Callable;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.Future;
 
 import com.google.common.net.InetAddresses;
 
@@ -1032,7 +1035,6 @@ public class CloudControllerServiceImpl implements 
CloudControllerService {
         
        }
 
-               
 
     @Override
     public boolean validateDeploymentPolicy(String cartridgeType, Partition[] 
partitions) 
@@ -1048,48 +1050,27 @@ public class CloudControllerServiceImpl implements 
CloudControllerService {
             log.error(msg);
             throw new InvalidCartridgeTypeException(msg);
         }
-
-        for (Partition partition : partitions) {
-            String provider = partition.getProvider();
-            IaasProvider iaasProvider = cartridge.getIaasProvider(provider);
-
-            if (iaasProvider == null) {
-                String msg =
-                             "Invalid Partition - " + partition.toString() +
-                                     ". Cause: Iaas Provider is null for 
Provider: " + provider;
-                log.error(msg);
-                throw new InvalidPartitionException(msg);
-            }
-
-            Iaas iaas = iaasProvider.getIaas();
-            
-            if (iaas == null) {
-                
-                try {
-                    iaas = CloudControllerUtil.getIaas(iaasProvider);
-                } catch (InvalidIaasProviderException e) {
-                    String msg =
-                            "Invalid Partition - " + partition.toString() +
-                            ". Cause: Unable to build Iaas of this 
IaasProvider [Provider] : " + provider+". "+e.getMessage();
-                    log.error(msg, e);
-                    throw new InvalidPartitionException(msg, e);
-                }
-                
-            }
-            
-            PartitionValidator validator = iaas.getPartitionValidator();
-            validator.setIaasProvider(iaasProvider);
-            IaasProvider updatedIaasProvider =
-                                               
validator.validate(partition.getId(),
-                                                                  
CloudControllerUtil.toJavaUtilProperties(partition.getProperties()));
-            // add to a temporary Map
-            partitionToIaasProviders.put(partition.getId(), 
updatedIaasProvider);
-            
-            if (log.isDebugEnabled()) {
-               log.debug("Partition "+partition.toString()+ " is validated 
successfully "
-                               + "against the Cartridge: "+cartridgeType);
-            }
-
+        
+        Map<String, Future<IaasProvider>> jobList = new HashMap<String, 
Future<IaasProvider>>();
+
+               for (Partition partition : partitions) {
+                       Callable<IaasProvider> worker = new 
PartitionValidatorCallable(
+                                       partition, cartridge);
+                       Future<IaasProvider> job = 
FasterLookUpDataHolder.getInstance()
+                                       .getExecutor().submit(worker);
+                       jobList.put(partition.getId(), job);
+               }
+        
+        // Retrieve the results of the concurrently performed sanity checks.
+        for (String partitionId : jobList.keySet()) {
+               Future<IaasProvider> job = jobList.get(partitionId);
+            try {
+               // add to a temporary Map
+               partitionToIaasProviders.put(partitionId, job.get());
+            } catch (Exception e) {
+                log.error(e.getMessage(), e);
+                throw new InvalidPartitionException(e.getMessage(), e);
+            } 
         }
 
         // if and only if the deployment policy valid

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/3a54eff3/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/runtime/FasterLookUpDataHolder.java
----------------------------------------------------------------------
diff --git 
a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/runtime/FasterLookUpDataHolder.java
 
b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/runtime/FasterLookUpDataHolder.java
index 0c1cb91..970e2c0 100644
--- 
a/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/runtime/FasterLookUpDataHolder.java
+++ 
b/components/org.apache.stratos.cloud.controller/src/main/java/org/apache/stratos/cloud/controller/runtime/FasterLookUpDataHolder.java
@@ -32,6 +32,8 @@ import java.util.Iterator;
 import java.util.List;
 import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.Executors;
 
 /**
  * This object holds all runtime data and provides faster access. This is a 
Singleton class.
@@ -66,6 +68,11 @@ public class FasterLookUpDataHolder implements Serializable{
        private Map<String, ClusterContext> clusterIdToContext = new 
ConcurrentHashMap<String, ClusterContext>();
        
        /**
+     * Thread pool used in this task to execute parallel tasks.
+     */
+    private transient ExecutorService executor = 
Executors.newFixedThreadPool(20);
+       
+       /**
         * List of registered {@link Cartridge}s
         */
        private List<Cartridge> cartridges;
@@ -119,7 +126,7 @@ public class FasterLookUpDataHolder implements Serializable{
        private FasterLookUpDataHolder() {
 
                cartridges = new ArrayList<Cartridge>();
-
+               
        }
 
        public List<Cartridge> getCartridges() {
@@ -359,4 +366,12 @@ public class FasterLookUpDataHolder implements 
Serializable{
         this.clusterIdToContext = clusterIdToContext;
     }
 
+       public ExecutorService getExecutor() {
+               return executor;
+       }
+
+       public void setExecutor(ExecutorService executor) {
+               this.executor = executor;
+       }
+
 }
\ No newline at end of file

Reply via email to