OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

[Bug] Cold start on memory-constrained hosts: get_delta_data_after_ts materializes the whole delta list into RAM and can hang/OOM

Open livechenjt-prog opened this issue 2 months ago • 4 comments

Summary

On a memory-constrained host (verified on ~1.8 GB RAM Alibaba Cloud ECS), cold-starting the server OOMs / hangs after the vectordb accumulates a large delta table (verified at ~2.8 GB of pending delta bytes). Root cause is not malformed records (that's tracked in #1892) but volume: get_delta_data_after_ts materializes every delta record into memory at once.

Reproduction

  1. Run the local vectordb backend until the LevelDB delta table grows large (our prod had ~2.8 GB pending delta).
  2. Restart the server on a host with limited RAM (~1.8 GB).
  3. Startup: cold start takes ~6.5 min and the process is at risk of OOM-kill / indefinite hang while _recover replays deltas.

Root cause

store_manager.py get_delta_data_after_ts (at v0.4.20, line 265):

def get_delta_data_after_ts(self, ns_ts: int) -> List[DeltaRecord]:
    delta_kv_list = self.storage.seek_to_end(str(ns_ts), StoreManager.DeltaTable)
    delta_list = [DeltaRecord.from_bytes(data=data[1]) for data in delta_kv_list]  # <-- materializes the WHOLE delta list
    return delta_list

seek_to_end returns an iterator, but the list-comprehension forces every DeltaRecord into RAM before _recover streams them into upsert_data. On a large delta table this is the same peak-memory spike whether or not individual records are valid. This is orthogonal to #1892: even with zero malformed records, a large delta table + low RAM OOMs during playback.

Proposed fix (lazily stream instead of materialize)

The fix is a lazily-yielding generator — keep only one DeltaRecord in memory at a time. This is backward-compatible because every downstream caller (_recover, get_delta_data_after_ts) consumes it via iteration / list(). We have been running this in production for 3+ days across 3 cold-start regression cycles without issue:

def get_delta_data_after_ts(self, ns_ts: int) -> Iterator[DeltaRecord]:
    """Lazily deserialize delta records so a large delta table can be replayed on
    memory-constrained hosts without materializing the whole list."""
    for _, bytes_data in self.storage.seek_to_end(str(ns_ts), StoreManager.DeltaTable):
        yield DeltaRecord.from_bytes(data=bytes_data)

Callers that need a list can wrap with list(...); native iteration gets the streaming benefit for free.

Environment

  • openviking 0.4.19 (same code at 0.4.20 — verified the wheel line 265 unchanged)
  • local vectordb backend
  • 1.8 GB RAM ECS, ~2.8 GB delta table
  • cold start ~6.5 min with the lazy patch; vulnerable to OOM/hang without it

Related

  • #1892 (malformed-record robustness) — different axis; our fix targets volume/laziness, not record validity. A lazy generator doesn't fix #1892's corrupt-record crash, and #1892's fix doesn't address peak-memory. Both are worth landing.

Happy to open a PR with the lazy generator if maintainers agree this axis is worth supporting.

livechenjt-prog avatar Sep 15 '26 13:09 livechenjt-prog

Thanks for reporting this! We agree that reducing memory usage during recovery is worth addressing.

Would you be willing to contribute a fix? We would very much welcome it! Could you first describe your proposed approach in more detail here so we can discuss it before implementation?

qin-ctx avatar Sep 17 '26 03:09 qin-ctx

And @zhoujh01 will check it.

qin-ctx avatar Sep 17 '26 03:09 qin-ctx

Thanks for the warm reception and for assigning @zhoujh01 to review this. Here is the full proposed approach for your review before I open a PR.

Proposed change (single function, backward-compatible)

Replace the eager list-comprehension in StoreManager.get_delta_data_after_ts with a lazily-yielding generator, so only one DeltaRecord is held in memory at a time while _recover replays a large delta table.

Before (v0.4.20, store_manager.py L265):

def get_delta_data_after_ts(self, ns_ts: int) -> List[DeltaRecord]:
    delta_kv_list = self.storage.seek_to_end(str(ns_ts), StoreManager.DeltaTable)
    delta_list = [DeltaRecord.from_bytes(data=data[1]) for data in delta_kv_list]
    return delta_list

After (lazy generator):

def get_delta_data_after_ts(self, ns_ts: int) -> Iterator[DeltaRecord]:
    for _, bytes_data in self.storage.seek_to_end(str(ns_ts), StoreManager.DeltaTable):
        yield DeltaRecord.from_bytes(bytes_data)

seek_to_end already returns an iterator; the list-comprehension was what forced the entire delta table into RAM before _recover streamed it. The generator keeps one record in memory. This mirrors the existing lazy pattern already in the file (iter_all_cands_data).

Why this is safe

  • Backward compatible: every downstream caller consumes it by iteration (_recover) or list(...). I verified both call-sites of get_delta_data_after_ts in 0.4.19/0.4.20 iterate it; none require a List type at the call boundary beyond iterating.
  • Orthogonal to #1892: #1892 is malformed-record robustness; this is volume/peak-memory (verified on prod with zero malformed records). The two fixes are complementary, both worth landing.
  • Does not touch the partial-cleanup/delete path (delete_delta_data_before_ts), which legitimately needs the full list to build delta_keys for deletion — that's a different operation from recovery playback.

Production validation (3+ days, 3 cold-start regression cycles)

Deployed as a local patch on our 1.8 GB RAM Alibaba Cloud ECS with a ~2.8 GB pending delta table:

Metric Without patch With lazy patch
Cold start OOM-kill / indefinite hang (>10min, port never binds) Completes in ~6.5 min, port binds, service healthy
Peak RSS during _recover Spikes toward OOM Steady, swap stays ~1 GB
Post-startup — 3 rounds of write→read E2E all passed; /health healthy, /ready ok

Clean-reinstall / version upgrade on 0.4.20 still shows the eager list at the same line, so the fix is not yet upstream.

Next step

If this approach looks right, I'll open a PR implementing exactly this (single-function change + a regression note). Happy to extend it per your feedback — e.g. if you'd prefer the helper be wrapped for partial-list semantics, or want a note/migration entry, I can fold that in before opening.

livechenjt-prog avatar Sep 17 '26 14:09 livechenjt-prog

Opened a PR for this: #5221.

One update on the approach after implementing it on the reporting host: the lazy-generator variant described above is not sufficient on its own. It removes the delta_list comprehension, but get_delta_data_after_ts still calls StoreEngineProxy.seek_to_end, whose seek_range call does list(self._backend._store_seek_range(...)) — the whole encoded delta table is still materialized before the first record is yielded. On this host the delta table grew from ~2.8 GB (when this issue was filed) to ~5.3 GB, and the cold start hung again with the lazy generator in place: 8081 never bound for 28 min, swap 2.3 GB, py-spy pinned at seek_to_end -> <listcomp> local_store.py:199.

The PR instead routes the replay through the bounded paging that already backs iter_all (seek_range_page, 1024 rows / 32 MiB per page): the page loop is extracted into _iter_page_scan, iter_all and a new iter_seek_to_end are thin delegations over it, and get_delta_data_after_ts yields page by page.

Same host, after the change: cold start 2 min 06 s to /health, swap peak 56 MiB, 3/3 write-read-delete E2E rounds clean, vector count preserved (13109 vs 13114 before restart).

Happy to adjust the shape if you would rather keep seek_to_end as the only entry point or handle the delta table differently.

livechenjt-prog avatar Sep 20 '26 07:09 livechenjt-prog