fix(config): fix config reloading errors that caused by using wrong relative paths to load config file

This commit is contained in:
2026-04-21 15:07:35 +08:00
parent 4af8feb178
commit 2927cb2b6f
4 changed files with 29 additions and 8 deletions

View File

@@ -89,7 +89,8 @@ object ConfigCenter : AutoCloseable {
fun initAll() {
val errorConfig = mutableMapOf<Path, String>()
registrations.forEach { (path, registration) ->
val pair = loadConfig(path, registration)
val configFile = paths.configDir.resolve(path).normalize()
val pair = loadConfig(configFile, registration)
if (pair != null) {
(registration as ConfigRegistration<Config>).init(pair.first, pair.second)
return@forEach
@@ -175,6 +176,7 @@ object ConfigCenter : AutoCloseable {
return field.isSynthetic || Modifier.isStatic(field.modifiers)
}
@Suppress("unused")
private fun handleUpsert(thisDir: Path, context: Path?) {
if (context == null || !Files.isRegularFile(context) || !isJsonFile(context)) {
return
@@ -182,6 +184,7 @@ object ConfigCenter : AutoCloseable {
reloadIfRegistered(context)
}
@Suppress("unused")
private fun handleDelete(thisDir: Path, context: Path?) {
if (context == null || !isJsonFile(context)) {
return
@@ -260,7 +263,7 @@ object ConfigCenter : AutoCloseable {
checkAgentStartup(!path.isAbsolute) {
AgentStartupException("Config path must be relative: $path", COMPONENT_NAME)
}
return paths.configDir.resolve(path).normalize()
return path.normalize()
}
}

View File

@@ -70,6 +70,13 @@ object LogAdviceProvider : Configurable, ConfigRegistration<AdviceLoggingConfig>
logLevel = config.logLevel
}
override fun onReload(
config: AdviceLoggingConfig,
json: JSONObject?
) {
this.logLevel = config.logLevel
}
override fun defaultConfig(): AdviceLoggingConfig = AdviceLoggingConfig(AdviceLoggingConfig.LogLevel.NONE)
}

View File

@@ -65,6 +65,10 @@ class ConfigCenterTest {
writeJson(workingDir.resolve(INVALID_PATH), "invalid-init", 1);
writeJson(workingDir.resolve(IDEMPOTENT_PATH), "idempotent-init", 1);
writeJson(configDir.resolve(INITIAL_PATH), "initial-config-dir", 1);
writeJson(configDir.resolve(NESTED_PATH), "nested-config-dir", 1);
writeJson(configDir.resolve(DELETE_PATH), "delete-config-dir", 1);
writeJson(configDir.resolve(INVALID_PATH), "invalid-config-dir", 1);
writeJson(configDir.resolve(IDEMPOTENT_PATH), "idempotent-config-dir", 1);
ConfigCenter.INSTANCE.register(() -> {
Map<Path, ConfigRegistration<? extends Config>> declared = new LinkedHashMap<>();
@@ -159,7 +163,7 @@ class ConfigCenterTest {
@Test
@Order(1)
void testStartOnlyInitializesOneRegisteredConfigAndDoesNotTriggerReload() {
Assertions.assertEquals(1, totalInitCount());
Assertions.assertEquals(5, totalInitCount());
Assertions.assertEquals(0, totalReloadCount());
}