Skip to content

Commit

Permalink
Merge pull request #1542 from lilai23/develop
Browse files Browse the repository at this point in the history
reduce startup time
  • Loading branch information
Sherlockhan authored Jun 26, 2024
2 parents d71229c + f781ca5 commit 380fde4
Show file tree
Hide file tree
Showing 25 changed files with 291 additions and 142 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ agent.config.ignoredPrefixes=io.sermant
agent.config.ignoredInterfaces=org.springframework.cglib.proxy.Factory
# Specifies which classes in the plugins are allowed to be bytecode enhanced (classes in the plugins are not allowed to be bytecode enhanced by default)
agent.config.serviceInjectList=io.sermant.discovery.service.lb.filter.NopInstanceFilter,io.sermant.discovery.service.lb.DiscoveryManager,io.sermant.discovery.service.util.ApplyUtil,io.sermant.discovery.service.lb.cache.InstanceCacheManager
# Generate unmatched class name to file, used to reduce startup time for the second time and after
agent.config.preFilter.enable=false
# Path of unmatched class name file, the default path is the same directory of sermant-agent.jar
agent.config.preFilter.path=
# File name of unmatched class name, the default file is 'unmatched_class_name.txt'
agent.config.preFilter.file=
#============================= core service configuration =============================#
# Heartbeat service switch
agent.service.heartbeat.enable=false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,12 @@ agent.config.ignoredPrefixes=io.sermant
agent.config.ignoredInterfaces=org.springframework.cglib.proxy.Factory
# Specifies which classes in the plugins are allowed to be bytecode enhanced (classes in the plugins are not allowed to be bytecode enhanced by default)
agent.config.serviceInjectList=io.sermant.discovery.service.lb.filter.NopInstanceFilter,io.sermant.discovery.service.lb.DiscoveryManager,io.sermant.discovery.service.util.ApplyUtil,io.sermant.discovery.service.lb.cache.InstanceCacheManager
# Generate unmatched class name to file, used to reduce startup time for the second time and after
agent.config.preFilter.enable=false
# Path of unmatched class name file, the default path is the same directory of sermant-agent.jar
agent.config.preFilter.path=
# File name of unmatched class name, the default file is 'unmatched_class_name.txt'
agent.config.preFilter.file=
#============================= core service configuration =============================#
# Heartbeat service switch
agent.service.heartbeat.enable=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ private static URL[] listCoreImplementUrls(String coreImplementPath) throws Malf
return urlList.toArray(new URL[0]);
}

