Skip to content

Optimize Converters.topicPartitionsSetF - #608

Open
nviliunov-evolution-throwaway wants to merge 1 commit into
masterfrom
nv/opt
Open

Optimize Converters.topicPartitionsSetF#608
nviliunov-evolution-throwaway wants to merge 1 commit into
masterfrom
nv/opt

Conversation

@nviliunov-evolution-throwaway

@nviliunov-evolution-throwaway nviliunov-evolution-throwaway commented Aug 10, 2026

Copy link
Copy Markdown
Contributor

Summary by CodeRabbit

  • Bug Fixes

    • Improved conversion of topic partition collections, including safer handling of invalid values and conversion failures.
    • Added non-fatal error protection during topic partition processing.
  • Refactor

    • Standardized internal partition creation and representation for more consistent behavior.

@sonarqubecloud

Copy link
Copy Markdown

@coderabbitai

coderabbitai Bot commented Aug 10, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

The PR updates topicPartitionsSetF to use explicit Try handling and a set builder. It also adds a private Partition implementation class and uses it for boundary and regular partitions.

Changes

Conversion and partition updates

Layer / File(s) Summary
Topic-partition set conversion
skafka/src/main/scala/com/evolutiongaming/skafka/Converters.scala
topicPartitionsSetF now converts elements with Try, rethrows conversion failures, and builds the result with Set.newBuilder inside catchNonFatal.
Concrete partition implementation
skafka/src/main/scala/com/evolutiongaming/skafka/Partition.scala
min, max, and Partition.of now instantiate the private Impl subclass instead of anonymous subclasses.

Estimated code review effort: 3 (Moderate) | ~20 minutes

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly identifies the main change: optimizing Converters.topicPartitionsSetF.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch nv/opt

Comment @coderabbitai help to get the list of available commands.

@coveralls

coveralls commented Aug 10, 2026

Copy link
Copy Markdown

Coverage Report for CI Build 31409780871

Coverage decreased (-0.1%) to 79.17%

Details

  • Coverage decreased (-0.1%) from the base build.
  • Patch coverage: 1 uncovered change across 1 file (8 of 9 lines covered, 88.89%).
  • 162 coverage regressions across 28 files.

Uncovered Changes

File Changed Covered %
skafka/src/main/scala/com/evolutiongaming/skafka/Converters.scala 6 5 83.33%
Total (2 files) 9 8 88.89%

Coverage Regressions

162 previously-covered lines in 28 files lost coverage.

Top 10 Files by Coverage Loss Lines Losing Coverage Coverage
skafka/src/main/scala/com/evolutiongaming/skafka/consumer/Consumer.scala 51 83.01%
skafka/src/main/scala/com/evolutiongaming/skafka/consumer/ConsumerMetrics.scala 18 40.7%
skafka/src/main/scala/com/evolutiongaming/skafka/producer/ProducerLogging.scala 15 42.31%
skafka/src/main/scala/com/evolutiongaming/skafka/producer/ProducerMetrics.scala 12 43.48%
modules/metrics_prometheus_v1/src/main/scala/com/evolutiongaming/skafka/producer/ProducerMetricsOf.scala 11 0.0%
modules/metrics/src/main/scala/com/evolutiongaming/skafka/producer/ProducerMetricsOf.scala 11 0.0%
skafka/src/main/scala/com/evolutiongaming/skafka/consumer/ConsumerLogging.scala 6 87.02%
modules/metrics_prometheus_v1/src/main/scala/com/evolutiongaming/skafka/metrics/KafkaMetricsCollector.scala 4 5.71%
skafka/src/main/scala/com/evolutiongaming/skafka/Converters.scala 4 89.23%
skafka/src/main/scala/com/evolutiongaming/skafka/KafkaHealthCheck.scala 4 60.29%

Coverage Stats

Coverage Status
Relevant Lines: 2602
Covered Lines: 2060
Line Coverage: 79.17%
Coverage Strength: 2.0 hits per line

💛 - Coveralls

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@skafka/src/main/scala/com/evolutiongaming/skafka/Converters.scala`:
- Line 19: Update the conversion method that currently loops through elements
with asScala[Try] to convert each element using asScala[F] and combine the
results through the existing ApplicativeThrowable[F] instance, preserving
accumulated errors instead of failing at the first invalid partition. If
accumulation cannot be supported, narrow the method contract to fail-fast
effects; add a regression test using an accumulating F instance.
- Around line 236-246: Fix the Scala 3.3.8 timeout in the rebalance listener’s
consumer.commit path by tracing the failure through
Converters.topicPartitionsSetF and the related commit flow. Ensure Java topic
partitions are converted reliably into the complete Scala Set and that
conversion failures propagate through ApplicativeThrowable without disrupting
the consumer callback; verify ProducerConsumerSpec’s “rebalance listener
correctness - consumer.commit” test passes.
🪄 Autofix

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 783123df-838e-4357-a20d-fbda05a6f53d

📥 Commits

Reviewing files that changed from the base of the PR and between a283b60 and 0198a28.

📒 Files selected for processing (2)
  • skafka/src/main/scala/com/evolutiongaming/skafka/Converters.scala
  • skafka/src/main/scala/com/evolutiongaming/skafka/Partition.scala

Comment thread skafka/src/main/scala/com/evolutiongaming/skafka/Converters.scala
Comment on lines +236 to +246
def topicPartitionsSetF[F[_]: ApplicativeThrowable](setJ: SetJ[TopicPartitionJ]): F[Set[TopicPartition]] =
ApplicativeThrowable[F].catchNonFatal {
val builder = Set.newBuilder[TopicPartition]
setJ.forEach { tpj =>
tpj.asScala[Try] match {
case Failure(exception) => throw exception
case Success(value) => builder.addOne(value)
}
}
builder.result()
}

@coderabbitai coderabbitai Bot Aug 10, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🩺 Stability & Availability | 🟠 Major | ⚡ Quick win

🧩 Analysis chain

🏁 Script executed:

#!/bin/bash
set -euo pipefail
gh run view 31409780871 --job 93524765345 --log-failed

Repository: evolution-gaming/skafka

Length of output: 50379


🏁 Script executed:

set -euo pipefail
log=/tmp/coderabbit-shell-logs/shell-output-7Vo8pu
printf '%s\n' '--- failure and conclusion markers ---'
rg -n -i 'error|failed|failure|aborted|exit code|process completed|all tests passed|success|cancelled|timed out' "$log" | tail -n 120
printf '%s\n' '--- final log lines ---'
tail -n 160 "$log"

Repository: evolution-gaming/skafka

Length of output: 46034


🏁 Script executed:

set -euo pipefail
printf '%s\n' '--- changed files ---'
git diff --stat
printf '%s\n' '--- Converters.scala implementation and callers ---'
rg -n -C 8 'topicPartitionsSetF|SetJ\[TopicPartitionJ\]' skafka/src/main/scala skafka/src/test/scala
printf '%s\n' '--- failing test context ---'
sed -n '120,155p' skafka/src/test/scala/com/evolutiongaming/skafka/ProducerConsumerSpec.scala
sed -n '415,440p' skafka/src/test/scala/com/evolutiongaming/skafka/ProducerConsumerSpec.scala
printf '%s\n' '--- Converters.scala diff ---'
git diff -- skafka/src/main/scala/com/evolutiongaming/skafka/Converters.scala

Repository: evolution-gaming/skafka

Length of output: 41603


🏁 Script executed:

set -euo pipefail
printf '%s\n' '--- repository state ---'
git status --short
git rev-parse --short HEAD
printf '%s\n' '--- ProducerConsumerSpec location ---'
fd -t f 'ProducerConsumerSpec.scala' .
printf '%s\n' '--- test declaration and timeout helper ---'
file=$(fd -t f 'ProducerConsumerSpec.scala' . | head -n 1)
rg -n -C 12 'rebalance listener correctness|consumer\.commit|33 seconds|def timeout|TimeoutException' "$file"
printf '%s\n' '--- commit files for the reviewed path ---'
git diff --name-status
git show --stat --oneline --decorate HEAD

Repository: evolution-gaming/skafka

Length of output: 7120


🏁 Script executed:

set -euo pipefail
git show --format=fuller --no-ext-diff -- skafka/src/main/scala/com/evolutiongaming/skafka/Converters.scala skafka/src/main/scala/com/evolutiongaming/skafka/Partition.scala
printf '%s\n' '--- commit-path call graph ---'
rg -n -C 10 'def commit|commit\(|onPartitionsRevoked|RebalanceListener1WithConsumer|topicPartitionsSetF' \
  skafka/src/main/scala/com/evolutiongaming/skafka/consumer \
  tests/src/test/scala/com/evolutiongaming/skafka/ProducerConsumerSpec.scala

Repository: evolution-gaming/skafka

Length of output: 50380


Resolve the Scala 3.3.8 test failure before merging.

tests/src/test/scala/com/evolutiongaming/skafka/ProducerConsumerSpec.scala fails rebalance listener correctness - consumer.commit with a 33-second TimeoutException.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@skafka/src/main/scala/com/evolutiongaming/skafka/Converters.scala` around
lines 236 - 246, Fix the Scala 3.3.8 timeout in the rebalance listener’s
consumer.commit path by tracing the failure through
Converters.topicPartitionsSetF and the related commit flow. Ensure Java topic
partitions are converted reliably into the complete Scala Set and that
conversion failures propagate through ApplicativeThrowable without disrupting
the consumer callback; verify ProducerConsumerSpec’s “rebalance listener
correctness - consumer.commit” test passes.

Source: MCP tools

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

flaky test, green now

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

@nviliunov-evolution-throwaway, acknowledged. The failing test was flaky and is green now. I will treat the reported timeout as intermittent rather than as confirmed regression from this PR.

You are interacting with an AI system.

@nviliunov-evolution-throwaway

Copy link
Copy Markdown
Contributor Author
Screenshot 2026-08-10 at 19 06 23 Benchmark analysis by AI

@mr-git
mr-git requested a review from Z1kkurat August 10, 2026 17:17

@mr-git mr-git left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM

r <- setJ.asScala.toList.traverse { _.asScala[F] }
} yield r.toSet
}
def topicPartitionsSetF[F[_]: ApplicativeThrowable](setJ: SetJ[TopicPartitionJ]): F[Set[TopicPartition]] =

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I was thinking about the soundness of the caller of this API...

Very possibly this comment should be a dedicated issue! cc: @Z1kkurat

IIUC, we call it after every poll and we pass here full list of assigned topic-partitions. I wonder, if metrics are tracking "sound" data - should we report any metric for topic-partitions, which didn't return any data? Maybe we should convert only the set of topic-partitions, which delivered at least one message in last poll?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I agree, the current approach to labeling metrics is very bizzare

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants