This is an automated email from the ASF dual-hosted git repository. wusheng pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/incubator-skywalking.git
The following commit(s) were added to refs/heads/master by this push: new b083099 Feature/1032 (#1042) b083099 is described below commit b08309953ebf3ae987eb90d25d1f22593e28ea32 Author: 彭勇升 pengys <8082...@qq.com> AuthorDate: Sat Apr 7 19:26:51 2018 +0800 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 + .../pom.xml | 20 ++-- .../caffeine/CacheModuleCaffeineProvider.java | 68 ++++++++++++ .../service/ApplicationCacheCaffeineService.java | 112 ++++++++++++++++++++ .../service/InstanceCacheCaffeineService.java | 117 +++++++++++++++++++++ .../NetworkAddressCacheCaffeineService.java | 112 ++++++++++++++++++++ .../service/ServiceIdCacheCaffeineService.java} | 18 ++-- .../service/ServiceNameCacheCaffeineService.java} | 37 +++---- ...alking.apm.collector.core.module.ModuleProvider | 3 +- .../guava/service/ServiceIdCacheGuavaService.java | 2 +- ...alking.apm.collector.core.module.ModuleProvider | 3 +- apm-collector/apm-collector-cache/pom.xml | 1 + apm-collector/pom.xml | 6 ++ 14 files changed, 464 insertions(+), 43 deletions(-) diff --git a/apm-collector/apm-collector-boot/pom.xml b/apm-collector/apm-collector-boot/pom.xml index 61af521..1f243f4 100644 --- a/apm-collector/apm-collector-boot/pom.xml +++ b/apm-collector/apm-collector-boot/pom.xml @@ -160,6 +160,11 @@ <artifactId>collector-cache-guava-provider</artifactId> <version>${project.version}</version> </dependency> + <dependency> + <groupId>org.apache.skywalking</groupId> + <artifactId>collector-cache-caffeine-provider</artifactId> + <version>${project.version}</version> + </dependency> <!-- cache provider --> <!-- configuration provider --> <dependency> 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 a3a33ea..f4fcd5c 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/pom.xml b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/pom.xml similarity index 69% copy from apm-collector/apm-collector-cache/pom.xml copy to apm-collector/apm-collector-cache/collector-cache-caffeine-provider/pom.xml index 3ec6a82..399d8f2 100644 --- a/apm-collector/apm-collector-cache/pom.xml +++ b/apm-collector/apm-collector-cache/collector-cache-caffeine-provider/pom.xml @@ -17,32 +17,28 @@ ~ --> -<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" +<project xmlns="http://maven.apache.org/POM/4.0.0" + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> - <artifactId>apm-collector</artifactId> + <artifactId>apm-collector-cache</artifactId> <groupId>org.apache.skywalking</groupId> <version>5.0.0-beta-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> - <artifactId>apm-collector-cache</artifactId> - <packaging>pom</packaging> - <modules> - <module>collector-cache-define</module> - <module>collector-cache-guava-provider</module> - </modules> + <artifactId>collector-cache-caffeine-provider</artifactId> + <packaging>jar</packaging> <dependencies> <dependency> <groupId>org.apache.skywalking</groupId> - <artifactId>apm-collector-core</artifactId> + <artifactId>collector-cache-define</artifactId> <version>${project.version}</version> </dependency> <dependency> - <groupId>org.apache.skywalking</groupId> - <artifactId>collector-storage-define</artifactId> - <version>${project.version}</version> + <groupId>com.github.ben-manes.caffeine</groupId> + <artifactId>caffeine</artifactId> </dependency> </dependencies> </project> \ 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 0000000..3c9779a --- /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<? extends Module> 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 0000000..b41b002 --- /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<String, Integer> 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<Integer, Application> 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<Integer, Integer> 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 0000000..fdf081d --- /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<Integer, Integer> applicationIdCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).initialCapacity(100).maximumSize(5000).build(); + + private final Cache<String, Integer> agentUUIDCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).initialCapacity(100).maximumSize(5000).build(); + + private final Cache<String, Integer> 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 0000000..e9068e8 --- /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<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; + } +} 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-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ServiceIdCacheCaffeineService.java similarity index 76% copy from apm-collector/apm-collector-cache/collector-cache-guava-provider/src/main/java/org/apache/skywalking/apm/collector/cache/guava/service/ServiceIdCacheGuavaService.java copy to apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ServiceIdCacheCaffeineService.java index fdb9eba..48b52eb 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-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ServiceIdCacheCaffeineService.java @@ -16,10 +16,11 @@ * */ -package org.apache.skywalking.apm.collector.cache.guava.service; +package org.apache.skywalking.apm.collector.cache.caffeine.service; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; +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; @@ -32,16 +33,16 @@ import org.slf4j.LoggerFactory; /** * @author peng-yongsheng */ -public class ServiceIdCacheGuavaService implements ServiceIdCacheService { +public class ServiceIdCacheCaffeineService implements ServiceIdCacheService { - private final Logger logger = LoggerFactory.getLogger(ServiceIdCacheGuavaService.class); + private final Logger logger = LoggerFactory.getLogger(ServiceIdCacheCaffeineService.class); - private final Cache<String, Integer> serviceIdCache = CacheBuilder.newBuilder().maximumSize(1000).build(); + private final Cache<String, Integer> serviceIdCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).initialCapacity(1000).maximumSize(10000).build(); private final ModuleManager moduleManager; private IServiceNameCacheDAO serviceNameCacheDAO; - public ServiceIdCacheGuavaService(ModuleManager moduleManager) { + public ServiceIdCacheCaffeineService(ModuleManager moduleManager) { this.moduleManager = moduleManager; } @@ -56,7 +57,8 @@ public class ServiceIdCacheGuavaService implements ServiceIdCacheService { int serviceId = 0; String id = applicationId + Const.ID_SPLIT + srcSpanType + Const.ID_SPLIT + serviceName; try { - serviceId = serviceIdCache.get(id, () -> getServiceNameCacheDAO().getServiceId(applicationId, srcSpanType, serviceName)); + Integer value = serviceIdCache.get(id, key -> getServiceNameCacheDAO().getServiceId(applicationId, srcSpanType, serviceName)); + serviceId = value == null ? 0 : value; } catch (Throwable e) { logger.error(e.getMessage(), e); } 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-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ServiceNameCacheCaffeineService.java similarity index 63% copy from apm-collector/apm-collector-cache/collector-cache-guava-provider/src/main/java/org/apache/skywalking/apm/collector/cache/guava/service/ServiceIdCacheGuavaService.java copy to apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ServiceNameCacheCaffeineService.java index fdb9eba..525907e 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-caffeine-provider/src/main/java/org/apache/skywalking/apm/collector/cache/caffeine/service/ServiceNameCacheCaffeineService.java @@ -16,32 +16,33 @@ * */ -package org.apache.skywalking.apm.collector.cache.guava.service; +package org.apache.skywalking.apm.collector.cache.caffeine.service; -import com.google.common.cache.Cache; -import com.google.common.cache.CacheBuilder; -import org.apache.skywalking.apm.collector.cache.service.ServiceIdCacheService; +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.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.apache.skywalking.apm.collector.storage.table.register.ServiceName; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author peng-yongsheng */ -public class ServiceIdCacheGuavaService implements ServiceIdCacheService { +public class ServiceNameCacheCaffeineService implements ServiceNameCacheService { - private final Logger logger = LoggerFactory.getLogger(ServiceIdCacheGuavaService.class); + private final Logger logger = LoggerFactory.getLogger(ServiceNameCacheCaffeineService.class); - private final Cache<String, Integer> serviceIdCache = CacheBuilder.newBuilder().maximumSize(1000).build(); + private final Cache<Integer, ServiceName> serviceCache = Caffeine.newBuilder().expireAfterWrite(10, TimeUnit.SECONDS).initialCapacity(100).maximumSize(10000).build(); private final ModuleManager moduleManager; private IServiceNameCacheDAO serviceNameCacheDAO; - public ServiceIdCacheGuavaService(ModuleManager moduleManager) { + public ServiceNameCacheCaffeineService(ModuleManager moduleManager) { this.moduleManager = moduleManager; } @@ -52,21 +53,21 @@ public class ServiceIdCacheGuavaService implements ServiceIdCacheService { 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; + public ServiceName get(int serviceId) { + ServiceName serviceName = null; try { - serviceId = serviceIdCache.get(id, () -> getServiceNameCacheDAO().getServiceId(applicationId, srcSpanType, serviceName)); + serviceName = serviceCache.get(serviceId, key -> getServiceNameCacheDAO().get(key)); } catch (Throwable e) { logger.error(e.getMessage(), e); } - if (serviceId == 0) { - serviceId = getServiceNameCacheDAO().getServiceId(applicationId, srcSpanType, serviceName); - if (serviceId != 0) { - serviceIdCache.put(id, serviceId); + if (ObjectUtils.isEmpty(serviceName)) { + serviceName = getServiceNameCacheDAO().get(serviceId); + if (ObjectUtils.isNotEmpty(serviceName)) { + serviceCache.put(serviceId, serviceName); } } - return serviceId; + + return serviceName; } } 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-caffeine-provider/src/main/resources/META-INF/services/org.apache.skywalking.apm.collector.core.module.ModuleProvider similarity index 90% copy from apm-collector/apm-collector-cache/collector-cache-guava-provider/src/main/resources/META-INF/services/org.apache.skywalking.apm.collector.core.module.ModuleProvider copy to apm-collector/apm-collector-cache/collector-cache-caffeine-provider/src/main/resources/META-INF/services/org.apache.skywalking.apm.collector.core.module.ModuleProvider index 7d55a5e..053f774 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-caffeine-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.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 fdb9eba..992bd03 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<String, Integer> serviceIdCache = CacheBuilder.newBuilder().maximumSize(1000).build(); + private final Cache<String, Integer> 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 7d55a5e..b7b1ba9 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 3ec6a82..bc7ae72 100644 --- a/apm-collector/apm-collector-cache/pom.xml +++ b/apm-collector/apm-collector-cache/pom.xml @@ -31,6 +31,7 @@ <modules> <module>collector-cache-define</module> <module>collector-cache-guava-provider</module> + <module>collector-cache-caffeine-provider</module> </modules> <dependencies> diff --git a/apm-collector/pom.xml b/apm-collector/pom.xml index 21d581a..7a37030 100644 --- a/apm-collector/pom.xml +++ b/apm-collector/pom.xml @@ -52,6 +52,7 @@ <slf4j.version>1.7.25</slf4j.version> <log4j.version>2.9.0</log4j.version> <guava.version>19.0</guava.version> + <caffeine.version>2.6.2</caffeine.version> <snakeyaml.version>1.18</snakeyaml.version> <byte-buddy.version>1.7.8</byte-buddy.version> <graphql-java-tools.version>4.3.0</graphql-java-tools.version> @@ -215,6 +216,11 @@ <version>${guava.version}</version> </dependency> <dependency> + <groupId>com.github.ben-manes.caffeine</groupId> + <artifactId>caffeine</artifactId> + <version>${caffeine.version}</version> + </dependency> + <dependency> <groupId>org.yaml</groupId> <artifactId>snakeyaml</artifactId> <version>${snakeyaml.version}</version> -- To stop receiving notification emails like this one, please contact wush...@apache.org.