Implement database abstraction, error checking (ref #5907) (#6107)

This PR does two things, because one lead to the other:

- Move the leveldb specific stuff into a small "backend" package that
defines a backend interface and the leveldb implementation. This allows,
potentially, in the future, switching the db implementation so another
KV store should we wish to do so.

- Add proper error handling all along the way. The db and backend
packages are now errcheck clean. However, I drew the line at modifying
the FileSet API in order to keep this manageable and not continue
refactoring all of the rest of Syncthing. As such, the FileSet methods
still panic on database errors, except for the "database is closed"
error which is instead handled by silently returning as quickly as
possible, with the assumption that we're anyway "on the way out".
This commit is contained in:
Jakob Borg
2019-11-29 09:11:52 +01:00
committed by GitHub
parent a5bbc12625
commit c71116ee94
38 changed files with 2029 additions and 1268 deletions

View File

@@ -16,6 +16,7 @@ import (
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/db"
"github.com/syncthing/syncthing/lib/db/backend"
"github.com/syncthing/syncthing/lib/fs"
"github.com/syncthing/syncthing/lib/protocol"
"github.com/syncthing/syncthing/lib/scanner"
@@ -315,8 +316,8 @@ func setupROFolder() (*model, *sendOnlyFolder) {
fcfg.ID = "ro"
fcfg.Type = config.FolderTypeReceiveOnly
w.SetFolder(fcfg)
m := newModel(w, myID, "syncthing", "dev", db.OpenMemory(), nil)
m := newModel(w, myID, "syncthing", "dev", db.NewLowlevel(backend.OpenMemory()), nil)
m.ServeBackground()
@@ -335,4 +336,4 @@ func setupROFolder() (*model, *sendOnlyFolder) {
m.fmut.RUnlock()
return m, f
}
}

View File

@@ -20,6 +20,7 @@ import (
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/db"
"github.com/syncthing/syncthing/lib/db/backend"
"github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/fs"
"github.com/syncthing/syncthing/lib/ignore"
@@ -91,7 +92,7 @@ func createFile(t *testing.T, name string, fs fs.Filesystem) protocol.FileInfo {
func setupSendReceiveFolder(files ...protocol.FileInfo) (*model, *sendReceiveFolder) {
w := createTmpWrapper(defaultCfg)
model := newModel(w, myID, "syncthing", "dev", db.OpenMemory(), nil)
model := newModel(w, myID, "syncthing", "dev", db.NewLowlevel(backend.OpenMemory()), nil)
fcfg := testFolderConfigTmp()
model.addFolder(fcfg)

View File

@@ -27,6 +27,7 @@ import (
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/db"
"github.com/syncthing/syncthing/lib/db/backend"
"github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/fs"
"github.com/syncthing/syncthing/lib/ignore"
@@ -306,7 +307,7 @@ func TestDeviceRename(t *testing.T) {
}
cfg := config.Wrap("testdata/tmpconfig.xml", rawCfg, events.NoopLogger)
db := db.OpenMemory()
db := db.NewLowlevel(backend.OpenMemory())
m := newModel(cfg, myID, "syncthing", "dev", db, nil)
if cfg.Devices()[device1].Name != "" {
@@ -402,7 +403,7 @@ func TestClusterConfig(t *testing.T) {
},
}
db := db.OpenMemory()
db := db.NewLowlevel(backend.OpenMemory())
wrapper := createTmpWrapper(cfg)
m := newModel(wrapper, myID, "syncthing", "dev", db, nil)
@@ -1533,7 +1534,7 @@ func waitForState(t *testing.T, m *model, folder, status string) {
func TestROScanRecovery(t *testing.T) {
testOs := &fatalOs{t}
ldb := db.OpenMemory()
ldb := db.NewLowlevel(backend.OpenMemory())
set := db.NewFileSet("default", defaultFs, ldb)
set.Update(protocol.LocalDeviceID, []protocol.FileInfo{
{Name: "dummyfile", Version: protocol.Vector{Counters: []protocol.Counter{{ID: 42, Value: 1}}}},
@@ -1584,7 +1585,7 @@ func TestROScanRecovery(t *testing.T) {
func TestRWScanRecovery(t *testing.T) {
testOs := &fatalOs{t}
ldb := db.OpenMemory()
ldb := db.NewLowlevel(backend.OpenMemory())
set := db.NewFileSet("default", defaultFs, ldb)
set.Update(protocol.LocalDeviceID, []protocol.FileInfo{
{Name: "dummyfile", Version: protocol.Vector{Counters: []protocol.Counter{{ID: 42, Value: 1}}}},
@@ -1633,7 +1634,7 @@ func TestRWScanRecovery(t *testing.T) {
}
func TestGlobalDirectoryTree(t *testing.T) {
db := db.OpenMemory()
db := db.NewLowlevel(backend.OpenMemory())
m := newModel(defaultCfgWrapper, myID, "syncthing", "dev", db, nil)
m.ServeBackground()
m.removeFolder(defaultFolderConfig)
@@ -1886,7 +1887,7 @@ func TestGlobalDirectoryTree(t *testing.T) {
}
func TestGlobalDirectorySelfFixing(t *testing.T) {
db := db.OpenMemory()
db := db.NewLowlevel(backend.OpenMemory())
m := newModel(defaultCfgWrapper, myID, "syncthing", "dev", db, nil)
m.ServeBackground()
m.removeFolder(defaultFolderConfig)
@@ -2063,7 +2064,7 @@ func BenchmarkTree_100_10(b *testing.B) {
}
func benchmarkTree(b *testing.B, n1, n2 int) {
db := db.OpenMemory()
db := db.NewLowlevel(backend.OpenMemory())
m := newModel(defaultCfgWrapper, myID, "syncthing", "dev", db, nil)
m.ServeBackground()
m.removeFolder(defaultFolderConfig)
@@ -2128,7 +2129,7 @@ func TestIssue3028(t *testing.T) {
}
func TestIssue4357(t *testing.T) {
db := db.OpenMemory()
db := db.NewLowlevel(backend.OpenMemory())
cfg := defaultCfgWrapper.RawCopy()
// Create a separate wrapper not to pollute other tests.
wrapper := createTmpWrapper(config.Configuration{})
@@ -2251,7 +2252,7 @@ func TestIssue2782(t *testing.T) {
}
func TestIndexesForUnknownDevicesDropped(t *testing.T) {
dbi := db.OpenMemory()
dbi := db.NewLowlevel(backend.OpenMemory())
files := db.NewFileSet("default", defaultFs, dbi)
files.Drop(device1)
@@ -2677,7 +2678,7 @@ func TestInternalScan(t *testing.T) {
func TestCustomMarkerName(t *testing.T) {
testOs := &fatalOs{t}
ldb := db.OpenMemory()
ldb := db.NewLowlevel(backend.OpenMemory())
set := db.NewFileSet("default", defaultFs, ldb)
set.Update(protocol.LocalDeviceID, []protocol.FileInfo{
{Name: "dummyfile"},
@@ -3052,7 +3053,7 @@ func TestPausedFolders(t *testing.T) {
func TestIssue4094(t *testing.T) {
testOs := &fatalOs{t}
db := db.OpenMemory()
db := db.NewLowlevel(backend.OpenMemory())
// Create a separate wrapper not to pollute other tests.
wrapper := createTmpWrapper(config.Configuration{})
m := newModel(wrapper, myID, "syncthing", "dev", db, nil)
@@ -3088,7 +3089,7 @@ func TestIssue4094(t *testing.T) {
func TestIssue4903(t *testing.T) {
testOs := &fatalOs{t}
db := db.OpenMemory()
db := db.NewLowlevel(backend.OpenMemory())
// Create a separate wrapper not to pollute other tests.
wrapper := createTmpWrapper(config.Configuration{})
m := newModel(wrapper, myID, "syncthing", "dev", db, nil)

View File

@@ -13,6 +13,7 @@ import (
"github.com/syncthing/syncthing/lib/config"
"github.com/syncthing/syncthing/lib/db"
"github.com/syncthing/syncthing/lib/db/backend"
"github.com/syncthing/syncthing/lib/events"
"github.com/syncthing/syncthing/lib/fs"
"github.com/syncthing/syncthing/lib/protocol"
@@ -102,7 +103,7 @@ func setupModelWithConnectionFromWrapper(w config.Wrapper) (*model, *fakeConnect
}
func setupModel(w config.Wrapper) *model {
db := db.OpenMemory()
db := db.NewLowlevel(backend.OpenMemory())
m := newModel(w, myID, "syncthing", "dev", db, nil)
m.ServeBackground()