<commit_before>package com.atlassian.jira.plugins.dvcs.sync.impl;

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;

import com.atlassian.jira.plugins.dvcs.model.DefaultProgress;
import com.atlassian.jira.plugins.dvcs.model.Progress;
import com.atlassian.jira.plugins.dvcs.model.Repository;
import com.atlassian.jira.plugins.dvcs.smartcommits.SmartcommitsChangesetsProcessor;
import com.atlassian.jira.plugins.dvcs.sync.SynchronisationOperation;
import com.atlassian.jira.plugins.dvcs.sync.Synchronizer;
import com.google.common.collect.MapMaker;

/**
 * Synchronization services
 */
public class DefaultSynchronizer implements Synchronizer, DisposableBean
{
    private final Logger log = LoggerFactory.getLogger(DefaultSynchronizer.class);

    private final ExecutorService executorService;
    private final SmartcommitsChangesetsProcessor smartcommitsChangesetsProcessor;


    public DefaultSynchronizer(ExecutorService executorService,
            SmartcommitsChangesetsProcessor smartcommitsChangesetsProcessor)
    {
        this.executorService = executorService;
        this.smartcommitsChangesetsProcessor = smartcommitsChangesetsProcessor;
    }

    // map of ALL Synchronisation Progresses - running and finished ones
    private final ConcurrentMap<Integer, Progress> progressMap = new MapMaker().makeMap();


    @Override
    public void synchronize(Repository repository, SynchronisationOperation operation)
    {//TODO this and the row 63 is not really thread safe as it represents atomic operation that is not executed atomically
        Progress progress = progressMap.get(repository.getId());
        //TODO isShouldStop really necessary? should we create a queue even if those conditions are not met?
        if (progress==null || progress.isFinished() || progress.isShouldStop())
        {
            addSynchronisationOperation(repository, operation);
        }
    }

    @Override
    public void stopSynchronization(Repository repository)
    {
        Progress progress = progressMap.get(repository.getId());
        if (progress!=null)
        {
            progress.setShouldStop(true);
        }
    }

    private void addSynchronisationOperation(final Repository repository, final SynchronisationOperation operation)
    {
        final DefaultProgress progress = operation.getProgress();
        progressMap.put(repository.getId(), progress);

        Runnable runnable = new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    progress.start();

                    if (progress.isShouldStop())
                    {
                        return;
                    }

                    operation.synchronise();

                    // at the end of execution
                    if (operation.isSoftSync())
                    {
                        smartcommitsChangesetsProcessor.startProcess();
                    }
                    //

                } catch (Throwable e)
                {
                    String errorMessage = e.getMessage() == null ? e.toString() : e.getMessage();
                    progress.setError(errorMessage);
                    log.warn(e.getMessage(), e);
                } finally
                {
                    progress.finish();
                }
            }
        };

        executorService.submit(runnable);

        progress.queued();
    }

    @Override
    public Progress getProgress(Repository repository)
    {
        return progressMap.get(repository.getId());
    }

    @Override
    public void putProgress(Repository repository, Progress progress)
    {
        progressMap.put(repository.getId(), progress);
    }

    @Override
    public void removeProgress(Repository repository)
    {
        progressMap.remove(repository.getId());
    }

    @Override
    public void destroy() throws Exception
    {
        for (Progress progress : progressMap.values())
        {
            progress.setShouldStop(true);
        }
        executorService.shutdown();
        executorService.awaitTermination(1, TimeUnit.MINUTES);
    }
}<commit_msg>BBC-530 Printing info message on synchronizer termination<commit_after>package com.atlassian.jira.plugins.dvcs.sync.impl;

import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;

import com.atlassian.jira.plugins.dvcs.model.DefaultProgress;
import com.atlassian.jira.plugins.dvcs.model.Progress;
import com.atlassian.jira.plugins.dvcs.model.Repository;
import com.atlassian.jira.plugins.dvcs.smartcommits.SmartcommitsChangesetsProcessor;
import com.atlassian.jira.plugins.dvcs.sync.SynchronisationOperation;
import com.atlassian.jira.plugins.dvcs.sync.Synchronizer;
import com.google.common.collect.MapMaker;

/**
 * Synchronization services
 */
public class DefaultSynchronizer implements Synchronizer, DisposableBean
{
    private final Logger log = LoggerFactory.getLogger(DefaultSynchronizer.class);

    private final ExecutorService executorService;
    private final SmartcommitsChangesetsProcessor smartcommitsChangesetsProcessor;


    public DefaultSynchronizer(ExecutorService executorService,
            SmartcommitsChangesetsProcessor smartcommitsChangesetsProcessor)
    {
        this.executorService = executorService;
        this.smartcommitsChangesetsProcessor = smartcommitsChangesetsProcessor;
    }

    // map of ALL Synchronisation Progresses - running and finished ones
    private final ConcurrentMap<Integer, Progress> progressMap = new MapMaker().makeMap();


    @Override
    public void synchronize(Repository repository, SynchronisationOperation operation)
    {//TODO this and the row 63 is not really thread safe as it represents atomic operation that is not executed atomically
        Progress progress = progressMap.get(repository.getId());
        //TODO isShouldStop really necessary? should we create a queue even if those conditions are not met?
        if (progress==null || progress.isFinished() || progress.isShouldStop())
        {
            addSynchronisationOperation(repository, operation);
        }
    }

    @Override
    public void stopSynchronization(Repository repository)
    {
        Progress progress = progressMap.get(repository.getId());
        if (progress!=null)
        {
            progress.setShouldStop(true);
        }
    }

    private void addSynchronisationOperation(final Repository repository, final SynchronisationOperation operation)
    {
        final DefaultProgress progress = operation.getProgress();
        progressMap.put(repository.getId(), progress);

        Runnable runnable = new Runnable()
        {
            @Override
            public void run()
            {
                try
                {
                    progress.start();

                    if (progress.isShouldStop())
                    {
                        return;
                    }

                    operation.synchronise();

                    // at the end of execution
                    if (operation.isSoftSync())
                    {
                        smartcommitsChangesetsProcessor.startProcess();
                    }
                    //

                } catch (Throwable e)
                {
                    String errorMessage = e.getMessage() == null ? e.toString() : e.getMessage();
                    progress.setError(errorMessage);
                    log.warn(e.getMessage(), e);
                } finally
                {
                    progress.finish();
                }
            }
        };

        executorService.submit(runnable);

        progress.queued();
    }

    @Override
    public Progress getProgress(Repository repository)
    {
        return progressMap.get(repository.getId());
    }

    @Override
    public void putProgress(Repository repository, Progress progress)
    {
        progressMap.put(repository.getId(), progress);
    }

    @Override
    public void removeProgress(Repository repository)
    {
        progressMap.remove(repository.getId());
    }

    @Override
    public void destroy() throws Exception
    {
        for (Progress progress : progressMap.values())
        {
            progress.setShouldStop(true);
        }
        executorService.shutdown();
        boolean result = executorService.awaitTermination(1, TimeUnit.MINUTES);
        log.info("Terminating Synchronizer executor returned: " + result);
    }
}
{'fraction_non_alphanumeric': 0.08171118153010412, 'fraction_numerical': 0.0014712539610683567, 'mean_word_length': 2.3410207939508507, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '6208399', 'n_tokens_mistral': 2229, 'n_tokens_neox': 2143, 'n_words': 465}