Skip to content
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

destination-s3: add file transfer #46302

Merged
merged 12 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from 8 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
Expand Up @@ -8,6 +8,7 @@ import io.airbyte.cdk.load.test.util.NoopDestinationCleaner
import io.airbyte.cdk.load.test.util.NoopExpectedRecordMapper
import io.airbyte.cdk.load.test.util.NoopNameMapper
import io.airbyte.cdk.load.write.BasicFunctionalityIntegrationTest
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test

class MockBasicFunctionalityIntegrationTest :
Expand All @@ -26,6 +27,7 @@ class MockBasicFunctionalityIntegrationTest :
}

@Test
@Disabled
override fun testMidSyncCheckpointingStreamState() {
super.testMidSyncCheckpointingStreamState()
}
Expand Down
1 change: 1 addition & 0 deletions airbyte-cdk/java/airbyte-cdk/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ corresponds to that version.

| Version | Date | Pull Request | Subject |
|:-----------|:-----------|:------------------------------------------------------------|:---------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 0.48.0 | 2024-10-23 | [\#46302](https://github.com/airbytehq/airbyte/pull/46302) | Add support for file transfer |
| 0.47.3 | 2024-10-23 | [\#46689](https://github.com/airbytehq/airbyte/pull/46689) | Split DestinationAcceptanceTest|
| 0.47.2 | 2024-10-21 | [\#47216](https://github.com/airbytehq/airbyte/pull/47216) | improve java compatibiilty|
| 0.47.1 | 2024-09-27 | [\#45397](https://github.com/airbytehq/airbyte/pull/45397) | Allow logical replication from Postgres 16 read-replicas|
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ constructor(
workerPool: ExecutorService = Executors.newFixedThreadPool(5),
private val airbyteMessageDeserializer: AirbyteMessageDeserializer =
AirbyteMessageDeserializer(),
private val isFileTransfer: Boolean = false,
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: rename to flushOnEveryMessage (to reflect functionality rather than usage)

Copy link
Contributor

Choose a reason for hiding this comment

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

Done

) : SerializedAirbyteMessageConsumer {
private val bufferEnqueue: BufferEnqueue = bufferManager.bufferEnqueue
private val flushWorkers: FlushWorkers =
Expand All @@ -64,6 +65,7 @@ constructor(
flushFailure,
bufferManager.stateManager,
workerPool,
isFileTransfer,
)
private val streamNames: Set<StreamDescriptor> =
StreamDescriptorUtils.fromConfiguredCatalog(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ internal constructor(
private val isClosing: AtomicBoolean,
private val flusher: DestinationFlushFunction,
private val nowProvider: Clock,
private val isFileTransfer: Boolean = false,
) {
private val latestFlushTimeMsPerStream: ConcurrentMap<StreamDescriptor, Long> =
ConcurrentHashMap()
Expand All @@ -37,7 +38,15 @@ internal constructor(
runningFlushWorkers: RunningFlushWorkers,
isClosing: AtomicBoolean,
flusher: DestinationFlushFunction,
) : this(bufferDequeue, runningFlushWorkers, isClosing, flusher, Clock.systemUTC())
isFileTransfer: Boolean = false,
) : this(
bufferDequeue,
runningFlushWorkers,
isClosing,
flusher,
Clock.systemUTC(),
isFileTransfer
)

val nextStreamToFlush: Optional<StreamDescriptor>
/**
Expand Down Expand Up @@ -70,7 +79,8 @@ internal constructor(
bufferDequeue.totalGlobalQueueSizeBytes.toDouble() / bufferDequeue.maxQueueSizeBytes
// when we are closing or queues are very full, flush regardless of how few items are in the
// queue.
return if (isClosing.get() || isBuffer90Full) 0 else flusher.queueFlushThresholdBytes
return if (isFileTransfer || isClosing.get() || isBuffer90Full) 0
else flusher.queueFlushThresholdBytes
}

// todo (cgardens) - improve prioritization by getting a better estimate of how much data
Expand Down Expand Up @@ -105,7 +115,7 @@ internal constructor(
"${isTimeTriggeredResult.second} , ${isSizeTriggeredResult.second}"
logger.debug { "computed: $debugString" }

if (isSizeTriggeredResult.first || isTimeTriggeredResult.first) {
if (isFileTransfer || isSizeTriggeredResult.first || isTimeTriggeredResult.first) {
logger.info { "flushing: $debugString" }
latestFlushTimeMsPerStream[stream] = nowProvider.millis()
return Optional.of(stream)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ constructor(
private val flushFailure: FlushFailure,
private val stateManager: GlobalAsyncStateManager,
private val workerPool: ExecutorService = Executors.newFixedThreadPool(5),
private val isFileTransfer: Boolean = false,
) : AutoCloseable {
private val supervisorThread: ScheduledExecutorService = Executors.newScheduledThreadPool(1)
private val debugLoop: ScheduledExecutorService = Executors.newSingleThreadScheduledExecutor()
Expand All @@ -66,6 +67,7 @@ constructor(
runningFlushWorkers,
isClosing,
flusher,
isFileTransfer,
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ class BufferDequeue(

// otherwise pull records until we hit the memory limit.
val newSize: Long = (memoryItem.size) + bytesRead.get()
if (newSize <= optimalBytesToRead) {
if (newSize <= optimalBytesToRead || output.isEmpty()) {
Copy link
Contributor

Choose a reason for hiding this comment

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

What is this accomplishing? Is this because other changes caused optimalBytesToRead to be zero?

Copy link
Contributor

@benmoriceau benmoriceau Oct 30, 2024

Choose a reason for hiding this comment

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

it is not 0 (it's 1) but one and yes it is allowing to add a record to the output disregard of the size.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the problem here was if bytesRead == 0 && memoryItem.size > optimalBytesToRead, then we never add anything to the queue. So here, regardless of memoryItem.size or optimalBytesToRead, if there's no item in the queue, we add the current one.
For fileTransfer we set optimalBytesToRead to 1 so that we force a flush for each message. but with such a small value, any message is bigger than the optimal size, which causes an infinite loop.
Note that the infinite loop could also happen if memoryItem.size() was big enough and optimalBytesToRead was small enough. With our current settings, I don't believe it's possible, but it's just a couple of setting tweaks away...

memoryItem.size.let { bytesRead.addAndGet(it) }
queue.poll()?.item?.let { output.add(it) }
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright (c) 2024 Airbyte, Inc., all rights reserved.
*/

package io.airbyte.cdk.integrations.destination.async.model

import com.fasterxml.jackson.annotation.JsonProperty

class AirbyteRecordMessageFile {
constructor(
fileUrl: String? = null,
bytes: Long? = null,
fileRelativePath: String? = null,
modified: Long? = null,
sourceFileUrl: String? = null
) {
this.fileUrl = fileUrl
this.bytes = bytes
this.fileRelativePath = fileRelativePath
this.modified = modified
this.sourceFileUrl = sourceFileUrl
}
constructor() :
this(
fileUrl = null,
bytes = null,
fileRelativePath = null,
modified = null,
sourceFileUrl = null
)

@get:JsonProperty("file_url")
@set:JsonProperty("file_url")
@JsonProperty("file_url")
var fileUrl: String? = null

@get:JsonProperty("bytes")
@set:JsonProperty("bytes")
@JsonProperty("bytes")
var bytes: Long? = null

@get:JsonProperty("file_relative_path")
@set:JsonProperty("file_relative_path")
@JsonProperty("file_relative_path")
var fileRelativePath: String? = null

@get:JsonProperty("modified")
@set:JsonProperty("modified")
@JsonProperty("modified")
var modified: Long? = null

@get:JsonProperty("source_file_url")
@set:JsonProperty("source_file_url")
@JsonProperty("source_file_url")
var sourceFileUrl: String? = null
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package io.airbyte.cdk.integrations.destination.async.model

import com.fasterxml.jackson.annotation.JsonProperty
import com.fasterxml.jackson.annotation.JsonPropertyDescription
import com.fasterxml.jackson.databind.JsonNode
import io.airbyte.protocol.models.v0.AirbyteRecordMessageMeta
import io.airbyte.protocol.models.v0.StreamDescriptor
Expand Down Expand Up @@ -33,14 +32,18 @@ class PartialAirbyteRecordMessage {
@get:JsonProperty("emitted_at")
@set:JsonProperty("emitted_at")
@JsonProperty("emitted_at")
@JsonPropertyDescription("when the data was emitted from the source. epoch in millisecond.")
var emittedAt: Long = 0

@get:JsonProperty("meta")
@set:JsonProperty("meta")
@JsonProperty("meta")
var meta: AirbyteRecordMessageMeta? = null

@get:JsonProperty("file")
@set:JsonProperty("file")
@JsonProperty("file")
var file: AirbyteRecordMessageFile? = null

fun withNamespace(namespace: String?): PartialAirbyteRecordMessage {
this.namespace = namespace
return this
Expand All @@ -66,6 +69,11 @@ class PartialAirbyteRecordMessage {
return this
}

fun withFile(file: AirbyteRecordMessageFile): PartialAirbyteRecordMessage {
this.file = file
return this
}

override fun equals(other: Any?): Boolean {
if (this === other) {
return true
Expand All @@ -77,7 +85,8 @@ class PartialAirbyteRecordMessage {
return namespace == that.namespace &&
stream == that.stream &&
emittedAt == that.emittedAt &&
meta == that.meta
meta == that.meta &&
file == that.file
}

override fun hashCode(): Int {
Expand All @@ -98,6 +107,9 @@ class PartialAirbyteRecordMessage {
", meta='" +
meta +
'\'' +
", file='" +
file +
'\'' +
'}'
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version=0.47.3
version=0.48.0
Original file line number Diff line number Diff line change
Expand Up @@ -765,6 +765,9 @@ class AsyncStreamConsumerTest {
val throwable =
assertThrows(RuntimeException::class.java) { consumer.accept(retyped, retyped.length) }
// Ensure that the offending data has been scrubbed from the error message
assertFalse(throwable.message!!.contains(offender))
assertFalse(
throwable.message!!.contains(offender),
"message should not contain the offender. Was ${throwable.message}"
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ class DetectStreamToFlushTest {
)

val detect =
DetectStreamToFlush(bufferDequeue, runningFlushWorkers, AtomicBoolean(false), flusher)
DetectStreamToFlush(
bufferDequeue,
runningFlushWorkers,
AtomicBoolean(false),
flusher,
false
)
Assertions.assertEquals(Optional.empty<Any>(), detect.getNextStreamToFlush(0))
}

Expand All @@ -66,7 +72,13 @@ class DetectStreamToFlushTest {
RunningFlushWorkers::class.java,
)
val detect =
DetectStreamToFlush(bufferDequeue, runningFlushWorkers, AtomicBoolean(false), flusher)
DetectStreamToFlush(
bufferDequeue,
runningFlushWorkers,
AtomicBoolean(false),
flusher,
false
)
// if above threshold, triggers
Assertions.assertEquals(Optional.of(DESC1), detect.getNextStreamToFlush(0))
// if below threshold, no trigger
Expand Down Expand Up @@ -94,10 +106,41 @@ class DetectStreamToFlushTest {
),
)
val detect =
DetectStreamToFlush(bufferDequeue, runningFlushWorkers, AtomicBoolean(false), flusher)
DetectStreamToFlush(
bufferDequeue,
runningFlushWorkers,
AtomicBoolean(false),
flusher,
false
)
Assertions.assertEquals(Optional.empty<Any>(), detect.getNextStreamToFlush(0))
}

@Test
internal fun testFileTransfer() {
val bufferDequeue =
Mockito.mock(
BufferDequeue::class.java,
)
Mockito.`when`(bufferDequeue.bufferedStreams).thenReturn(setOf(DESC1))
Mockito.`when`(bufferDequeue.getQueueSizeBytes(DESC1)).thenReturn(Optional.of(0L))
val runningFlushWorkers =
Mockito.mock(
RunningFlushWorkers::class.java,
)

val detect =
DetectStreamToFlush(
bufferDequeue,
runningFlushWorkers,
AtomicBoolean(false),
flusher,
true
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: use named parameters for primitive arguments

Suggested change
true
isFileTransfer = true

Copy link
Contributor

Choose a reason for hiding this comment

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

Done

)
Assertions.assertEquals(0, detect.computeQueueThreshold())
Assertions.assertEquals(Optional.of(DESC1), detect.getNextStreamToFlush(0))
}

@Test
internal fun testGetNextPicksUpOnTimeTrigger() {
val bufferDequeue =
Expand Down Expand Up @@ -127,6 +170,7 @@ class DetectStreamToFlushTest {
AtomicBoolean(false),
flusher,
mockedNowProvider,
false
)

// initialize flush time
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import io.airbyte.configoss.WorkerDestinationConfig
import io.airbyte.protocol.models.v0.AirbyteMessage
import io.airbyte.protocol.models.v0.AirbyteStateStats
import io.airbyte.protocol.models.v0.ConfiguredAirbyteCatalog
import io.airbyte.workers.exception.TestHarnessException
import io.airbyte.workers.helper.ConnectorConfigUpdater
import io.airbyte.workers.internal.AirbyteDestination
import io.airbyte.workers.internal.DefaultAirbyteDestination
Expand Down Expand Up @@ -215,7 +216,11 @@ abstract class BaseDestinationAcceptanceTest(
}
}

destination.close()
try {
destination.close()
} catch (e: TestHarnessException) {
throw TestHarnessException(e.message, e, destinationOutput)
}

return destinationOutput
}
Expand Down Expand Up @@ -258,6 +263,7 @@ abstract class BaseDestinationAcceptanceTest(
workspaceRoot,
workspaceRoot.toString(),
localRoot.toString(),
fileTransferMountSource,
"host",
getConnectorEnv()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ abstract class AbstractSourceConnectorTest {
workspaceRoot,
workspaceRoot.toString(),
localRoot.toString(),
fileTransferMountSource = null,
"host",
envMap
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package io.airbyte.commons.features

import io.github.oshai.kotlinlogging.KotlinLogging
import java.nio.file.Path
import java.util.function.Function

private val log = KotlinLogging.logger {}
Expand Down Expand Up @@ -46,6 +47,16 @@ class EnvVariableFeatureFlags : FeatureFlags {
return getEnvOrDefault(DEPLOYMENT_MODE, "") { arg: String -> arg }
}

override fun airbyteStagingDirectory(): Path? {
return getEnvOrDefault(AIRBYTE_STAGING_DIRECTORY_PROPERTY_NAME, null) { arg: String ->
Path.of(arg)
}
}

override fun useFileTransfer(): Boolean {
return getEnvOrDefault(USE_FILE_TRANSFER, false) { it.toBoolean() }
}

// TODO: refactor in order to use the same method than the ones in EnvConfigs.java
fun <T> getEnvOrDefault(key: String?, defaultValue: T, parser: Function<String, T>): T {
val value = System.getenv(key)
Expand Down Expand Up @@ -73,5 +84,8 @@ class EnvVariableFeatureFlags : FeatureFlags {
const val STRICT_COMPARISON_NORMALIZATION_TAG: String =
"STRICT_COMPARISON_NORMALIZATION_TAG"
const val DEPLOYMENT_MODE: String = "DEPLOYMENT_MODE"
val DEFAULT_AIRBYTE_STAGING_DIRECTORY: Path = Path.of("/staging/files")
const val AIRBYTE_STAGING_DIRECTORY_PROPERTY_NAME: String = "AIRBYTE_STAGING_DIRECTORY"
const val USE_FILE_TRANSFER = "USE_FILE_TRANSFER"
}
}
Loading
Loading