View Javadoc
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.data.pipeline.scenario.migration;
19  
20  import org.apache.shardingsphere.data.pipeline.core.job.config.PipelineJobConfiguration;
21  import org.apache.shardingsphere.data.pipeline.core.context.TransmissionProcessContext;
22  import org.apache.shardingsphere.data.pipeline.core.datanode.DataNodeUtils;
23  import org.apache.shardingsphere.data.pipeline.core.job.progress.yaml.swapper.YamlTransmissionJobItemProgressSwapper;
24  import org.apache.shardingsphere.data.pipeline.core.job.type.PipelineJobType;
25  import org.apache.shardingsphere.data.pipeline.core.pojo.PipelineJobInfo;
26  import org.apache.shardingsphere.data.pipeline.core.pojo.PipelineJobMetaData;
27  import org.apache.shardingsphere.data.pipeline.core.consistencycheck.ConsistencyCheckJobItemProgressContext;
28  import org.apache.shardingsphere.data.pipeline.core.consistencycheck.PipelineDataConsistencyChecker;
29  import org.apache.shardingsphere.data.pipeline.core.job.id.PipelineJobIdUtils;
30  import org.apache.shardingsphere.data.pipeline.core.job.service.PipelineJobConfigurationManager;
31  import org.apache.shardingsphere.data.pipeline.scenario.migration.check.consistency.MigrationDataConsistencyChecker;
32  import org.apache.shardingsphere.data.pipeline.scenario.migration.config.MigrationJobConfiguration;
33  import org.apache.shardingsphere.data.pipeline.scenario.migration.config.yaml.swapper.YamlMigrationJobConfigurationSwapper;
34  
35  import java.util.Collection;
36  import java.util.LinkedList;
37  import java.util.Optional;
38  
39  /**
40   * Migration job type.
41   */
42  public final class MigrationJobType implements PipelineJobType {
43      
44      @Override
45      public String getCode() {
46          return "01";
47      }
48      
49      @SuppressWarnings("unchecked")
50      @Override
51      public YamlMigrationJobConfigurationSwapper getYamlJobConfigurationSwapper() {
52          return new YamlMigrationJobConfigurationSwapper();
53      }
54      
55      @SuppressWarnings("unchecked")
56      @Override
57      public YamlTransmissionJobItemProgressSwapper getYamlJobItemProgressSwapper() {
58          return new YamlTransmissionJobItemProgressSwapper();
59      }
60      
61      @Override
62      public Class<MigrationJob> getJobClass() {
63          return MigrationJob.class;
64      }
65      
66      @Override
67      public Optional<String> getToBeStartDisabledNextJobType() {
68          return Optional.of("CONSISTENCY_CHECK");
69      }
70      
71      @Override
72      public Optional<String> getToBeStoppedPreviousJobType() {
73          return Optional.of("CONSISTENCY_CHECK");
74      }
75      
76      @Override
77      public PipelineJobInfo getJobInfo(final String jobId) {
78          PipelineJobMetaData jobMetaData = new PipelineJobMetaData(PipelineJobIdUtils.getElasticJobConfigurationPOJO(jobId));
79          Collection<String> sourceTables = new LinkedList<>();
80          new PipelineJobConfigurationManager(new MigrationJobType()).<MigrationJobConfiguration>getJobConfiguration(jobId).getJobShardingDataNodes()
81                  .forEach(each -> each.getEntries().forEach(entry -> entry.getDataNodes().forEach(dataNode -> sourceTables.add(DataNodeUtils.formatWithSchema(dataNode)))));
82          return new PipelineJobInfo(jobMetaData, null, String.join(",", sourceTables));
83      }
84      
85      @Override
86      public PipelineDataConsistencyChecker buildDataConsistencyChecker(final PipelineJobConfiguration jobConfig, final TransmissionProcessContext processContext,
87                                                                        final ConsistencyCheckJobItemProgressContext progressContext) {
88          return new MigrationDataConsistencyChecker((MigrationJobConfiguration) jobConfig, processContext, progressContext);
89      }
90      
91      @Override
92      public String getType() {
93          return "MIGRATION";
94      }
95  }