-
Notifications
You must be signed in to change notification settings - Fork 141
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract Committer and RebalanceCoordinator classes from Runloop + uni…
…t tests (#1375) The code is mostly just moved to a different place, the logic was left mostly intact. The 'interface' between the components has been decoupled more, i.e. the rebalance listener no longer access the full Runloop's State and the pending commits are stored internally in the Committer. Care has been taken to make the Committer usable during rebalancing as well, with the proper access to the Consumer for example. The part that waits the end of the streams and their commits has been changed to use the Committer. Includes unit tests for the two new components. --------- Co-authored-by: Erik van Oosten <[email protected]>
- Loading branch information
1 parent
b861c99
commit bdde6e3
Showing
10 changed files
with
1,099 additions
and
493 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
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
204 changes: 204 additions & 0 deletions
204
zio-kafka-test/src/test/scala/zio/kafka/consumer/internal/CommitterSpec.scala
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,204 @@ | ||
package zio.kafka.consumer.internal | ||
|
||
import org.apache.kafka.clients.consumer.OffsetAndMetadata | ||
import org.apache.kafka.common.TopicPartition | ||
import org.apache.kafka.common.errors.RebalanceInProgressException | ||
import zio.kafka.consumer.diagnostics.Diagnostics | ||
import zio.test._ | ||
import zio.{ durationInt, Promise, Ref, ZIO } | ||
|
||
import java.util.{ Map => JavaMap } | ||
import scala.jdk.CollectionConverters.MapHasAsJava | ||
|
||
object CommitterSpec extends ZIOSpecDefault { | ||
override def spec = suite("Committer")( | ||
test("signals that a new commit is available") { | ||
for { | ||
runtime <- ZIO.runtime[Any] | ||
commitAvailable <- Promise.make[Nothing, Unit] | ||
committer <- LiveCommitter | ||
.make( | ||
10.seconds, | ||
Diagnostics.NoOp, | ||
new DummyMetrics, | ||
onCommitAvailable = commitAvailable.succeed(()).unit, | ||
sameThreadRuntime = runtime | ||
) | ||
tp = new TopicPartition("topic", 0) | ||
_ <- committer.commit(Map(tp -> new OffsetAndMetadata(0))).forkScoped | ||
_ <- commitAvailable.await | ||
} yield assertCompletes | ||
}, | ||
test("handles a successful commit by completing the commit effect") { | ||
for { | ||
runtime <- ZIO.runtime[Any] | ||
commitAvailable <- Promise.make[Nothing, Unit] | ||
committer <- LiveCommitter.make( | ||
10.seconds, | ||
Diagnostics.NoOp, | ||
new DummyMetrics, | ||
onCommitAvailable = commitAvailable.succeed(()).unit, | ||
sameThreadRuntime = runtime | ||
) | ||
tp = new TopicPartition("topic", 0) | ||
commitFiber <- committer.commit(Map(tp -> new OffsetAndMetadata(0))).forkScoped | ||
_ <- commitAvailable.await | ||
_ <- committer.processQueuedCommits((offsets, callback) => ZIO.attempt(callback.onComplete(offsets, null))) | ||
_ <- commitFiber.join | ||
} yield assertCompletes | ||
}, | ||
test("handles a failed commit by completing the commit effect with a failure") { | ||
for { | ||
runtime <- ZIO.runtime[Any] | ||
commitAvailable <- Promise.make[Nothing, Unit] | ||
committer <- LiveCommitter.make( | ||
10.seconds, | ||
Diagnostics.NoOp, | ||
new DummyMetrics, | ||
onCommitAvailable = commitAvailable.succeed(()).unit, | ||
sameThreadRuntime = runtime | ||
) | ||
tp = new TopicPartition("topic", 0) | ||
commitFiber <- committer.commit(Map(tp -> new OffsetAndMetadata(0))).forkScoped | ||
_ <- commitAvailable.await | ||
_ <- committer.processQueuedCommits((offsets, callback) => | ||
ZIO.attempt(callback.onComplete(offsets, new RuntimeException("Commit failed"))) | ||
) | ||
result <- commitFiber.await | ||
} yield assertTrue(result.isFailure) | ||
}, | ||
test("retries when rebalancing") { | ||
for { | ||
runtime <- ZIO.runtime[Any] | ||
commitAvailable <- Promise.make[Nothing, Unit] | ||
committer <- LiveCommitter.make( | ||
10.seconds, | ||
Diagnostics.NoOp, | ||
new DummyMetrics, | ||
onCommitAvailable = commitAvailable.succeed(()).unit, | ||
sameThreadRuntime = runtime | ||
) | ||
tp = new TopicPartition("topic", 0) | ||
commitFiber <- committer.commit(Map(tp -> new OffsetAndMetadata(0))).forkScoped | ||
_ <- commitAvailable.await | ||
_ <- committer.processQueuedCommits((offsets, callback) => | ||
ZIO.attempt(callback.onComplete(offsets, new RebalanceInProgressException("Rebalance in progress"))) | ||
) | ||
_ <- committer.processQueuedCommits((offsets, callback) => ZIO.attempt(callback.onComplete(offsets, null))) | ||
result <- commitFiber.await | ||
} yield assertTrue(result.isSuccess) | ||
}, | ||
test("adds 1 to the committed last offset") { | ||
for { | ||
runtime <- ZIO.runtime[Any] | ||
commitAvailable <- Promise.make[Nothing, Unit] | ||
committer <- LiveCommitter.make( | ||
10.seconds, | ||
Diagnostics.NoOp, | ||
new DummyMetrics, | ||
onCommitAvailable = commitAvailable.succeed(()).unit, | ||
sameThreadRuntime = runtime | ||
) | ||
tp = new TopicPartition("topic", 0) | ||
_ <- committer.commit(Map(tp -> new OffsetAndMetadata(1))).forkScoped | ||
_ <- commitAvailable.await | ||
committedOffsets <- Promise.make[Nothing, JavaMap[TopicPartition, OffsetAndMetadata]] | ||
_ <- committer.processQueuedCommits((offsets, callback) => | ||
committedOffsets.succeed(offsets) *> ZIO.attempt(callback.onComplete(offsets, null)) | ||
) | ||
offsetsCommitted <- committedOffsets.await | ||
} yield assertTrue( | ||
offsetsCommitted == Map(tp -> new OffsetAndMetadata(2)).asJava | ||
) | ||
}, | ||
test("batches commits from multiple partitions and offsets") { | ||
for { | ||
runtime <- ZIO.runtime[Any] | ||
commitsAvailable <- Promise.make[Nothing, Unit] | ||
nrCommitsDone <- Ref.make(0) | ||
committer <- LiveCommitter.make( | ||
10.seconds, | ||
Diagnostics.NoOp, | ||
new DummyMetrics, | ||
onCommitAvailable = | ||
ZIO.whenZIO(nrCommitsDone.updateAndGet(_ + 1).map(_ == 3))(commitsAvailable.succeed(())).unit, | ||
sameThreadRuntime = runtime | ||
) | ||
tp = new TopicPartition("topic", 0) | ||
tp2 = new TopicPartition("topic", 1) | ||
commitFiber1 <- committer.commit(Map(tp -> new OffsetAndMetadata(1))).forkScoped | ||
commitFiber2 <- committer.commit(Map(tp -> new OffsetAndMetadata(2))).forkScoped | ||
commitFiber3 <- committer.commit(Map(tp2 -> new OffsetAndMetadata(3))).forkScoped | ||
_ <- commitsAvailable.await | ||
committedOffsets <- Promise.make[Nothing, JavaMap[TopicPartition, OffsetAndMetadata]] | ||
_ <- committer.processQueuedCommits((offsets, callback) => | ||
committedOffsets.succeed(offsets) *> ZIO.attempt(callback.onComplete(offsets, null)) | ||
) | ||
_ <- commitFiber1.join zip commitFiber2.join zip commitFiber3.join | ||
offsetsCommitted <- committedOffsets.await | ||
} yield assertTrue( | ||
offsetsCommitted == Map(tp -> new OffsetAndMetadata(3), tp2 -> new OffsetAndMetadata(4)).asJava | ||
) | ||
}, | ||
test("keeps track of pending commits") { | ||
for { | ||
runtime <- ZIO.runtime[Any] | ||
commitAvailable <- Promise.make[Nothing, Unit] | ||
committer <- LiveCommitter.make( | ||
10.seconds, | ||
Diagnostics.NoOp, | ||
new DummyMetrics, | ||
onCommitAvailable = commitAvailable.succeed(()).unit, | ||
sameThreadRuntime = runtime | ||
) | ||
tp = new TopicPartition("topic", 0) | ||
commitFiber <- committer.commit(Map(tp -> new OffsetAndMetadata(0))).forkScoped | ||
_ <- commitAvailable.await | ||
_ <- committer.processQueuedCommits((offsets, callback) => ZIO.attempt(callback.onComplete(offsets, null))) | ||
pendingCommitsDuringCommit <- committer.pendingCommitCount | ||
_ <- committer.cleanupPendingCommits | ||
pendingCommitsAfterCommit <- committer.pendingCommitCount | ||
_ <- commitFiber.join | ||
} yield assertTrue(pendingCommitsDuringCommit == 1 && pendingCommitsAfterCommit == 0) | ||
}, | ||
test("keep track of committed offsets") { | ||
for { | ||
runtime <- ZIO.runtime[Any] | ||
commitAvailable <- Promise.make[Nothing, Unit] | ||
committer <- LiveCommitter.make( | ||
10.seconds, | ||
Diagnostics.NoOp, | ||
new DummyMetrics, | ||
onCommitAvailable = commitAvailable.succeed(()).unit, | ||
sameThreadRuntime = runtime | ||
) | ||
tp = new TopicPartition("topic", 0) | ||
commitFiber <- committer.commit(Map(tp -> new OffsetAndMetadata(0))).forkScoped | ||
_ <- commitAvailable.await | ||
_ <- committer.processQueuedCommits((offsets, callback) => ZIO.attempt(callback.onComplete(offsets, null))) | ||
committedOffsets <- committer.getCommittedOffsets | ||
_ <- commitFiber.join | ||
} yield assertTrue(committedOffsets.offsets == Map(tp -> 0L)) | ||
}, | ||
test("clean committed offsets of no-longer assigned partitions") { | ||
for { | ||
runtime <- ZIO.runtime[Any] | ||
commitAvailable <- Promise.make[Nothing, Unit] | ||
committer <- LiveCommitter.make( | ||
10.seconds, | ||
Diagnostics.NoOp, | ||
new DummyMetrics, | ||
onCommitAvailable = commitAvailable.succeed(()).unit, | ||
sameThreadRuntime = runtime | ||
) | ||
tp = new TopicPartition("topic", 0) | ||
commitFiber <- committer.commit(Map(tp -> new OffsetAndMetadata(0))).forkScoped | ||
_ <- commitAvailable.await | ||
_ <- committer.processQueuedCommits((offsets, callback) => ZIO.attempt(callback.onComplete(offsets, null))) | ||
_ <- committer.keepCommitsForPartitions(Set.empty) | ||
committedOffsets <- committer.getCommittedOffsets | ||
_ <- commitFiber.join | ||
} yield assertTrue(committedOffsets.offsets.isEmpty) | ||
} | ||
) @@ TestAspect.withLiveClock @@ TestAspect.nonFlaky(100) | ||
} |
Oops, something went wrong.