Skip to content

lib/fs: Fix (remove) casefs caching - #9685

Closed
calmh wants to merge 2 commits into
syncthing:mainfrom
calmh:jb/fixmtimefs
Closed

calmh wants to merge 2 commits into
syncthing:mainfrom
calmh:jb/fixmtimefs

Conversation

@calmh

@calmh calmh commented Sep 6, 2024 •

Copy link
Copy Markdown
Member

@imsodin 👋

This is a somewhat unclean patch as I'm doing a couple of things at once, but we can sort that out in time. The main thing is that I want a discussion around the caching of caseFs instances. That's what causes the problem in #9677, because at the end of the chain in NewFilesystem we try to wrap it in a caseFs, but then the caseFs registry pulls a switcheroo on us and gives us a different wrapped filesystem that doesn't have an mtimeFs in the stack. This happens because we only sometimes create with an mtimeFs -- namely when we have a FileSet (database) available.

Now, I completely forget why we thought we needed to cache caseFs instances. As far as I can tell we create essentially two "kinds" of FS instances -- very short lives ones here and there when we want to check folder health and free space and such, and these don't include mtimeFs in the stack, or the long lived one that is set in the folder instance and shares the folder lifetime.

The current cache manager does one other thing though and that is it periodically clears the cache inside the cacheFs. We could potentially do that some other way, like the folder itself could explicitly do it when it completes a scan/pull cycle for example.

@imsodin

imsodin commented Sep 6, 2024

Copy link
Copy Markdown
Member

Oh it is the case FS cache after all...
Thanks for finding this! I completely missed that we create a filesystem sometimes without the mtime layer.

Now, I completely forget why we thought we needed to cache caseFs instances.

Same. And the PR introducing it doesn't help in that regard (#6974), because the main change and all explanation is about improving the file caching. This might indicate that the filesystem caching is related, which would make sense if we recreated the filesystem a lot, as we then also drop cache. However we keep the filesystem in the folder, and that's where most of the filesystem interaction comes from. The only place that can also cause quite a few filesystem interactions that creates a new filesystem every time, is model.Request. If we want to keep that behaviour, lets make it a realCaser registry, reusing only that but always creating a new filesystem wrapping the given filesystem.

The current cache manager does one other thing though and that is it periodically clears the cache inside the cacheFs. We could potentially do that some other way, like the folder itself could explicitly do it when it completes a scan/pull cycle for example.

Keeping a realCaser only registry (option mentioned above) would mean we can keep the cache clearing there.
The folder could do it for it's own filesystem, but it can't for all the other transient ones, right?
Another option would be to check and drop in every case fs method. It's a bit wasteful as we keep cache potentially for a long time between operations, but it's small so that seems ok.

Comment thread lib/fs/casefs.go
}

func (f *caseFilesystem) Rename(oldpath, newpath string) error {
f.dropCache(oldpath, newpath)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's the reason for this change, i.e. why must the cache not apply to renames?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this is not related to the real issue I was solving, this is me futzing about. I changed the cache time from one second to 15 seconds to make the cache more effective, but I was thinking something that case-only renames would be especially sensitive to this, but really, if that's the case we'll just retry so I think there's no real reason now that I look at it in broad daylight

@calmh

calmh commented Sep 6, 2024

Copy link
Copy Markdown
Member Author

The only place that can also cause quite a few filesystem interactions that creates a new filesystem every time, is model.Request.

really this should probably just ask the folder runner for the filesystem to use

@calmh

calmh commented Sep 6, 2024

Copy link
Copy Markdown
Member Author

@imsodin I think we need to do some minor re-architecting here. The current caseFs caching is bad and should go away, it's an enormous footgun that's evidently really hard to troubleshoot, it took me like two hours staring at the code and experimenting to even find it. Maybe or maybe not we should cache the realCaser and maybe we should think about how we drop that cache.

I think I'll do a quick patch release with the problematic change reverted, and we can fix this properly in good time for next release?

@imsodin

imsodin commented Sep 6, 2024

Copy link
Copy Markdown
Member

I think I'll do a quick patch release with the problematic change reverted, and we can fix this properly in good time for next release?

Thanks a lot for doing that, definitely good to fix the immediate issue and buy us time!

really this should probably just ask the folder runner for the filesystem to use

That sounds good. I'd like to make that a bit clearer in terms of available methods to get filesystems for a folder. It's not that great if one just needs to know that getting a new filesystem could be costly. It's however very non-obvious to me how to do that - FolderConfiguration.Filesystem is very pervasive/used all over the place. Centralising getting/creating the filesystem to get a handle on this would be tedious. However maybe I am overthinking this and only model is relevant, the rest being just minor/one-off filesystem interactions.

I think we need to do some minor re-architecting here. The current caseFs caching is bad and should go away, it's an enormous footgun that's evidently really hard to troubleshoot, it took me like two hours staring at the code and experimenting to even find it. Maybe or maybe not we should cache the realCaser and maybe we should think about how we drop that cache.

For pretty bad reasons I currently tend towards keeping things pretty close to how things are, just cache the realCaser only, not the filesystem. Just because then we don't need to think about how to drop caches and not leaking maps/routines.

@imsodin

imsodin commented Sep 7, 2024

Copy link
Copy Markdown
Member

I gave the option "only cache the cache" a go here, together with a reproducer unit test for the scenario you identified (did fail before, doesn't now): #9680

@calmh calmh closed this Sep 11, 2024
@calmh
calmh deleted the jb/fixmtimefs branch May 26, 2025 13:42
@st-review st-review added the frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion label Jul 1, 2026
@syncthing syncthing locked and limited conversation to collaborators Jul 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

frozen-due-to-age Issues closed and untouched for a long time, together with being locked for discussion

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants