|
| 1 | +/* Copyright (c) 2024 Airbyte, Inc., all rights reserved. */ |
| 2 | +package io.airbyte.integrations.source.mysql |
| 3 | + |
| 4 | +import io.airbyte.cdk.ConfigErrorException |
| 5 | +import io.airbyte.cdk.command.JdbcSourceConfiguration |
| 6 | +import io.airbyte.cdk.command.SourceConfiguration |
| 7 | +import io.airbyte.cdk.command.SourceConfigurationFactory |
| 8 | +import io.airbyte.cdk.ssh.SshConnectionOptions |
| 9 | +import io.airbyte.cdk.ssh.SshTunnelMethodConfiguration |
| 10 | +import io.github.oshai.kotlinlogging.KotlinLogging |
| 11 | +import jakarta.inject.Singleton |
| 12 | +import java.net.URLDecoder |
| 13 | +import java.nio.charset.StandardCharsets |
| 14 | +import java.time.Duration |
| 15 | + |
| 16 | +private val log = KotlinLogging.logger {} |
| 17 | + |
| 18 | +/** Mysql-specific implementation of [SourceConfiguration] */ |
| 19 | +data class MysqlSourceConfiguration( |
| 20 | + override val realHost: String, |
| 21 | + override val realPort: Int, |
| 22 | + override val sshTunnel: SshTunnelMethodConfiguration, |
| 23 | + override val sshConnectionOptions: SshConnectionOptions, |
| 24 | + override val jdbcUrlFmt: String, |
| 25 | + override val jdbcProperties: Map<String, String>, |
| 26 | + override val namespaces: Set<String>, |
| 27 | + val cursorConfiguration: CursorConfiguration, |
| 28 | + override val maxConcurrency: Int, |
| 29 | + override val resourceAcquisitionHeartbeat: Duration = Duration.ofMillis(100L), |
| 30 | + override val checkpointTargetInterval: Duration, |
| 31 | + override val checkPrivileges: Boolean, |
| 32 | +) : JdbcSourceConfiguration { |
| 33 | + override val global = cursorConfiguration is CdcCursor |
| 34 | +} |
| 35 | + |
| 36 | +@Singleton |
| 37 | +class MysqlSourceConfigurationFactory : |
| 38 | + SourceConfigurationFactory<MysqlSourceConfigurationJsonObject, MysqlSourceConfiguration> { |
| 39 | + override fun makeWithoutExceptionHandling( |
| 40 | + pojo: MysqlSourceConfigurationJsonObject, |
| 41 | + ): MysqlSourceConfiguration { |
| 42 | + val realHost: String = pojo.host |
| 43 | + val realPort: Int = pojo.port |
| 44 | + val sshTunnel: SshTunnelMethodConfiguration = pojo.getTunnelMethodValue() |
| 45 | + val jdbcProperties = mutableMapOf<String, String>() |
| 46 | + jdbcProperties["user"] = pojo.username |
| 47 | + pojo.password?.let { jdbcProperties["password"] = it } |
| 48 | + |
| 49 | + // Parse URL parameters. |
| 50 | + val pattern = "^([^=]+)=(.*)$".toRegex() |
| 51 | + for (pair in (pojo.jdbcUrlParams ?: "").trim().split("&".toRegex())) { |
| 52 | + if (pair.isBlank()) { |
| 53 | + continue |
| 54 | + } |
| 55 | + val result: MatchResult? = pattern.matchEntire(pair) |
| 56 | + if (result == null) { |
| 57 | + log.warn { "ignoring invalid JDBC URL param '$pair'" } |
| 58 | + } else { |
| 59 | + val key: String = result.groupValues[1].trim() |
| 60 | + val urlEncodedValue: String = result.groupValues[2].trim() |
| 61 | + jdbcProperties[key] = URLDecoder.decode(urlEncodedValue, StandardCharsets.UTF_8) |
| 62 | + } |
| 63 | + } |
| 64 | + // Determine protocol and configure encryption. |
| 65 | + val encryption: Encryption = pojo.getEncryptionValue() |
| 66 | + if (encryption is SslVerifyCertificate) { |
| 67 | + // TODO: reuse JdbcSSLCOnnectionUtils; parse the input into properties |
| 68 | + } |
| 69 | + // Build JDBC URL |
| 70 | + val address = "%s:%d" |
| 71 | + val jdbcUrlFmt = "jdbc:mysql://${address}" |
| 72 | + jdbcProperties["useCursorFetch"] = "true" |
| 73 | + jdbcProperties["sessionVariables"] = "autocommit=0" |
| 74 | + val defaultSchema: String = pojo.username.uppercase() |
| 75 | + val sshOpts = SshConnectionOptions.fromAdditionalProperties(pojo.getAdditionalProperties()) |
| 76 | + val checkpointTargetInterval: Duration = |
| 77 | + Duration.ofSeconds(pojo.checkpointTargetIntervalSeconds?.toLong() ?: 0) |
| 78 | + if (!checkpointTargetInterval.isPositive) { |
| 79 | + throw ConfigErrorException("Checkpoint Target Interval should be positive") |
| 80 | + } |
| 81 | + val maxConcurrency: Int = pojo.concurrency ?: 0 |
| 82 | + if ((pojo.concurrency ?: 0) <= 0) { |
| 83 | + throw ConfigErrorException("Concurrency setting should be positive") |
| 84 | + } |
| 85 | + return MysqlSourceConfiguration( |
| 86 | + realHost = realHost, |
| 87 | + realPort = realPort, |
| 88 | + sshTunnel = sshTunnel, |
| 89 | + sshConnectionOptions = sshOpts, |
| 90 | + jdbcUrlFmt = jdbcUrlFmt, |
| 91 | + jdbcProperties = jdbcProperties, |
| 92 | + namespaces = pojo.schemas?.toSet() ?: setOf(defaultSchema), |
| 93 | + cursorConfiguration = pojo.getCursorConfigurationValue(), |
| 94 | + checkpointTargetInterval = checkpointTargetInterval, |
| 95 | + maxConcurrency = maxConcurrency, |
| 96 | + checkPrivileges = pojo.checkPrivileges ?: true, |
| 97 | + ) |
| 98 | + } |
| 99 | +} |
0 commit comments