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

com.alibaba.arthas.tunnel.server.cluster.InMemoryClusterStore Maven / Gradle / Ivy

The newest version!
package com.alibaba.arthas.tunnel.server.cluster;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.Cache;
import org.springframework.cache.Cache.ValueWrapper;
import org.springframework.cache.caffeine.CaffeineCache;

import com.alibaba.arthas.tunnel.server.AgentClusterInfo;

/**
 * 
 * @author hengyunabc 2020-12-02
 *
 */
public class InMemoryClusterStore implements TunnelClusterStore {
    private final static Logger logger = LoggerFactory.getLogger(InMemoryClusterStore.class);

    private Cache cache;

    @Override
    public AgentClusterInfo findAgent(String agentId) {

        ValueWrapper valueWrapper = cache.get(agentId);
        if (valueWrapper == null) {
            return null;
        }

        AgentClusterInfo info = (AgentClusterInfo) valueWrapper.get();
        return info;
    }

    @Override
    public void removeAgent(String agentId) {
        cache.evict(agentId);
    }

    @Override
    public void addAgent(String agentId, AgentClusterInfo info, long timeout, TimeUnit timeUnit) {
        cache.put(agentId, info);
    }

    @Override
    public Collection allAgentIds() {
        CaffeineCache caffeineCache = (CaffeineCache) cache;
        com.github.benmanes.caffeine.cache.Cache nativeCache = caffeineCache.getNativeCache();
        return (Collection) (Collection) nativeCache.asMap().keySet();
    }

    @Override
    public Map agentInfo(String appName) {
        CaffeineCache caffeineCache = (CaffeineCache) cache;
        com.github.benmanes.caffeine.cache.Cache nativeCache = caffeineCache.getNativeCache();

        ConcurrentMap map = (ConcurrentMap) (ConcurrentMap) nativeCache
                .asMap();

        Map result = new HashMap();

        String prefix = appName + "_";
        for (Entry entry : map.entrySet()) {
            String agentId = entry.getKey();
            if (agentId.startsWith(prefix)) {
                result.put(agentId, entry.getValue());
            }
        }

        return result;

    }

    public Cache getCache() {
        return cache;
    }

    public void setCache(Cache cache) {
        this.cache = cache;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy