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

Optimize pnl queries #1812

Merged
merged 5 commits into from
Jun 28, 2024
Merged

Optimize pnl queries #1812

merged 5 commits into from
Jun 28, 2024

Conversation

dydxwill
Copy link
Contributor

@dydxwill dydxwill commented Jun 28, 2024

Changelist

Add auxo option to only run db migration. Plan is to manually deploy roundtable first to make sure it's not the culprit.
Add blockTime index to pnl_ticks table. Without index, EXPLAIN ANALYZE WITH maxBlockTime AS ( SELECT MAX("blockTime") as "maxBlockTime" FROM "pnl_ticks" ) SELECT maxBlockTime."maxBlockTime" as max, COUNT(*) as count FROM "pnl_ticks", maxBlockTime WHERE "pnl_ticks"."blockTime" = maxBlockTime."maxBlockTime" GROUP BY 1; has Execution Time: 14363.724 ms. With index, Execution Time: 0.119 ms

In pnl instrumentation task, query the cache since data exists there. This will help reduce db load. Remove unused helper functions

Test Plan

See internal mainnet dashboards for decrease in pnl instrumentation task latency
Unit tested

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

    • Added index creation for blockTime column in the pnl_ticks table for improved query performance.
    • Updated PNL ticks instrumentation to use Redis for faster access and calculations.
  • Bug Fixes

    • Corrected the expected count value in PNL ticks table test for accurate test results.
  • Improvements

    • Enhanced query in findLatestProcessedBlocktimeAndCount for more efficient data retrieval.
    • Added conditional early return in ECS Task Definition creation to handle specific event flags.

Copy link
Contributor

coderabbitai bot commented Jun 28, 2024

Walkthrough

The recent updates involve refining test cases, database migration scripts, and Pnl ticks handling in various modules of the indexer packages. Tests were adjusted, new migration scripts were added to manage a blockTime index, and functionalities were updated to optimize querying and data retrieval processes. Additionally, a new conditional check was introduced in the AUXO service to handle specific migration tasks without creating unnecessary ECS Task Definitions.

Changes

File Path Change Summary
indexer/packages/postgres/__tests__/stores/pnl-ticks-table.test.ts Updated test case to expect a count value of 1 instead of 2.
.../src/db/migrations/migration_files/20240628141851_create_pnl_ticks_block_time_index.ts Added migration script to create/drop an index on the blockTime column in pnl_ticks table.
indexer/packages/postgres/src/stores/pnl-ticks-table.ts Updated findLatestProcessedBlocktimeAndCount function to use CTE for querying max block time and count.
indexer/services/roundtable/__tests__/tasks/pnl-instrumentation.test.ts Switched to using getMostRecentPnlTicksForEachAccount from helpers and updated related mock implementations.
indexer/services/roundtable/src/tasks/pnl-instrumentation.ts Modified logic to retrieve recent PNL ticks from Redis instead of database, updating handling of PNL tick times.
indexer/services/auxo/src/index.ts Added conditional early return based on event.onlyRunDbMigrationAndCreateKafkaTopics to avoid creating ECS Task Definitions unnecessarily.
indexer/services/auxo/src/types.ts Introduced a new field onlyRunDbMigrationAndCreateKafkaTopics to AuxoEventJson interface.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant AuxoService
    participant Redis
    participant PnlTicksTable

    Client->>AuxoService: event with onlyRunDbMigrationAndCreateKafkaTopics
    AuxoService-->>Client: Return success early
    
    Client->>AuxoService: request PNL ticks for subaccounts
    AuxoService->>Redis: getMostRecentPnlTicksForEachAccount
    Redis-->>AuxoService: return recent PNL ticks
    AuxoService->>PnlTicksTable: findLatestProcessedBlocktimeAndCount
    PnlTicksTable-->>AuxoService: return maxBlockTime and count
    AuxoService-->>Client: Return PNL ticks and metadata
Loading

Poem

