You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@stratos.apache.org by is...@apache.org on 2013/07/05 12:33:07 UTC

[30/34] committing refactoered adc components and top level pom in components

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ca25b1f7/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/clients/CloudControllerStubClient.java
----------------------------------------------------------------------
diff --git a/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/clients/CloudControllerStubClient.java b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/clients/CloudControllerStubClient.java
new file mode 100644
index 0000000..209a830
--- /dev/null
+++ b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/clients/CloudControllerStubClient.java
@@ -0,0 +1,93 @@
+/**
+ *  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.mediator.autoscale.lbautoscale.clients;
+
+import org.apache.axis2.AxisFault;
+import org.apache.stratos.mediator.autoscale.lbautoscale.util.AutoscalerTaskDSHolder;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.wso2.carbon.stratos.cloud.controller.stub.CloudControllerServiceStub;
+
+/**
+ * This is the client class this calls Autoscaler service.
+ */
+public class CloudControllerStubClient extends CloudControllerClient {
+
+    private CloudControllerServiceStub stub;
+    private static final String CLOUD_CONTROLLER_EPR = AutoscalerTaskDSHolder.getInstance().getLoadBalancerConfig().getAutoscalerServiceEpr();
+
+    private static final Log log = LogFactory.getLog(CloudControllerStubClient.class);
+
+    // public CloudControllerStubClient(String epr) throws AxisFault {
+    //
+    // try {
+    //
+    // stub = new CloudControllerServiceStub(epr);
+    // stub._getServiceClient().getOptions().setTimeOutInMilliSeconds(90000);
+    //
+    // } catch (AxisFault axisFault) {
+    // String msg =
+    // "Failed to initiate AutoscalerService client. " + axisFault.getMessage();
+    // log.error(msg, axisFault);
+    // throw new AxisFault(msg, axisFault);
+    // }
+    // }
+
+    // public boolean init(boolean isSpi) throws Exception {
+    //
+    // return stub.initAutoscaler(isSpi);
+    // }
+
+    public String startInstance(String domainName, String subDomainName) throws Exception {
+
+        return stub.startInstance(domainName, subDomainName);
+    }
+
+    public boolean terminateInstance(String domainName, String subDomainName) throws Exception {
+
+        return stub.terminateInstance(domainName, subDomainName);
+    }
+
+    public boolean
+        terminateLastlySpawnedInstance(String domainName, String subDomainName) throws Exception {
+
+        return stub.terminateLastlySpawnedInstance(domainName, subDomainName);
+    }
+
+    public int getPendingInstanceCount(String domainName, String subDomainName) throws Exception {
+
+        return stub.getPendingInstanceCount(domainName, subDomainName);
+    }
+
+    @Override
+    public void init() {
+
+        try {
+
+            stub = new CloudControllerServiceStub(CLOUD_CONTROLLER_EPR);
+            stub._getServiceClient().getOptions().setTimeOutInMilliSeconds(300000);
+
+        } catch (AxisFault axisFault) {
+            String msg = "Failed to initiate AutoscalerService client. " + axisFault.getMessage();
+            log.error(msg, axisFault);
+            throw new RuntimeException(msg, axisFault);
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ca25b1f7/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/context/AppDomainContext.java
----------------------------------------------------------------------
diff --git a/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/context/AppDomainContext.java b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/context/AppDomainContext.java
new file mode 100644
index 0000000..f8155a7
--- /dev/null
+++ b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/context/AppDomainContext.java
@@ -0,0 +1,170 @@
+/**
+ *  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.mediator.autoscale.lbautoscale.context;
+
+import java.util.*;
+import java.util.concurrent.ConcurrentHashMap;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.lb.common.conf.LoadBalancerConfiguration;
+
+
+/**
+ * Contextual information related to autoscaling for a particular clustering domain
+ */
+public class AppDomainContext extends LoadBalancerContext{
+
+    private static final long serialVersionUID = 6582721801663800609L;
+
+    private static final Log log = LogFactory.getLog(AppDomainContext.class);
+
+    /**
+     * Request tokens of requests in flight
+     * <p/>
+     * Key - request token ID, Value - message received time
+     */
+    private Map<String, Long> requestTokens = new ConcurrentHashMap<String, Long>();
+    private List<Integer> requestTokenListLengths;
+    private LoadBalancerConfiguration.ServiceConfiguration serviceConfig;
+
+    public AppDomainContext(LoadBalancerConfiguration.ServiceConfiguration serviceConfig) {
+        this.serviceConfig = serviceConfig;
+        requestTokenListLengths = new Vector<Integer>(serviceConfig.getRoundsToAverage());
+    }
+
+    public LoadBalancerConfiguration.ServiceConfiguration getServiceConfig() {
+        return serviceConfig;
+    }
+
+    /**
+     * If there is insufficient number of messages we cannot make a scaling decision.
+     *
+     * @return true - if a scaling decision can be made
+     */
+    public boolean canMakeScalingDecision() {
+        return requestTokenListLengths.size() >= serviceConfig.getRoundsToAverage();
+    }
+
+    public void addRequestToken(String tokenId) {
+        requestTokens.put(tokenId, System.currentTimeMillis());
+        if (log.isDebugEnabled()) {
+            log.debug("Request Tokens Added : "+requestTokens.size());
+        }
+    }
+
+    public void removeRequestToken(String tokenId) {
+        requestTokens.remove(tokenId);
+    }
+
+//    public int getRunningInstanceCount() {
+//        return super.getRunningInstanceCount();
+//    }
+
+    /**
+     * This will set the running instance count for this app domain
+     * and also will return the difference of current running instance count and previous count.
+     * @param runningInstanceCount current running instance count
+     * @return difference of current running instance count and previous count.
+     */
+//    public int setRunningInstanceCount(int runningInstanceCount) {
+//        int diff = 0;
+//        
+//        if(this.runningInstanceCount < runningInstanceCount){
+//            diff = runningInstanceCount - this.runningInstanceCount;
+//        }
+//        
+//        this.runningInstanceCount = runningInstanceCount;
+//        
+//        return diff;
+//    }
+
+    public void expireRequestTokens() {
+        for (Map.Entry<String, Long> entry : requestTokens.entrySet()) {
+            if (System.currentTimeMillis() - entry.getValue() >= serviceConfig.getMessageExpiryTime()) {
+                requestTokens.remove(entry.getKey());
+                if (log.isDebugEnabled()) {
+                    log.debug("Request Tokens Expired : " + requestTokens.get(entry.getKey()));
+                }
+            }
+        }
+    }
+
+    public void recordRequestTokenListLength() {
+        if (requestTokenListLengths.size() >= serviceConfig.getRoundsToAverage()) {
+            int count = requestTokenListLengths.remove(0);
+            if (log.isDebugEnabled()) {
+                log.debug("Request Tokens Removed : " + count);
+            }
+        }
+        if (log.isDebugEnabled()) {
+            log.debug("Request Tokens Added : " + requestTokens.size());
+        }
+        requestTokenListLengths.add(requestTokens.size());
+    }
+
+
+//    public synchronized int getPendingInstances() {
+//        return pendingInstances;
+//    }
+
+//    public synchronized void incrementPendingInstances() {
+//        this.pendingInstances++;
+//    }
+
+//    public synchronized void decrementPendingInstancesIfNotZero(int diff) {
+//        
+//        while (diff > 0 && this.pendingInstances > 0 ){
+//            this.pendingInstances--;
+//            diff--;
+//        }
+//        
+//    }
+    
+//    public synchronized int getInstances() {
+//        return runningInstanceCount + pendingInstances;
+//    }
+
+    /**
+     * Get the average requests in flight, averaged over the number of  of observations rounds
+     *
+     * @return number of average requests in flight. -1 if there no requests were received
+     */
+    public long getAverageRequestsInFlight() {
+        long total = 0;
+        for (Integer messageQueueLength : requestTokenListLengths) {
+            total += messageQueueLength;
+        }
+        int size = requestTokenListLengths.size();
+        if (size == 0) {
+            return -1; // -1 means that no requests have been received
+        }
+        
+        if (log.isDebugEnabled()) {
+            log.debug("Total Tokens : "+total+ " : Size: "+size);
+        }
+        return (long) total / size;
+    }
+
+
+//    public synchronized void resetRunningPendingInstances() {
+//        pendingInstances = 0;
+//        runningInstanceCount = 0;
+//    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ca25b1f7/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/context/LoadBalancerContext.java
----------------------------------------------------------------------
diff --git a/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/context/LoadBalancerContext.java b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/context/LoadBalancerContext.java
new file mode 100644
index 0000000..b6a6d9b
--- /dev/null
+++ b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/context/LoadBalancerContext.java
@@ -0,0 +1,104 @@
+/**
+ *  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.mediator.autoscale.lbautoscale.context;
+
+import java.io.Serializable;
+
+/**
+ * Contextual information related to autoscaling for a particular domain
+ */
+public class LoadBalancerContext implements Serializable{
+
+    private static final long serialVersionUID = -2022110665957598060L;
+    private int runningInstances;
+    private int pendingInstances;
+    private int terminatingInstances;
+    
+    public synchronized int getTerminatingInstanceCount() {
+        return terminatingInstances;
+    }
+    
+    public synchronized int getRunningInstanceCount() {
+        return runningInstances;
+    }
+    
+    public synchronized int getPendingInstanceCount() {
+        return pendingInstances;
+    }
+
+    /**
+     * This will set the running instance count for a domain
+     * and also will return the difference of current running instance count and previous count.
+     * @param runningInstanceCount current running instance count
+     * @return difference of current running instance count and previous count.
+     */
+    public synchronized int setRunningInstanceCount(int count) {
+        int diff = 0;
+
+        if (this.runningInstances < count) {
+            diff = count - this.runningInstances;
+        }
+
+        this.runningInstances = count;
+
+        return diff;
+    }
+
+    public synchronized int getInstances() {
+        return runningInstances + pendingInstances;
+    }
+    
+    public synchronized void setPendingInstanceCount(int count) {
+        
+        this.pendingInstances = count;
+    }
+    
+    public synchronized void setTerminatingInstanceCount(int count) {
+        
+        this.terminatingInstances = count;
+    }
+
+    public synchronized void incrementPendingInstances(int diff) {
+
+        this.pendingInstances += diff;
+    }
+    
+    public synchronized void incrementTerminatingInstances(int diff) {
+
+        this.terminatingInstances += diff;
+    }
+    
+    public synchronized void decrementPendingInstancesIfNotZero(int diff) {
+
+        while (diff > 0 && this.pendingInstances > 0) {
+            this.pendingInstances--;
+            diff--;
+        }
+
+    }
+    
+    public synchronized void decrementTerminatingInstancesIfNotZero(int diff) {
+
+        while (diff > 0 && this.terminatingInstances > 0) {
+            this.terminatingInstances--;
+            diff--;
+        }
+
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ca25b1f7/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/internal/AutoscalerTaskServiceComponent.java
----------------------------------------------------------------------
diff --git a/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/internal/AutoscalerTaskServiceComponent.java b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/internal/AutoscalerTaskServiceComponent.java
new file mode 100644
index 0000000..06a8f5e
--- /dev/null
+++ b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/internal/AutoscalerTaskServiceComponent.java
@@ -0,0 +1,325 @@
+/**
+ *  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.mediator.autoscale.lbautoscale.internal;
+
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.axis2.description.Parameter;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.lb.common.conf.LoadBalancerConfiguration;
+import org.apache.stratos.lb.common.service.LoadBalancerConfigurationService;
+import org.apache.stratos.mediator.autoscale.lbautoscale.mediators.AutoscaleInMediator;
+import org.apache.stratos.mediator.autoscale.lbautoscale.mediators.AutoscaleOutMediator;
+import org.apache.stratos.mediator.autoscale.lbautoscale.task.*;
+import org.apache.stratos.mediator.autoscale.lbautoscale.util.AutoscalerTaskDSHolder;
+import org.apache.synapse.ManagedLifecycle;
+import org.apache.synapse.Mediator;
+import org.apache.synapse.SynapseConstants;
+import org.apache.synapse.SynapseException;
+import org.apache.synapse.core.SynapseEnvironment;
+import org.apache.synapse.mediators.base.SequenceMediator;
+import org.apache.synapse.mediators.filters.InMediator;
+import org.apache.synapse.mediators.filters.OutMediator;
+import org.apache.synapse.task.Task;
+import org.apache.synapse.task.TaskConstants;
+import org.apache.synapse.task.TaskDescription;
+import org.apache.synapse.task.TaskScheduler;
+import org.apache.synapse.task.service.TaskManagementService;
+import org.osgi.framework.BundleContext;
+import org.osgi.service.component.ComponentContext;
+import org.quartz.JobBuilder;
+import org.quartz.JobDetail;
+import org.wso2.carbon.registry.core.exceptions.RegistryException;
+import org.wso2.carbon.registry.core.service.RegistryService;
+import org.wso2.carbon.stratos.cloud.controller.interfaces.CloudControllerService;
+import org.wso2.carbon.user.core.service.RealmService;
+import org.wso2.carbon.utils.Axis2ConfigurationContextObserver;
+import org.wso2.carbon.utils.ConfigurationContextService;
+
+import java.util.Date;
+import java.util.Map;
+
+/**
+ * @scr.component name="autoscaler.task.component" immediate="true"
+ * @scr.reference name="carbon.core.configurationContextService"
+ * interface="org.wso2.carbon.utils.ConfigurationContextService"
+ * cardinality="1..1" policy="dynamic"
+ * bind="setConfigurationContextService" unbind="unsetConfigurationContextService"
+ * @scr.reference name="user.realmservice.default"
+ * interface="org.wso2.carbon.user.core.service.RealmService"
+ * cardinality="1..1" policy="dynamic" bind="setRealmService"
+ * unbind="unsetRealmService"
+ * @scr.reference name="org.apache.stratos.lb.common"
+ * interface="org.apache.stratos.lb.common.service.LoadBalancerConfigurationService"
+ * cardinality="1..1" policy="dynamic" bind="setLoadBalancerConfigurationService"
+ * unbind="unsetLoadBalancerConfigurationService"
+ * @scr.reference name="registry.service"
+ * interface="org.wso2.carbon.registry.core.service.RegistryService"
+ * cardinality="1..1" policy="dynamic"
+ * bind="setRegistryService" unbind="unsetRegistryService"
+ * @scr.reference name="org.wso2.carbon.stratos.cloud.controller"
+ * interface="org.wso2.carbon.stratos.cloud.controller.interfaces.CloudControllerService"
+ * cardinality="1..1" policy="dynamic" bind="setCloudControllerService"
+ * unbind="unsetCloudControllerService"
+ */
+public class AutoscalerTaskServiceComponent {
+
+    private static final Log log = LogFactory.getLog(AutoscalerTaskServiceComponent.class);
+    private ConfigurationContext configurationContext = null;
+
+    protected void activate(ComponentContext context) {
+    	
+    	try{
+
+        // read config file
+//        String configURL = System.getProperty(AutoscaleConstants.LOAD_BALANCER_CONFIG);
+//        LoadBalancerConfiguration lbConfig = new LoadBalancerConfiguration();
+//        lbConfig.init(configURL);
+        
+        if(configurationContext == null){
+            String msg = "Configuration context is null. Autoscaler task activation failed.";
+            log.fatal(msg);
+            throw new RuntimeException(msg);
+        }
+
+        // load synapse environment
+        Parameter synEnv =
+                configurationContext.getAxisConfiguration().getParameter(SynapseConstants.SYNAPSE_ENV);
+
+        if (synEnv == null || synEnv.getValue() == null ||
+                !(synEnv.getValue() instanceof SynapseEnvironment)) {
+
+            String message = "Unable to initialize the Synapse Configuration : Can not find the ";
+            log.fatal(message + "Synapse Environment");
+            throw new SynapseException(message + "Synapse Environment");
+        }
+
+        SynapseEnvironment synapseEnv = (SynapseEnvironment) synEnv.getValue();
+
+        /** Initializing autoscaleIn and autoscaleOut Mediators.**/
+
+        LoadBalancerConfiguration lbConfig = AutoscalerTaskDSHolder.getInstance().getWholeLoadBalancerConfig();
+        // check whether autoscaling is enabled
+        if (lbConfig.getLoadBalancerConfig().isAutoscaleEnabled()) {
+
+            // get the main sequence mediator
+            SequenceMediator mainSequence =
+                    (SequenceMediator) synapseEnv.getSynapseConfiguration().getSequence("main");
+
+            // iterate through its child mediators
+            for (Mediator child : mainSequence.getList()) {
+
+                // find the InMediator
+                if (child instanceof InMediator) {
+                    InMediator inSequence = (InMediator) child;
+
+                    // if the first child of InMediator isn't an AutoscaleInMediator
+                    if (!(inSequence.getList().get(0) instanceof AutoscaleInMediator)) {
+
+                        // we gonna add it!
+                        inSequence.getList().add(0, new AutoscaleInMediator());
+                        if (log.isDebugEnabled()) {
+                            log.debug("Added Mediator: " + inSequence.getChild(0) + "" +
+                                    " to InMediator. Number of child mediators in InMediator" + " is " +
+                                    inSequence.getList().size() + ".");
+                        }
+                    }
+
+                }
+
+                // find the OutMediator
+                if (child instanceof OutMediator) {
+
+                    OutMediator outSequence = (OutMediator) child;
+
+                    // if the first child of OutMediator isn't an AutoscaleOutMediator
+                    if (!(outSequence.getList().get(0) instanceof AutoscaleOutMediator)) {
+
+                        // we gonna add it!
+                        outSequence.getList().add(0, new AutoscaleOutMediator());
+
+                        if (log.isDebugEnabled()) {
+                            log.debug("Added Mediator: " + outSequence.getChild(0) + "" +
+                                    " to OutMediator. Number of child mediators in OutMediator" +
+                                    " is " + outSequence.getList().size() + ".");
+                        }
+
+                    }
+                }
+            }
+
+            /** Initializing Autoscaler Task **/
+
+            BundleContext bundleContext = context.getBundleContext();
+            if (log.isDebugEnabled()) {
+                log.debug("Initiating Autoscaler task service component");
+            }
+
+            if (bundleContext.getServiceReference(TaskManagementService.class.getName()) != null) {
+                bundleContext.registerService(TaskManagementService.class.getName(),
+                        new AutoscalerTaskMgmtAdminService(), null);
+            }
+
+
+            AutoscalerTaskInitializer listener = new AutoscalerTaskInitializer();
+
+            if (bundleContext.getServiceReference(Axis2ConfigurationContextObserver.class.getName()) != null) {
+                bundleContext.registerService(Axis2ConfigurationContextObserver.class.getName(),
+                        listener, null);
+            }
+
+            if (configurationContext != null) {
+                TaskScheduler scheduler =
+                        (TaskScheduler) configurationContext.getProperty(AutoscalerTaskInitializer.CARBON_TASK_SCHEDULER);
+                if (scheduler == null) {
+                    scheduler = new TaskScheduler(TaskConstants.TASK_SCHEDULER);
+                    scheduler.init(null);
+                    configurationContext.setProperty(AutoscalerTaskInitializer.CARBON_TASK_SCHEDULER,
+                            scheduler);
+                } else if (!scheduler.isInitialized()) {
+                    scheduler.init(null);
+                }
+            }
+            
+            String autoscalerClass = lbConfig.getLoadBalancerConfig().getAutoscalerTaskClass();
+            Task task;
+            if (autoscalerClass != null) {
+                try {
+                    task = (Task) Class.forName(autoscalerClass).newInstance();
+                } catch (Exception e) {
+                    String msg = "Cannot instantiate Autoscaling Task. Class: " + autoscalerClass
+                    		+". It should implement 'org.apache.synapse.task.Task' and "
+                    		+"'org.apache.synapse.ManagedLifecycle' interfaces.";
+                    log.error(msg, e);
+                    throw new RuntimeException(msg, e);
+                }
+            } else {
+                task = new ServiceRequestsInFlightAutoscaler();
+            }
+
+//            ServiceRequestsInFlightAutoscaler autoscalerTask =
+//                    new ServiceRequestsInFlightAutoscaler();
+
+            ((ManagedLifecycle) task).init(synapseEnv);
+
+            // specify scheduler task details
+            JobBuilder jobBuilder = JobBuilder.newJob(AutoscalingJob.class)
+                .withIdentity("autoscalerJob");
+            JobDetail job = jobBuilder.build();
+
+            Map<String, Object> dataMap = job.getJobDataMap();
+            dataMap.put(AutoscalingJob.AUTOSCALER_TASK, task);
+            dataMap.put(AutoscalingJob.SYNAPSE_ENVI, synapseEnv);
+
+            final TaskDescription taskDescription = new TaskDescription();
+            taskDescription.setTaskClass(ServiceRequestsInFlightAutoscaler.class.getName());
+            taskDescription.setName("autoscaler");
+            //taskDescription.setCount(SimpleTrigger.REPEAT_INDEFINITELY);
+
+            int interval = AutoscalerTaskDSHolder.getInstance().getLoadBalancerConfig().getAutoscalerTaskInterval();
+            taskDescription.setInterval(interval);
+            taskDescription.setStartTime(new Date(System.currentTimeMillis() + (interval*2)));
+
+            TaskSchedulingManager scheduler = new TaskSchedulingManager();
+            scheduler.scheduleTask(taskDescription, dataMap, configurationContext);
+
+
+        } else {
+
+            log.info("Autoscaling is disabled.");
+        }
+    	} catch (Throwable e) {
+            log.error("Failed to activate Autoscaler Task Service Component. ", e);
+        }
+    }
+
+
+    protected void deactivate(ComponentContext ctx) {
+        AutoscalerTaskDSHolder.getInstance().setConfigurationContextService(null);
+        if (log.isDebugEnabled()) {
+            log.debug("Autoscaler task bundle is deactivated");
+        }
+    }
+
+	protected void setCloudControllerService(CloudControllerService cc) {
+        AutoscalerTaskDSHolder.getInstance().setCloudControllerService(cc);
+    }
+    
+    protected void unsetCloudControllerService(CloudControllerService cc) {
+        AutoscalerTaskDSHolder.getInstance().setCloudControllerService(null);
+    }
+
+    protected void setConfigurationContextService(ConfigurationContextService context) {
+        if (log.isDebugEnabled()) {
+            log.debug("ConfigurationContextService bound to the Autoscaler task initialization process");
+        }
+        this.configurationContext = context.getServerConfigContext();
+        AutoscalerTaskDSHolder.getInstance().setConfigurationContextService(context);
+    }
+
+    protected void unsetConfigurationContextService(ConfigurationContextService configurationContextService) {
+        if (log.isDebugEnabled()) {
+            log.debug("ConfigurationContextService unbound from the Autoscaler task");
+        }
+        this.configurationContext = null;
+        AutoscalerTaskDSHolder.getInstance().setConfigurationContextService(null);
+    }
+
+    protected void setRealmService(RealmService realmService) {
+        if (log.isDebugEnabled()) {
+            log.debug("Bound realm service from the Autoscaler task");
+        }
+        AutoscalerTaskDSHolder.getInstance().setRealmService(realmService);
+    }
+
+    protected void unsetRealmService(RealmService realmService) {
+        if (log.isDebugEnabled()) {
+            log.debug("Unbound realm service from the Autoscaler task");
+        }
+        AutoscalerTaskDSHolder.getInstance().setRealmService(null);
+    }
+    
+    protected void setLoadBalancerConfigurationService(LoadBalancerConfigurationService lbConfigSer){
+        AutoscalerTaskDSHolder.getInstance().setLbConfigService(lbConfigSer);
+    }
+    
+    protected void unsetLoadBalancerConfigurationService(LoadBalancerConfigurationService lbConfigSer){
+        AutoscalerTaskDSHolder.getInstance().setLbConfigService(null);
+    }
+    
+    protected void setRegistryService(RegistryService regService) {
+        if (log.isDebugEnabled()) {
+            log.debug("RegistryService bound to the endpoint component");
+        }
+        try {
+            AutoscalerTaskDSHolder.getInstance().setConfigRegistry(regService.getConfigSystemRegistry());
+            AutoscalerTaskDSHolder.getInstance().setGovernanceRegistry(regService.getGovernanceSystemRegistry());
+        } catch (RegistryException e) {
+            log.error("Couldn't retrieve the registry from the registry service");
+        }
+    }
+
+    protected void unsetRegistryService(RegistryService regService) {
+        if (log.isDebugEnabled()) {
+            log.debug("RegistryService unbound from the endpoint component");
+        }
+        AutoscalerTaskDSHolder.getInstance().setConfigRegistry(null);
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ca25b1f7/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/internal/RegistryManager.java
----------------------------------------------------------------------
diff --git a/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/internal/RegistryManager.java b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/internal/RegistryManager.java
new file mode 100644
index 0000000..0f5a03d
--- /dev/null
+++ b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/internal/RegistryManager.java
@@ -0,0 +1,54 @@
+/**
+ *  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.mediator.autoscale.lbautoscale.internal;
+
+import org.apache.stratos.mediator.autoscale.lbautoscale.util.AutoscalerTaskDSHolder;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.stratos.lb.common.util.DomainMapping;
+import org.wso2.carbon.registry.core.Resource;
+import org.wso2.carbon.registry.core.exceptions.RegistryException;
+import org.wso2.carbon.registry.core.session.UserRegistry;
+
+public class RegistryManager {
+    UserRegistry governanceRegistry = AutoscalerTaskDSHolder.getInstance().getGovernanceRegistry();
+    private static final Log log = LogFactory.getLog(RegistryManager.class);
+    /**
+     *
+     */
+    private Resource resource = null;
+    public static final String HOST_INFO = "hostinfo/";
+    public static final String ACTUAL_HOST = "actual.host";
+
+    public DomainMapping getMapping(String hostName) {
+        DomainMapping domainMapping;
+        try {
+            if (governanceRegistry.resourceExists(HOST_INFO + hostName)) {
+                resource = governanceRegistry.get(HOST_INFO + hostName);
+                domainMapping = new DomainMapping(hostName);
+                domainMapping.setActualHost(resource.getProperty(ACTUAL_HOST));
+                return domainMapping;
+            }
+        } catch (RegistryException e) {
+            log.info("Error while getting registry resource");
+            throw new RuntimeException(e);
+        }
+        return null;
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ca25b1f7/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/mediators/AutoscaleInMediator.java
----------------------------------------------------------------------
diff --git a/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/mediators/AutoscaleInMediator.java b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/mediators/AutoscaleInMediator.java
new file mode 100644
index 0000000..6ec1da3
--- /dev/null
+++ b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/mediators/AutoscaleInMediator.java
@@ -0,0 +1,174 @@
+/**
+ *  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.mediator.autoscale.lbautoscale.mediators;
+
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.stratos.mediator.autoscale.lbautoscale.context.AppDomainContext;
+import org.apache.stratos.mediator.autoscale.lbautoscale.internal.RegistryManager;
+import org.apache.stratos.mediator.autoscale.lbautoscale.util.AutoscaleConstants;
+import org.apache.stratos.mediator.autoscale.lbautoscale.util.AutoscaleUtil;
+import org.apache.stratos.mediator.autoscale.lbautoscale.util.AutoscalerTaskDSHolder;
+import org.apache.http.protocol.HTTP;
+import org.apache.synapse.ManagedLifecycle;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.core.SynapseEnvironment;
+import org.apache.synapse.core.axis2.Axis2MessageContext;
+import org.apache.synapse.mediators.AbstractMediator;
+import org.apache.stratos.lb.common.conf.LoadBalancerConfiguration;
+import org.apache.stratos.lb.common.conf.util.HostContext;
+import org.apache.stratos.lb.common.cache.URLMappingCache;
+import org.apache.stratos.lb.common.util.DomainMapping;
+
+import java.util.Map;
+
+/**
+ * This Synapse mediator generates a token per request received. These tokens are used for tracking
+ * the number of requests in flight. Once a response is received, the relevant token will be removed
+ * by the {@link AutoscaleOutMediator}
+ *
+ * @see AutoscaleOutMediator
+ */
+public class AutoscaleInMediator extends AbstractMediator implements ManagedLifecycle {
+
+    private LoadBalancerConfiguration lbConfig;
+    private Map<String, HostContext> hostCtxts;
+    /**
+     * keep the size of cache which used to keep hostNames of url mapping.
+     */
+    private URLMappingCache mappingCache;
+    private RegistryManager registryManager;
+    private int sizeOfCache;
+
+    public AutoscaleInMediator() {
+
+        this.lbConfig = AutoscalerTaskDSHolder.getInstance().getWholeLoadBalancerConfig();
+        hostCtxts = lbConfig.getHostContextMap();
+        sizeOfCache = lbConfig.getLoadBalancerConfig().getSizeOfCache();
+        mappingCache = URLMappingCache.getInstance(sizeOfCache);
+    }
+
+    public boolean mediate(MessageContext synCtx) {
+
+        if (log.isDebugEnabled()) {
+            log.debug("Mediation started .......... " + AutoscaleInMediator.class.getName());
+
+        }
+
+        ConfigurationContext configCtx =
+                                         ((Axis2MessageContext) synCtx).getAxis2MessageContext()
+                                                                       .getConfigurationContext();
+        String uuid = org.apache.axiom.util.UIDGenerator.generateUID();
+        synCtx.setProperty(AutoscaleConstants.REQUEST_ID, uuid);
+
+        Map<String, Map<String, ?>> appDomainContexts =
+                                                                       AutoscaleUtil.getAppDomainContexts(configCtx,
+                                                                               lbConfig);
+        org.apache.axis2.context.MessageContext axis2MessageContext =
+                ((Axis2MessageContext) synCtx).getAxis2MessageContext();
+        Map<String, String> transportHeaders = (Map<String, String>) axis2MessageContext.
+                getProperty(org.apache.axis2.context.MessageContext.TRANSPORT_HEADERS);
+        String targetHost = transportHeaders.get(HTTP.TARGET_HOST);
+        
+        String toAddress = synCtx.getTo().getAddress();
+        if (targetHost.contains(":")) {
+            targetHost = targetHost.substring(0, targetHost.indexOf(':'));
+        }
+
+        int tenantId = AutoscaleUtil.getTenantId(synCtx.getTo().toString());
+
+        String domain = null, subDomain = null;
+        
+        log.debug("************ Target Host: "+targetHost + " -- Tenant id : "+tenantId);
+
+        HostContext ctxt = hostCtxts.get(targetHost);
+        
+        if (ctxt == null) {
+
+            DomainMapping domainMapping = mappingCache.getMapping(targetHost);
+            if (domainMapping == null) {
+                registryManager = new RegistryManager();
+                domainMapping = registryManager.getMapping(targetHost);
+                mappingCache.addValidMapping(targetHost, domainMapping);
+            }
+            if (domainMapping != null) {
+
+                String actualHost = domainMapping.getActualHost();
+
+                // get the HostContext from the actual host name in the case of domain 
+                // mapping.
+                ctxt = hostCtxts.get(actualHost);
+
+            }
+        }
+
+        if (ctxt == null) {
+            log.debug("Host Context is null.");
+        	// we don't need to do anything 
+        	return true;
+        }
+
+
+        // gets the corresponding domain
+        domain = ctxt.getDomainFromTenantId(tenantId);
+        synCtx.setProperty(AutoscaleConstants.TARGET_DOMAIN, domain);
+
+        // gets the corresponding sub domain
+        subDomain = ctxt.getSubDomainFromTenantId(tenantId);
+        synCtx.setProperty(AutoscaleConstants.TARGET_SUB_DOMAIN, subDomain);
+
+        if (appDomainContexts.get(domain) == null) {
+            // if we do not find a correct context, we just ignore
+            log.debug("AppDomainContext not found for domain " + domain);
+
+        } else {
+            AppDomainContext appDomainContext = (AppDomainContext) appDomainContexts.get(domain).get(subDomain);
+
+            if (appDomainContext != null) {
+                appDomainContext.addRequestToken(uuid);
+                System.setProperty(AutoscaleConstants.IS_TOUCHED, "true");
+
+            } else {
+                // if we do not find a correct context, we just ignore
+                log.debug("AppDomainContext not found for sub domain: " + subDomain +
+                          " of domain: " + domain);
+            }
+        }
+
+        return true;
+    }
+
+    @Override
+    public void destroy() {
+
+        log.info("Mediator destroyed! " + AutoscaleInMediator.class.getName());
+    }
+
+    @Override
+    public void init(SynapseEnvironment arg0) {
+
+        if (log.isDebugEnabled()) {
+            log.debug("Mediator initialized! " + AutoscaleInMediator.class.getName());
+        }
+    }
+    
+    private void throwException(String msg){
+        log.error(msg);
+        throw new RuntimeException(msg);
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ca25b1f7/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/mediators/AutoscaleOutMediator.java
----------------------------------------------------------------------
diff --git a/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/mediators/AutoscaleOutMediator.java b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/mediators/AutoscaleOutMediator.java
new file mode 100644
index 0000000..3640c08
--- /dev/null
+++ b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/mediators/AutoscaleOutMediator.java
@@ -0,0 +1,78 @@
+/**
+ *  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.mediator.autoscale.lbautoscale.mediators;
+
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.stratos.mediator.autoscale.lbautoscale.util.AutoscaleConstants;
+import org.apache.synapse.ManagedLifecycle;
+import org.apache.synapse.MessageContext;
+import org.apache.synapse.core.SynapseEnvironment;
+import org.apache.synapse.core.axis2.Axis2MessageContext;
+import org.apache.synapse.mediators.AbstractMediator;
+import org.apache.stratos.mediator.autoscale.lbautoscale.context.AppDomainContext;
+
+import java.util.Map;
+
+/**
+ * This Synapse mediator removes the request token generated by the {@link AutoscaleInMediator}
+ * 
+ * @see AutoscaleInMediator
+ */
+public class AutoscaleOutMediator extends AbstractMediator implements ManagedLifecycle {
+
+    public boolean mediate(MessageContext synCtx) {
+
+        log.debug("Mediation started ....... " + AutoscaleOutMediator.class.getName());
+
+        ConfigurationContext configCtx =
+            ((Axis2MessageContext) synCtx).getAxis2MessageContext().getConfigurationContext();
+
+        String domain = (String) synCtx.getProperty(AutoscaleConstants.TARGET_DOMAIN);
+        // gets the corresponding sub domain
+        String subDomain = (String) synCtx.getProperty(AutoscaleConstants.TARGET_SUB_DOMAIN);
+        
+        String tokenId = (String) synCtx.getProperty(AutoscaleConstants.REQUEST_ID);
+        @SuppressWarnings("unchecked")
+        Map<String, Map<String, AppDomainContext>> appDomainContexts =
+            (Map<String, Map<String, AppDomainContext>>) configCtx.getPropertyNonReplicable(AutoscaleConstants.APP_DOMAIN_CONTEXTS);
+        AppDomainContext appDomainContext = null ;
+        
+        if(appDomainContexts.get(domain) != null){
+        	appDomainContext = appDomainContexts.get(domain).get(subDomain);
+        }
+        
+        if (appDomainContext != null) {
+            appDomainContext.removeRequestToken(tokenId);
+            System.setProperty(AutoscaleConstants.IS_TOUCHED, "true");
+        } else {
+            log.debug("AppDomainContext not found for domain " + domain+" and sub domain : "+subDomain);
+        }
+        return true;
+    }
+
+    @Override
+    public void destroy() {
+        log.debug("Autoscale out mediator destroyed...");
+    }
+
+    @Override
+    public void init(SynapseEnvironment arg0) {
+        log.debug("Autoscale out mediator started...");
+    }
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ca25b1f7/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/state/check/PendingInstancesStateChecker.java
----------------------------------------------------------------------
diff --git a/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/state/check/PendingInstancesStateChecker.java b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/state/check/PendingInstancesStateChecker.java
new file mode 100644
index 0000000..3cf1c85
--- /dev/null
+++ b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/state/check/PendingInstancesStateChecker.java
@@ -0,0 +1,148 @@
+/**
+ *  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.mediator.autoscale.lbautoscale.state.check;
+
+import org.apache.axis2.clustering.management.GroupManagementAgent;
+import org.apache.stratos.mediator.autoscale.lbautoscale.clients.CloudControllerClient;
+import org.apache.stratos.mediator.autoscale.lbautoscale.context.LoadBalancerContext;
+import org.apache.stratos.mediator.autoscale.lbautoscale.util.AutoscaleConstants;
+import org.apache.stratos.mediator.autoscale.lbautoscale.util.AutoscaleUtil;
+import org.apache.stratos.mediator.autoscale.lbautoscale.util.AutoscalerTaskDSHolder;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class PendingInstancesStateChecker implements Runnable {
+    
+    private static final Log log = LogFactory.getLog(PendingInstancesStateChecker.class);
+    private LoadBalancerContext groupCtxt = null;
+    private String domain;
+    private String subDomain;
+    private int expectedIncrementOfinstanceCount, originalRunningInstanceCount;
+    private CloudControllerClient ccClient;
+    
+    public PendingInstancesStateChecker(LoadBalancerContext ctxt, String aDomain, String aSubDomain,
+            int anexpectedInstanceCount, int currentCount, CloudControllerClient client) {
+        groupCtxt = ctxt;
+        domain = aDomain;
+        subDomain = aSubDomain;
+        expectedIncrementOfinstanceCount = anexpectedInstanceCount;
+        originalRunningInstanceCount = currentCount;
+        ccClient = client;
+    }
+
+    @Override
+    public void run() {
+
+        if (groupCtxt != null) {
+
+            int totalWaitedTime = 0;
+            int serverStartupDelay = AutoscalerTaskDSHolder
+                .getInstance()
+                .getWholeLoadBalancerConfig()
+                .getLoadBalancerConfig()
+                .getServerStartupDelay();
+
+            log.debug("Pending Instances State Checker has started for: " +
+                AutoscaleUtil.domainSubDomainString(domain, subDomain) + ". Check expiry time : " + serverStartupDelay);
+
+            // for each sub domain, get the clustering group management agent
+            GroupManagementAgent agent =
+                AutoscalerTaskDSHolder.getInstance().getAgent()
+                    .getGroupManagementAgent(domain,
+                        subDomain);
+            int startingRunningInstanceCount = agent.getMembers().size();
+
+            // we give some time for the server to get joined, we'll check time to time
+            // whether the instance has actually joined the ELB.
+            while ((agent.getMembers().size() < (originalRunningInstanceCount + expectedIncrementOfinstanceCount)) &&
+                totalWaitedTime < serverStartupDelay) {
+                int upToDateRunningInstanceCount = agent.getMembers().size();
+
+                log.debug(AutoscaleUtil.domainSubDomainString(domain, subDomain) +
+                    " - Number of current running instances " +
+                        upToDateRunningInstanceCount);
+
+                if (upToDateRunningInstanceCount > startingRunningInstanceCount) {
+                    int newlyJoinedInstanceCount = upToDateRunningInstanceCount - startingRunningInstanceCount;
+                    // set new running instance count
+                    groupCtxt.setRunningInstanceCount(upToDateRunningInstanceCount);
+                    // decrement the pending instance count
+                    groupCtxt.decrementPendingInstancesIfNotZero(newlyJoinedInstanceCount);
+                    // update the starting running instance count
+                    startingRunningInstanceCount = upToDateRunningInstanceCount;
+
+                    log.debug(AutoscaleUtil.domainSubDomainString(domain, subDomain) +
+                        " - Instances newly joined: " +
+                            newlyJoinedInstanceCount);
+                }
+
+                try {
+                    Thread.sleep(AutoscaleConstants.INSTANCE_REMOVAL_CHECK_TIME);
+                } catch (InterruptedException ignore) {
+                }
+
+                totalWaitedTime += AutoscaleConstants.INSTANCE_REMOVAL_CHECK_TIME;
+            }
+
+            log.debug(AutoscaleUtil.domainSubDomainString(domain, subDomain) + "- Waited for : " +
+                totalWaitedTime +
+                " (milliseconds) till pending members get joined.");
+
+            if (agent.getMembers().size() < (originalRunningInstanceCount + expectedIncrementOfinstanceCount)) {
+
+                int instanceCountFailedToJoin =
+                    originalRunningInstanceCount + expectedIncrementOfinstanceCount - agent.getMembers().size();
+                log.debug(AutoscaleUtil.domainSubDomainString(domain, subDomain) +
+                    "Instances that are failed to join: " +
+                        instanceCountFailedToJoin);
+
+                // to avoid an infinite loop
+                int retries = instanceCountFailedToJoin + 2;
+
+                while (instanceCountFailedToJoin > 0 && retries > 0) {
+                    // instances spawned haven't joined ELB, so we assume that instance is
+                    // corrupted.
+                    // hence, we ask CC to terminate it.
+                    try {
+                        log.debug("Terminating lastly spwaned instance of " +
+                            AutoscaleUtil.domainSubDomainString(domain, subDomain));
+                        ccClient.terminateLastlySpawnedInstance(domain, subDomain);
+                        instanceCountFailedToJoin--;
+                        // decrement pending count
+                        groupCtxt.decrementPendingInstancesIfNotZero(1);
+                    } catch (Exception e) {
+                        log
+                            .error(
+                                "Instance termination failed for " +
+                                    AutoscaleUtil.domainSubDomainString(domain, subDomain),
+                                e);
+                    } finally {
+                        retries--;
+                    }
+                }
+
+                // decrement pending count
+                groupCtxt.decrementPendingInstancesIfNotZero(instanceCountFailedToJoin);
+            }
+
+        }
+
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ca25b1f7/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/state/check/TerminatingInstancesStateChecker.java
----------------------------------------------------------------------
diff --git a/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/state/check/TerminatingInstancesStateChecker.java b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/state/check/TerminatingInstancesStateChecker.java
new file mode 100644
index 0000000..d135d22
--- /dev/null
+++ b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/state/check/TerminatingInstancesStateChecker.java
@@ -0,0 +1,89 @@
+/**
+ *  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.mediator.autoscale.lbautoscale.state.check;
+
+import org.apache.axis2.clustering.management.GroupManagementAgent;
+import org.apache.stratos.mediator.autoscale.lbautoscale.context.LoadBalancerContext;
+import org.apache.stratos.mediator.autoscale.lbautoscale.util.AutoscaleConstants;
+import org.apache.stratos.mediator.autoscale.lbautoscale.util.AutoscaleUtil;
+import org.apache.stratos.mediator.autoscale.lbautoscale.util.AutoscalerTaskDSHolder;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+public class TerminatingInstancesStateChecker implements Runnable {
+    
+    private static final Log log = LogFactory.getLog(TerminatingInstancesStateChecker.class);
+    private LoadBalancerContext groupCtxt = null;
+    private String domain;
+    private String subDomain;
+    
+    public TerminatingInstancesStateChecker(LoadBalancerContext ctxt, String aDomain, String aSubDomain) {
+        groupCtxt = ctxt;
+        domain = aDomain;
+        subDomain = aSubDomain;
+    }
+
+    @Override
+    public void run() {
+
+        if (groupCtxt != null) {
+
+            groupCtxt.incrementTerminatingInstances(1);
+            
+            int totalWaitedTime = 0;
+            int serverStartupDelay = AutoscalerTaskDSHolder
+                .getInstance()
+                .getWholeLoadBalancerConfig()
+                .getLoadBalancerConfig()
+                .getServerStartupDelay();
+            log.info("Terminating Instances State Checker has started for: " +
+                AutoscaleUtil.domainSubDomainString(domain, subDomain) + ". Check expiry time : " + serverStartupDelay);
+
+            // for each sub domain, get the clustering group management agent
+            GroupManagementAgent agent =
+                AutoscalerTaskDSHolder.getInstance().getAgent()
+                    .getGroupManagementAgent(domain,
+                        subDomain);
+
+            int startingRunningInstanceCount = agent.getMembers().size();
+
+            // we give some time for the server to be terminated, we'll check time to time
+            // whether the instance has actually left the cluster.
+            while (agent.getMembers().size() == startingRunningInstanceCount &&
+                totalWaitedTime < serverStartupDelay) {
+
+                try {
+                    Thread.sleep(AutoscaleConstants.INSTANCE_REMOVAL_CHECK_TIME);
+                } catch (InterruptedException ignore) {
+                }
+
+                totalWaitedTime += AutoscaleConstants.INSTANCE_REMOVAL_CHECK_TIME;
+            }
+
+            log.debug(AutoscaleUtil.domainSubDomainString(domain, subDomain) + "- Waited for : " +
+                totalWaitedTime +
+                " (milliseconds) till terminating member left the cluster.");
+
+            // we recalculate number of alive instances
+            groupCtxt.decrementTerminatingInstancesIfNotZero(1);
+        }
+
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ca25b1f7/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/task/AutoscalerTaskInitializer.java
----------------------------------------------------------------------
diff --git a/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/task/AutoscalerTaskInitializer.java b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/task/AutoscalerTaskInitializer.java
new file mode 100644
index 0000000..a38fecd
--- /dev/null
+++ b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/task/AutoscalerTaskInitializer.java
@@ -0,0 +1,50 @@
+/**
+ *  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.mediator.autoscale.lbautoscale.task;
+
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.synapse.task.TaskConstants;
+import org.apache.synapse.task.TaskDescriptionRepository;
+import org.apache.synapse.task.TaskScheduler;
+import org.wso2.carbon.utils.AbstractAxis2ConfigurationContextObserver;
+
+public class AutoscalerTaskInitializer extends AbstractAxis2ConfigurationContextObserver {
+
+    public static final java.lang.String CARBON_TASK_SCHEDULER = "CARBON_TASK_SCHEDULER";
+    public static final java.lang.String CARBON_TASK_REPOSITORY = "CARBON_TASK_REPOSITORY";
+
+    public void createdConfigurationContext(ConfigurationContext configContext) {
+        TaskScheduler scheduler = (TaskScheduler)configContext.getProperty(AutoscalerTaskInitializer.CARBON_TASK_SCHEDULER);
+        if (scheduler == null) {
+            scheduler = new TaskScheduler(TaskConstants.TASK_SCHEDULER);
+            scheduler.init(null);
+            configContext.setProperty(AutoscalerTaskInitializer.CARBON_TASK_SCHEDULER, scheduler);
+        } else if(!scheduler.isInitialized()) {
+            scheduler.init(null);
+        }
+
+        if (configContext.getProperty(AutoscalerTaskInitializer.CARBON_TASK_REPOSITORY) == null) {
+            TaskDescriptionRepository repository = new TaskDescriptionRepository();
+            configContext.setProperty(
+                    AutoscalerTaskInitializer.CARBON_TASK_REPOSITORY, repository);
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ca25b1f7/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/task/AutoscalerTaskMgmtAdminService.java
----------------------------------------------------------------------
diff --git a/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/task/AutoscalerTaskMgmtAdminService.java b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/task/AutoscalerTaskMgmtAdminService.java
new file mode 100644
index 0000000..5f83f7e
--- /dev/null
+++ b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/task/AutoscalerTaskMgmtAdminService.java
@@ -0,0 +1,348 @@
+/**
+ *  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.mediator.autoscale.lbautoscale.task;
+
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.util.AXIOMUtil;
+import org.apache.axis2.AxisFault;
+import org.apache.axis2.context.ConfigurationContext;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.task.TaskDescription;
+import org.apache.synapse.task.TaskDescriptionFactory;
+import org.apache.synapse.task.TaskDescriptionSerializer;
+import org.apache.synapse.task.service.TaskManagementService;
+import org.wso2.carbon.core.AbstractAdmin;
+import org.wso2.carbon.task.TaskManagementException;
+import org.wso2.carbon.task.TaskManager;
+
+import javax.xml.namespace.QName;
+import javax.xml.stream.XMLStreamException;
+import java.util.*;
+
+public class AutoscalerTaskMgmtAdminService extends AbstractAdmin implements TaskManagementService{
+
+    private static final Log log = LogFactory.getLog(AutoscalerTaskMgmtAdminService.class);
+
+    private TaskSchedulingManager schedulingManager;
+
+
+    private static final String TASK_EXTENSION_NS =
+            "http://www.wso2.org/tasks";
+
+    private static final OMFactory FACTORY = OMAbstractFactory.getOMFactory();
+
+    private static final OMNamespace TASK_OM_NAMESPACE = FACTORY.createOMNamespace(
+            TASK_EXTENSION_NS, "task");
+
+    public AutoscalerTaskMgmtAdminService(){}
+    
+    public AutoscalerTaskMgmtAdminService(ConfigurationContext configurationContext) {
+        this.schedulingManager = TaskSchedulingManager.getInstance();
+        this.configurationContext = configurationContext;
+    }
+
+    public void shutdown() {
+        schedulingManager.shutDown(getConfigContext());
+    }
+
+    public void addTaskDescription(TaskDescription taskDescription, Map<String, Object> resources)
+            throws Exception {
+        if (log.isDebugEnabled()) {
+            log.debug("Adding the Task " + taskDescription.getName());
+        }
+        try {
+            schedulingManager.scheduleTask(taskDescription, resources, getConfigContext());
+        } catch (Exception e) {
+            String msg = "Cannot add the task " + taskDescription.getName() + "..";
+            log.error(msg, e);  
+            throw e;
+        }
+    }
+
+    /**
+     * Passing the Task Description to scheduling manager to actually add it to the Task
+     * Description Repository.
+     *
+     * @param taskDescription TaskDescription
+     */
+    public void addTaskDescription(TaskDescription taskDescription) {
+        if (log.isDebugEnabled()) {
+            log.debug("Adding the Task " + taskDescription.getName());
+        }
+        try {
+            Map<String, Object> resources = new HashMap<String, Object>();
+            schedulingManager.scheduleTask(taskDescription, resources, getConfigContext());
+        } catch (Exception e) {
+            String msg = "Cannot add the task";
+            log.error(msg, e);
+        }
+    }
+
+    public void addTaskDescriptionInAnOmElement(String taskElementString) throws
+                                                                          TaskManagementException,
+                                                                          XMLStreamException {
+        OMElement taskElement = AXIOMUtil.stringToOM(taskElementString);
+        if (log.isDebugEnabled()) {
+            log.debug("Add TaskDescription - Get a Task configuration  :" + taskElement);
+        }
+        TaskDescription taskDescription = validateAndCreate(taskElement);
+
+        try {
+            addTaskDescription(taskDescription);
+        } catch (Exception e) {
+            try {
+                getTaskManager().deleteTaskDescription(taskDescription.getName(),
+                        taskDescription.getGroup());
+            } catch (Exception ignored) {
+            }
+            handleException("Error creating a task : " + e.getMessage(), e);
+        }
+    }
+
+
+
+    public void deleteTaskDescription(TaskDescription taskDescription) {
+        String taskName = taskDescription.getName();
+        if (log.isDebugEnabled()) {
+            log.debug("Deleting the task " + taskName);
+        }
+        try {
+            schedulingManager.deleteTaskDescription(taskDescription, getConfigContext());
+        } catch (Exception e) {
+            log.error("Cannot delete the task " + taskName, e);
+        }
+    }
+
+    /**
+     * Indicating Task Scheduling manager to delete the task with the given task Name.
+     *
+     * @param taskName taskName
+     */
+    public void deleteTaskDescription(String taskName) {
+        if (log.isDebugEnabled()) {
+            log.debug("Deleting the task " + taskName);
+        }
+        try {
+            TaskDescription taskDescription = new TaskDescription();
+            taskDescription.setName(taskName);
+            schedulingManager.deleteTaskDescription(taskDescription, getConfigContext());
+        } catch (Exception e) {
+            log.error("Cannot delete the task " + taskName, e);
+        }
+    }
+
+    /**
+     * Notifying the Task Scheduling Manager to delete the previous Task Description from the
+     * Task Description Repository and add the edited task.
+     *
+     * @param taskDescription TaskDescription
+     */
+    public void editTaskDescription(TaskDescription taskDescription) {
+        if (log.isDebugEnabled()) {
+            log.debug("Editing the task " + taskDescription.getName());
+        }
+        if (schedulingManager.isContains(taskDescription.getName(), getConfigContext())) {
+            schedulingManager.deleteTaskDescription(taskDescription, getConfigContext());
+            schedulingManager.scheduleTask(taskDescription, null, getConfigContext());
+        } else {
+            log.error("Task " + taskDescription.getName() + "does not exist");
+        }
+    }
+
+    public void editTaskDescriptionInOmElement(
+            String taskElementString) throws TaskManagementException, XMLStreamException {
+        OMElement taskElement = AXIOMUtil.stringToOM(taskElementString);
+        if (log.isDebugEnabled()) {
+            log.debug("Edit TaskDescription - Get a Task configuration  :" + taskElement);
+        }
+        try {
+            editTaskDescription(validateAndCreate(taskElement));
+        } catch (Exception e) {
+            String msg = "Error editing Task";
+            throw new TaskManagementException(msg, e);
+        }
+    }
+
+    /**
+     * Returns the list of Task Descriptions that have been already added to the Task Description
+     * Repository.
+     *
+     * @return A list of Task Descriptions
+     */
+    public List<TaskDescription> getAllTaskDescriptions() {
+        List<TaskDescription> taskDescriptions = new ArrayList<TaskDescription>();
+        Iterator<TaskDescription> iterator = schedulingManager.getAllTaskDescriptions(
+                getConfigContext());
+
+        while (iterator.hasNext()) {
+            TaskDescription taskDescription = iterator.next();
+            if (taskDescription != null) {
+                taskDescriptions.add(taskDescription);
+            }
+        }
+        if (log.isDebugEnabled()) {
+            log.debug("All available Task based Scheduled Functions " + taskDescriptions);
+        }
+        return taskDescriptions;
+    }
+
+    public String getAllTaskDescriptionsInAnOMElement() throws AxisFault {
+        OMElement rootElement =
+                OMAbstractFactory.getOMFactory().createOMElement(new QName(TASK_EXTENSION_NS,
+                        "taskExtension", "task"));
+        try {
+            List<TaskDescription> descriptions = getAllTaskDescriptions();
+            for (TaskDescription taskDescription : descriptions) {
+                if (taskDescription != null) {
+                    OMElement taskElement =
+                            TaskDescriptionSerializer.serializeTaskDescription(TASK_OM_NAMESPACE,
+                                                                               taskDescription);
+                    validateTaskElement(taskElement);
+                    rootElement.addChild(taskElement);
+                }
+            }
+        } catch (TaskManagementException e) {
+            String msg = "Error loading all tasks";
+            log.error(msg, e);
+            throw new AxisFault(msg, e);
+        }
+        if (log.isDebugEnabled()) {
+            log.debug("Returning all TaskDescriptions as  :" + rootElement);
+        }
+        return rootElement.toString();
+    }
+
+    /**
+     * Returns the names of job groups that are being executed.
+     *
+     * @return An array of strings
+     */
+    public String[] getAllJobGroups() {
+        List<String> strings = getTaskManager().getAllJobGroups();
+        return strings.toArray(new String[strings.size()]);
+    }
+
+    /**
+     * Returns the TaskDescription associates with a particular task name
+     *
+     * @param taskName Task Name
+     * @return TaskDescription
+     */
+    public TaskDescription getTaskDescription(String taskName) {
+        return schedulingManager.getTaskDescription(taskName, getConfigContext());
+    }
+
+    public boolean isContains(String taskName) {
+        return schedulingManager.isContains(taskName, getConfigContext());
+    }
+
+    /**
+     * @param taskClass taskClass
+     * @return list of strings containing properties of the task class
+     */
+    public List<String> getPropertyNames(String taskClass) {
+        return null;
+    }
+
+    /**
+     * Returns the TaskManager instance currently being used
+     *
+     * @return TaskManager
+     */
+    private synchronized TaskManager getTaskManager() {
+        return (TaskManager) getConfigContext().getProperty(
+                TaskManager.CARBON_TASK_MANAGER);
+    }
+
+    /**
+     * Validates the OMElement which has a particular TaskDescription
+     * serialized into it.
+     *
+     * @param taskElement taskElement
+     * @throws TaskManagementException TaskManagementException
+     */
+    private static void validateTaskElement(
+            OMElement taskElement) throws TaskManagementException {
+        if (taskElement == null) {
+            handleException("Task Description OMElement can not be found.");
+        }
+    }
+
+    /**
+     * Handles the exception thrown and logs it.
+     *
+     * @param msg message to be logged
+     * @throws TaskManagementException TaskManagementException
+     */
+    private static void handleException(String msg) throws TaskManagementException {
+        log.error(msg);
+        throw new TaskManagementException(msg);
+    }
+
+    /**
+     * Handles the exception thrown and logs it.
+     *
+     * @param msg message to be logged
+     * @param e   exception thrown
+     * @throws TaskManagementException TaskManagementException
+     */
+    private static void handleException(String msg, Exception e) throws TaskManagementException {
+        log.error(msg, e);
+        throw new TaskManagementException(msg, e);
+    }
+
+    /**
+     * Validates an OMElement which has a TaskDescription serialized into it
+     * and returns the corresponding TaskDescription
+     *
+     * @param taskElement OMElement containing the TaskDescription
+     * @return TaskDescription
+     * @throws TaskManagementException TaskManagementException
+     */
+    private static TaskDescription validateAndCreate(
+            OMElement taskElement) throws TaskManagementException {
+
+        validateTaskElement(taskElement);
+        TaskDescription taskDescription =
+                TaskDescriptionFactory.createTaskDescription(taskElement, TASK_OM_NAMESPACE);
+        validateTaskDescription(taskDescription);
+        if (log.isDebugEnabled()) {
+            log.debug("Task Description : " + taskDescription);
+        }
+        return taskDescription;
+    }
+
+    /**
+     * Validates TaskDescriptions
+     *
+     * @param description TaskDescription
+     * @throws TaskManagementException TaskManagementException
+     */
+    private static void validateTaskDescription(
+            TaskDescription description) throws TaskManagementException {
+        if (description == null) {
+            handleException("Task Description can not be found.");
+        }
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-stratos/blob/ca25b1f7/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/task/AutoscalingJob.java
----------------------------------------------------------------------
diff --git a/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/task/AutoscalingJob.java b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/task/AutoscalingJob.java
new file mode 100644
index 0000000..852c6bd
--- /dev/null
+++ b/components/autoscaler/org.apache.stratos.mediator.autoscale/4.1.3/src/main/java/org/apache/stratos/mediator/autoscale/lbautoscale/task/AutoscalingJob.java
@@ -0,0 +1,68 @@
+/**
+ *  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.mediator.autoscale.lbautoscale.task;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.synapse.ManagedLifecycle;
+import org.apache.synapse.task.Task;
+import org.quartz.Job;
+import org.quartz.JobDetail;
+import org.quartz.JobExecutionContext;
+import org.quartz.JobExecutionException;
+import java.util.Map;
+
+public class AutoscalingJob implements Job {
+
+    public static final String AUTOSCALER_TASK = "autoscalerTask";
+
+    public static final String SYNAPSE_ENVI = "synapseEnv";
+
+    private static final Log log = LogFactory.getLog(AutoscalingJob.class);
+
+    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
+
+        try {
+
+            Task task = null;
+
+            JobDetail jobDetail = jobExecutionContext.getJobDetail();
+
+            if (log.isDebugEnabled()) {
+                log.debug("Executing Autoscaler task : " + jobDetail.getKey().getName());
+            }
+
+            @SuppressWarnings("rawtypes")
+            Map mjdm = jobExecutionContext.getMergedJobDataMap();
+
+            task = (Task) mjdm.get(AUTOSCALER_TASK);
+
+            if (task instanceof ManagedLifecycle) {
+                // Execute Autoscaler task
+                ((ServiceRequestsInFlightAutoscaler) task).execute();
+            }
+
+        } catch (Exception e) {
+            throw new JobExecutionException(e);
+        }
+
+    }
+
+}