Skip to content

Reduce database size by optimizing version list storage #6372

Closed

Description

@calmh

It's somewhat rare in the grand scheme of things, but there are setups with hundreds of devices in a cluster. In some cases this results in files with hundreds of entries in the version vector. This is fine and expected. In our global version list we keep for each file an array of (version vector, device, invalid). In the case where there are many devices and a long version vector we get that vector repeated for every device and this global entry can grow to hundreds of kilobytes for a single file entry.

Thing is though, in the steady state all these hundreds of version vectors are actually the same. So if we grouped the version list by version vector, with each item being (version vector, (device, invalid)...) we'd cut that by a factor hundreds.

In one database I'm looking at, the change would be from

 0x00: 1169574 items, 70491 KB keys +  5506325 KB data, 60 B +   4707 B avg,   7634 B max
 0x01:   52634 items,  3004 KB keys +  5412346 KB data, 57 B + 102829 B avg, 517727 B max
 0x02:  259848 items, 23082 KB keys +     1039 KB data, 88 B +      4 B avg,    256 B max
...
 Total 1581101 items, 98761 KB keys + 10935349 KB data.

to

 0x00: 1169574 items, 70491 KB keys + 5506325 KB data, 60 B +  4707 B avg,  7634 B max
 0x01:   52634 items,  3004 KB keys +  818664 KB data, 57 B + 15553 B avg, 56103 B max
 0x02:  259848 items, 23082 KB keys +    1039 KB data, 88 B +     4 B avg,   256 B max
...
 Total 1581101 items, 98761 KB keys + 6341667 KB data.

or about a 40% reduction in database size. Smaller setups aren't going to gain that, but it won't hurt either.

The actual file entries still contain all the copies of the version vectors, so class 0x00 is large. Theoretically we could do the same there as with the block lists and bring that down too, but that would actually hurt small setups with the extra indirection. Although we could just skip the indirection (storing it like we do now) if the version list is smaller than x, and have getFile handle both the version list being already in place or there being a hash pointer to it.

Activity

  1. assigned and unassigned on Mar 24, 2020
  2. added
    enhancement
    on Mar 24, 2020
  3. imsodin commented on May 7, 2020

    @imsodin
    Member

    I implemented a prototype to restructure the global list to save a list of versions, and devices that have that version, instead of one long list of devices (duplicating lots of versions). I didn't fully consider the implications of a change in the proto structures: It means unmarshalling global lists now fails with the new code on old databases, and that can happen during a few of the old transitions. One option is to convert to the new format before all the existing transitions, but that obviously might have unintended side-effects. A second one is duplicating a lot of code using the old proto types just for db transitions. A (possible) third to reimplement existing transitions doing "everything by hand", i.e. direct db interaction not using methods like updateGlobal (we should probably always do that in future transitions). And a fourth to make a hard version break without db transitions. But the gain of this change seems quite small compared to such a cost. I'll look into the feasibility of options 1-3 tomorrow - any general thoughts on it?

    The new structures are:

    diff --git a/lib/db/structs.proto b/lib/db/structs.proto
    index 1acbaaee..f3a8ac2b 100644
    --- a/lib/db/structs.proto
    +++ b/lib/db/structs.proto
    @@ -13,14 +13,15 @@ option (gogoproto.goproto_unrecognized_all) = false;
     option (gogoproto.goproto_sizecache_all) = false;
     
     message FileVersion {
    -    protocol.Vector version = 1 [(gogoproto.nullable) = false];
    -    bytes           device  = 2;
    -    bool            invalid = 3;
    +    protocol.Vector version         = 1 [(gogoproto.nullable) = false];
    +    repeated bytes  devices         = 2;
    +    repeated bytes  invalid_devices = 3;
     }
     
     message VersionList {
         option (gogoproto.goproto_stringer) = false;
    -    repeated FileVersion versions = 1 [(gogoproto.nullable) = false];
    +    repeated FileVersion versions         = 1 [(gogoproto.nullable) = false];
    +    repeated FileVersion invalid_versions = 2 [(gogoproto.nullable) = false];
     }
     
     // Must be the same as FileInfo but without the blocks field
    

    The invalid handling might seem a bit random: It's due to the fact that now a version can be mixed between invalid and valid entries or have just invalid ones. Currently we just sort invalid devices behind valid ones, and that distinction is now no longer there -> account them separately. I'll try to write it up better later when I have more time (and found solution to the practicalities) :)

  4. AudriusButkevicius commented on May 7, 2020

    @AudriusButkevicius
    Member

    The double invalid stuff looks fishy.

  5. 6 remaining items

  6. imsodin commented on May 8, 2020

    @imsodin
    Member

    On the naming side of things: I will have an old and new variant respectively. In proto the requirements are clear: Old names aren't renamed. Thus I'd go with V2 suffix (e.g. FileVersionV2) for the new variant. On the go side I could use gogoproto.customname to name the new variant without any suffix (e.g. FileVersion) and the old one with a suffix (e.g. FileVersionOld1 or FileVersionDepr1). I like not having suffixes in the code, however switching around names between proto and go is a source of confusion. Then again I'd say proto stuff is hardly ever looked at. Opinions?

  7. AudriusButkevicius commented on May 8, 2020

    @AudriusButkevicius
    Member

    Sorry, whats the issue with renaming stuff to old/deprecated?

  8. imsodin commented on May 8, 2020

    @imsodin
    Member

    Never mind, it's anyway not possible: gogoproto.customname is only available on message members, not on the name of messages themselves.

    Sorry, whats the issue with renaming stuff to old/deprecated?

    If I change the name of the old/deprecated messages in proto, the unmarshalling of old proto data (dbs on migration, json test data) won't work anymore.

  9. AudriusButkevicius commented on May 8, 2020

    @AudriusButkevicius
    Member

    Unmarshalling is done on the field id, not field name. And message names are completely meaningless in proto, as you pass the bytes to some deserializer and that's it.

  10. imsodin commented on May 8, 2020

    @imsodin
    Member

    ... argh - yeah of course, thanks for insisting.
    I was wrapping my head around proto the wrong way: I was thinking we tell it to look up FileVersion in some data structure... which doesn't exists, we pass the serialized data to a specific type to deserialize. Basic stuff...
    At least that means there wasn't a naming problem in the first place.

  11. imsodin commented on May 13, 2020

    @imsodin
    Member

    I implemented a new global list with version vectors stored once only. The whole diff is a monster - I'll probably put it up for review in step-by-step PRs to a feature branch in the Syncthing repo. The branch lives on top of PR #6635, so that's the first thing to happen.
    https://github.com/imsodin/syncthing/tree/indirectversions

    One high-level bit for feedback without jumping into the deep end are db migrations:
    They needed a fair bit of code duplication and adjustments to the 0to1 migration. Everything starting with line 751 are old methods: master...imsodin:indirectversions#diff-59e58e2c68f4fbad47386d8ccbaee68cR751
    I haven't looked into Jakobs suggestion above to convert global lists on load and using "the new global list handling" for the old migrations. Given the current amount of duplication, do you think it is worth investing time to give that a try?

  12. added this to the v1.7.0 milestone on May 30, 2020
  13. added
    frozen-due-to-age
    on May 31, 2021
  14. locked and limited conversation to collaborators on May 31, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementfrozen-due-to-age

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions