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

Get funding index maps for vault positions in chunks. #2525

Merged
merged 45 commits into from
Oct 22, 2024

Conversation

vincentwschau
Copy link
Contributor

@vincentwschau vincentwschau commented Oct 21, 2024

Changelist

Chunk the funding index maps to get for each vault subaccount to reduce the amount of rows scanned. Heights to get funding index maps for will be separated into chunks that cover 250k blocks each (number of blocks per chunk can be configured).
Updated funding index map query to limit searches to both a min and max block height.

Additional fixes:

  • updated function to get vault subaccounts to filter out vaults for non-existent clob pairs

Test Plan

Existing unit tests.

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 a new configuration parameter for vault settings, enhancing customization options.
    • Added a method for retrieving funding index maps in a chunked manner to optimize database load.
  • Improvements

    • Enhanced logging capabilities for vault operations, improving error tracking and observability.
    • Implemented performance measurements to monitor the duration of key operations.
    • Refined filtering criteria for funding index updates to ensure more accurate data retrieval.
    • Added a new variable to improve SQL query filtering for funding index updates.

mergify bot and others added 30 commits September 24, 2024 15:03
@vincentwschau vincentwschau requested a review from a team as a code owner October 21, 2024 21:12
Copy link
Contributor

coderabbitai bot commented Oct 21, 2024

Walkthrough

The changes in this pull request involve modifications to several files, primarily focusing on enhancing the functionality and performance of the funding index updates retrieval process. Key updates include the addition of a maximum height variable in the SQL query logic, a new configuration parameter for vault settings, improved logging and performance measurement in the vault controller, and the introduction of methods for efficient data retrieval. These changes collectively refine the data handling and observability of the vault-related operations.

Changes

File Path Change Summary
indexer/packages/postgres/src/stores/funding-index-updates-table.ts Added variable maxheight in findFundingIndexMaps to limit effectiveAtHeight in SQL query.
indexer/services/comlink/src/config.ts Introduced new config parameter VAULT_FETCH_FUNDING_INDEX_BLOCK_WINDOWS with default value 250,000.
indexer/services/comlink/src/controllers/api/v4/vault-controller.ts Added logging and timing for operations, introduced getFundingIndexMapsChunked, and added getHeightWindows.

Possibly related PRs

Suggested labels

backport/indexer/v7.x

Suggested reviewers

  • shrenujb
  • tqin7

Poem

In the fields where numbers play,
A max height leads the way.
Configs grow, and logs will sing,
Fetching funds, a joyful thing!
With each update, we refine,
Hopping forward, all align! 🐇✨


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 72c423a and 60639d9.

📒 Files selected for processing (1)
  • indexer/services/comlink/src/controllers/api/v4/vault-controller.ts (7 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • indexer/services/comlink/src/controllers/api/v4/vault-controller.ts

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 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 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.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 5

🧹 Outside diff range and nitpick comments (2)
indexer/services/comlink/src/config.ts (1)

66-66: LGTM. Consider adding a comment for clarity.

The addition of VAULT_FETCH_FUNDING_INDEX_BLOCK_WINDOWS aligns with the PR objective of chunking data retrieval for vault positions. The implementation is consistent with the existing code style.

Consider adding a brief comment explaining the purpose and impact of this configuration parameter. For example:

// Defines the block window size for chunked retrieval of funding index data for vaults
VAULT_FETCH_FUNDING_INDEX_BLOCK_WINDOWS: parseInteger({default: 250_000 }),
indexer/packages/postgres/src/stores/funding-index-updates-table.ts (1)

245-245: LGTM! Consider a minor optimization.

The introduction of maxheight and its use in the SQL query is a good optimization that aligns with the PR's objective of chunking data retrieval. This should improve query performance by limiting the range of data fetched.

Consider using Math.max() for clarity:

const maxheight: number = Math.max(...heightNumbers);

This change would make the intent more explicit and potentially more performant for larger arrays.

Also applies to: 259-259

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 0eff57c and afeb453.

📒 Files selected for processing (3)
  • indexer/packages/postgres/src/stores/funding-index-updates-table.ts (2 hunks)
  • indexer/services/comlink/src/config.ts (1 hunks)
  • indexer/services/comlink/src/controllers/api/v4/vault-controller.ts (7 hunks)
🧰 Additional context used
🔇 Additional comments (2)
indexer/packages/postgres/src/stores/funding-index-updates-table.ts (1)

Line range hint 1-300: Overall assessment: Changes are well-implemented and align with PR objectives.

The modifications to the findFundingIndexMaps function effectively implement the chunking strategy for funding index map retrieval. The changes are focused and don't introduce any apparent risks to other parts of the codebase. This implementation should contribute to reducing the number of rows scanned during queries, as intended in the PR objectives.

To ensure there are no unintended side effects, please run the existing unit tests for this file and confirm that all tests pass.

indexer/services/comlink/src/controllers/api/v4/vault-controller.ts (1)

1-1: Approved: Correctly importing logger and stats

The import statement correctly brings in logger and stats from @dydxprotocol-indexer/base, which are used for logging and performance measurement.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between afeb453 and 41fe266.

📒 Files selected for processing (2)
  • indexer/services/comlink/src/config.ts (1 hunks)
  • indexer/services/comlink/src/controllers/api/v4/vault-controller.ts (7 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • indexer/services/comlink/src/config.ts
🧰 Additional context used
🔇 Additional comments (7)
indexer/services/comlink/src/controllers/api/v4/vault-controller.ts (7)

1-1: New import added for logging.

The logger import from '@dydxprotocol-indexer/base' has been added. This is a good practice for improving the observability of the application.


74-79: Performance timing added for fetching vaults.

The addition of stats.timing is a good practice for monitoring the performance of the getVaultMapping function. This will help in identifying potential bottlenecks.


Line range hint 81-107: Performance timing added for fetching ticks, positions, and equity.

Similar to the previous comment, performance timing has been added for fetching ticks, positions, and equity. This is a good practice for monitoring the performance of these operations.


Line range hint 338-392: Performance timing added for fetching subaccount positions.

Performance timing has been added for fetching subaccount positions. This is consistent with the previous additions and will help in monitoring the performance of this operation.


Line range hint 394-413: New function call and performance timing for funding index maps.

A new function getFundingIndexMapsChunked has been introduced to fetch funding index maps. Performance timing has also been added for this operation. This change appears to be aimed at improving efficiency in retrieving funding index maps.


579-603: New function getFundingIndexMapsChunked introduced.

This function retrieves funding index maps in chunks to reduce database load. It uses the getHeightWindows function to process updated heights in batches. This is a good optimization for handling large datasets.


605-631: New helper function getHeightWindows introduced.

This function separates an array of heights into chunks based on a window size. It's used by getFundingIndexMapsChunked to optimize database queries. The implementation looks correct and efficient.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 41fe266 and c3cd754.

📒 Files selected for processing (1)
  • indexer/services/comlink/src/controllers/api/v4/vault-controller.ts (7 hunks)
🧰 Additional context used
🔇 Additional comments (4)
indexer/services/comlink/src/controllers/api/v4/vault-controller.ts (4)

74-79: Ensure stats.timing is called even if an error occurs

If getVaultMapping() throws an error, the stats.timing call will be skipped, and the timing metric won't be recorded. Wrap the code in a try...finally block to ensure that timing is always recorded.


104-107: Ensure timing metrics are recorded on errors

The stats.timing call will be skipped if an error occurs before it's reached. Enclose the preceding code in a try...finally block to guarantee timing metrics are captured even when exceptions are thrown.


389-392: Record timing metrics even if fetching positions fails

If an error occurs during the fetching of positions, the stats.timing call won't execute. To ensure timing is always recorded, use a try...finally block around the fetching logic.


409-413: Ensure funding fetch timing is recorded on errors

The timing metric for fetching funding will not be recorded if an error occurs before stats.timing is called. Wrap the fetching code in a try...finally block to ensure the timing is always captured.

@vincentwschau vincentwschau merged commit 0a28594 into main Oct 22, 2024
16 checks passed
@vincentwschau vincentwschau deleted the vincentc/chunk-vault-equity branch October 22, 2024 16:17
@vincentwschau
Copy link
Contributor Author

vincentwschau commented Oct 22, 2024

@Mergifyio backport release/indexer/v7.x

Copy link
Contributor

mergify bot commented Oct 22, 2024

backport release/indexer/v7.x

✅ Backports have been created

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.

2 participants