MetaCacheEntry.java

// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you 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 org.apache.doris.datasource.metacache;

import org.apache.doris.common.CacheFactory;
import org.apache.doris.common.Config;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.stats.CacheStats;
import com.google.common.collect.Maps;

import java.util.Map;
import java.util.Objects;
import java.util.OptionalLong;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.atomic.AtomicLong;
import java.util.function.BiConsumer;
import java.util.function.Function;
import java.util.function.Predicate;

/**
 * Unified cache entry abstraction.
 * It stores one logical cache dataset and provides optional lazy loading,
 * key/predicate/full invalidation, and lightweight runtime stats.
 */
public class MetaCacheEntry<K, V> {
    private final String name;
    private final Function<K, V> loader;
    private final CacheSpec cacheSpec;
    private final boolean autoRefresh;
    private final Cache<K, V> data;
    private final AtomicLong invalidateCount = new AtomicLong(0);

    public MetaCacheEntry(String name, Function<K, V> loader, CacheSpec cacheSpec, ExecutorService refreshExecutor) {
        this(name, loader, cacheSpec, refreshExecutor, true);
    }

    public MetaCacheEntry(String name, Function<K, V> loader, CacheSpec cacheSpec, ExecutorService refreshExecutor,
            boolean autoRefresh) {
        this.name = name;
        this.loader = Objects.requireNonNull(loader, "loader can not be null");
        this.cacheSpec = Objects.requireNonNull(cacheSpec, "cacheSpec can not be null");
        this.autoRefresh = autoRefresh;
        Objects.requireNonNull(refreshExecutor, "refreshExecutor can not be null");
        boolean enabled = CacheSpec.isCacheEnabled(
                this.cacheSpec.isEnable(), this.cacheSpec.getTtlSecond(), this.cacheSpec.getCapacity());
        OptionalLong expireAfterAccessSec =
                enabled ? CacheSpec.toExpireAfterAccess(this.cacheSpec.getTtlSecond()) : OptionalLong.empty();
        OptionalLong refreshAfterWriteSec =
                enabled && autoRefresh
                        ? OptionalLong.of(Config.external_cache_refresh_time_minutes * 60)
                        : OptionalLong.empty();
        long maxSize = enabled ? this.cacheSpec.getCapacity() : 0L;
        CacheFactory cacheFactory = new CacheFactory(
                expireAfterAccessSec,
                refreshAfterWriteSec,
                maxSize,
                true,
                null);
        this.data = cacheFactory.buildCache(loader::apply, refreshExecutor);
    }

    public String name() {
        return name;
    }

    public V get(K key) {
        return data.get(key, loader::apply);
    }

    public V get(K key, Function<K, V> missLoader) {
        return data.get(key, Objects.requireNonNull(missLoader, "missLoader can not be null")::apply);
    }

    public V getIfPresent(K key) {
        return data.getIfPresent(key);
    }

    public void put(K key, V value) {
        data.put(key, value);
    }

    public void invalidateKey(K key) {
        if (data.asMap().remove(key) != null) {
            invalidateCount.incrementAndGet();
        }
    }

    public void invalidateIf(Predicate<K> predicate) {
        data.asMap().keySet().removeIf(key -> {
            if (predicate.test(key)) {
                invalidateCount.incrementAndGet();
                return true;
            }
            return false;
        });
    }

    public void invalidateAll() {
        long size = data.estimatedSize();
        data.invalidateAll();
        invalidateCount.addAndGet(size);
    }

    public void forEach(BiConsumer<K, V> consumer) {
        data.asMap().forEach(consumer);
    }

    public Map<String, String> stats() {
        CacheStats cacheStats = data.stats();

        Map<String, String> result = Maps.newHashMap();
        result.put("hit_ratio", String.valueOf(cacheStats.hitRate()));
        result.put("hit_count", String.valueOf(cacheStats.hitCount()));
        result.put("read_count", String.valueOf(cacheStats.requestCount()));
        result.put("load_count", String.valueOf(cacheStats.loadCount()));
        result.put("eviction_count", String.valueOf(cacheStats.evictionCount()));
        result.put("average_load_penalty", String.valueOf(cacheStats.averageLoadPenalty()));
        result.put("invalidate_count", String.valueOf(invalidateCount.get()));
        result.put("estimated_size", String.valueOf(data.estimatedSize()));
        result.put("enabled", String.valueOf(cacheSpec.isEnable()));
        result.put("ttl_second", String.valueOf(cacheSpec.getTtlSecond()));
        result.put("capacity", String.valueOf(cacheSpec.getCapacity()));
        result.put("auto_refresh", String.valueOf(autoRefresh));
        return result;
    }
}