Repository navigation
fix(index): keep the shuffler scratch directory alive for the whole build - #8993
Conversation
…uild prepare_vector_segment_build created the shuffler's scratch directory as a local TempStdDir and returned only the shuffler, so the guard dropped when the function returned: the directory was removed before the build wrote into it (the writer recreated it) and nothing ever cleaned it up afterwards, leaking one scratch directory per index build. Return the guard alongside the shuffler so callers hold it until the build finishes. The incremental build path already keeps its guard in the enclosing function scope and is unchanged.
zhangyue19921010
left a comment
There was a problem hiding this comment.
Overall LGTM. But can we streamline the test cases?
|
Dropped The diff is +125/-22 now instead of +202/-22, and the test module's import block is back to untouched. |
|
|
Not as a separate test. It only existed as the child process payload, so I folded it into The child's filter comes from Negative control still holds: with the guard bound as bare |
There was a problem hiding this comment.
✅ Gate recommendation: approve.
The test-only follow-up preserves the production fix and folds the child-process payload into the end-to-end scratch-directory regression. Both shuffler paths now exercise cleanup without retaining a separate ignored child test, and no material risk remains.
|
Thank you @zhangyue19921010 |
Every IVF index build leaves a `.tmp*` directory behind holding a shuffled copy of the vector column — tens of MB apiece, one per `funes index` run that rebuilds the vector index. On a machine that does not clear $TMPDIR they accumulate without bound; #175 reports 25.5 GB over five days. lance's `prepare_vector_segment_build` creates the scratch directory, hands the shuffler its *path*, and returns only the shuffler, so the `TempStdDir` guard drops on return. strace shows the directory removed while still empty, then recreated by the shuffle writer through `create_dir_all(parent)` — from then on nothing owns it. lance fixed this in 13.0.0 (lance-format/lance#8993, merged a day after v12.0.0-rc.1 was cut); 11 and 12 both leak, so funes has to clean up after them until the pin can move. Sweep on the way in, not on the way out: funes' memory lock is per-FUNES_HOME and does not cover another lance process on the same $TMPDIR, so the sweep must never touch a directory a build could still be filling. One that is over an hour old and holds nothing but the four shuffle files is settled — mid-write object_store's staging files sit alongside them. Steady state is one directory. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]>
) ## Problem The v3 shufflers hold only `output_dir: Path`, so whoever creates the shuffle scratch directory has to keep a `TempStdDir` guard alive for the whole build. #8993 fixed one instance of that going wrong, but the invariant it left behind is a naming convention: `let (.., _) = prepare_vector_segment_build(..)` compiles, binds nothing, drops the guard at the end of the statement, and the leak is back. `#[must_use]` does not help, because binding to `_` counts as a use. Three build paths each kept their own guard alive by hand, and the regression test reaches only one of them. Fixes #9264. ## What this changes `IvfShuffler` and `TwoFileShuffler` can now own the directory they shuffle into, the way the legacy shuffler already does with `owned_temp_dir`. A private `ScratchDir` pairs the directory with its guard in one constructor, so outside the shuffler module a shuffler cannot be handed a guard for some directory other than the one it writes into. `create_owned_ivf_shuffler` creates the directory and hands it over; `create_ivf_shuffler` keeps its signature for the two call sites that shuffle into a subdirectory of a staging area they clean up themselves. The readers get a clone of the guard as well, not just the shufflers. `IvfIndexBuilder` declares `shuffler` before `shuffle_reader`, so on drop the reader outlives the shuffler and can be the last thing that still needs the directory. Without that clone, dropping the shuffler first would delete the partition files before the reader reads them. `prepare_vector_segment_build` returns its original 4-tuple again, and the incremental and optimize paths no longer build a `TempStdDir` of their own. No existing public item of `lance-index` changes signature; `create_owned_ivf_shuffler` is new and takes no `tempfile` type, and the builder methods that do are private. ## Test plan `test_owned_scratch_dir_outlives_the_shuffler` is an `rstest` over both shufflers. Each case drops the shuffler, asserts the directory is still there and reads a partition through the reader, then drops the reader and asserts the directory is gone. Negative control: with the readers' guard clone replaced by `None`, both cases fail on the first assertion, including the `IvfShuffler` case that nothing exercised before. `test_shuffle_scratch_dir_is_not_leaked` from #8993 is unchanged and covers the fresh `create_index` build end to end. The incremental, optimize and distributed paths still have no leak test of their own; they now go through the same `create_owned_ivf_shuffler` that the shuffler-level test pins. Not covered, and worth knowing: `create_shuffler` selects between the two shufflers on `LANCE_LEGACY_SHUFFLER`, which nothing in the repo sets, so that branch itself is still driven only by the default arm. --------- Co-authored-by: Xuanwo <[email protected]>
lance 13 holds the IVF shuffler's scratch directory for the whole build (lance-format/lance#8993), so an index build leaves nothing in $TMPDIR and the sweep has nothing left to reclaim. The sweep goes, with its unit test, the tripwire test (which fails on lance 13, as designed) and the Cargo.toml note that asked for this. Co-Authored-By: Claude Opus 5.5 <[email protected]>
lance 13 holds the IVF shuffler's scratch directory for the whole build (lance-format/lance#8993), so an index build leaves nothing in $TMPDIR and the sweep has nothing left to reclaim. The sweep goes, with its unit test, the tripwire test (which fails on lance 13, as designed) and the Cargo.toml note that asked for this. Co-Authored-By: Claude Opus 5.5 <[email protected]>
Problem
prepare_vector_segment_buildcreates the shuffle scratch directory as a localTempStdDirand returns only the shuffler, which carries just the directory path. The guard drops when the helper returns, so the directory is removed before the build writes into it. The shuffle writer recreates it, and from then on nothing owns it. Every vector index build leaks one scratch directory, and each one holds a shuffled copy of the vector column.Fixes #8992.
What this changes
The helper returns the guard alongside the shuffler, and both callers hold it until the build finishes. The incremental build path and the optimize path already keep their guards in the enclosing function scope and are untouched.
#6980 fixed the same thing for the legacy IVF_HNSW partition staging directory.
Test plan
test_shuffle_scratch_dir_is_not_leakedruns two real index builds in a child process withTMPDIRpointed at an isolated directory, then asserts no scratch directory survives. Negative control: with the guard bound as bare_at the local call site, it fails and lists both directories, one per build. The builds go throughcreate_index, so the test covers the local path; the distributed path needs precomputed centroids and is not exercised by it.End to end, one index build with
TMPDIRpointed at an empty directory:abcd800cd.tmpXXXXXX/withshuffle_data.lance,shuffle_data.spill,shuffle_offsets.lance,shuffle_offsets.spill