Skip to content

cdk-java move the generationId handling to its own class #43329

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/*
* Copyright (c) 2024 Airbyte, Inc., all rights reserved.
*/

package io.airbyte.cdk.integrations.destination.jdbc

import io.airbyte.cdk.db.jdbc.JdbcDatabase

interface JdbcGenerationHandler {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this method should live in DestinationHandler, since we'll need it for all destinations (i.e. not just JDBC)

/**
* get the value of _airbyte_generation_id for any row in table {@code rawNamespace.rawName}
*
* @returns true if the table exists and contains such a row, false otherwise
*/
fun getGenerationIdInTable(database: JdbcDatabase, namespace: String, name: String): Long?
}
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,6 @@ interface SqlOperations {
/** Check if the data record is valid and ok to be written to destination */
fun isValidData(data: JsonNode?): Boolean

/**
* get the value of _airbyte_generation_id for any row in table {@code rawNamespace.rawName}
*
* @returns true if the table exists and contains such a row, false otherwise
*/
fun getGenerationIdInTable(database: JdbcDatabase, namespace: String, name: String): Long?

/** overwrite the raw table with the temporary raw table */
fun overwriteRawTable(database: JdbcDatabase, rawNamespace: String, rawName: String)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,8 @@ abstract class AbstractJdbcDestination<DestinationState : MinimumDestinationStat

protected abstract fun getSqlOperations(config: JsonNode): SqlOperations

protected abstract fun getGenerationHandler(): JdbcGenerationHandler

protected abstract fun getDestinationHandler(
config: JsonNode,
databaseName: String,
Expand Down Expand Up @@ -289,6 +291,7 @@ abstract class AbstractJdbcDestination<DestinationState : MinimumDestinationStat
outputRecordCollector,
database,
getSqlOperations(config),
getGenerationHandler(),
namingResolver,
config,
catalog,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ object JdbcBufferedConsumerFactory {
outputRecordCollector: Consumer<AirbyteMessage>,
database: JdbcDatabase,
sqlOperations: SqlOperations,
generationIdHandler: JdbcGenerationHandler,
namingResolver: NamingConventionTransformer,
config: JsonNode,
catalog: ConfiguredAirbyteCatalog,
Expand All @@ -77,12 +78,19 @@ object JdbcBufferedConsumerFactory {
onStartFunction(
database,
sqlOperations,
generationIdHandler,
writeConfigs,
typerDeduper,
namingResolver,
parsedCatalog
),
onCloseFunction(database, sqlOperations, parsedCatalog, typerDeduper),
onCloseFunction(
database,
sqlOperations,
generationIdHandler,
parsedCatalog,
typerDeduper
),
JdbcInsertFlushFunction(
defaultNamespace,
recordWriterFunction(database, sqlOperations, writeConfigs, catalog),
Expand All @@ -98,15 +106,15 @@ object JdbcBufferedConsumerFactory {

private fun createWriteConfigs(
database: JdbcDatabase,
sqlOperations: SqlOperations,
generationIdHandler: JdbcGenerationHandler,
namingResolver: NamingConventionTransformer,
parsedCatalog: ParsedCatalog,
): List<WriteConfig> {
return parsedCatalog.streams.map {
val rawSuffix: String =
if (
it.minimumGenerationId == 0L ||
sqlOperations.getGenerationIdInTable(
generationIdHandler.getGenerationIdInTable(
database,
it.id.rawNamespace,
it.id.rawName
Expand Down Expand Up @@ -162,6 +170,7 @@ object JdbcBufferedConsumerFactory {
private fun onStartFunction(
database: JdbcDatabase,
sqlOperations: SqlOperations,
generationIdHandler: JdbcGenerationHandler,
writeConfigs: MutableList<WriteConfig>,
typerDeduper: TyperDeduper,
namingResolver: NamingConventionTransformer,
Expand All @@ -170,7 +179,7 @@ object JdbcBufferedConsumerFactory {
return OnStartFunction {
typerDeduper.prepareSchemasAndRunMigrations()
writeConfigs.addAll(
createWriteConfigs(database, sqlOperations, namingResolver, parsedCatalog)
createWriteConfigs(database, generationIdHandler, namingResolver, parsedCatalog)
)
LOGGER.info {
"Preparing raw tables in destination started for ${writeConfigs.size} streams"
Expand All @@ -194,7 +203,7 @@ object JdbcBufferedConsumerFactory {
0L -> {}
writeConfig.generationId ->
if (
sqlOperations.getGenerationIdInTable(
generationIdHandler.getGenerationIdInTable(
database,
schemaName,
dstTableName + writeConfig.rawTableSuffix
Expand Down Expand Up @@ -268,6 +277,7 @@ object JdbcBufferedConsumerFactory {
private fun onCloseFunction(
database: JdbcDatabase,
sqlOperations: SqlOperations,
generationIdHandler: JdbcGenerationHandler,
catalog: ParsedCatalog,
typerDeduper: TyperDeduper
): OnCloseFunction {
Expand All @@ -278,7 +288,7 @@ object JdbcBufferedConsumerFactory {
catalog.streams.forEach {
if (
it.minimumGenerationId != 0L &&
sqlOperations.getGenerationIdInTable(
generationIdHandler.getGenerationIdInTable(
database,
it.id.rawNamespace,
it.id.rawName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import io.airbyte.cdk.db.jdbc.JdbcDatabase
import io.airbyte.cdk.integrations.base.JavaBaseConstants
import io.airbyte.cdk.integrations.base.JavaBaseConstants.DestinationColumns
import io.airbyte.cdk.integrations.destination.jdbc.ColumnDefinition
import io.airbyte.cdk.integrations.destination.jdbc.SqlOperations
import io.airbyte.cdk.integrations.destination.jdbc.JdbcGenerationHandler
import io.airbyte.cdk.integrations.destination.jdbc.TableDefinition
import io.airbyte.cdk.integrations.util.ConnectorExceptionUtil.getResultsOrLogAndThrowFirst
import io.airbyte.commons.concurrency.CompletableFutures
Expand Down Expand Up @@ -55,7 +55,7 @@ abstract class JdbcDestinationHandler<DestinationState>(
protected val rawTableNamespace: String,
private val dialect: SQLDialect,
private val columns: DestinationColumns = DestinationColumns.V2_WITH_GENERATION,
private val sqlOperations: SqlOperations,
protected val generationHandler: JdbcGenerationHandler,
) : DestinationHandler<DestinationState> {
protected val dslContext: DSLContext
get() = DSL.using(dialect)
Expand Down Expand Up @@ -343,13 +343,13 @@ abstract class JdbcDestinationHandler<DestinationState>(
isFinalTableEmpty,
destinationState,
finalTableGenerationId =
sqlOperations.getGenerationIdInTable(
generationHandler.getGenerationIdInTable(
jdbcDatabase,
streamConfig.id.rawNamespace,
streamConfig.id.rawName
),
finalTempTableGenerationId =
sqlOperations.getGenerationIdInTable(
generationHandler.getGenerationIdInTable(
jdbcDatabase,
streamConfig.id.rawNamespace,
streamConfig.id.rawName + AbstractStreamOperation.TMP_TABLE_SUFFIX
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ package io.airbyte.cdk.integrations.destination.jdbc.typing_deduping

import com.fasterxml.jackson.databind.JsonNode
import io.airbyte.cdk.db.jdbc.JdbcDatabase
import io.airbyte.cdk.integrations.destination.jdbc.SqlOperations
import io.airbyte.cdk.integrations.destination.jdbc.JdbcGenerationHandler
import io.airbyte.integrations.base.destination.typing_deduping.AirbyteType
import io.airbyte.integrations.base.destination.typing_deduping.DestinationInitialStatus
import io.airbyte.integrations.base.destination.typing_deduping.Sql
Expand All @@ -19,14 +19,14 @@ class NoOpJdbcDestinationHandler<DestinationState>(
jdbcDatabase: JdbcDatabase,
rawTableSchemaName: String,
sqlDialect: SQLDialect,
sqlOperations: SqlOperations,
generationHandler: JdbcGenerationHandler,
) :
JdbcDestinationHandler<DestinationState>(
databaseName,
jdbcDatabase,
rawTableSchemaName,
sqlDialect,
sqlOperations = sqlOperations
generationHandler = generationHandler,
) {

override fun execute(sql: Sql) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ class AbstractJdbcDestinationTest {

override fun getSqlOperations(config: JsonNode): SqlOperations = TestJdbcSqlOperations()

override fun getGenerationHandler(): JdbcGenerationHandler = mock()

override fun getDestinationHandler(
config: JsonNode,
databaseName: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,6 @@ class TestJdbcSqlOperations : JdbcSqlOperations() {
// Not required for the testing
}

override fun getGenerationIdInTable(
database: JdbcDatabase,
rawNamespace: String,
rawName: String
): Long {
return -1L
// Not required for the testing
}

override fun overwriteRawTable(database: JdbcDatabase, rawNamespace: String, rawName: String) {
// Not required for the testing
}
Expand Down
Loading