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

org.graylog2.plugin.lookup.LookupCache Maven / Gradle / Ivy

There is a newer version: 5.2.7
Show newest version
/**
 * This file is part of Graylog.
 *
 * Graylog is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Graylog is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Graylog.  If not, see .
 */
package org.graylog2.plugin.lookup;

import com.codahale.metrics.Gauge;
import com.codahale.metrics.Meter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.Timer;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.google.common.util.concurrent.AbstractIdleService;
import com.google.inject.assistedinject.Assisted;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.annotation.Nullable;
import java.util.Optional;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicReference;

import static org.graylog2.utilities.ObjectUtils.objectId;

public abstract class LookupCache extends AbstractIdleService {
    private static final Logger LOG = LoggerFactory.getLogger(LookupCache.class);
    private String id;

    private final String name;
    private final LookupCacheConfiguration config;

    private final Meter totalCount;
    private final Meter hitCount;
    private final Meter missCount;
    private final Timer lookupTimer;
    private final Gauge entryCount;

    private AtomicReference error = new AtomicReference<>();

    protected LookupCache(String id,
                          String name,
                          LookupCacheConfiguration config,
                          MetricRegistry metricRegistry) {
        this.id = id;
        this.name = name;
        this.config = config;

        this.totalCount = metricRegistry.meter(MetricRegistry.name("org.graylog2.lookup.caches", id, "requests"));
        this.hitCount = metricRegistry.meter(MetricRegistry.name("org.graylog2.lookup.caches", id, "hits"));
        this.missCount = metricRegistry.meter(MetricRegistry.name("org.graylog2.lookup.caches", id, "misses"));
        this.lookupTimer = metricRegistry.timer(MetricRegistry.name("org.graylog2.lookup.caches", id, "lookupTime"));
        this.entryCount = metricRegistry.register(MetricRegistry.name("org.graylog2.lookup.caches", id, "entries"), () -> entryCount());
    }

    public void incrTotalCount() {
        totalCount.mark();
    }

    public void incrHitCount() {
        hitCount.mark();
    }

    public void incrMissCount() {
        missCount.mark();
    }

    public Timer.Context lookupTimer() {
        return lookupTimer.time();
    }

    public Gauge entryCount() {
        // Returns -1 if the cache does not support counting entries
        return () -> -1L;
    }

    @Override
    protected void startUp() throws Exception {
        // Make sure startUp() never throws an error - we handle errors internally
        try {
            doStart();
        } catch (Exception e) {
            LOG.error("Couldn't start cache <{}/{}/@{}>", name(), id(), objectId(this), e);
            setError(e);
        }
    }

    protected abstract void doStart() throws Exception;

    @Override
    protected void shutDown() throws Exception {
        // Make sure shutDown() never throws an error - we handle errors internally
        try {
            doStop();
        } catch (Exception e) {
            LOG.error("Couldn't stop cache <{}/{}/@{}>", name(), id(), objectId(this), e);
        }
    }

    protected abstract void doStop() throws Exception;

    protected void clearError() {
        error.set(null);
    }

    public Optional getError() {
        return Optional.ofNullable(error.get());
    }

    protected void setError(Throwable throwable) {
        error.set(throwable);
    }

    @Nullable
    public String id() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public abstract LookupResult get(LookupCacheKey key, Callable loader);

    public abstract LookupResult getIfPresent(LookupCacheKey key);

    public abstract void purge();

    public abstract void purge(LookupCacheKey purgeKey);

    public LookupCacheConfiguration getConfig() {
        return config;
    }

    public String name() {
        return name;
    }

    public interface Factory {
        T create(@Assisted("id") String id, @Assisted("name") String name, LookupCacheConfiguration configuration);

        Descriptor getDescriptor();
    }

    public abstract static class Descriptor {

        private final String type;
        private final Class configClass;

        public Descriptor(String type, Class configClass) {
            this.type = type;
            this.configClass = configClass;
        }

        @JsonProperty("type")
        public String getType() {
            return type;
        }

        @JsonProperty("config_class")
        public Class getConfigClass() {
            return configClass;
        }

        @JsonProperty("default_config")
        public abstract C defaultConfiguration();

    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy