Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(postgres): Add indexer on blocks."time" and optimize roundtable query #2742

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

teddyding
Copy link
Contributor

@teddyding teddyding commented Mar 10, 2025

Changelist

Context

  • Replaced BlockTable.findAll() with new optimized BlockTable.findBlockByCreatedOnOrAfter() function
  • Updated the getNextBlock in AggregateTradingReward class

Test Plan

Unit test

Author/Reviewer Checklist

  • If this PR has changes that result in a different app state given the same prior state and transaction list, manually add the state-breaking label.
  • If the PR has breaking postgres changes to the indexer add the indexer-postgres-breaking label.
  • If this PR isn't state-breaking but has changes that modify behavior in PrepareProposal or ProcessProposal, manually add the label proposal-breaking.
  • If this PR is one of many that implement a specific feature, manually label them all feature:[feature-name].
  • If you wish to for mergify-bot to automatically create a PR to backport your change to a release branch, manually add the label backport/[branch-name].
  • Manually add any of the following labels: refactor, chore, bug.

Summary by CodeRabbit

  • New Features

    • Introduced enhanced block retrieval capabilities leveraging specific queries for block heights and creation times.
    • Streamlined trading rewards processing to improve data handling efficiency.
  • Tests

    • Added new test cases to verify block retrieval functionality across various time-based conditions.
  • Chores

    • Deployed a database migration that creates an optimized index for faster queries on recent block data.
    • Updated reference timestamps in test data for consistency.

@teddyding teddyding requested a review from a team as a code owner March 10, 2025 15:12
Copy link
Contributor

coderabbitai bot commented Mar 10, 2025

Walkthrough

This pull request introduces new query methods in the BlockTable module and updates related tests. In particular, it adds asynchronous functions findByBlockHeights and findBlockByCreatedOnOrAfter while deprecating the use of the createdOnOrAfter parameter in the existing findAll method. The changes include additional test cases for findBlockByCreatedOnOrAfter, updates to block timestamp constants, and a new migration that creates an index for blocks with time since March 2025. The AggregateTradingReward task is also updated to use the new retrieval method.

Changes

File(s) Change Summary
indexer/.../stores/block-table.test.ts
indexer/.../helpers/constants.ts
Added three test cases for findBlockByCreatedOnOrAfter and updated block timestamp values in constants.
indexer/.../stores/block-table.ts Added two new asynchronous methods: findByBlockHeights and findBlockByCreatedOnOrAfter; modified findAll to deprecate the createdOnOrAfter parameter.
indexer/.../aggregate-trading-rewards.ts Updated getNextBlock to call findBlockByCreatedOnOrAfter instead of findAll, adjusting the handling of the returned block.
indexer/.../migrations/migration_files/20250226144411_create_blocks_time_since_march2025_idx.ts Introduced a migration that creates an index on the time column for blocks from March 2025, with corresponding rollback functionality.

Sequence Diagram(s)

sequenceDiagram
    participant AT as AggregateTradingReward
    participant BT as BlockTable
    AT->>BT: Call findBlockByCreatedOnOrAfter(timestamp)
    BT-->>AT: Return block or undefined
    alt Block found
        AT->>AT: Process block (access blockHeight)
    else No block found
        AT->>AT: Log error and throw exception
    end
Loading

Possibly related PRs

Suggested labels

backport/indexer/v7.x

Poem

I'm a bunny in a code-filled glade,
Hopping through queries freshly made.
With indexes built and tests in sight,
I nibble on changes with pure delight.
My whiskers twitch at each new line,
Celebrating code that now does shine!
🐰✨


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 4ca78d7 and 4f6a916.

📒 Files selected for processing (5)
  • indexer/packages/postgres/__tests__/helpers/constants.ts (1 hunks)
  • indexer/packages/postgres/__tests__/stores/block-table.test.ts (2 hunks)
  • indexer/packages/postgres/src/db/migrations/migration_files/20250226144411_create_blocks_time_since_march2025_idx.ts (1 hunks)
  • indexer/packages/postgres/src/stores/block-table.ts (1 hunks)
  • indexer/services/roundtable/src/tasks/aggregate-trading-rewards.ts (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • indexer/services/roundtable/src/tasks/aggregate-trading-rewards.ts
  • indexer/packages/postgres/tests/stores/block-table.test.ts
⏰ Context from checks skipped due to timeout of 90000ms (13)
  • GitHub Check: test / run_command
  • GitHub Check: call-build-ecs-service-vulcan / (vulcan) Check docker image build
  • GitHub Check: call-build-ecs-service-socks / (socks) Check docker image build
  • GitHub Check: call-build-ecs-service-roundtable / (roundtable) Check docker image build
  • GitHub Check: call-build-ecs-service-ender / (ender) Check docker image build
  • GitHub Check: call-build-ecs-service-comlink / (comlink) Check docker image build
  • GitHub Check: check-build-bazooka
  • GitHub Check: check-build-auxo
  • GitHub Check: lint
  • GitHub Check: run_command
  • GitHub Check: Analyze (javascript-typescript)
  • GitHub Check: Analyze (go)
  • GitHub Check: Summary
🔇 Additional comments (8)
indexer/packages/postgres/__tests__/helpers/constants.ts (2)

461-463: Timestamp updated to align with new index

The timestamp for defaultBlock has been updated from 2022 to March 2025 to align with the new index that targets blocks since March 2025. This ensures that test data will properly exercise the new index functionality.


465-467: Timestamp updated to align with new index

Similarly, the timestamp for defaultBlock2 has been updated to March 6, 2025, providing sequential test data that will work with the new index and can be used to test the new query functionality.

indexer/packages/postgres/src/db/migrations/migration_files/20250226144411_create_blocks_time_since_march2025_idx.ts (3)

3-9: Well-designed partial index for performance optimization

This migration creates a partial index on the time column that only includes blocks from March 2025 onwards. The CONCURRENTLY flag is correctly used to prevent table locking during index creation, which is important for production deployments.


11-16: Clean index removal in down migration

The down migration properly drops the index using DROP INDEX CONCURRENTLY to avoid locking the table during removal.


18-20: Appropriate transaction configuration

Setting transaction: false is essential for concurrent index operations, as PostgreSQL doesn't allow CREATE INDEX CONCURRENTLY within a transaction. This is correctly configured.

indexer/packages/postgres/src/stores/block-table.ts (3)

19-35: Good addition of batch block query functionality

The new findByBlockHeights function efficiently retrieves multiple blocks in a single query, properly ordered by block height. This is a useful enhancement for batch operations.


37-56: Efficient implementation of timestamp-based block retrieval

The findBlockByCreatedOnOrAfter function is well-implemented to:

  1. Query blocks with timestamps >= the input timestamp
  2. Order them ascending by time to get the earliest matching block
  3. Limit to 1 result for efficiency
  4. Handle the empty result case appropriately

This function will properly utilize the new partial index when querying for blocks after March 2025.


58-62: Good deprecation notice with migration guidance

The deprecation notice on findAll provides clear guidance to migrate to the more specific methods. This helps maintain code quality while ensuring a smooth transition for existing code.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate unit testing code.
    • @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.

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 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.

@teddyding teddyding changed the title perf(postgres): blocktable.findBlockByCreatedOnOrAfter, and use in `aggregate-tra… perf(postgres): Add indexer on blocks."time" and optimize roundtable query Mar 10, 2025
@teddyding teddyding force-pushed the td/impr-get-blocks branch from 4ca78d7 to 4f6a916 Compare March 10, 2025 15:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

Successfully merging this pull request may close these issues.

1 participant