<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.protocol.operation.CreateTestOperation;
import com.hazelcast.simulator.protocol.operation.StartTestOperation;
import com.hazelcast.simulator.protocol.operation.StartTestPhaseOperation;
import com.hazelcast.simulator.protocol.operation.StopTestOperation;
import com.hazelcast.simulator.protocol.registry.ComponentRegistry;
import com.hazelcast.simulator.test.TestCase;
import com.hazelcast.simulator.test.TestPhase;
import com.hazelcast.simulator.test.TestSuite;
import org.apache.log4j.Logger;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

import static com.hazelcast.simulator.test.TestPhase.GLOBAL_TEARDOWN;
import static com.hazelcast.simulator.test.TestPhase.GLOBAL_VERIFY;
import static com.hazelcast.simulator.test.TestPhase.GLOBAL_WARMUP;
import static com.hazelcast.simulator.test.TestPhase.LOCAL_TEARDOWN;
import static com.hazelcast.simulator.test.TestPhase.LOCAL_VERIFY;
import static com.hazelcast.simulator.test.TestPhase.LOCAL_WARMUP;
import static com.hazelcast.simulator.test.TestPhase.RUN;
import static com.hazelcast.simulator.test.TestPhase.SETUP;
import static com.hazelcast.simulator.utils.CommonUtils.getElapsedSeconds;
import static com.hazelcast.simulator.utils.CommonUtils.rethrow;
import static com.hazelcast.simulator.utils.CommonUtils.sleepSeconds;
import static com.hazelcast.simulator.utils.FormatUtils.formatPercentage;
import static com.hazelcast.simulator.utils.FormatUtils.padRight;
import static com.hazelcast.simulator.utils.FormatUtils.secondsToHuman;
import static java.lang.String.format;

/**
 * Responsible for running a single {@link TestCase}.
 *
 * Multiple TestCases can be run in parallel, by having multiple TestCaseRunners in parallel.
 */
final class TestCaseRunner implements TestPhaseListener {

    private static final int RUN_PHASE_LOG_INTERVAL_SECONDS = 30;
    private static final int WAIT_FOR_PHASE_COMPLETION_LOG_INTERVAL_SECONDS = 30;

    private static final Logger LOGGER = Logger.getLogger(TestCaseRunner.class);
    private static final ConcurrentMap<TestPhase, Object> LOG_TEST_PHASE_COMPLETION = new ConcurrentHashMap<TestPhase, Object>();

    private final ConcurrentMap<TestPhase, AtomicInteger> phaseCompletedMap = new ConcurrentHashMap<TestPhase, AtomicInteger>();
    private final CountDownLatch waitForStopThread = new CountDownLatch(1);

    private final int testIndex;
    private final TestCase testCase;
    private final String testCaseId;
    private final TestSuite testSuite;

    private final RemoteClient remoteClient;
    private final FailureContainer failureContainer;
    private final PerformanceStateContainer performanceStateContainer;
    private final ComponentRegistry componentRegistry;

    private final String prefix;
    private final ConcurrentMap<TestPhase, CountDownLatch> testPhaseSyncMap;

    private final boolean isVerifyEnabled;
    private final boolean isPassiveMembers;

    private final boolean monitorPerformance;
    private final int logPerformanceIntervalSeconds;
    private final int logRunPhaseIntervalSeconds;

    TestCaseRunner(int testIndex, TestCase testCase, Coordinator coordinator, int paddingLength,
                   ConcurrentMap<TestPhase, CountDownLatch> testPhaseSyncMap) {
        this.testIndex = testIndex;
        this.testCase = testCase;
        this.testCaseId = testCase.getId();
        this.testSuite = coordinator.getTestSuite();

        this.remoteClient = coordinator.getRemoteClient();
        this.failureContainer = coordinator.getFailureContainer();
        this.performanceStateContainer = coordinator.getPerformanceStateContainer();
        this.componentRegistry = coordinator.getComponentRegistry();

        this.prefix = padRight(testCaseId, paddingLength + 1);
        this.testPhaseSyncMap = testPhaseSyncMap;

        CoordinatorParameters coordinatorParameters = coordinator.getCoordinatorParameters();
        this.isVerifyEnabled = coordinatorParameters.isVerifyEnabled();

        ClusterLayoutParameters clusterLayoutParameters = coordinator.getClusterLayoutParameters();
        this.isPassiveMembers = (coordinatorParameters.isPassiveMembers() && clusterLayoutParameters.getClientWorkerCount() > 0);

        WorkerParameters workerParameters = coordinator.getWorkerParameters();
        this.monitorPerformance = workerParameters.isMonitorPerformance();
        this.logPerformanceIntervalSeconds = workerParameters.getWorkerPerformanceMonitorIntervalSeconds();
        this.logRunPhaseIntervalSeconds = workerParameters.getRunPhaseLogIntervalSeconds(RUN_PHASE_LOG_INTERVAL_SECONDS);

        for (TestPhase testPhase : TestPhase.values()) {
            phaseCompletedMap.put(testPhase, new AtomicInteger());
        }

        performanceStateContainer.init(testCaseId);
    }

