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

kinesis: use stage materializer with IODispatcher instead of injected EC #3047

Merged
merged 5 commits into from
Jan 17, 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
Expand Up @@ -40,8 +40,7 @@ private[kinesis] object KinesisSchedulerSourceStage {
private[kinesis] class KinesisSchedulerSourceStage(
settings: KinesisSchedulerSourceSettings,
schedulerBuilder: ShardRecordProcessorFactory => Scheduler
)(implicit ec: ExecutionContext)
extends GraphStageWithMaterializedValue[SourceShape[CommittableRecord], Future[Scheduler]] {
) extends GraphStageWithMaterializedValue[SourceShape[CommittableRecord], Future[Scheduler]] {

private val out = Outlet[CommittableRecord]("Records")
override def shape: SourceShape[CommittableRecord] = new SourceShape[CommittableRecord](out)
Expand Down Expand Up @@ -69,6 +68,7 @@ private[kinesis] class KinesisSchedulerSourceStage(
private[this] var schedulerOpt: Option[Scheduler] = None

override def preStart(): Unit = {
implicit val ec: ExecutionContext = executionContext(attributes)
val scheduler = schedulerBuilder(new ShardRecordProcessorFactory {
override def shardRecordProcessor(): ShardRecordProcessor =
new ShardProcessor(newRecordCallback)
Expand Down Expand Up @@ -103,6 +103,23 @@ private[kinesis] class KinesisSchedulerSourceStage(
failStage(SchedulerUnexpectedShutdown(e))
}
override def postStop(): Unit =
schedulerOpt.foreach(scheduler => Future(if (!scheduler.shutdownComplete()) scheduler.shutdown()))
schedulerOpt.foreach(
scheduler => if (!scheduler.shutdownComplete()) scheduler.shutdown()
)
Copy link
Member

Choose a reason for hiding this comment

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

Contains a lock and possible other blocking stuff, better do it right away inside the stage which is running on the IO dispatcher or make sure this also runs on the ec.


protected def executionContext(attributes: Attributes): ExecutionContext = {
val dispatcherId = (attributes.get[ActorAttributes.Dispatcher](ActorAttributes.IODispatcher) match {
case ActorAttributes.Dispatcher("") =>
ActorAttributes.IODispatcher
case d => d
}) match {
case d @ ActorAttributes.IODispatcher =>
// this one is not a dispatcher id, but is a config path pointing to the dispatcher id
materializer.system.settings.config.getString(d.dispatcher)
case d => d.dispatcher
}

materializer.system.dispatchers.lookup(dispatcherId)
}
Copy link
Member

Choose a reason for hiding this comment

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

This is identical to what is done for the JMS connector. Sadly we don't have anything more straight forward out of the box.

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
package akka.stream.alpakka.kinesis.scaladsl

import akka.NotUsed
import akka.dispatch.ExecutionContexts
import akka.stream._
import akka.stream.alpakka.kinesis.impl.KinesisSchedulerSourceStage
import akka.stream.alpakka.kinesis.{
Expand Down Expand Up @@ -36,13 +35,7 @@ object KinesisSchedulerSource {
schedulerBuilder: ShardRecordProcessorFactory => Scheduler,
settings: KinesisSchedulerSourceSettings
): Source[CommittableRecord, Future[Scheduler]] =
Source
.fromMaterializer { (mat, _) =>
import mat.executionContext
Source
.fromGraph(new KinesisSchedulerSourceStage(settings, schedulerBuilder))
}
.mapMaterializedValue(_.flatMap(identity)(ExecutionContexts.parasitic))
Source.fromGraph(new KinesisSchedulerSourceStage(settings, schedulerBuilder))

def sharded(
schedulerBuilder: ShardRecordProcessorFactory => Scheduler,
Expand Down