Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split StatisticsNodePathGenerator #34700

Merged
merged 4 commits into from
Feb 17, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperConfiguration;
import org.apache.shardingsphere.elasticjob.reg.zookeeper.ZookeeperRegistryCenter;
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.mode.node.path.statistics.StatisticsNodePathGenerator;
import org.apache.shardingsphere.mode.node.path.statistics.job.StatisticsJobNodePath;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepositoryConfiguration;

Expand Down Expand Up @@ -68,7 +68,7 @@ public void initialize() {

private CoordinatorRegistryCenter createRegistryCenter(final ModeConfiguration modeConfig) {
ClusterPersistRepositoryConfiguration repositoryConfig = (ClusterPersistRepositoryConfiguration) modeConfig.getRepository();
String namespace = repositoryConfig.getNamespace() + StatisticsNodePathGenerator.getJobPath();
String namespace = repositoryConfig.getNamespace() + new StatisticsJobNodePath().getRootPath();
CoordinatorRegistryCenter result = new ZookeeperRegistryCenter(getZookeeperConfiguration(repositoryConfig, namespace));
result.init();
return result;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,14 @@

import com.google.common.base.Strings;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.infra.metadata.statistics.TableStatistics;
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereTable;
import org.apache.shardingsphere.infra.metadata.statistics.TableStatistics;
import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
import org.apache.shardingsphere.infra.yaml.data.pojo.YamlRowStatistics;
import org.apache.shardingsphere.infra.yaml.data.swapper.YamlRowStatisticsSwapper;
import org.apache.shardingsphere.mode.node.path.statistics.StatisticsNodePathGenerator;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.statistics.database.StatisticsTableNodePath;
import org.apache.shardingsphere.mode.node.path.statistics.database.StatisticsTableRowNodePath;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;

import java.util.ArrayList;
Expand All @@ -48,10 +50,10 @@ public final class TableRowDataPersistService {
*/
public void persist(final String databaseName, final String schemaName, final String tableName, final Collection<YamlRowStatistics> rows) {
if (rows.isEmpty()) {
repository.persist(StatisticsNodePathGenerator.getTablePath(databaseName, schemaName, tableName.toLowerCase()), "");
repository.persist(new NodePathGenerator(new StatisticsTableNodePath(databaseName, schemaName)).getPath(tableName.toLowerCase()), "");
} else {
rows.forEach(
each -> repository.persist(StatisticsNodePathGenerator.getTableRowPath(databaseName, schemaName, tableName.toLowerCase(), each.getUniqueKey()), YamlEngine.marshal(each)));
rows.forEach(each -> repository.persist(new NodePathGenerator(new StatisticsTableRowNodePath(databaseName, schemaName, tableName.toLowerCase())).getPath(each.getUniqueKey()),
YamlEngine.marshal(each)));
}
}

Expand All @@ -64,7 +66,7 @@ public void persist(final String databaseName, final String schemaName, final St
* @param rows rows
*/
public void delete(final String databaseName, final String schemaName, final String tableName, final Collection<YamlRowStatistics> rows) {
rows.forEach(each -> repository.delete(StatisticsNodePathGenerator.getTableRowPath(databaseName, schemaName, tableName.toLowerCase(), each.getUniqueKey())));
rows.forEach(each -> repository.delete(new NodePathGenerator(new StatisticsTableRowNodePath(databaseName, schemaName, tableName.toLowerCase())).getPath(each.getUniqueKey())));
}

/**
Expand All @@ -78,8 +80,8 @@ public void delete(final String databaseName, final String schemaName, final Str
public TableStatistics load(final String databaseName, final String schemaName, final ShardingSphereTable table) {
TableStatistics result = new TableStatistics(table.getName());
YamlRowStatisticsSwapper swapper = new YamlRowStatisticsSwapper(new ArrayList<>(table.getAllColumns()));
for (String each : repository.getChildrenKeys(StatisticsNodePathGenerator.getTablePath(databaseName, schemaName, table.getName()))) {
String yamlRow = repository.query(StatisticsNodePathGenerator.getTableRowPath(databaseName, schemaName, table.getName(), each));
for (String each : repository.getChildrenKeys(new NodePathGenerator(new StatisticsTableNodePath(databaseName, schemaName)).getPath(table.getName()))) {
String yamlRow = repository.query(new NodePathGenerator(new StatisticsTableRowNodePath(databaseName, schemaName, table.getName())).getPath(each));
if (!Strings.isNullOrEmpty(yamlRow)) {
result.getRows().add(swapper.swapToObject(YamlEngine.unmarshal(yamlRow, YamlRowStatistics.class)));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@
import org.apache.shardingsphere.infra.yaml.data.pojo.YamlRowStatistics;
import org.apache.shardingsphere.infra.yaml.data.swapper.YamlRowStatisticsSwapper;
import org.apache.shardingsphere.mode.metadata.persist.metadata.service.TableRowDataPersistService;
import org.apache.shardingsphere.mode.node.path.statistics.StatisticsNodePathGenerator;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.statistics.database.StatisticsDatabaseNodePath;
import org.apache.shardingsphere.mode.node.path.statistics.database.StatisticsSchemaNodePath;
import org.apache.shardingsphere.mode.node.path.statistics.database.StatisticsTableNodePath;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;

import java.util.ArrayList;
Expand Down Expand Up @@ -54,7 +57,7 @@ public StatisticsPersistService(final PersistRepository repository) {
* @return statistics
*/
public ShardingSphereStatistics load(final ShardingSphereMetaData metaData) {
Collection<String> databaseNames = repository.getChildrenKeys(StatisticsNodePathGenerator.getDatabasesRootPath());
Collection<String> databaseNames = repository.getChildrenKeys(new StatisticsDatabaseNodePath().getRootPath());
if (databaseNames.isEmpty()) {
return new ShardingSphereStatistics();
}
Expand All @@ -67,15 +70,15 @@ public ShardingSphereStatistics load(final ShardingSphereMetaData metaData) {

private DatabaseStatistics load(final ShardingSphereDatabase database) {
DatabaseStatistics result = new DatabaseStatistics();
for (String each : repository.getChildrenKeys(StatisticsNodePathGenerator.getSchemaRootPath(database.getName())).stream().filter(database::containsSchema).collect(Collectors.toList())) {
for (String each : repository.getChildrenKeys(new StatisticsSchemaNodePath(database.getName()).getRootPath()).stream().filter(database::containsSchema).collect(Collectors.toList())) {
result.putSchemaStatistics(each, load(database.getName(), database.getSchema(each)));
}
return result;
}

private SchemaStatistics load(final String databaseName, final ShardingSphereSchema schema) {
SchemaStatistics result = new SchemaStatistics();
for (String each : repository.getChildrenKeys(StatisticsNodePathGenerator.getTableRootPath(databaseName, schema.getName())).stream().filter(schema::containsTable)
for (String each : repository.getChildrenKeys(new StatisticsTableNodePath(databaseName, schema.getName()).getRootPath()).stream().filter(schema::containsTable)
.collect(Collectors.toList())) {
result.putTableStatistics(each, tableRowDataPersistService.load(databaseName, schema.getName(), schema.getTable(each)));

Expand All @@ -98,7 +101,7 @@ public void persist(final ShardingSphereDatabase database, final String schemaNa
}

private void persistSchema(final String databaseName, final String schemaName) {
repository.persist(StatisticsNodePathGenerator.getSchemaPath(databaseName, schemaName), "");
repository.persist(new NodePathGenerator(new StatisticsSchemaNodePath(databaseName)).getPath(schemaName), "");
}

private void persistTableData(final ShardingSphereDatabase database, final String schemaName, final SchemaStatistics schemaStatistics) {
Expand Down Expand Up @@ -133,6 +136,6 @@ public void update(final AlteredDatabaseStatistics alteredDatabaseStatistics) {
* @param databaseName database name
*/
public void delete(final String databaseName) {
repository.delete(StatisticsNodePathGenerator.getDatabasePath(databaseName));
repository.delete(new NodePathGenerator(new StatisticsDatabaseNodePath()).getPath(databaseName));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.mode.node.path.statistics;

import org.apache.shardingsphere.mode.node.path.NodePath;

/**
* Statistics node path.
*/
public final class StatisticsNodePath implements NodePath {

private static final String ROOT_NODE = "/statistics";

@Override
public String getRootPath() {
return ROOT_NODE;
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,12 @@

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.NodePathPattern;
import org.apache.shardingsphere.mode.node.path.statistics.database.StatisticsDatabaseNodePath;
import org.apache.shardingsphere.mode.node.path.statistics.database.StatisticsSchemaNodePath;
import org.apache.shardingsphere.mode.node.path.statistics.database.StatisticsTableNodePath;
import org.apache.shardingsphere.mode.node.path.statistics.database.StatisticsTableRowNodePath;

import java.util.Optional;
import java.util.regex.Matcher;
Expand All @@ -42,7 +47,7 @@ public final class StatisticsNodePathParser {
*/
public static Optional<String> findDatabaseName(final String path, final boolean containsChildPath) {
String endPattern = containsChildPath ? "?" : "$";
Pattern pattern = Pattern.compile(StatisticsNodePathGenerator.getDatabasePath(NodePathPattern.IDENTIFIER) + endPattern, Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(new NodePathGenerator(new StatisticsDatabaseNodePath()).getPath(NodePathPattern.IDENTIFIER) + endPattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty();
}
Expand All @@ -56,7 +61,7 @@ public static Optional<String> findDatabaseName(final String path, final boolean
*/
public static Optional<String> findSchemaName(final String path, final boolean containsChildPath) {
String endPattern = containsChildPath ? "?" : "$";
Pattern pattern = Pattern.compile(StatisticsNodePathGenerator.getSchemaPath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER) + endPattern, Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(new NodePathGenerator(new StatisticsSchemaNodePath()).getPath(NodePathPattern.IDENTIFIER) + endPattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(2)) : Optional.empty();
}
Expand All @@ -70,8 +75,7 @@ public static Optional<String> findSchemaName(final String path, final boolean c
*/
public static Optional<String> findTableName(final String path, final boolean containsChildPath) {
String endPattern = containsChildPath ? "?" : "$";
Pattern pattern = Pattern.compile(
StatisticsNodePathGenerator.getTablePath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER) + endPattern, Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(new NodePathGenerator(new StatisticsTableNodePath()).getPath(NodePathPattern.IDENTIFIER) + endPattern, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(3)) : Optional.empty();
}
Expand All @@ -83,8 +87,7 @@ public static Optional<String> findTableName(final String path, final boolean co
* @return found row unique key
*/
public static Optional<String> findRowUniqueKey(final String path) {
Pattern pattern = Pattern.compile(
StatisticsNodePathGenerator.getTableRowPath(NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER, NodePathPattern.IDENTIFIER, UNIQUE_KEY_PATTERN) + "$", Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(new NodePathGenerator(new StatisticsTableRowNodePath()).getPath(UNIQUE_KEY_PATTERN) + "$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(path);
return matcher.find() ? Optional.of(matcher.group(4)) : Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.shardingsphere.mode.node.path.statistics.database;

import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.statistics.StatisticsNodePath;

/**
* Statistics database node path.
*/
public final class StatisticsDatabaseNodePath implements NodePath {

private static final String DATABASES_NODE = "databases";

private final NodePathGenerator nodePathGenerator = new NodePathGenerator(new StatisticsNodePath());

@Override
public String getRootPath() {
return nodePathGenerator.getPath(DATABASES_NODE);
}
}
Loading
Loading