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

feat(arns): allow operator to override arns ttls and force refreshes … #212

Merged
merged 1 commit into from
Sep 26, 2024

Conversation

dtfiedler
Copy link
Collaborator

@dtfiedler dtfiedler commented Sep 26, 2024

…based on env variable

Introduces ARNS_RESOLVER_OVERRIDE_TTL_SECONDS env variable which can be used to override when to refresh arns names. If set to 0 the resolver will always attempt to fetch the latest resolution data from the resolvers. If it fails to refretch, it will fallback to what it has cached.

Testing

ARNS_RESOLVER_OVERRIDE_TTL_SECONDS=0

First resolved at: 1727378491422
Second resolved at: 1727378509536 (18s later)

Logs

info: Resolving name... {"class":"CompositeArNSResolver","name":"dtf","overrides":{"ttlSeconds":0},"timestamp":"2024-09-26T19:21:29.144Z"}
info: Cache miss for arns name {"class":"CompositeArNSResolver","name":"dtf","timestamp":"2024-09-26T19:21:29.145Z"}
info: Attempting to resolve name with resolver {"class":"CompositeArNSResolver","name":"dtf","timestamp":"2024-09-26T19:21:29.145Z","type":"OnDemandArNSResolver"}
info: Resolving name... {"class":"OnDemandArNSResolver","name":"dtf","timestamp":"2024-09-26T19:21:29.145Z"}
info: Resolved name {"class":"CompositeArNSResolver","name":"dtf","resolution":{"name":"dtf","processId":"j2CetgCJlRhiuoqtxEWkXjaaar-KvxqI3i8d0K7Grqo","resolvedAt":1727378491422,"resolvedId":"TzeIML94iFdoQKc6rz0JP74FLSXNscWc52q_uvXtC34","ttl":3600},"timestamp":"2024-09-26T19:21:31.423Z"}
info: Resolving name... {"class":"CompositeArNSResolver","name":"dtf","overrides":{"ttlSeconds":0},"timestamp":"2024-09-26T19:21:47.201Z"}
info: Cache miss for arns name {"class":"CompositeArNSResolver","name":"dtf","timestamp":"2024-09-26T19:21:47.201Z"}
info: Attempting to resolve name with resolver {"class":"CompositeArNSResolver","name":"dtf","timestamp":"2024-09-26T19:21:47.201Z","type":"OnDemandArNSResolver"}
info: Resolving name... {"class":"OnDemandArNSResolver","name":"dtf","timestamp":"2024-09-26T19:21:47.201Z"}
info: Resolved name {"class":"CompositeArNSResolver","name":"dtf","resolution":{"name":"dtf","processId":"j2CetgCJlRhiuoqtxEWkXjaaar-KvxqI3i8d0K7Grqo","resolvedAt":1727378509536,"resolvedId":"TzeIML94iFdoQKc6rz0JP74FLSXNscWc52q_uvXtC34","ttl":3600},"timestamp":"2024-09-26T19:21:49.536Z"}

…based on env variable

Intrdocues `ARNS_RESOLVER_OVERRIDE_TTL_SECONDS` env variable which can be used to override when to refresh arns names. If set to `0` the resolver will always attempt to fetch the latest resolution data from the resolvers.
Copy link
Contributor

coderabbitai bot commented Sep 26, 2024

📝 Walkthrough

Walkthrough

The changes introduce new configuration options for the ARNS resolver, specifically related to time-to-live (TTL) settings. Two constants are added to manage TTL values from environment variables. The createArNSResolver function and the CompositeArNSResolver class are updated to accept an optional overrides parameter, allowing for more flexible TTL configuration. Additionally, the system configuration is modified to include these new TTL settings, enhancing the overall configurability of the ARNS resolver.

Changes

Files Change Summary
src/config.ts Added constants ARNS_RESOLVER_OVERRIDE_TTL_SECONDS_STRING and ARNS_RESOLVER_OVERRIDE_TTL_SECONDS to manage TTL settings from environment variables.
src/init/resolvers.ts Updated createArNSResolver function to include an optional overrides parameter that allows specifying a ttlSeconds value.
src/resolution/composite-arns-resolver.ts Modified CompositeArNSResolver class to accept an optional overrides parameter in the constructor, affecting TTL handling in the resolve method.
src/system.ts Introduced overrides property in the nameResolver configuration, including a ttlSeconds field set to the value of config.ARNS_RESOLVER_OVERRIDE_TTL_SECONDS.

Possibly related PRs

Suggested reviewers

  • karlprieb
  • djwhitt

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

codecov bot commented Sep 26, 2024

Codecov Report

Attention: Patch coverage is 77.77778% with 2 lines in your changes missing coverage. Please review.

Project coverage is 68.71%. Comparing base (d12f1f9) to head (7e5b2b5).
Report is 1 commits behind head on develop.

Files with missing lines Patch % Lines
src/config.ts 77.77% 2 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #212      +/-   ##
===========================================
- Coverage    70.20%   68.71%   -1.50%     
===========================================
  Files           32       32              
  Lines         7908     7917       +9     
  Branches       430      431       +1     
===========================================
- Hits          5552     5440     -112     
- Misses        2356     2476     +120     
- Partials         0        1       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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 (5)
src/init/resolvers.ts (1)

73-73: LGTM! Consider adding a comment for clarity.

The addition of the overrides parameter aligns well with the PR objectives, allowing operators to control ARNs TTLs. The implementation is correct and doesn't affect existing functionality.

Consider adding a brief comment explaining the purpose of the overrides parameter, for example:

/**
 * @param overrides Optional settings to override default behavior
 * @param overrides.ttlSeconds Optional TTL in seconds for ARNs resolution
 */
overrides?: {
  ttlSeconds?: number;
};

This will help future developers understand the purpose and usage of this parameter.

Also applies to: 80-82

src/resolution/composite-arns-resolver.ts (2)

27-32: LGTM! Consider adding JSDoc for the overrides property.

The new overrides property aligns well with the PR objectives. The TODO comment is a good practice for future enhancements.

Consider adding a JSDoc comment to explain the purpose and usage of the overrides property. This will improve code documentation and maintainability. For example:

/**
 * Optional overrides for resolver behavior.
 * @property {number} [ttlSeconds] - Override for the TTL in seconds.
 */
private overrides: {
  ttlSeconds?: number;
  // TODO: other overrides like fallback txId if not found in resolution
} | undefined;

64-65: LGTM! TTL override logic implemented correctly. Consider extracting the TTL calculation.

The changes correctly implement the TTL override functionality while maintaining backward compatibility. The cache validity check is properly updated to use the potentially overridden TTL.

For improved readability and maintainability, consider extracting the TTL calculation into a separate method. This would make the logic more explicit and easier to test. For example:

private getEffectiveTTL(cachedResolution: NameResolution): number | undefined {
  return this.overrides?.ttlSeconds ?? cachedResolution.ttl;
}

// Then in the resolve method:
const ttlSeconds = this.getEffectiveTTL(cachedResolution);
if (
  cachedResolution !== undefined &&
  cachedResolution.resolvedAt !== undefined &&
  ttlSeconds !== undefined &&
  cachedResolution.resolvedAt + ttlSeconds * 1000 > Date.now()
) {
  // ... rest of the code
}

Also applies to: 69-70

src/config.ts (1)

271-278: LGTM! Consider adding a comment for clarity.

The implementation of ARNS_RESOLVER_OVERRIDE_TTL_SECONDS is correct and aligns well with the existing codebase. It provides a flexible way to override the TTL for ARNS resolvers through an environment variable.

Consider adding a brief comment explaining the purpose of these constants and how they affect the ARNS resolver behavior. This would improve code readability and maintainability. For example:

// Override the default TTL for ARNS resolvers. If set to 0, it forces a refresh on each resolution.
export const ARNS_RESOLVER_OVERRIDE_TTL_SECONDS_STRING = env.varOrUndefined(
  'ARNS_RESOLVER_OVERRIDE_TTL_SECONDS',
);

export const ARNS_RESOLVER_OVERRIDE_TTL_SECONDS =
  ARNS_RESOLVER_OVERRIDE_TTL_SECONDS_STRING !== undefined
    ? +ARNS_RESOLVER_OVERRIDE_TTL_SECONDS_STRING
    : undefined;
src/system.ts (1)

571-574: LGTM! Consider documenting the new configuration option.

The addition of the overrides object with the ttlSeconds property aligns well with the PR objectives. It provides the desired flexibility for operators to control the TTL for ARN resolution.

A few suggestions:

  1. Consider adding documentation for the ARNS_RESOLVER_OVERRIDE_TTL_SECONDS configuration option, including its purpose, expected values, and any default value.
  2. Regarding the TODO comment, do you have any specific plans or timeline for implementing other overrides like the fallback txId? It might be helpful to create an issue to track this future enhancement.
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between d12f1f9 and 7e5b2b5.

📒 Files selected for processing (4)
  • src/config.ts (1 hunks)
  • src/init/resolvers.ts (2 hunks)
  • src/resolution/composite-arns-resolver.ts (2 hunks)
  • src/system.ts (1 hunks)
🔇 Additional comments (6)
src/init/resolvers.ts (1)

120-120: Verify CompositeArNSResolver implementation

The overrides parameter is correctly passed to the CompositeArNSResolver constructor. However, we should verify that the CompositeArNSResolver class is prepared to handle this new parameter.

Let's check the implementation of CompositeArNSResolver:

✅ Verification successful

CompositeArNSResolver correctly handles the overrides parameter

The overrides parameter is properly assigned and utilized within the CompositeArNSResolver class constructor and methods.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify CompositeArNSResolver implementation
# Expected result: CompositeArNSResolver constructor should accept an 'overrides' parameter

# Search for the CompositeArNSResolver class definition
ast-grep --lang typescript --pattern 'class CompositeArNSResolver {
  constructor($_) {
    $$$
  }
  $$$
}'

# Search for usage of 'overrides' within the CompositeArNSResolver class
rg --type typescript -A 5 'overrides' src/resolution/composite-arns-resolver.ts

Length of output: 225


Script:

#!/bin/bash
# Description: Verify CompositeArNSResolver implementation with adjusted file search
# Expected result: CompositeArNSResolver constructor should accept an 'overrides' parameter

# Search for the CompositeArNSResolver class definition with a broader pattern
ast-grep --lang typescript --pattern 'class CompositeArNSResolver {
  constructor($_) {
    $$$
  }
  $$$
}'

# Search for usage of 'overrides' within the CompositeArNSResolver class by targeting .ts files explicitly
rg -A 5 'overrides' --glob '*.ts' src/resolution/composite-arns-resolver.ts

Length of output: 1267


Script:

#!/bin/bash
# Description: Verify CompositeArNSResolver implementation with improved patterns
# Expected result: CompositeArNSResolver constructor should accept an 'overrides' parameter

# Search for the CompositeArNSResolver class definition with a flexible pattern
ast-grep --lang typescript --pattern 'class CompositeArNSResolver {
  constructor($_) {
    $$$
  }
  $$$
}'

# Search for usage of 'overrides' within the CompositeArNSResolver class by targeting .ts files explicitly
rg -A 5 'overrides' --glob '*.ts' src/resolution/composite-arns-resolver.ts

Length of output: 1267

src/resolution/composite-arns-resolver.ts (3)

38-38: LGTM! Constructor changes implement the overrides functionality correctly.

The updated constructor signature and implementation properly handle the new overrides parameter. The changes maintain backward compatibility by making the parameter optional.

Also applies to: 43-45, 50-50


54-54: LGTM! Enhanced logging improves observability.

The updated logging statement now includes the overrides information, which is valuable for debugging and monitoring the resolver's behavior with respect to TTL overrides.


Line range hint 1-124: Overall, the changes successfully implement the TTL override functionality.

The modifications to the CompositeArNSResolver class effectively introduce the ability to override TTLs for ARN resolution, aligning well with the PR objectives. The implementation is backward compatible and maintains existing functionality. The code quality is good, with clear type definitions and appropriate error handling.

A few minor suggestions have been made to further improve code documentation and readability:

  1. Adding JSDoc for the overrides property.
  2. Extracting the TTL calculation logic into a separate method for better maintainability.

These changes enhance the flexibility of the ARN resolution process and provide operators with more control over caching behavior, as intended.

src/config.ts (2)

271-278: Summary: New ARNS resolver TTL override feature added.

The changes introduce a new feature to override the TTL for ARNS resolvers through environment variables. This addition:

  1. Aligns well with the existing configuration patterns in the file.
  2. Provides more flexibility in managing ARNS resolution behavior.
  3. Has minimal impact on the existing codebase structure.

These changes enhance the configurability of the AR.IO Gateway, particularly for operators who need fine-grained control over ARNS resolution caching. Ensure that the team is aware of this new configuration option and updates any relevant documentation or deployment scripts accordingly.


271-278: Verify the usage of new constants in the codebase.

The new constants ARNS_RESOLVER_OVERRIDE_TTL_SECONDS_STRING and ARNS_RESOLVER_OVERRIDE_TTL_SECONDS provide a mechanism to override the TTL for ARNS resolvers. To ensure correct implementation:

  1. Verify that the parts of the codebase using these constants handle the potential undefined value correctly.
  2. Check if any existing ARNS resolver logic needs to be updated to incorporate this new override functionality.

To assist with this verification, you can run the following script:

This script will help identify where the new constants are used and where ARNS resolver logic might need to be updated.

@djwhitt djwhitt merged commit 4035060 into develop Sep 26, 2024
4 checks passed
@djwhitt djwhitt deleted the PE-6828-override-ttl branch September 26, 2024 20:40
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