<commit_before>package com.emc.mongoose.metrics;

import com.emc.mongoose.concurrent.DaemonBase;
import com.emc.mongoose.concurrent.ServiceTaskExecutor;
import com.emc.mongoose.concurrent.ThreadDump;
import com.emc.mongoose.logging.*;
import com.github.akurilov.commons.concurrent.AsyncRunnable;
import com.github.akurilov.fiber4j.FiberBase;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.ThreadContext;

import java.rmi.RemoteException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static com.emc.mongoose.Constants.KEY_CLASS_NAME;
import static com.emc.mongoose.Constants.KEY_STEP_ID;
import static org.apache.logging.log4j.CloseableThreadContext.Instance;
import static org.apache.logging.log4j.CloseableThreadContext.put;

/**
 * Created by kurila on 18.05.17.
 */
public final class MetricsManager
        extends DaemonBase {

    private static final String CLS_NAME = MetricsManager.class.getSimpleName();
    private static final MetricsManager INSTANCE;
    private static final int TIMEOUT = 10;

    static {
        try {
            INSTANCE = new MetricsManager();
        } catch (final Throwable cause) {
            throw new AssertionError(cause);
        }
    }

    private final Map<String, Map<MetricsContext, AutoCloseable>> allMetrics = new HashMap<>();
    private final Set<MetricsContext> selectedMetrics = new TreeSet<>();
    private final Lock allMetricsLock = new ReentrantLock();

    private final AsyncRunnable task = new FiberBase(ServiceTaskExecutor.INSTANCE) {

        private long outputPeriodMillis;
        private long lastOutputTs;
        private long nextOutputTs;

        @Override
        protected final void doClose() {
        }

        @Override
        protected final void invokeTimed(final long startTimeNanos) {

            try {
                for (final String id : allMetrics.keySet()) {
                    for (final MetricsContext metricsCtx : allMetrics.get(id).keySet()) {

                        metricsCtx.refreshLastSnapshot();
                    }
                }
            } catch (final ConcurrentModificationException ignored) {
            }

            if (allMetricsLock.tryLock()) {

                ThreadContext.put(KEY_CLASS_NAME, CLS_NAME);

                try {
                    int actualConcurrency;
                    int nextConcurrencyThreshold;
                    for (final String id : allMetrics.keySet()) {
                        for (final MetricsContext metricsCtx : allMetrics.get(id).keySet()) {

                            ThreadContext.put(KEY_STEP_ID, metricsCtx.stepId());

                            actualConcurrency = metricsCtx.actualConcurrency();
                            //metricsCtx.refreshLastSnapshot();

                            // threshold load state checks
                            nextConcurrencyThreshold = metricsCtx.concurrencyThreshold();<commit_msg>resolved #1178 'Metrics manager unregister timeout' [skip ci]<commit_after>package com.emc.mongoose.metrics;

import com.emc.mongoose.concurrent.DaemonBase;
import com.emc.mongoose.concurrent.ServiceTaskExecutor;
import com.emc.mongoose.concurrent.ThreadDump;
import com.emc.mongoose.logging.*;
import com.github.akurilov.commons.concurrent.AsyncRunnable;
import com.github.akurilov.fiber4j.FiberBase;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.ThreadContext;

import java.rmi.RemoteException;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

import static com.emc.mongoose.Constants.KEY_CLASS_NAME;
import static com.emc.mongoose.Constants.KEY_STEP_ID;
import static org.apache.logging.log4j.CloseableThreadContext.Instance;
import static org.apache.logging.log4j.CloseableThreadContext.put;

/**
 * Created by kurila on 18.05.17.
 */
public final class MetricsManager
        extends DaemonBase {

    private static final String CLS_NAME = MetricsManager.class.getSimpleName();
    private static final MetricsManager INSTANCE;
    private static final int TIMEOUT = 10;

    static {
        try {
            INSTANCE = new MetricsManager();
        } catch (final Throwable cause) {
            throw new AssertionError(cause);
        }
    }

    private final Map<String, Map<MetricsContext, AutoCloseable>> allMetrics = new HashMap<>();
    private final Set<MetricsContext> selectedMetrics = new TreeSet<>();
    private final Lock allMetricsLock = new ReentrantLock();

    private final AsyncRunnable task = new FiberBase(ServiceTaskExecutor.INSTANCE) {

        private long outputPeriodMillis;
        private long lastOutputTs;
        private long nextOutputTs;

        @Override
        protected final void doClose() {
        }

        @Override
        protected final void invokeTimed(final long startTimeNanos) {

            try {
                for (final String id : allMetrics.keySet()) {
                    for (final MetricsContext metricsCtx : allMetrics.get(id).keySet()) {

                        metricsCtx.refreshLastSnapshot();
                    }
                }
            } catch (final Exception ignored) {
            }

            if (allMetricsLock.tryLock()) {

                ThreadContext.put(KEY_CLASS_NAME, CLS_NAME);

                try {
                    int actualConcurrency;
                    int nextConcurrencyThreshold;
                    for (final String id : allMetrics.keySet()) {
                        for (final MetricsContext metricsCtx : allMetrics.get(id).keySet()) {

                            ThreadContext.put(KEY_STEP_ID, metricsCtx.stepId());

                            actualConcurrency = metricsCtx.actualConcurrency();
                            //metricsCtx.refreshLastSnapshot();

                            // threshold load state checks
                            nextConcurrencyThreshold = metricsCtx.concurrencyThreshold();
{'fraction_non_alphanumeric': 0.08396319421623398, 'fraction_numerical': 0.004929346040092014, 'mean_word_length': 2.427364864864865, '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': '3106389', 'n_tokens_mistral': 1628, 'n_tokens_neox': 1577, 'n_words': 352}