Skip to content

Commit

Permalink
Refactor StatesNodePath (#34717)
Browse files Browse the repository at this point in the history
* Refactor StatesNodePath

* Refactor StatesNodePath

* Refactor StatesNodePath
  • Loading branch information
terrymanu authored Feb 19, 2025
1 parent 0697712 commit e5897df
Show file tree
Hide file tree
Showing 14 changed files with 197 additions and 98 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import com.google.common.base.Strings;
import lombok.RequiredArgsConstructor;
import org.apache.shardingsphere.mode.node.path.state.StatesNodePathGenerator;
import org.apache.shardingsphere.mode.node.path.state.cluster.ClusterNodePath;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;

/**
Expand All @@ -36,7 +36,7 @@ public final class ClusterStatePersistService {
* @param state to be updated cluster state
*/
public void update(final ClusterState state) {
repository.persist(StatesNodePathGenerator.getClusterStatePath(), state.name());
repository.persist(new ClusterNodePath().getRootPath(), state.name());
}

/**
Expand All @@ -45,7 +45,7 @@ public void update(final ClusterState state) {
* @return loaded cluster state
*/
public ClusterState load() {
String value = repository.query(StatesNodePathGenerator.getClusterStatePath());
String value = repository.query(new ClusterNodePath().getRootPath());
return Strings.isNullOrEmpty(value) ? ClusterState.OK : ClusterState.valueOf(value);
}
}
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.state;

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

/**
* States node path.
*/
public final class StatesNodePath implements NodePath {

private static final String ROOT_NODE = "/states";

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

This file was deleted.

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.state.cluster;

import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.state.StatesNodePath;

/**
* Cluster node path.
*/
public final class ClusterNodePath implements NodePath {

private static final String ROOT_NODE = "cluster_state";

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

@Override
public String getRootPath() {
return nodePathGenerator.getPath(ROOT_NODE);
}
}
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.state.database;

import org.apache.shardingsphere.mode.node.path.NodePath;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.state.StatesNodePath;

/**
* Database listener coordinator node path.
*/
public final class DatabaseListenerCoordinatorNodePath implements NodePath {

private static final String ROOT_NODE = "database_listener_coordinator";

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

@Override
public String getRootPath() {
return nodePathGenerator.getPath(ROOT_NODE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,21 @@
* limitations under the License.
*/

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

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;

import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* States node path parser.
* Database listener coordinator node path parser.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class StatesNodePathParser {
public final class DatabaseListenerCoordinatorNodePathParser {

private static final String DATABASE_PATTERN = "(\\w+)";

Expand All @@ -39,7 +40,7 @@ public final class StatesNodePathParser {
* @return found database name
*/
public static Optional<String> findDatabaseName(final String databaseListenerCoordinatorNodePath) {
Pattern pattern = Pattern.compile(StatesNodePathGenerator.getDatabaseListenerCoordinatorNodePath(DATABASE_PATTERN) + "$", Pattern.CASE_INSENSITIVE);
Pattern pattern = Pattern.compile(new NodePathGenerator(new DatabaseListenerCoordinatorNodePath()).getPath(DATABASE_PATTERN) + "$", Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(databaseListenerCoordinatorNodePath);
return matcher.find() ? Optional.of(matcher.group(1)) : Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,7 @@
class StatesNodePathTest {

@Test
void assertGetClusterStatePath() {
assertThat(StatesNodePathGenerator.getClusterStatePath(), is("/states/cluster_state"));
}

@Test
void assertGetDatabaseListenerCoordinatorNodeRootPath() {
assertThat(StatesNodePathGenerator.getDatabaseListenerCoordinatorNodeRootPath(), is("/states/database_listener_coordinator"));
}

@Test
void assertGetDatabaseListenerCoordinatorNodePath() {
assertThat(StatesNodePathGenerator.getDatabaseListenerCoordinatorNodePath("foo_db"), is("/states/database_listener_coordinator/foo_db"));
void assertGetRootPath() {
assertThat(new StatesNodePath().getRootPath(), is("/states"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.state.cluster;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class ClusterNodePathTest {

@Test
void assertGetRootPath() {
assertThat(new ClusterNodePath().getRootPath(), is("/states/cluster_state"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* limitations under the License.
*/

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

import org.junit.jupiter.api.Test;

Expand All @@ -25,11 +25,11 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertFalse;

class StatesNodePathParserTest {
class DatabaseListenerCoordinatorNodePathParserTest {

@Test
void assertFindDatabaseName() {
assertThat(StatesNodePathParser.findDatabaseName("/states/database_listener_coordinator/foo_db"), is(Optional.of("foo_db")));
assertFalse(StatesNodePathParser.findDatabaseName("/states/database_listener_coordinator").isPresent());
assertThat(DatabaseListenerCoordinatorNodePathParser.findDatabaseName("/states/database_listener_coordinator/foo_db"), is(Optional.of("foo_db")));
assertFalse(DatabaseListenerCoordinatorNodePathParser.findDatabaseName("/states/database_listener_coordinator").isPresent());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* 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.state.database;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;

class DatabaseListenerCoordinatorNodePathTest {

@Test
void assertGetRootPath() {
assertThat(new DatabaseListenerCoordinatorNodePath().getRootPath(), is("/states/database_listener_coordinator"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@

package org.apache.shardingsphere.mode.manager.cluster.dispatch.handler.global.type;

import org.apache.shardingsphere.mode.manager.cluster.dispatch.handler.global.GlobalDataChangedEventHandler;
import org.apache.shardingsphere.mode.node.path.state.StatesNodePathGenerator;
import org.apache.shardingsphere.mode.event.DataChangedEvent;
import org.apache.shardingsphere.mode.event.DataChangedEvent.Type;
import org.apache.shardingsphere.mode.manager.ContextManager;
import org.apache.shardingsphere.mode.manager.cluster.dispatch.handler.global.GlobalDataChangedEventHandler;
import org.apache.shardingsphere.mode.node.path.state.cluster.ClusterNodePath;
import org.apache.shardingsphere.mode.state.cluster.ClusterState;

import java.util.Arrays;
Expand All @@ -34,7 +34,7 @@ public final class ClusterStateChangedHandler implements GlobalDataChangedEventH

@Override
public String getSubscribedKey() {
return StatesNodePathGenerator.getClusterStatePath();
return new ClusterNodePath().getRootPath();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@
import org.apache.shardingsphere.mode.metadata.refresher.statistics.StatisticsRefreshEngine;
import org.apache.shardingsphere.mode.node.path.NodePathGenerator;
import org.apache.shardingsphere.mode.node.path.metadata.DatabaseNodePath;
import org.apache.shardingsphere.mode.node.path.state.StatesNodePathGenerator;
import org.apache.shardingsphere.mode.node.path.state.StatesNodePathParser;
import org.apache.shardingsphere.mode.node.path.state.database.DatabaseListenerCoordinatorNodePathParser;
import org.apache.shardingsphere.mode.node.path.state.database.DatabaseListenerCoordinatorNodePath;
import org.apache.shardingsphere.mode.repository.cluster.ClusterPersistRepository;

import java.util.Arrays;
Expand All @@ -42,7 +42,7 @@ public final class DatabaseListenerChangedHandler implements GlobalDataChangedEv

@Override
public String getSubscribedKey() {
return StatesNodePathGenerator.getDatabaseListenerCoordinatorNodeRootPath();
return new DatabaseListenerCoordinatorNodePath().getRootPath();
}

@Override
Expand All @@ -52,7 +52,8 @@ public Collection<Type> getSubscribedTypes() {

@Override
public void handle(final ContextManager contextManager, final DataChangedEvent event) {
StatesNodePathParser.findDatabaseName(event.getKey()).ifPresent(optional -> handle(contextManager, optional, ClusterDatabaseListenerCoordinatorType.valueOf(event.getValue())));
DatabaseListenerCoordinatorNodePathParser.findDatabaseName(event.getKey())
.ifPresent(optional -> handle(contextManager, optional, ClusterDatabaseListenerCoordinatorType.valueOf(event.getValue())));
}

private static void handle(final ContextManager contextManager, final String databaseName, final ClusterDatabaseListenerCoordinatorType clusterDatabaseListenerCoordinatorType) {
Expand Down
Loading

0 comments on commit e5897df

Please sign in to comment.