<commit_before>/*
 * 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.autoscaler.monitor.component;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.stratos.autoscaler.Constants;
import org.apache.stratos.autoscaler.algorithm.AutoscaleAlgorithm;
import org.apache.stratos.autoscaler.algorithm.OneAfterAnother;
import org.apache.stratos.autoscaler.algorithm.RoundRobin;
import org.apache.stratos.autoscaler.applications.ApplicationHolder;
import org.apache.stratos.autoscaler.applications.dependency.DependencyBuilder;
import org.apache.stratos.autoscaler.applications.dependency.DependencyTree;
import org.apache.stratos.autoscaler.applications.dependency.context.ApplicationChildContext;
import org.apache.stratos.autoscaler.applications.dependency.context.ClusterChildContext;
import org.apache.stratos.autoscaler.applications.dependency.context.GroupChildContext;
import org.apache.stratos.autoscaler.applications.topic.ApplicationBuilder;
import org.apache.stratos.autoscaler.event.publisher.ClusterStatusEventPublisher;
import org.apache.stratos.autoscaler.exception.application.DependencyBuilderException;
import org.apache.stratos.autoscaler.exception.application.MonitorNotFoundException;
import org.apache.stratos.autoscaler.exception.application.TopologyInConsistentException;
import org.apache.stratos.autoscaler.exception.partition.PartitionValidationException;
import org.apache.stratos.autoscaler.exception.policy.PolicyValidationException;
import org.apache.stratos.autoscaler.monitor.Monitor;
import org.apache.stratos.autoscaler.monitor.MonitorFactory;
import org.apache.stratos.autoscaler.monitor.cluster.AbstractClusterMonitor;
import org.apache.stratos.autoscaler.monitor.cluster.VMClusterMonitor;
import org.apache.stratos.autoscaler.monitor.events.builder.MonitorStatusEventBuilder;
import org.apache.stratos.autoscaler.util.ServiceReferenceHolder;
import org.apache.stratos.messaging.domain.applications.Group;
import org.apache.stratos.messaging.domain.applications.GroupStatus;
import org.apache.stratos.messaging.domain.applications.ParentComponent;
import org.apache.stratos.messaging.domain.instance.ClusterInstance;
import org.apache.stratos.messaging.domain.instance.Instance;
import org.apache.stratos.messaging.domain.topology.ClusterStatus;
import org.apache.stratos.messaging.message.receiver.topology.TopologyManager;

import java.util.*;

/**
 * Monitor is to monitor it's child monitors and
 * control them according to the dependencies respectively.
 */
public abstract class ParentComponentMonitor extends Monitor {
    private static final Log log = LogFactory.getLog(ParentComponentMonitor.class);

    //The monitors dependency tree with all the start-able/kill-able dependencies
    protected DependencyTree startupDependencyTree;
    //The monitors dependency tree with all the scaling dependencies
    protected DependencyTree scalingDependencyTree;
    //monitors map, key=GroupAlias/clusterId and value=GroupMonitor/AbstractClusterMonitor
    protected Map<String, Monitor> aliasToActiveMonitorsMap;
    //Pending monitors list
    protected List<String> pendingMonitorsList;
    //instanceIds map, stopped monitors
    protected List<String> inactiveMonitorsList;
    //terminating instances list
    protected List<String> terminatingMonitorsList;

    public ParentComponentMonitor(ParentComponent component) throws DependencyBuilderException {
        aliasToActiveMonitorsMap = new HashMap<String, Monitor>();
        inactiveMonitorsList = new ArrayList<String>();
        terminatingMonitorsList = new ArrayList<String>();
        pendingMonitorsList = new ArrayList<String>();
        //clusterIdToClusterMonitorsMap = new HashMap<String, AbstractClusterMonitor>();
        this.id = component.getUniqueIdentifier();
        //Building the startup dependencies for this monitor within the immediate children
        startupDependencyTree = DependencyBuilder.getInstance().buildDependency(component);
        //Building the scaling dependencies for this monitor within the immediate children
        scalingDependencyTree = DependencyBuilder.getInstance().buildDependency(component);
    }

    /**
     * This will start the parallel dependencies at once from the top level.
     * it will get invoked when the monitor starts up only.
     */
    public void startDependency(ParentComponent component, List<String> instanceIds) {
        //start the first dependency
        List<ApplicationChildContext> applicationContexts = this.startupDependencyTree.
                getStartAbleDependencies();
        startDependency(applicationContexts, instanceIds);

    }

    /**
     * This will start the parallel dependencies at once from the top level.
     * it will get invoked when the monitor starts up only.
     */
    public void startDependency(ParentComponent component, String instanceId) throws
            MonitorNotFoundException {
        //start the first dependency
        List<ApplicationChildContext> applicationContexts = this.startupDependencyTree.
                getStartAbleDependencies();
        startDependency(applicationContexts, instanceId);

    }

