Skip to content

Span Buffer Multiprocess Enhancement with Health Monitoring#6

Open
everettbu wants to merge 1 commit into
span-flusher-stablefrom
span-flusher-multiprocess
Open

Span Buffer Multiprocess Enhancement with Health Monitoring#6
everettbu wants to merge 1 commit into
span-flusher-stablefrom
span-flusher-multiprocess

Conversation

@everettbu

@everettbu everettbu commented Jul 26, 2025

Copy link
Copy Markdown

Test 6

Summary by CodeRabbit

  • New Features
    • Added a configurable option to set the maximum number of flusher processes for span processing, enabling parallel flushing for improved performance.
  • Documentation
    • Updated backend documentation with an additional anti-pattern example for type checking in union types.
  • Bug Fixes
    • Improved backpressure tracking to handle multiple flusher processes independently.
  • Tests
    • Added and updated tests to verify correct behavior and process limits for the new multi-process flusher functionality.

@coderabbitai

coderabbitai Bot commented Jul 26, 2025

Copy link
Copy Markdown

Walkthrough

The changes introduce support for multiple flusher processes in the span consumer, allowing parallel span flushing by distributing shards across processes. This includes updates to the flusher implementation, its factory, configuration options, and related tests. Documentation was also updated to clarify type checking anti-patterns in backend code.

Changes

Cohort / File(s) Change Summary
Documentation Update
CLAUDE.md
Added an anti-pattern and corrected example for type checking union types in backend documentation.
Consumer Configuration
src/sentry/consumers/__init__.py
Added a --flusher-processes CLI option to configure the number of flusher processes for the "process-spans" Kafka consumer.
Flusher Factory
src/sentry/spans/consumers/process/factory.py
Extended ProcessSpansStrategyFactory to accept and propagate a flusher_processes parameter to the flusher.
Span Flusher Parallelization
src/sentry/spans/consumers/process/flusher.py
Refactored SpanFlusher to support multiple processes, distributing shards and tracking health/backpressure per process. Updated process management, submission, and joining logic accordingly.
Span Consumer Tests
tests/sentry/spans/consumers/process/test_consumer.py
Added timing adjustments to existing tests and introduced a new test to verify flusher process limits and shard distribution.
Flusher Backpressure Test
tests/sentry/spans/consumers/process/test_flusher.py
Updated backpressure assertion to check across multiple process states instead of a single value.

Sequence Diagram(s)

sequenceDiagram
    participant CLI
    participant ConsumerFactory
    participant FlusherFactory
    participant SpanFlusher
    participant FlusherProcess1
    participant FlusherProcessN

    CLI->>ConsumerFactory: Start "process-spans" with --flusher-processes=N
    ConsumerFactory->>FlusherFactory: Create with flusher_processes=N
    FlusherFactory->>SpanFlusher: Instantiate with max_processes=N
    SpanFlusher->>FlusherProcess1: Assign subset of shards
    SpanFlusher->>FlusherProcessN: Assign subset of shards
    Note over FlusherProcess1,FlusherProcessN: Each process flushes assigned shards in parallel
    SpanFlusher->>ConsumerFactory: Report health/backpressure per process
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~40 minutes

Poem

A fluffle of processes, working side by side,
Flushing spans in parallel, with sharded pride.
Tests keep us honest, docs keep us wise—
Now flusher’s a team, not a solo surprise!
🐇✨

“Hop, hop, hooray for parallel play!”

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment
  • Commit unit tests in branch span-flusher-multiprocess

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai generate unit tests to generate unit tests for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@everettbu

Copy link
Copy Markdown
Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 28, 2025

Copy link
Copy Markdown
✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@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

🔭 Outside diff range comments (1)
src/sentry/spans/consumers/process/flusher.py (1)

337-348: Fix unused loop variable.

The static analysis correctly identified that process_index is unused in the loop body.

-        for process_index, process in self.processes.items():
+        for _, process in self.processes.items():
🧹 Nitpick comments (2)
src/sentry/spans/consumers/process/flusher.py (2)

31-32: Update docstring to reflect multi-process architecture.

The docstring still mentions "A background multiprocessing manager" (singular) but now creates multiple processes. Update to reflect the new multi-process design.

-    A background multiprocessing manager that polls Redis for new segments to flush and to produce to Kafka.
-    Creates one process per shard for parallel processing.
+    A background multiprocessing manager that polls Redis for new segments to flush and to produce to Kafka.
+    Creates multiple processes to handle shards in parallel, with the number of processes configurable
+    via max_processes parameter.

127-133: Remove unused legacy method.

The _create_process_for_shard method appears to be legacy code from the previous single-shard-per-process design and is no longer used.

Consider removing this unused method to reduce code complexity.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between c2c41b0 and 9bf83b0.

📒 Files selected for processing (6)
  • CLAUDE.md (1 hunks)
  • src/sentry/consumers/__init__.py (1 hunks)
  • src/sentry/spans/consumers/process/factory.py (3 hunks)
  • src/sentry/spans/consumers/process/flusher.py (8 hunks)
  • tests/sentry/spans/consumers/process/test_consumer.py (4 hunks)
  • tests/sentry/spans/consumers/process/test_flusher.py (1 hunks)
🧰 Additional context used
🪛 Ruff (0.12.2)
src/sentry/spans/consumers/process/flusher.py

337-337: Loop control variable process_index not used within loop body

Rename unused process_index to _process_index

(B007)

🔇 Additional comments (13)
tests/sentry/spans/consumers/process/test_flusher.py (1)

83-83: LGTM: Test correctly adapted for multi-process backpressure tracking.

The assertion change properly reflects the new architecture where backpressure is tracked per flusher process instead of globally. Using any() to check if at least one process is experiencing backpressure is the correct approach.

CLAUDE.md (1)

449-458: LGTM: Excellent addition to the anti-patterns guide.

This new example clearly demonstrates the proper way to handle union type checking. Using isinstance() instead of hasattr() is indeed the safer and more explicit approach, as it prevents false positives and makes the code's intent clearer.

src/sentry/spans/consumers/process/factory.py (2)

41-42: LGTM: Clean parameter addition with proper defaults.

The new flusher_processes parameter is well-designed with appropriate type annotation and default value that maintains backward compatibility.


52-52: LGTM: Proper parameter flow implementation.

The parameter is correctly stored as an instance attribute and passed to the SpanFlusher with the appropriate max_processes parameter name. The implementation follows expected patterns.

Also applies to: 74-74

src/sentry/consumers/__init__.py (1)

430-438: LGTM: Well-designed CLI option addition.

The new --flusher-processes option is properly implemented with:

  • Clear naming and parameter mapping
  • Sensible default value of 1
  • Descriptive help text
  • Proper integration with existing multiprocessing options
tests/sentry/spans/consumers/process/test_consumer.py (4)

1-1: LGTM: Appropriate test setup changes.

The time import addition and transaction=True parameter are needed to support the test improvements and proper database transaction handling.

Also applies to: 12-12


60-63: LGTM: Good timing improvements for threaded test reliability.

The additional poll() call and short sleep properly account for the asynchronous nature of the flusher threads, ensuring they have time to process the drift change before the test continues.


84-123: LGTM: Comprehensive test coverage for multi-process functionality.

This new test thoroughly validates the key aspects of the multi-process flusher:

  • Process count limits are properly enforced
  • Shard distribution works correctly across processes
  • Configuration parameters are applied as expected

The test design is solid with clear assertions and realistic scenarios.


82-83: LGTM: Proper function separation formatting.

The additional blank lines follow Python style guidelines for separating function definitions.

src/sentry/spans/consumers/process/flusher.py (4)

59-67: LGTM! Clean shard distribution implementation.

The modulo-based shard distribution ensures even load balancing across processes. This approach effectively distributes shards in a round-robin fashion.


144-146: Well-implemented shard tagging for observability.

The comma-separated shard tag provides good visibility into which shards each process handles, making debugging and monitoring easier.


218-260: Robust multi-process health monitoring implementation.

The health check properly monitors all processes independently, tracks restart counts per process, and provides detailed metrics for each affected shard when issues occur.


