<commit_before>/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch 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.elasticsearch.gradle.test

import org.elasticsearch.gradle.Version
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.tasks.Input

/** Configuration for an elasticsearch cluster, used for integration tests. */
class ClusterConfiguration {

    private final Project project

    @Input
    String distribution = 'default'

    @Input
    int numNodes = 1

    @Input
    int numBwcNodes = 0

    @Input
    Version bwcVersion = null

    @Input
    int httpPort = 0

    @Input
    int transportPort = 0

    /**
     * An override of the data directory. Input is the node number and output
     * is the override data directory.
     */
    @Input
    Closure<String> dataDir = null

    /** Optional override of the cluster name. */
    @Input
    String clusterName = null

    @Input
    boolean daemonize = true

    @Input
    boolean debug = false

    /**
     * Configuration of the setting {@code discovery.zen.minimum_master_nodes} on the nodes.
     * In case of more than one node, this defaults to the number of nodes
     */
    @Input
    Closure<Integer> minimumMasterNodes = {
        if (bwcVersion != null && bwcVersion.before("6.5.0")) {
            return numNodes > 1 ? numNodes : -1
        } else {
            return numNodes > 1 ? numNodes.intdiv(2) + 1 : -1
        }
    }

    /**
     * Whether the initial_master_nodes setting should be automatically derived from the nodes
     * in the cluster. Only takes effect if all nodes in the cluster understand this setting
     * and the discovery type is not explicitly set.
     */
    @Input
    boolean autoSetInitialMasterNodes = true

    /**
     * Whether the file-based discovery provider should be automatically setup based on
     * the nodes in the cluster. Only takes effect if no other hosts provider is already
     * configured.
     */
    @Input
    boolean autoSetHostsProvider = true

    @Input
    String jvmArgs = "-Xms" + System.getProperty('tests.heap.size', '512m') +
        " " + "-Xmx" + System.getProperty('tests.heap.size', '512m') +
        " " + System.getProperty('tests.jvm.argline', '')

    /**
     * Should the shared environment be cleaned on cluster startup? Defaults
     * to {@code true} so we run with a clean cluster but some tests wish to
     * preserve snapshots between clusters so they set this to true.
     */
    @Input
    boolean cleanShared = true

    /**
     * A closure to call which returns the unicast host to connect to for cluster formation.
     *
     * This allows multi node clusters, or a new cluster to connect to an existing cluster.
     * The closure takes three arguments, the NodeInfo for the first node in the cluster,
     * the NodeInfo for the node current being configured, an AntBuilder which may be used
     * to wait on conditions before returning.
     */
    @Input
    Closure unicastTransportUri = { NodeInfo seedNode, NodeInfo node, AntBuilder ant ->
        if (seedNode == node) {
            return null
        }
        ant.waitfor(maxwait: '40', maxwaitunit: 'second', checkevery: '500', checkeveryunit: 'millisecond') {
            resourceexists {
                file(file: seedNode.transportPortsFile.toString())
            }
        }
        return seedNode.transportUri()
    }

    /**<commit_msg>Add an exception throw if waiting on transport port file fails (#37574)<commit_after>/*
 * Licensed to Elasticsearch under one or more contributor
 * license agreements. See the NOTICE file distributed with
 * this work for additional information regarding copyright
 * ownership. Elasticsearch 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.elasticsearch.gradle.test

import org.elasticsearch.gradle.Version
import org.gradle.api.GradleException
import org.gradle.api.Project
import org.gradle.api.tasks.Input

/** Configuration for an elasticsearch cluster, used for integration tests. */
class ClusterConfiguration {

    private final Project project

    @Input
    String distribution = 'default'

    @Input
    int numNodes = 1

    @Input
    int numBwcNodes = 0

    @Input
    Version bwcVersion = null

    @Input
    int httpPort = 0

    @Input
    int transportPort = 0

    /**
     * An override of the data directory. Input is the node number and output
     * is the override data directory.
     */
    @Input
    Closure<String> dataDir = null

    /** Optional override of the cluster name. */
    @Input
    String clusterName = null

    @Input
    boolean daemonize = true

    @Input
    boolean debug = false

    /**
     * Configuration of the setting {@code discovery.zen.minimum_master_nodes} on the nodes.
     * In case of more than one node, this defaults to the number of nodes
     */
    @Input
    Closure<Integer> minimumMasterNodes = {
        if (bwcVersion != null && bwcVersion.before("6.5.0")) {
            return numNodes > 1 ? numNodes : -1
        } else {
            return numNodes > 1 ? numNodes.intdiv(2) + 1 : -1
        }
    }

    /**
     * Whether the initial_master_nodes setting should be automatically derived from the nodes
     * in the cluster. Only takes effect if all nodes in the cluster understand this setting
     * and the discovery type is not explicitly set.
     */
    @Input
    boolean autoSetInitialMasterNodes = true

    /**
     * Whether the file-based discovery provider should be automatically setup based on
     * the nodes in the cluster. Only takes effect if no other hosts provider is already
     * configured.
     */
    @Input
    boolean autoSetHostsProvider = true

    @Input
    String jvmArgs = "-Xms" + System.getProperty('tests.heap.size', '512m') +
        " " + "-Xmx" + System.getProperty('tests.heap.size', '512m') +
        " " + System.getProperty('tests.jvm.argline', '')

    /**
     * Should the shared environment be cleaned on cluster startup? Defaults
     * to {@code true} so we run with a clean cluster but some tests wish to
     * preserve snapshots between clusters so they set this to true.
     */
    @Input
    boolean cleanShared = true

    /**
     * A closure to call which returns the unicast host to connect to for cluster formation.
     *
     * This allows multi node clusters, or a new cluster to connect to an existing cluster.
     * The closure takes three arguments, the NodeInfo for the first node in the cluster,
     * the NodeInfo for the node current being configured, an AntBuilder which may be used
     * to wait on conditions before returning.
     */
    @Input
    Closure unicastTransportUri = { NodeInfo seedNode, NodeInfo node, AntBuilder ant ->
        if (seedNode == node) {
            return null
        }
        ant.waitfor(maxwait: '40', maxwaitunit: 'second', checkevery: '500', checkeveryunit: 'millisecond',
                timeoutproperty: "failed.${seedNode.transportPortsFile.path}") {
            resourceexists {
                file(file: seedNode.transportPortsFile.toString())
            }
        }
        if (ant.properties.containsKey("failed.${seedNode.transportPortsFile.path}".toString())) {
            throw new GradleException("Failed to locate seed node transport file [${seedNode.transportPortsFile}]: " +
                    "timed out waiting for it to be created after ${waitSeconds} seconds")
        }
        return seedNode.transportUri()
    }

    /**
{'fraction_non_alphanumeric': 0.07807842238018803, 'fraction_numerical': 0.006993808759458839, 'mean_word_length': 2.6605119597146456, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2374230', 'n_tokens_mistral': 2300, 'n_tokens_neox': 2219, 'n_words': 932}