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

Refactor mosaic tile fetching and caching logic; improve CPU core det… #85

Merged
merged 5 commits into from
Dec 6, 2024

Conversation

dqunbp
Copy link
Collaborator

@dqunbp dqunbp commented Dec 6, 2024

…ection

https://kontur.fibery.io/Tasks/Task/OpenAerialMap-mosaic-prod-performance-issues-20341

Summary by CodeRabbit

  • New Features

    • Introduced new functions for caching mosaic tiles (cachedMosaic256px and cachedMosaic512px).
    • Enhanced tile fetching logic based on zoom levels, including handling of empty tiles.
    • Added tests for mosaic tile generation, ensuring correct behavior with various scenarios.
  • Improvements

    • Streamlined tile fetching request prioritization for better performance.
    • Improved error handling and logging for tile fetching processes.
    • Enhanced caching functionality with stricter image extension enforcement.
  • Refactor

    • Restructured tile management and caching logic for clarity and efficiency.

Copy link
Contributor

coderabbitai bot commented Dec 6, 2024

Important

Review skipped

Review was skipped due to path filters

⛔ Files ignored due to path filters (1)
  • package-lock.json is excluded by !**/package-lock.json

CodeRabbit blocks several paths by default. You can override this behavior by explicitly including those paths in the path filters. For example, including **/dist/** will override the default block on the dist directory, by removing the pattern from both the lists.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Walkthrough

The changes involve significant updates to the src/mosaic.mjs, src/titiler_fetcher.mjs, and test files for mosaic functionality. In mosaic.mjs, the tile fetching and caching mechanisms have been restructured, introducing new functions for caching mosaic tiles and refining logic for fetching based on zoom levels. The titiler_fetcher.mjs file now determines CPU core count from the environment and simplifies the priority logic for tile fetching requests. Additionally, new tests have been added to ensure the functionality works as intended.

Changes

File Change Summary
src/mosaic.mjs - Removed cacheDelete and cachePurgeMosaic imports.
- Updated cacheGetTile and cachePutTile to restrict extensions to "png" and "jpg".
- Enhanced source function for zoom level handling and tile intersection logic.
- Introduced cachedMosaic256px and cachedMosaic512px functions for caching.
- Updated mosaic256px and mosaic512px functions with detailed comments.
- Improved tile sorting logic based on metadata.
src/titiler_fetcher.mjs - Adjusted CPU core count determination based on NUM_CPUS environment variable.
- Simplified priority calculation in enqueueTileFetching from Math.pow(2, z) to direct assignment of z.
- Standardized error logging format.
tests/mosaic.mjs - Activated previously skipped test case: mosaic(14, 9485, 5610) and 2 parent tiles.
tests/mosaic512px.mjs - Introduced a new test suite for mosaic tile generation, including a mock database and cache system.
- Added tests for single image handling, multiple image sorting, scaled tile generation, and parent tile logic.
src/mosaic_cache_invalidation_job.mjs - Added console log statement in invalidateMosaicCache to output cacheInfo for debugging purposes.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant Mosaic
    participant Fetcher

    User->>Mosaic: Request tile
    Mosaic->>Mosaic: Check zoom level
    Mosaic->>Mosaic: Validate tile extension
    Mosaic->>Fetcher: Fetch tile
    Fetcher->>Mosaic: Return tile
    Mosaic->>User: Provide tile
Loading

🐇 "In the land of tiles, we fetch and play,
With zoom and cache, we brighten the day.
New functions abound, like rabbits in spring,
Fetching with joy, oh what joy they bring!
So hop along, let the tiles align,
In the world of mosaic, everything's fine!" 🌼


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

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Reviewing files that changed from the base of the PR and between cb33660 and c408161.

📒 Files selected for processing (2)
  • src/mosaic.mjs (11 hunks)
  • src/titiler_fetcher.mjs (2 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
src/titiler_fetcher.mjs

[error] 6-6: Use Number.parseInt instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.parseInt instead.

(lint/style/useNumberNamespace)

src/mosaic.mjs

[error] 237-237: This let declares a variable that is only assigned once.

'finalTiles' is never reassigned.

Safe fix: Use const instead.

(lint/style/useConst)


[error] 263-263: Use Number.POSITIVE_INFINITY instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.POSITIVE_INFINITY instead.

(lint/style/useNumberNamespace)

🔇 Additional comments (2)
src/mosaic.mjs (1)

46-48: Ensure cache functions handle null values correctly

When a tile doesn't intersect with the image footprint, null is cached using cachePutTile(null, ...). Please verify that cacheGetTile and cachePutTile can correctly handle and distinguish between a cached null value and a cache miss to prevent potential errors or unintended behavior.

src/titiler_fetcher.mjs (1)

50-50: Re-evaluate the priority logic in tile request queue

Changing the priority calculation from Math.pow(2, z) to z alters how tile fetching requests are prioritized. Previously, higher zoom levels had exponentially higher priority, ensuring detailed tiles were fetched sooner. With linear priority, this behavior changes. Please verify that this adjustment aligns with the intended tile fetching strategy and does not negatively impact performance or user experience.

src/mosaic.mjs Outdated Show resolved Hide resolved
src/mosaic.mjs Outdated Show resolved Hide resolved
src/titiler_fetcher.mjs Show resolved Hide resolved
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: 4

📜 Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

📥 Commits

Reviewing files that changed from the base of the PR and between c408161 and 2815855.

⛔ Files ignored due to path filters (5)
  • __tests__/mosaic@1x-15-18970-11220.png is excluded by !**/*.png, !**/*.png
  • __tests__/mosaic@2x-11-1233-637.png is excluded by !**/*.png, !**/*.png
  • __tests__/mosaic@2x-12-2371-1402.png is excluded by !**/*.png, !**/*.png
  • __tests__/mosaic@2x-13-4742-2805.png is excluded by !**/*.png, !**/*.png
  • __tests__/mosaic@2x-14-9485-5610.png is excluded by !**/*.png, !**/*.png
📒 Files selected for processing (4)
  • __tests__/mosaic.mjs (1 hunks)
  • __tests__/mosaic512px.mjs (1 hunks)
  • src/mosaic.mjs (6 hunks)
  • src/mosaic_cache_invalidation_job.mjs (1 hunks)
🧰 Additional context used
🪛 Biome (1.9.4)
src/mosaic.mjs

[error] 245-245: This let declares a variable that is only assigned once.

'finalTiles' is never reassigned.

Safe fix: Use const instead.

(lint/style/useConst)


[error] 257-257: Use Number.POSITIVE_INFINITY instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.POSITIVE_INFINITY instead.

(lint/style/useNumberNamespace)

__tests__/mosaic512px.mjs

[error] 2-2: A Node.js builtin module should be imported with the node: protocol.

Using the node: protocol is more explicit and signals that the imported module belongs to Node.js.
Unsafe fix: Add the node: protocol.

(lint/style/useNodejsImportProtocol)


[error] 3-3: A Node.js builtin module should be imported with the node: protocol.

Using the node: protocol is more explicit and signals that the imported module belongs to Node.js.
Unsafe fix: Add the node: protocol.

(lint/style/useNodejsImportProtocol)


[error] 25-25: Template literals are preferred over string concatenation.

Unsafe fix: Use a template literal.

(lint/style/useTemplate)

🔇 Additional comments (3)
src/mosaic.mjs (2)

245-245: Use 'const' instead of 'let' for variables that are not reassigned

The variable finalTiles is declared with let but is never reassigned. To enhance code readability and prevent accidental reassignment, consider using const instead.

Apply this diff to fix the issue:

-let finalTiles = mapTilesWithMetadata(rowTiles, resolvedRowTiles, metadataByUuid);
+const finalTiles = mapTilesWithMetadata(rowTiles, resolvedRowTiles, metadataByUuid);
🧰 Tools
🪛 Biome (1.9.4)

[error] 245-245: This let declares a variable that is only assigned once.

'finalTiles' is never reassigned.

Safe fix: Use const instead.

(lint/style/useConst)


257-257: Use 'Number.POSITIVE_INFINITY' instead of 'Infinity'

For consistency with ES2015 standards, replace the global Infinity with Number.POSITIVE_INFINITY.

Apply this diff to fix the issue:

-            gsd: Infinity,
+            gsd: Number.POSITIVE_INFINITY,
🧰 Tools
🪛 Biome (1.9.4)

[error] 257-257: Use Number.POSITIVE_INFINITY instead of the equivalent global.

ES2015 moved some globals into the Number namespace for consistency.
Safe fix: Use Number.POSITIVE_INFINITY instead.

(lint/style/useNumberNamespace)

__tests__/mosaic.mjs (1)

145-145: Test is now active, ensure test coverage and correctness

Activating the previously skipped test increases test coverage. Please verify that the test passes consistently and covers all expected scenarios.

__tests__/mosaic512px.mjs Show resolved Hide resolved
__tests__/mosaic512px.mjs Show resolved Hide resolved
__tests__/mosaic512px.mjs Show resolved Hide resolved
src/mosaic_cache_invalidation_job.mjs Outdated Show resolved Hide resolved
@dqunbp dqunbp merged commit 94557fd into main Dec 6, 2024
3 checks passed
@dqunbp dqunbp deleted the 20341-openaerialmap-mosaic-prod-performance-issues branch December 6, 2024 12:20
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