In databases deep, where timestamps align,
Optimized queries find block times that shine.
With redis and tests, each tick now improved,
Pnl instruments, precision behooved.
New fields emerge, Kubernetes stands tall,
So here’s to the code, the best of them all! 🐇✨


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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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: 3

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 8487f36 and 22b1011.

Files selected for processing (5)
  • indexer/packages/postgres/tests/stores/pnl-ticks-table.test.ts (1 hunks)
  • indexer/packages/postgres/src/db/migrations/migration_files/20240628141851_create_pnl_ticks_block_time_index.ts (1 hunks)
  • indexer/packages/postgres/src/stores/pnl-ticks-table.ts (1 hunks)
  • indexer/services/roundtable/tests/tasks/pnl-instrumentation.test.ts (4 hunks)
  • indexer/services/roundtable/src/tasks/pnl-instrumentation.ts (2 hunks)
Files skipped from review due to trivial changes (1)
  • indexer/packages/postgres/tests/stores/pnl-ticks-table.test.ts
Additional comments not posted (6)
indexer/packages/postgres/src/db/migrations/migration_files/20240628141851_create_pnl_ticks_block_time_index.ts (2)

9-13: Ensure the use of CONCURRENTLY in index dropping.

Using CONCURRENTLY with DROP INDEX is correct and can help avoid locking issues during the migration. Good use of PostgreSQL features to ensure smoother operations.


15-17: Review transaction configuration.

The migration script sets transaction: false. Ensure this is intentional, as running index operations outside of a transaction can be risky if not handled properly.

indexer/services/roundtable/src/tasks/pnl-instrumentation.ts (1)

14-14: Ensure proper usage of helper functions.

The import of getMostRecentPnlTicksForEachAccount suggests a change in how PNL data is retrieved. Ensure this change is reflected throughout the file and aligns with the project's data handling strategies.

indexer/services/roundtable/__tests__/tasks/pnl-instrumentation.test.ts (2)

8-8: Review updated imports and mock setup.

The test file has updated imports and a new mock setup for getMostRecentPnlTicksForEachAccount. Ensure that these changes are necessary and correctly implemented to reflect the updated logic in the main task file.

Also applies to: 11-11, 14-14, 16-16


50-61: Ensure test coverage for new logic.

The mock implementations for getMostRecentPnlTicksForEachAccount in various scenarios need to accurately reflect the changes in data handling. Ensure that the test cases cover all new edge cases and behaviors introduced by the updates.

Also applies to: 81-92, 117-124

indexer/packages/postgres/src/stores/pnl-ticks-table.ts (1)

223-236: Review the refactored query using CTE.

The refactored function findLatestProcessedBlocktimeAndCount uses a CTE to optimize the query. Ensure that the CTE is correctly formatted and that the query efficiently retrieves the required data.

