This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 941b1b668df Add ContextManagerLifecycleListenerModeRequired (#35016)
941b1b668df is described below
commit 941b1b668df0731785639616d57ec82c6e2986e7
Author: Liang Zhang <[email protected]>
AuthorDate: Mon Mar 17 00:14:24 2025 +0800
Add ContextManagerLifecycleListenerModeRequired (#35016)
---
...teSplittingContextManagerLifecycleListener.java | 2 +
.../PipelineContextManagerLifecycleListener.java | 2 +
...ticsCollectContextManagerLifecycleListener.java | 2 +
.../mode/manager/ContextManager.java | 13 ++----
.../ContextManagerLifecycleListenerFactory.java | 50 ++++++++++++++++++++++
...ontextManagerLifecycleListenerModeRequired.java | 38 ++++++++++++++++
6 files changed, 98 insertions(+), 9 deletions(-)
diff --git
a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/listener/ReadwriteSplittingContextManagerLifecycleListener.java
b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/listener/ReadwriteSplittingContextManagerLifecycleListener.java
index ce7e240718a..7c6b577824e 100644
---
a/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/listener/ReadwriteSplittingContextManagerLifecycleListener.java
+++
b/features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/listener/ReadwriteSplittingContextManagerLifecycleListener.java
@@ -23,12 +23,14 @@ import
org.apache.shardingsphere.infra.rule.attribute.datasource.StaticDataSourc
import
org.apache.shardingsphere.infra.state.datasource.qualified.QualifiedDataSourceState;
import org.apache.shardingsphere.mode.manager.ContextManager;
import
org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListener;
+import
org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListenerModeRequired;
import java.util.Map;
/**
* Readwrite splitting context manager lifecycle listener.
*/
+@ContextManagerLifecycleListenerModeRequired("Cluster")
public class ReadwriteSplittingContextManagerLifecycleListener implements
ContextManagerLifecycleListener {
@Override
diff --git
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/listener/PipelineContextManagerLifecycleListener.java
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/listener/PipelineContextManagerLifecycleListener.java
index 09c5fb2dff3..26f9c69a0da 100644
---
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/listener/PipelineContextManagerLifecycleListener.java
+++
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/listener/PipelineContextManagerLifecycleListener.java
@@ -35,6 +35,7 @@ import
org.apache.shardingsphere.elasticjob.lite.lifecycle.domain.JobBriefInfo;
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
import org.apache.shardingsphere.mode.manager.ContextManager;
import
org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListener;
+import
org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListenerModeRequired;
import java.util.List;
import java.util.stream.Collectors;
@@ -42,6 +43,7 @@ import java.util.stream.Collectors;
/**
* Pipeline context manager lifecycle listener.
*/
+@ContextManagerLifecycleListenerModeRequired("Cluster")
@Slf4j
public final class PipelineContextManagerLifecycleListener implements
ContextManagerLifecycleListener {
diff --git
a/kernel/schedule/core/src/main/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectContextManagerLifecycleListener.java
b/kernel/schedule/core/src/main/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectContextManagerLifecycleListener.java
index 361be27bf49..92532d2a46e 100644
---
a/kernel/schedule/core/src/main/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectContextManagerLifecycleListener.java
+++
b/kernel/schedule/core/src/main/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectContextManagerLifecycleListener.java
@@ -20,10 +20,12 @@ package
org.apache.shardingsphere.schedule.core.job.statistics.collect;
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
import org.apache.shardingsphere.mode.manager.ContextManager;
import
org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListener;
+import
org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListenerModeRequired;
/**
* Statistics collect context manager lifecycle listener.
*/
+@ContextManagerLifecycleListenerModeRequired("Cluster")
public final class StatisticsCollectContextManagerLifecycleListener implements
ContextManagerLifecycleListener {
@Override
diff --git
a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
index c2fcf3bc097..fc427242702 100644
---
a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
+++
b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java
@@ -39,11 +39,10 @@ import
org.apache.shardingsphere.infra.metadata.database.schema.manager.GenericS
import
org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
import
org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsFactory;
import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
-import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
-import
org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListener;
-import org.apache.shardingsphere.mode.metadata.manager.MetaDataContextManager;
+import
org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListenerFactory;
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
import org.apache.shardingsphere.mode.metadata.factory.MetaDataContextsFactory;
+import org.apache.shardingsphere.mode.metadata.manager.MetaDataContextManager;
import
org.apache.shardingsphere.mode.metadata.manager.resource.SwitchingResource;
import org.apache.shardingsphere.mode.persist.PersistServiceFacade;
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;
@@ -80,9 +79,7 @@ public final class ContextManager implements AutoCloseable {
persistServiceFacade = new PersistServiceFacade(repository,
computeNodeInstanceContext.getModeConfiguration(), metaDataContextManager);
stateContext = new
StateContext(persistServiceFacade.getStateService().load());
executorEngine =
ExecutorEngine.createExecutorEngineWithSize(metaDataContexts.getMetaData().getProps().<Integer>getValue(ConfigurationPropertyKey.KERNEL_EXECUTOR_SIZE));
- if (computeNodeInstanceContext.getModeConfiguration().isCluster()) {
-
ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class).forEach(each
-> each.onInitialized(this));
- }
+ ContextManagerLifecycleListenerFactory.getListeners(this).forEach(each
-> each.onInitialized(this));
}
/**
@@ -231,9 +228,7 @@ public final class ContextManager implements AutoCloseable {
@Override
public void close() {
- if (computeNodeInstanceContext.getModeConfiguration().isCluster()) {
-
ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class).forEach(each
-> each.onDestroyed(this));
- }
+ ContextManagerLifecycleListenerFactory.getListeners(this).forEach(each
-> each.onDestroyed(this));
executorEngine.close();
metaDataContexts.getMetaData().close();
persistServiceFacade.close();
diff --git
a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/listener/ContextManagerLifecycleListenerFactory.java
b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/listener/ContextManagerLifecycleListenerFactory.java
new file mode 100644
index 00000000000..af895a7bf72
--- /dev/null
+++
b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/listener/ContextManagerLifecycleListenerFactory.java
@@ -0,0 +1,50 @@
+/*
+ * 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.manager.listener;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
+import org.apache.shardingsphere.mode.manager.ContextManager;
+
+import java.util.Collection;
+import java.util.LinkedList;
+
+/**
+ * Context manager lifecycle listener factory.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class ContextManagerLifecycleListenerFactory {
+
+ /**
+ * Get context manager lifecycle listeners.
+ *
+ * @param contextManager context manager
+ * @return got listeners
+ */
+ public static Collection<ContextManagerLifecycleListener>
getListeners(final ContextManager contextManager) {
+ Collection<ContextManagerLifecycleListener> result = new
LinkedList<>();
+ for (ContextManagerLifecycleListener each :
ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class))
{
+ ContextManagerLifecycleListenerModeRequired modeRequired =
each.getClass().getAnnotation(ContextManagerLifecycleListenerModeRequired.class);
+ if (null == modeRequired ||
modeRequired.value().equals(contextManager.getComputeNodeInstanceContext().getModeConfiguration().getType()))
{
+ result.add(each);
+ }
+ }
+ return result;
+ }
+}
diff --git
a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/listener/ContextManagerLifecycleListenerModeRequired.java
b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/listener/ContextManagerLifecycleListenerModeRequired.java
new file mode 100644
index 00000000000..fd408dbab0f
--- /dev/null
+++
b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/listener/ContextManagerLifecycleListenerModeRequired.java
@@ -0,0 +1,38 @@
+/*
+ * 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.manager.listener;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.RetentionPolicy;
+import java.lang.annotation.Target;
+
+/**
+ * Context manager lifecycle listener mode required.
+ */
+@Target(ElementType.TYPE)
+@Retention(RetentionPolicy.RUNTIME)
+public @interface ContextManagerLifecycleListenerModeRequired {
+
+ /**
+ * Get mode type.
+ *
+ * @return mode type
+ */
+ String value();
+}