Skip to content

Added 2 attributes in partialFilterExpression to optimize IndexScan - #370

Closed
RaghuveeRR07 wants to merge 2 commits into
FailproofAI:mainfrom
RaghuveeRR07:OptimizeIndex
Closed

Added 2 attributes in partialFilterExpression to optimize IndexScan#370
RaghuveeRR07 wants to merge 2 commits into
FailproofAI:mainfrom
RaghuveeRR07:OptimizeIndex

Conversation

@RaghuveeRR07

Copy link
Copy Markdown

Index now applies only to documents where:

  • fanout_id exists
  • status is in {CREATED, QUEUED, EXECUTED}

@coderabbitai

coderabbitai Bot commented Sep 6, 2025

Copy link
Copy Markdown
Contributor

Note

Other AI code review bot(s) detected

CodeRabbit has detected other AI code review bot(s) in this pull request and will avoid duplicating their findings in the review comments. This may lead to a less comprehensive review.

📝 Walkthrough

Summary by CodeRabbit

  • Bug Fixes

    • Reduced intermittent duplicate/conflict errors during task retries and fanout scenarios.
  • Chores

    • Optimized database indexing for active states to improve responsiveness in queue and execution-related operations.
    • Removed an unused index to streamline storage and maintenance.

Walkthrough

Changed MongoDB indexes in state-manager/app/models/db/state.py: removed the run_id_status_index and converted uniq_fanout_retry into a partial unique index that applies only when fanout_id exists and status is one of CREATED, QUEUED, or EXECUTED. No other logic changed.

Changes

Cohort / File(s) Summary
MongoDB index update
state-manager/app/models/db/state.py
Removed run_id_status_index. Modified uniq_fanout_retry IndexModel to add partialFilterExpression requiring fanout_id to exist and status ∈ [CREATED, QUEUED, EXECUTED]; fields and uniqueness unchanged.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Possibly related issues

Poem

I twitch my whiskers, hop with glee,
One index pruned, one set free.
Fanout only when it's found,
Status queued where answers sound.
My burrow hums — tidy and spry. 🐇

✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

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

@gemini-code-assist gemini-code-assist Bot 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.

Summary of Changes

Hello @RaghuveeRR07, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request optimizes database indexing by modifying an existing index and removing another. The "uniq_fanout_retry" index is now more efficient by applying only to a specific subset of documents, which should improve query performance for relevant operations. The removal of "run_id_status_index" further streamlines the database schema.

Highlights

  • Index Modification: The "uniq_fanout_retry" index in "state.py" has been updated to include a "partialFilterExpression".
  • Index Filtering Conditions: This partial filter ensures the index only applies to documents where "fanout_id" exists and "status" is one of "CREATED", "QUEUED", or "EXECUTED", optimizing its use.
  • Index Removal: The "run_id_status_index" has been removed, streamlining the index definitions.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot 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.

Code Review

This pull request optimizes a MongoDB index by adding a partialFilterExpression, which is a good approach to reduce index size. However, the change also removes the run_id_status_index, which raises a performance concern for queries that might rely on it. My review includes feedback on this potential issue, along with suggestions to improve code style and maintainability by removing unnecessary whitespace and replacing hardcoded strings with enum members.

Comment on lines +97 to 103
"fanout_id": { "$exists": True },
"status": { "$in": ["CREATED", "QUEUED", "EXECUTED"] }
}
)

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.

high

Removing the run_id_status_index could cause significant performance degradation for queries that filter by run_id and status. These queries would no longer be indexed and might result in slow collection scans. If this index is still in use, it should be retained or replaced with a suitable alternative.

import time
import uuid

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.

medium

This line contains unnecessary whitespace. It should be a blank line without any indentation to conform with PEP 8 style guidelines.1

Style Guide References

Footnotes

  1. PEP 8 advises against extraneous whitespace, including on blank lines. It also recommends blank lines to separate logical sections, like imports from class definitions.

