<commit_before>package org.ow2.proactive.resourcemanager.nodesource.infrastructure;

import java.io.IOException;
import java.security.KeyException;
import java.util.Hashtable;
import java.util.concurrent.atomic.AtomicInteger;

import org.objectweb.proactive.core.config.CentralPAPropertyRepository;
import org.objectweb.proactive.core.node.Node;
import org.objectweb.proactive.core.util.ProActiveCounter;
import org.ow2.proactive.authentication.crypto.Credentials;
import org.ow2.proactive.resourcemanager.core.properties.PAResourceManagerProperties;
import org.ow2.proactive.resourcemanager.exception.RMException;
import org.ow2.proactive.resourcemanager.nodesource.common.Configurable;
import org.ow2.proactive.resourcemanager.utils.RMNodeStarter.CommandLineBuilder;
import org.ow2.proactive.resourcemanager.utils.RMNodeStarter.OperatingSystem;


public class LocalInfrastructure extends InfrastructureManager {

    @Configurable(description = "Absolute path to credentials file\nused to add the node to the Resource Manager", credential = true)
    private Credentials credentials;
    @Configurable(description = "Maximum number of nodes to\nbe deployed on Resource Manager machine")
    private int maxNodes = 4;
    private AtomicInteger atomicMaxNodes;
    @Configurable(description = "in ms. After this timeout expired\nthe node is considered to be lost")
    private int nodeTimeout = 5000;
    @Configurable(description = "Aditionnal ProActive properties")
    private String paProperties = "";

    /** To link a nodeName with its process */
    private Hashtable<String, Process> nodeNameToProcess;
    /** To link a deploying node url with a boolean, used to notify deploying node removal */
    private Hashtable<String, Boolean> isDeployingNodeLost;
    /** Notifies the acquisition loop that the node has been acquired and that it can exit the loop */
    private Hashtable<String, Boolean> isNodeAcquired;

    /** The shell interpret and its "command" argument */
    private static final String SHELL_INTERPRET = "/bin/sh";
    private static final String SHELL_COMMAND_OPTION = "-c";

    public LocalInfrastructure() {
    }

    public String getDescription() {
        return "Deploys nodes on Resource Manager's machine";
    }

    @Override
    public void acquireAllNodes() {
        while (this.atomicMaxNodes.getAndDecrement() >= 1) {
            this.nodeSource.executeInParallel(new Runnable() {
                public void run() {
                    LocalInfrastructure.this.acquireNodeImpl();
                }
            });
        }
        //one decremented once too many
        this.atomicMaxNodes.getAndIncrement();
        logger.info("Cannot acquire more nodes");
    }

    @Override
    public void acquireNode() {
        if (this.atomicMaxNodes.getAndDecrement() >= 1) {
            this.nodeSource.executeInParallel(new Runnable() {
                public void run() {
                    LocalInfrastructure.this.acquireNodeImpl();
                }
            });
        } else {
            //one decremented once too many
            this.atomicMaxNodes.getAndIncrement();
            logger.warn("Cannot acquire a new node");
        }
    }

    private void acquireNodeImpl() {
        String nodeName = "local-" + this.nodeSource.getName() + "-" + ProActiveCounter.getUniqID();
        String os = System.getProperty("os.name");
        OperatingSystem osObj = null;
        //assuming no cygwin, windows or the "others"...
        if (os.contains("Windows")) {
            osObj = OperatingSystem.WINDOWS;
        } else {
            osObj = OperatingSystem.UNIX;
        }
        String rmHome = PAResourceManagerProperties.RM_HOME.getValueAsString();
        if (!rmHome.endsWith(osObj.fs)) {
            rmHome += osObj.fs;
        }
        CommandLineBuilder clb = this.getDefaultCommandLineBuilder(osObj);
        //RM_Home set in bin/unix/env script
        clb.setRmHome(rmHome);
        boolean containsSpace = false;
        if (rmHome.contains(" ")) {
            containsSpace = true;
        }
        if (!this.paProperties.contains(CentralPAPropertyRepository.JAVA_SECURITY_POLICY.getName())) {
            this.paProperties += " " + CentralPAPropertyRepository.JAVA_SECURITY_POLICY.getCmdLine();
            if (containsSpace) {
                this.paProperties += "\"";
            }
            this.paProperties += rmHome + "config" + osObj.fs + "security.java.policy-client";
            if (containsSpace) {
                this.paProperties += "\"";
            }
        }
        if (!this.paProperties.contains(CentralPAPropertyRepository.LOG4J.getName())) {
            this.paProperties += " " + CentralPAPropertyRepository.LOG4J.getCmdLine();
            if (containsSpace) {
                this.paProperties += "\"";
            }
            //often needs this specific log4j config on windows... because of tty problems??
            this.paProperties += rmHome + "config" + osObj.fs + "log4j" + osObj.fs + "rm-log4j-server";
            if (containsSpace) {
                this.paProperties += "\"";
            }
        }
        if (!this.paProperties.contains(PAResourceManagerProperties.RM_HOME.getKey())) {
            this.paProperties += " " + PAResourceManagerProperties.RM_HOME.getCmdLine();
            if (containsSpace) {
                this.paProperties += "\"";
            }
            this.paProperties += rmHome;
            if (containsSpace) {
                if (rmHome.endsWith("\\")) {
                    this.paProperties += "\\";
                }
                this.paProperties += "\"";
            }
        }
        clb.setPaProperties(this.paProperties);
        clb.setNodeName(nodeName);
        try {
            clb.setCredentialsValueAndNullOthers(new String(this.credentials.getBase64()));
        } catch (KeyException e) {
            createLostNode(nodeName, "Cannot decrypt credentials value", e);
            return;
        }
        String cmdLine = null;
        try {
            cmdLine = clb.buildCommandLine(true);
        } catch (IOException e) {<commit_msg>SCHEDULING-1493 Race condition in the LocalInfrastructure.acquireNodeImpl<commit_after>package org.ow2.proactive.resourcemanager.nodesource.infrastructure;

import java.io.IOException;
import java.security.KeyException;
import java.util.Hashtable;
import java.util.concurrent.atomic.AtomicInteger;

import org.objectweb.proactive.core.config.CentralPAPropertyRepository;
import org.objectweb.proactive.core.node.Node;
import org.objectweb.proactive.core.util.ProActiveCounter;
import org.ow2.proactive.authentication.crypto.Credentials;
import org.ow2.proactive.resourcemanager.core.properties.PAResourceManagerProperties;
import org.ow2.proactive.resourcemanager.exception.RMException;
import org.ow2.proactive.resourcemanager.nodesource.common.Configurable;
import org.ow2.proactive.resourcemanager.utils.RMNodeStarter.CommandLineBuilder;
import org.ow2.proactive.resourcemanager.utils.RMNodeStarter.OperatingSystem;


public class LocalInfrastructure extends InfrastructureManager {

    @Configurable(description = "Absolute path to credentials file\nused to add the node to the Resource Manager", credential = true)
    private Credentials credentials;
    @Configurable(description = "Maximum number of nodes to\nbe deployed on Resource Manager machine")
    private int maxNodes = 4;
    private AtomicInteger atomicMaxNodes;
    @Configurable(description = "in ms. After this timeout expired\nthe node is considered to be lost")
    private int nodeTimeout = 5000;
    @Configurable(description = "Aditionnal ProActive properties")
    private String paProperties = "";

    /** To link a nodeName with its process */
    private Hashtable<String, Process> nodeNameToProcess;
    /** To link a deploying node url with a boolean, used to notify deploying node removal */
    private Hashtable<String, Boolean> isDeployingNodeLost;
    /** Notifies the acquisition loop that the node has been acquired and that it can exit the loop */
    private Hashtable<String, Boolean> isNodeAcquired;

    /** The shell interpret and its "command" argument */
    private static final String SHELL_INTERPRET = "/bin/sh";
    private static final String SHELL_COMMAND_OPTION = "-c";

    public LocalInfrastructure() {
    }

    public String getDescription() {
        return "Deploys nodes on Resource Manager's machine";
    }

    @Override
    public void acquireAllNodes() {
        while (this.atomicMaxNodes.getAndDecrement() >= 1) {
            this.nodeSource.executeInParallel(new Runnable() {
                public void run() {
                    LocalInfrastructure.this.acquireNodeImpl();
                }
            });
        }
        //one decremented once too many
        this.atomicMaxNodes.getAndIncrement();
        logger.info("Cannot acquire more nodes");
    }

    @Override
    public void acquireNode() {
        if (this.atomicMaxNodes.getAndDecrement() >= 1) {
            this.nodeSource.executeInParallel(new Runnable() {
                public void run() {
                    LocalInfrastructure.this.acquireNodeImpl();
                }
            });
        } else {
            //one decremented once too many
            this.atomicMaxNodes.getAndIncrement();
            logger.warn("Cannot acquire a new node");
        }
    }

    private void acquireNodeImpl() {
        String nodeName = "local-" + this.nodeSource.getName() + "-" + ProActiveCounter.getUniqID();
        String os = System.getProperty("os.name");
        OperatingSystem osObj = null;
        //assuming no cygwin, windows or the "others"...
        if (os.contains("Windows")) {
            osObj = OperatingSystem.WINDOWS;
        } else {
            osObj = OperatingSystem.UNIX;
        }
        String rmHome = PAResourceManagerProperties.RM_HOME.getValueAsString();
        if (!rmHome.endsWith(osObj.fs)) {
            rmHome += osObj.fs;
        }
        CommandLineBuilder clb = this.getDefaultCommandLineBuilder(osObj);
        //RM_Home set in bin/unix/env script
        clb.setRmHome(rmHome);
        boolean containsSpace = false;
        if (rmHome.contains(" ")) {
            containsSpace = true;
        }
        String paProperties = this.paProperties;
        if (!paProperties.contains(CentralPAPropertyRepository.JAVA_SECURITY_POLICY.getName())) {
            paProperties += " " + CentralPAPropertyRepository.JAVA_SECURITY_POLICY.getCmdLine();
            if (containsSpace) {
                paProperties += "\"";
            }
            paProperties += rmHome + "config" + osObj.fs + "security.java.policy-client";
            if (containsSpace) {
                paProperties += "\"";
            }
        }
        if (!paProperties.contains(CentralPAPropertyRepository.LOG4J.getName())) {
            paProperties += " " + CentralPAPropertyRepository.LOG4J.getCmdLine();
            if (containsSpace) {
                paProperties += "\"";
            }
            //often needs this specific log4j config on windows... because of tty problems??
            paProperties += rmHome + "config" + osObj.fs + "log4j" + osObj.fs + "rm-log4j-server";
            if (containsSpace) {
                paProperties += "\"";
            }
        }
        if (!paProperties.contains(PAResourceManagerProperties.RM_HOME.getKey())) {
            paProperties += " " + PAResourceManagerProperties.RM_HOME.getCmdLine();
            if (containsSpace) {
                paProperties += "\"";
            }
            paProperties += rmHome;
            if (containsSpace) {
                if (rmHome.endsWith("\\")) {
                    paProperties += "\\";
                }
                paProperties += "\"";
            }
        }
        clb.setPaProperties(paProperties);
        clb.setNodeName(nodeName);
        try {
            clb.setCredentialsValueAndNullOthers(new String(this.credentials.getBase64()));
        } catch (KeyException e) {
            createLostNode(nodeName, "Cannot decrypt credentials value", e);
            return;
        }
        String cmdLine = null;
        try {
            cmdLine = clb.buildCommandLine(true);
        } catch (IOException e) {
{'fraction_non_alphanumeric': 0.10156760287393861, 'fraction_numerical': 0.0037557152188112345, 'mean_word_length': 2.4349411104879417, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 13, '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': '2150124', 'n_tokens_mistral': 3316, 'n_tokens_neox': 3205, 'n_words': 726}