<commit_before>package eu.stratuslab.hudson;

import static eu.stratuslab.hudson.CloudParameterUtils.isEmptyStringOrNull;
import static eu.stratuslab.hudson.CloudParameterUtils.isPositiveInteger;
import static eu.stratuslab.hudson.CloudParameterUtils.validateClientLocation;
import static eu.stratuslab.hudson.CloudParameterUtils.validateEndpoint;
import hudson.Extension;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import hudson.model.Label;
import hudson.model.Node;
import hudson.slaves.Cloud;
import hudson.slaves.CloudRetentionStrategy;
import hudson.slaves.NodeProperty;
import hudson.slaves.NodeProvisioner.PlannedNode;
import hudson.slaves.AbstractCloudImpl;
import hudson.slaves.ComputerLauncher;
import hudson.util.FormValidation;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

import eu.stratuslab.hudson.SlaveTemplate.InstanceTypes;

public class StratusLabCloud extends AbstractCloudImpl {

    private static final Logger LOGGER = Logger.getLogger(StratusLabCloud.class
            .getName());

    private static final int IDLE_MINUTES = 10;

    private static final String CLOUD_NAME = "StratusLab Cloud";

    private static final String EMPTY_STRING = "";

    public final StratusLabProxy.StratusLabParams params;

    public final String clientLocation;

    public final String endpoint;

    public final String username;

    public final String password;

    public final String instanceLimit;

    public final int instanceLimitInt;

    public final List<SlaveTemplate> templates;

    private final Map<String, SlaveTemplate> labelToTemplateMap;

    private static final AtomicInteger serial = new AtomicInteger(0);

    @DataBoundConstructor
    public StratusLabCloud(String clientLocation, String endpoint,
            String username, String password, String instanceLimit,
            List<SlaveTemplate> templates) {

        super(CLOUD_NAME, instanceLimit);

        this.clientLocation = clientLocation;
        this.endpoint = endpoint;
        this.username = username;
        this.password = password;

        params = new StratusLabProxy.StratusLabParams(clientLocation, endpoint,
                username, password);

        this.instanceLimit = instanceLimit;
        this.templates = copyToImmutableList(templates);

        labelToTemplateMap = mapLabelsToTemplates(this.templates);

        int value = 1;
        try {
            value = Integer.parseInt(instanceLimit);
        } catch (IllegalArgumentException e) {
        }
        instanceLimitInt = value;

        String msg = "configuration updated with " + labelToTemplateMap.size()
                + " label(s) and " + this.templates.size()
                + " slave template(s)";
        LOGGER.info(msg);
    }

    private List<SlaveTemplate> copyToImmutableList(
            List<SlaveTemplate> templates) {
        ArrayList<SlaveTemplate> list = new ArrayList<SlaveTemplate>();
        if (templates != null) {
            list.addAll(templates);
        }
        list.trimToSize();
        return Collections.unmodifiableList(list);
    }

    private Map<String, SlaveTemplate> mapLabelsToTemplates(
            List<SlaveTemplate> templates) {

        Map<String, SlaveTemplate> map = new HashMap<String, SlaveTemplate>();

        for (SlaveTemplate template : templates) {
            for (String label : template.labels) {
                map.put(label, template);
            }
        }

        return Collections.unmodifiableMap(map);
    }

    // TODO: Check if this is necessary or defined in superclass.
    @SuppressWarnings("unchecked")
    public Descriptor<Cloud> getDescriptor() {
        return Hudson.getInstance().getDescriptor(getClass());
    }

    public boolean canProvision(Label label) {
        if (label != null) {
            return labelToTemplateMap.containsKey(label.getName());
        } else {
            return false;
        }
    }

