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

Cache and Lazy Loading Related Modifications #1454

Draft
wants to merge 12 commits into
base: entityCacheOptimisation
Choose a base branch
from

Conversation

shubham1g5
Copy link
Contributor

@shubham1g5 shubham1g5 commented Jan 3, 2025

Product Description

Supporting changes for dimagi/commcare-android#2928

  • Adds entity loading cancellation and progress hooks
  • Supporting methods to turn on cache and lazy loading for an entity list with the help of the new models detailed here
  • Changes to AsyncEntity to support seletive caching and lazy-loading for only optimizable fields

Safety Assurance

Safety story

Doesn't introduce any changes in existing behaviour

Automated test coverage

QA Plan

Will go through UAT but possibly after merge.

Special deploy instructions

  • This PR can be deployed after merge with no further considerations.

Rollback instructions

  • This PR can be reverted after deploy with no further considerations.

Review

  • The set of people pinged as reviewers is appropriate for the level of risk of the change.

Duplicate PR

Automatically duplicate this PR as defined in contributing.md.

Summary by CodeRabbit

Release Notes

  • New Features

    • Introduced advanced caching and performance optimization mechanisms for entity loading
    • Added progress tracking for entity loading processes
    • Enhanced detail view configuration with new performance attributes
  • Improvements

    • Implemented cancellation support for asynchronous entity loading
    • Refined field evaluation and caching strategies
    • Added optimization options for detail fields
  • Technical Updates

    • Updated parsing logic for detail and field configurations
    • Introduced new interfaces for tracking entity loading progress
    • Modified serialization methods to support new caching features

Copy link

coderabbitai bot commented Jan 3, 2025

Important

Review skipped

Auto incremental reviews are disabled on this repository.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

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

Walkthrough

The pull request introduces a comprehensive enhancement to the entity loading and caching mechanism in CommCare's backend. The changes focus on improving performance and flexibility by introducing new attributes like cacheEnabled and optimize, adding a progress tracking interface, and refactoring methods in several key classes. The modifications provide more granular control over how entities are loaded, cached, and processed, with a particular emphasis on lazy loading and optimization strategies.

Changes

File Change Summary
AsyncEntity.java - Added cacheEnabled field
- Updated constructor to accept Detail object
- Introduced methods for field evaluation and caching
AsyncNodeEntityFactory.java - Added cancellation mechanism for async operations
- Modified caching and loading methods
- Introduced cancelLoading() method
EntityLoadingProgressListener.java - New interface for tracking entity loading progress
- Added EntityLoadingProgressPhase enum
EntityStorageCache.java - Added ValueType enum
- Updated cache key generation methods
NodeEntityFactory.java - Added progress listener support
- Added placeholder methods for caching and cancellation
Detail.java - Added cacheEnabled field
- Updated constructor and serialization methods
- Added isCacheEnabled() and shouldOptimize() methods
DetailField.java - Added optimize field
- Added getter and setter for optimization
DetailFieldParser.java - Added parsing for "optimize" attribute
DetailParser.java - Replaced isLazyLoading with lazyLoading
- Added cacheEnabled parsing
Test files - Updated test cases to reflect new performance attributes

Sequence Diagram

sequenceDiagram
    participant Client
    participant NodeEntityFactory
    participant AsyncNodeEntityFactory
    participant EntityLoadingProgressListener
    participant AsyncEntity

    Client->>NodeEntityFactory: setEntityProgressListener()
    Client->>NodeEntityFactory: prepareEntities()
    NodeEntityFactory->>AsyncNodeEntityFactory: primeCache()
    AsyncNodeEntityFactory->>EntityLoadingProgressListener: publishProgress(PHASE_PROCESSING)
    AsyncNodeEntityFactory->>AsyncEntity: calculateNonLazyFields()
    AsyncEntity-->>AsyncNodeEntityFactory: Field Data
    AsyncNodeEntityFactory->>EntityLoadingProgressListener: publishProgress(PHASE_CACHING)
    AsyncNodeEntityFactory->>EntityLoadingProgressListener: publishProgress(PHASE_COMPLETE)
Loading

Poem

🐰 Caching Rabbit's Delight

In fields of data, swift and bright,
Lazy loading takes its flight,
Optimize, cache with all my might,
Performance dancing left and right!

Hop hop hooray! 🚀


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 generate docstrings to generate docstrings for this PR. (Beta)
  • @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.

@shubham1g5
Copy link
Contributor Author

@coderabbitai full review

Copy link

coderabbitai bot commented Jan 10, 2025

✅ Actions performed

Full review triggered.

Copy link

@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

🧹 Nitpick comments (9)
src/main/java/org/commcare/cases/entity/AsyncEntity.java (2)

179-179: Avoid using printStackTrace() in production code

Using xpe.printStackTrace(); is not recommended in production environments. Consider removing it or replacing it with appropriate logging.

