feat(apisix-standalone): lrucache ttl and active invalidation#460
Conversation
|
Note Reviews pausedIt looks like this branch is under active development. To avoid overwhelming you with review comments due to an influx of new commits, CodeRabbit has automatically paused this review. You can configure this behavior by changing the Use the following commands to manage reviews:
Use the checkboxes below for quick actions:
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughReplaces the four ChangesLRU Cache and bypassCache Feature
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Important Pre-merge checks failedPlease resolve all errors before merging. Addressing warnings is optional. ❌ Failed checks (1 error, 1 warning)
✅ Passed checks (4 passed)
✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🧹 Nitpick comments (2)
libs/backend-apisix-standalone/e2e/cache.e2e-spec.ts (2)
485-485: ⚡ Quick winUse key invalidation in teardown to fully reset all cache buckets.
Line 485 clears only
configCacheandrawConfigCache;versionandlatestVersioncan persist and couple later suites that reuse the same cache key.Proposed fix
- afterAll(() => (configCache.clear(), rawConfigCache.clear())); + afterAll(() => invalidateCache(cacheKey));🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@libs/backend-apisix-standalone/e2e/cache.e2e-spec.ts` at line 485, The afterAll hook in the test file only clears configCache and rawConfigCache, but does not clear the version and latestVersion cache buckets. This allows cached state to persist between test suites, causing potential test coupling. Add calls to clear the version and latestVersion cache buckets in the afterAll hook on the same line, using the same pattern as the existing cache.clear() calls for configCache and rawConfigCache to ensure all cache buckets are fully reset after each test suite.
510-527: ⚡ Quick winAssert
TASK_STARTemission in the bypassCache test.This suite validates data freshness and API activity, but it doesn’t assert the new
TASK_STARTcontract introduced for bypass invalidation. Adding that check will prevent silent regressions in the event/logging path.Proposed enhancement
it('dump with bypassCache fetches fresh data from APISIX', async () => { const freshBackend = new BackendAPISIXStandalone({ server: server1, token: token1, tlsSkipVerify: true, cacheKey, bypassCache: true, ...defaultBackendOptions, }); let apiCall = 0; + let taskStartCount = 0; const sub = freshBackend.on('AXIOS_DEBUG', () => apiCall++); + const taskSub = freshBackend.on('TASK_START', ({ name }) => { + taskStartCount++; + expect(name).toContain(`Cache invalidated for key: "${cacheKey}"`); + }); const result = await dumpConfiguration(freshBackend); sub.unsubscribe(); + taskSub.unsubscribe(); expect(apiCall).toBeGreaterThan(0); + expect(taskStartCount).toEqual(1); expect(result).toMatchObject(syncedConfig); });🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the rest with a brief reason, keep changes minimal, and validate. In `@libs/backend-apisix-standalone/e2e/cache.e2e-spec.ts` around lines 510 - 527, The test for the bypassCache feature in the dumpConfiguration call currently only validates AXIOS_DEBUG event emissions but doesn't assert the TASK_START event that was introduced for bypass invalidation. Add a listener for the TASK_START event on the freshBackend instance similar to the existing AXIOS_DEBUG listener, initialize a counter to track TASK_START emissions, and then add an assertion to verify that TASK_START was emitted at least once during the dumpConfiguration call to ensure the bypass invalidation contract is properly tested.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@apps/cli/src/server/sync.ts`:
- Line 77: The `any` cast on the `task.opts` expression in the httpsAgent
assignment within sync.ts is masking a missing schema definition. Add
`tlsSkipVerify: z.boolean().optional()` to the `SyncTask.opts` schema definition
in schema.ts under the looseObject, then remove the `(task.opts as any)` cast
from sync.ts line 77 and similarly remove any `any` cast from validate.ts line
76 that accesses this property, allowing TypeScript to properly type-check the
access to the tlsSkipVerify property.
In `@libs/backend-apisix-standalone/src/cache.ts`:
- Around line 7-8: The Number() conversions for the max and ttl environment
variables lack validation, allowing invalid values like NaN, zero, or negative
numbers to pass through and destabilize the cache. Add validation checks after
converting the environment variables for both max and ttl to ensure they are
positive numbers, and apply sensible clamping to keep them within acceptable
ranges (for example, max should be at least 1 and ttl should be greater than
zero). Handle cases where the conversion results in NaN or invalid values by
either using the default fallback values or raising an error to fail fast on
misconfiguration.
---
Nitpick comments:
In `@libs/backend-apisix-standalone/e2e/cache.e2e-spec.ts`:
- Line 485: The afterAll hook in the test file only clears configCache and
rawConfigCache, but does not clear the version and latestVersion cache buckets.
This allows cached state to persist between test suites, causing potential test
coupling. Add calls to clear the version and latestVersion cache buckets in the
afterAll hook on the same line, using the same pattern as the existing
cache.clear() calls for configCache and rawConfigCache to ensure all cache
buckets are fully reset after each test suite.
- Around line 510-527: The test for the bypassCache feature in the
dumpConfiguration call currently only validates AXIOS_DEBUG event emissions but
doesn't assert the TASK_START event that was introduced for bypass invalidation.
Add a listener for the TASK_START event on the freshBackend instance similar to
the existing AXIOS_DEBUG listener, initialize a counter to track TASK_START
emissions, and then add an assertion to verify that TASK_START was emitted at
least once during the dumpConfiguration call to ensure the bypass invalidation
contract is properly tested.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: c785b6c6-83cf-4a62-939d-6bcbe4f1186b
⛔ Files ignored due to path filters (1)
pnpm-lock.yamlis excluded by!**/pnpm-lock.yaml
📒 Files selected for processing (6)
apps/cli/src/server/sync.tslibs/backend-apisix-standalone/e2e/cache.e2e-spec.tslibs/backend-apisix-standalone/package.jsonlibs/backend-apisix-standalone/src/cache.tslibs/backend-apisix-standalone/src/index.tspnpm-workspace.yaml
Description
Replace the cache in the apisix-standalone backend with an LRU cache that supports TTL.
By default, the cache holds 16 cache keys with a TTL of 60 minutes.
The LRU cache supports overriding its size and TTL using the
ADC_APISIX_STANDALONE_CACHE_MAXandADC_APISIX_STANDALONE_CACHE_TTL_MSenvironment variables.Additionally, this backend supports active cache invalidation. To enable this, set
bypassCachein thetask.optsof the sync API. This behavior first deletes the local cache, which triggers the cache initialization process and repopulates the cache. As a result, the sync API will perform a diff against the latest cache.Fix: apache/apisix-ingress-controller#2774 (comment)
Checklist
Summary by CodeRabbit
Release Notes
New Features
bypassCacheflag for sync/dump requests to force fresh configuration retrieval by invalidating the relevant cached entry first.TASK_START-level logging for backend task events (event name and request id).Tests
Chores