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

Implement Graceful Shutdown #55

Merged
merged 5 commits into from
Nov 19, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions src/main/scala/zio/kafka/client/Consumer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ class Consumer private (
): BlockingTask[Map[TopicPartition, Long]] =
consumer.withConsumer(_.endOffsets(partitions.asJava, timeout.asJava).asScala.mapValues(_.longValue()).toMap)

def gracefulShutdown: UIO[Unit] =
runloop.deps.gracefulShutdown

def listTopics(timeout: Duration = Duration.Infinity): BlockingTask[Map[String, List[PartitionInfo]]] =
consumer.withConsumer(_.listTopics(timeout.asJava).asScala.mapValues(_.asScala.toList).toMap)

Expand Down
33 changes: 27 additions & 6 deletions src/main/scala/zio/kafka/client/Runloop.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ object Runloop {
partitions: Queue[Take[Throwable, (TopicPartition, ZStreamChunk[Any, Throwable, ByteArrayCommittableRecord])]],
rebalancingRef: Ref[Boolean],
rebalanceListener: ConsumerRebalanceListener,
diagnostics: Diagnostics
diagnostics: Diagnostics,
shutdownRef: Ref[Boolean]
) {
def commit(cmd: Command.Commit) = commitQueue.offer(cmd).unit
def commits = ZStream.fromQueue(commitQueue)
Expand All @@ -60,6 +61,14 @@ object Runloop {
partitions.offer(Take.Value(tp -> data)).unit

def emitIfEnabledDiagnostic(event: DiagnosticEvent) = diagnostics.emitIfEnabled(event)

val isShutdown = shutdownRef.get

def gracefulShutdown: UIO[Unit] =
for {
shutdown <- shutdownRef.modify((_, true))
_ <- partitions.offer(Take.End).when(!shutdown)
} yield ()
}
object Deps {
def make(
Expand Down Expand Up @@ -100,6 +109,7 @@ object Runloop {
}
}
.toManaged_
shutdownRef <- Ref.make(false).toManaged_
} yield Deps(
consumer,
pollFrequency,
Expand All @@ -109,7 +119,8 @@ object Runloop {
partitions,
rebalancingRef,
listener,
diagnostics
diagnostics,
shutdownRef
)
}

Expand Down Expand Up @@ -384,17 +395,27 @@ object Runloop {
else doCommit(List(cmd)).as(state)
} yield newState

def handleShutdown(state: State, cmd: Command): BlockingTask[State] = cmd match {
case Command.Poll() => UIO.succeed(state)
case req @ Command.Request(_, _) => req.cont.fail(None).as(state)
case cmd @ Command.Commit(_, _) => handleCommit(state, cmd)
}

ZStream
.mergeAll(3, 32)(
deps.polls,
deps.requests,
deps.commits
)
.foldM(State.initial) { (state, cmd) =>
cmd match {
case Command.Poll() => handlePoll(state)
case req @ Command.Request(_, _) => handleRequest(state, req)
case cmd @ Command.Commit(_, _) => handleCommit(state, cmd)
deps.isShutdown.flatMap { shutdown =>
if (shutdown) handleShutdown(state, cmd)
else
cmd match {
case Command.Poll() => handlePoll(state)
case req @ Command.Request(_, _) => handleRequest(state, req)
case cmd @ Command.Commit(_, _) => handleCommit(state, cmd)
}
}
}
.onError { cause =>
Expand Down
19 changes: 19 additions & 0 deletions src/test/scala/zio/kafka/client/ConsumerTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -232,5 +232,24 @@ class ConsumerTest extends WordSpecLike with Matchers with LazyLogging with Defa
} yield consumeResult.fold(_ => succeed, _ => fail("Expected consumeWith to fail"))
}
}

"shutting down gracefully" should {
"not receive messages after shutting down" in runWithConsumer("group150", "client150") { consumer =>
for {
kvs <- ZIO((1 to 5).toList.map(i => (s"key$i", s"msg$i")))
_ <- produceMany("topic150", kvs)
_ <- consumer.gracefulShutdown
records <- consumer
.subscribeAnd(Subscription.Topics(Set("topic150")))
.plainStream(Serde.string, Serde.string)
.flattenChunks
.take(5)
.runCollect
_ <- ZIO.effectTotal(records.map { r =>
(r.record.key, r.record.value)
} shouldBe empty)
} yield ()
}
}
}
}