Skip to content

Conversation

@akshayutture-augment
Copy link

…#97529)

* dont lazy init unified storage

* Inits index when creating new resource server. Fixes trace propagation by passing span ctx. Update some logging.

* Use finer grained cache locking when building indexes to speed things up. Locking the whole function was slowing things down.

* formatting

* linter fix

* go mod

* make update-workspace

* fix workspaces check error

* update dependency owner in mod file

* wait 1 second before querying metrics

* try with big timeout, see if fixes CI. Wont fail locally.

* skips postgres integration test. Only fails in drone. Will fix later.

* put delay back to 500 ms
Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Your free trial has ended. If you'd like to continue receiving code reviews, you can add a payment method here.

@akshayutture-augment
Copy link
Author

@greptileai

@greptile-apps
Copy link

greptile-apps bot commented Nov 14, 2025

Greptile Overview

Confidence Score: 2/5

  • This PR has a critical race condition that will cause runtime panics during initialization
  • The optimization to reduce lock contention in bleve.go introduced a race condition where TotalDocs() reads the cache without synchronization while concurrent goroutines write to it. This will cause "concurrent map read/write" panics when the search index initializes.
  • Pay close attention to pkg/storage/unified/search/bleve.go - the TotalDocs() method must acquire a read lock before iterating the cache

Important Files Changed

File Analysis

Filename Score Overview
pkg/storage/unified/search/bleve.go 2/5 Moved mutex acquisition after index building to reduce lock contention, but introduced critical race condition in TotalDocs() method
pkg/storage/unified/resource/search.go 4/5 Fixed context propagation in tracing spans and improved logging, replaced incorrect logger import with proper instance logger
pkg/storage/unified/resource/server.go 5/5 Moved initialization to constructor and removed redundant Init() checks from each operation, improving startup reliability and reducing overhead

Sequence Diagram

sequenceDiagram
    participant Constructor as NewResourceServer
    participant Server as server.Init()
    participant Search as searchSupport.init()
    participant Bleve as bleveBackend
    participant Storage as SQL Backend
    participant Watcher as Event Watcher

    Note over Constructor: Initialization moved to constructor
    Constructor->>Server: Call Init(ctx)
    Server->>Search: search.init(ctx)
    
    Note over Search: Parallel index building
    Search->>Storage: GetResourceStats(ctx)
    Storage-->>Search: Return stats
    
    par For each resource type
        Search->>Search: build(ctx, nsr, size, rv)
        Search->>Bleve: BuildIndex(ctx, key, ...)
        Note over Bleve: Lock moved after build
        Bleve->>Bleve: Create index & build documents
        Bleve->>Bleve: Lock cache & add index
        Bleve-->>Search: Return index
    end
    
    Note over Search: RACE: TotalDocs called here
    Search->>Bleve: TotalDocs()
    Note over Bleve: Iterates cache WITHOUT lock
    Bleve-->>Search: Return count
    
    Search->>Storage: WatchWriteEvents(ctx)
    Storage-->>Search: Event channel
    Search->>Watcher: Start goroutine
    
    Server->>Server: initWatcher()
    Server-->>Constructor: Return initialized server
    
    Note over Server: Operations no longer call Init()
    Server->>Server: Create/Update/Delete/Read/List/Watch
    Note over Server: Removed redundant Init() checks
Loading

Copy link

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

Additional Comments (1)

  1. pkg/storage/unified/search/bleve.go, line 144-154 (link)

    logic: Race condition: TotalDocs() iterates over b.cache without holding a lock, while BuildIndex modifies the cache on line 137-139. This can cause concurrent map read/write panics when called during initialization.

5 files reviewed, 1 comment

Edit Code Review Agent Settings | Greptile

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.

3 participants