Add ServiceLoaderUtil to improve SPI classloader mechanism and update SpiLoader

Signed-off-by: Eric Zhao <sczyh16@gmail.com>
This commit is contained in:
Eric Zhao 2019-10-16 00:51:11 +08:00
parent dbf1f97dde
commit 8d5654f727
3 changed files with 87 additions and 4 deletions

View File

@ -48,6 +48,7 @@ public class SentinelConfig {
public static final String TOTAL_METRIC_FILE_COUNT = "csp.sentinel.metric.file.total.count";
public static final String COLD_FACTOR = "csp.sentinel.flow.cold.factor";
public static final String STATISTIC_MAX_RT = "csp.sentinel.statistic.max.rt";
public static final String SPI_CLASSLOADER = "csp.sentinel.spi.classloader";
static final String DEFAULT_CHARSET = "UTF-8";
static final long DEFAULT_SINGLE_METRIC_FILE_SIZE = 1024 * 1024 * 50;

View File

@ -0,0 +1,45 @@
/*
* Copyright 1999-2019 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.csp.sentinel.spi;
import java.util.ServiceLoader;
import com.alibaba.csp.sentinel.config.SentinelConfig;
/**
* @author Eric Zhao
* @since 1.7.0
*/
public final class ServiceLoaderUtil {
private static final String CLASSLOADER_DEFAULT = "default";
private static final String CLASSLOADER_CONTEXT = "context";
public static <S> ServiceLoader<S> getServiceLoader(Class<S> clazz) {
if (shouldUseContextClassloader()) {
return ServiceLoader.load(clazz);
} else {
return ServiceLoader.load(clazz, clazz.getClassLoader());
}
}
public static boolean shouldUseContextClassloader() {
String classloaderConf = SentinelConfig.getConfig(SentinelConfig.SPI_CLASSLOADER);
return CLASSLOADER_CONTEXT.equalsIgnoreCase(classloaderConf);
}
private ServiceLoaderUtil() {}
}

View File

@ -23,6 +23,7 @@ import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;
import com.alibaba.csp.sentinel.log.RecordLog;
import com.alibaba.csp.sentinel.spi.ServiceLoaderUtil;
import com.alibaba.csp.sentinel.spi.SpiOrder;
/**
@ -34,12 +35,13 @@ public final class SpiLoader {
private static final Map<String, ServiceLoader> SERVICE_LOADER_MAP = new ConcurrentHashMap<String, ServiceLoader>();
public static <T> T loadFirstInstance(Class<T> clazz) {
AssertUtil.notNull(clazz, "SPI class cannot be null");
try {
String key = clazz.getName();
// Not thread-safe, as it's expected to be resolved in a thread-safe context.
ServiceLoader<T> serviceLoader = SERVICE_LOADER_MAP.get(key);
if (serviceLoader == null) {
serviceLoader = ServiceLoader.load(clazz);
serviceLoader = ServiceLoaderUtil.getServiceLoader(clazz);
SERVICE_LOADER_MAP.put(key, serviceLoader);
}
@ -56,6 +58,41 @@ public final class SpiLoader {
}
}
/**
* Load the first-found specific SPI instance (excluding provided default SPI class).
* If no other SPI implementation found, then create a default SPI instance.
*
* @param clazz class of the SPI interface
* @param defaultClass class of the default SPI implementation (if no other implementation found)
* @param <T> SPI type
* @return the first specific SPI instance if exists, or else the default SPI instance
* @since 1.7.0
*/
public static <T> T loadFirstInstanceOrDefault(Class<T> clazz, Class<? extends T> defaultClass) {
AssertUtil.notNull(clazz, "SPI class cannot be null");
AssertUtil.notNull(defaultClass, "default SPI class cannot be null");
try {
String key = clazz.getName();
// Not thread-safe, as it's expected to be resolved in a thread-safe context.
ServiceLoader<T> serviceLoader = SERVICE_LOADER_MAP.get(key);
if (serviceLoader == null) {
serviceLoader = ServiceLoaderUtil.getServiceLoader(clazz);
SERVICE_LOADER_MAP.put(key, serviceLoader);
}
for (T instance : serviceLoader) {
if (instance.getClass() != defaultClass) {
return instance;
}
}
return defaultClass.newInstance();
} catch (Throwable t) {
RecordLog.warn("[SpiLoader] ERROR: loadFirstInstanceOrDefault failed", t);
t.printStackTrace();
return null;
}
}
/**
* Load the SPI instance with highest priority.
*
@ -70,7 +107,7 @@ public final class SpiLoader {
// Not thread-safe, as it's expected to be resolved in a thread-safe context.
ServiceLoader<T> serviceLoader = SERVICE_LOADER_MAP.get(key);
if (serviceLoader == null) {
serviceLoader = ServiceLoader.load(clazz);
serviceLoader = ServiceLoaderUtil.getServiceLoader(clazz);
SERVICE_LOADER_MAP.put(key, serviceLoader);
}
@ -105,7 +142,7 @@ public final class SpiLoader {
// Not thread-safe, as it's expected to be resolved in a thread-safe context.
ServiceLoader<T> serviceLoader = SERVICE_LOADER_MAP.get(key);
if (serviceLoader == null) {
serviceLoader = ServiceLoader.load(clazz);
serviceLoader = ServiceLoaderUtil.getServiceLoader(clazz);
SERVICE_LOADER_MAP.put(key, serviceLoader);
}
@ -135,7 +172,7 @@ public final class SpiLoader {
// Not thread-safe, as it's expected to be resolved in a thread-safe context.
ServiceLoader<T> serviceLoader = SERVICE_LOADER_MAP.get(key);
if (serviceLoader == null) {
serviceLoader = ServiceLoader.load(clazz);
serviceLoader = ServiceLoaderUtil.getServiceLoader(clazz);
SERVICE_LOADER_MAP.put(key, serviceLoader);
}