NetworkAddressCacheCaffeineService.java 4.6 KB
Newer Older
彭勇升 pengys 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
/*
 * 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.skywalking.apm.collector.cache.caffeine.service;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;
import java.util.concurrent.TimeUnit;
import org.apache.skywalking.apm.collector.cache.service.NetworkAddressCacheService;
import org.apache.skywalking.apm.collector.core.module.ModuleManager;
import org.apache.skywalking.apm.collector.core.util.Const;
import org.apache.skywalking.apm.collector.core.util.ObjectUtils;
import org.apache.skywalking.apm.collector.core.util.StringUtils;
import org.apache.skywalking.apm.collector.storage.StorageModule;
import org.apache.skywalking.apm.collector.storage.dao.cache.INetworkAddressCacheDAO;
import org.apache.skywalking.apm.collector.storage.table.register.NetworkAddress;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author peng-yongsheng
 */
public class NetworkAddressCacheCaffeineService implements NetworkAddressCacheService {

    private final Logger logger = LoggerFactory.getLogger(NetworkAddressCacheCaffeineService.class);

    private final Cache<String, Integer> addressCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).initialCapacity(100).maximumSize(5000).build();

    private final ModuleManager moduleManager;
    private INetworkAddressCacheDAO networkAddressCacheDAO;

    public NetworkAddressCacheCaffeineService(ModuleManager moduleManager) {
        this.moduleManager = moduleManager;
    }

    private INetworkAddressCacheDAO getNetworkAddressCacheDAO() {
        if (ObjectUtils.isEmpty(networkAddressCacheDAO)) {
            this.networkAddressCacheDAO = moduleManager.find(StorageModule.NAME).getService(INetworkAddressCacheDAO.class);
        }
        return this.networkAddressCacheDAO;
    }

    public int getAddressId(String networkAddress) {
        int addressId = 0;
        try {
            Integer value = addressCache.get(networkAddress, key -> getNetworkAddressCacheDAO().getAddressId(key));
            addressId = value == null ? 0 : value;

            if (addressId == 0) {
                addressId = getNetworkAddressCacheDAO().getAddressId(networkAddress);
                if (addressId != 0) {
                    addressCache.put(networkAddress, addressId);
                }
            }
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
        }

        return addressId;
    }

    private final Cache<Integer, String> idCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).initialCapacity(100).maximumSize(5000).build();

    public String getAddress(int addressId) {
        String networkAddress = Const.EMPTY_STRING;
        try {
            networkAddress = idCache.get(addressId, key -> getNetworkAddressCacheDAO().getAddressById(key));
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
        }

        if (StringUtils.isEmpty(networkAddress)) {
            networkAddress = getNetworkAddressCacheDAO().getAddressById(addressId);
            if (StringUtils.isNotEmpty(networkAddress)) {
                idCache.put(addressId, networkAddress);
            }
        }
        return networkAddress;
    }

    private final Cache<Integer, NetworkAddress> addressObjCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).initialCapacity(100).maximumSize(5000).build();

    @Override public boolean compare(int addressId, int spanLayer, int serverType) {
        try {
            NetworkAddress address = addressObjCache.get(addressId, key -> getNetworkAddressCacheDAO().getAddress(key));

            if (ObjectUtils.isNotEmpty(address)) {
                if (spanLayer != address.getSpanLayer() || serverType != address.getServerType()) {
                    return false;
                }
            }
        } catch (Throwable e) {
            logger.error(e.getMessage(), e);
        }
        return true;
    }
}