All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.alibaba.csp.sentinel.cluster.flow.statistic.metric.ClusterParamMetric Maven / Gradle / Ivy

/*
 * Copyright 1999-2018 Alibaba Group Holding Ltd.
 *
 * 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 com.alibaba.csp.sentinel.cluster.flow.statistic.metric;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import com.alibaba.csp.sentinel.cluster.flow.statistic.data.ClusterFlowEvent;
import com.alibaba.csp.sentinel.slots.statistic.base.LongAdder;
import com.alibaba.csp.sentinel.slots.statistic.cache.CacheMap;
import com.alibaba.csp.sentinel.util.AssertUtil;

/**
 * @author Eric Zhao
 * @since 1.4.0
 */
public class ClusterParamMetric {

    public static final int DEFAULT_CLUSTER_MAX_CAPACITY = 4000;

    private final ClusterParameterLeapArray metric;

    public ClusterParamMetric(int sampleCount, int intervalInMs) {
        this(sampleCount, intervalInMs, DEFAULT_CLUSTER_MAX_CAPACITY);
    }

    public ClusterParamMetric(int sampleCount, int intervalInMs, int maxCapacity) {
        AssertUtil.isTrue(sampleCount > 0, "sampleCount should be positive");
        AssertUtil.isTrue(intervalInMs > 0, "interval should be positive");
        AssertUtil.isTrue(intervalInMs % sampleCount == 0, "time span needs to be evenly divided");
        this.metric = new ClusterParameterLeapArray<>(sampleCount, intervalInMs, maxCapacity);
    }

    public long getSum(Object value) {
        if (value == null) {
            return 0;
        }

        metric.currentWindow();
        long sum = 0;

        List> buckets = metric.values();
        for (CacheMap bucket : buckets) {
            long count = getCount(bucket.get(value));
            sum += count;
        }
        return sum;
    }

    private long getCount(/*@Nullable*/ LongAdder adder) {
        return adder == null ? 0 : adder.sum();
    }

    public void addValue(Object value, int count) {
        if (value == null) {
            return;
        }
        CacheMap data = metric.currentWindow().value();
        LongAdder newCounter = new LongAdder();
        LongAdder currentCounter = data.putIfAbsent(value, newCounter);
        if (currentCounter != null) {
            currentCounter.add(count);
        } else {
            newCounter.add(count);
        }
    }

    public double getAvg(Object value) {
        return getSum(value) / metric.getIntervalInSecond();
    }

    public Map getTopValues(int number) {
        metric.currentWindow();
        List> buckets = metric.values();

        Map result = new HashMap<>(buckets.size());

        for (CacheMap b : buckets) {
            Set subSet = b.keySet(true);
            for (Object o : subSet) {
                Long count = result.get(o);
                if (count == null) {
                    count = getCount(b.get(o));
                } else {
                    count += getCount(b.get(o));
                }
                result.put(o, count);
            }
        }

        // After merge, get the top set one.
        Set> set = result.entrySet();
        List> list = new ArrayList<>(set);
        Collections.sort(list, new Comparator>() {
            @Override
            public int compare(Entry a,
                               Entry b) {
                return (int)(b.getValue() == null ? 0 : b.getValue()) - (int)(a.getValue() == null ? 0 : a.getValue());
            }
        });

        Map doubleResult = new HashMap();

        int size = list.size() > number ? number : list.size();
        for (int i = 0; i < size; i++) {
            Map.Entry x = list.get(i);
            if (x.getValue() == 0) {
                break;
            }
            doubleResult.put(x.getKey(), ((double)x.getValue()) / metric.getIntervalInSecond());
        }

        return doubleResult;
    }
}