<commit_before>package foundation.stack.jdbc;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import com.github.dockerjava.core.command.PullImageResultCallback;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import foundation.stack.docker.Bootstrap;
import foundation.stack.docker.DockerClient;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;

/**
 * @author <NAME> (<EMAIL>)
 */
public abstract class DockerDatabaseServerContainerReferenceManager<ReferenceType> {

    private static Logger logger = Logger.getLogger(DockerDatabaseServerContainerReferenceManager.class.getName());

    private static final String MYSQL_IMAGE_NAME_PROPERTY = "MYSQL_IMAGE_NAME";
    private static final String MYSQL_IMAGE_NAME = "mysql";

    private static final String MYSQL_IMAGE_TAG_PROPERTY = "MYSQL_IMAGE_TAG";
    private static final String MYSQL_VERSION = "5.7";

    private static final String ROOT_PASSWORD_PROPERTY = "ROOT_PASSWORD";
    private static final String DEFAULT_ROOT_PASSWORD = null;

    private static final String APPLICATION_USER_NAME_PROPERTY = "APPLICATION_USER_NAME";
    private static final String DEFAULT_APPLICATION_USER_NAME = null;

    private static final String APPLICATION_USER_PASSWORD_PROPERTY = "APPLICATION_USER_PASSWORD";
    private static final String DEFAULT_APPLICATION_PASSWORD = null;

    private static final String DOCKER_HOST_NAME = "stackfoundation";

    private static final String BYPASS_INSTALLATION = "BYPASS_INSTALLATION";

    private static int findFreePort() throws IOException {
        try (ServerSocket portSearch = new ServerSocket(0)) {
            return portSearch.getLocalPort();
        }
    }

    private static void suppressDockerClientVerboseLogging() {
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        context.getLogger("org.apache.http").setLevel(Level.ERROR);
        context.getLogger(PullImageResultCallback.class).setLevel(Level.ERROR);
    }

    private static void waitBrieflyForDatabaseServerConnect(String containerIp, int sqlServerPort) throws InterruptedException {
        int retries = 10;
        boolean connected = false;
        while (!connected) {
            try (Socket clientConnect = new Socket(containerIp, sqlServerPort)) {
                connected = true;
                logger.log(java.util.logging.Level.FINE, "Successfully connected to DB on {0}:{1}", new Object[]{containerIp, sqlServerPort});
            } catch (IOException e) {
                Thread.sleep(100);
                if (retries == 0) {
                    break;
                }

                retries--;
            }
        }
    }

    private final LoadingCache<ContainerReferenceKey, ReferenceType> databaseServerContainerReferences =
            CacheBuilder.newBuilder().build(new CacheLoader<ContainerReferenceKey, ReferenceType>() {
                @Override
                public ReferenceType load(ContainerReferenceKey key) throws Exception {
                    return createReference(createContainerAndGetConnectionString(key.getApplicationName(), key.getProgressMonitor()));
                }
            });

    protected abstract ReferenceType createReference(String connectionString);

    protected String getApplicationUserPassword(String applicationName) {
        return System.getProperty(APPLICATION_USER_PASSWORD_PROPERTY, DEFAULT_APPLICATION_PASSWORD);
    }

    protected String getApplicationUserName(String applicationName) {
        return System.getProperty(APPLICATION_USER_NAME_PROPERTY, DEFAULT_APPLICATION_USER_NAME);
    }

    protected String getRootPassword(String applicationName) {
        return System.getProperty(ROOT_PASSWORD_PROPERTY, DEFAULT_ROOT_PASSWORD);
    }

