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

Pubsub sink #327

Merged
merged 1 commit into from
Aug 22, 2023
Merged

Pubsub sink #327

merged 1 commit into from
Aug 22, 2023

Conversation

pondzix
Copy link
Contributor

@pondzix pondzix commented Aug 10, 2023

  • full implementation of PubSub sink: producing events asynchronously + background pubsub health check
  • core part of integration tests based on CE2 has been copied to the new http4s module and ported to CE3.

@benjben benjben force-pushed the http4s_config branch 4 times, most recently from dbbd023 to 76a3474 Compare August 16, 2023 17:09
Base automatically changed from http4s_config to feature/http4s August 16, 2023 17:20
@pondzix pondzix force-pushed the http4s/pubsub_sink branch 2 times, most recently from f577884 to 9deaf81 Compare August 18, 2023 07:37
Copy link
Contributor

@benjben benjben left a comment

Choose a reason for hiding this comment

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

Looks great @pondzix !

}
}

private def parse(resource: String): Either[ExitCode, Config[PubSubSinkConfig]] = {
Copy link
Contributor

Choose a reason for hiding this comment

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

Could be put in http4s module ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I would keep it here for now. When we add more sinks and we indeed keep repeating this piece of code, we can extract to common shared module.


private def parse(resource: String): Either[ExitCode, Config[PubSubSinkConfig]] = {
val path = Paths.get(getClass.getResource(resource).toURI)
ConfigParser.fromPath[IO, PubSubSinkConfig](Some(path)).value.unsafeRunSync()
Copy link
Contributor

Choose a reason for hiding this comment

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

Could we use CatsEffect to avoid this unsafeRunSync() ?

Comment on lines +9 to +36
object BuilderOps {

implicit class PublisherBuilderOps(val builder: Publisher.Builder) extends AnyVal {
def setProvidersForEmulator(): Publisher.Builder =
customEmulatorHost().fold(builder) { emulatorHost =>
builder
.setChannelProvider(createCustomChannelProvider(emulatorHost))
.setCredentialsProvider(NoCredentialsProvider.create())
}
}

implicit class TopicAdminBuilderOps(val builder: TopicAdminSettings.Builder) extends AnyVal {
def setProvidersForEmulator(): TopicAdminSettings.Builder =
customEmulatorHost().fold(builder) { emulatorHost =>
builder
.setTransportChannelProvider(createCustomChannelProvider(emulatorHost))
.setCredentialsProvider(NoCredentialsProvider.create())
}
}

private def customEmulatorHost(): Option[String] =
sys.env.get("PUBSUB_EMULATOR_HOST")

private def createCustomChannelProvider(emulatorHost: String): FixedTransportChannelProvider = {
val channel = ManagedChannelBuilder.forTarget(emulatorHost).usePlaintext().build()
FixedTransportChannelProvider.create(GrpcTransportChannel.create(channel))
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice !

}

object PubSubSink {
private val UserAgent = s"snowplow/stream-collector-${BuildInfo.version}"
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
private val UserAgent = s"snowplow/stream-collector-${BuildInfo.version}"
private val UserAgent = s"${BuildInfo.dockerAlias}:${BuildInfo.version}"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Shouldn't that be just BuildInfo.dockerAlias? It looks like it already contains version

@@ -67,7 +67,7 @@ object ConfigParser {
}

private def loadAll(config: TypesafeConfig): TypesafeConfig =
namespaced(ConfigFactory.load(namespaced(config.withFallback(namespaced(ConfigFactory.load())))))
namespaced(config.withFallback(namespaced(ConfigFactory.load())))
Copy link
Contributor

Choose a reason for hiding this comment

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

healthCheckTask: F[Unit]
): Resource[F, Unit] = {
val checkThenSleep = healthCheckTask *> Async[F].sleep(sinkConfig.startupCheckInterval)
checkThenSleep.untilM_(isHealthyState.get).background.void
Copy link
Contributor

Choose a reason for hiding this comment

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

👌

) extends Sink[F] {

override def storeRawEvents(events: List[Array[Byte]], key: String): F[Unit] =
produceBatch(events).start.void
Copy link
Contributor

Choose a reason for hiding this comment

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

If the tracker doesn't batch the events, events will always be of size 1. But nevertheless this seems fine to me to start a Fiber for each single event.

)

private def handlePublishError(error: Throwable): F[Unit] =
isHealthyState.set(false) *> Logger[F].error(createErrorMessage(error))
Copy link
Contributor

Choose a reason for hiding this comment

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

Related to the discussion about logging policy, we'll need to decide if we should be logging errors for individual events or if we need some batching.

Copy link
Contributor

Choose a reason for hiding this comment

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

Good point. This only becomes an issue if there is an outage and if it lasts for a long time. Ideally we should find a nice pattern in the code for de-duping log messages upon errors. And we should start using it everywhere when we've decided on that pattern.

No need to hold up this current PR though.

@pondzix pondzix force-pushed the http4s/pubsub_sink branch 6 times, most recently from f5f9e72 to 656ccd6 Compare August 21, 2023 10:15
Copy link
Contributor

@istreeter istreeter left a comment

Choose a reason for hiding this comment

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

Looks good :)


private def createTopicAdminClient[F[_]: Sync](): Resource[F, TopicAdminClient] = {
val builder = TopicAdminSettings.newBuilder().setProvidersForEmulator().build()
Resource.make(Sync[F].delay(TopicAdminClient.create(builder)))(client => Sync[F].delay(client.close()))
Copy link
Contributor

Choose a reason for hiding this comment

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

Do you know if client.close() is blocking or non-blocking?

)

private def handlePublishError(error: Throwable): F[Unit] =
isHealthyState.set(false) *> Logger[F].error(createErrorMessage(error))
Copy link
Contributor

Choose a reason for hiding this comment

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

Good point. This only becomes an issue if there is an outage and if it lasts for a long time. Ideally we should find a nice pattern in the code for de-duping log messages upon errors. And we should start using it everywhere when we've decided on that pattern.

No need to hold up this current PR though.

@pondzix pondzix merged commit cfdaa58 into feature/http4s Aug 22, 2023
2 of 3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants