From 818b49b13fdb1fb22c4621f9f0f2a13de0916cc8 Mon Sep 17 00:00:00 2001 From: Liang Zhang Date: Thu, 9 Nov 2023 18:28:19 +0800 Subject: [PATCH] Refactor JobCodeRegistry (#28995) --- .../common/job/type/JobCodeRegistry.java | 11 +++++------ .../pipeline/core/job/PipelineJobIdUtils.java | 4 +--- .../common/job/type}/JobCodeRegistryTest.java | 8 ++------ ...phere.data.pipeline.common.job.type.JobType | 18 ++++++++++++++++++ .../api/impl/ConsistencyCheckJobAPI.java | 2 +- .../migration/api/impl/MigrationJobAPI.java | 2 +- 6 files changed, 28 insertions(+), 17 deletions(-) rename {test/it/pipeline/src/test/java/org/apache/shardingsphere/test/it/data/pipeline/spi/job => kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/common/job/type}/JobCodeRegistryTest.java (65%) create mode 100644 kernel/data-pipeline/core/src/test/resources/META-INF/services/org.apache.shardingsphere.data.pipeline.common.job.type.JobType 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 a9e849078286f..cdd230ebf032d 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 @@ @Slf4j public final class JobCodeRegistry { - private static final Map JOB_CODE_AND_TYPE_MAP = new HashMap<>(); + private static final Map 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 d8b76b155f4df..08f06b3ddbebb 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.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 static String marshalJobIdCommonPrefix(final PipelineJobId pipelineJobId) */ 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 cc0112e8443d9..29833b8f38029 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 0000000000000..29ae2443e75e2 --- /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 633797c49d70f..b9d1a11426f47 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 @@ protected String getJobClassName() { @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 98f23834bfaeb..3eb8f36e54ca1 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 void refreshTableMetadata(final String jobId, final String databaseName) @Override public JobType getJobType() { - return TypedSPILoader.getService(JobType.class, JobCodeRegistry.getJobType(MigrationJobType.TYPE_CODE)); + return JobCodeRegistry.getJobType(MigrationJobType.TYPE_CODE); } @Override