<commit_before>/*
 * Copyright 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package net.uncontended.precipice.circuit.experimental;

import net.uncontended.precipice.Failable;
import net.uncontended.precipice.metrics.IntervalIterator;
import net.uncontended.precipice.metrics.Rolling;
import net.uncontended.precipice.metrics.counts.PartitionedCount;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public class DefaultHealthChecker implements HealthChecker {

    private AtomicLong lastHealthNanoTime;
    private ArrayList<InternalGauge<?>> gauges = new ArrayList<>();
    private HealthSnapshot health = new HealthSnapshot(0, 0);

    private final long healthRefreshNanos;
    private final long trailingPeriodNanos;
    private final long failureThreshold;
    private final int failurePercentageThreshold;
    private final long sampleSizeThreshold;

    public DefaultHealthChecker() {
        lastHealthNanoTime = new AtomicLong(System.nanoTime());
        healthRefreshNanos = 0;
        trailingPeriodNanos = 0;
        failureThreshold = 0;
        failurePercentageThreshold = 0;
        sampleSizeThreshold = 0;
    }

    @Override
    public boolean isHealthy(long nanoTime) {
        HealthSnapshot health = getHealthSnapshot(nanoTime);

        long failures = health.failures;
        int failurePercentage = health.failurePercentage;
        if (failureThreshold < failures || failurePercentageThreshold < failurePercentage &&
                sampleSizeThreshold < health.total) {
            return false;
        }
        return true;
    }

    private HealthSnapshot getHealthSnapshot(long currentNanoTime) {
        long lastHealthNanoTime = this.lastHealthNanoTime.get();
        if (currentNanoTime - (lastHealthNanoTime + healthRefreshNanos) > 0) {
            if (this.lastHealthNanoTime.compareAndSet(lastHealthNanoTime, currentNanoTime)) {
                HealthSnapshot newHealth = getHealth(trailingPeriodNanos, currentNanoTime);
                health = newHealth;
                return newHealth;
            }
        }
        return health;
    }

    private synchronized HealthSnapshot getHealth(long trailingPeriodNanos, long nanoTime) {
        long total = 0;
        long failures = 0;<commit_msg>Iron out some details of the health checker<commit_after>/*
 * Copyright 2016 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package net.uncontended.precipice.circuit.experimental;

import net.uncontended.precipice.Failable;
import net.uncontended.precipice.metrics.IntervalIterator;
import net.uncontended.precipice.metrics.Rolling;
import net.uncontended.precipice.metrics.counts.PartitionedCount;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

public class DefaultHealthChecker implements HealthChecker {

    private final long healthRefreshNanos;
    private final long trailingPeriodNanos;
    private final long failureThreshold;
    private final int failurePercentageThreshold;
    private final long sampleSizeThreshold;

    private final AtomicLong lastHealthNanoTime = new AtomicLong();
    private final ArrayList<InternalGauge<?>> gauges = new ArrayList<>();

    private volatile HealthSnapshot health = new HealthSnapshot(0, 0);

    public DefaultHealthChecker(long healthRefreshNanos, long trailingPeriodNanos, long failureThreshold,
                                int failurePercentageThreshold, long sampleSizeThreshold) {
        this.healthRefreshNanos = healthRefreshNanos;
        this.trailingPeriodNanos = trailingPeriodNanos;
        this.failureThreshold = failureThreshold;
        this.failurePercentageThreshold = failurePercentageThreshold;
        this.sampleSizeThreshold = sampleSizeThreshold;
    }

    @Override
    public boolean isHealthy(long nanoTime) {
        HealthSnapshot health = getHealthSnapshot(nanoTime);

        long failures = health.failures;
        int failurePercentage = health.failurePercentage;
        if (failureThreshold < failures || failurePercentageThreshold < failurePercentage &&
                sampleSizeThreshold < health.total) {
            return false;
        }
        return true;
    }

    public void init(long nanoTime) {
        lastHealthNanoTime.set(nanoTime);
    }

    private HealthSnapshot getHealthSnapshot(long currentNanoTime) {
        long lastHealthNanoTime = this.lastHealthNanoTime.get();
        if (currentNanoTime - (lastHealthNanoTime + healthRefreshNanos) > 0) {
            if (this.lastHealthNanoTime.compareAndSet(lastHealthNanoTime, currentNanoTime)) {
                HealthSnapshot newHealth = getHealth(trailingPeriodNanos, currentNanoTime);
                health = newHealth;
                return newHealth;
            }
        }
        return health;
    }

    private synchronized HealthSnapshot getHealth(long trailingPeriodNanos, long nanoTime) {
        long total = 0;
        long failures = 0;
{'fraction_non_alphanumeric': 0.06890284203569068, 'fraction_numerical': 0.005122273628552544, 'mean_word_length': 3.389412617839014, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5330888', 'n_tokens_mistral': 1565, 'n_tokens_neox': 1508, 'n_words': 452}