    public Collection<PlannedNode> provision(Label label, int excessWorkload) {

        Collection<PlannedNode> nodes = new LinkedList<PlannedNode>();

        // The returned value for template should never be null because
        // only labels will be used for which the canProvision method
        // returned true. However, label can be null if Hudson is searching
        // for a node without a label. This implementation doesn't support
        // that.
        if (label != null) {
            SlaveTemplate template = labelToTemplateMap.get(label.getName());

            int executors = template.getExecutors();

            String displayName = generateDisplayName(label,
                    template.marketplaceId, template.instanceType);

            int numberOfInstances = StratusLabProxy
                    .getNumberOfDefinedInstances(params);

            for (int i = 0; i < excessWorkload; i += executors) {
                SlaveCreator c = new SlaveCreator(template, this, label,
                        displayName);
                Future<Node> futureNode = Computer.threadPoolForRemoting
                        .submit(c);
                if (numberOfInstances < instanceLimitInt) {
                    nodes.add(new PlannedNode(displayName, futureNode,
                            executors));
                    numberOfInstances++;
                } else {
                    LOGGER.warning("instance limit (" + instanceLimitInt
                            + ") exceeded not provisioning node");
                }
            }<commit_msg>change instanceLimit variable to an int from a String<commit_after>package eu.stratuslab.hudson;

import static eu.stratuslab.hudson.CloudParameterUtils.isEmptyStringOrNull;
import static eu.stratuslab.hudson.CloudParameterUtils.isPositiveInteger;
import static eu.stratuslab.hudson.CloudParameterUtils.validateClientLocation;
import static eu.stratuslab.hudson.CloudParameterUtils.validateEndpoint;
import hudson.Extension;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.Hudson;
import hudson.model.Label;
import hudson.model.Node;
import hudson.slaves.Cloud;
import hudson.slaves.CloudRetentionStrategy;
import hudson.slaves.NodeProperty;
import hudson.slaves.NodeProvisioner.PlannedNode;
import hudson.slaves.AbstractCloudImpl;
import hudson.slaves.ComputerLauncher;
import hudson.util.FormValidation;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Logger;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;

import eu.stratuslab.hudson.SlaveTemplate.InstanceTypes;

public class StratusLabCloud extends AbstractCloudImpl {

    private static final Logger LOGGER = Logger.getLogger(StratusLabCloud.class
            .getName());

    private static final int IDLE_MINUTES = 10;

    private static final String CLOUD_NAME = "StratusLab Cloud";

    private static final String EMPTY_STRING = "";

    public final StratusLabProxy.StratusLabParams params;

    public final String clientLocation;

    public final String endpoint;

    public final String username;

    public final String password;

    public final int instanceLimit;

    public final List<SlaveTemplate> templates;

    private final Map<String, SlaveTemplate> labelToTemplateMap;

    private static final AtomicInteger serial = new AtomicInteger(0);

    @DataBoundConstructor
    public StratusLabCloud(String clientLocation, String endpoint,
            String username, String password, int instanceLimit,
            List<SlaveTemplate> templates) {

        super(CLOUD_NAME, String.valueOf(instanceLimit));

        this.clientLocation = clientLocation;
        this.endpoint = endpoint;
        this.username = username;
        this.password = password;

        params = new StratusLabProxy.StratusLabParams(clientLocation, endpoint,
                username, password);

        this.instanceLimit = instanceLimit;
        this.templates = copyToImmutableList(templates);

        labelToTemplateMap = mapLabelsToTemplates(this.templates);

        String msg = "configuration updated with " + labelToTemplateMap.size()
                + " label(s) and " + this.templates.size()
                + " slave template(s)";
        LOGGER.info(msg);
    }

    private List<SlaveTemplate> copyToImmutableList(
            List<SlaveTemplate> templates) {
        ArrayList<SlaveTemplate> list = new ArrayList<SlaveTemplate>();
        if (templates != null) {
            list.addAll(templates);
        }
        list.trimToSize();
        return Collections.unmodifiableList(list);
    }

    private Map<String, SlaveTemplate> mapLabelsToTemplates(
            List<SlaveTemplate> templates) {

        Map<String, SlaveTemplate> map = new HashMap<String, SlaveTemplate>();

        for (SlaveTemplate template : templates) {
            for (String label : template.labels) {
                map.put(label, template);
            }
        }

        return Collections.unmodifiableMap(map);
    }

    // TODO: Check if this is necessary or defined in superclass.
    @SuppressWarnings("unchecked")
    public Descriptor<Cloud> getDescriptor() {
        return Hudson.getInstance().getDescriptor(getClass());
    }

    public boolean canProvision(Label label) {
        if (label != null) {
            return labelToTemplateMap.containsKey(label.getName());
        } else {
            return false;
        }
    }

    public Collection<PlannedNode> provision(Label label, int excessWorkload) {

        Collection<PlannedNode> nodes = new LinkedList<PlannedNode>();

        // The returned value for template should never be null because
        // only labels will be used for which the canProvision method
        // returned true. However, label can be null if Hudson is searching
        // for a node without a label. This implementation doesn't support
        // that.
        if (label != null) {
            SlaveTemplate template = labelToTemplateMap.get(label.getName());

            int executors = template.getExecutors();

            String displayName = generateDisplayName(label,
                    template.marketplaceId, template.instanceType);

            int numberOfInstances = StratusLabProxy
                    .getNumberOfDefinedInstances(params);

            for (int i = 0; i < excessWorkload; i += executors) {
                SlaveCreator c = new SlaveCreator(template, this, label,
                        displayName);
                Future<Node> futureNode = Computer.threadPoolForRemoting
                        .submit(c);
                if (numberOfInstances < instanceLimit) {
                    nodes.add(new PlannedNode(displayName, futureNode,
                            executors));
                    numberOfInstances++;
                } else {
                    LOGGER.warning("instance limit (" + instanceLimit
                            + ") exceeded not provisioning node");
                }
            }
{'fraction_non_alphanumeric': 0.08127721335268505, 'fraction_numerical': 0.0007683770169896696, 'mean_word_length': 2.760513643659711, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 35, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3104858', 'n_tokens_mistral': 3115, 'n_tokens_neox': 2942, 'n_words': 747}