This is an automated email from the ASF dual-hosted git repository.

chengzhang 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 818b49b13fd Refactor JobCodeRegistry (#28995)
818b49b13fd is described below

commit 818b49b13fdb1fb22c4621f9f0f2a13de0916cc8
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Nov 9 18:28:19 2023 +0800

    Refactor JobCodeRegistry (#28995)
---
 .../data/pipeline/common/job/type/JobCodeRegistry.java | 11 +++++------
 .../data/pipeline/core/job/PipelineJobIdUtils.java     |  4 +---
 .../pipeline/common/job/type}/JobCodeRegistryTest.java |  8 ++------
 ...hardingsphere.data.pipeline.common.job.type.JobType | 18 ++++++++++++++++++
 .../api/impl/ConsistencyCheckJobAPI.java               |  2 +-
 .../scenario/migration/api/impl/MigrationJobAPI.java   |  2 +-
 6 files changed, 28 insertions(+), 17 deletions(-)

diff --git 
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/job/type/JobCodeRegistry.java
 
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/job/type/JobCodeRegistry.java
index a9e84907828..cdd230ebf03 100644
--- 
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/job/type/JobCodeRegistry.java
+++ 
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/common/job/type/JobCodeRegistry.java
@@ -33,12 +33,12 @@ import java.util.Map;
 @Slf4j
 public final class JobCodeRegistry {
     
-    private static final Map<String, String> JOB_CODE_AND_TYPE_MAP = new 
HashMap<>();
+    private static final Map<String, JobType> JOB_CODE_AND_TYPE_MAP = new 
HashMap<>();
     
     static {
         for (JobType each : 
ShardingSphereServiceLoader.getServiceInstances(JobType.class)) {
             Preconditions.checkArgument(2 == each.getCode().length(), "Job 
type code length is not 2.");
-            JOB_CODE_AND_TYPE_MAP.put(each.getCode(), each.getType());
+            JOB_CODE_AND_TYPE_MAP.put(each.getCode(), each);
         }
     }
     
@@ -48,9 +48,8 @@ public final class JobCodeRegistry {
      * @param jobTypeCode job type code
      * @return job type
      */
-    public static String getJobType(final String jobTypeCode) {
-        String result = JOB_CODE_AND_TYPE_MAP.get(jobTypeCode);
-        Preconditions.checkNotNull(result, "Can not get job type by `%s`.", 
jobTypeCode);
-        return result;
+    public static JobType getJobType(final String jobTypeCode) {
+        
Preconditions.checkArgument(JOB_CODE_AND_TYPE_MAP.containsKey(jobTypeCode), 
"Can not get job type by `%s`.", jobTypeCode);
+        return JOB_CODE_AND_TYPE_MAP.get(jobTypeCode);
     }
 }
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 d8b76b155f4..08f06b3ddbe 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
@@ -30,7 +30,6 @@ import 
org.apache.shardingsphere.data.pipeline.common.job.type.JobCodeRegistry;
 import org.apache.shardingsphere.data.pipeline.common.job.type.JobType;
 import org.apache.shardingsphere.data.pipeline.common.util.InstanceTypeUtils;
 import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
 
 import java.nio.charset.StandardCharsets;
 
@@ -63,8 +62,7 @@ public final class PipelineJobIdUtils {
      */
     public static JobType parseJobType(final String jobId) {
         verifyJobId(jobId);
-        String jobTypeCode = jobId.substring(1, 3);
-        return TypedSPILoader.getService(JobType.class, 
JobCodeRegistry.getJobType(jobTypeCode));
+        return JobCodeRegistry.getJobType(jobId.substring(1, 3));
     }
     
     private static void verifyJobId(final String jobId) {
diff --git 
a/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/spi/job/JobCodeRegistryTest.java
 
b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/common/job/type/JobCodeRegistryTest.java
similarity index 65%
rename from 
test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/spi/job/JobCodeRegistryTest.java
rename to 
kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/common/job/type/JobCodeRegistryTest.java
index cc0112e8443..29833b8f380 100644
--- 
a/test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/spi/job/JobCodeRegistryTest.java
+++ 
b/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/common/job/type/JobCodeRegistryTest.java
@@ -15,11 +15,8 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.test.it.data.pipeline.spi.job;
+package org.apache.shardingsphere.data.pipeline.common.job.type;
 
-import org.apache.shardingsphere.data.pipeline.common.job.type.JobCodeRegistry;
-import 
org.apache.shardingsphere.data.pipeline.scenario.consistencycheck.ConsistencyCheckJobType;
-import 
org.apache.shardingsphere.data.pipeline.scenario.migration.MigrationJobType;
 import org.junit.jupiter.api.Test;
 
 import static org.hamcrest.CoreMatchers.is;
@@ -29,7 +26,6 @@ class JobCodeRegistryTest {
     
     @Test
     void assertGetJobType() {
-        assertThat(JobCodeRegistry.getJobType(MigrationJobType.TYPE_CODE), 
is("MIGRATION"));
-        
assertThat(JobCodeRegistry.getJobType(ConsistencyCheckJobType.TYPE_CODE), 
is("CONSISTENCY_CHECK"));
+        assertThat(JobCodeRegistry.getJobType("00").getType(), is("FIXTURE"));
     }
 }
diff --git 
a/kernel/data-pipeline/core/src/test/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.common.job.type.JobType
 
b/kernel/data-pipeline/core/src/test/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.common.job.type.JobType
new file mode 100644
index 00000000000..29ae2443e75
--- /dev/null
+++ 
b/kernel/data-pipeline/core/src/test/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.common.job.type.JobType
@@ -0,0 +1,18 @@
+#
+# 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.
+#
+
+org.apache.shardingsphere.data.pipeline.common.job.type.FixtureJobType
diff --git 
a/kernel/data-pipeline/scenario/consistencycheck/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/consistencycheck/api/impl/ConsistencyCheckJobAPI.java
 
b/kernel/data-pipeline/scenario/consistencycheck/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/consistencycheck/api/impl/ConsistencyCheckJobAPI.java
index 633797c49d7..b9d1a11426f 100644
--- 
a/kernel/data-pipeline/scenario/consistencycheck/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/consistencycheck/api/impl/ConsistencyCheckJobAPI.java
+++ 
b/kernel/data-pipeline/scenario/consistencycheck/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/consistencycheck/api/impl/ConsistencyCheckJobAPI.java
@@ -406,6 +406,6 @@ public final class ConsistencyCheckJobAPI extends 
AbstractPipelineJobAPIImpl {
     
     @Override
     public JobType getJobType() {
-        return TypedSPILoader.getService(JobType.class, 
JobCodeRegistry.getJobType(ConsistencyCheckJobType.TYPE_CODE));
+        return JobCodeRegistry.getJobType(ConsistencyCheckJobType.TYPE_CODE);
     }
 }
diff --git 
a/kernel/data-pipeline/scenario/migration/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/migration/api/impl/MigrationJobAPI.java
 
b/kernel/data-pipeline/scenario/migration/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/migration/api/impl/MigrationJobAPI.java
index 98f23834bfa..3eb8f36e54c 100644
--- 
a/kernel/data-pipeline/scenario/migration/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/migration/api/impl/MigrationJobAPI.java
+++ 
b/kernel/data-pipeline/scenario/migration/src/main/java/org/apache/shardingsphere/data/pipeline/scenario/migration/api/impl/MigrationJobAPI.java
@@ -491,7 +491,7 @@ public final class MigrationJobAPI extends 
AbstractInventoryIncrementalJobAPIImp
     
     @Override
     public JobType getJobType() {
-        return TypedSPILoader.getService(JobType.class, 
JobCodeRegistry.getJobType(MigrationJobType.TYPE_CODE));
+        return JobCodeRegistry.getJobType(MigrationJobType.TYPE_CODE);
     }
     
     @Override

Reply via email to