From f574e3de470088de3b95b7803ef29629f15a6c6a Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Tue, 7 Jan 2025 12:30:17 +0800 Subject: [PATCH] Refactor StatesNodePath (#34274) * Refactor ComputeNodePersistService * Refactor DatabaseMetaDataNodePath * Refactor GlobalNodePath * Refactor StatesNodePath --- .../node/DatabaseMetaDataNodePath.java | 14 ++++---- .../metadata/persist/node/GlobalNodePath.java | 24 +++++++------- .../metadata/persist/node/StatesNodePath.java | 32 ++++++++++--------- .../global/GlobalRulePersistService.java | 7 ++-- .../schema/SchemaMetaDataPersistService.java | 4 +-- .../node/DatabaseMetaDataNodePathTest.java | 8 ++--- .../persist/node/StatesNodePathTest.java | 19 ++++++----- .../unified/ComputeNodePersistService.java | 13 ++------ .../ListenerAssistedPersistService.java | 4 +-- .../ListenerAssistedChangedHandler.java | 4 +-- 10 files changed, 64 insertions(+), 65 deletions(-) diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/DatabaseMetaDataNodePath.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/DatabaseMetaDataNodePath.java index 45bb1dc955fe3..f609624221eab 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/DatabaseMetaDataNodePath.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/DatabaseMetaDataNodePath.java @@ -62,12 +62,12 @@ public static String getDatabasePath(final String databaseName) { } /** - * Get schemas path. + * Get schema root path. * * @param databaseName database name - * @return schemas path + * @return schema root path */ - public static String getSchemasPath(final String databaseName) { + public static String getSchemaRootPath(final String databaseName) { return String.join("/", getDatabasePath(databaseName), SCHEMAS_NODE); } @@ -79,17 +79,17 @@ public static String getSchemasPath(final String databaseName) { * @return schema path */ public static String getSchemaPath(final String databaseName, final String schemaName) { - return String.join("/", getSchemasPath(databaseName), schemaName); + return String.join("/", getSchemaRootPath(databaseName), schemaName); } /** - * Get tables path. + * Get table root path. * * @param databaseName database name * @param schemaName schema name - * @return tables path + * @return table root path */ - public static String getTablesPath(final String databaseName, final String schemaName) { + public static String getTableRootPath(final String databaseName, final String schemaName) { return String.join("/", getSchemaPath(databaseName, schemaName), TABLES_NODE); } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/GlobalNodePath.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/GlobalNodePath.java index c4ddc1a56f5fe..ff89f0b7f9f87 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/GlobalNodePath.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/GlobalNodePath.java @@ -46,42 +46,42 @@ public static String getRuleRootPath() { /** * Get global rule path. * - * @param ruleName rule name + * @param ruleTypeName rule type name * @return global rule path */ - public static String getRulePath(final String ruleName) { - return String.join("/", getRuleRootPath(), ruleName); + public static String getRulePath(final String ruleTypeName) { + return String.join("/", getRuleRootPath(), ruleTypeName); } /** * Get global rule versions path. * - * @param ruleName rule name + * @param ruleTypeName rule type name * @return global rule versions path */ - public static String getRuleVersionsPath(final String ruleName) { - return String.join("/", getRulePath(ruleName), VERSIONS_NODE); + public static String getRuleVersionsPath(final String ruleTypeName) { + return String.join("/", getRulePath(ruleTypeName), VERSIONS_NODE); } /** * Get global rule version path. * - * @param ruleName rule name + * @param ruleTypeName rule type name * @param version version * @return global rule version path */ - public static String getRuleVersionPath(final String ruleName, final String version) { - return String.join("/", getRuleVersionsPath(ruleName), version); + public static String getRuleVersionPath(final String ruleTypeName, final String version) { + return String.join("/", getRuleVersionsPath(ruleTypeName), version); } /** * Get global rule active version path. * - * @param ruleName rule name + * @param ruleTypeName rule type name * @return global rule active version path */ - public static String getRuleActiveVersionPath(final String ruleName) { - return String.join("/", getRulePath(ruleName), ACTIVE_VERSION_NODE); + public static String getRuleActiveVersionPath(final String ruleTypeName) { + return String.join("/", getRulePath(ruleTypeName), ACTIVE_VERSION_NODE); } /** diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/StatesNodePath.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/StatesNodePath.java index 9b4139ed10910..a33db17659488 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/StatesNodePath.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/StatesNodePath.java @@ -36,6 +36,8 @@ public final class StatesNodePath { private static final String LISTENER_ASSISTED_NODE = "listener_assisted"; + private static final String DATABASE_PATTERN = "(\\w+)"; + /** * Get cluster state path. * @@ -46,33 +48,33 @@ public static String getClusterStatePath() { } /** - * Get listener assisted node path. + * Get listener assisted node root path. * - * @return listener assisted node path + * @return listener assisted node root path */ - public static String getListenerAssistedNodePath() { + public static String getListenerAssistedNodeRootPath() { return String.join("/", ROOT_NODE, LISTENER_ASSISTED_NODE); } /** - * Get database name by listener assisted node path. + * Get database name listener assisted node path. * - * @param nodePath path - * @return database name + * @param databaseName database name + * @return database name listener assisted node path */ - public static Optional findDatabaseNameByListenerAssistedNodePath(final String nodePath) { - Pattern pattern = Pattern.compile(getListenerAssistedNodePath() + "/(\\w+)$", Pattern.CASE_INSENSITIVE); - Matcher matcher = pattern.matcher(nodePath); - return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty(); + public static String getListenerAssistedNodePath(final String databaseName) { + return String.join("/", getListenerAssistedNodeRootPath(), databaseName); } /** - * Get database name listener assisted node path. + * Find database name by listener assisted node path. * - * @param databaseName database name - * @return database name listener assisted node path + * @param listenerAssistedNodePath listener assisted node path + * @return found database name */ - public static String getDatabaseNameListenerAssistedNodePath(final String databaseName) { - return String.join("/", ROOT_NODE, LISTENER_ASSISTED_NODE, databaseName); + public static Optional findDatabaseName(final String listenerAssistedNodePath) { + Pattern pattern = Pattern.compile(getListenerAssistedNodePath(DATABASE_PATTERN) + "$", Pattern.CASE_INSENSITIVE); + Matcher matcher = pattern.matcher(listenerAssistedNodePath); + return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty(); } } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java index 63ce71ba0456c..3982d8f078bcd 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/config/global/GlobalRulePersistService.java @@ -90,10 +90,11 @@ private Collection persistTuples(final Collection versions = metaDataVersionPersistService.getVersions(GlobalNodePath.getRuleVersionsPath(each.getKey())); String nextActiveVersion = versions.isEmpty() ? MetaDataVersion.DEFAULT_VERSION : String.valueOf(Integer.parseInt(versions.get(0)) + 1); repository.persist(GlobalNodePath.getRuleVersionPath(each.getKey(), nextActiveVersion), each.getValue()); - if (Strings.isNullOrEmpty(repository.query(GlobalNodePath.getRuleActiveVersionPath(each.getKey())))) { - repository.persist(GlobalNodePath.getRuleActiveVersionPath(each.getKey()), MetaDataVersion.DEFAULT_VERSION); + String ruleActiveVersionPath = GlobalNodePath.getRuleActiveVersionPath(each.getKey()); + if (Strings.isNullOrEmpty(repository.query(ruleActiveVersionPath))) { + repository.persist(ruleActiveVersionPath, MetaDataVersion.DEFAULT_VERSION); } - result.add(new MetaDataVersion(GlobalNodePath.getRulePath(each.getKey()), repository.query(GlobalNodePath.getRuleActiveVersionPath(each.getKey())), nextActiveVersion)); + result.add(new MetaDataVersion(GlobalNodePath.getRulePath(each.getKey()), repository.query(ruleActiveVersionPath), nextActiveVersion)); } return result; } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistService.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistService.java index dce6e86961047..a9c65aecf903c 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistService.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/service/metadata/schema/SchemaMetaDataPersistService.java @@ -53,7 +53,7 @@ public SchemaMetaDataPersistService(final PersistRepository repository, final Me * @param schemaName to be added schema name */ public void add(final String databaseName, final String schemaName) { - repository.persist(DatabaseMetaDataNodePath.getTablesPath(databaseName, schemaName), ""); + repository.persist(DatabaseMetaDataNodePath.getTableRootPath(databaseName, schemaName), ""); } /** @@ -113,7 +113,7 @@ public void alterByRuleDropped(final String databaseName, final ShardingSphereSc * @return schemas */ public Collection load(final String databaseName) { - return repository.getChildrenKeys(DatabaseMetaDataNodePath.getSchemasPath(databaseName)).stream() + return repository.getChildrenKeys(DatabaseMetaDataNodePath.getSchemaRootPath(databaseName)).stream() .map(each -> new ShardingSphereSchema(each, tableMetaDataPersistService.load(databaseName, each), viewMetaDataPersistService.load(databaseName, each))).collect(Collectors.toList()); } } diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/DatabaseMetaDataNodePathTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/DatabaseMetaDataNodePathTest.java index fedbb260054f7..4cc15510b6ba8 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/DatabaseMetaDataNodePathTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/DatabaseMetaDataNodePathTest.java @@ -39,8 +39,8 @@ void assertGetDatabasePath() { } @Test - void assertGetSchemasPath() { - assertThat(DatabaseMetaDataNodePath.getSchemasPath("foo_db"), is("/metadata/foo_db/schemas")); + void assertGetSchemaRootPath() { + assertThat(DatabaseMetaDataNodePath.getSchemaRootPath("foo_db"), is("/metadata/foo_db/schemas")); } @Test @@ -49,8 +49,8 @@ void assertGetSchemaPath() { } @Test - void assertGetTablesPath() { - assertThat(DatabaseMetaDataNodePath.getTablesPath("foo_db", "foo_schema"), is("/metadata/foo_db/schemas/foo_schema/tables")); + void assertGetTableRootPath() { + assertThat(DatabaseMetaDataNodePath.getTableRootPath("foo_db", "foo_schema"), is("/metadata/foo_db/schemas/foo_schema/tables")); } @Test diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/StatesNodePathTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/StatesNodePathTest.java index 1cd3d7b716f8a..2f174fca6f13b 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/StatesNodePathTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/StatesNodePathTest.java @@ -19,9 +19,11 @@ import org.junit.jupiter.api.Test; -import static org.hamcrest.MatcherAssert.assertThat; +import java.util.Optional; + import static org.hamcrest.CoreMatchers.is; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; class StatesNodePathTest { @@ -31,17 +33,18 @@ void assertGetClusterStatePath() { } @Test - void assertGetListenerAssistedNodePath() { - assertThat(StatesNodePath.getListenerAssistedNodePath(), is("/states/listener_assisted")); + void assertGetListenerAssistedNodeRootPath() { + assertThat(StatesNodePath.getListenerAssistedNodeRootPath(), is("/states/listener_assisted")); } @Test - void assertFindDatabaseNameByListenerAssistedNodePath() { - assertTrue(StatesNodePath.findDatabaseNameByListenerAssistedNodePath("/states/listener_assisted/foo_db").isPresent()); + void assertGetListenerAssistedNodePath() { + assertThat(StatesNodePath.getListenerAssistedNodePath("foo_db"), is("/states/listener_assisted/foo_db")); } @Test - void assertGetDatabaseNameListenerAssistedNodePath() { - assertThat(StatesNodePath.getDatabaseNameListenerAssistedNodePath("foo_db"), is("/states/listener_assisted/foo_db")); + void assertFindDatabaseName() { + assertThat(StatesNodePath.findDatabaseName("/states/listener_assisted/foo_db"), is(Optional.of("foo_db"))); + assertFalse(StatesNodePath.findDatabaseName("/states/listener_assisted").isPresent()); } } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/persist/service/unified/ComputeNodePersistService.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/persist/service/unified/ComputeNodePersistService.java index 38af55fcd4e96..5c2b20548e37b 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/persist/service/unified/ComputeNodePersistService.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/persist/service/unified/ComputeNodePersistService.java @@ -34,8 +34,8 @@ import java.util.Arrays; import java.util.Collection; -import java.util.LinkedHashSet; import java.util.LinkedList; +import java.util.Objects; import java.util.Optional; import java.util.stream.Collectors; @@ -162,15 +162,8 @@ public ComputeNodeInstance loadComputeNodeInstance(final InstanceMetaData instan * @return assigned worker IDs */ public Collection getAssignedWorkerIds() { - Collection childrenKeys = repository.getChildrenKeys(ComputeNodePath.getWorkerIdRootPath()); - Collection result = new LinkedHashSet<>(childrenKeys.size(), 1F); - for (String each : childrenKeys) { - String workerId = repository.query(ComputeNodePath.getWorkerIdPath(each)); - if (null != workerId) { - result.add(Integer.parseInt(workerId)); - } - } - return result; + Collection instanceIds = repository.getChildrenKeys(ComputeNodePath.getWorkerIdRootPath()); + return instanceIds.stream().map(each -> repository.query(ComputeNodePath.getWorkerIdPath(each))).filter(Objects::nonNull).map(Integer::parseInt).collect(Collectors.toSet()); } /** diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/persist/service/unified/ListenerAssistedPersistService.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/persist/service/unified/ListenerAssistedPersistService.java index 514e9fe435506..b425d4ee758e1 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/persist/service/unified/ListenerAssistedPersistService.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/persist/service/unified/ListenerAssistedPersistService.java @@ -36,7 +36,7 @@ public final class ListenerAssistedPersistService { * @param listenerAssistedType listener assisted type */ public void persistDatabaseNameListenerAssisted(final String databaseName, final ListenerAssistedType listenerAssistedType) { - repository.persistEphemeral(StatesNodePath.getDatabaseNameListenerAssistedNodePath(databaseName), listenerAssistedType.name()); + repository.persistEphemeral(StatesNodePath.getListenerAssistedNodePath(databaseName), listenerAssistedType.name()); } /** @@ -45,6 +45,6 @@ public void persistDatabaseNameListenerAssisted(final String databaseName, final * @param databaseName database name */ public void deleteDatabaseNameListenerAssisted(final String databaseName) { - repository.delete(StatesNodePath.getDatabaseNameListenerAssistedNodePath(databaseName)); + repository.delete(StatesNodePath.getListenerAssistedNodePath(databaseName)); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/ListenerAssistedChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/ListenerAssistedChangedHandler.java index 85accfd61bd10..fabac4bc9c34c 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/ListenerAssistedChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/ListenerAssistedChangedHandler.java @@ -39,7 +39,7 @@ public final class ListenerAssistedChangedHandler implements DataChangedEventHan @Override public String getSubscribedKey() { - return StatesNodePath.getListenerAssistedNodePath(); + return StatesNodePath.getListenerAssistedNodeRootPath(); } @Override @@ -49,7 +49,7 @@ public Collection getSubscribedTypes() { @Override public void handle(final ContextManager contextManager, final DataChangedEvent event) { - StatesNodePath.findDatabaseNameByListenerAssistedNodePath(event.getKey()).ifPresent(optional -> handle(contextManager, optional, ListenerAssistedType.valueOf(event.getValue()))); + StatesNodePath.findDatabaseName(event.getKey()).ifPresent(optional -> handle(contextManager, optional, ListenerAssistedType.valueOf(event.getValue()))); } private static void handle(final ContextManager contextManager, final String databaseName, final ListenerAssistedType listenerAssistedType) {