This is an automated email from the ASF dual-hosted git repository.
tuichenchuxin 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 6295a0723b6 Simplify ContextManagerLifecycleListener interface and
remove mode configuration is null judge (#25205)
6295a0723b6 is described below
commit 6295a0723b65d23a4e8e2123fd6623d2898744b2
Author: zhaojinchao <[email protected]>
AuthorDate: Tue Apr 18 13:47:32 2023 +0800
Simplify ContextManagerLifecycleListener interface and remove mode
configuration is null judge (#25205)
---
.../jdbc/core/datasource/ShardingSphereDataSource.java | 10 +++++-----
.../data/pipeline/cdc/core/job/CDCJobIdTest.java | 2 +-
.../data/pipeline/core/context/PipelineContextKey.java | 14 +++++++-------
.../data/pipeline/core/job/PipelineJobIdUtils.java | 2 +-
.../listener/PipelineContextManagerLifecycleListener.java | 12 +++++-------
.../ShardingSphereDataContextManagerLifecycleListener.java | 10 +++-------
.../data/pipeline/core/context/PipelineContextKeyTest.java | 8 ++++----
.../data/pipeline/core/job/PipelineJobIdUtilsTest.java | 2 +-
.../manager/listener/ContextManagerLifecycleListener.java | 9 +++------
.../proxy/initializer/BootstrapInitializer.java | 7 +++----
.../data/pipeline/core/util/JobConfigurationBuilder.java | 2 +-
11 files changed, 34 insertions(+), 44 deletions(-)
diff --git
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
index 73789e034f9..99ea67399df 100644
---
a/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
+++
b/jdbc/core/src/main/java/org/apache/shardingsphere/driver/jdbc/core/datasource/ShardingSphereDataSource.java
@@ -60,7 +60,7 @@ public final class ShardingSphereDataSource extends
AbstractDataSourceAdapter im
this.databaseName = databaseName;
contextManager = createContextManager(databaseName, modeConfig, new
HashMap<>(), new LinkedList<>(), new Properties());
jdbcContext = new
JDBCContext(contextManager.getDataSourceMap(databaseName));
- contextManagerInitializedCallback(modeConfig, contextManager,
databaseName);
+ contextManagerInitializedCallback(databaseName, contextManager);
}
public ShardingSphereDataSource(final String databaseName, final
ModeConfiguration modeConfig, final Map<String, DataSource> dataSourceMap,
@@ -68,7 +68,7 @@ public final class ShardingSphereDataSource extends
AbstractDataSourceAdapter im
this.databaseName = databaseName;
contextManager = createContextManager(databaseName, modeConfig,
dataSourceMap, ruleConfigs, null == props ? new Properties() : props);
jdbcContext = new
JDBCContext(contextManager.getDataSourceMap(databaseName));
- contextManagerInitializedCallback(modeConfig, contextManager,
databaseName);
+ contextManagerInitializedCallback(databaseName, contextManager);
}
private ContextManager createContextManager(final String databaseName,
final ModeConfiguration modeConfig, final Map<String, DataSource> dataSourceMap,
@@ -82,10 +82,10 @@ public final class ShardingSphereDataSource extends
AbstractDataSourceAdapter im
return TypedSPILoader.getService(ContextManagerBuilder.class, null ==
modeConfig ? null : modeConfig.getType()).build(param);
}
- private void contextManagerInitializedCallback(final ModeConfiguration
modeConfig, final ContextManager contextManager, final String databaseName) {
+ private void contextManagerInitializedCallback(final String databaseName,
final ContextManager contextManager) {
for (ContextManagerLifecycleListener each :
ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class))
{
try {
- each.onInitialized(InstanceType.JDBC, databaseName,
modeConfig, contextManager);
+ each.onInitialized(databaseName, contextManager);
// CHECKSTYLE:OFF
} catch (final Exception ignored) {
// CHECKSTYLE:ON
@@ -139,7 +139,7 @@ public final class ShardingSphereDataSource extends
AbstractDataSourceAdapter im
private void contextManagerDestroyedCallback(final String databaseName) {
for (ContextManagerLifecycleListener each :
ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class))
{
try {
- each.onDestroyed(InstanceType.JDBC, databaseName);
+ each.onDestroyed(databaseName, InstanceType.JDBC);
// CHECKSTYLE:OFF
} catch (final Exception ignored) {
// CHECKSTYLE:ON
diff --git
a/kernel/data-pipeline/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/core/job/CDCJobIdTest.java
b/kernel/data-pipeline/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/core/job/CDCJobIdTest.java
index 9d6b1aade72..5255981c8b5 100644
---
a/kernel/data-pipeline/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/core/job/CDCJobIdTest.java
+++
b/kernel/data-pipeline/cdc/core/src/test/java/org/apache/shardingsphere/data/pipeline/cdc/core/job/CDCJobIdTest.java
@@ -33,7 +33,7 @@ class CDCJobIdTest {
@Test
void parseJobType() {
- PipelineContextKey contextKey =
PipelineContextKey.build(InstanceType.PROXY, "sharding_db");
+ PipelineContextKey contextKey =
PipelineContextKey.build("sharding_db", InstanceType.PROXY);
CDCJobId pipelineJobId = new CDCJobId(contextKey,
Arrays.asList("test", "t_order"), false);
String jobId =
PipelineJobIdUtils.marshalJobIdCommonPrefix(pipelineJobId) + "abcd";
JobType actualJobType = PipelineJobIdUtils.parseJobType(jobId);
diff --git
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/context/PipelineContextKey.java
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/context/PipelineContextKey.java
index 238eb25013a..43a81405c28 100644
---
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/context/PipelineContextKey.java
+++
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/context/PipelineContextKey.java
@@ -31,19 +31,19 @@ import java.util.Objects;
@Getter
public final class PipelineContextKey {
- private final InstanceType instanceType;
-
private final String databaseName;
+ private final InstanceType instanceType;
+
/**
* Build context key.
*
- * @param instanceType instance type
* @param databaseName database name
+ * @param instanceType instance type
* @return context key
*/
- public static PipelineContextKey build(final InstanceType instanceType,
final String databaseName) {
- return new PipelineContextKey(instanceType, databaseName);
+ public static PipelineContextKey build(final String databaseName, final
InstanceType instanceType) {
+ return new PipelineContextKey(databaseName, instanceType);
}
/**
@@ -52,7 +52,7 @@ public final class PipelineContextKey {
* @return context key
*/
public static PipelineContextKey buildForProxy() {
- return new PipelineContextKey(InstanceType.PROXY, "");
+ return new PipelineContextKey("", InstanceType.PROXY);
}
/**
@@ -62,7 +62,7 @@ public final class PipelineContextKey {
* @return context key
*/
public static PipelineContextKey buildForProxy(final String databaseName) {
- return new PipelineContextKey(InstanceType.PROXY, databaseName);
+ return new PipelineContextKey(databaseName, InstanceType.PROXY);
}
@Override
diff --git
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobIdUtils.java
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobIdUtils.java
index c4f27320081..73929f9f9fc 100644
---
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobIdUtils.java
+++
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobIdUtils.java
@@ -85,6 +85,6 @@ public final class PipelineJobIdUtils {
char instanceType = jobId.charAt(5);
short databaseNameLength =
Shorts.fromByteArray(Hex.decodeHex(jobId.substring(6, 10)));
String databaseName = new String(Hex.decodeHex(jobId.substring(10, 10
+ databaseNameLength)), StandardCharsets.UTF_8);
- return
PipelineContextKey.build(InstanceTypeUtils.decode(instanceType), databaseName);
+ return PipelineContextKey.build(databaseName,
InstanceTypeUtils.decode(instanceType));
}
}
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 b0191bf3101..9bdd5365a4f 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
@@ -37,10 +37,8 @@ import
org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleLi
public final class PipelineContextManagerLifecycleListener implements
ContextManagerLifecycleListener {
@Override
- public void onInitialized(final InstanceType instanceType, final String
databaseName, final ModeConfiguration modeConfig, final ContextManager
contextManager) {
- if (null == modeConfig) {
- return;
- }
+ public void onInitialized(final String databaseName, final ContextManager
contextManager) {
+ ModeConfiguration modeConfig =
contextManager.getInstanceContext().getModeConfiguration();
if (!contextManager.getInstanceContext().isCluster()) {
log.info("mode type is not Cluster, mode type='{}', ignore",
modeConfig.getType());
return;
@@ -49,14 +47,14 @@ public final class PipelineContextManagerLifecycleListener
implements ContextMan
if (DefaultDatabase.LOGIC_NAME.equals(databaseName)) {
return;
}
- PipelineContextKey contextKey = PipelineContextKey.build(instanceType,
databaseName);
+ PipelineContextKey contextKey = PipelineContextKey.build(databaseName,
contextManager.getInstanceContext().getInstance().getMetaData().getType());
PipelineContextManager.putContext(contextKey, new
PipelineContext(modeConfig, contextManager));
PipelineMetaDataNodeWatcher.getInstance(contextKey);
ElasticJobServiceLoader.registerTypedService(ElasticJobListener.class);
}
@Override
- public void onDestroyed(final InstanceType instanceType, final String
databaseName) {
-
PipelineContextManager.removeContext(PipelineContextKey.build(instanceType,
databaseName));
+ public void onDestroyed(final String databaseName, final InstanceType
instanceType) {
+
PipelineContextManager.removeContext(PipelineContextKey.build(databaseName,
instanceType));
}
}
diff --git
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/listener/ShardingSphereDataContextManagerLifecycleListener.java
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/listener/ShardingSphereDataContextManagerLifecycleListener.java
index eb310cff02e..d73b4cb8d42 100644
---
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/listener/ShardingSphereDataContextManagerLifecycleListener.java
+++
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/listener/ShardingSphereDataContextManagerLifecycleListener.java
@@ -18,7 +18,6 @@
package org.apache.shardingsphere.data.pipeline.core.listener;
import
org.apache.shardingsphere.data.pipeline.core.execute.ShardingSphereDataJobWorker;
-import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
import org.apache.shardingsphere.mode.manager.ContextManager;
import
org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleListener;
@@ -29,20 +28,17 @@ import
org.apache.shardingsphere.mode.manager.listener.ContextManagerLifecycleLi
public final class ShardingSphereDataContextManagerLifecycleListener
implements ContextManagerLifecycleListener {
@Override
- public void onInitialized(final InstanceType instanceType, final String
databaseName, final ModeConfiguration modeConfig, final ContextManager
contextManager) {
- if (null == modeConfig) {
- return;
- }
+ public void onInitialized(final String databaseName, final ContextManager
contextManager) {
if (!contextManager.getInstanceContext().isCluster()) {
return;
}
- if (instanceType != InstanceType.PROXY) {
+ if (InstanceType.PROXY !=
contextManager.getInstanceContext().getInstance().getMetaData().getType()) {
return;
}
ShardingSphereDataJobWorker.initialize(contextManager);
}
@Override
- public void onDestroyed(final InstanceType instanceType, final String
databaseName) {
+ public void onDestroyed(final String databaseName, final InstanceType
instanceType) {
}
}
diff --git
a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/context/PipelineContextKeyTest.java
b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/context/PipelineContextKeyTest.java
index 3f0811b4957..1cfccee3bb6 100644
---
a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/context/PipelineContextKeyTest.java
+++
b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/context/PipelineContextKeyTest.java
@@ -29,16 +29,16 @@ class PipelineContextKeyTest {
@Test
void assertHashCodeEqualsForProxyMode() {
- PipelineContextKey contextKey1 =
PipelineContextKey.build(InstanceType.PROXY, null);
- PipelineContextKey contextKey2 =
PipelineContextKey.build(InstanceType.PROXY, "sharding_db");
+ PipelineContextKey contextKey1 = PipelineContextKey.build(null,
InstanceType.PROXY);
+ PipelineContextKey contextKey2 =
PipelineContextKey.build("sharding_db", InstanceType.PROXY);
assertThat(contextKey1.hashCode(), is(contextKey2.hashCode()));
assertThat(contextKey1, is(contextKey2));
}
@Test
void assertHashCodeEqualsForJdbcMode() {
- PipelineContextKey contextKey1 =
PipelineContextKey.build(InstanceType.JDBC, "logic_db");
- PipelineContextKey contextKey2 =
PipelineContextKey.build(InstanceType.JDBC, "sharding_db");
+ PipelineContextKey contextKey1 = PipelineContextKey.build("logic_db",
InstanceType.JDBC);
+ PipelineContextKey contextKey2 =
PipelineContextKey.build("sharding_db", InstanceType.JDBC);
assertTrue(contextKey1.hashCode() != contextKey2.hashCode());
assertNotEquals(contextKey1, contextKey2);
}
diff --git
a/kernel/data-pipeline/scenario/migration/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobIdUtilsTest.java
b/kernel/data-pipeline/scenario/migration/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobIdUtilsTest.java
index 7d17ecadd8b..2af6ee23d0d 100644
---
a/kernel/data-pipeline/scenario/migration/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobIdUtilsTest.java
+++
b/kernel/data-pipeline/scenario/migration/src/test/java/org/apache/shardingsphere/data/pipeline/core/job/PipelineJobIdUtilsTest.java
@@ -40,7 +40,7 @@ class PipelineJobIdUtilsTest {
}
private void assertParse0(final InstanceType instanceType) {
- PipelineContextKey contextKey = PipelineContextKey.build(instanceType,
"sharding_db");
+ PipelineContextKey contextKey =
PipelineContextKey.build("sharding_db", instanceType);
MigrationJobId pipelineJobId = new MigrationJobId(contextKey,
Collections.singletonList("t_order:ds_0.t_order_0,ds_0.t_order_1"));
String jobId = new MigrationJobAPI().marshalJobId(pipelineJobId);
assertThat(PipelineJobIdUtils.parseJobType(jobId),
instanceOf(MigrationJobType.class));
diff --git
a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/listener/ContextManagerLifecycleListener.java
b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/listener/ContextManagerLifecycleListener.java
index 2eb2830f0ad..a2e0010ef19 100644
---
a/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/listener/ContextManagerLifecycleListener.java
+++
b/mode/core/src/main/java/org/apache/shardingsphere/mode/manager/listener/ContextManagerLifecycleListener.java
@@ -17,7 +17,6 @@
package org.apache.shardingsphere.mode.manager.listener;
-import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
import org.apache.shardingsphere.infra.util.spi.annotation.SingletonSPI;
import org.apache.shardingsphere.mode.manager.ContextManager;
@@ -31,18 +30,16 @@ public interface ContextManagerLifecycleListener {
/**
* Callback on initialized.
*
- * @param instanceType instance type
* @param databaseName database name
- * @param modeConfig mode configuration
* @param contextManager context manager
*/
- void onInitialized(InstanceType instanceType, String databaseName,
ModeConfiguration modeConfig, ContextManager contextManager);
+ void onInitialized(String databaseName, ContextManager contextManager);
/**
* Callback on destroyed.
*
- * @param instanceType instance type
* @param databaseName database name
+ * @param instanceType instance type
*/
- void onDestroyed(InstanceType instanceType, String databaseName);
+ void onDestroyed(String databaseName, InstanceType instanceType);
}
diff --git
a/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
b/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
index 8e46b981d07..3be0cb15d06 100644
---
a/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
+++
b/proxy/bootstrap/src/main/java/org/apache/shardingsphere/proxy/initializer/BootstrapInitializer.java
@@ -23,7 +23,6 @@ import
org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
import org.apache.shardingsphere.infra.config.props.ConfigurationPropertyKey;
import org.apache.shardingsphere.infra.instance.metadata.InstanceMetaData;
import
org.apache.shardingsphere.infra.instance.metadata.InstanceMetaDataBuilder;
-import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
import org.apache.shardingsphere.infra.util.spi.ShardingSphereServiceLoader;
import org.apache.shardingsphere.infra.util.spi.type.typed.TypedSPILoader;
import
org.apache.shardingsphere.infra.yaml.config.swapper.mode.YamlModeConfigurationSwapper;
@@ -59,7 +58,7 @@ public final class BootstrapInitializer {
ProxyConfiguration proxyConfig = new
YamlProxyConfigurationSwapper().swap(yamlConfig);
ContextManager contextManager = createContextManager(proxyConfig,
modeConfig, port, force);
ProxyContext.init(contextManager);
- contextManagerInitializedCallback(modeConfig, contextManager);
+ contextManagerInitializedCallback(contextManager);
ShardingSphereProxyVersion.setVersion(contextManager);
}
@@ -76,10 +75,10 @@ public final class BootstrapInitializer {
return TypedSPILoader.getService(InstanceMetaDataBuilder.class,
instanceType).build(port);
}
- private void contextManagerInitializedCallback(final ModeConfiguration
modeConfig, final ContextManager contextManager) {
+ private void contextManagerInitializedCallback(final ContextManager
contextManager) {
for (ContextManagerLifecycleListener each :
ShardingSphereServiceLoader.getServiceInstances(ContextManagerLifecycleListener.class))
{
try {
- each.onInitialized(InstanceType.PROXY, null, modeConfig,
contextManager);
+ each.onInitialized(null, contextManager);
// CHECKSTYLE:OFF
} catch (final Exception ex) {
// CHECKSTYLE:ON
diff --git
a/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/util/JobConfigurationBuilder.java
b/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/util/JobConfigurationBuilder.java
index 339c9c8b841..b8c1878f458 100644
---
a/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/util/JobConfigurationBuilder.java
+++
b/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/core/util/JobConfigurationBuilder.java
@@ -78,7 +78,7 @@ public final class JobConfigurationBuilder {
result.setTargetTableSchemaMap(targetTableSchemaMap);
result.setTablesFirstDataNodes("t_order:ds_0.t_order");
result.setJobShardingDataNodes(Collections.singletonList("t_order:ds_0.t_order"));
- PipelineContextKey contextKey =
PipelineContextKey.build(InstanceType.PROXY,
RandomStringUtils.randomAlphabetic(32));
+ PipelineContextKey contextKey =
PipelineContextKey.build(RandomStringUtils.randomAlphabetic(32),
InstanceType.PROXY);
result.setJobId(generateMigrationJobId(contextKey, result));
Map<String, YamlPipelineDataSourceConfiguration> sources = new
LinkedHashMap<>();
String databaseNameSuffix = RandomStringUtils.randomAlphabetic(9);