Skip to content

Clear APIResource cache on undeploy event #13071

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

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

Conversation

PasanT9
Copy link
Contributor

@PasanT9 PasanT9 commented Apr 8, 2025

Improvements to API resource cache invalidation:

Invalidating the resource cache when undeploying an API, and ensuring the resource cache is cleared when creating API revisions.

Fix: wso2/api-manager#3843

Copy link

coderabbitai bot commented Apr 8, 2025

📝 Walkthrough

Walkthrough

This pull request introduces a new method to determine if API resource validation is enabled and adjusts the cache management logic in two components. In the gateway deployer, a method isAPIResourceValidationEnabled() is added to check configuration and conditionally invoke cache invalidation during API undeployment. In addition, the cache clearing call in the API revision update process is repositioned to occur after processing the event type, ensuring more consistent cache updates during API revision deployments.

Changes

File(s) Change Summary
components/apimgt/.../InMemoryAPIDeployer.java Added isAPIResourceValidationEnabled() method to check configuration; modified unDeployAPI to conditionally call CacheInvalidationServiceImpl.invalidateResourceCache() based on validation status.
components/apimgt/.../SubscriptionDataStoreImpl.java Repositioned the clearResourceCache call within addOrUpdateAPIRevisionWithUrlTemplates to occur after event type check, ensuring the cache update logic executes post event handling.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant GatewayDeployer as InMemoryAPIDeployer
    participant Config as APIManagerConfiguration
    participant CacheService as CacheInvalidationServiceImpl

    Client->>GatewayDeployer: Call unDeployAPI(gatewayEvent)
    GatewayDeployer->>GatewayDeployer: isAPIResourceValidationEnabled()
    GatewayDeployer->>Config: Retrieve validation configuration
    Config-->>GatewayDeployer: Return true/false
    alt Validation Enabled
        GatewayDeployer->>CacheService: invalidateResourceCache(context, version, tenantDomain, [])
    else Validation Disabled
        Note right of GatewayDeployer: Skip cache invalidation
    end
    GatewayDeployer-->>Client: API undeployment complete
Loading
sequenceDiagram
    participant EventSource as API Revision Event
    participant DataStore as SubscriptionDataStoreImpl
    participant API as API Object

    EventSource->>DataStore: Trigger addOrUpdateAPIRevisionWithUrlTemplates(event)
    DataStore->>DataStore: Check if event type is REMOVE_API_FROM_GATEWAY
    alt Not REMOVE_API_FROM_GATEWAY
        DataStore->>DataStore: Load new API and update API map
        DataStore->>API: If API is valid, then...
        API-->>DataStore: API loaded
        DataStore->>DataStore: clearResourceCache(api, tenantDomain)
    else REMOVE_API_FROM_GATEWAY
        Note right of DataStore: Skip API map update and cache clear
    end
Loading

Assessment against linked issues

Objective Addressed Explanation
Cache Inconsistency During API Revision Deployment Leading to Incorrect API State (#3843)

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 7e0858d and d30b89d.

📒 Files selected for processing (2)
  • components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java (3 hunks)
  • components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/model/impl/SubscriptionDataStoreImpl.java (1 hunks)
🧰 Additional context used
🧬 Code Definitions (1)
components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java (1)
components/apimgt/org.wso2.carbon.apimgt.impl/src/main/java/org/wso2/carbon/apimgt/impl/APIManagerConfiguration.java (1)
  • APIManagerConfiguration (95-2857)
⏰ Context from checks skipped due to timeout of 90000ms (6)
  • GitHub Check: build-product (3, group3)
  • GitHub Check: build-product (4, group4)
  • GitHub Check: build-product (2, group2)
  • GitHub Check: build-product (1, group1)
  • GitHub Check: run-benchmark-test
  • GitHub Check: build-carbon
🔇 Additional comments (4)
components/apimgt/org.wso2.carbon.apimgt.gateway/src/main/java/org/wso2/carbon/apimgt/gateway/InMemoryAPIDeployer.java (3)

49-50: Added imports for cache management.

The newly added imports support the cache invalidation functionality that will be implemented below, providing necessary classes for configuration management and cache operations.


376-382: Enhanced API undeployment with resource cache invalidation.

This change implements clearing the API resource cache during API undeployment, which aligns with the PR objective of "invalidating the resource cache when an API is undeployed". The conditional check ensures this happens only when resource validation is enabled.

The implementation correctly passes relevant parameters from the gateway event to invalidate the specific API's cache entries.


399-413: Added method to check if API resource validation is enabled.

This method properly retrieves the Gateway resource cache configuration from API Manager settings. The default fallback to true ensures cache validation remains enabled even if configuration reading fails, which is a safer approach for maintaining data consistency.

The error handling approach with logging is appropriate, and returning a default value ensures the system continues to function even if configuration retrieval fails.

components/apimgt/org.wso2.carbon.apimgt.keymgt/src/main/java/org/wso2/carbon/apimgt/keymgt/model/impl/SubscriptionDataStoreImpl.java (1)

818-820: Repositioned cache clearing operation for better timing.

This change improves the API revision update process by moving the cache clearing operation to execute after the event type has been processed and any necessary updates to the API map have been completed. This ensures that cache invalidation happens with the most up-to-date API information.

This change addresses the PR objective of "ensuring that the resource cache is cleared during the creation of API revisions" by placing the cache invalidation at the right point in the execution flow.

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ 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.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @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.

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.

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.

Cache Inconsistency During API Revision Deployment Leading to Incorrect API State
1 participant