private static URL[] listCommonLibUrls(String commonLibPath) throws MalformedURLException {
private static List<URL> listCommonLibUrls(String commonLibPath) throws MalformedURLException {
File commonLibDir = new File(FileUtils.validatePath(commonLibPath));
if (!commonLibDir.exists() || !commonLibDir.isDirectory()) {
throw new FileCheckException("common lib is not exist or is not directory.");
Expand All @@ -134,6 +134,6 @@ private static URL[] listCommonLibUrls(String commonLibPath) throws MalformedURL
for (File jar : jars) {
urlList.add(jar.toURI().toURL());
}
return urlList.toArray(new URL[0]);
return urlList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -128,26 +128,26 @@ public static String getInstanceId() {
*/
public static void build(Map<String, Object> argsMap) {
implementDir = new File(FileUtils.validatePath(argsMap.get(CommonConstant.CORE_IMPLEMENT_DIR_KEY).toString()));
if (!implementDir.exists() || !implementDir.isDirectory()) {
if (!implementDir.isDirectory()) {
LOGGER.warning("Implement directory not found! ");
}
configFile = new File(FileUtils.validatePath(argsMap.get(CommonConstant.CORE_CONFIG_FILE_KEY).toString()));
if (!configFile.exists() || !configFile.isFile()) {
if (!configFile.isFile()) {
LOGGER.warning("Config file is not found! ");
}
pluginSettingFile = new File(FileUtils.validatePath(argsMap.get(CommonConstant.PLUGIN_SETTING_FILE_KEY)
.toString()));
if (!pluginSettingFile.exists() || !pluginSettingFile.isFile()) {
if (!pluginSettingFile.isFile()) {
LOGGER.warning("Plugin setting file is not found! ");
}
logSettingFile = new File(FileUtils.validatePath(argsMap.get(CommonConstant.LOG_SETTING_FILE_KEY)
.toString()));
if (!logSettingFile.exists() || !logSettingFile.isFile()) {
if (!logSettingFile.isFile()) {
LOGGER.warning("Log setting file is not found! Using default log setting file in resources.");
}
pluginPackageDir = new File(FileUtils.validatePath(argsMap.get(CommonConstant.PLUGIN_PACKAGE_DIR_KEY)
.toString()));
if (!pluginPackageDir.exists() || !pluginPackageDir.isDirectory()) {
if (!pluginPackageDir.isDirectory()) {
LOGGER.warning("Plugin package directory is not found! ");
}

Expand All @@ -162,20 +162,11 @@ public static void build(Map<String, Object> argsMap) {

static {
final String currentFile = BootArgsIndexer.class.getProtectionDomain().getCodeSource().getLocation().getPath();
JarFile jarFile = null;
try {
jarFile = new JarFile(currentFile);
try (JarFile jarFile = new JarFile(currentFile)) {
CORE_VERSION = JarFileUtils.getManifestAttr(jarFile, CommonConstant.CORE_VERSION_KEY).toString();
} catch (IOException e) {
LOGGER.severe("Failed to read the core version from the manifest file: " + currentFile);
throw new SchemaException(SchemaException.MISSING_VERSION, currentFile);
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (IOException ignored) {
LOGGER.warning("Unexpected exception occurs. ");
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public static synchronized void initialize(Map<String, Object> args) {
* @param classLoader classLoader, which determines from which class Loader api operations are performed
*/
protected static void loadConfig(File configFile, ClassLoader classLoader) {
if (configFile.exists() && configFile.isFile()) {
if (configFile.isFile()) {
doLoadConfig(configFile, classLoader);
} else {
loadDefaultConfig(classLoader);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import io.sermant.core.config.common.BaseConfig;

import java.io.File;
import java.util.Locale;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -67,11 +66,12 @@ public interface LoadConfigStrategy<T> {
T getConfigHolder(File config, Map<String, Object> argsMap);

/**
* Loading configuration: Loads the configuration information of the main carrier object to the configuration object
* Loading configuration: Loads the configuration information of the main carrier object to the configuration
* object
*
* @param holder configuration holder
* @param config configuration object
* @param <R> configuration class type
* @param <R> configuration class type
* @param isDynamic is the config loaded dynamically
* @return configuration object after processing
*/
Expand All @@ -84,7 +84,7 @@ public interface LoadConfigStrategy<T> {
*/
class DefaultLoadConfigStrategy implements LoadConfigStrategy<Object> {
/**
* 日志
* logger
*/
private static final Logger LOGGER = LoggerFactory.getLogger();

Expand All @@ -95,15 +95,15 @@ public boolean canLoad(File file) {

@Override
public Object getConfigHolder(File config, Map<String, Object> argsMap) {
LOGGER.log(Level.WARNING, String.format(Locale.ROOT, "[%s] will do nothing when reading config file. ",
DefaultLoadConfigStrategy.class.getName()));
LOGGER.log(Level.WARNING, "[{0}] will do nothing when reading config file. ",
DefaultLoadConfigStrategy.class.getName());
return argsMap;
}

@Override
public <R extends BaseConfig> R loadConfig(Object holder, R config, boolean isDynamic) {
LOGGER.log(Level.WARNING, String.format(Locale.ROOT, "[%s] will do nothing when loading config. ",
DefaultLoadConfigStrategy.class.getName()));
LOGGER.log(Level.WARNING, "[{0}] will do nothing when loading config. ",
DefaultLoadConfigStrategy.class.getName());
return config;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,13 +80,12 @@ public static void setField(Object obj, Field field, Object value) {
field.set(obj, value);
}
} catch (IllegalAccessException ignored) {
LOGGER.log(Level.WARNING, String.format(Locale.ROOT,
"Cannot access field [%s] of [%s], try forced set.",
field.getName(), field.getDeclaringClass().getName()));
LOGGER.log(Level.WARNING, "Cannot access field [{0}] of [{1}], try forced set.",
new String[]{field.getName(), field.getDeclaringClass().getName()});
forceSet(obj, field, value);
} catch (InvocationTargetException ignored) {
LOGGER.log(Level.WARNING, String.format(Locale.ROOT,
"Failed to set field [%s] of [%s].", field.getName(), field.getDeclaringClass().getName()));
LOGGER.log(Level.WARNING, "Failed to set field [{0}] of [{1}].",
new String[]{field.getName(), field.getDeclaringClass().getName()});
}
}

Expand Down Expand Up @@ -124,11 +123,11 @@ private static void forceSet(Object obj, Field field, Object value) {
field.set(obj, value);
modifiersField.setInt(field, modifiers);
} catch (NoSuchFieldException e) {
LOGGER.log(Level.WARNING, String.format(Locale.ROOT,
"Missing modifiers field [%s] of [%s]. ", field.getName(), field.getDeclaringClass().getName()));
LOGGER.log(Level.WARNING, "Missing modifiers field [{0}] of [{1}]. ",
new String[]{field.getName(), field.getDeclaringClass().getName()});
} catch (IllegalAccessException e) {
LOGGER.log(Level.WARNING, String.format(Locale.ROOT,
"Forced set field [%s] of [%s] failed.", field.getName(), field.getDeclaringClass().getName()));
LOGGER.log(Level.WARNING, "Forced set field [{0}] of [{1}] failed.",
new String[]{field.getName(), field.getDeclaringClass().getName()});
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,23 @@ public class ConfigValueUtil {
key -> key.toLowerCase(Locale.ROOT).replace('.', '-')
};

private static final Map<Class<?>, BaseTypeParser> BASE_TYPE_PARSERS = new HashMap<Class<?>, BaseTypeParser>() {
{
put(int.class, Integer::parseInt);
put(Integer.class, Integer::valueOf);
put(short.class, Short::parseShort);
put(Short.class, Short::valueOf);
put(long.class, Long::parseLong);
put(Long.class, Long::valueOf);
put(float.class, Float::parseFloat);
put(Float.class, Float::valueOf);
put(double.class, Double::parseDouble);
put(Double.class, Double::valueOf);
put(boolean.class, Boolean::parseBoolean);
put(Boolean.class, Boolean::valueOf);
}
};

/**
* function for obtaining reference config values
* <p>Priority: Startup Configuration > Environment Variables > Startup Parameters > Configuration File</p>
Expand Down Expand Up @@ -245,8 +262,8 @@ public static <K, V> Map<K, V> toMapType(String configStr, Class<K> keyType, Cla
}

/**
* Converts configuration information strings to int, short, long, float, double, enumeration, String, and
* Object, and returns null if conversion fails
* Converts configuration information strings to int, short, long, float, double, enumeration, String, and Object,
* and returns null if conversion fails
*
* @param configStr configuration information string
* @param type type of object
Expand All @@ -258,38 +275,19 @@ public static <R> R toBaseType(String configStr, Class<R> type) {
if (configStr == null || "".equals(configStr)) {
return (R) result;
}
if (type == int.class) {
result = Integer.parseInt(configStr);
} else if (type == Integer.class) {
result = Integer.valueOf(configStr);
} else if (type == short.class) {
result = Short.parseShort(configStr);
} else if (type == Short.class) {
result = Short.valueOf(configStr);
} else if (type == long.class) {
result = Long.parseLong(configStr);
} else if (type == Long.class) {
result = Long.valueOf(configStr);
} else if (type == float.class) {
result = Float.parseFloat(configStr);
} else if (type == Float.class) {
result = Float.valueOf(configStr);
} else if (type == double.class) {
result = Double.parseDouble(configStr);
} else if (type == Double.class) {
result = Double.valueOf(configStr);
} else if (type == boolean.class) {
result = Boolean.parseBoolean(configStr);
} else if (type == Boolean.class) {
result = Boolean.valueOf(configStr);
} else if (type.isEnum()) {
result = Enum.valueOf((Class) type, configStr);
} else if (type == String.class || type == Object.class) {
result = configStr;
} else {
result = null;

BaseTypeParser baseTypeParser = BASE_TYPE_PARSERS.get(type);
if (baseTypeParser != null) {
return (R) baseTypeParser.parse(configStr);
}
return (R) result;

if (type.isEnum()) {
return (R) Enum.valueOf((Class) type, configStr);
}
if (type == String.class || type == Object.class) {
return (R) configStr;
}
return null;
}

/**
Expand Down Expand Up @@ -378,8 +376,8 @@ private static String getValByFixedKey(String key, String configVal, Map<String,
}

/**
* Environment variables are read after being processed by KeyFormatter. The priorities are: Startup
* Configuration > Environment Variables > Startup Parameters > Configuration File
* Environment variables are read after being processed by KeyFormatter. The priorities are: Startup Configuration >
* Environment Variables > Startup Parameters > Configuration File
*
* @param key key
* @param argsMap argsMap
Expand Down Expand Up @@ -500,4 +498,22 @@ public interface FixedValueProvider {
*/
String getFixedValue(String key);
}

/**
* base type parser
*
* @param <R>
* @author lilai
* @since 2024-06-14
*/
@FunctionalInterface
interface BaseTypeParser<R> {
/**
* parse string to base type such as int, double
*
* @param str original string
* @return parsed object
*/
R parse(String str);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static void uninstallAll() {
* @param isDynamic Whether the plugin is dynamic
*/
public static void initPlugins(Set<String> pluginNames, boolean isDynamic) {
if (pluginNames == null || pluginNames.isEmpty()) {
if (CollectionUtils.isEmpty(pluginNames)) {
LOGGER.log(Level.WARNING, "Non plugin is configured to be initialized.");
return;
}
Expand Down Expand Up @@ -300,9 +300,7 @@ private static Optional<URL> toUrl(File file) {
*/
private static boolean processByJarFile(String pluginName, File jar, boolean ifCheckSchema,
JarFileConsumer consumer) {
JarFile jarFile = null;
try {
jarFile = new JarFile(jar);
try (JarFile jarFile = new JarFile(jar)) {
if (ifCheckSchema && !PluginSchemaValidator.checkSchema(pluginName, getRealPluginName(pluginName),
jarFile)) {
throw new SchemaException(SchemaException.UNEXPECTED_EXT_JAR, jar.getPath());
Expand All @@ -314,14 +312,6 @@ private static boolean processByJarFile(String pluginName, File jar, boolean ifC
} catch (IOException ignored) {
LOGGER.warning(String.format(Locale.ROOT, "Check schema of %s failed. ", jar.getPath()));
return false;
} finally {
if (jarFile != null) {
try {
jarFile.close();
} catch (IOException ioException) {
LOGGER.severe("Occurred ioException when close jar.");
}
}
}
}

Expand All @@ -344,7 +334,7 @@ private static String getRealPluginName(String pluginName) {
* @return all jars
*/
private static File[] listJars(File dir) {
if (!dir.exists() || !dir.isDirectory()) {
if (!dir.isDirectory()) {
return new File[0];
}
final File[] files = dir.listFiles(new FileFilter() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,24 +93,15 @@ public static void initialize(boolean isDynamic) {
* @return Plugin configuration
*/
private static PluginSetting loadSetting() {
Reader reader = null;
try {
reader = new InputStreamReader(Files.newInputStream(BootArgsIndexer.getPluginSettingFile().toPath()),
CommonConstant.DEFAULT_CHARSET);
try (Reader reader = new InputStreamReader(
Files.newInputStream(BootArgsIndexer.getPluginSettingFile().toPath()),
CommonConstant.DEFAULT_CHARSET)) {
Optional<PluginSetting> pluginSettingOptional = OperationManager.getOperation(YamlConverter.class)
.convert(reader, PluginSetting.class);
return pluginSettingOptional.orElse(null);
} catch (IOException ignored) {
LOGGER.warning("Plugin setting file is not found. ");
return new PluginSetting();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException ignored) {
LOGGER.warning("Unexpected exception occurs. ");
}
}
}
}
}
Loading

0 comments on commit 380fde4

Please sign in to comment.