Skip to content

Commit

Permalink
Refactor QualifiedDataSourceNodePath (#34262)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Jan 6, 2025
1 parent c660d1c commit 3366c5e
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand All @@ -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<QualifiedDataSource> extractQualifiedDataSource(final String qualifiedDataSourcePath) {
Pattern pattern = Pattern.compile(getRootPath() + "/(\\S+)$", Pattern.CASE_INSENSITIVE);
public static Optional<QualifiedDataSource> 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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<QualifiedDataSource> actual = QualifiedDataSourceNode.extractQualifiedDataSource("/nodes/qualified_data_sources/replica_query_db.readwrite_ds.replica_ds_0");
void assertFindQualifiedDataSource() {
Optional<QualifiedDataSource> 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<QualifiedDataSource> actual = QualifiedDataSourceNodePath.findQualifiedDataSource("/nodes/xxx/");
assertFalse(actual.isPresent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -46,10 +46,10 @@ public final class QualifiedDataSourceStatePersistService {
* @return loaded qualified data source states
*/
public Map<String, QualifiedDataSourceState> load() {
Collection<String> qualifiedDataSourceNodes = repository.getChildrenKeys(QualifiedDataSourceNode.getRootPath());
Collection<String> qualifiedDataSourceNodes = repository.getChildrenKeys(QualifiedDataSourceNodePath.getRootPath());
Map<String, QualifiedDataSourceState> 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)));
}
Expand All @@ -67,7 +67,7 @@ public Map<String, QualifiedDataSourceState> 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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -43,7 +43,7 @@ public final class QualifiedDataSourceChangedHandler implements DataChangedEvent

@Override
public String getSubscribedKey() {
return QualifiedDataSourceNode.getRootPath();
return QualifiedDataSourceNodePath.getRootPath();
}

@Override
Expand All @@ -56,7 +56,7 @@ public void handle(final ContextManager contextManager, final DataChangedEvent e
if (Strings.isNullOrEmpty(event.getValue())) {
return;
}
Optional<QualifiedDataSource> qualifiedDataSource = QualifiedDataSourceNode.extractQualifiedDataSource(event.getKey());
Optional<QualifiedDataSource> qualifiedDataSource = QualifiedDataSourceNodePath.findQualifiedDataSource(event.getKey());
if (!qualifiedDataSource.isPresent()) {
return;
}
Expand Down

0 comments on commit 3366c5e

Please sign in to comment.