- 
                Notifications
    You must be signed in to change notification settings 
- Fork 749
improvement: cache validation requests for sql linter #6574
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
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
This file was deleted.
      
      Oops, something went wrong.
      
    
  
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| /* Copyright 2024 Marimo. All rights reserved. */ | ||
|  | ||
| import { LRUCache } from "@/utils/lru"; | ||
| import type { | ||
| DeferredRequestRegistry, | ||
| RequestId, | ||
| } from "./DeferredRequestRegistry"; | ||
|  | ||
| type ToKey<REQ> = (request: REQ) => string; | ||
|  | ||
| interface CachingOptions<REQ> { | ||
| toKey?: ToKey<REQ>; | ||
| maxSize?: number; | ||
| } | ||
|  | ||
| /** | ||
| * Light wrapper adding memoization and in-flight de-duplication on top of | ||
| * DeferredRequestRegistry, keyed by a string representation of the request. | ||
| */ | ||
| export class CachingRequestRegistry<REQ, RES> { | ||
| private delegate: DeferredRequestRegistry<REQ, RES>; | ||
| private toKey: ToKey<REQ>; | ||
| private cache: LRUCache<string, Promise<RES>>; | ||
|  | ||
| static jsonStringifySortKeys<T>(): ToKey<T> { | ||
| return (o: T) => { | ||
| if (typeof o !== "object" || o === null) { | ||
| return String(o); | ||
| } | ||
| return JSON.stringify(o, Object.keys(o).sort(), 2); | ||
| }; | ||
| } | ||
|  | ||
| constructor( | ||
| delegate: DeferredRequestRegistry<REQ, RES>, | ||
| options: CachingOptions<REQ> = {}, | ||
| ) { | ||
| this.delegate = delegate; | ||
| this.toKey = | ||
| options.toKey ?? CachingRequestRegistry.jsonStringifySortKeys(); | ||
| const maxSize = options.maxSize ?? 128; | ||
| this.cache = new LRUCache<string, Promise<RES>>(maxSize); | ||
| } | ||
|  | ||
| /** | ||
| * Resolve via cache if present, else delegate; de-duplicates concurrent | ||
| * requests with the same key and stores successful results in the cache. | ||
| */ | ||
| public request(req: REQ): Promise<RES> { | ||
| const key = this.toKey(req); | ||
|  | ||
| const cached = this.cache.get(key); | ||
| if (cached !== undefined) { | ||
| return cached; | ||
| } | ||
|  | ||
| const promise = this.delegate.request(req); | ||
| this.cache.set(key, promise); | ||
| return promise.catch((err) => { | ||
| this.cache.delete(key); | ||
| throw err; | ||
| }); | ||
| } | ||
|  | ||
| // Path through to the delegate | ||
| public resolve(requestId: RequestId, response: RES) { | ||
| this.delegate.resolve(requestId, response); | ||
| } | ||
|  | ||
| // Path through to the delegate | ||
| public reject(requestId: RequestId, error: Error) { | ||
| this.delegate.reject(requestId, error); | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
  
    
      
          
            73 changes: 73 additions & 0 deletions
          
          73 
        
  frontend/src/core/network/__tests__/CachingRequestRegistry.test.ts
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| /* Copyright 2024 Marimo. All rights reserved. */ | ||
| import { beforeEach, describe, expect, it, vi } from "vitest"; | ||
| import { CachingRequestRegistry } from "../CachingRequestRegistry"; | ||
| import { | ||
| DeferredRequestRegistry, | ||
| type RequestId, | ||
| } from "../DeferredRequestRegistry"; | ||
|  | ||
| vi.mock("@/utils/uuid", () => ({ | ||
| generateUUID: vi.fn().mockReturnValue("uuid"), | ||
| })); | ||
|  | ||
| describe("CachingRequestRegistry", () => { | ||
| const REQUEST_ID = "uuid" as RequestId; | ||
| let makeRequestMock = vi.fn(); | ||
| let delegate: DeferredRequestRegistry<unknown, unknown>; | ||
| let caching: CachingRequestRegistry<unknown, unknown>; | ||
|  | ||
| beforeEach(() => { | ||
| makeRequestMock = vi.fn().mockResolvedValue(undefined); | ||
| delegate = new DeferredRequestRegistry("operation", makeRequestMock); | ||
| caching = new CachingRequestRegistry(delegate); | ||
| }); | ||
|  | ||
| it("should cache successful responses for identical requests", async () => { | ||
| const req = { a: 1 }; | ||
|  | ||
| const p1 = caching.request(req); | ||
| expect(makeRequestMock).toHaveBeenCalledTimes(1); | ||
| expect(makeRequestMock).toHaveBeenCalledWith(REQUEST_ID, req); | ||
|  | ||
| // Resolve first request | ||
| delegate.resolve(REQUEST_ID, "response"); | ||
| await expect(p1).resolves.toBe("response"); | ||
|  | ||
| // Second call with equivalent request gets served from cache | ||
| const p2 = caching.request({ a: 1 }); | ||
| expect(makeRequestMock).toHaveBeenCalledTimes(1); | ||
| await expect(p2).resolves.toBe("response"); | ||
| }); | ||
|  | ||
| it("should de-duplicate in-flight requests with the same key", async () => { | ||
| const req = { q: "select *" }; | ||
|  | ||
| const p1 = caching.request(req); | ||
| const p2 = caching.request({ q: "select *" }); | ||
|  | ||
| // Only one network invocation while in-flight | ||
| expect(makeRequestMock).toHaveBeenCalledTimes(1); | ||
| expect(p1).toStrictEqual(p2); | ||
|  | ||
| // Resolve and ensure both resolve to same result | ||
| delegate.resolve(REQUEST_ID, "ok"); | ||
| await expect(p1).resolves.toBe("ok"); | ||
| await expect(p2).resolves.toBe("ok"); | ||
| }); | ||
|  | ||
| it("should not cache errors", async () => { | ||
| // First call rejects | ||
| makeRequestMock.mockRejectedValueOnce(new Error("boom")); | ||
|  | ||
| await expect(caching.request({ x: 1 })).rejects.toThrow("boom"); | ||
| expect(makeRequestMock).toHaveBeenCalledTimes(1); | ||
|  | ||
| // Next call should attempt again (not cached) | ||
| const p2 = caching.request({ x: 1 }); | ||
| expect(makeRequestMock).toHaveBeenCalledTimes(2); | ||
|  | ||
| // Resolve the second request | ||
| delegate.resolve(REQUEST_ID, "ok"); | ||
| await expect(p2).resolves.toBe("ok"); | ||
| }); | ||
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Light2Dark since
onlyParseis true in one case but false in the other, the cache is not that effective. maybe they can both betrue? or bothfalse(for just duckdb)Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, both can be false if in validate sql mode for duckdb.