Skip to content

Commit 59c0a77

Browse files
authored
Refactor usage of ContextManagerLifecycleListener.onInitialized (#35014)
* Refactor usage of ContextManagerLifecycleListener.onInitialized * Refactor usage of ContextManagerLifecycleListener.onInitialized * Refactor usage of ContextManagerLifecycleListener.onInitialized * Refactor usage of ContextManagerLifecycleListener.onInitialized
1 parent 186b8e0 commit 59c0a77

File tree

5 files changed

+11
-29
lines changed

5 files changed

+11
-29
lines changed

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

+4-10
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,6 @@ public class ReadwriteSplittingContextManagerLifecycleListener implements Contex
3333

3434
@Override
3535
public void onInitialized(final ContextManager contextManager) {
36-
if (contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster()) {
37-
updateQualifiedDataSourceState(contextManager);
38-
}
39-
}
40-
41-
@Override
42-
public void onDestroyed(final ContextManager contextManager) {
43-
}
44-
45-
private void updateQualifiedDataSourceState(final ContextManager contextManager) {
4636
Map<String, QualifiedDataSourceState> qualifiedDataSourceStateMap = contextManager.getPersistServiceFacade().getQualifiedDataSourceStateService().load();
4737
qualifiedDataSourceStateMap.forEach((key, value) -> updateQualifiedDataSourceState(contextManager.getMetaDataContexts().getMetaData(), new QualifiedDataSource(key), value));
4838
}
@@ -54,4 +44,8 @@ private void updateQualifiedDataSourceState(final ShardingSphereMetaData metaDat
5444
}
5545
});
5646
}
47+
48+
@Override
49+
public void onDestroyed(final ContextManager contextManager) {
50+
}
5751
}

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

+1-7
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import org.apache.shardingsphere.elasticjob.infra.spi.ElasticJobServiceLoader;
3333
import org.apache.shardingsphere.elasticjob.lite.lifecycle.api.JobConfigurationAPI;
3434
import org.apache.shardingsphere.elasticjob.lite.lifecycle.domain.JobBriefInfo;
35-
import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
3635
import org.apache.shardingsphere.infra.database.core.DefaultDatabase;
3736
import org.apache.shardingsphere.mode.manager.ContextManager;
3837
import org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListener;
@@ -48,17 +47,12 @@ public final class PipelineContextManagerLifecycleListener implements ContextMan
4847

4948
@Override
5049
public void onInitialized(final ContextManager contextManager) {
51-
ModeConfiguration modeConfig = contextManager.getComputeNodeInstanceContext().getModeConfiguration();
52-
if (!contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster()) {
53-
log.info("mode type is not Cluster, mode type='{}', ignore", modeConfig.getType());
54-
return;
55-
}
5650
String preSelectedDatabaseName = contextManager.getPreSelectedDatabaseName();
5751
if (DefaultDatabase.LOGIC_NAME.equals(preSelectedDatabaseName)) {
5852
return;
5953
}
6054
PipelineContextKey contextKey = new PipelineContextKey(preSelectedDatabaseName, contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType());
61-
PipelineContextManager.putContext(contextKey, new PipelineContext(modeConfig, contextManager));
55+
PipelineContextManager.putContext(contextKey, new PipelineContext(contextManager.getComputeNodeInstanceContext().getModeConfiguration(), contextManager));
6256
PipelineMetaDataNodeWatcher.getInstance(contextKey);
6357
ElasticJobServiceLoader.registerTypedService(ElasticJobListener.class);
6458
try {

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ public final class StatisticsCollectContextManagerLifecycleListener implements C
2828

2929
@Override
3030
public void onInitialized(final ContextManager contextManager) {
31-
if (contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster()
32-
&& InstanceType.PROXY == contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()) {
31+
if (InstanceType.PROXY == contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()) {
3332
new StatisticsCollectJobWorker(contextManager).initialize();
3433
}
3534
}

kernel/schedule/core/src/test/java/org/apache/shardingsphere/schedule/core/job/statistics/collect/StatisticsCollectContextManagerLifecycleListenerTest.java

+2-9
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,12 @@
3030

3131
class StatisticsCollectContextManagerLifecycleListenerTest {
3232

33-
@Test
34-
void assertOnInitializedWithNotClusterMode() {
35-
ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
36-
new StatisticsCollectContextManagerLifecycleListener().onInitialized(contextManager);
37-
verify(contextManager.getComputeNodeInstanceContext()).getModeConfiguration();
38-
}
39-
4033
@Test
4134
void assertOnInitializedWithNotProxy() {
4235
ContextManager contextManager = mock(ContextManager.class, RETURNS_DEEP_STUBS);
4336
when(contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster()).thenReturn(true);
4437
new StatisticsCollectContextManagerLifecycleListener().onInitialized(contextManager);
45-
verify(contextManager.getComputeNodeInstanceContext(), times(2)).getModeConfiguration();
38+
verify(contextManager.getComputeNodeInstanceContext()).getModeConfiguration();
4639
}
4740

4841
@Test
@@ -51,7 +44,7 @@ void assertOnInitializedWithProxy() {
5144
when(contextManager.getComputeNodeInstanceContext().getModeConfiguration().isCluster()).thenReturn(true);
5245
when(contextManager.getComputeNodeInstanceContext().getInstance().getMetaData().getType()).thenReturn(InstanceType.PROXY);
5346
new StatisticsCollectContextManagerLifecycleListener().onInitialized(contextManager);
54-
verify(contextManager.getComputeNodeInstanceContext(), times(3)).getModeConfiguration();
47+
verify(contextManager.getComputeNodeInstanceContext(), times(2)).getModeConfiguration();
5548
}
5649

5750
@Test

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

+3-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,9 @@ public ContextManager(final MetaDataContexts metaDataContexts, final ComputeNode
8181
stateContext = new StateContext(persistServiceFacade.getStateService().load());
8282
executorEngine = ExecutorEngine.createExecutorEngineWithSize(metaDataContexts.getMetaData().getProps().<Integer>getValue(ConfigurationPropertyKey.KERNEL_EXECUTOR_SIZE));
8383
for (ContextManagerLifecycleListener each : ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class)) {
84-
each.onInitialized(this);
84+
if (computeNodeInstanceContext.getModeConfiguration().isCluster()) {
85+
each.onInitialized(this);
86+
}
8587
}
8688
}
8789

0 commit comments

Comments
 (0)