277-285: Conservative but safe backpressure handling.

The implementation correctly checks all processes for backpressure and pauses the entire consumer if any process is under pressure. While this is conservative, it ensures data consistency and prevents partial segment writes.

Comment on lines +51 to 52
self.max_processes = max_processes or len(buffer.assigned_shards)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

⚠️ Potential issue

Add validation for max_processes parameter.

The code doesn't validate if max_processes is positive. A zero or negative value would cause issues in the modulo operation for shard distribution.

-        self.max_processes = max_processes or len(buffer.assigned_shards)
+        self.max_processes = max_processes or len(buffer.assigned_shards)
+        if self.max_processes <= 0:
+            raise ValueError(f"max_processes must be positive, got {self.max_processes}")
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
self.max_processes = max_processes or len(buffer.assigned_shards)
self.max_processes = max_processes or len(buffer.assigned_shards)
if self.max_processes <= 0:
raise ValueError(f"max_processes must be positive, got {self.max_processes}")
🤖 Prompt for AI Agents
In src/sentry/spans/consumers/process/flusher.py around lines 51 to 52, the
max_processes parameter is assigned without validation, which can cause errors
if it is zero or negative. Add a check to ensure max_processes is a positive
integer before using it; if it is not positive, raise a ValueError or set it to
a default positive value like the length of buffer.assigned_shards to prevent
issues in the modulo operation.

Comment on lines +51 to +60
self.max_processes = max_processes or len(buffer.assigned_shards)

self.mp_context = mp_context = multiprocessing.get_context("spawn")
self.stopped = mp_context.Value("i", 0)
self.redis_was_full = False
self.current_drift = mp_context.Value("i", 0)
self.backpressure_since = mp_context.Value("i", 0)
self.healthy_since = mp_context.Value("i", 0)
self.process_restarts = 0
self.produce_to_pipe = produce_to_pipe

self._create_process()

def _create_process(self):
# Determine which shards get their own processes vs shared processes
self.num_processes = min(self.max_processes, len(buffer.assigned_shards))

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Verification agent

🧩 Analysis chain

Handle edge case of empty assigned shards.

If buffer.assigned_shards is empty and max_processes is not provided, this would result in 0 processes being created, which could cause issues.


🏁 Script executed:

#!/bin/bash
# Check if there are any safeguards against empty assigned_shards in SpansBuffer initialization
ast-grep --pattern 'class SpansBuffer {
  $$$
  __init__($$$) {
    $$$
  }
  $$$
}'

# Also check for any validation in the buffer implementation
rg -A 5 "assigned_shards" --type py

Length of output: 8173


Ensure SpanFlusher never spins up zero worker processes
The constructor in src/sentry/spans/consumers/process/flusher.py computes:

self.max_processes = max_processes or len(buffer.assigned_shards)
…
self.num_processes = min(self.max_processes, len(buffer.assigned_shards))

If buffer.assigned_shards is empty (and max_processes isn’t set), this yields num_processes == 0, creating an empty process_to_shards_map and no workers—which will break downstream logic.

Proposed fixes (choose one):

  • Guard against empty shards at the top of SpanFlusher.__init__:
    if not buffer.assigned_shards:
        raise ValueError("SpanFlusher requires at least one assigned_shard")
  • Enforce a minimum of one process when computing num_processes:
    - self.num_processes = min(self.max_processes, len(buffer.assigned_shards))
    + self.num_processes = max(1, min(self.max_processes, len(buffer.assigned_shards)))

Either approach will prevent a zero‐process edge case in production.

🤖 Prompt for AI Agents
In src/sentry/spans/consumers/process/flusher.py around lines 51 to 60, the
calculation of self.num_processes can result in zero if buffer.assigned_shards
is empty and max_processes is not set, causing no worker processes to be created
and breaking downstream logic. To fix this, add a guard at the start of the
SpanFlusher.__init__ method that raises a ValueError if buffer.assigned_shards
is empty, ensuring there is at least one assigned shard before proceeding.
Alternatively, enforce a minimum of one process by adjusting the calculation of
self.num_processes to never be less than one.

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.

2 participants