-
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.
Allow for some auth exceptions during poll (#1270)
Some brokers are sometimes too slow to authorize or authenticate a poll. This results in spurious exceptions. With this change we continue polling unless the error happens too often. We test the change with a new type of unit test for Runloop.
- Loading branch information
1 parent
a8596f3
commit c717359
Showing
4 changed files
with
173 additions
and
6 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
zio-kafka-test/src/test/scala/zio/kafka/consumer/internal/RunloopSpec.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,127 @@ | ||
package zio.kafka.consumer.internal | ||
|
||
import org.apache.kafka.clients.consumer.{ ConsumerRecord, MockConsumer, OffsetResetStrategy } | ||
import org.apache.kafka.common.TopicPartition | ||
import org.apache.kafka.common.errors.{ AuthenticationException, AuthorizationException } | ||
import zio._ | ||
import zio.kafka.consumer.{ ConsumerSettings, Subscription } | ||
import zio.kafka.consumer.diagnostics.Diagnostics | ||
import zio.kafka.consumer.internal.RunloopAccess.PartitionAssignment | ||
import zio.metrics.{ MetricState, Metrics } | ||
import zio.stream.{ Take, ZStream } | ||
import zio.test.TestAspect.withLiveClock | ||
import zio.test._ | ||
|
||
import scala.jdk.CollectionConverters._ | ||
|
||
object RunloopSpec extends ZIOSpecDefault { | ||
|
||
private type BinaryMockConsumer = MockConsumer[Array[Byte], Array[Byte]] | ||
private type PartitionsHub = Hub[Take[Throwable, PartitionAssignment]] | ||
|
||
private val tp10 = new TopicPartition("t1", 0) | ||
private val key123 = "123".getBytes | ||
|
||
private val consumerSettings = ConsumerSettings(List("bootstrap")) | ||
|
||
override def spec: Spec[TestEnvironment with Scope, Any] = | ||
suite("RunloopSpec")( | ||
test("runloop creates a new partition stream and polls for new records") { | ||
withRunloop { (mockConsumer, partitionsHub, runloop) => | ||
mockConsumer.schedulePollTask { () => | ||
mockConsumer.updateEndOffsets(Map(tp10 -> Long.box(0L)).asJava) | ||
mockConsumer.rebalance(Seq(tp10).asJava) | ||
mockConsumer.addRecord(makeConsumerRecord(tp10, key123)) | ||
} | ||
for { | ||
streamStream <- ZStream.fromHubScoped(partitionsHub) | ||
_ <- runloop.addSubscription(Subscription.Topics(Set(tp10.topic()))) | ||
record <- streamStream | ||
.map(_.exit) | ||
.flattenExitOption | ||
.flattenChunks | ||
.take(1) | ||
.mapZIO { case (_, stream) => | ||
stream.runHead | ||
.someOrFail(new AssertionError("Expected at least 1 record")) | ||
} | ||
.runHead | ||
.someOrFail(new AssertionError("Expected at least 1 record from the streams")) | ||
} yield assertTrue( | ||
record.key sameElements key123 | ||
) | ||
} | ||
}, | ||
test("runloop retries poll upon AuthorizationException and AuthenticationException") { | ||
withRunloop { (mockConsumer, partitionsHub, runloop) => | ||
mockConsumer.schedulePollTask { () => | ||
mockConsumer.updateEndOffsets(Map(tp10 -> Long.box(0L)).asJava) | ||
mockConsumer.rebalance(Seq(tp10).asJava) | ||
} | ||
mockConsumer.schedulePollTask { () => | ||
mockConsumer.setPollException(new AuthorizationException("~~test~~")) | ||
} | ||
mockConsumer.schedulePollTask { () => | ||
mockConsumer.setPollException(new AuthenticationException("~~test~~")) | ||
} | ||
mockConsumer.schedulePollTask { () => | ||
mockConsumer.addRecord(makeConsumerRecord(tp10, key123)) | ||
} | ||
for { | ||
streamStream <- ZStream.fromHubScoped(partitionsHub) | ||
_ <- runloop.addSubscription(Subscription.Topics(Set(tp10.topic()))) | ||
record <- streamStream | ||
.map(_.exit) | ||
.flattenExitOption | ||
.flattenChunks | ||
.take(1) | ||
.mapZIO { case (_, stream) => | ||
stream.runHead | ||
.someOrFail(new AssertionError("Expected at least 1 record")) | ||
} | ||
.runHead | ||
.someOrFail(new AssertionError("Expected at least 1 record from the streams")) | ||
authErrorCount <- ZIO.metrics.map(counterValue("ziokafka_consumer_poll_auth_errors")) | ||
} yield assertTrue( | ||
record.key sameElements key123, | ||
authErrorCount.contains(2d) | ||
) | ||
} | ||
} | ||
) @@ withLiveClock | ||
|
||
private def withRunloop( | ||
f: (BinaryMockConsumer, PartitionsHub, Runloop) => ZIO[Scope, Throwable, TestResult] | ||
): ZIO[Scope, Throwable, TestResult] = | ||
ZIO.scoped { | ||
val mockConsumer = new BinaryMockConsumer(OffsetResetStrategy.LATEST) | ||
for { | ||
consumerAccess <- ConsumerAccess.make(mockConsumer) | ||
consumerScope <- ZIO.scope | ||
partitionsHub <- ZIO | ||
.acquireRelease(Hub.unbounded[Take[Throwable, PartitionAssignment]])(_.shutdown) | ||
.provide(ZLayer.succeed(consumerScope)) | ||
runloop <- Runloop.make( | ||
consumerSettings, | ||
100.millis, | ||
100.millis, | ||
Diagnostics.NoOp, | ||
consumerAccess, | ||
partitionsHub | ||
) | ||
result <- f(mockConsumer, partitionsHub, runloop) | ||
} yield result | ||
} | ||
|
||
private def makeConsumerRecord(tp: TopicPartition, key: Array[Byte]): ConsumerRecord[Array[Byte], Array[Byte]] = | ||
new ConsumerRecord[Array[Byte], Array[Byte]](tp.topic(), tp.partition(), 0L, key, "value".getBytes) | ||
|
||
private def counterValue(counterName: String)(metrics: Metrics): Option[Double] = | ||
metrics.metrics | ||
.find(_.metricKey.name == counterName) | ||
.map(_.metricState) | ||
.flatMap { | ||
case MetricState.Counter(count) => Some(count) | ||
case _ => Option.empty[Double] | ||
} | ||
} |
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