<commit_before>/*
 * Copyright (c) 2008-2015, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed 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 com.hazelcast.simulator.coordinator;

import com.hazelcast.simulator.common.GitInfo;
import com.hazelcast.simulator.common.JavaProfiler;
import com.hazelcast.simulator.common.SimulatorProperties;
import com.hazelcast.simulator.coordinator.remoting.AgentsClient;
import com.hazelcast.simulator.coordinator.remoting.NewProtocolAgentsClient;
import com.hazelcast.simulator.protocol.connector.CoordinatorConnector;
import com.hazelcast.simulator.protocol.registry.AgentData;
import com.hazelcast.simulator.protocol.registry.ComponentRegistry;
import com.hazelcast.simulator.provisioner.Bash;
import com.hazelcast.simulator.test.TestCase;
import com.hazelcast.simulator.test.TestPhase;
import com.hazelcast.simulator.test.TestSuite;
import com.hazelcast.simulator.utils.CommandLineExitException;
import com.hazelcast.simulator.utils.ThreadSpawner;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import static com.hazelcast.simulator.coordinator.CoordinatorUtils.createAddressConfig;
import static com.hazelcast.simulator.coordinator.CoordinatorUtils.getMaxTestCaseIdLength;
import static com.hazelcast.simulator.coordinator.CoordinatorUtils.getPort;
import static com.hazelcast.simulator.coordinator.CoordinatorUtils.initMemberLayout;
import static com.hazelcast.simulator.protocol.configuration.Ports.AGENT_PORT;
import static com.hazelcast.simulator.utils.CloudProviderUtils.isEC2;
import static com.hazelcast.simulator.utils.CommonUtils.exitWithError;
import static com.hazelcast.simulator.utils.CommonUtils.getSimulatorVersion;
import static com.hazelcast.simulator.utils.CommonUtils.secondsToHuman;
import static com.hazelcast.simulator.utils.CommonUtils.sleepSeconds;
import static com.hazelcast.simulator.utils.ExecutorFactory.createFixedThreadPool;
import static com.hazelcast.simulator.utils.FileUtils.getFilesFromClassPath;
import static com.hazelcast.simulator.utils.FileUtils.getSimulatorHome;
import static com.hazelcast.simulator.utils.HarakiriMonitorUtils.getStartHarakiriMonitorCommandOrNull;
import static com.hazelcast.simulator.utils.SimulatorUtils.loadComponentRegister;
import static java.lang.String.format;

public final class Coordinator {

    static final File SIMULATOR_HOME = getSimulatorHome();
    static final File WORKING_DIRECTORY = new File(System.getProperty("user.dir"));
    static final File UPLOAD_DIRECTORY = new File(WORKING_DIRECTORY, "upload");

    private static final int COOLDOWN_SECONDS = 10;
    private static final int TEST_CASE_RUNNER_SLEEP_PERIOD = 30;
    private static final int EXECUTOR_TERMINATION_TIMEOUT_SECONDS = 10;

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

    int cooldownSeconds = COOLDOWN_SECONDS;
    int testCaseRunnerSleepPeriod = TEST_CASE_RUNNER_SLEEP_PERIOD;

    private final CoordinatorParameters parameters;
    private final TestSuite testSuite;

    private final ComponentRegistry componentRegistry;

    private final SimulatorProperties props;
    private final Bash bash;

    private AgentsClient agentsClient;
    private NewProtocolAgentsClient newProtocolAgentsClient;
    private CoordinatorConnector coordinatorConnector;

    private FailureMonitor failureMonitor;
    private PerformanceMonitor performanceMonitor;

    private ExecutorService parallelExecutor;

    public Coordinator(CoordinatorParameters parameters, TestSuite testSuite) {
        this.parameters = parameters;
        this.testSuite = testSuite;

        this.componentRegistry = loadComponentRegister(parameters.getAgentsFile());

        this.props = parameters.getSimulatorProperties();
        this.bash = new Bash(props);
    }

    CoordinatorParameters getParameters() {
        return parameters;
    }

    TestSuite getTestSuite() {
        return testSuite;
    }

    private void run() throws Exception {
        try {
            initAgents();

            startWorkers();

            failureMonitor = new FailureMonitor(agentsClient, testSuite.id);
            failureMonitor.start();

            performanceMonitor = new PerformanceMonitor(agentsClient);

            runTestSuite();

            logFailureInfo();
        } finally {
            if (performanceMonitor != null) {
                performanceMonitor.stop();
            }

            if (failureMonitor != null) {<commit_msg>Start FailureMonitor before workers start to catch startup errors.<commit_after>/*
 * Copyright (c) 2008-2015, Hazelcast, Inc. All Rights Reserved.
 *
 * Licensed 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 com.hazelcast.simulator.coordinator;

import com.hazelcast.simulator.common.GitInfo;
import com.hazelcast.simulator.common.JavaProfiler;
import com.hazelcast.simulator.common.SimulatorProperties;
import com.hazelcast.simulator.coordinator.remoting.AgentsClient;
import com.hazelcast.simulator.coordinator.remoting.NewProtocolAgentsClient;
import com.hazelcast.simulator.protocol.connector.CoordinatorConnector;
import com.hazelcast.simulator.protocol.registry.AgentData;
import com.hazelcast.simulator.protocol.registry.ComponentRegistry;
import com.hazelcast.simulator.provisioner.Bash;
import com.hazelcast.simulator.test.TestCase;
import com.hazelcast.simulator.test.TestPhase;
import com.hazelcast.simulator.test.TestSuite;
import com.hazelcast.simulator.utils.CommandLineExitException;
import com.hazelcast.simulator.utils.ThreadSpawner;
import org.apache.log4j.Logger;

import java.io.File;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

import static com.hazelcast.simulator.coordinator.CoordinatorUtils.createAddressConfig;
import static com.hazelcast.simulator.coordinator.CoordinatorUtils.getMaxTestCaseIdLength;
import static com.hazelcast.simulator.coordinator.CoordinatorUtils.getPort;
import static com.hazelcast.simulator.coordinator.CoordinatorUtils.initMemberLayout;
import static com.hazelcast.simulator.protocol.configuration.Ports.AGENT_PORT;
import static com.hazelcast.simulator.utils.CloudProviderUtils.isEC2;
import static com.hazelcast.simulator.utils.CommonUtils.exitWithError;
import static com.hazelcast.simulator.utils.CommonUtils.getSimulatorVersion;
import static com.hazelcast.simulator.utils.CommonUtils.secondsToHuman;
import static com.hazelcast.simulator.utils.CommonUtils.sleepSeconds;
import static com.hazelcast.simulator.utils.ExecutorFactory.createFixedThreadPool;
import static com.hazelcast.simulator.utils.FileUtils.getFilesFromClassPath;
import static com.hazelcast.simulator.utils.FileUtils.getSimulatorHome;
import static com.hazelcast.simulator.utils.HarakiriMonitorUtils.getStartHarakiriMonitorCommandOrNull;
import static com.hazelcast.simulator.utils.SimulatorUtils.loadComponentRegister;
import static java.lang.String.format;

public final class Coordinator {

    static final File SIMULATOR_HOME = getSimulatorHome();
    static final File WORKING_DIRECTORY = new File(System.getProperty("user.dir"));
    static final File UPLOAD_DIRECTORY = new File(WORKING_DIRECTORY, "upload");

    private static final int COOLDOWN_SECONDS = 10;
    private static final int TEST_CASE_RUNNER_SLEEP_PERIOD = 30;
    private static final int EXECUTOR_TERMINATION_TIMEOUT_SECONDS = 10;

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

    int cooldownSeconds = COOLDOWN_SECONDS;
    int testCaseRunnerSleepPeriod = TEST_CASE_RUNNER_SLEEP_PERIOD;

    private final CoordinatorParameters parameters;
    private final TestSuite testSuite;

    private final ComponentRegistry componentRegistry;

    private final SimulatorProperties props;
    private final Bash bash;

    private AgentsClient agentsClient;
    private NewProtocolAgentsClient newProtocolAgentsClient;
    private CoordinatorConnector coordinatorConnector;

    private FailureMonitor failureMonitor;
    private PerformanceMonitor performanceMonitor;

    private ExecutorService parallelExecutor;

    public Coordinator(CoordinatorParameters parameters, TestSuite testSuite) {
        this.parameters = parameters;
        this.testSuite = testSuite;

        this.componentRegistry = loadComponentRegister(parameters.getAgentsFile());

        this.props = parameters.getSimulatorProperties();
        this.bash = new Bash(props);
    }

    CoordinatorParameters getParameters() {
        return parameters;
    }

    TestSuite getTestSuite() {
        return testSuite;
    }

    private void run() throws Exception {
        try {
            initAgents();

            failureMonitor = new FailureMonitor(agentsClient, testSuite.id);
            failureMonitor.start();

            startWorkers();

            performanceMonitor = new PerformanceMonitor(agentsClient);

            runTestSuite();

            logFailureInfo();
        } finally {
            if (performanceMonitor != null) {
                performanceMonitor.stop();
            }

            if (failureMonitor != null) {
{'fraction_non_alphanumeric': 0.07529522909777987, 'fraction_numerical': 0.003778932451582428, 'mean_word_length': 4.679184549356223, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '5565923', 'n_tokens_mistral': 2975, 'n_tokens_neox': 2889, 'n_words': 658}