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

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

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

import Alachisoft.NCache.DataModel.Client;
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.caching.statistics.customcounters.AverageCounter;
import com.alachisoft.ncache.common.caching.statistics.customcounters.NumberOfItemCounter;
import com.alachisoft.ncache.common.caching.statistics.customcounters.PerformanceCounterBase;
import com.alachisoft.ncache.common.caching.statistics.customcounters.RateOfCounter;
import com.alachisoft.ncache.common.monitoring.CounterDataCollection;
import com.alachisoft.ncache.common.monitoring.CounterMetadataCollection;
import com.alachisoft.ncache.common.monitoring.IntervalCounterDataCollection;

import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;

public class ClientSession extends MetricsSession {

    private Client clientMetadata;
    private Map convertedCustomCounters = new HashMap<>();
    private Map countersData = new HashMap<>();

    public ClientSession(String sessionId, Client clientMeta) {
        super(sessionId, MetricPublisher.Client, "");
        this.clientMetadata = clientMeta;
    }

    public Client getClientMetadata() {
        return clientMetadata;
    }

    public void convertToCustomCounters(List counters) {
        synchronized (convertedCustomCounters) {
            convertedCustomCounters.clear();
            counters.forEach(entry -> {
                switch (entry.getType()) {
                    case AverageCounter:
                        convertedCustomCounters.put(entry.getName(), new AverageCounter(entry.getName(), ""));
                        break;
                    case NumberOfItemCounter:
                        convertedCustomCounters.put(entry.getName(), new NumberOfItemCounter(entry.getName(), ""));
                        break;
                    case SampleCounter:
                    case RateOfCounter:
                        convertedCustomCounters.put(entry.getName(), new RateOfCounter(entry.getName(), ""));
                        break;
                }
            });
        }
    }

    @Override
    public void addCountersMetadata(CounterMetadataCollection counterMeta) {
        updateWriteTime();
        storeCountersMetadata(counterMeta);
        convertToCustomCounters(counterMeta.getCounters().stream()
                .map(x ->{
                    Counter counter = new Counter();
                    counter.setCategory(counterMeta.getCategory());
                    counter.setDescription(x.getDescription());
                    counter.setName(x.getName());
                    counter.setType(x.getType());
                    return counter;
                }).collect(Collectors.toList()));
    }

    public void storeInmemory() {
        convertToCounters();
    }

    public void accumulateCounters(CounterData counterData, short id) {
        try {
            synchronized (convertedCustomCounters) {
                if (convertedCustomCounters != null) {
                    PerformanceCounterBase customCounter = convertedCustomCounters.get(getCounterIDMapCommon().getCounterName(id));
                    if (customCounter != null) {
                        customCounter.incrementBy(counterData.getData());
                    }
                }
            }
        } catch (Exception e) {
            getLogger().Error("Accumulate Counters, Client Session", e.toString());
            getLogger().Info("counterData: " + counterData + " id: " + id);
        }
    }

    public void convertToCounters() {
        List counterDatas = new ArrayList<>();
        synchronized (convertedCustomCounters) {
            Iterator> iterator = convertedCustomCounters.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = iterator.next();
                PerformanceCounterBase customCounter = entry.getValue();
                String counterName = entry.getKey();
                short id = getCounterIDMapCommon().getCounterID(counterName);

                CounterData counterData = new CounterData();
                counterData.setData(customCounter.getValue());
                counterData.setCounterId(id);
                counterData.setTimestamp(Date.from(Instant.now()));

                populateCounterMeta(counterData);
                counterDatas.add(counterData);
            }
        }
        addCountersDataToIndex(counterDatas, true);
    }

    @Override
    public void storeCountersData(String sessionId, CounterDataCollection counterData) {
        try {
            List counterDatas = new ArrayList<>();
            for (IntervalCounterDataCollection intervalData : counterData.getValues()) {
                Date interval = intervalData.getTimestamp();
                Iterator> iterator = intervalData.getValues().entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry entry = iterator.next();
                    short id = entry.getKey();
                    double value = entry.getValue();

                    CounterData data = new CounterData();
                    data.setData(value);
                    data.setCounterId(id);
                    data.setTimestamp(interval);

                    populateCounterMeta(data);
                    counterDatas.add(data);
                }
                addCountersDataToIndex(counterDatas, true);
                counterDatas.clear();
            }
        } catch (Exception e) {
            getLogger().Error("Client Session", e.toString());
        }
    }

    @Override
    public void publishMetric(String instanceName, CounterData counterData) {
        try {
            metricsAgentManager.publishCounterData(instanceName, counterData, getParamsList());
        } catch (Exception e) {
            getLogger().Error("MetricServer", e.toString());
        }
    }

    @Override
    protected List getParamsList() {
        List list = new ArrayList<>();
        list.add(clientMetadata.getCacheName());
        list.add(clientMetadata.getCacheConfigId());
        list.add(clientMetadata.getBindIP());
        return list;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy