From 61b94b9ea51d2591145e07361f2a1816a480b6bf Mon Sep 17 00:00:00 2001 From: Jakob Borg Date: Mon, 8 Apr 2024 11:14:27 +0200 Subject: [PATCH] lib/db: Drop indexes for outgoing data to force refresh (ref #9496) (#9502) ### Purpose Resend our indexes since we fixed that index-sending issue. I made a new thing to only drop the non-local-device index IDs, i.e., those for other devices. This means we will see a mismatch and resend all indexes, but they will not. This is somewhat cleaner as it avoids resending everything twice when two devices are upgraded, and in any case, we have no reason to force a resend of incoming indexes here. ### Testing It happens on my computer... --- lib/db/keyer.go | 5 +++++ lib/db/lowlevel.go | 18 ++++++++++++++++++ lib/db/schemaupdater.go | 17 +++++++++++------ 3 files changed, 34 insertions(+), 6 deletions(-) diff --git a/lib/db/keyer.go b/lib/db/keyer.go index 6a9e1165f..137b5cc29 100644 --- a/lib/db/keyer.go +++ b/lib/db/keyer.go @@ -103,6 +103,7 @@ type keyer interface { // index IDs GenerateIndexIDKey(key, device, folder []byte) (indexIDKey, error) FolderFromIndexIDKey(key []byte) ([]byte, bool) + DeviceFromIndexIDKey(key []byte) ([]byte, bool) // Mtimes GenerateMtimesKey(key, folder []byte) (mtimesKey, error) @@ -308,6 +309,10 @@ func (k defaultKeyer) FolderFromIndexIDKey(key []byte) ([]byte, bool) { return k.folderIdx.Val(binary.BigEndian.Uint32(key[keyPrefixLen+keyDeviceLen:])) } +func (k defaultKeyer) DeviceFromIndexIDKey(key []byte) ([]byte, bool) { + return k.folderIdx.Val(binary.BigEndian.Uint32(key[keyPrefixLen : keyPrefixLen+keyDeviceLen])) +} + type mtimesKey []byte func (k defaultKeyer) GenerateMtimesKey(key, folder []byte) (mtimesKey, error) { diff --git a/lib/db/lowlevel.go b/lib/db/lowlevel.go index 86bbaa31f..bc0036b28 100644 --- a/lib/db/lowlevel.go +++ b/lib/db/lowlevel.go @@ -665,6 +665,24 @@ func (db *Lowlevel) dropIndexIDs() error { return t.Commit() } +// dropOtherDeviceIndexIDs drops all index IDs for devices other than the +// local device. This means we will resend our indexes to all other devices, +// but they don't have to resend to us. +func (db *Lowlevel) dropOtherDeviceIndexIDs() error { + t, err := db.newReadWriteTransaction() + if err != nil { + return err + } + defer t.close() + if err := t.deleteKeyPrefixMatching([]byte{KeyTypeIndexID}, func(key []byte) bool { + dev, _ := t.keyer.DeviceFromIndexIDKey(key) + return !bytes.Equal(dev, protocol.LocalDeviceID[:]) + }); err != nil { + return err + } + return t.Commit() +} + func (db *Lowlevel) dropMtimes(folder []byte) error { key, err := db.keyer.GenerateMtimesKey(nil, folder) if err != nil { diff --git a/lib/db/schemaupdater.go b/lib/db/schemaupdater.go index ccf71b685..c5ccb8cfd 100644 --- a/lib/db/schemaupdater.go +++ b/lib/db/schemaupdater.go @@ -20,7 +20,7 @@ import ( // do not put restrictions on downgrades (e.g. for repairs after a bugfix). const ( dbVersion = 14 - dbMigrationVersion = 19 + dbMigrationVersion = 20 dbMinSyncthingVersion = "v1.9.0" ) @@ -102,7 +102,8 @@ func (db *schemaUpdater) updateSchema() error { {14, 14, "v1.9.0", db.updateSchemaTo14}, {14, 16, "v1.9.0", db.checkRepairMigration}, {14, 17, "v1.9.0", db.migration17}, - {14, 19, "v1.9.0", db.dropIndexIDsMigration}, + {14, 19, "v1.9.0", db.dropAllIndexIDsMigration}, + {14, 20, "v1.9.0", db.dropOutgoingIndexIDsMigration}, } for _, m := range migrations { @@ -130,13 +131,13 @@ func (db *schemaUpdater) updateSchema() error { } func (*schemaUpdater) writeVersions(m migration, miscDB *NamespacedKV) error { - if err := miscDB.PutInt64("dbVersion", m.schemaVersion); err != nil && err == nil { + if err := miscDB.PutInt64("dbVersion", m.schemaVersion); err != nil { return err } - if err := miscDB.PutString("dbMinSyncthingVersion", m.minSyncthingVersion); err != nil && err == nil { + if err := miscDB.PutString("dbMinSyncthingVersion", m.minSyncthingVersion); err != nil { return err } - if err := miscDB.PutInt64("dbMigrationVersion", m.migrationVersion); err != nil && err == nil { + if err := miscDB.PutInt64("dbMigrationVersion", m.migrationVersion); err != nil { return err } return nil @@ -831,10 +832,14 @@ func (db *schemaUpdater) migration17(prev int) error { return nil } -func (db *schemaUpdater) dropIndexIDsMigration(_ int) error { +func (db *schemaUpdater) dropAllIndexIDsMigration(_ int) error { return db.dropIndexIDs() } +func (db *schemaUpdater) dropOutgoingIndexIDsMigration(_ int) error { + return db.dropOtherDeviceIndexIDs() +} + func rewriteGlobals(t readWriteTransaction) error { it, err := t.NewPrefixIterator([]byte{KeyTypeGlobal}) if err != nil {