refactor: use modern Protobuf encoder (#9817)

At a high level, this is what I've done and why:

- I'm moving the protobuf generation for the `protocol`, `discovery` and
`db` packages to the modern alternatives, and using `buf` to generate
because it's nice and simple.
- After trying various approaches on how to integrate the new types with
the existing code, I opted for splitting off our own data model types
from the on-the-wire generated types. This means we can have a
`FileInfo` type with nicer ergonomics and lots of methods, while the
protobuf generated type stays clean and close to the wire protocol. It
does mean copying between the two when required, which certainly adds a
small amount of inefficiency. If we want to walk this back in the future
and use the raw generated type throughout, that's possible, this however
makes the refactor smaller (!) as it doesn't change everything about the
type for everyone at the same time.
- I have simply removed in cold blood a significant number of old
database migrations. These depended on previous generations of generated
messages of various kinds and were annoying to support in the new
fashion. The oldest supported database version now is the one from
Syncthing 1.9.0 from Sep 7, 2020.
- I changed config structs to be regular manually defined structs.

For the sake of discussion, some things I tried that turned out not to
work...

### Embedding / wrapping

Embedding the protobuf generated structs in our existing types as a data
container and keeping our methods and stuff:

```
package protocol

type FileInfo struct {
  *generated.FileInfo
}
```

This generates a lot of problems because the internal shape of the
generated struct is quite different (different names, different types,
more pointers), because initializing it doesn't work like you'd expect
(i.e., you end up with an embedded nil pointer and a panic), and because
the types of child types don't get wrapped. That is, even if we also
have a similar wrapper around a `Vector`, that's not the type you get
when accessing `someFileInfo.Version`, you get the `*generated.Vector`
that doesn't have methods, etc.

### Aliasing

```
package protocol

type FileInfo = generated.FileInfo
```

Doesn't help because you can't attach methods to it, plus all the above.

### Generating the types into the target package like we do now and
attaching methods

This fails because of the different shape of the generated type (as in
the embedding case above) plus the generated struct already has a bunch
of methods that we can't necessarily override properly (like `String()`
and a bunch of getters).

### Methods to functions

I considered just moving all the methods we attach to functions in a
specific package, so that for example

```
package protocol

func (f FileInfo) Equal(other FileInfo) bool
```

would become

```
package fileinfos

func Equal(a, b *generated.FileInfo) bool
```

and this would mostly work, but becomes quite verbose and cumbersome,
and somewhat limits discoverability (you can't see what methods are
available on the type in auto completions, etc). In the end I did this
in some cases, like in the database layer where a lot of things like
`func (fv *FileVersion) IsEmpty() bool` becomes `func fvIsEmpty(fv
*generated.FileVersion)` because they were anyway just internal methods.

Fixes #8247
This commit is contained in:
Jakob Borg
2024-12-01 16:50:17 +01:00
committed by GitHub
parent 2b8ee4c7a5
commit 77970d5113
203 changed files with 7437 additions and 28636 deletions
+42 -42
View File
@@ -391,7 +391,7 @@ func TestClusterConfig(t *testing.T) {
}
cfg.Folders = []config.FolderConfiguration{
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder1",
Path: "testdata1",
Devices: []config.FolderDeviceConfiguration{
@@ -400,7 +400,7 @@ func TestClusterConfig(t *testing.T) {
},
},
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder2",
Path: "testdata2",
Paused: true, // should still be included
@@ -410,7 +410,7 @@ func TestClusterConfig(t *testing.T) {
},
},
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder3",
Path: "testdata3",
Devices: []config.FolderDeviceConfiguration{
@@ -499,7 +499,7 @@ func TestIntroducer(t *testing.T) {
},
Folders: []config.FolderConfiguration{
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder1",
Path: "testdata",
Devices: []config.FolderDeviceConfiguration{
@@ -507,7 +507,7 @@ func TestIntroducer(t *testing.T) {
},
},
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder2",
Path: "testdata",
Devices: []config.FolderDeviceConfiguration{
@@ -560,7 +560,7 @@ func TestIntroducer(t *testing.T) {
},
Folders: []config.FolderConfiguration{
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder1",
Path: "testdata",
Devices: []config.FolderDeviceConfiguration{
@@ -569,7 +569,7 @@ func TestIntroducer(t *testing.T) {
},
},
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder2",
Path: "testdata",
Devices: []config.FolderDeviceConfiguration{
@@ -615,7 +615,7 @@ func TestIntroducer(t *testing.T) {
},
Folders: []config.FolderConfiguration{
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder1",
Path: "testdata",
Devices: []config.FolderDeviceConfiguration{
@@ -624,7 +624,7 @@ func TestIntroducer(t *testing.T) {
},
},
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder2",
Path: "testdata",
Devices: []config.FolderDeviceConfiguration{
@@ -667,7 +667,7 @@ func TestIntroducer(t *testing.T) {
},
Folders: []config.FolderConfiguration{
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder1",
Path: "testdata",
Devices: []config.FolderDeviceConfiguration{
@@ -676,7 +676,7 @@ func TestIntroducer(t *testing.T) {
},
},
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder2",
Path: "testdata",
Devices: []config.FolderDeviceConfiguration{
@@ -719,7 +719,7 @@ func TestIntroducer(t *testing.T) {
},
Folders: []config.FolderConfiguration{
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder1",
Path: "testdata",
Devices: []config.FolderDeviceConfiguration{
@@ -728,7 +728,7 @@ func TestIntroducer(t *testing.T) {
},
},
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder2",
Path: "testdata",
Devices: []config.FolderDeviceConfiguration{
@@ -775,7 +775,7 @@ func TestIntroducer(t *testing.T) {
},
Folders: []config.FolderConfiguration{
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder1",
Path: "testdata",
Devices: []config.FolderDeviceConfiguration{
@@ -784,7 +784,7 @@ func TestIntroducer(t *testing.T) {
},
},
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder2",
Path: "testdata",
Devices: []config.FolderDeviceConfiguration{
@@ -826,7 +826,7 @@ func TestIntroducer(t *testing.T) {
},
Folders: []config.FolderConfiguration{
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder1",
Path: "testdata",
Devices: []config.FolderDeviceConfiguration{
@@ -835,7 +835,7 @@ func TestIntroducer(t *testing.T) {
},
},
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder2",
Path: "testdata",
Devices: []config.FolderDeviceConfiguration{
@@ -873,7 +873,7 @@ func TestIssue4897(t *testing.T) {
},
Folders: []config.FolderConfiguration{
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder1",
Path: "testdata",
Devices: []config.FolderDeviceConfiguration{
@@ -1030,7 +1030,7 @@ func TestAutoAcceptNewFolderPremutationsNoPanic(t *testing.T) {
for _, dev2folder := range premutations {
cfg := defaultAutoAcceptCfg.Copy()
if localFolder.Label != "" {
fcfg := newFolderConfiguration(defaultCfgWrapper, localFolder.ID, localFolder.Label, fs.FilesystemTypeFake, localFolder.ID)
fcfg := newFolderConfiguration(defaultCfgWrapper, localFolder.ID, localFolder.Label, config.FilesystemTypeFake, localFolder.ID)
fcfg.Paused = localFolderPaused
cfg.Folders = append(cfg.Folders, fcfg)
}
@@ -1075,7 +1075,7 @@ func TestAutoAcceptExistingFolder(t *testing.T) {
tcfg := defaultAutoAcceptCfg.Copy()
tcfg.Folders = []config.FolderConfiguration{
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: id,
Path: idOther, // To check that path does not get changed.
},
@@ -1101,7 +1101,7 @@ func TestAutoAcceptNewAndExistingFolder(t *testing.T) {
tcfg := defaultAutoAcceptCfg.Copy()
tcfg.Folders = []config.FolderConfiguration{
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: id1,
Path: id1, // from previous test case, to verify that path doesn't get changed.
},
@@ -1127,7 +1127,7 @@ func TestAutoAcceptAlreadyShared(t *testing.T) {
tcfg := defaultAutoAcceptCfg.Copy()
tcfg.Folders = []config.FolderConfiguration{
{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: id,
Path: id,
Devices: []config.FolderDeviceConfiguration{
@@ -1226,7 +1226,7 @@ func TestAutoAcceptPausedWhenFolderConfigChanged(t *testing.T) {
idOther := srand.String(8) // To check that path does not get changed.
tcfg := defaultAutoAcceptCfg.Copy()
fcfg := newFolderConfiguration(defaultCfgWrapper, id, "", fs.FilesystemTypeFake, idOther)
fcfg := newFolderConfiguration(defaultCfgWrapper, id, "", config.FilesystemTypeFake, idOther)
fcfg.Paused = true
// The order of devices here is wrong (cfg.clean() sorts them), which will cause the folder to restart.
// Because of the restart, folder gets removed from m.deviceFolder, which means that generateClusterConfig will not panic.
@@ -1272,7 +1272,7 @@ func TestAutoAcceptPausedWhenFolderConfigNotChanged(t *testing.T) {
idOther := srand.String(8) // To check that path does not get changed.
tcfg := defaultAutoAcceptCfg.Copy()
fcfg := newFolderConfiguration(defaultCfgWrapper, id, "", fs.FilesystemTypeFake, idOther)
fcfg := newFolderConfiguration(defaultCfgWrapper, id, "", config.FilesystemTypeFake, idOther)
fcfg.Paused = true
// The new folder is exactly the same as the one constructed by handleAutoAccept, which means
// the folder will not be restarted (even if it's paused), yet handleAutoAccept used to add the folder
@@ -1521,7 +1521,7 @@ func TestIgnores(t *testing.T) {
// Invalid path, treated like no patterns at all.
fcfg := config.FolderConfiguration{
ID: "fresh", Path: "XXX",
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
}
ignores := ignore.New(fcfg.Filesystem(nil), ignore.WithCache(m.cfg.Options().CacheIgnoredFiles))
m.mut.Lock()
@@ -1609,7 +1609,7 @@ func waitForState(t *testing.T, sub events.Subscription, folder, expected string
func TestROScanRecovery(t *testing.T) {
fcfg := config.FolderConfiguration{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "default",
Path: srand.String(32),
Type: config.FolderTypeSendOnly,
@@ -1656,7 +1656,7 @@ func TestROScanRecovery(t *testing.T) {
func TestRWScanRecovery(t *testing.T) {
fcfg := config.FolderConfiguration{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "default",
Path: srand.String(32),
Type: config.FolderTypeSendReceive,
@@ -2522,7 +2522,7 @@ func TestVersionRestore(t *testing.T) {
// We verify that the content matches at the expected filenames
// after the restore operation.
fcfg := newFolderConfiguration(defaultCfgWrapper, "default", "default", fs.FilesystemTypeFake, srand.String(32))
fcfg := newFolderConfiguration(defaultCfgWrapper, "default", "default", config.FilesystemTypeFake, srand.String(32))
fcfg.Versioning.Type = "simple"
fcfg.FSWatcherEnabled = false
filesystem := fcfg.Filesystem(nil)
@@ -2744,7 +2744,7 @@ func TestIssue4094(t *testing.T) {
folderPath := "nonexistent"
cfg := defaultCfgWrapper.RawCopy()
fcfg := config.FolderConfiguration{
FilesystemType: fs.FilesystemTypeFake,
FilesystemType: config.FilesystemTypeFake,
ID: "folder1",
Path: folderPath,
Paused: true,
@@ -2861,7 +2861,7 @@ func TestFolderRestartZombies(t *testing.T) {
waiter, err := wrapper.Modify(func(cfg *config.Configuration) {
cfg.Options.RawMaxFolderConcurrency = -1
_, i, _ := cfg.Folder("default")
cfg.Folders[i].FilesystemType = fs.FilesystemTypeFake
cfg.Folders[i].FilesystemType = config.FilesystemTypeFake
})
must(t, err)
waiter.Wait()
@@ -3204,7 +3204,7 @@ func TestRenameSequenceOrder(t *testing.T) {
count := 0
snap := dbSnapshot(t, m, "default")
snap.WithHave(protocol.LocalDeviceID, func(i protocol.FileIntf) bool {
snap.WithHave(protocol.LocalDeviceID, func(i protocol.FileInfo) bool {
count++
return true
})
@@ -3236,7 +3236,7 @@ func TestRenameSequenceOrder(t *testing.T) {
var firstExpectedSequence int64
var secondExpectedSequence int64
failed := false
snap.WithHaveSequence(0, func(i protocol.FileIntf) bool {
snap.WithHaveSequence(0, func(i protocol.FileInfo) bool {
t.Log(i)
if i.FileName() == "17" {
firstExpectedSequence = i.SequenceNo() + 1
@@ -3270,7 +3270,7 @@ func TestRenameSameFile(t *testing.T) {
count := 0
snap := dbSnapshot(t, m, "default")
snap.WithHave(protocol.LocalDeviceID, func(i protocol.FileIntf) bool {
snap.WithHave(protocol.LocalDeviceID, func(i protocol.FileInfo) bool {
count++
return true
})
@@ -3293,7 +3293,7 @@ func TestRenameSameFile(t *testing.T) {
prevSeq := int64(0)
seen := false
snap.WithHaveSequence(0, func(i protocol.FileIntf) bool {
snap.WithHaveSequence(0, func(i protocol.FileInfo) bool {
if i.SequenceNo() <= prevSeq {
t.Fatalf("non-increasing sequences: %d <= %d", i.SequenceNo(), prevSeq)
}
@@ -3333,7 +3333,7 @@ func TestRenameEmptyFile(t *testing.T) {
}
count := 0
snap.WithBlocksHash(empty.BlocksHash, func(_ protocol.FileIntf) bool {
snap.WithBlocksHash(empty.BlocksHash, func(_ protocol.FileInfo) bool {
count++
return true
})
@@ -3343,7 +3343,7 @@ func TestRenameEmptyFile(t *testing.T) {
}
count = 0
snap.WithBlocksHash(file.BlocksHash, func(_ protocol.FileIntf) bool {
snap.WithBlocksHash(file.BlocksHash, func(_ protocol.FileInfo) bool {
count++
return true
})
@@ -3362,7 +3362,7 @@ func TestRenameEmptyFile(t *testing.T) {
defer snap.Release()
count = 0
snap.WithBlocksHash(empty.BlocksHash, func(_ protocol.FileIntf) bool {
snap.WithBlocksHash(empty.BlocksHash, func(_ protocol.FileInfo) bool {
count++
return true
})
@@ -3372,7 +3372,7 @@ func TestRenameEmptyFile(t *testing.T) {
}
count = 0
snap.WithBlocksHash(file.BlocksHash, func(i protocol.FileIntf) bool {
snap.WithBlocksHash(file.BlocksHash, func(i protocol.FileInfo) bool {
count++
if i.FileName() != "new-file" {
t.Fatalf("unexpected file name %s, expected new-file", i.FileName())
@@ -3408,7 +3408,7 @@ func TestBlockListMap(t *testing.T) {
}
var paths []string
snap.WithBlocksHash(fi.BlocksHash, func(fi protocol.FileIntf) bool {
snap.WithBlocksHash(fi.BlocksHash, func(fi protocol.FileInfo) bool {
paths = append(paths, fi.FileName())
return true
})
@@ -3441,7 +3441,7 @@ func TestBlockListMap(t *testing.T) {
defer snap.Release()
paths = paths[:0]
snap.WithBlocksHash(fi.BlocksHash, func(fi protocol.FileIntf) bool {
snap.WithBlocksHash(fi.BlocksHash, func(fi protocol.FileInfo) bool {
paths = append(paths, fi.FileName())
return true
})
@@ -3468,7 +3468,7 @@ func TestScanRenameCaseOnly(t *testing.T) {
snap := dbSnapshot(t, m, fcfg.ID)
defer snap.Release()
found := false
snap.WithHave(protocol.LocalDeviceID, func(i protocol.FileIntf) bool {
snap.WithHave(protocol.LocalDeviceID, func(i protocol.FileInfo) bool {
if found {
t.Fatal("got more than one file")
}
@@ -3487,7 +3487,7 @@ func TestScanRenameCaseOnly(t *testing.T) {
snap = dbSnapshot(t, m, fcfg.ID)
defer snap.Release()
found = false
snap.WithHave(protocol.LocalDeviceID, func(i protocol.FileIntf) bool {
snap.WithHave(protocol.LocalDeviceID, func(i protocol.FileInfo) bool {
if i.FileName() == name {
if i.IsDeleted() {
return true