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

Refactor StatesNodePath #34274

Merged
merged 4 commits into from
Jan 7, 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 @@ -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);
}

Expand All @@ -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);
}

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

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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<String> 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<String> 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();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,11 @@ private Collection<MetaDataVersion> persistTuples(final Collection<RepositoryTup
List<String> 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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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), "");
}

/**
Expand Down Expand Up @@ -113,7 +113,7 @@ public void alterByRuleDropped(final String databaseName, final ShardingSphereSc
* @return schemas
*/
public Collection<ShardingSphereSchema> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

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

Expand Down Expand Up @@ -162,15 +162,8 @@ public ComputeNodeInstance loadComputeNodeInstance(final InstanceMetaData instan
* @return assigned worker IDs
*/
public Collection<Integer> getAssignedWorkerIds() {
Collection<String> childrenKeys = repository.getChildrenKeys(ComputeNodePath.getWorkerIdRootPath());
Collection<Integer> 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<String> instanceIds = repository.getChildrenKeys(ComputeNodePath.getWorkerIdRootPath());
return instanceIds.stream().map(each -> repository.query(ComputeNodePath.getWorkerIdPath(each))).filter(Objects::nonNull).map(Integer::parseInt).collect(Collectors.toSet());
}

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

/**
Expand All @@ -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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public final class ListenerAssistedChangedHandler implements DataChangedEventHan

@Override
public String getSubscribedKey() {
return StatesNodePath.getListenerAssistedNodePath();
return StatesNodePath.getListenerAssistedNodeRootPath();
}

@Override
Expand All @@ -49,7 +49,7 @@ public Collection<Type> 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) {
Expand Down
Loading