Apply this diff to remove the unnecessary printStackTrace():

-                xpe.printStackTrace();

256-256: Remove printStackTrace() from exception handling

Direct usage of printStackTrace(); should be avoided. Use the logging framework to log exceptions instead.

Apply this diff to remove the printStackTrace() call:

-                xpe.printStackTrace();
src/main/java/org/commcare/cases/entity/EntityLoadingProgressListener.java (2)

25-25: Typo in documentation comment

There's a typo in the comment for PHASE_UNCACHED_CALCULATION.

Apply this diff to correct the spelling of "already":

-             * already cached and similarly can be very quick when most things are arealdy cached.
+             * already cached and similarly can be very quick when most things are already cached.

52-52: Typo in Javadoc parameter description

The word "corrosponding" should be "corresponding" in the method documentation.

Apply this diff to fix the typo:

-         * @param progress progress corrosponding to the current entity loading phase
+         * @param progress progress corresponding to the current entity loading phase
src/main/java/org/commcare/cases/entity/AsyncNodeEntityFactory.java (2)

Line range hint 82-100: Good improvements to primeCache method.

The changes enhance the method with cancellation support and progress tracking. Consider adding error handling to report failures to the progress listener.

 protected void primeCache() {
     if (isCancelled) return;
     if (progressListener != null) {
         progressListener.publishEntityLoadingProgress(
                 EntityLoadingProgressListener.EntityLoadingProgressPhase.PHASE_CACHING, 0, 100);
     }
+    try {
         if (mEntityCache == null || mTemplateIsCachable == null || !mTemplateIsCachable || mCacheHost == null) {
             return;
         }

         String[][] cachePrimeKeys = mCacheHost.getCachePrimeGuess();
         if (cachePrimeKeys == null) {
             return;
         }
         mEntityCache.primeCache(mEntitySet,cachePrimeKeys, detail);
+    } catch (Exception e) {
+        if (progressListener != null) {
+            progressListener.publishEntityLoadingProgress(
+                    EntityLoadingProgressListener.EntityLoadingProgressPhase.PHASE_CACHING, -1, 100);
+        }
+        throw e;
+    }
     if (progressListener != null) {
         progressListener.publishEntityLoadingProgress(
                 EntityLoadingProgressListener.EntityLoadingProgressPhase.PHASE_CACHING, 100, 100);
     }
 }

Line range hint 133-178: Consider optimizing the nested loops for better performance.

The implementation correctly handles cancellation and progress tracking. However, the nested loops in setUnCachedData could be optimized for better performance.

Consider these optimizations:

  1. Pre-calculate the number of fields to avoid repeated calls to getNumFields()
  2. Batch the progress updates to reduce overhead
 protected void setUnCachedData(List<Entity<TreeReference>> entities) {
+    final int numFields = detail.getFields().length;
+    final int progressUpdateInterval = Math.max(1, entities.size() / 100);
     for (int i = 0; i < entities.size(); i++) {
         if (isCancelled) return;
         AsyncEntity e = (AsyncEntity)entities.get(i);
-        for (int col = 0; col < e.getNumFields(); ++col) {
+        for (int col = 0; col < numFields; ++col) {
             if (detail.getFields()[col].isOptimize()) {
                 e.getField(col);
                 e.getSortField(col);
             }
         }
-        if (progressListener != null) {
+        if (progressListener != null && i % progressUpdateInterval == 0) {
             progressListener.publishEntityLoadingProgress(
                     EntityLoadingProgressListener.EntityLoadingProgressPhase.PHASE_UNCACHED_CALCULATION, i,
                     entities.size());
         }
     }
+    if (progressListener != null) {
+        progressListener.publishEntityLoadingProgress(
+                EntityLoadingProgressListener.EntityLoadingProgressPhase.PHASE_UNCACHED_CALCULATION,
+                entities.size(), entities.size());
+    }
 }
src/main/java/org/commcare/cases/entity/NodeEntityFactory.java (1)

198-208: Well-structured base class methods.

Good implementation of base methods with appropriate error handling. Consider adding Javadoc to document the expected behavior of these methods.

+/**
+ * Caches the provided entities. Default implementation throws RuntimeException.
+ * Subclasses should override this method if they support caching.
+ *
+ * @param entities List of entities to cache
+ * @throws RuntimeException if caching is not supported
+ */
 public void cacheEntities(List<Entity<TreeReference>> entities) {
     throw new RuntimeException("Method not supported for normal Node Entity Factory");
 }

+/**
+ * Sets the progress listener for entity loading operations.
+ *
+ * @param progressListener The progress listener to use
+ */
 public void setEntityProgressListener(EntityLoadingProgressListener progressListener) {
     this.progressListener = progressListener;
 }

+/**
+ * Cancels the current loading operation. Default implementation throws RuntimeException.
+ * Subclasses should override this method if they support cancellation.
+ *
+ * @throws RuntimeException if cancellation is not supported
+ */
 public void cancelLoading() {
     throw new RuntimeException("Method not supported for normal Node Entity Factory");
 }
src/test/java/org/commcare/backend/suite/model/test/AppStructureTests.java (1)

259-263: Consider adding more comprehensive test cases.

While the current test covers the basic functionality, consider adding test cases for:

  1. Cache disabled scenarios
  2. Mixed optimize flags across fields
  3. Edge cases with empty or invalid attributes
@Test
public void testDetailPerformanceAttributes_comprehensive() {
    // Test cache disabled
    Detail detailNoCaching = mApp.getSession().getPlatform().getDetail("m1_case_short");
    assertFalse(detailNoCaching.isCacheEnabled());
    
    // Test mixed optimize flags
    Detail detailMixedOptimize = mApp.getSession().getPlatform().getDetail("m2_case_short");
    assertTrue(detailMixedOptimize.getFields()[0].isOptimize());
    assertFalse(detailMixedOptimize.getFields()[1].isOptimize());
}
src/main/java/org/commcare/suite/model/Detail.java (1)

518-528: Consider optimizing the field iteration.

The method could be more efficient by short-circuiting when the first optimizable field is found.

Consider this optimization:

 public boolean shouldOptimize() {
     if (cacheEnabled || lazyLoading) {
         for (DetailField field : fields) {
             if (field.isOptimize()) {
-                return true;
+                return true; // Early return on first optimizable field
             }
-        }
+        }
     }
     return false;
 }
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between ea93ab7 and 0f48c78.

📒 Files selected for processing (11)
  • src/main/java/org/commcare/cases/entity/AsyncEntity.java (10 hunks)
  • src/main/java/org/commcare/cases/entity/AsyncNodeEntityFactory.java (6 hunks)
  • src/main/java/org/commcare/cases/entity/EntityLoadingProgressListener.java (1 hunks)
  • src/main/java/org/commcare/cases/entity/EntityStorageCache.java (1 hunks)
  • src/main/java/org/commcare/cases/entity/NodeEntityFactory.java (3 hunks)
  • src/main/java/org/commcare/suite/model/Detail.java (8 hunks)
  • src/main/java/org/commcare/suite/model/DetailField.java (5 hunks)
  • src/main/java/org/commcare/xml/DetailFieldParser.java (1 hunks)
  • src/main/java/org/commcare/xml/DetailParser.java (2 hunks)
  • src/test/java/org/commcare/backend/suite/model/test/AppStructureTests.java (1 hunks)
  • src/test/resources/app_structure/suite.xml (2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (4)
  • GitHub Check: task-list-completed
  • GitHub Check: task-list-completed
  • GitHub Check: task-list-completed
  • GitHub Check: task-list-completed
🔇 Additional comments (26)
src/main/java/org/commcare/cases/entity/EntityStorageCache.java (2)

12-15: LGTM! Well-structured enum addition.

The new ValueType enum provides clear type safety for distinguishing between normal and sort fields. The naming is descriptive and follows Java conventions.


19-19: Verify implementations of breaking changes.

The interface has two breaking changes:

  1. Added ValueType parameter to getCacheKey
  2. Renamed getSortFieldIdFromCacheKey to getFieldIdFromCacheKey

These changes improve the API but require updates to all implementing classes.

Let's verify the implementations:

Also applies to: 25-25

src/main/java/org/commcare/cases/entity/AsyncEntity.java (6)

4-5: Imports added for ValueType constants

The static imports for TYPE_NORMAL_FIELD and TYPE_SORT_FIELD enhance code readability and maintainability.


53-53: Consistent addition of cacheEnabled flag

The introduction of the cacheEnabled boolean aligns with existing coding conventions and integrates well with the class's functionality.


Line range hint 74-94: Refactored constructor improves cohesion

Changing the constructor to accept a Detail object instead of separate parameters simplifies the initialization process and improves cohesion between AsyncEntity and Detail.


98-118: calculateNonLazyFields method correctly processes field evaluation

The logic in calculateNonLazyFields accurately handles different scenarios based on caching and lazy loading configurations, ensuring that fields are evaluated appropriately.


138-145: Efficient field retrieval with proper caching

The getField method efficiently checks for existing data and evaluates non-optimized fields immediately, improving performance.


146-171: Proper synchronization and exception handling in getField

The method correctly synchronizes access to shared resources and handles potential I/O exceptions, adhering to best practices for concurrent programming.

src/main/java/org/commcare/cases/entity/EntityLoadingProgressListener.java (1)

1-56: New EntityLoadingProgressListener interface is well-designed

The interface effectively allows tracking of entity loading progress with clearly defined phases and documentation.

src/main/java/org/commcare/xml/DetailParser.java (2)

42-43: Parsing of new attributes cache_enabled and lazy_loading

The addition of parsing for cache_enabled and lazy_loading attributes enhances the configurability of Detail objects.


145-145: Updated constructor call for Detail

Including cacheEnabled in the Detail constructor ensures that the caching configuration is properly propagated.

src/main/java/org/commcare/cases/entity/AsyncNodeEntityFactory.java (3)

33-33: LGTM! Good addition of cancellation support.

Adding cancellation support is a good practice for async operations to prevent unnecessary processing.


122-131: Well-structured caching logic.

Good separation of concerns between new and legacy caching pathways based on the cache enabled flag.


59-60: Verify initialization of AsyncEntity with new parameters.

The constructor now includes additional parameters for caching support. Ensure that all parameters are properly initialized in the AsyncEntity class.

✅ Verification successful

Constructor implementation verified and correct

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Check AsyncEntity constructor implementation
ast-grep --pattern 'class AsyncEntity {
  $$$
  AsyncEntity($$$) {
    $$$
  }
  $$$
}'

Length of output: 89


Script:

#!/bin/bash
# Find the file containing AsyncEntity class
rg "class AsyncEntity" -l

# If found, examine the implementation
echo "=== AsyncEntity Implementation ==="
rg "class AsyncEntity" -A 50

Length of output: 5295

src/main/java/org/commcare/cases/entity/NodeEntityFactory.java (1)

28-28: LGTM! Good encapsulation of progress listener.

The protected field allows proper access in subclasses while maintaining encapsulation.

src/main/java/org/commcare/xml/DetailFieldParser.java (1)

34-35: LGTM! Consistent attribute parsing implementation.

The parsing of the "optimize" attribute follows the established pattern. Verify that the XML schema documentation is updated to reflect this new attribute.

src/main/java/org/commcare/suite/model/DetailField.java (4)

80-81: LGTM: Field declaration follows Java conventions.

The optimize field is properly declared as a private boolean field, following Java conventions.


306-308: LGTM: Standard getter implementation.

The isOptimize() method follows Java bean naming conventions for boolean getters.


446-448: LGTM: Builder pattern implementation.

The setOptimize() method in the Builder class follows the builder pattern correctly.


219-219: LGTM: Proper serialization handling.

The optimize field is correctly handled in both readExternal and writeExternal methods using ExtUtil.

Also applies to: 253-253

src/main/java/org/commcare/suite/model/Detail.java (4)

112-112: LGTM: Field declaration follows Java conventions.

The cacheEnabled field is properly declared as a private boolean field.


130-130: LGTM: Constructor parameter properly handled.

The cacheEnabled parameter is correctly added and assigned.

Also applies to: 182-182


238-240: LGTM: Standard getter implementation.

The isCacheEnabled() method follows Java bean naming conventions for boolean getters.


272-275: LGTM: Clear deprecation notice.

The comment clearly explains that this is a legacy way to enable cache and index, helping future maintainers understand the transition.

src/test/resources/app_structure/suite.xml (2)

14-14: LGTM: Detail attributes properly configured.

The cache_enabled attribute is correctly added alongside the existing lazy_loading attribute.


69-69: LGTM: Field optimization enabled.

The optimize attribute is correctly added to the field element, which will trigger the optimization in the corresponding DetailField instance.

src/main/java/org/commcare/cases/entity/AsyncEntity.java Outdated Show resolved Hide resolved
@shubham1g5 shubham1g5 force-pushed the cacheAndIndexModifications branch from 0f48c78 to 2ec92ad Compare January 10, 2025 11:02
@shubham1g5 shubham1g5 requested review from ctsims and avazirna January 14, 2025 07:16
@shubham1g5 shubham1g5 changed the title Cache and Index Related Modifications Cache and Lazy Loading Related Modifications Jan 14, 2025
private void primeCache() {
protected void primeCache() {
if (isCancelled) return;
if (progressListener != null) {
Copy link
Member

Choose a reason for hiding this comment

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

Will it cause issues for this progress update to occur before all of the null checks below that dump out of the method and/or without a finally {} clause? It seems it it would keep the listener spinning forever.

Copy link
Member

@ctsims ctsims left a comment

Choose a reason for hiding this comment

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

These changes all seem clear / appropriate for the stated spec. Left a couple of points of feedback

Knowing that part of the goal here is long term to be managing entity loading over more flexible lifecycles, I'm a little worried that issues with managing the listener lifecycle here could be an issue.

* @param progressListener The progress listener to use
*/
public void setEntityProgressListener(EntityLoadingProgressListener progressListener) {
this.progressListener = progressListener;
Copy link
Member

Choose a reason for hiding this comment

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

If there is an existing listener set, I think this method should close it out before changing the current one. Alternatively this method should potentially error out, or otherwise treat the listener as immutable once set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants