|
15 | 15 | */ |
16 | 16 | package com.google.cloud.teleport.v2.templates; |
17 | 17 |
|
| 18 | +import static com.google.cloud.teleport.v2.spanner.migrations.constants.Constants.CASSANDRA_SOURCE_TYPE; |
18 | 19 | import static com.google.cloud.teleport.v2.spanner.migrations.constants.Constants.MYSQL_SOURCE_TYPE; |
19 | 20 | import static com.google.cloud.teleport.v2.spanner.migrations.constants.Constants.POSTGRES_SOURCE_TYPE; |
20 | 21 | import static com.google.cloud.teleport.v2.spanner.migrations.constants.Constants.RUN_MODE_REGULAR; |
|
38 | 39 | import com.google.cloud.teleport.v2.spanner.ddl.Ddl; |
39 | 40 | import com.google.cloud.teleport.v2.spanner.migrations.shard.CassandraShard; |
40 | 41 | import com.google.cloud.teleport.v2.spanner.migrations.shard.Shard; |
| 42 | +import com.google.cloud.teleport.v2.spanner.migrations.source.config.CassandraConnectionConfig; |
| 43 | +import com.google.cloud.teleport.v2.spanner.migrations.source.config.JdbcShardConfig; |
| 44 | +import com.google.cloud.teleport.v2.spanner.migrations.source.config.SourceConfigParser; |
| 45 | +import com.google.cloud.teleport.v2.spanner.migrations.source.config.SourceConnectionConfig; |
41 | 46 | import com.google.cloud.teleport.v2.spanner.migrations.transformation.CustomTransformation; |
42 | 47 | import com.google.cloud.teleport.v2.spanner.migrations.utils.CassandraConfigFileReader; |
43 | 48 | import com.google.cloud.teleport.v2.spanner.migrations.utils.CassandraDriverConfigLoader; |
44 | 49 | import com.google.cloud.teleport.v2.spanner.migrations.utils.DataflowWorkerMachineTypeUtils; |
| 50 | +import com.google.cloud.teleport.v2.spanner.migrations.utils.ISecretManagerAccessor; |
45 | 51 | import com.google.cloud.teleport.v2.spanner.migrations.utils.SecretManagerAccessorImpl; |
46 | | -import com.google.cloud.teleport.v2.spanner.migrations.utils.ShardFileReader; |
47 | 52 | import com.google.cloud.teleport.v2.spanner.sourceddl.CassandraInformationSchemaScanner; |
48 | 53 | import com.google.cloud.teleport.v2.spanner.sourceddl.MySqlInformationSchemaScanner; |
49 | 54 | import com.google.cloud.teleport.v2.spanner.sourceddl.PostgreSQLInformationSchemaScanner; |
|
61 | 66 | import com.google.cloud.teleport.v2.templates.transforms.UpdateDlqMetricsFn; |
62 | 67 | import com.google.cloud.teleport.v2.transforms.DLQWriteTransform; |
63 | 68 | import com.google.cloud.teleport.v2.values.FailsafeElement; |
| 69 | +import com.google.common.base.Preconditions; |
64 | 70 | import com.google.common.base.Strings; |
65 | 71 | import com.zaxxer.hikari.HikariConfig; |
66 | 72 | import com.zaxxer.hikari.HikariDataSource; |
@@ -647,20 +653,14 @@ public static PipelineResult run(Options options) { |
647 | 653 | .get(SpannerInformationSchemaProcessorTransform.SHADOW_TABLE_DDL_TAG) |
648 | 654 | .apply("View Shadow DDL", View.asSingleton()); |
649 | 655 |
|
650 | | - List<Shard> shards; |
651 | | - String shardingMode; |
652 | | - if (MYSQL_SOURCE_TYPE.equals(options.getSourceType()) |
653 | | - || POSTGRES_SOURCE_TYPE.equals(options.getSourceType())) { |
654 | | - ShardFileReader shardFileReader = new ShardFileReader(new SecretManagerAccessorImpl()); |
655 | | - shards = shardFileReader.getOrderedShardDetails(options.getSourceShardsFilePath()); |
656 | | - shardingMode = Constants.SHARDING_MODE_MULTI_SHARD; |
| 656 | + List<Shard> shards = getShardList(options.getSourceType(), options.getSourceShardsFilePath()); |
657 | 657 |
|
658 | | - } else { |
659 | | - CassandraConfigFileReader cassandraConfigFileReader = new CassandraConfigFileReader(); |
660 | | - shards = cassandraConfigFileReader.getCassandraShard(options.getSourceShardsFilePath()); |
661 | | - LOG.info("Cassandra config is: {}", shards.get(0)); |
662 | | - shardingMode = Constants.SHARDING_MODE_SINGLE_SHARD; |
663 | | - } |
| 658 | + // cassandra is always a single sharded migration. |
| 659 | + // for JDBC, shards size and IsShardedMigration option is used below. |
| 660 | + String shardingMode = |
| 661 | + options.getSourceType().equals(CASSANDRA_SOURCE_TYPE) |
| 662 | + ? Constants.SHARDING_MODE_SINGLE_SHARD |
| 663 | + : Constants.SHARDING_MODE_MULTI_SHARD; |
664 | 664 |
|
665 | 665 | if (MYSQL_SOURCE_TYPE.equals(options.getSourceType())) { |
666 | 666 | validateMySQLNotReadOnly(shards); |
@@ -951,6 +951,53 @@ static void buildPipeline( |
951 | 951 | .build()); |
952 | 952 | } |
953 | 953 |
|
| 954 | + /** |
| 955 | + * Returns a list of shards based on the source type and source shards file path. This should be |
| 956 | + * removed in Phase 2 of Standardizing config. |
| 957 | + * |
| 958 | + * @param sourceType The type of the source database. |
| 959 | + * @param sourceShardsFilePath The GCS path to the source shards configuration file. |
| 960 | + * @return A list of shards. |
| 961 | + */ |
| 962 | + public static List<Shard> getShardList(String sourceType, String sourceShardsFilePath) { |
| 963 | + ISecretManagerAccessor secretManagerAccessor = new SecretManagerAccessorImpl(); |
| 964 | + SourceConfigParser sourceConfigParser = new SourceConfigParser(secretManagerAccessor); |
| 965 | + SourceConnectionConfig sourceConnectionConfig; |
| 966 | + try { |
| 967 | + // Parse the source shards configuration file to respective |
| 968 | + // SourceConnectionConfig. |
| 969 | + sourceConnectionConfig = |
| 970 | + sourceConfigParser.parseConfiguration(sourceType, sourceShardsFilePath); |
| 971 | + } catch (Exception e) { |
| 972 | + LOG.error("Error parsing source config", e); |
| 973 | + throw new RuntimeException("Error parsing source config", e); |
| 974 | + } |
| 975 | + List<Shard> shards; |
| 976 | + if (sourceConnectionConfig instanceof JdbcShardConfig) { |
| 977 | + shards = ((JdbcShardConfig) sourceConnectionConfig).getShardConfigs(); |
| 978 | + LOG.info("JDBC shard config is parsed."); |
| 979 | + } else if (sourceConnectionConfig instanceof CassandraConnectionConfig) { |
| 980 | + CassandraConfigFileReader cassandraConfigFileReader = new CassandraConfigFileReader(); |
| 981 | + shards = |
| 982 | + cassandraConfigFileReader.getCassandraShard( |
| 983 | + ((CassandraConnectionConfig) sourceConnectionConfig).getOptionsMap()); |
| 984 | + LOG.info("Cassandra shard config is parsed."); |
| 985 | + } else { |
| 986 | + String errorMessage = |
| 987 | + "Invalid source config for source type: " |
| 988 | + + sourceType |
| 989 | + + ". Source config parsed to: " |
| 990 | + + sourceConnectionConfig.getClass() |
| 991 | + + ". Source config file path: " |
| 992 | + + sourceShardsFilePath; |
| 993 | + LOG.error(errorMessage); |
| 994 | + throw new RuntimeException(errorMessage); |
| 995 | + } |
| 996 | + Preconditions.checkArgument( |
| 997 | + shards != null && !shards.isEmpty(), "Shard list should have at least 1 element."); |
| 998 | + return shards; |
| 999 | + } |
| 1000 | + |
954 | 1001 | public static SpannerIO.ReadChangeStream getReadChangeStreamDoFn( |
955 | 1002 | Options options, SpannerConfig spannerConfig) { |
956 | 1003 |
|
|
0 commit comments