diff --git a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/cluster/ReadwriteSplittingQualifiedDataSourceDeletedSubscriber.java b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/cluster/ReadwriteSplittingQualifiedDataSourceDeletedSubscriber.java index 437aa755f2338..1b9c607d7221d 100644 --- a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/cluster/ReadwriteSplittingQualifiedDataSourceDeletedSubscriber.java +++ b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/cluster/ReadwriteSplittingQualifiedDataSourceDeletedSubscriber.java @@ -19,7 +19,7 @@ import com.google.common.eventbus.Subscribe; import lombok.Setter; -import org.apache.shardingsphere.metadata.persist.node.QualifiedDataSourceNode; +import org.apache.shardingsphere.metadata.persist.node.QualifiedDataSourceNodePath; import org.apache.shardingsphere.mode.deliver.DeliverEventSubscriber; import org.apache.shardingsphere.mode.spi.PersistRepository; @@ -38,6 +38,6 @@ public final class ReadwriteSplittingQualifiedDataSourceDeletedSubscriber implem */ @Subscribe public void delete(final QualifiedDataSourceDeletedEvent event) { - repository.delete(QualifiedDataSourceNode.getQualifiedDataSourceNodePath(event.getQualifiedDataSource())); + repository.delete(QualifiedDataSourceNodePath.getQualifiedDataSourcePath(event.getQualifiedDataSource())); } } diff --git a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/QualifiedDataSourceNode.java b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/QualifiedDataSourceNodePath.java similarity index 72% rename from kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/QualifiedDataSourceNode.java rename to kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/QualifiedDataSourceNodePath.java index 735371e5348fa..f0745c2be0aa4 100644 --- a/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/QualifiedDataSourceNode.java +++ b/kernel/metadata/core/src/main/java/org/apache/shardingsphere/metadata/persist/node/QualifiedDataSourceNodePath.java @@ -26,22 +26,24 @@ import java.util.regex.Pattern; /** - * Qualified data source node. + * Qualified data source node path. */ @NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class QualifiedDataSourceNode { +public final class QualifiedDataSourceNodePath { private static final String ROOT_NODE = "nodes"; - private static final String QUALIFIED_DATA_SOURCES = "qualified_data_sources"; + private static final String QUALIFIED_DATA_SOURCES_NODE = "qualified_data_sources"; + + private static final String QUALIFIED_DATA_SOURCE_PATTERN = "(\\S+)"; /** * Get qualified data source root path. * - * @return root path of qualified data source + * @return qualified data source root path */ public static String getRootPath() { - return String.join("/", "", ROOT_NODE, QUALIFIED_DATA_SOURCES); + return String.join("/", "", ROOT_NODE, QUALIFIED_DATA_SOURCES_NODE); } /** @@ -50,18 +52,18 @@ public static String getRootPath() { * @param qualifiedDataSource qualified data source * @return qualified data source path */ - public static String getQualifiedDataSourceNodePath(final QualifiedDataSource qualifiedDataSource) { + public static String getQualifiedDataSourcePath(final QualifiedDataSource qualifiedDataSource) { return String.join("/", getRootPath(), qualifiedDataSource.toString()); } /** - * Extract qualified data source. + * Find qualified data source. * * @param qualifiedDataSourcePath qualified data source path - * @return extracted qualified data source + * @return found qualified data source */ - public static Optional extractQualifiedDataSource(final String qualifiedDataSourcePath) { - Pattern pattern = Pattern.compile(getRootPath() + "/(\\S+)$", Pattern.CASE_INSENSITIVE); + public static Optional findQualifiedDataSource(final String qualifiedDataSourcePath) { + Pattern pattern = Pattern.compile(String.join("/", getRootPath(), QUALIFIED_DATA_SOURCE_PATTERN + "$"), Pattern.CASE_INSENSITIVE); Matcher matcher = pattern.matcher(qualifiedDataSourcePath); return matcher.find() ? Optional.of(new QualifiedDataSource(matcher.group(1))) : Optional.empty(); } diff --git a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/QualifiedDataSourceNodeTest.java b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/QualifiedDataSourceNodePathTest.java similarity index 69% rename from kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/QualifiedDataSourceNodeTest.java rename to kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/QualifiedDataSourceNodePathTest.java index 11a270e9ad257..5f6072251eca6 100644 --- a/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/QualifiedDataSourceNodeTest.java +++ b/kernel/metadata/core/src/test/java/org/apache/shardingsphere/metadata/persist/node/QualifiedDataSourceNodePathTest.java @@ -24,27 +24,34 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -class QualifiedDataSourceNodeTest { +class QualifiedDataSourceNodePathTest { @Test void assertGetRootPath() { - assertThat(QualifiedDataSourceNode.getRootPath(), is("/nodes/qualified_data_sources")); + assertThat(QualifiedDataSourceNodePath.getRootPath(), is("/nodes/qualified_data_sources")); } @Test - void assertGetQualifiedDataSourceNodePath() { - assertThat(QualifiedDataSourceNode.getQualifiedDataSourceNodePath(new QualifiedDataSource("replica_query_db.readwrite_ds.replica_ds_0")), + void assertGetQualifiedDataSourcePath() { + assertThat(QualifiedDataSourceNodePath.getQualifiedDataSourcePath(new QualifiedDataSource("replica_query_db.readwrite_ds.replica_ds_0")), is("/nodes/qualified_data_sources/replica_query_db.readwrite_ds.replica_ds_0")); } @Test - void assertExtractQualifiedDataSource() { - Optional actual = QualifiedDataSourceNode.extractQualifiedDataSource("/nodes/qualified_data_sources/replica_query_db.readwrite_ds.replica_ds_0"); + void assertFindQualifiedDataSource() { + Optional actual = QualifiedDataSourceNodePath.findQualifiedDataSource("/nodes/qualified_data_sources/replica_query_db.readwrite_ds.replica_ds_0"); assertTrue(actual.isPresent()); assertThat(actual.get().getDatabaseName(), is("replica_query_db")); assertThat(actual.get().getGroupName(), is("readwrite_ds")); assertThat(actual.get().getDataSourceName(), is("replica_ds_0")); } + + @Test + void assertNotFindQualifiedDataSource() { + Optional actual = QualifiedDataSourceNodePath.findQualifiedDataSource("/nodes/xxx/"); + assertFalse(actual.isPresent()); + } } diff --git a/mode/core/src/main/java/org/apache/shardingsphere/mode/persist/service/unified/QualifiedDataSourceStatePersistService.java b/mode/core/src/main/java/org/apache/shardingsphere/mode/persist/service/unified/QualifiedDataSourceStatePersistService.java index b76d992fb2d8f..82ad7f899a6bd 100644 --- a/mode/core/src/main/java/org/apache/shardingsphere/mode/persist/service/unified/QualifiedDataSourceStatePersistService.java +++ b/mode/core/src/main/java/org/apache/shardingsphere/mode/persist/service/unified/QualifiedDataSourceStatePersistService.java @@ -24,7 +24,7 @@ import org.apache.shardingsphere.infra.util.yaml.YamlEngine; import org.apache.shardingsphere.mode.spi.PersistRepository; import org.apache.shardingsphere.infra.state.datasource.qualified.QualifiedDataSourceState; -import org.apache.shardingsphere.metadata.persist.node.QualifiedDataSourceNode; +import org.apache.shardingsphere.metadata.persist.node.QualifiedDataSourceNodePath; import org.apache.shardingsphere.infra.state.datasource.qualified.yaml.YamlQualifiedDataSourceState; import org.apache.shardingsphere.infra.state.datasource.qualified.yaml.YamlQualifiedDataSourceStateSwapper; @@ -46,10 +46,10 @@ public final class QualifiedDataSourceStatePersistService { * @return loaded qualified data source states */ public Map load() { - Collection qualifiedDataSourceNodes = repository.getChildrenKeys(QualifiedDataSourceNode.getRootPath()); + Collection qualifiedDataSourceNodes = repository.getChildrenKeys(QualifiedDataSourceNodePath.getRootPath()); Map result = new HashMap<>(qualifiedDataSourceNodes.size(), 1F); qualifiedDataSourceNodes.forEach(each -> { - String yamlContent = repository.query(QualifiedDataSourceNode.getQualifiedDataSourceNodePath(new QualifiedDataSource(each))); + String yamlContent = repository.query(QualifiedDataSourceNodePath.getQualifiedDataSourcePath(new QualifiedDataSource(each))); if (!Strings.isNullOrEmpty(yamlContent)) { result.put(each, new YamlQualifiedDataSourceStateSwapper().swapToObject(YamlEngine.unmarshal(yamlContent, YamlQualifiedDataSourceState.class))); } @@ -67,7 +67,7 @@ public Map load() { */ public void update(final String databaseName, final String groupName, final String storageUnitName, final DataSourceState dataSourceState) { QualifiedDataSourceState status = new QualifiedDataSourceState(dataSourceState); - repository.persist(QualifiedDataSourceNode.getQualifiedDataSourceNodePath( + repository.persist(QualifiedDataSourceNodePath.getQualifiedDataSourcePath( new QualifiedDataSource(databaseName, groupName, storageUnitName)), YamlEngine.marshal(new YamlQualifiedDataSourceStateSwapper().swapToYamlConfiguration(status))); } } diff --git a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/QualifiedDataSourceChangedHandler.java b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/QualifiedDataSourceChangedHandler.java index f2bb401f99bc2..d066ccd34b05b 100644 --- a/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/QualifiedDataSourceChangedHandler.java +++ b/mode/type/cluster/core/src/main/java/org/apache/shardingsphere/mode/manager/cluster/dispatch/handler/global/QualifiedDataSourceChangedHandler.java @@ -26,7 +26,7 @@ import org.apache.shardingsphere.infra.state.datasource.qualified.yaml.YamlQualifiedDataSourceState; import org.apache.shardingsphere.infra.state.datasource.qualified.yaml.YamlQualifiedDataSourceStateSwapper; import org.apache.shardingsphere.infra.util.yaml.YamlEngine; -import org.apache.shardingsphere.metadata.persist.node.QualifiedDataSourceNode; +import org.apache.shardingsphere.metadata.persist.node.QualifiedDataSourceNodePath; import org.apache.shardingsphere.mode.event.DataChangedEvent; import org.apache.shardingsphere.mode.event.DataChangedEvent.Type; import org.apache.shardingsphere.mode.manager.ContextManager; @@ -43,7 +43,7 @@ public final class QualifiedDataSourceChangedHandler implements DataChangedEvent @Override public String getSubscribedKey() { - return QualifiedDataSourceNode.getRootPath(); + return QualifiedDataSourceNodePath.getRootPath(); } @Override @@ -56,7 +56,7 @@ public void handle(final ContextManager contextManager, final DataChangedEvent e if (Strings.isNullOrEmpty(event.getValue())) { return; } - Optional qualifiedDataSource = QualifiedDataSourceNode.extractQualifiedDataSource(event.getKey()); + Optional qualifiedDataSource = QualifiedDataSourceNodePath.findQualifiedDataSource(event.getKey()); if (!qualifiedDataSource.isPresent()) { return; }