    @Override
    public void completed(TestPhase testPhase) {
        phaseCompletedMap.get(testPhase).incrementAndGet();
    }

    void run() {
        try {
            createTest();
            runPhase(SETUP);

            runPhase(LOCAL_WARMUP);
            runPhase(GLOBAL_WARMUP);

            startTest();
            waitForTestCompletion();

            if (isVerifyEnabled) {
                runPhase(GLOBAL_VERIFY);
                runPhase(LOCAL_VERIFY);
            } else {
                echo("Skipping Test verification");
            }

            runPhase(GLOBAL_TEARDOWN);
            runPhase(LOCAL_TEARDOWN);
        } catch (Exception e) {
            throw rethrow(e);
        }
    }

    private void createTest() {
        echo("Starting Test initialization");
        remoteClient.sendToAllWorkers(new CreateTestOperation(testIndex, testCase));
        echo("Completed Test initialization");
    }

    private void runPhase(TestPhase testPhase) {
        if (testSuite.isFailFast() && failureContainer.hasCriticalFailure(testCaseId)) {
            echo("Skipping Test " + testPhase.desc() + " (critical failure)");<commit_msg>Fixed CheckStyle issue in TestCaseRunner.<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.protocol.operation.CreateTestOperation;
import com.hazelcast.simulator.protocol.operation.StartTestOperation;
import com.hazelcast.simulator.protocol.operation.StartTestPhaseOperation;
import com.hazelcast.simulator.protocol.operation.StopTestOperation;
import com.hazelcast.simulator.protocol.registry.ComponentRegistry;
import com.hazelcast.simulator.test.TestCase;
import com.hazelcast.simulator.test.TestPhase;
import com.hazelcast.simulator.test.TestSuite;
import org.apache.log4j.Logger;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;

import static com.hazelcast.simulator.test.TestPhase.GLOBAL_TEARDOWN;
import static com.hazelcast.simulator.test.TestPhase.GLOBAL_VERIFY;
import static com.hazelcast.simulator.test.TestPhase.GLOBAL_WARMUP;
import static com.hazelcast.simulator.test.TestPhase.LOCAL_TEARDOWN;
import static com.hazelcast.simulator.test.TestPhase.LOCAL_VERIFY;
import static com.hazelcast.simulator.test.TestPhase.LOCAL_WARMUP;
import static com.hazelcast.simulator.test.TestPhase.RUN;
import static com.hazelcast.simulator.test.TestPhase.SETUP;
import static com.hazelcast.simulator.utils.CommonUtils.getElapsedSeconds;
import static com.hazelcast.simulator.utils.CommonUtils.rethrow;
import static com.hazelcast.simulator.utils.CommonUtils.sleepSeconds;
import static com.hazelcast.simulator.utils.FormatUtils.formatPercentage;
import static com.hazelcast.simulator.utils.FormatUtils.padRight;
import static com.hazelcast.simulator.utils.FormatUtils.secondsToHuman;
import static java.lang.String.format;

/**
 * Responsible for running a single {@link TestCase}.
 *
 * Multiple TestCases can be run in parallel, by having multiple TestCaseRunners in parallel.
 */
final class TestCaseRunner implements TestPhaseListener {

    private static final int RUN_PHASE_LOG_INTERVAL_SECONDS = 30;
    private static final int WAIT_FOR_PHASE_COMPLETION_LOG_INTERVAL_SECONDS = 30;

    private static final Logger LOGGER = Logger.getLogger(TestCaseRunner.class);
    private static final ConcurrentMap<TestPhase, Object> LOG_TEST_PHASE_COMPLETION = new ConcurrentHashMap<TestPhase, Object>();

    private final ConcurrentMap<TestPhase, AtomicInteger> phaseCompletedMap = new ConcurrentHashMap<TestPhase, AtomicInteger>();
    private final CountDownLatch waitForStopThread = new CountDownLatch(1);

    private final int testIndex;
    private final TestCase testCase;
    private final String testCaseId;
    private final TestSuite testSuite;

    private final RemoteClient remoteClient;
    private final FailureContainer failureContainer;
    private final PerformanceStateContainer performanceStateContainer;
    private final ComponentRegistry componentRegistry;

    private final String prefix;
    private final ConcurrentMap<TestPhase, CountDownLatch> testPhaseSyncMap;

    private final boolean isVerifyEnabled;
    private final boolean isPassiveMembers;

    private final boolean monitorPerformance;
    private final int logPerformanceIntervalSeconds;
    private final int logRunPhaseIntervalSeconds;

    TestCaseRunner(int testIndex, TestCase testCase, Coordinator coordinator, int paddingLength,
                   ConcurrentMap<TestPhase, CountDownLatch> testPhaseSyncMap) {
        this.testIndex = testIndex;
        this.testCase = testCase;
        this.testCaseId = testCase.getId();
        this.testSuite = coordinator.getTestSuite();

        this.remoteClient = coordinator.getRemoteClient();
        this.failureContainer = coordinator.getFailureContainer();
        this.performanceStateContainer = coordinator.getPerformanceStateContainer();
        this.componentRegistry = coordinator.getComponentRegistry();

        this.prefix = padRight(testCaseId, paddingLength + 1);
        this.testPhaseSyncMap = testPhaseSyncMap;

        CoordinatorParameters coordinatorParameters = coordinator.getCoordinatorParameters();
        this.isVerifyEnabled = coordinatorParameters.isVerifyEnabled();

        ClusterLayoutParameters clusterLayoutParameters = coordinator.getClusterLayoutParameters();
        this.isPassiveMembers = (coordinatorParameters.isPassiveMembers() && clusterLayoutParameters.getClientWorkerCount() > 0);

        WorkerParameters workerParameters = coordinator.getWorkerParameters();
        this.monitorPerformance = workerParameters.isMonitorPerformance();
        this.logPerformanceIntervalSeconds = workerParameters.getWorkerPerformanceMonitorIntervalSeconds();
        this.logRunPhaseIntervalSeconds = workerParameters.getRunPhaseLogIntervalSeconds(RUN_PHASE_LOG_INTERVAL_SECONDS);

        for (TestPhase testPhase : TestPhase.values()) {
            phaseCompletedMap.put(testPhase, new AtomicInteger());
        }
    }

    @Override
    public void completed(TestPhase testPhase) {
        phaseCompletedMap.get(testPhase).incrementAndGet();
    }

    void run() {
        try {
            initPerformanceMonitor();
            createTest();
            runPhase(SETUP);

            runPhase(LOCAL_WARMUP);
            runPhase(GLOBAL_WARMUP);

            startTest();
            waitForTestCompletion();

            if (isVerifyEnabled) {
                runPhase(GLOBAL_VERIFY);
                runPhase(LOCAL_VERIFY);
            } else {
                echo("Skipping Test verification");
            }

            runPhase(GLOBAL_TEARDOWN);
            runPhase(LOCAL_TEARDOWN);
        } catch (Exception e) {
            throw rethrow(e);
        }
    }

    private void initPerformanceMonitor() {
        if (monitorPerformance) {
            performanceStateContainer.init(testCaseId);
        }
    }

    private void createTest() {
        echo("Starting Test initialization");
        remoteClient.sendToAllWorkers(new CreateTestOperation(testIndex, testCase));
        echo("Completed Test initialization");
    }

    private void runPhase(TestPhase testPhase) {
        if (testSuite.isFailFast() && failureContainer.hasCriticalFailure(testCaseId)) {
            echo("Skipping Test " + testPhase.desc() + " (critical failure)");
{'fraction_non_alphanumeric': 0.07765289738724274, 'fraction_numerical': 0.002919281856663261, 'mean_word_length': 4.14763335837716, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 17, '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': '6081206', 'n_tokens_mistral': 3676, 'n_tokens_neox': 3560, 'n_words': 815}