Comment on lines +3 to +7
export async function up(knex: Knex): Promise<void> {
await knex.raw(`
CREATE INDEX CONCURRENTLY IF NOT EXISTS "pnl_ticks_blocktime_index" ON "pnl_ticks" ("blockTime");
`);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure the use of CONCURRENTLY in index creation.

The use of CONCURRENTLY with IF NOT EXISTS in the CREATE INDEX statement is generally not supported by PostgreSQL. You might need to remove CONCURRENTLY if this causes issues during migration.

-    CREATE INDEX CONCURRENTLY IF NOT EXISTS "pnl_ticks_blocktime_index" ON "pnl_ticks" ("blockTime");
+    CREATE INDEX IF NOT EXISTS "pnl_ticks_blocktime_index" ON "pnl_ticks" ("blockTime");
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
export async function up(knex: Knex): Promise<void> {
await knex.raw(`
CREATE INDEX CONCURRENTLY IF NOT EXISTS "pnl_ticks_blocktime_index" ON "pnl_ticks" ("blockTime");
`);
}
export async function up(knex: Knex): Promise<void> {
await knex.raw(`
CREATE INDEX IF NOT EXISTS "pnl_ticks_blocktime_index" ON "pnl_ticks" ("blockTime");
`);
}

Comment on lines +39 to +46
// Get the most recent PNL ticks for each subaccount from Redis
const mostRecentPnlTicks: PnlTickForSubaccounts = await getMostRecentPnlTicksForEachAccount();
const mostRecentPnlTickTimes:
{
[subaccountId: string]: string
} = await PnlTicksTable.findMostRecentPnlTickTimeForEachAccount(
'1',
} = _.mapValues(
mostRecentPnlTicks,
(pnlTick: PnlTicksCreateObject) => pnlTick.blockTime,
Copy link
Contributor

Choose a reason for hiding this comment

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

Optimize data retrieval and processing.

The retrieval and processing of PNL data from Redis using getMostRecentPnlTicksForEachAccount and mapping over the results with lodash could be optimized or simplified if performance issues are noted.

-  const mostRecentPnlTicks: PnlTickForSubaccounts = await getMostRecentPnlTicksForEachAccount();
-  const mostRecentPnlTickTimes:
-  {
-    [subaccountId: string]: string
-  } = _.mapValues(
-    mostRecentPnlTicks,
-    (pnlTick: PnlTicksCreateObject) => pnlTick.blockTime,
-  );
+  const mostRecentPnlTickTimes = await getMostRecentPnlTicksForEachAccount();
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
// Get the most recent PNL ticks for each subaccount from Redis
const mostRecentPnlTicks: PnlTickForSubaccounts = await getMostRecentPnlTicksForEachAccount();
const mostRecentPnlTickTimes:
{
[subaccountId: string]: string
} = await PnlTicksTable.findMostRecentPnlTickTimeForEachAccount(
'1',
} = _.mapValues(
mostRecentPnlTicks,
(pnlTick: PnlTicksCreateObject) => pnlTick.blockTime,
// Get the most recent PNL ticks for each subaccount from Redis
const mostRecentPnlTickTimes = await getMostRecentPnlTicksForEachAccount();

@@ -1,15 +1,17 @@
import { logger, stats } from '@dydxprotocol-indexer/base';
import {
BlockFromDatabase,
BlockTable,
PnlTicksTable,
BlockTable, PnlTicksCreateObject,
Copy link
Contributor

Choose a reason for hiding this comment

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

Tip

Codebase Verification

Review updated imports for consistency and necessity.

The imports for PnlTicksCreateObject and PnlTickForSubaccounts are present in the file but are not used. Please review and remove any unused imports to maintain code cleanliness.

  • PnlTicksCreateObject imported at line 4.
  • PnlTickForSubaccounts imported at line 9.
Analysis chain

Review updated imports for consistency and necessity.

The imports for PnlTicksCreateObject and PnlTickForSubaccounts have been added. Verify that these are used appropriately in the file and check for any unused imports.

Also applies to: 9-9

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the usage of newly imported modules.

# Test: Search for usage of `PnlTicksCreateObject` and `PnlTickForSubaccounts` in the file.
ast-grep --lang javascript --pattern $'import { $$$ } from "@dydxprotocol-indexer/postgres"; $$$ PnlTicksCreateObject $$$'
ast-grep --lang javascript --pattern $'import { $$$ } from "@dydxprotocol-indexer/redis"; $$$ PnlTickForSubaccounts $$$'

Length of output: 1253



Script:

#!/bin/bash
# Description: Verify the usage of newly imported modules.

# Test: Search for the import statements of `PnlTicksCreateObject` and `PnlTickForSubaccounts`.
ast-grep --lang typescript --pattern 'import { PnlTicksCreateObject } from "@dydxprotocol-indexer/postgres"' indexer/services/roundtable/src/tasks/pnl-instrumentation.ts
ast-grep --lang typescript --pattern 'import { PnlTickForSubaccounts } from "@dydxprotocol-indexer/redis"' indexer/services/roundtable/src/tasks/pnl-instrumentation.ts

# Test: Search for the usage of `PnlTicksCreateObject` and `PnlTickForSubaccounts` in the file.
ast-grep --lang typescript --pattern 'PnlTicksCreateObject' indexer/services/roundtable/src/tasks/pnl-instrumentation.ts
ast-grep --lang typescript --pattern 'PnlTickForSubaccounts' indexer/services/roundtable/src/tasks/pnl-instrumentation.ts

Length of output: 815



Script:

#!/bin/bash
# Description: Verify the usage of newly imported modules within the file.

# Test: Search for the usage of `PnlTicksCreateObject` in the file.
ast-grep --lang typescript --pattern 'PnlTicksCreateObject' indexer/services/roundtable/src/tasks/pnl-instrumentation.ts

# Test: Search for the usage of `PnlTickForSubaccounts` in the file.
ast-grep --lang typescript --pattern 'PnlTickForSubaccounts' indexer/services/roundtable/src/tasks/pnl-instrumentation.ts

Length of output: 473

@dydxwill dydxwill changed the title Optimize queries Optimize pnl queries Jun 28, 2024
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: 0

Outside diff range and nitpick comments (1)
indexer/services/auxo/src/index.ts (1)

Line range hint 217-468: Optimization suggestion: Remove unnecessary else clauses.

The static analysis has identified unnecessary else clauses which can be safely removed to improve code readability.

- else {
-   throw new Error('No services found');
- }
+ throw new Error('No services found');
- else {
-   throw new Error('Not all services found');
- }
+ throw new Error('Not all services found');

Aside from this, the functions related to ECS task definition creation and service upgrades are well-implemented.

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 22b1011 and c1c65d4.

Files selected for processing (4)
  • indexer/packages/postgres/tests/stores/pnl-ticks-table.test.ts (2 hunks)
  • indexer/packages/postgres/src/stores/pnl-ticks-table.ts (2 hunks)
  • indexer/services/auxo/src/index.ts (1 hunks)
  • indexer/services/auxo/src/types.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • indexer/services/auxo/src/types.ts
Files skipped from review as they are similar to previous changes (2)
  • indexer/packages/postgres/tests/stores/pnl-ticks-table.test.ts
  • indexer/packages/postgres/src/stores/pnl-ticks-table.ts
Additional context used
Biome
indexer/services/auxo/src/index.ts

[error] 156-158: This else clause can be omitted because previous branches break early.

Unsafe fix: Omit the else clause.

(lint/style/noUselessElse)


[error] 459-468: This else clause can be omitted because previous branches break early.

Unsafe fix: Omit the else clause.

(lint/style/noUselessElse)

Additional comments not posted (4)
indexer/services/auxo/src/index.ts (4)

74-81: Early return implementation is correct and clear.

This change aligns with the PR objectives to optimize operations by conditionally running tasks based on the event's properties.


Line range hint 86-149: Function upgradeBazooka is well-implemented with appropriate logging and error handling.

The function correctly updates the Lambda function and waits for the update status, providing detailed logging throughout the process.


Line range hint 151-193: Function getImageDetail correctly handles image retrieval with comprehensive logging and error management.

The implementation ensures that if no image details are found, an error is thrown, which is crucial for preventing further erroneous operations.


Line range hint 195-215: Function runDbAndKafkaMigration is implemented correctly with detailed logging and appropriate Lambda invocation.

The use of the RequestResponse invocation type ensures that the function waits for the Lambda execution to complete, which is necessary for synchronous operations.

@dydxwill dydxwill merged commit 3c07400 into main Jun 28, 2024
11 checks passed
@dydxwill dydxwill deleted the wl/release_dbg branch June 28, 2024 22:54
@dydxwill
Copy link
Contributor Author

@Mergifyio backport release/indexer/v5.x

Copy link
Contributor

mergify bot commented Jun 28, 2024

backport release/indexer/v5.x

✅ Backports have been created

mergify bot pushed a commit that referenced this pull request Jun 28, 2024
(cherry picked from commit 3c07400)

# Conflicts:
#	indexer/services/auxo/src/types.ts
@mergify mergify bot mentioned this pull request Jun 28, 2024
6 tasks
dydxwill added a commit that referenced this pull request Jun 28, 2024
Co-authored-by: dydxwill <[email protected]>
Co-authored-by: Will Liu <[email protected]>
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