You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Context: We are using kafka compacted topics to keep our application state within Kafka as well, and the state is read using saramaConsumer and PartitionConsumer APIs. The app state can be read concurrently and and thus we have introduced locking so that only a single PartitionConsumer can be started at a time to avoid error The topic/partition is already being consumed. Below is what the implementation looks like after removing some app specific code
Code
kp.Mu.Lock()
deferkp.Mu.Unlock()
// Create a partition consumerpartitionConsumer, err:=kp.Consumer.ConsumePartition(kp.Topic, 0, sarama.OffsetOldest)
iferr!=nil {
returnnil, err
}
deferpartitionConsumer.Close()
// Get the high watermark offsethighWaterMarkOffset:=partitionConsumer.HighWaterMarkOffset()
ifhighWaterMarkOffset==0 {
returnnil
}
timeoutChan:=kp.Clock.After(timeout)
for {
select {
casemessage, ok:=<-partitionConsumer.Messages():
if!ok {
returnnil
}
iferr:=processMessage(message); err!=nil {
returnerr
}
// Stop consuming once all messages have been readifmessage.Offset+1>=highWaterMarkOffset {
returnnil
}
caseerr:=<-partitionConsumer.Errors():
returnerr.Errcase<-ctx.Done():
returnctx.Err()
case<-timeoutChan:
returnnil
}
}
Problem: The tests which uses mocks Consumer and PartitionConsumer APIs are failing when executed concurrently with the error The topic/partition is already being consumed and I've found that the mocks.PartitionConsumer Close() method should set pc.consumed = false for direct pc.Close() calls. Also the check if !pc.consumed needs to be removed as the pc.Close() from consumer.Close() will fail otherwise.
Context: We are using
kafka
compacted topics to keep our application state within Kafka as well, and the state is read using saramaConsumer
andPartitionConsumer
APIs. The app state can be read concurrently and and thus we have introduced locking so that only a singlePartitionConsumer
can be started at a time to avoid errorThe topic/partition is already being consumed
. Below is what the implementation looks like after removing some app specific codeCode
Problem: The tests which uses mocks
Consumer
andPartitionConsumer
APIs are failing when executed concurrently with the errorThe topic/partition is already being consumed
and I've found that the mocks.PartitionConsumer Close() method should setpc.consumed = false
for direct pc.Close() calls. Also the checkif !pc.consumed
needs to be removed as the pc.Close() from consumer.Close() will fail otherwise.Test
The text was updated successfully, but these errors were encountered: