<commit_before>/*
 *  Copyright (c) 2016-2017, Salesforce.com, Inc.
 *  All rights reserved.
 *  Licensed under the BSD 3-Clause license.
 *  For full license text, see the LICENSE.txt file in repo root
 *    or https://opensource.org/licenses/BSD-3-Clause
 */

package com.salesforce.pyplyn.duct.etl.configuration;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.salesforce.pyplyn.configuration.Configuration;
import com.salesforce.pyplyn.configuration.UpdatableConfigurationSetProvider;
import com.salesforce.pyplyn.status.MeterType;
import com.salesforce.pyplyn.status.SystemStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

import static java.util.Objects.isNull;

/**
 * Loads all configurations, disregarding any logic to split on multiple hosts
 *   annotated as Singleton as there should only be one instance of this class in operation
 *
 * @author <NAME> &lt;<EMAIL>&gt;
 * @since 3.0
 */
@Singleton
public class SinglePartitionConfigurationProvider implements UpdatableConfigurationSetProvider<ConfigurationWrapper> {
    protected static final Logger logger = LoggerFactory.getLogger(SinglePartitionConfigurationProvider.class);
    private static final String PROCESS_NAME = "ConfigurationProvider";
    private final Lock updateLock = new ReentrantLock();
    protected final ConfigurationProvider provider;
    private final SystemStatus systemStatus;
    protected Map<Configuration, ConfigurationWrapper> configurations = new HashMap<>();


    /**
     * Default constructor
     */
    @Inject
    public SinglePartitionConfigurationProvider(ConfigurationProvider provider, SystemStatus systemStatus) {
        this.provider = provider;
        this.systemStatus = systemStatus;
    }

    /**
     * Updates the known configuration list
     * <p/>
     * <p/>This implementation acquires a lock to guard against multiple threads attempting to update at the same time.
     */
    @Override
    public void updateConfigurations() {
        try {
            // attempt to acquire a lock and update the list of configurations
            if (updateLock.tryLock(100, TimeUnit.MILLISECONDS)) {
                // make a copy of all current elements
                Map<Configuration, ConfigurationWrapper> oldEntries = ImmutableMap.copyOf(configurations);

                try {
                    // merge new configurations into existing map
                    mergeConfigurationSets(provider.get(), configurations);

                } catch (RuntimeException e) {
                    logger.error("Unexpected error updating configurations!", e);
                    markFailure();

                    // if we could not update configurations, we will restore the old set
                    restoreBackedUpEntries(oldEntries);

                } finally {
                    updateLock.unlock();
                }
            }

        } catch (InterruptedException e) {
            // nothing to do, either the program is stopping or another thread was updating the configurations
            logger.warn("Could not acquire lock to update configurations!", e);<commit_msg>Added more logging in configuration update process<commit_after>/*
 *  Copyright (c) 2016-2017, Salesforce.com, Inc.
 *  All rights reserved.
 *  Licensed under the BSD 3-Clause license.
 *  For full license text, see the LICENSE.txt file in repo root
 *    or https://opensource.org/licenses/BSD-3-Clause
 */

package com.salesforce.pyplyn.duct.etl.configuration;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.salesforce.pyplyn.configuration.Configuration;
import com.salesforce.pyplyn.configuration.UpdatableConfigurationSetProvider;
import com.salesforce.pyplyn.status.MeterType;
import com.salesforce.pyplyn.status.SystemStatus;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

/**
 * Loads all configurations, disregarding any logic to split on multiple hosts
 *   annotated as Singleton as there should only be one instance of this class in operation
 *
 * @author <NAME> &lt;<EMAIL>&gt;
 * @since 3.0
 */
@Singleton
public class SinglePartitionConfigurationProvider implements UpdatableConfigurationSetProvider<ConfigurationWrapper> {
    protected static final Logger logger = LoggerFactory.getLogger(SinglePartitionConfigurationProvider.class);
    private static final String PROCESS_NAME = "ConfigurationProvider";
    private final Lock updateLock = new ReentrantLock();
    protected final ConfigurationProvider provider;
    private final SystemStatus systemStatus;
    protected Map<Configuration, ConfigurationWrapper> configurations = new HashMap<>();


    /**
     * Default constructor
     */
    @Inject
    public SinglePartitionConfigurationProvider(ConfigurationProvider provider, SystemStatus systemStatus) {
        this.provider = provider;
        this.systemStatus = systemStatus;
    }

    /**
     * Updates the known configuration list
     * <p/>
     * <p/>This implementation acquires a lock to guard against multiple threads attempting to update at the same time.
     */
    @Override
    public void updateConfigurations() {
        try {
            // attempt to acquire a lock and update the list of configurations
            logger.info("Attempting configuration update");
            if (updateLock.tryLock(1000, TimeUnit.MILLISECONDS)) {
                Map<Configuration, ConfigurationWrapper> oldEntries = null;

                try {
                    // make a copy of all current elements
                    oldEntries = ImmutableMap.copyOf(configurations);

                    // merge new configurations into existing map
                    logger.info("Merging configurations");
                    mergeConfigurationSets(provider.get(), configurations);

                } catch (RuntimeException e) {
                    logger.error("Unexpected error updating configurations!", e);
                    markFailure();

                    // if we could not update configurations, we will restore the old set
                    if (nonNull(oldEntries)) {
                        restoreBackedUpEntries(oldEntries);
                    }

                } finally {
                    updateLock.unlock();
                }

            } else {
                logger.warn("Could not acquire lock to update configurations!");
            }

        } catch (InterruptedException e) {
            // nothing to do, either the program is stopping or another thread was updating the configurations
            logger.warn("Interrupted while trying to acquire lock to update configurations!", e);
{'fraction_non_alphanumeric': 0.07971596998928189, 'fraction_numerical': 0.004689174705251876, 'mean_word_length': 2.9248159831756047, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, 'https://': 2, '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': '3878700', 'n_tokens_mistral': 1866, 'n_tokens_neox': 1786, 'n_words': 551}