Skip to content

lib/db, lib/model: Keep need stats in metadata (ref #5899) - #6413

Merged
calmh merged 21 commits into
syncthing:masterfrom
imsodin:db/allNeed-sep
May 11, 2020
Merged

calmh merged 21 commits into
syncthing:masterfrom
imsodin:db/allNeed-sep

Conversation

@imsodin

@imsodin imsodin commented Mar 15, 2020

Copy link
Copy Markdown
Member

This is the "main thing" from which I separated #6411 and #6412 (still in this PR as the first two commits). I also kept the rest as three commits for now, as they address separate issues and thus makes reviewing potentially easier - they should still be merged together in the end.

Purpose

The purpose of this PR is to get rid of the frequent db access due to folder summaries/API completion queries. These require the global list and unmarshalling lots of stuff. I added a new metadata flag for needed files. Thus when a file is added to the db, that flag is updated and the summary/db query can just directly access those values from memory. Only if the actual list of needed files is required (i.e. user opens the list in UI) the db needs to be accessed.

Testing

I extended tests in #6402 to cover more ground on "needs". And I now extended all these tests to look at both the metadata and db when checking needs.

I have also done some manual testing a while ago and before merging want to run this on my main setup.

@calmh

calmh commented Mar 15, 2020

Copy link
Copy Markdown
Member

Zomg

Comment thread lib/db/schemaupdater.go Outdated
// it will get recreated later in normal operations.
meta := newMetadataTracker()
metaErr := meta.fromDB(db.Lowlevel, []byte(folder))
meta.resetAll(protocol.GlobalDeviceID) // Is rewritten in updateGlobal

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.

I believe this is wrong: Updating global will do nothing except updating the Deleted field of in global list entries, as the versions won't change (thus no change in need or global counts). I probably need to not touch global and recalc needs only. And I definitely want to write a test for the transition :)

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.

It is good (wrote the test) - a few lines above I empty the global list (because it wouldn't do anything otherwise, not even update the deleted field as the version doesn't change).

@calmh

calmh commented Mar 16, 2020

Copy link
Copy Markdown
Member

Does this transform every file put into n gets, one for each device in the setup? So if I have a thousand devices, every update becomes a thousand reads plus, potentially, a thousand puts of updated metadata?

@imsodin

imsodin commented Mar 16, 2020

Copy link
Copy Markdown
Member Author

Database access stays the same, max two: The new and old global file (and usually only one, as the inserted one is already the global one in most cases). What changes is that now the VersionList, that is anyway loaded, gets checked for every connected device and the metadata updated. So when the global version gets higher, there will be need checks and metadata bumps for every device, but that's in-memory only.

But good point, need to check benchmarks, didn't do that yet.

@imsodin
imsodin marked this pull request as ready for review March 23, 2020 16:17
@imsodin

imsodin commented Mar 29, 2020

Copy link
Copy Markdown
Member Author

I added a benchmark for updating with lots of remotes: There's 10 additional remotes with half of them on the old version, the other half on the new version. Then I update another device (local or a remote) between those two sets of files (as in existing benchs). I don't see any relevant change running it for 10s. While I expected no significant change (as db operations should be much costlier than a single loop over all devices with a few comparisons and writes to memory), they seem suspiciously close:

master:

BenchmarkUpdate100Changed10Remotes
BenchmarkUpdate100Changed10Remotes-4         	14577836	      1601 ns/op	     448 B/op	      16 allocs/op
BenchmarkUpdate100ChangedRemote10Remotes
BenchmarkUpdate100ChangedRemote10Remotes-4   	14450607	      1723 ns/op	     448 B/op	      15 allocs/op

PR:

BenchmarkUpdate100Changed10Remotes
BenchmarkUpdate100Changed10Remotes-4         	14166939	      1595 ns/op	     448 B/op	      16 allocs/op
BenchmarkUpdate100ChangedRemote10Remotes
BenchmarkUpdate100ChangedRemote10Remotes-4   	15173292	      1593 ns/op	     448 B/op	      15 allocs/op

A benchmark for getting need stats before and now feels pretty pointless: Now it's looking up a struct in memory, before iterating/unmarshalling the entire global prefix of the db doing lots of checks.

@calmh

calmh commented Mar 29, 2020

Copy link
Copy Markdown
Member

A benchmark for getting need stats before and now feels pretty pointless

but that's the one that shows the awesomeness 😿

@imsodin

imsodin commented Mar 29, 2020 •

Copy link
Copy Markdown
Member Author

but that's the one that shows the awesomeness crying_cat_face

Brace for the awesomeness:

master:

BenchmarkUpdate100Changed10Remotes
BenchmarkUpdate100Changed10Remotes-4         	 7592064	      1578 ns/op	     448 B/op	      16 allocs/op
BenchmarkUpdate100ChangedRemote10Remotes
BenchmarkUpdate100ChangedRemote10Remotes-4   	 6654831	      2070 ns/op	     448 B/op	      15 allocs/op
BenchmarkNeedCount
BenchmarkNeedCount-4       		 	    1989           6158423 ns/op          906604 B/op     14100 allocs/op

PR:

BenchmarkUpdate100Changed10Remotes
BenchmarkUpdate100Changed10Remotes-4         	 7089142	      1775 ns/op	     448 B/op	      16 allocs/op
BenchmarkUpdate100ChangedRemote10Remotes
BenchmarkUpdate100ChangedRemote10Remotes-4   	 6603822	      1782 ns/op	     448 B/op	      15 allocs/op
BenchmarkNeedCount
BenchmarkNeedCount-4   				 6317196              1743 ns/op             976 B/op        11 allocs/op

@calmh calmh left a comment

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.

I'm not sure it's humanly possible to review this, the global list juggling code was already one of the more complicated pieces of machinery. Nonetheless I'll give it at an attempt to test and run this a bit :) (hang in there)

Comment thread lib/db/schemaupdater.go Resolved
@imsodin

imsodin commented Apr 16, 2020

Copy link
Copy Markdown
Member Author

Most of the global update stuff is optimisations. I thought about in a first step doing a version that just stupidly checks everything and doesn't take any shortcuts - I can look into that again.

@AudriusButkevicius

Copy link
Copy Markdown
Member

I guess this just needs a lonely evening and a large crate of wine?

@imsodin

imsodin commented Apr 30, 2020

Copy link
Copy Markdown
Member Author

I could make smaller, functional but on their own useless PRs against a feature branch to make it shorter evenings with smaller creates (or equally sized ones and funnier evenings). Essentially the first few commits here, plus hopefully i can split the last of the first ones up further.

@calmh calmh left a comment

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.

Barring the one mistake I happened to find, this seems fine. I mean, there might be a hundred new bugs in the global list juggling / need updates in the db transaction code but I'll never find them. We will have to see by seeing how this plays out and applying STRECHECKDBEVERY=1s when it seems broken.

Comment thread lib/model/model.go Resolved
Comment thread lib/db/schemaupdater.go Resolved
calmh
calmh previously approved these changes May 11, 2020
Comment thread lib/db/schemaupdater.go Resolved
calmh
calmh previously approved these changes May 11, 2020

@calmh calmh left a comment

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.

Running this on my regular desktop made everything out of sync for everyone, whereas it was all in sync before the migration:

Screen Shot 2020-05-11 at 12 34 38

@calmh
calmh dismissed their stale review May 11, 2020 10:36

lets look at that though

@calmh

calmh commented May 11, 2020

Copy link
Copy Markdown
Member

Rerunning with STRECHECKDBEVERY=1s to force metadata recalc brought it back in line...

@imsodin

imsodin commented May 11, 2020

Copy link
Copy Markdown
Member Author

The migration does not trigger a db check/meta recalc itself, but requires at least a meta recalc. Which on upgrade will happen, as schema upgrades come before the version comparison, leading to the check and recalc part. However that should only cause incorrect metadata, not out of sync - looking into that.

@imsodin

imsodin commented May 11, 2020

Copy link
Copy Markdown
Member Author

That's it, could reproduce before (d1b13b7), is working correctly now:
Running a dev build, the check/recalc on upgrade did not happen. This check recalculates the amount of needed files, which should be zero, but isn't due to the migration. When you click on the out of sync items list, it is empty, because the actually stored list of files that are out of sync is correct. Then recalculating metadata the two get in sync again.
Now the migration triggers a meta recalc regardless of upgrade or not, by deleting the metadata in db.

@calmh
calmh merged commit a94951b into syncthing:master May 11, 2020
@calmh calmh added this to the v1.6.0 milestone May 11, 2020
@imsodin
imsodin deleted the db/allNeed-sep branch May 11, 2020 13:11
@AudriusButkevicius

Copy link
Copy Markdown
Member

Is v1.6.0 rc going out tomorrow, given sudden influx of reviews?

@calmh

calmh commented May 11, 2020

Copy link
Copy Markdown
Member

That’s generally the plan yes

@AudriusButkevicius

Copy link
Copy Markdown
Member

Ok, I'll try to wrap up the reorder stuff shortly, given its close.

@calmh calmh added the api Issues or pull requests that change public APIs (config, REST) label May 12, 2020
imsodin added a commit to imsodin/syncthing that referenced this pull request May 12, 2020
imsodin added a commit to imsodin/syncthing that referenced this pull request May 12, 2020
@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 May 12, 2021
@syncthing syncthing locked and limited conversation to collaborators May 12, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

api Issues or pull requests that change public APIs (config, REST) 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.

4 participants