    private String createContainerAndGetConnectionString(String applicationName, ProgressMonitor progressMonitor) throws Exception {
        suppressDockerClientVerboseLogging();

        progressMonitor.workChanged(5, 100);
        progressMonitor.statusChanged("STATUS_LOCAL_DOCKER_MACHINE_BOOT");

        DockerClient dockerClient = Bootstrap.bootstrapAndConnect(DOCKER_HOST_NAME, System.getenv().containsKey(BYPASS_INSTALLATION));
        String imageName = System.getProperty(MYSQL_IMAGE_NAME_PROPERTY, MYSQL_IMAGE_NAME);
        String versionTag = System.getProperty(MYSQL_IMAGE_TAG_PROPERTY, MYSQL_VERSION);

        boolean imageExists = DockerUtilities.doesDockerHostHaveImage(dockerClient, imageName, versionTag);
        if (!imageExists) {
            progressMonitor.workChanged(45, 100);
            progressMonitor.statusChanged("STATUS_LOCAL_DOCKER_IMAGE_PULL");

            DockerUtilities.pullImageToDockerHost(dockerClient, imageName, versionTag);
        }<commit_msg>Increase db connection is ready by increasing the retry mechanism.<commit_after>package foundation.stack.jdbc;

import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.LoggerContext;
import com.github.dockerjava.core.command.PullImageResultCallback;
import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import foundation.stack.docker.Bootstrap;
import foundation.stack.docker.DockerClient;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutionException;
import java.util.logging.Logger;

/**
 * @author <NAME> (<EMAIL>)
 */
public abstract class DockerDatabaseServerContainerReferenceManager<ReferenceType> {

    private static Logger logger = Logger.getLogger(DockerDatabaseServerContainerReferenceManager.class.getName());

    private static final String MYSQL_IMAGE_NAME_PROPERTY = "MYSQL_IMAGE_NAME";
    private static final String MYSQL_IMAGE_NAME = "mysql";

    private static final String MYSQL_IMAGE_TAG_PROPERTY = "MYSQL_IMAGE_TAG";
    private static final String MYSQL_VERSION = "5.7";

    private static final String ROOT_PASSWORD_PROPERTY = "ROOT_PASSWORD";
    private static final String DEFAULT_ROOT_PASSWORD = null;

    private static final String APPLICATION_USER_NAME_PROPERTY = "APPLICATION_USER_NAME";
    private static final String DEFAULT_APPLICATION_USER_NAME = null;

    private static final String APPLICATION_USER_PASSWORD_PROPERTY = "APPLICATION_USER_PASSWORD";
    private static final String DEFAULT_APPLICATION_PASSWORD = null;

    private static final String DOCKER_HOST_NAME = "stackfoundation";

    private static final String BYPASS_INSTALLATION = "BYPASS_INSTALLATION";

    private static int findFreePort() throws IOException {
        try (ServerSocket portSearch = new ServerSocket(0)) {
            return portSearch.getLocalPort();
        }
    }

    private static void suppressDockerClientVerboseLogging() {
        LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
        context.getLogger("org.apache.http").setLevel(Level.ERROR);
        context.getLogger(PullImageResultCallback.class).setLevel(Level.ERROR);
    }

    private void waitBrieflyForDatabaseServerConnect(String containerIp, int sqlServerPort) throws InterruptedException {
        int retries = 100;
        boolean connected = false;
        logger.log(java.util.logging.Level.INFO, "Trying to connect to DB..");
        while (!connected) {
            try (Socket clientConnect = new Socket(containerIp, sqlServerPort)) {
                connected = true;
                logger.log(java.util.logging.Level.INFO, "Successfully connected to DB on {0}:{1} after {2} retries", new Object[]{containerIp, sqlServerPort, (100 - retries)});
            } catch (IOException e) {
                Thread.sleep(100);
                if (retries == 0) {
                    break;
                }
                retries--;
            }
        }
        if (!connected) {
            logger.log(java.util.logging.Level.INFO, "Could not connect to database!!");
        }
    }

    private final LoadingCache<ContainerReferenceKey, ReferenceType> databaseServerContainerReferences =
            CacheBuilder.newBuilder().build(new CacheLoader<ContainerReferenceKey, ReferenceType>() {
                @Override
                public ReferenceType load(ContainerReferenceKey key) throws Exception {
                    return createReference(createContainerAndGetConnectionString(key.getApplicationName(), key.getProgressMonitor()));
                }
            });

    protected abstract ReferenceType createReference(String connectionString);

    protected String getApplicationUserPassword(String applicationName) {
        return System.getProperty(APPLICATION_USER_PASSWORD_PROPERTY, DEFAULT_APPLICATION_PASSWORD);
    }

    protected String getApplicationUserName(String applicationName) {
        return System.getProperty(APPLICATION_USER_NAME_PROPERTY, DEFAULT_APPLICATION_USER_NAME);
    }

    protected String getRootPassword(String applicationName) {
        return System.getProperty(ROOT_PASSWORD_PROPERTY, DEFAULT_ROOT_PASSWORD);
    }

    private String createContainerAndGetConnectionString(String applicationName, ProgressMonitor progressMonitor) throws Exception {
        logger.log(java.util.logging.Level.INFO, "Generating container string for application {0}", applicationName);

        suppressDockerClientVerboseLogging();

        progressMonitor.workChanged(5, 100);
        progressMonitor.statusChanged("STATUS_LOCAL_DOCKER_MACHINE_BOOT");

        DockerClient dockerClient = Bootstrap.bootstrapAndConnect(DOCKER_HOST_NAME, System.getenv().containsKey(BYPASS_INSTALLATION));
        String imageName = System.getProperty(MYSQL_IMAGE_NAME_PROPERTY, MYSQL_IMAGE_NAME);
        String versionTag = System.getProperty(MYSQL_IMAGE_TAG_PROPERTY, MYSQL_VERSION);

        boolean imageExists = DockerUtilities.doesDockerHostHaveImage(dockerClient, imageName, versionTag);
        if (!imageExists) {
            progressMonitor.workChanged(45, 100);
            progressMonitor.statusChanged("STATUS_LOCAL_DOCKER_IMAGE_PULL");

            DockerUtilities.pullImageToDockerHost(dockerClient, imageName, versionTag);
        }
{'fraction_non_alphanumeric': 0.07819722650231124, 'fraction_numerical': 0.004622496147919877, 'mean_word_length': 3.9032105760151086, 'pattern_counts': {'":': 0, '<': 13, '<?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': '683869', 'n_tokens_mistral': 2725, 'n_tokens_neox': 2629, 'n_words': 559}