forked from airbytehq/airbyte-platform
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: implement pub-sub writer (#12812)
- Loading branch information
1 parent
d77f3f9
commit 7ad6444
Showing
12 changed files
with
262 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
...ker/src/main/kotlin/io/airbyte/workers/config/StateCheckSumEventPubSubPublisherFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package io.airbyte.workers.config | ||
|
||
import com.google.api.gax.batching.BatchingSettings | ||
import com.google.cloud.pubsub.v1.Publisher | ||
import com.google.pubsub.v1.TopicName | ||
import io.micronaut.context.annotation.Factory | ||
import io.micronaut.context.annotation.Requires | ||
import io.micronaut.context.annotation.Value | ||
import jakarta.inject.Named | ||
import jakarta.inject.Singleton | ||
import org.threeten.bp.Duration | ||
import java.util.function.Supplier | ||
|
||
private const val MAX_BATCH_SIZE = 50L | ||
private const val MAX_BYTES_THRESHOLD = 5000L | ||
private const val MAX_DELAY_THRESHOLD = 100L | ||
|
||
@Factory | ||
class StateCheckSumEventPubSubPublisherFactory { | ||
@Singleton | ||
@Requires(property = "airbyte.cloud.pubsub.enabled", value = "true", defaultValue = "false") | ||
@Named("pubSubPublisherSupplier") | ||
fun pubSubPublisherSupplier( | ||
@Value("\${airbyte.cloud.pubsub.topic}") topicName: String, | ||
@Value("\${airbyte.cloud.pubsub.request-bytes-threshold}") requestBytesThreshold: Int, | ||
@Value("\${airbyte.cloud.pubsub.message-count-batch-size}") messageCountBatchSize: Int, | ||
@Value("\${airbyte.cloud.pubsub.publish-delay-threshold-ms}") publishDelayThreshold: Int, | ||
): Supplier<Publisher> { | ||
val batchingSettings = | ||
BatchingSettings.newBuilder() | ||
.setElementCountThreshold(Math.min(messageCountBatchSize.toLong(), MAX_BATCH_SIZE)) | ||
.setRequestByteThreshold(Math.min(requestBytesThreshold.toLong(), MAX_BYTES_THRESHOLD)) | ||
.setDelayThreshold(Duration.ofMillis(Math.min(publishDelayThreshold.toLong(), MAX_DELAY_THRESHOLD))) | ||
.build() | ||
|
||
return Supplier { | ||
Publisher.newBuilder(TopicName.parse(topicName)).setBatchingSettings(batchingSettings).build() | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...mmons-worker/src/main/kotlin/io/airbyte/workers/general/StateCheckSumEventPubSubWriter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package io.airbyte.workers.general | ||
|
||
import com.google.cloud.pubsub.v1.Publisher | ||
import com.google.protobuf.ByteString | ||
import com.google.pubsub.v1.PubsubMessage | ||
import io.airbyte.commons.json.Jsons | ||
import io.airbyte.workers.models.StateCheckSumCountEvent | ||
import io.github.oshai.kotlinlogging.KotlinLogging | ||
import io.micronaut.context.annotation.Requires | ||
import jakarta.inject.Named | ||
import jakarta.inject.Singleton | ||
import java.util.concurrent.TimeUnit | ||
import java.util.function.Supplier | ||
|
||
private val logger = KotlinLogging.logger { } | ||
|
||
@Singleton | ||
@Requires(property = "airbyte.cloud.pubsub.enabled", value = "true", defaultValue = "false") | ||
@Named("pubSubWriter") | ||
class StateCheckSumEventPubSubWriter( | ||
@Named("pubSubPublisherSupplier") private val publisherSupplier: Supplier<Publisher>, | ||
) { | ||
private val publisher: Publisher by lazy { publisherSupplier.get() } | ||
|
||
fun publishEvent(event: List<StateCheckSumCountEvent>) { | ||
try { | ||
event.forEach { stateCheckSumEvent -> Jsons.serialize(stateCheckSumEvent)?.let { writeMessageToPubSub(it) } } | ||
} catch (e: Exception) { | ||
logger.error(e) { "Swallowed exception during publishing pubsub message" } | ||
} | ||
} | ||
|
||
private fun writeMessageToPubSub(message: String) { | ||
try { | ||
val data = ByteString.copyFromUtf8(message) | ||
val pubsubMessage: PubsubMessage = | ||
PubsubMessage | ||
.newBuilder() | ||
.setData(data) | ||
// Filter in subscription is set to -> attributes.event = "state_checksum_metrics" | ||
.putAllAttributes(mapOf("event" to "state_checksum_metrics")) | ||
.build() | ||
publisher.publish(pubsubMessage) | ||
} catch (e: Exception) { | ||
logger.error(e) { "Swallowed exception during writing pubsub message" } | ||
} | ||
} | ||
|
||
fun close() { | ||
try { | ||
logger.info { "Closing StateCheckSumEventPubSubWriter" } | ||
publisher.shutdown() | ||
publisher.awaitTermination(1, TimeUnit.MINUTES) | ||
} catch (e: Exception) { | ||
logger.error(e) { "Swallowed exception during closing pubsub class" } | ||
} | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
airbyte-commons-worker/src/main/kotlin/io/airbyte/workers/models/StateCheckSumCountEvent.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package io.airbyte.workers.models | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator | ||
import com.fasterxml.jackson.annotation.JsonProperty | ||
|
||
data class StateCheckSumCountEvent | ||
@JsonCreator | ||
constructor( | ||
@JsonProperty("airbyte_version") val airbyteVersion: String?, | ||
@JsonProperty("attempt_number") val attemptNumber: Long, | ||
@JsonProperty("connection_id") val connectionId: String, | ||
@JsonProperty("deployment_id") val deploymentId: String?, | ||
@JsonProperty("deployment_mode") val deploymentMode: String?, | ||
@JsonProperty("email") val email: String?, | ||
@JsonProperty("id") val id: String, | ||
@JsonProperty("job_id") val jobId: String, | ||
@JsonProperty("record_count") val recordCount: Long, | ||
@JsonProperty("state_hash") val stateHash: String, | ||
@JsonProperty("state_id") val stateId: String, | ||
@JsonProperty("state_origin") val stateOrigin: String, | ||
@JsonProperty("state_type") val stateType: String, | ||
@JsonProperty("stream_name") val streamName: String?, | ||
@JsonProperty("stream_namespace") val streamNamespace: String?, | ||
@JsonProperty("timestamp") val timestamp: Long, | ||
@JsonProperty("valid_data") val validData: Boolean, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
111 changes: 111 additions & 0 deletions
111
...s-worker/src/test/kotlin/io/airbyte/workers/general/StateCheckSumEventPubSubWriterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
package io.airbyte.workers.general | ||
|
||
import com.google.api.core.ApiFuture | ||
import com.google.cloud.pubsub.v1.Publisher | ||
import com.google.protobuf.ByteString | ||
import com.google.pubsub.v1.PubsubMessage | ||
import io.airbyte.commons.json.Jsons | ||
import io.airbyte.workers.models.StateCheckSumCountEvent | ||
import io.micronaut.context.annotation.Property | ||
import io.micronaut.test.extensions.junit5.annotation.MicronautTest | ||
import io.mockk.Runs | ||
import io.mockk.clearAllMocks | ||
import io.mockk.every | ||
import io.mockk.just | ||
import io.mockk.mockk | ||
import io.mockk.verify | ||
import org.junit.jupiter.api.AfterEach | ||
import org.junit.jupiter.api.BeforeEach | ||
import org.junit.jupiter.api.Test | ||
import java.time.Instant | ||
import java.util.Random | ||
import java.util.UUID | ||
import java.util.function.Supplier | ||
import javax.inject.Inject | ||
|
||
@MicronautTest | ||
@Property(name = "airbyte.cloud.pubsub.enabled", value = "true") | ||
@Property(name = "airbyte.cloud.pubsub.topic", value = "dummy-topic") | ||
@Property(name = "airbyte.cloud.pubsub.message-count-batch-size", value = "10") | ||
@Property(name = "airbyte.cloud.pubsub.request-bytes-threshold", value = "10") | ||
@Property(name = "airbyte.cloud.pubsub.message-count-batch-size", value = "10") | ||
@Property(name = "airbyte.cloud.pubsub.publish-delay-threshold-ms", value = "10") | ||
class StateCheckSumEventPubSubWriterTest { | ||
private val publisherSupplier = mockk<Supplier<Publisher>>(relaxed = true) | ||
|
||
private val publisher = mockk<Publisher>(relaxed = true) | ||
|
||
private lateinit var pubSubWriter: StateCheckSumEventPubSubWriter | ||
|
||
@Inject | ||
lateinit var pubSubWriterMicronautBean: StateCheckSumEventPubSubWriter | ||
|
||
@BeforeEach | ||
fun setUp() { | ||
every { publisherSupplier.get() } returns publisher | ||
pubSubWriter = StateCheckSumEventPubSubWriter(publisherSupplier) | ||
} | ||
|
||
@AfterEach | ||
fun tearDown() { | ||
clearAllMocks() | ||
} | ||
|
||
@Test | ||
fun `bean creation should work`() { | ||
pubSubWriterMicronautBean.publishEvent(listOf(getDummyCheckSumEvent())) | ||
pubSubWriterMicronautBean.close() | ||
} | ||
|
||
@Test | ||
fun `events should be written to pubsub`() { | ||
val event = getDummyCheckSumEvent() | ||
val expectedMessage: PubsubMessage = | ||
PubsubMessage | ||
.newBuilder() | ||
.setData(ByteString.copyFromUtf8(Jsons.serialize(event))) | ||
// Filter in subscription is set to -> attributes.event = "state_checksum_metrics" | ||
.putAllAttributes(mapOf("event" to "state_checksum_metrics")) | ||
.build() | ||
every { publisher.shutdown() } just Runs | ||
every { publisher.awaitTermination(any(), any()) } returns true | ||
every { publisher.publish(expectedMessage) } returns mockk<ApiFuture<String>>() | ||
|
||
pubSubWriter.publishEvent(listOf(event, event, event, event, event, event)) | ||
pubSubWriter.close() | ||
|
||
verify(exactly = 6) { publisher.publish(expectedMessage) } | ||
} | ||
|
||
@Test | ||
fun `close should shutdown publisher`() { | ||
every { publisher.shutdown() } just Runs | ||
every { publisher.awaitTermination(any(), any()) } returns true | ||
|
||
pubSubWriter.close() | ||
|
||
verify(exactly = 1) { publisher.shutdown() } | ||
} | ||
|
||
private fun getDummyCheckSumEvent(): StateCheckSumCountEvent { | ||
return StateCheckSumCountEvent( | ||
UUID.randomUUID().toString(), | ||
Random().nextLong(), | ||
UUID.randomUUID().toString(), | ||
"LOCAL_MACHINE", | ||
"LOCAL", | ||
"[email protected]", | ||
UUID.randomUUID().toString(), | ||
Random().nextLong().toString(), | ||
Random().nextLong(), | ||
UUID.randomUUID().toString(), | ||
Random().nextLong().toString(), | ||
"SOURCE", | ||
"STREAM", | ||
"stream", | ||
null, | ||
Instant.now().toEpochMilli() * 1000L, | ||
Random().nextBoolean(), | ||
) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters