Skip to content

Commit d1463e5

Browse files
committed
Add ContextManagerLifecycleListenerModeRequired
1 parent 69e9cf4 commit d1463e5

File tree

6 files changed

+98
-9
lines changed

6 files changed

+98
-9
lines changed

features/readwrite-splitting/core/src/main/java/org/apache/shardingsphere/readwritesplitting/listener/ReadwriteSplittingContextManagerLifecycleListener.java

+2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,14 @@
2323
import org.apache.shardingsphere.infra.state.datasource.qualified.QualifiedDataSourceState;
2424
import org.apache.shardingsphere.mode.manager.ContextManager;
2525
import org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListener;
26+
import org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListenerModeRequired;
2627

2728
import java.util.Map;
2829

2930
/**
3031
* Readwrite splitting context manager lifecycle listener.
3132
*/
33+
@ContextManagerLifecycleListenerModeRequired("Cluster")
3234
public class ReadwriteSplittingContextManagerLifecycleListener implements ContextManagerLifecycleListener {
3335

3436
@Override

kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/listener/PipelineContextManagerLifecycleListener.java

+2
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,15 @@
3535
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
3636
import org.apache.shardingsphere.mode.manager.ContextManager;
3737
import org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListener;
38+
import org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListenerModeRequired;
3839

3940
import java.util.List;
4041
import java.util.stream.Collectors;
4142

4243
/**
4344
* Pipeline context manager lifecycle listener.
4445
*/
46+
@ContextManagerLifecycleListenerModeRequired("Cluster")
4547
@Slf4j
4648
public final class PipelineContextManagerLifecycleListener implements ContextManagerLifecycleListener {
4749

kernel/schedule/core/src/main/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectContextManagerLifecycleListener.java

+2
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,12 @@
2020
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
2121
import org.apache.shardingsphere.mode.manager.ContextManager;
2222
import org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListener;
23+
import org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListenerModeRequired;
2324

2425
/**
2526
* Statistics collect context manager lifecycle listener.
2627
*/
28+
@ContextManagerLifecycleListenerModeRequired("Cluster")
2729
public final class StatisticsCollectContextManagerLifecycleListener implements ContextManagerLifecycleListener {
2830

2931
@Override

mode/core/src/main/java/org/apache/shardingsphere/mode/manager/ContextManager.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,10 @@
3939
import org.apache.shardingsphere.infra.metadata.database.schema.model.ShardingSphereSchema;
4040
import org.apache.shardingsphere.infra.metadata.statistics.builder.ShardingSphereStatisticsFactory;
4141
import org.apache.shardingsphere.infra.rule.builder.global.GlobalRulesBuilder;
42-
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
43-
import org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListener;
44-
import org.apache.shardingsphere.mode.metadata.manager.MetaDataContextManager;
42+
import org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListenerFactory;
4543
import org.apache.shardingsphere.mode.metadata.MetaDataContexts;
4644
import org.apache.shardingsphere.mode.metadata.factory.MetaDataContextsFactory;
45+
import org.apache.shardingsphere.mode.metadata.manager.MetaDataContextManager;
4746
import org.apache.shardingsphere.mode.metadata.manager.resource.SwitchingResource;
4847
import org.apache.shardingsphere.mode.persist.PersistServiceFacade;
4948
import org.apache.shardingsphere.mode.spi.repository.PersistRepository;
@@ -80,9 +79,7 @@ public ContextManager(final MetaDataContexts metaDataContexts, final ComputeNode
8079
persistServiceFacade = new PersistServiceFacade(repository, computeNodeInstanceContext.getModeConfiguration(), metaDataContextManager);
8180
stateContext = new StateContext(persistServiceFacade.getStateService().load());
8281
executorEngine = ExecutorEngine.createExecutorEngineWithSize(metaDataContexts.getMetaData().getProps().<Integer>getValue(ConfigurationPropertyKey.KERNEL_EXECUTOR_SIZE));
83-
if (computeNodeInstanceContext.getModeConfiguration().isCluster()) {
84-
ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class).forEach(each -> each.onInitialized(this));
85-
}
82+
ContextManagerLifecycleListenerFactory.getListeners(this).forEach(each -> each.onInitialized(this));
8683
}
8784

8885
/**
@@ -231,9 +228,7 @@ public String getPreSelectedDatabaseName() {
231228

232229
@Override
233230
public void close() {
234-
if (computeNodeInstanceContext.getModeConfiguration().isCluster()) {
235-
ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class).forEach(each -> each.onDestroyed(this));
236-
}
231+
ContextManagerLifecycleListenerFactory.getListeners(this).forEach(each -> each.onDestroyed(this));
237232
executorEngine.close();
238233
metaDataContexts.getMetaData().close();
239234
persistServiceFacade.close();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.mode.manager.listener;
19+
20+
import lombok.AccessLevel;
21+
import lombok.NoArgsConstructor;
22+
import org.apache.shardingsphere.infra.spi.ShardingSphereServiceLoader;
23+
import org.apache.shardingsphere.mode.manager.ContextManager;
24+
25+
import java.util.Collection;
26+
import java.util.LinkedList;
27+
28+
/**
29+
* Context manager lifecycle listener factory.
30+
*/
31+
@NoArgsConstructor(access = AccessLevel.PRIVATE)
32+
public final class ContextManagerLifecycleListenerFactory {
33+
34+
/**
35+
* Get context manager lifecycle listeners.
36+
*
37+
* @param contextManager context manager
38+
* @return got listeners
39+
*/
40+
public static Collection<ContextManagerLifecycleListener> getListeners(final ContextManager contextManager) {
41+
Collection<ContextManagerLifecycleListener> result = new LinkedList<>();
42+
for (ContextManagerLifecycleListener each : ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class)) {
43+
ContextManagerLifecycleListenerModeRequired modeRequired = each.getClass().getAnnotation(ContextManagerLifecycleListenerModeRequired.class);
44+
if (null == modeRequired || modeRequired.value().equals(contextManager.getComputeNodeInstanceContext().getModeConfiguration().getType())) {
45+
result.add(each);
46+
}
47+
}
48+
return result;
49+
}
50+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
package org.apache.shardingsphere.mode.manager.listener;
19+
20+
import java.lang.annotation.ElementType;
21+
import java.lang.annotation.Retention;
22+
import java.lang.annotation.RetentionPolicy;
23+
import java.lang.annotation.Target;
24+
25+
/**
26+
* Context manager lifecycle listener mode required.
27+
*/
28+
@Target(ElementType.TYPE)
29+
@Retention(RetentionPolicy.RUNTIME)
30+
public @interface ContextManagerLifecycleListenerModeRequired {
31+
32+
/**
33+
* Get mode type.
34+
*
35+
* @return mode type
36+
*/
37+
String value();
38+
}

0 commit comments

Comments
 (0)