From b08309953ebf3ae987eb90d25d1f22593e28ea32 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BD=AD=E5=8B=87=E5=8D=87=20pengys?= <8082209@qq.com> Date: Sat, 7 Apr 2018 19:26:51 +0800 Subject: [PATCH] Feature/1032 (#1042) * 1. Add the performance collection point. 2. Report formatter. #1032 * Add a new cache implementation by caffeine. #1032 --- apm-collector/apm-collector-boot/pom.xml | 5 + .../src/main/resources/application.yml | 3 + .../collector-cache-caffeine-provider/pom.xml | 44 +++++++ .../caffeine/CacheModuleCaffeineProvider.java | 68 ++++++++++ .../ApplicationCacheCaffeineService.java | 112 +++++++++++++++++ .../service/InstanceCacheCaffeineService.java | 117 ++++++++++++++++++ .../NetworkAddressCacheCaffeineService.java | 112 +++++++++++++++++ .../ServiceIdCacheCaffeineService.java | 74 +++++++++++ .../ServiceNameCacheCaffeineService.java | 73 +++++++++++ ...g.apm.collector.core.module.ModuleProvider | 19 +++ .../service/ServiceIdCacheGuavaService.java | 2 +- ...g.apm.collector.core.module.ModuleProvider | 3 +- apm-collector/apm-collector-cache/pom.xml | 1 + apm-collector/pom.xml | 6 + 14 files changed, 636 insertions(+), 3 deletions(-) create mode 100644 apm-collector/apm-collector-cache/collector-cache-caffeine-provider/pom.xml create mode 100644 apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/CacheModuleCaffeineProvider.java create mode 100644 apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ApplicationCacheCaffeineService.java create mode 100644 apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/InstanceCacheCaffeineService.java create mode 100644 apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/NetworkAddressCacheCaffeineService.java create mode 100644 apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ServiceIdCacheCaffeineService.java create mode 100644 apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ServiceNameCacheCaffeineService.java create mode 100644 apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/resources/META-INF/services/org.apache.skywalking.apm.collector.core.module.ModuleProvider diff --git a/apm-collector/apm-collector-boot/pom.xml b/apm-collector/apm-collector-boot/pom.xml index 61af52172..1f243f4cd 100644 --- a/apm-collector/apm-collector-boot/pom.xml +++ b/apm-collector/apm-collector-boot/pom.xml @@ -160,6 +160,11 @@ collector-cache-guava-provider ${project.version} + + org.apache.skywalking + collector-cache-caffeine-provider + ${project.version} + diff --git a/apm-collector/apm-collector-boot/src/main/resources/application.yml b/apm-collector/apm-collector-boot/src/main/resources/application.yml index a3a33ea9f..f4fcd5ce2 100644 --- a/apm-collector/apm-collector-boot/src/main/resources/application.yml +++ b/apm-collector/apm-collector-boot/src/main/resources/application.yml @@ -23,6 +23,9 @@ naming: host: localhost port: 10800 context_path: / +cache: +# guava: + caffeine: remote: gRPC: host: localhost diff --git a/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/pom.xml b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/pom.xml new file mode 100644 index 000000000..399d8f2f7 --- /dev/null +++ b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/pom.xml @@ -0,0 +1,44 @@ + + + + + + apm-collector-cache + org.apache.skywalking + 5.0.0-beta-SNAPSHOT + + 4.0.0 + + collector-cache-caffeine-provider + jar + + + + org.apache.skywalking + collector-cache-define + ${project.version} + + + com.github.ben-manes.caffeine + caffeine + + + \ No newline at end of file diff --git a/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/CacheModuleCaffeineProvider.java b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/CacheModuleCaffeineProvider.java new file mode 100644 index 000000000..3c9779ad7 --- /dev/null +++ b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/CacheModuleCaffeineProvider.java @@ -0,0 +1,68 @@ +/* + * 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; + +import java.util.Properties; +import org.apache.skywalking.apm.collector.cache.CacheModule; +import org.apache.skywalking.apm.collector.cache.caffeine.service.ApplicationCacheCaffeineService; +import org.apache.skywalking.apm.collector.cache.caffeine.service.InstanceCacheCaffeineService; +import org.apache.skywalking.apm.collector.cache.caffeine.service.NetworkAddressCacheCaffeineService; +import org.apache.skywalking.apm.collector.cache.caffeine.service.ServiceIdCacheCaffeineService; +import org.apache.skywalking.apm.collector.cache.caffeine.service.ServiceNameCacheCaffeineService; +import org.apache.skywalking.apm.collector.cache.service.ApplicationCacheService; +import org.apache.skywalking.apm.collector.cache.service.InstanceCacheService; +import org.apache.skywalking.apm.collector.cache.service.NetworkAddressCacheService; +import org.apache.skywalking.apm.collector.cache.service.ServiceIdCacheService; +import org.apache.skywalking.apm.collector.cache.service.ServiceNameCacheService; +import org.apache.skywalking.apm.collector.core.module.Module; +import org.apache.skywalking.apm.collector.core.module.ModuleProvider; +import org.apache.skywalking.apm.collector.core.module.ServiceNotProvidedException; +import org.apache.skywalking.apm.collector.storage.StorageModule; + +/** + * @author peng-yongsheng + */ +public class CacheModuleCaffeineProvider extends ModuleProvider { + + @Override public String name() { + return "caffeine"; + } + + @Override public Class module() { + return CacheModule.class; + } + + @Override public void prepare(Properties config) throws ServiceNotProvidedException { + this.registerServiceImplementation(ApplicationCacheService.class, new ApplicationCacheCaffeineService(getManager())); + this.registerServiceImplementation(InstanceCacheService.class, new InstanceCacheCaffeineService(getManager())); + this.registerServiceImplementation(ServiceIdCacheService.class, new ServiceIdCacheCaffeineService(getManager())); + this.registerServiceImplementation(ServiceNameCacheService.class, new ServiceNameCacheCaffeineService(getManager())); + this.registerServiceImplementation(NetworkAddressCacheService.class, new NetworkAddressCacheCaffeineService(getManager())); + } + + @Override public void start(Properties config) { + } + + @Override public void notifyAfterCompleted() { + } + + @Override public String[] requiredModules() { + return new String[] {StorageModule.NAME}; + } +} diff --git a/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ApplicationCacheCaffeineService.java b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ApplicationCacheCaffeineService.java new file mode 100644 index 000000000..b41b00284 --- /dev/null +++ b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ApplicationCacheCaffeineService.java @@ -0,0 +1,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.ApplicationCacheService; +import org.apache.skywalking.apm.collector.core.module.ModuleManager; +import org.apache.skywalking.apm.collector.core.util.ObjectUtils; +import org.apache.skywalking.apm.collector.storage.StorageModule; +import org.apache.skywalking.apm.collector.storage.dao.cache.IApplicationCacheDAO; +import org.apache.skywalking.apm.collector.storage.table.register.Application; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author peng-yongsheng + */ +public class ApplicationCacheCaffeineService implements ApplicationCacheService { + + private final Logger logger = LoggerFactory.getLogger(ApplicationCacheCaffeineService.class); + + private final Cache codeCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).initialCapacity(100).maximumSize(1000).build(); + + private final ModuleManager moduleManager; + private IApplicationCacheDAO applicationCacheDAO; + + public ApplicationCacheCaffeineService(ModuleManager moduleManager) { + this.moduleManager = moduleManager; + } + + private IApplicationCacheDAO getApplicationCacheDAO() { + if (ObjectUtils.isEmpty(applicationCacheDAO)) { + this.applicationCacheDAO = moduleManager.find(StorageModule.NAME).getService(IApplicationCacheDAO.class); + } + return this.applicationCacheDAO; + } + + @Override public int getApplicationIdByCode(String applicationCode) { + int applicationId = 0; + try { + Integer value = codeCache.get(applicationCode, key -> getApplicationCacheDAO().getApplicationIdByCode(key)); + applicationId = value == null ? 0 : value; + } catch (Throwable e) { + logger.error(e.getMessage(), e); + } + + if (applicationId == 0) { + applicationId = getApplicationCacheDAO().getApplicationIdByCode(applicationCode); + if (applicationId != 0) { + codeCache.put(applicationCode, applicationId); + } + } + return applicationId; + } + + private final Cache applicationCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).initialCapacity(100).maximumSize(1000).build(); + + @Override public Application getApplicationById(int applicationId) { + Application application = null; + try { + application = applicationCache.get(applicationId, key -> getApplicationCacheDAO().getApplication(key)); + } catch (Throwable e) { + logger.error(e.getMessage(), e); + } + + if (ObjectUtils.isEmpty(application)) { + application = getApplicationCacheDAO().getApplication(applicationId); + if (ObjectUtils.isNotEmpty(application)) { + applicationCache.put(applicationId, application); + } + } + return application; + } + + private final Cache addressIdCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).initialCapacity(100).maximumSize(1000).build(); + + @Override public int getApplicationIdByAddressId(int addressId) { + int applicationId = 0; + try { + Integer value = addressIdCache.get(addressId, key -> getApplicationCacheDAO().getApplicationIdByAddressId(key)); + applicationId = value == null ? 0 : value; + } catch (Throwable e) { + logger.error(e.getMessage(), e); + } + + if (applicationId == 0) { + applicationId = getApplicationCacheDAO().getApplicationIdByAddressId(addressId); + if (applicationId != 0) { + addressIdCache.put(addressId, applicationId); + } + } + return applicationId; + } +} diff --git a/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/InstanceCacheCaffeineService.java b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/InstanceCacheCaffeineService.java new file mode 100644 index 000000000..fdf081dc1 --- /dev/null +++ b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/InstanceCacheCaffeineService.java @@ -0,0 +1,117 @@ +/* + * 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.InstanceCacheService; +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.storage.StorageModule; +import org.apache.skywalking.apm.collector.storage.dao.cache.IInstanceCacheDAO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author peng-yongsheng + */ +public class InstanceCacheCaffeineService implements InstanceCacheService { + + private final Logger logger = LoggerFactory.getLogger(InstanceCacheCaffeineService.class); + + private final Cache applicationIdCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).initialCapacity(100).maximumSize(5000).build(); + + private final Cache agentUUIDCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).initialCapacity(100).maximumSize(5000).build(); + + private final Cache addressIdCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).initialCapacity(100).maximumSize(5000).build(); + + private final ModuleManager moduleManager; + private IInstanceCacheDAO instanceCacheDAO; + + public InstanceCacheCaffeineService(ModuleManager moduleManager) { + this.moduleManager = moduleManager; + } + + private IInstanceCacheDAO getInstanceCacheDAO() { + if (ObjectUtils.isEmpty(instanceCacheDAO)) { + this.instanceCacheDAO = moduleManager.find(StorageModule.NAME).getService(IInstanceCacheDAO.class); + } + return this.instanceCacheDAO; + } + + @Override public int getApplicationId(int instanceId) { + int applicationId = 0; + try { + Integer value = applicationIdCache.get(instanceId, key -> getInstanceCacheDAO().getApplicationId(key)); + applicationId = value == null ? 0 : value; + } catch (Throwable e) { + logger.error(e.getMessage(), e); + } + + if (applicationId == 0) { + applicationId = getInstanceCacheDAO().getApplicationId(instanceId); + if (applicationId != 0) { + applicationIdCache.put(instanceId, applicationId); + } + } + return applicationId; + } + + @Override public int getInstanceIdByAgentUUID(int applicationId, String agentUUID) { + String cacheKey = applicationId + Const.ID_SPLIT + agentUUID; + + int instanceId = 0; + try { + Integer value = agentUUIDCache.get(cacheKey, key -> getInstanceCacheDAO().getInstanceIdByAgentUUID(applicationId, agentUUID)); + instanceId = value == null ? 0 : value; + } catch (Throwable e) { + logger.error(e.getMessage(), e); + } + + if (instanceId == 0) { + instanceId = getInstanceCacheDAO().getInstanceIdByAgentUUID(applicationId, agentUUID); + if (applicationId != 0) { + agentUUIDCache.put(cacheKey, instanceId); + } + } + return instanceId; + } + + @Override public int getInstanceIdByAddressId(int applicationId, int addressId) { + String cacheKey = applicationId + Const.ID_SPLIT + addressId; + + int instanceId = 0; + try { + Integer value = addressIdCache.get(cacheKey, key -> getInstanceCacheDAO().getInstanceIdByAddressId(applicationId, addressId)); + instanceId = value == null ? 0 : value; + } catch (Throwable e) { + logger.error(e.getMessage(), e); + } + + if (instanceId == 0) { + instanceId = getInstanceCacheDAO().getInstanceIdByAddressId(applicationId, addressId); + if (applicationId != 0) { + addressIdCache.put(cacheKey, instanceId); + } + } + return instanceId; + } +} diff --git a/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/NetworkAddressCacheCaffeineService.java b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/NetworkAddressCacheCaffeineService.java new file mode 100644 index 000000000..e9068e8c9 --- /dev/null +++ b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/NetworkAddressCacheCaffeineService.java @@ -0,0 +1,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 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 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 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; + } +} diff --git a/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ServiceIdCacheCaffeineService.java b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ServiceIdCacheCaffeineService.java new file mode 100644 index 000000000..48b52eb06 --- /dev/null +++ b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ServiceIdCacheCaffeineService.java @@ -0,0 +1,74 @@ +/* + * 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.ServiceIdCacheService; +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.storage.StorageModule; +import org.apache.skywalking.apm.collector.storage.dao.cache.IServiceNameCacheDAO; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author peng-yongsheng + */ +public class ServiceIdCacheCaffeineService implements ServiceIdCacheService { + + private final Logger logger = LoggerFactory.getLogger(ServiceIdCacheCaffeineService.class); + + private final Cache serviceIdCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).initialCapacity(1000).maximumSize(10000).build(); + + private final ModuleManager moduleManager; + private IServiceNameCacheDAO serviceNameCacheDAO; + + public ServiceIdCacheCaffeineService(ModuleManager moduleManager) { + this.moduleManager = moduleManager; + } + + private IServiceNameCacheDAO getServiceNameCacheDAO() { + if (ObjectUtils.isEmpty(serviceNameCacheDAO)) { + this.serviceNameCacheDAO = moduleManager.find(StorageModule.NAME).getService(IServiceNameCacheDAO.class); + } + return this.serviceNameCacheDAO; + } + + @Override public int get(int applicationId, int srcSpanType, String serviceName) { + int serviceId = 0; + String id = applicationId + Const.ID_SPLIT + srcSpanType + Const.ID_SPLIT + serviceName; + try { + Integer value = serviceIdCache.get(id, key -> getServiceNameCacheDAO().getServiceId(applicationId, srcSpanType, serviceName)); + serviceId = value == null ? 0 : value; + } catch (Throwable e) { + logger.error(e.getMessage(), e); + } + + if (serviceId == 0) { + serviceId = getServiceNameCacheDAO().getServiceId(applicationId, srcSpanType, serviceName); + if (serviceId != 0) { + serviceIdCache.put(id, serviceId); + } + } + return serviceId; + } +} diff --git a/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ServiceNameCacheCaffeineService.java b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ServiceNameCacheCaffeineService.java new file mode 100644 index 000000000..525907ed4 --- /dev/null +++ b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ServiceNameCacheCaffeineService.java @@ -0,0 +1,73 @@ +/* + * 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.ServiceNameCacheService; +import org.apache.skywalking.apm.collector.core.module.ModuleManager; +import org.apache.skywalking.apm.collector.core.util.ObjectUtils; +import org.apache.skywalking.apm.collector.storage.StorageModule; +import org.apache.skywalking.apm.collector.storage.dao.cache.IServiceNameCacheDAO; +import org.apache.skywalking.apm.collector.storage.table.register.ServiceName; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author peng-yongsheng + */ +public class ServiceNameCacheCaffeineService implements ServiceNameCacheService { + + private final Logger logger = LoggerFactory.getLogger(ServiceNameCacheCaffeineService.class); + + private final Cache serviceCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).initialCapacity(100).maximumSize(10000).build(); + + private final ModuleManager moduleManager; + private IServiceNameCacheDAO serviceNameCacheDAO; + + public ServiceNameCacheCaffeineService(ModuleManager moduleManager) { + this.moduleManager = moduleManager; + } + + private IServiceNameCacheDAO getServiceNameCacheDAO() { + if (ObjectUtils.isEmpty(serviceNameCacheDAO)) { + this.serviceNameCacheDAO = moduleManager.find(StorageModule.NAME).getService(IServiceNameCacheDAO.class); + } + return this.serviceNameCacheDAO; + } + + public ServiceName get(int serviceId) { + ServiceName serviceName = null; + try { + serviceName = serviceCache.get(serviceId, key -> getServiceNameCacheDAO().get(key)); + } catch (Throwable e) { + logger.error(e.getMessage(), e); + } + + if (ObjectUtils.isEmpty(serviceName)) { + serviceName = getServiceNameCacheDAO().get(serviceId); + if (ObjectUtils.isNotEmpty(serviceName)) { + serviceCache.put(serviceId, serviceName); + } + } + + return serviceName; + } +} diff --git a/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/resources/META-INF/services/org.apache.skywalking.apm.collector.core.module.ModuleProvider b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/resources/META-INF/services/org.apache.skywalking.apm.collector.core.module.ModuleProvider new file mode 100644 index 000000000..053f7746d --- /dev/null +++ b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/resources/META-INF/services/org.apache.skywalking.apm.collector.core.module.ModuleProvider @@ -0,0 +1,19 @@ +# +# 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. +# +# + +org.apache.skywalking.apm.collector.cache.caffeine.CacheModuleCaffeineProvider diff --git a/apm-collector/apm-collector-cache/collector-cache-guava-provider/src/main/java/org/apache/skywalking/apm/collector/cache/guava/service/ServiceIdCacheGuavaService.java b/apm-collector/apm-collector-cache/collector-cache-guava-provider/src/main/java/org/apache/skywalking/apm/collector/cache/guava/service/ServiceIdCacheGuavaService.java index fdb9eba50..992bd032b 100644 --- a/apm-collector/apm-collector-cache/collector-cache-guava-provider/src/main/java/org/apache/skywalking/apm/collector/cache/guava/service/ServiceIdCacheGuavaService.java +++ b/apm-collector/apm-collector-cache/collector-cache-guava-provider/src/main/java/org/apache/skywalking/apm/collector/cache/guava/service/ServiceIdCacheGuavaService.java @@ -36,7 +36,7 @@ public class ServiceIdCacheGuavaService implements ServiceIdCacheService { private final Logger logger = LoggerFactory.getLogger(ServiceIdCacheGuavaService.class); - private final Cache serviceIdCache = CacheBuilder.newBuilder().maximumSize(1000).build(); + private final Cache serviceIdCache = CacheBuilder.newBuilder().maximumSize(10000).build(); private final ModuleManager moduleManager; private IServiceNameCacheDAO serviceNameCacheDAO; diff --git a/apm-collector/apm-collector-cache/collector-cache-guava-provider/src/main/resources/META-INF/services/org.apache.skywalking.apm.collector.core.module.ModuleProvider b/apm-collector/apm-collector-cache/collector-cache-guava-provider/src/main/resources/META-INF/services/org.apache.skywalking.apm.collector.core.module.ModuleProvider index 7d55a5ed1..b7b1ba9c8 100644 --- a/apm-collector/apm-collector-cache/collector-cache-guava-provider/src/main/resources/META-INF/services/org.apache.skywalking.apm.collector.core.module.ModuleProvider +++ b/apm-collector/apm-collector-cache/collector-cache-guava-provider/src/main/resources/META-INF/services/org.apache.skywalking.apm.collector.core.module.ModuleProvider @@ -16,5 +16,4 @@ # # - -org.apache.skywalking.apm.collector.cache.guava.CacheModuleGuavaProvider +org.apache.skywalking.apm.collector.cache.guava.CacheModuleGuavaProvider \ No newline at end of file diff --git a/apm-collector/apm-collector-cache/pom.xml b/apm-collector/apm-collector-cache/pom.xml index 3ec6a8278..bc7ae7227 100644 --- a/apm-collector/apm-collector-cache/pom.xml +++ b/apm-collector/apm-collector-cache/pom.xml @@ -31,6 +31,7 @@ collector-cache-define collector-cache-guava-provider + collector-cache-caffeine-provider diff --git a/apm-collector/pom.xml b/apm-collector/pom.xml index 21d581a89..7a37030bb 100644 --- a/apm-collector/pom.xml +++ b/apm-collector/pom.xml @@ -52,6 +52,7 @@ 1.7.25 2.9.0 19.0 + 2.6.2 1.18 1.7.8 4.3.0 @@ -214,6 +215,11 @@ guava ${guava.version} + + com.github.ben-manes.caffeine + caffeine + ${caffeine.version} + org.yaml snakeyaml