|
| 1 | +/* |
| 2 | + * Copyright (c) 2021 Airbyte, Inc., all rights reserved. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.airbyte.integrations.source.mssql; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.databind.JsonNode; |
| 8 | +import io.debezium.annotation.VisibleForTesting; |
| 9 | +import java.util.Properties; |
| 10 | +import org.slf4j.Logger; |
| 11 | +import org.slf4j.LoggerFactory; |
| 12 | + |
| 13 | +public class MssqlCdcHelper { |
| 14 | + |
| 15 | + // legacy replication method config before version 0.4.0 |
| 16 | + // it is an enum with possible values: STANDARD and CDC |
| 17 | + private static final String LEGACY_REPLICATION_FIELD = "replication_method"; |
| 18 | + // new replication method config since version 0.4.0 |
| 19 | + // it is an oneOf object |
| 20 | + private static final String REPLICATION_FIELD = "replication"; |
| 21 | + private static final String REPLICATION_TYPE_FIELD = "replication_type"; |
| 22 | + private static final String CDC_SNAPSHOT_ISOLATION_FIELD = "snapshot_isolation"; |
| 23 | + private static final String CDC_DATA_TO_SYNC_FIELD = "data_to_sync"; |
| 24 | + |
| 25 | + public enum ReplicationMethod { |
| 26 | + STANDARD, |
| 27 | + CDC |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * The default "SNAPSHOT" mode can prevent other (non-Airbyte) transactions from updating table rows |
| 32 | + * while we snapshot. References: |
| 33 | + * https://docs.microsoft.com/en-us/sql/t-sql/statements/set-transaction-isolation-level-transact-sql?view=sql-server-ver15 |
| 34 | + * https://debezium.io/documentation/reference/1.4/connectors/sqlserver.html#sqlserver-property-snapshot-isolation-mode |
| 35 | + */ |
| 36 | + public enum SnapshotIsolation { |
| 37 | + |
| 38 | + SNAPSHOT("Snapshot", "snapshot"), |
| 39 | + READ_COMMITTED("Read Committed", "read_committed"); |
| 40 | + |
| 41 | + private final String snapshotIsolationLevel; |
| 42 | + private final String debeziumIsolationMode; |
| 43 | + |
| 44 | + SnapshotIsolation(final String snapshotIsolationLevel, final String debeziumIsolationMode) { |
| 45 | + this.snapshotIsolationLevel = snapshotIsolationLevel; |
| 46 | + this.debeziumIsolationMode = debeziumIsolationMode; |
| 47 | + } |
| 48 | + |
| 49 | + public String getDebeziumIsolationMode() { |
| 50 | + return debeziumIsolationMode; |
| 51 | + } |
| 52 | + |
| 53 | + public static SnapshotIsolation from(final String jsonValue) { |
| 54 | + for (final SnapshotIsolation value : values()) { |
| 55 | + if (value.snapshotIsolationLevel.equalsIgnoreCase(jsonValue)) { |
| 56 | + return value; |
| 57 | + } |
| 58 | + } |
| 59 | + throw new IllegalArgumentException("Unexpected snapshot isolation level: " + jsonValue); |
| 60 | + } |
| 61 | + |
| 62 | + } |
| 63 | + |
| 64 | + // https://debezium.io/documentation/reference/1.4/connectors/sqlserver.html#sqlserver-property-snapshot-mode |
| 65 | + public enum DataToSync { |
| 66 | + |
| 67 | + EXISTING_AND_NEW("Existing and New", "initial"), |
| 68 | + NEW_CHANGES_ONLY("New Changes Only", "schema_only"); |
| 69 | + |
| 70 | + private final String dataToSyncConfig; |
| 71 | + private final String debeziumSnapshotMode; |
| 72 | + |
| 73 | + DataToSync(final String value, final String debeziumSnapshotMode) { |
| 74 | + this.dataToSyncConfig = value; |
| 75 | + this.debeziumSnapshotMode = debeziumSnapshotMode; |
| 76 | + } |
| 77 | + |
| 78 | + public String getDebeziumSnapshotMode() { |
| 79 | + return debeziumSnapshotMode; |
| 80 | + } |
| 81 | + |
| 82 | + public static DataToSync from(final String value) { |
| 83 | + for (final DataToSync s : values()) { |
| 84 | + if (s.dataToSyncConfig.equalsIgnoreCase(value)) { |
| 85 | + return s; |
| 86 | + } |
| 87 | + } |
| 88 | + throw new IllegalArgumentException("Unexpected data to sync setting: " + value); |
| 89 | + } |
| 90 | + |
| 91 | + } |
| 92 | + |
| 93 | + private static final Logger LOGGER = LoggerFactory.getLogger(MssqlCdcHelper.class); |
| 94 | + |
| 95 | + @VisibleForTesting |
| 96 | + static boolean isCdc(final JsonNode config) { |
| 97 | + // legacy replication method config before version 0.4.0 |
| 98 | + if (config.hasNonNull(LEGACY_REPLICATION_FIELD)) { |
| 99 | + return ReplicationMethod.valueOf(config.get(LEGACY_REPLICATION_FIELD).asText()) == ReplicationMethod.CDC; |
| 100 | + } |
| 101 | + // new replication method config since version 0.4.0 |
| 102 | + if (config.hasNonNull(REPLICATION_FIELD)) { |
| 103 | + final JsonNode replicationConfig = config.get(REPLICATION_FIELD); |
| 104 | + return ReplicationMethod.valueOf(replicationConfig.get(REPLICATION_TYPE_FIELD).asText()) == ReplicationMethod.CDC; |
| 105 | + } |
| 106 | + return false; |
| 107 | + } |
| 108 | + |
| 109 | + @VisibleForTesting |
| 110 | + static SnapshotIsolation getSnapshotIsolationConfig(final JsonNode config) { |
| 111 | + // legacy replication method config before version 0.4.0 |
| 112 | + if (config.hasNonNull(LEGACY_REPLICATION_FIELD)) { |
| 113 | + return SnapshotIsolation.SNAPSHOT; |
| 114 | + } |
| 115 | + // new replication method config since version 0.4.0 |
| 116 | + if (config.hasNonNull(REPLICATION_FIELD)) { |
| 117 | + final JsonNode replicationConfig = config.get(REPLICATION_FIELD); |
| 118 | + final JsonNode snapshotIsolation = replicationConfig.get(CDC_SNAPSHOT_ISOLATION_FIELD); |
| 119 | + return SnapshotIsolation.from(snapshotIsolation.asText()); |
| 120 | + } |
| 121 | + return SnapshotIsolation.SNAPSHOT; |
| 122 | + } |
| 123 | + |
| 124 | + @VisibleForTesting |
| 125 | + static DataToSync getDataToSyncConfig(final JsonNode config) { |
| 126 | + // legacy replication method config before version 0.4.0 |
| 127 | + if (config.hasNonNull(LEGACY_REPLICATION_FIELD)) { |
| 128 | + return DataToSync.EXISTING_AND_NEW; |
| 129 | + } |
| 130 | + // new replication method config since version 0.4.0 |
| 131 | + if (config.hasNonNull(REPLICATION_FIELD)) { |
| 132 | + final JsonNode replicationConfig = config.get(REPLICATION_FIELD); |
| 133 | + final JsonNode dataToSync = replicationConfig.get(CDC_DATA_TO_SYNC_FIELD); |
| 134 | + return DataToSync.from(dataToSync.asText()); |
| 135 | + } |
| 136 | + return DataToSync.EXISTING_AND_NEW; |
| 137 | + } |
| 138 | + |
| 139 | + @VisibleForTesting |
| 140 | + static Properties getDebeziumProperties(final JsonNode config) { |
| 141 | + final Properties props = new Properties(); |
| 142 | + props.setProperty("connector.class", "io.debezium.connector.sqlserver.SqlServerConnector"); |
| 143 | + |
| 144 | + // https://debezium.io/documentation/reference/1.4/connectors/sqlserver.html#sqlserver-property-include-schema-changes |
| 145 | + props.setProperty("include.schema.changes", "false"); |
| 146 | + // https://debezium.io/documentation/reference/1.4/connectors/sqlserver.html#sqlserver-property-provide-transaction-metadata |
| 147 | + props.setProperty("provide.transaction.metadata", "false"); |
| 148 | + |
| 149 | + props.setProperty("converters", "mssql_converter"); |
| 150 | + props.setProperty("mssql_converter.type", "io.airbyte.integrations.debezium.internals.MSSQLConverter"); |
| 151 | + |
| 152 | + props.setProperty("snapshot.mode", getDataToSyncConfig(config).getDebeziumSnapshotMode()); |
| 153 | + props.setProperty("snapshot.isolation.mode", getSnapshotIsolationConfig(config).getDebeziumIsolationMode()); |
| 154 | + |
| 155 | + return props; |
| 156 | + } |
| 157 | + |
| 158 | +} |
0 commit comments