    /**
     * This will start the parallel dependencies at once from the top level.
     * it will get invoked when the monitor starts up only.
     */
    public boolean startDependencyByInstanceCreation(String childId, String instanceId) throws
            MonitorNotFoundException {
        //start the first dependency
        List<ApplicationChildContext> applicationContexts =
                this.startupDependencyTree.getStarAbleDependencies(childId);
        return startDependency(applicationContexts, instanceId);
    }

    /**
     * This will get invoked based on the activation event of its one of the child
     *
     * @param id alias/clusterId of which receive the activated event
     */
    public boolean startDependency(String id, String instanceId) throws MonitorNotFoundException {
        List<ApplicationChildContext> applicationContexts = this.startupDependencyTree
                .getStarAbleDependencies(id);
        List<String> instanceIds = new ArrayList<String>();
        instanceIds.add(instanceId);
        boolean startup = startDependency(applicationContexts, instanceIds);
        return startup;
    }

    /**
     * This will start the parallel dependencies at once from the top level
     * by traversing to find the terminated dependencies.
     * it will get invoked when start a child monitor on termination of a sub tree
     */
    public void startDependencyOnTermination(String instanceId) throws TopologyInConsistentException,
            MonitorNotFoundException, PolicyValidationException, PartitionValidationException {
        //start the first dependency which went to terminated
        List<ApplicationChildContext> applicationContexts = this.startupDependencyTree.
                getStarAbleDependenciesByTermination(this, instanceId);
        for(ApplicationChildContext context : applicationContexts) {
            if(context instanceof GroupChildContext) {
                GroupMonitor groupMonitor = (GroupMonitor) this.aliasToActiveMonitorsMap.
                                                        get(context.getId());
                groupMonitor.createInstanceAndStartDependencyOnScaleup(instanceId);
            } else if(context instanceof ClusterChildContext) {
                VMClusterMonitor clusterMonitor = (VMClusterMonitor) this.
                                                    aliasToActiveMonitorsMap.get(context.getId());
                clusterMonitor.createClusterInstanceOnScaleUp(instanceId);
            }<commit_msg>fixing a build failure due to partial commit<commit_after>/*
 * 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.autoscaler.monitor.component;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.stratos.autoscaler.Constants;
import org.apache.stratos.autoscaler.algorithm.AutoscaleAlgorithm;
import org.apache.stratos.autoscaler.algorithm.OneAfterAnother;
import org.apache.stratos.autoscaler.algorithm.RoundRobin;
import org.apache.stratos.autoscaler.applications.ApplicationHolder;
import org.apache.stratos.autoscaler.applications.dependency.DependencyBuilder;
import org.apache.stratos.autoscaler.applications.dependency.DependencyTree;
import org.apache.stratos.autoscaler.applications.dependency.context.ApplicationChildContext;
import org.apache.stratos.autoscaler.applications.dependency.context.ClusterChildContext;
import org.apache.stratos.autoscaler.applications.dependency.context.GroupChildContext;
import org.apache.stratos.autoscaler.applications.topic.ApplicationBuilder;
import org.apache.stratos.autoscaler.event.publisher.ClusterStatusEventPublisher;
import org.apache.stratos.autoscaler.exception.application.DependencyBuilderException;
import org.apache.stratos.autoscaler.exception.application.MonitorNotFoundException;
import org.apache.stratos.autoscaler.exception.application.TopologyInConsistentException;
import org.apache.stratos.autoscaler.exception.partition.PartitionValidationException;
import org.apache.stratos.autoscaler.exception.policy.PolicyValidationException;
import org.apache.stratos.autoscaler.monitor.Monitor;
import org.apache.stratos.autoscaler.monitor.MonitorFactory;
import org.apache.stratos.autoscaler.monitor.cluster.AbstractClusterMonitor;
import org.apache.stratos.autoscaler.monitor.cluster.VMClusterMonitor;
import org.apache.stratos.autoscaler.monitor.events.builder.MonitorStatusEventBuilder;
import org.apache.stratos.autoscaler.util.ServiceReferenceHolder;
import org.apache.stratos.messaging.domain.applications.Group;
import org.apache.stratos.messaging.domain.applications.GroupStatus;
import org.apache.stratos.messaging.domain.applications.ParentComponent;
import org.apache.stratos.messaging.domain.instance.ClusterInstance;
import org.apache.stratos.messaging.domain.instance.Instance;
import org.apache.stratos.messaging.domain.topology.ClusterStatus;
import org.apache.stratos.messaging.message.receiver.topology.TopologyManager;

import java.util.*;

/**
 * Monitor is to monitor it's child monitors and
 * control them according to the dependencies respectively.
 */
public abstract class ParentComponentMonitor extends Monitor {
    private static final Log log = LogFactory.getLog(ParentComponentMonitor.class);

    //The monitors dependency tree with all the start-able/kill-able dependencies
    protected DependencyTree startupDependencyTree;
    //The monitors dependency tree with all the scaling dependencies
    protected DependencyTree scalingDependencyTree;
    //monitors map, key=GroupAlias/clusterId and value=GroupMonitor/AbstractClusterMonitor
    protected Map<String, Monitor> aliasToActiveMonitorsMap;
    //Pending monitors list
    protected List<String> pendingMonitorsList;
    //instanceIds map, stopped monitors
    protected List<String> inactiveMonitorsList;
    //terminating instances list
    protected List<String> terminatingMonitorsList;

    public ParentComponentMonitor(ParentComponent component) throws DependencyBuilderException {
        aliasToActiveMonitorsMap = new HashMap<String, Monitor>();
        inactiveMonitorsList = new ArrayList<String>();
        terminatingMonitorsList = new ArrayList<String>();
        pendingMonitorsList = new ArrayList<String>();
        //clusterIdToClusterMonitorsMap = new HashMap<String, AbstractClusterMonitor>();
        this.id = component.getUniqueIdentifier();
        //Building the startup dependencies for this monitor within the immediate children
        startupDependencyTree = DependencyBuilder.getInstance().buildDependency(component);
        //Building the scaling dependencies for this monitor within the immediate children
        scalingDependencyTree = DependencyBuilder.getInstance().buildDependency(component);
    }

    /**
     * This will start the parallel dependencies at once from the top level.
     * it will get invoked when the monitor starts up only.
     */
    public void startDependency(ParentComponent component, List<String> instanceIds) {
        //start the first dependency
        List<ApplicationChildContext> applicationContexts = this.startupDependencyTree.
                getStartAbleDependencies();
        startDependency(applicationContexts, instanceIds);

    }

    /**
     * This will start the parallel dependencies at once from the top level.
     * it will get invoked when the monitor starts up only.
     */
    public void startDependency(ParentComponent component, String instanceId) throws
            MonitorNotFoundException {
        //start the first dependency
        List<ApplicationChildContext> applicationContexts = this.startupDependencyTree.
                getStartAbleDependencies();
        startDependency(applicationContexts, instanceId);

    }

    /**
     * This will start the parallel dependencies at once from the top level.
     * it will get invoked when the monitor starts up only.
     */
    public boolean startDependencyByInstanceCreation(String childId, String instanceId) throws
            MonitorNotFoundException {
        //start the first dependency
        List<ApplicationChildContext> applicationContexts =
                this.startupDependencyTree.getStarAbleDependencies(childId);
        return startDependency(applicationContexts, instanceId);
    }

    /**
     * This will get invoked based on the activation event of its one of the child
     *
     * @param id alias/clusterId of which receive the activated event
     */
    public boolean startDependency(String id, String instanceId) throws MonitorNotFoundException {
        List<ApplicationChildContext> applicationContexts = this.startupDependencyTree
                .getStarAbleDependencies(id);
        List<String> instanceIds = new ArrayList<String>();
        instanceIds.add(instanceId);
        boolean startup = startDependency(applicationContexts, instanceIds);
        return startup;
    }

    /**
     * This will start the parallel dependencies at once from the top level
     * by traversing to find the terminated dependencies.
     * it will get invoked when start a child monitor on termination of a sub tree
     */

    public void startDependencyOnTermination(String instanceId) throws TopologyInConsistentException,
            MonitorNotFoundException, PolicyValidationException, PartitionValidationException {

        //start the first dependency which went to terminated
        List<ApplicationChildContext> applicationContexts = this.startupDependencyTree.
                getStarAbleDependenciesByTermination(this, instanceId);
        for(ApplicationChildContext context : applicationContexts) {
            if(context instanceof GroupChildContext) {
                GroupMonitor groupMonitor = (GroupMonitor) this.aliasToActiveMonitorsMap.
                                                        get(context.getId());
                groupMonitor.createInstanceAndStartDependencyOnScaleup(instanceId);
            } else if(context instanceof ClusterChildContext) {
                VMClusterMonitor clusterMonitor = (VMClusterMonitor) this.
                                                    aliasToActiveMonitorsMap.get(context.getId());
                clusterMonitor.createClusterInstanceOnScaleUp(instanceId);
            }
{'fraction_non_alphanumeric': 0.06920496675101512, 'fraction_numerical': 0.00047078208674159946, 'mean_word_length': 3.998235294117647, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1122931', 'n_tokens_mistral': 4093, 'n_tokens_neox': 3933, 'n_words': 1113}