Feature/1032 (#1042)
* 1. Add the performance collection point. 2. Report formatter. #1032 * Add a new cache implementation by caffeine. #1032
This commit is contained in:
parent
cc655e1435
commit
b08309953e
|
|
@ -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>
|
||||
|
|
|
|||
|
|
@ -23,6 +23,9 @@ naming:
|
|||
host: localhost
|
||||
port: 10800
|
||||
context_path: /
|
||||
cache:
|
||||
# guava:
|
||||
caffeine:
|
||||
remote:
|
||||
gRPC:
|
||||
host: localhost
|
||||
|
|
|
|||
|
|
@ -0,0 +1,44 @@
|
|||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<!--
|
||||
~ 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.
|
||||
~
|
||||
-->
|
||||
|
||||
<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-cache</artifactId>
|
||||
<groupId>org.apache.skywalking</groupId>
|
||||
<version>5.0.0-beta-SNAPSHOT</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>collector-cache-caffeine-provider</artifactId>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.skywalking</groupId>
|
||||
<artifactId>collector-cache-define</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.github.ben-manes.caffeine</groupId>
|
||||
<artifactId>caffeine</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
|
@ -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};
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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<String, Integer> 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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<Integer, ServiceName> 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;
|
||||
}
|
||||
}
|
||||
|
|
@ -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
|
||||
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -16,5 +16,4 @@
|
|||
#
|
||||
#
|
||||
|
||||
|
||||
org.apache.skywalking.apm.collector.cache.guava.CacheModuleGuavaProvider
|
||||
org.apache.skywalking.apm.collector.cache.guava.CacheModuleGuavaProvider
|
||||
|
|
@ -31,6 +31,7 @@
|
|||
<modules>
|
||||
<module>collector-cache-define</module>
|
||||
<module>collector-cache-guava-provider</module>
|
||||
<module>collector-cache-caffeine-provider</module>
|
||||
</modules>
|
||||
|
||||
<dependencies>
|
||||
|
|
|
|||
|
|
@ -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>
|
||||
|
|
@ -214,6 +215,11 @@
|
|||
<artifactId>guava</artifactId>
|
||||
<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>
|
||||
|
|
|
|||
Loading…
Reference in New Issue