rahulsmahadev opened a new pull request, #4527:
URL: https://github.com/apache/iceberg-python/pull/4527

   # Rationale for this change
   
   Bug: with `io.manifest.cache-enabled=true` (#4335), scan planning can fail. 
Planning reads manifests on the `ExecutorFactory` thread pool, and every 
manifest and manifest list read of one `FileIO` goes through its 
`ContentCache`, an `OrderedDict` without a lock. `put()` iterates the entries 
in `_purge_expired()` while other threads insert or evict entries, and `get()` 
can call `move_to_end()` on an entry another thread just evicted:
   
   ```python
   catalog = SqlCatalog("default", uri="sqlite:///catalog.db", 
warehouse="file:///tmp/warehouse", **{"io.manifest.cache-enabled": "true"})
   table = catalog.load_table("db.events")  # 200 manifests; manifest merging 
disabled while creating them
   table.scan().plan_files()
   # RuntimeError: OrderedDict mutated during iteration
   # or KeyError: '<manifest path>' from move_to_end()
   ```
   
   It depends on thread timing: in a local 200-manifest repro with 
`sys.setswitchinterval(1e-6)`, 10 of 10 cold scans failed with the cache 
enabled and none with it disabled. This is the same error #1905 reported for 
the old manifest cache, fixed by adding a lock in #2555.
   
   Fix: guard `get()`, `put()` and `clear()` with one `threading.Lock` per 
cache. Storage reads stay outside the lock (`_open_metadata_file` reads the 
file between `get()` and `put()`). Pickling takes a snapshot of the entries and 
the byte count under the lock, and `_TransientStateMixin` (#4357) recreates the 
lock after unpickling, so cache-enabled FileIOs still pickle with their cached 
bytes.
   
   Java plans manifests on `ThreadPools.getWorkerPool()` and reads manifests 
and manifest lists through `ManifestFiles.contentCache(io).tryCache(input)`; 
its `ContentCache` wraps a Caffeine cache, which is thread-safe. PyIceberg has 
the same flow (`plan_manifest_entries` on `ExecutorFactory`, then 
`_open_metadata_file`); this PR covers the cache.
   
   Remaining differences from Java:
   - Two threads that miss the same file at once both read it; Caffeine's 
`cache.get(location, loader)` loads it once.
   - Not changed here: entries expire a fixed time after they are written 
(Java: after the last access), and `put()` checks every entry for expiry.
   
   ## Are these changes tested?
   
   Yes, added `test_cache_is_thread_safe`, 
`test_cache_is_consistent_when_pickled_during_updates` and 
`test_cache_is_kept_when_file_io_is_pickled` to 
`tests/io/test_content_cache.py`. The first reads through one small, 
short-lived cache from two threads while a third clears it; it fails on main 
with `RuntimeError: OrderedDict mutated during iteration` and passes with this 
change. The second pickles the cache while another thread updates it, and 
checks that every copy's byte count matches its entries; without the snapshot, 
copies are inconsistent in every run. The third pickles a cache-enabled 
`PyArrowFileIO` and `FsspecFileIO`; it passes on main too and fails if the lock 
is pickled (`TypeError: cannot pickle '_thread.lock' object`).
   
   ## Are there any user-facing changes?
   
   No, the manifest content cache from #4335 is not released yet.
   
   This pull request and its description were written by Isaac.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to