Comment thread state-manager/app/models/db/state.py Outdated
name="uniq_fanout_retry",
partialFilterExpression={
"fanout_id": { "$exists": True },
"status": { "$in": ["CREATED", "QUEUED", "EXECUTED"] }

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.

medium

To improve maintainability and prevent potential bugs, it's better to use the StateStatusEnum members directly instead of hardcoding string values for the status. This ensures that if the enum values change, this index definition will be updated automatically.

Suggested change
"status": { "$in": ["CREATED", "QUEUED", "EXECUTED"] }
"status": { "$in": [StateStatusEnum.CREATED, StateStatusEnum.QUEUED, StateStatusEnum.EXECUTED] }

@coderabbitai coderabbitai Bot 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.

Actionable comments posted: 4

♻️ Duplicate comments (2)
state-manager/app/models/db/state.py (2)

12-12: Trim stray indentation on blank line.

Blank line has spaces; drop them to satisfy linters.


95-99: Re-check impact of removing run_id_status_index on query patterns.

If you have hot paths filtering by run_id and status, removing that index can regress latency. Consider retaining a supporting index (possibly partial on active statuses).

To assess usage across the repo:

#!/bin/bash
# Find code filtering by both run_id and status (string and ODM forms)
rg -nP -C2 '(run_id).{0,120}(status)|(status).{0,120}(run_id)'
rg -nP -C2 'State\.(run_id|status)\b'

If these are common and latency-sensitive, add:

IndexModel([("run_id", 1), ("status", 1)], name="run_id_status_index_active",
           partialFilterExpression={"status": {"$in": ["CREATED","QUEUED","EXECUTED"]}})
📜 Review details

Configuration used: CodeRabbit UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 193a04a and 78b51cf.

📒 Files selected for processing (1)
  • state-manager/app/models/db/state.py (2 hunks)

Comment on lines +95 to +99
name="uniq_fanout_retry",
partialFilterExpression={
"fanout_id": { "$exists": True },
"status": { "$in": [StateStatusEnum.CREATED, StateStatusEnum.QUEUED, StateStatusEnum.EXECUTED] }
}

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.

🧹 Nitpick (assertive)

⚠️ Potential issue

Changing a unique index to a partial unique index requires a migration (IndexOptionsConflict otherwise).

MongoDB won’t mutate index options in-place. Reusing the old name “uniq_fanout_retry” will fail until the old index is dropped. Prefer a two-step migration or a temporary new name.

Option A (rename, then remove old):

-                name="uniq_fanout_retry",
+                name="uniq_fanout_retry_active",

Then, in a migration: create the new index, validate, drop the old one.

Sample mongosh steps to run in prod/Stage:

// 1) Create new partial unique index
db.state.createIndex(
  { node_name:1, namespace_name:1, graph_name:1, identifier:1, run_id:1, retry_count:1, fanout_id:1 },
  {
    name: "uniq_fanout_retry_active",
    unique: true,
    partialFilterExpression: {
      fanout_id: { $exists: true },
      status: { $in: ["CREATED","QUEUED","EXECUTED"] }
    }
  }
)
// 2) Verify no dupes and query plans look good
// 3) Drop old index
db.state.dropIndex("uniq_fanout_retry")

If you prefer to keep the original name, drop the old index first during maintenance, then recreate with the new options.


🧹 Nitpick (assertive)

Guard for duplicate-key on status transitions into the partial set.

Duplicates outside the partial set may surface as E11000 when a doc’s status changes into {CREATED, QUEUED, EXECUTED}. Ensure writers handle this with idempotent upserts/retries.

If needed, I can draft a small retry utility that treats E11000 as a signal to re-read the canonical doc.

Comment on lines +96 to +99
partialFilterExpression={
"fanout_id": { "$exists": True },
"status": { "$in": [StateStatusEnum.CREATED, StateStatusEnum.QUEUED, StateStatusEnum.EXECUTED] }
}

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.

🧹 Nitpick (assertive)

Confirm need for fanout_id: {$exists: true}.

Model sets fanout_id with a default UUID (Line 29), so “exists” may be redundant unless legacy docs lack it. If all docs have fanout_id, removing this predicate will enlarge index coverage and simplify the filter.

🤖 Prompt for AI Agents
In state-manager/app/models/db/state.py around lines 96 to 99, the
partialFilterExpression currently requires "fanout_id": { "$exists": True }
which may be redundant because fanout_id is set with a default UUID at model
definition (line 29); verify whether any legacy documents exist without
fanout_id (query the collection for documents where fanout_id is null/absent),
and if none are found remove the "$exists": True clause from the
partialFilterExpression so the index covers more documents; after changing,
rebuild the index and run relevant unit/integration tests or migration scripts
to ensure no regression.

💡 Verification agent

🧩 Analysis chain

Ensure Enum values are BSON-serializable strings (or use .value explicitly).

If StateStatusEnum isn’t a str Enum, PyMongo will reject Enum members in index options. Using .value is safe and explicit.

Apply:

-                partialFilterExpression={
-                    "fanout_id": { "$exists": True },
-                    "status": { "$in": [StateStatusEnum.CREATED, StateStatusEnum.QUEUED, StateStatusEnum.EXECUTED] }
-                }
+                partialFilterExpression={
+                    "fanout_id": {"$exists": True},
+                    "status": {"$in": [StateStatusEnum.CREATED.value, StateStatusEnum.QUEUED.value, StateStatusEnum.EXECUTED.value]},
+                }

Verification script to confirm the enum type:


🏁 Script executed:

#!/bin/bash
# Expect: StateStatusEnum subclasses `str, Enum` or similar.
rg -nP -C2 'class\s+StateStatusEnum\s*\('

Length of output: 334


StateStatusEnum subclasses str, so its members are BSON-serializable; using .value is purely optional for clarity.

🤖 Prompt for AI Agents
In state-manager/app/models/db/state.py around lines 96 to 99, the
partialFilterExpression currently uses StateStatusEnum members with .value;
since StateStatusEnum subclasses str and its members are BSON-serializable,
remove the unnecessary .value calls and use the enum members directly (e.g.,
StateStatusEnum.CREATED) for clarity and consistency, ensuring the resulting
partialFilterExpression still contains the intended string values.

@codecov

codecov Bot commented Sep 7, 2025

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

name="run_id_status_index"
name="uniq_fanout_retry",
partialFilterExpression={
"fanout_id": { "$exists": True },

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@RaghuveeRR07 why this? if you see the model fanout_id always exists

name="uniq_fanout_retry",
partialFilterExpression={
"fanout_id": { "$exists": True },
"status": { "$in": [StateStatusEnum.CREATED, StateStatusEnum.QUEUED, StateStatusEnum.EXECUTED] }

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

why only these CREATED, QUEUED, EXECUTED status?

@NiveditJain

Copy link
Copy Markdown
Member

Hey @RaghuveeRR07 wanted to check if you got a chance to improve this implementation as per our last discussion?

@NiveditJain

NiveditJain commented Sep 29, 2025

Copy link
Copy Markdown
Member

Closing this PR for now. @RaghuveeRR07 please re-open this as you complete fix for this.

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