Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement a file handle cache #54

Merged
merged 7 commits into from
Apr 21, 2024
Merged

Implement a file handle cache #54

merged 7 commits into from
Apr 21, 2024

Conversation

nsmith-
Copy link
Member

@nsmith- nsmith- commented Mar 5, 2024

To prevent constantly opening and closing in calls to cat_file, introduce a fs-level cache of open read-only file handles. If a file handle needs to be opened for writing, we first close any read-only handles to the same path.
Since fsspec by design keeps AbstractFileSystem objects alive indefinitely through a metaclass, we implement a TTL mechanism to expire our client's read-only handles so that other clients may have a chance of succeeding in opening the file for writing. If not, other clients may encounter:

[3003] Output file ... is already opened by 1 reader; open denied.

@lgray
Copy link
Contributor

lgray commented Mar 6, 2024

Would it be reasonable to make this a cache with a TTL and/or the cache invalidates on being serialized?

@lgray
Copy link
Contributor

lgray commented Mar 6, 2024

@nsmith-
Copy link
Member Author

nsmith- commented Mar 6, 2024

TTL would be good, yes. In the case a loop is available, it should be easy to implement the expiry watchdog with an asyncio.sleep.
We can't use off-the-shelf stuff from cachetools because it is not async-friendly. I found aiocache but it seems to be targeting something else.

nsmith- added 3 commits March 6, 2024 13:44
To prevent constantly opening and closing in calls to cat_file.
Also, fix up the vector read size code to work correctly

lint
@nsmith- nsmith- force-pushed the filehandle_cache branch from 5abaf07 to a209f38 Compare March 6, 2024 19:44
@nsmith- nsmith- marked this pull request as ready for review March 6, 2024 21:00
@nsmith-
Copy link
Member Author

nsmith- commented Mar 6, 2024

@chrisburr if you can also give this a review that'd be good

@lgray
Copy link
Contributor

lgray commented Mar 6, 2024

FWIW you can probably be much more aggressive with the default TTL, even if it's 5s you're not spamming the server with open requests at that point. Or do studies indicate this is not the case?

@chrisburr
Copy link
Member

5 seconds is very short, I've not

  • The XRootD defaults are extremely long
  • I regularly interact with servers where opening a file takes more than 5 seconds
  • It's reasonable to request a range that takes more than 5 seconds to download

As it stands it looks like a file could be closed while a read is actively running? Maybe you need a semaphore around the file handle so it's only eligable for removal when the count of active users is zero.

@nsmith-
Copy link
Member Author

nsmith- commented Mar 7, 2024

As it stands it looks like a file could be closed while a read is actively running? Maybe you need a semaphore around the file handle so it's only eligable for removal when the count of active users is zero.

If we require the ttl value to be greater than the timeout, and the LRU max_size=None, I think this is prevented. This isn't currently the case.
We can also add a semaphore, e.g. returning tuple[client.File, asyncio.Semaphore] to be used in all async operations and hold the file alive. But perhaps xrootd can do this for us: in the case of attempting to close while reading, either the close or the read operation should fail. If the close operation fails we can just ignore such failures and let the _prune_cache attempt the close again later. I'll write a test for it in a bit and try it out.

@nsmith-
Copy link
Member Author

nsmith- commented Mar 7, 2024

Ok, neither operation fails. I guess it just completes the read. In this case there shouldn't be any issue.

@nsmith-
Copy link
Member Author

nsmith- commented Apr 1, 2024

@lobis (and/or @chrisburr ) can you review this?

@lgray
Copy link
Contributor

lgray commented Apr 16, 2024

repinging this

@lobis (and/or @chrisburr ) can you review this?

@lgray lgray self-requested a review April 21, 2024 14:38
@lgray lgray merged commit 8782798 into main Apr 21, 2024
7 checks passed
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.

Performance impact of opening a new XRootD File in every cat operation (TBasket in Uproot)
3 participants