From 9a5f7fbadf4a9afca1bb66abbed3809d7189a34f Mon Sep 17 00:00:00 2001 From: Simon Frei Date: Mon, 18 May 2020 10:42:51 +0200 Subject: [PATCH 1/2] lib/db: Don't panic on seq. coruption when debugging (#6662) --- lib/db/transactions.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/db/transactions.go b/lib/db/transactions.go index 36459d19e..eafb970c7 100644 --- a/lib/db/transactions.go +++ b/lib/db/transactions.go @@ -233,8 +233,7 @@ func (t *readOnlyTransaction) withHaveSequence(folder []byte, startSeq int64, fn if shouldDebug() { if seq := t.keyer.SequenceFromSequenceKey(dbi.Key()); f.Sequence != seq { - l.Warnf("Sequence index corruption (folder %v, file %v): sequence %d != expected %d", string(folder), f.Name, f.Sequence, seq) - panic("sequence index corruption") + l.Debugf("Sequence index corruption (folder %v, file %v): sequence %d != expected %d", string(folder), f.Name, f.Sequence, seq) } } if !fn(f) { From 8c741776997fd22e8017c19650e40f22802372bd Mon Sep 17 00:00:00 2001 From: Simon Frei Date: Wed, 20 May 2020 11:01:27 +0200 Subject: [PATCH 2/2] lib/db: Initialize need meta on first dev occurrence (fixes #6668) (#6669) --- lib/db/meta.go | 11 +++++++++-- lib/db/set_test.go | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/lib/db/meta.go b/lib/db/meta.go index b5632f9b2..b8a462ae2 100644 --- a/lib/db/meta.go +++ b/lib/db/meta.go @@ -118,9 +118,16 @@ func (m *metadataTracker) countsPtr(dev protocol.DeviceID, flag uint32) *Counts idx = len(m.counts.Counts) m.counts.Counts = append(m.counts.Counts, Counts{DeviceID: dev[:], LocalFlags: flag}) m.indexes[key] = idx - if flag == needFlag { + // Need bucket must be initialized when a device first occurs in + // the metadatatracker, even if there's no change to the need + // bucket itself. + nkey := metaKey{dev, needFlag} + nidx, ok := m.indexes[nkey] + if !ok { // Initially a new device needs everything, except deletes - m.counts.Counts[idx] = m.allNeededCounts(dev) + nidx = len(m.counts.Counts) + m.counts.Counts = append(m.counts.Counts, m.allNeededCounts(dev)) + m.indexes[nkey] = nidx } } return &m.counts.Counts[idx] diff --git a/lib/db/set_test.go b/lib/db/set_test.go index b6b2cd777..bd5f2346c 100644 --- a/lib/db/set_test.go +++ b/lib/db/set_test.go @@ -1653,6 +1653,24 @@ func TestUpdateWithOneFileTwice(t *testing.T) { } } +// https://github.com/syncthing/syncthing/issues/6668 +func TestNeedRemoteOnly(t *testing.T) { + ldb := db.NewLowlevel(backend.OpenMemory()) + defer ldb.Close() + + s := db.NewFileSet("test", fs.NewFilesystem(fs.FilesystemTypeFake, ""), ldb) + + remote0Have := fileList{ + protocol.FileInfo{Name: "b", Version: protocol.Vector{Counters: []protocol.Counter{{ID: myID, Value: 1001}}}, Blocks: genBlocks(2)}, + } + s.Update(remoteDevice0, remote0Have) + + need := needSize(s, remoteDevice0) + if !need.Equal(db.Counts{}) { + t.Error("Expected nothing needed, got", need) + } +} + func replace(fs *db.FileSet, device protocol.DeviceID, files []protocol.FileInfo) { fs.Drop(device) fs.Update(device, files)