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

Alachisoft.NCache.Sessions.MetricsSessions.CacheSession Maven / Gradle / Ivy

The newest version!
package Alachisoft.NCache.Sessions.MetricsSessions;

import Alachisoft.NCache.DataModel.CacheConfig;
import Alachisoft.NCache.DataModel.Counter;
import Alachisoft.NCache.DataModel.CounterData;
import Alachisoft.NCache.MetricServer.MetricPublisher;
import Alachisoft.NCache.Sessions.MetricsSession;
import com.alachisoft.ncache.common.monitoring.CounterType;
import com.alachisoft.ncache.common.monitoring.Publisher;
import org.checkerframework.checker.units.qual.C;

import java.util.ArrayList;
import java.util.List;

public final class CacheSession extends MetricsSession {

    private CacheConfig cacheMetadata;
    private boolean needToPublishMetaCounters = true;

    public CacheSession(String sessionId, CacheConfig metadata, String version) {
        super(sessionId, MetricPublisher.NCache, metadata.getConfigId() + (metadata.isFromReplica() ? "_Replica" : ""));
        this.setVersion(version);
        if (metadata != null) {
            cacheMetadata = metadata;
        }
    }

    @Override
    public void publishMetric(String instanceName, CounterData counterData) {
        try {
            String counterName = extractCounterName(instanceName);
            if (counterName.equals("ncache_cpu_usage") || counterName.equals("ncache_memory_usage") || counterName.equals("ncache_network_usage")) {
                publishSystemMetrics(instanceName, counterData);
            } else {
                publishCacheMetrics(instanceName, counterData);
            }
        } catch (Exception e) {
            getLogger().Error("Metric Server", "Unable to persist Metadata for Session: " + getSessionId() + "\n" + e.toString());
        }
    }

    public void publishSystemMetrics(String instanceName, CounterData counterData) {
        metricsAgentManager.publishCounterData(instanceName, counterData, getSystemMetricsParamsList());
    }

    public void publishCacheMetrics(String instanceName, CounterData counterData) {
        metricsAgentManager.publishCounterData(instanceName, counterData, getParamsList());

        if (needToPublishMetaCounters) {
            publishTotalCacheSizeCounter();
            publishConfiguredServersCounter();
            needToPublishMetaCounters = false;
        }
    }

    private void publishConfiguredServersCounter() {
        Counter counter = createCounter("configured_servers_count", "Total number of configured servers", CounterType.NumberOfItemCounter, Publisher.NCache);
        CounterData counterData = new CounterData();
        counterData.setData(cacheMetadata.getConfiguredServersCount());
        counterData.setCounterMetadata(counter);
        String instanceName = getCounterInstanceName(getCounterNameWithLiteral(counter.getName(), counter.getCategory()));
        metricsAgentManager.publishCounterData(instanceName, counterData, getConfiguredServersParamsList());
    }

    private void publishTotalCacheSizeCounter() {
        Counter counter = createCounter("total_cache_size", "Total size of the cache", CounterType.NumberOfItemCounter, Publisher.NCache);
        CounterData counterData = new CounterData();
        counterData.setData(cacheMetadata.getCacheSize());
        counterData.setCounterMetadata(counter);
        String instanceName = getCounterInstanceName(getCounterNameWithLiteral(counter.getName(), counter.getCategory()));
        metricsAgentManager.publishCounterData(instanceName, counterData, getParamsList());
    }

    @Override
    protected List getParamsList() {
        List list = new ArrayList<>();
        list.add(cacheMetadata.getCacheId());
        list.add(cacheMetadata.getConfigId());
        list.add(cacheMetadata.getClientServerIP());
        list.add(Boolean.toString(cacheMetadata.isFromReplica()));
        return list;
    }

    private List getSystemMetricsParamsList() {
        List list = new ArrayList<>();
        list.add(cacheMetadata.getClusterIP());
        list.add(cacheMetadata.getInstallationType());
        return list;
    }

    private List getConfiguredServersParamsList() {
        List list = getParamsList();
        list.add(cacheMetadata.getConfiguredServers());
        return list;
    }

    private Counter createCounter(String counterName, String description, CounterType type, Publisher publisher) {
        Counter counter =  new Counter();
        counter.setName(counterName);
        counter.setType(type);
        counter.setName(description);
        counter.setCategory(publisher);

        return counter;
    }

    @Override
    public void dispose() {
        synchronized (counters) {
            for (String counterInstance : counters) {
                if (counterInstance.contains("configured_servers_count")) {
                    metricsAgentManager.disposeCounterInstance(counterInstance, getConfiguredServersParamsList());
                } else if (counterInstance.contains("ncache_cpu_usage") || counterInstance.contains("ncache_memory_usage") || counterInstance.contains("ncache_network_usage")) {
                    metricsAgentManager.disposeCounterInstance(counterInstance, getSystemMetricsParamsList());
                }
                metricsAgentManager.disposeCounterInstance(counterInstance, getParamsList());
            }
        }
    }

    private String extractCounterName(String instanceName) {
        String result = "";
        try {
            result = instanceName.split(":")[1];
        } catch (Exception e) {
        }
        return result;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy