<commit_before>package org.apache.mesos.mini;

import com.github.dockerjava.api.InternalServerErrorException;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.model.PortBinding;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.log4j.Logger;
import org.apache.mesos.mini.docker.DockerProxy;
import org.apache.mesos.mini.docker.DockerUtil;
import org.apache.mesos.mini.docker.PrivateDockerRegistry;
import org.apache.mesos.mini.mesos.MesosClusterConfig;
import org.apache.mesos.mini.mesos.MesosContainer;
import org.apache.mesos.mini.state.State;
import org.apache.mesos.mini.util.MesosClusterStateResponse;
import org.apache.mesos.mini.util.Predicate;
import org.json.JSONObject;
import org.junit.rules.ExternalResource;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import static com.jayway.awaitility.Awaitility.await;

/**
 * Starts the mesos cluster. Responsible for setting up proxy and private docker registry. Once started, users can add
 * their own images to the private registry.
 */
public class MesosCluster extends ExternalResource {
    private static Logger LOGGER = Logger.getLogger(MesosCluster.class);
    private final MesosClusterConfig config;
    private MesosContainer mesosContainer;
    private DockerUtil dockerUtil;

    public MesosCluster(MesosClusterConfig config) {
        this.config = config;
        this.dockerUtil = new DockerUtil(config.dockerClient);
    }

    public void start() {
        try {
            DockerProxy dockerProxy = new DockerProxy(config.dockerClient);
            dockerProxy.start();

            // Pulls registry images and start container
            PrivateDockerRegistry privateDockerRegistry = new PrivateDockerRegistry(config.dockerClient, this.config);
            privateDockerRegistry.startPrivateRegistryContainer();

            // start the container
            mesosContainer = new MesosContainer(config.dockerClient, this.config);
            mesosContainer.startMesosLocalContainer(privateDockerRegistry.getContainerId());

            // wait until the given number of slaves are registered
            new MesosClusterStateResponse(mesosContainer.getMesosMasterURL(), config.numberOfSlaves).waitFor();

        } catch (Throwable e) {
            LOGGER.error("Error during startup", e);
            dockerUtil.stop(); // remove all created docker containers (not handled by after then re-throwing e)
            throw e;
        }
    }

    /**
     * Pull and start a docker image. This container will be destroyed when the Mesos cluster is shut down.
     *
     * @param containerName The name of the image
     * @return The id of the container
     */
    public String addAndStartContainer(String containerName) {
        DockerUtil dockerUtil = new DockerUtil(config.dockerClient);
        dockerUtil.pullImage(containerName, "latest");
        CreateContainerCmd command = config.dockerClient.createContainerCmd(containerName).withPortBindings(PortBinding.parse("0.0.0.0:80:80"));
        return dockerUtil.createAndStart(command);
    }

    public State getStateInfo() throws UnirestException {
        String json = Unirest.get("http://" + mesosContainer.getMesosMasterURL() + "/state.json").asString().getBody();

        return State.fromJSON(json);<commit_msg>Removed unecessary port binding<commit_after>package org.apache.mesos.mini;

import com.github.dockerjava.api.InternalServerErrorException;
import com.github.dockerjava.api.command.CreateContainerCmd;
import com.github.dockerjava.api.model.PortBinding;
import com.mashape.unirest.http.Unirest;
import com.mashape.unirest.http.exceptions.UnirestException;
import org.apache.log4j.Logger;
import org.apache.mesos.mini.docker.DockerProxy;
import org.apache.mesos.mini.docker.DockerUtil;
import org.apache.mesos.mini.docker.PrivateDockerRegistry;
import org.apache.mesos.mini.mesos.MesosClusterConfig;
import org.apache.mesos.mini.mesos.MesosContainer;
import org.apache.mesos.mini.state.State;
import org.apache.mesos.mini.util.MesosClusterStateResponse;
import org.apache.mesos.mini.util.Predicate;
import org.json.JSONObject;
import org.junit.rules.ExternalResource;

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import static com.jayway.awaitility.Awaitility.await;

/**
 * Starts the mesos cluster. Responsible for setting up proxy and private docker registry. Once started, users can add
 * their own images to the private registry.
 */
public class MesosCluster extends ExternalResource {
    private static Logger LOGGER = Logger.getLogger(MesosCluster.class);
    private final MesosClusterConfig config;
    private MesosContainer mesosContainer;
    private DockerUtil dockerUtil;

    public MesosCluster(MesosClusterConfig config) {
        this.config = config;
        this.dockerUtil = new DockerUtil(config.dockerClient);
    }

    public void start() {
        try {
            DockerProxy dockerProxy = new DockerProxy(config.dockerClient);
            dockerProxy.start();

            // Pulls registry images and start container
            PrivateDockerRegistry privateDockerRegistry = new PrivateDockerRegistry(config.dockerClient, this.config);
            privateDockerRegistry.startPrivateRegistryContainer();

            // start the container
            mesosContainer = new MesosContainer(config.dockerClient, this.config);
            mesosContainer.startMesosLocalContainer(privateDockerRegistry.getContainerId());

            // wait until the given number of slaves are registered
            new MesosClusterStateResponse(mesosContainer.getMesosMasterURL(), config.numberOfSlaves).waitFor();

        } catch (Throwable e) {
            LOGGER.error("Error during startup", e);
            dockerUtil.stop(); // remove all created docker containers (not handled by after then re-throwing e)
            throw e;
        }
    }

    /**
     * Pull and start a docker image. This container will be destroyed when the Mesos cluster is shut down.
     *
     * @param containerName The name of the image
     * @return The id of the container
     */
    public String addAndStartContainer(String containerName) {
        DockerUtil dockerUtil = new DockerUtil(config.dockerClient);
        dockerUtil.pullImage(containerName, "latest");
        CreateContainerCmd command = config.dockerClient.createContainerCmd(containerName);
        return dockerUtil.createAndStart(command);
    }

    public State getStateInfo() throws UnirestException {
        String json = Unirest.get("http://" + mesosContainer.getMesosMasterURL() + "/state.json").asString().getBody();

        return State.fromJSON(json);
{'fraction_non_alphanumeric': 0.08755352133471135, 'fraction_numerical': 0.0014764506127270044, 'mean_word_length': 3.681409813407049, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 1, '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': '1457393', 'n_tokens_mistral': 1911, 'n_tokens_neox': 1820, 'n_words': 432}