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

Alachisoft.NCache.Sessions.MetricsSession Maven / Gradle / Ivy

The newest version!
package Alachisoft.NCache.Sessions;

import Alachisoft.NCache.Common.Logger.ILogger;
import Alachisoft.NCache.Common.Logger.JLogger;
import Alachisoft.NCache.DataModel.MetricsDBSession;
import Alachisoft.NCache.DataModel.PublisherVersion;
import Alachisoft.NCache.MetricServer.MetricPublisher;
import Alachisoft.NCache.MetricsAgents.MetricsAgentManager;
import com.alachisoft.ncache.common.monitoring.CounterDataCollection;
import com.alachisoft.ncache.common.monitoring.CounterMetadataCollection;

import java.time.Duration;
import java.time.Instant;
import java.util.Date;

public abstract class MetricsSession extends CounterSession {

    private MetricPublisher publisher;
    private String publisherId;
    private boolean isMetadataPersisted = true;
    protected MetricsDBSession metric;
    protected PublisherVersion publisherVersion = null;
    public MetricsDBSession getMetric() {
        return metric;
    }

    public String getSessionId() {
        return sessionId;
    }

    public void setSessionId(String sessionId) {
        this.sessionId = sessionId;
    }

    public String getVersion() {
        return version;
    }

    public void setVersion(String version) {
        this.version = version;
    }

    public ILogger getLogger() {
        return logger;
    }

    public void setLogger(ILogger logger) {
        this.logger = logger;
    }

    public MetricPublisher getPublisher() {
        return publisher;
    }

    public void setPublisher(MetricPublisher publisher) {
        this.publisher = publisher;
    }

    public Date getLastWriteTime() {
        return lastWriteTime;
    }

    public void setLastWriteTime(Date lastWriteTime) {
        this.lastWriteTime = lastWriteTime;
    }

    public Date getLastReadTime() {
        return lastReadTime;
    }

    public void setLastReadTime(Date lastReadTime) {
        this.lastReadTime = lastReadTime;
    }

    public boolean isMetadataPersisted() {
        return isMetadataPersisted;
    }

    public void setMetadataPersisted(boolean metadataPersisted) {
        isMetadataPersisted = metadataPersisted;
    }
    private String sessionId;
    private Date lastWriteTime;
    private Date lastReadTime;
    private String version;
    private ILogger logger;

    private static final int READ_EXPIRATION_TIME = 100; // seconds
    private static final int WRITE_EXPIRATION_TIME = 30; // seconds

    protected int inMemoryInterval = 30;

    public MetricsSession(String sessionId, MetricPublisher publisher, String cacheConfigId) {
        super(cacheConfigId);
        this.sessionId = sessionId;
        this.publisher = publisher;
        updatedPublisher();
    }

    protected void updatedPublisher() {
        String[] sessionToken = sessionId.split(":");
        if (sessionToken.length > 1) {
            String pubString = sessionToken[1];
            if (pubString.equals("NCache")) {
                publisher = MetricPublisher.NCache;
            }
            if (pubString.equals("Bridge")) {
                publisher = MetricPublisher.Bridge;
            }
            if (pubString.equals("NCacheClient")) {
                publisher = MetricPublisher.Client;
            }
            if (pubString.equals("BridgedCache")) {
                publisher = MetricPublisher.BridgedCache;
            }
            if (pubString.equals("NCachePersistence")) {
                publisher = MetricPublisher.NCachePersistence;
            }
            if (pubString.equals("NCacheLucene")) {
                publisher = MetricPublisher.NCacheLucene;
            }
        }
    }

    public void initialize(JLogger logger, MetricsAgentManager metricsAgentManager) {
        this.logger = logger;
        counterRefTime = 0;
        initializeCounters(inMemoryInterval, logger, metricsAgentManager);
    }

    protected void updateWriteTime() {
        lastWriteTime = Date.from(Instant.now());
    }

    protected void updateReadTime() {
        lastReadTime = Date.from(Instant.now());
    }

    public boolean isWriteActive() {
        if (lastWriteTime != null) {
            double secs = Duration.between(lastWriteTime.toInstant(), Date.from(Instant.now()).toInstant()).toSeconds();
            return lastWriteTime != null && secs <= WRITE_EXPIRATION_TIME;
        } else {
            return false;
        }
    }

    public boolean isReadActive() {
        if (lastReadTime != null) {
            return lastReadTime != null && Duration.between(lastReadTime.toInstant(), Date.from(Instant.now()).toInstant()).toSeconds() <= READ_EXPIRATION_TIME;
        } else {
            return false;
        }
    }

    public boolean isExpired() {
        return !(isReadActive() || isWriteActive());
    }
    /**
     * This is the method that will persist the Metadata for cache client or Bridge.
     */
    public void persistMetadata() {
        // Implementation goes here
    }

    /**
     * This is the method used to store MetricDbSession in the database.
     */
    public void persistMetricMetadata() {
        // Implementation goes here
    }

    /**
     * This method checks if write is active and persists counters.
     */
    public void persist() {
        if (isWriteActive()) {
            persistCounters();
        }
    }

    /**
     * This method persists session metadata if it hasn't been persisted already.
     */
    public void persistSessionMetadata() {
        if (!isMetadataPersisted()) {
            persistMetadata();
            isMetadataPersisted = true;
        }
    }
    public void loadMetricDbSession()
    {
    }
    public void addCountersMetadata(CounterMetadataCollection counterMeta)
    {
        updateWriteTime();
        storeCountersMetadata(counterMeta);
    }
    public void addCountersData(String sessionId, CounterDataCollection counterData)
    {
        updateWriteTime();
        storeCountersData(sessionId, counterData);

    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy