Skip to content
This repository was archived by the owner on Oct 10, 2025. It is now read-only.

Conversation

@1amageek
Copy link

@1amageek 1amageek commented Oct 8, 2025

Summary

This PR implements parallel loading support for HNSW indexes in the vector extension, addressing critical thread safety and lifecycle management issues.

Key Changes

  1. Thread-Safe HNSW Index Loading

    • Added background thread lifecycle management with proper synchronization
    • Implemented DatabaseLifeCycleManager to prevent TOCTOU race conditions
    • Fixed transaction management for background loading threads
  2. Catalog Entry Propagation

    • Updated Index::load() interface to accept CatalogEntry* parameter
    • Propagated catalog entries through extension framework
    • Enables proper metadata access during index initialization
  3. Transaction Timestamp Persistence

    • Added lastTimestamp persistence in database header
    • Ensures transaction consistency across database restarts
    • Prevents timestamp conflicts during recovery
  4. Index Loading Synchronization

    • Added index loading wait mechanism for INSERT and COPY FROM operations
    • Implemented synchronous HNSW loading during WAL replay
    • Prevents data corruption from concurrent operations
  5. Bug Fixes

    • Fixed OverflowFile checkpoint corruption when no data is written
    • Fixed catalog entry handling to prevent infinite loops
    • Added proper error handling and validation

Test Coverage

  • Added parallel_loading_test.cpp for HNSW parallel loading scenarios
  • Added overflow_file_checkpoint_test.cpp for checkpoint edge cases
  • All existing tests passing

Related Issues

Fixes a bug where OverflowFile::checkpoint() unconditionally allocated a header page even when no data had been written, causing PrimaryKeyIndexStorageInfo corruption and database reopen failures.

**Root cause:**
When creating a VectorIndex without inserting data, OverflowFile::checkpoint() allocated a page unnecessarily, setting overflowHeaderPage to a valid page index instead of INVALID_PAGE_IDX.

**Fix:**
Skip checkpoint when headerChanged == false, following the same design pattern as NodeTable and RelTable. The headerChanged flag is only set to true when actual string data (>12 bytes) is written via OverflowFileHandle::setStringOverflow().

**Test coverage:**
Added overflow_file_checkpoint_test.cpp with 5 test cases documenting the bug scenario and verifying correct behavior.
- Add catalogEntry parameter to index_load_func_t signature
- Update PrimaryKeyIndex::load() to accept catalogEntry (unused)
- Update IndexHolder::load() to pass catalogEntry to load functions
- Prevents recursive catalog lock acquisition during parallel loading
- All index types now have consistent load() signature
- OnDiskHNSWIndex::load() now uses catalogEntry instead of catalog lookup
- FTSIndex::load() now uses catalogEntry instead of catalog lookup
- Implement parallel loading in vector_extension with thread pool
- Add cancellation support for background index loading
- Eliminates catalog access during index loading
- Add vectorIndexLoaderThread to Database for proper thread ownership
- Implement startVectorIndexLoader() and joinVectorIndexLoaderThread()
- Add callback API for vector index loading completion notification
- Add cancellation support with vectorIndexLoadCancelled flag
- Add backgroundThreadStartMutex to prevent TOCTOU race
- Move extension autoload after WAL recovery completes
- Ensures clean shutdown without use-after-free
- Serialize/deserialize lastTimestamp in DatabaseHeader
- Restore TransactionManager state after checkpoint recovery
- Move extension autoload after WAL recovery completes
- Prevents timestamp conflicts when reopening database
- Maintains backward compatibility with older database files
- Add null check for catalog entries after version chain traversal
- Add MAX_ITERATIONS limit to prevent infinite loops in version chains
- Fix catalog entry validation in getEntries()
- Add null guard in transaction catalog entry creation
- Add vector_parallel_loading_test to CMake
- Change dlopen to RTLD_GLOBAL for proper symbol resolution
- Add getExtensionCatalogTransaction() and getExtensionCheckpointTransaction()
- Fix extension function registration to use proper transaction contexts
- Update OnDiskHNSWIndex::load() signature in header
- Update FTSIndex::load() signature in header
- Consistency with implementation changes
- FTSExtension now passes catalogEntry to index.load()
- Fix includes for extension.h in FTS and HNSW implementations
- Update extension CMakeLists for proper library dependencies
- Mark notifyVectorIndexLoadComplete as KUZU_API
- Mark startVectorIndexLoader as KUZU_API
- Mark TransactionManager public methods as KUZU_API
- Required for proper symbol export in shared libraries
This commit ensures HNSW indexes are loaded synchronously during WAL
replay (recovery) to prevent race conditions where WAL records access
indexes before background loading completes.

Changes:
- Add isRecoveryInProgress flag to DatabaseLifeCycleManager
- VectorExtension detects recovery mode and loads indexes synchronously
- Extract loadHNSWIndexesSync() function for both sync and async paths
- Add index loaded assertions in NodeTable insert/delete operations
- Add parallel_loading_test to verify recovery behavior

Fixes race condition where NodeInsertionRecord/NodeDeletionRecord could
access unloaded indexes during WAL replay.
This commit adds synchronization to ensure vector indexes are loaded
before INSERT and COPY FROM operations that require index access.

Changes:
- VectorExtension: Add static-link detection for synchronous loading
  in test environments
- Binder: Wait for vector index loading in bindInsertNode()
- Binder: Wait for vector index loading in bindCopyNodeFrom()
- Add 30-second timeout with 100ms polling interval
- Graceful error handling if loading fails or times out

This prevents race conditions where INSERT/COPY operations execute
before background index loading completes in dynamic-load scenarios.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant