From 76e1162a2eb6934b14f6c4be70501236da4ed3a4 Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Mon, 8 Jan 2024 17:14:18 +0100 Subject: [PATCH 1/6] stores: change ResetConsensusSubscription to delete from tables instead of dropping them --- stores/sql.go | 26 ++++++++++++++------------ stores/sql_test.go | 4 ++++ 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/stores/sql.go b/stores/sql.go index d7ee636db..0da9a35f6 100644 --- a/stores/sql.go +++ b/stores/sql.go @@ -581,18 +581,20 @@ func initConsensusInfo(db *gorm.DB) (dbConsensusInfo, modules.ConsensusChangeID, } func (s *SQLStore) ResetConsensusSubscription() error { - // drop tables - err := s.db.Migrator().DropTable(&dbConsensusInfo{}, &dbSiacoinElement{}, &dbTransaction{}) - if err != nil { - return err - } - // recreate the tables. - err = s.db.Migrator().AutoMigrate(&dbConsensusInfo{}, &dbSiacoinElement{}, &dbTransaction{}) - if err != nil { - return err - } - // initialise the consenus_info table. - ci, _, err := initConsensusInfo(s.db) + // empty tables and reinit consensus_infos + var ci dbConsensusInfo + err := s.retryTransaction(func(tx *gorm.DB) error { + if err := s.db.Exec("DELETE FROM consensus_infos").Error; err != nil { + return err + } else if err := s.db.Exec("DELETE FROM siacoin_elements").Error; err != nil { + return err + } else if err := s.db.Exec("DELETE FROM transactions").Error; err != nil { + return err + } else if ci, _, err = initConsensusInfo(tx); err != nil { + return err + } + return nil + }) if err != nil { return err } diff --git a/stores/sql_test.go b/stores/sql_test.go index 9f072d3ad..5a5870a9f 100644 --- a/stores/sql_test.go +++ b/stores/sql_test.go @@ -190,6 +190,10 @@ func TestConsensusReset(t *testing.T) { t.Fatal(err) } + // Reopen the SQLStore. + ss = ss.Reopen() + defer ss.Close() + // Check tables. var count int64 if err := ss.db.Model(&dbConsensusInfo{}).Count(&count).Error; err != nil || count != 1 { From 3997211d64ccaa612fb26da2251534ed68fc1630 Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Tue, 9 Jan 2024 12:12:50 +0100 Subject: [PATCH 2/6] store: fix RefreshHealth query to not update object health of every object every time --- stores/metadata.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/stores/metadata.go b/stores/metadata.go index 09201760e..848b67731 100644 --- a/stores/metadata.go +++ b/stores/metadata.go @@ -2073,14 +2073,22 @@ LIMIT ? // Update the health of the objects associated with the updated slabs. if isSQLite(s.db) { - return tx.Exec(`UPDATE objects SET health = src.health FROM src - INNER JOIN slices ON slices.db_slab_id = src.id - WHERE slices.db_object_id = objects.id`).Error + return tx.Exec(`UPDATE objects SET health = i.health FROM ( + SELECT slices.db_object_id, MIN(s.health) AS health + FROM slices + INNER JOIN src s ON s.id = slices.db_slab_id + GROUP BY slices.db_object_id + ) i + WHERE i.db_object_id = objects.id`).Error } else { return tx.Exec(`UPDATE objects - INNER JOIN slices sli ON sli.db_object_id = objects.id - INNER JOIN src s ON s.id = sli.db_slab_id - SET objects.health = s.health`).Error + INNER JOIN ( + SELECT slices.db_object_id, MIN(s.health) as health + FROM slices + INNER JOIN src s ON s.id = slices.db_slab_id + GROUP BY slices.db_object_id + ) i ON objects.id = i.db_object_id + SET objects.health = i.health`).Error } }) if err != nil { From 832192184b55cb45b989110753b1e801b4dc8433 Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Tue, 9 Jan 2024 14:34:13 +0100 Subject: [PATCH 3/6] stores: add <> to WHERE clause --- stores/metadata.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/stores/metadata.go b/stores/metadata.go index 848b67731..ed762ed56 100644 --- a/stores/metadata.go +++ b/stores/metadata.go @@ -2077,9 +2077,10 @@ LIMIT ? SELECT slices.db_object_id, MIN(s.health) AS health FROM slices INNER JOIN src s ON s.id = slices.db_slab_id + INNER JOIN objects o ON o.id = slices.db_object_id GROUP BY slices.db_object_id ) i - WHERE i.db_object_id = objects.id`).Error + WHERE i.db_object_id = objects.id AND objects.health <> i.health`).Error } else { return tx.Exec(`UPDATE objects INNER JOIN ( @@ -2088,7 +2089,9 @@ LIMIT ? INNER JOIN src s ON s.id = slices.db_slab_id GROUP BY slices.db_object_id ) i ON objects.id = i.db_object_id - SET objects.health = i.health`).Error + SET objects.health = i.health + WHERE objects.health <> i.health + `).Error } }) if err != nil { From 309d356701e1fe41ab04b8af7d60064fda46d112 Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Tue, 9 Jan 2024 15:22:41 +0100 Subject: [PATCH 4/6] stores: creates indices on temporary table --- stores/metadata.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/stores/metadata.go b/stores/metadata.go index ed762ed56..9e99c70f7 100644 --- a/stores/metadata.go +++ b/stores/metadata.go @@ -2058,6 +2058,10 @@ LIMIT ? return err } else if err = tx.Exec("CREATE TEMPORARY TABLE src AS ?", healthQuery).Error; err != nil { return err + } else if err = tx.Exec("CREATE INDEX src_id ON src (id)").Error; err != nil { + return err + } else if err = tx.Exec("CREATE INDEX src_health ON src (health)").Error; err != nil { + return err } var res *gorm.DB From c8337f23942c5b960343cfdfc41f94792d08629a Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Tue, 9 Jan 2024 16:06:29 +0100 Subject: [PATCH 5/6] stores: use != --- stores/metadata.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/stores/metadata.go b/stores/metadata.go index 9e99c70f7..16522e961 100644 --- a/stores/metadata.go +++ b/stores/metadata.go @@ -2084,7 +2084,7 @@ LIMIT ? INNER JOIN objects o ON o.id = slices.db_object_id GROUP BY slices.db_object_id ) i - WHERE i.db_object_id = objects.id AND objects.health <> i.health`).Error + WHERE i.db_object_id = objects.id AND objects.health != i.health`).Error } else { return tx.Exec(`UPDATE objects INNER JOIN ( @@ -2094,7 +2094,7 @@ LIMIT ? GROUP BY slices.db_object_id ) i ON objects.id = i.db_object_id SET objects.health = i.health - WHERE objects.health <> i.health + WHERE objects.health != i.health `).Error } }) From a2209d7d56db619ccb4b2b92e50a63a9bac1b1ef Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Tue, 9 Jan 2024 16:08:02 +0100 Subject: [PATCH 6/6] stores: remove health index --- stores/metadata.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/stores/metadata.go b/stores/metadata.go index 16522e961..0ce043874 100644 --- a/stores/metadata.go +++ b/stores/metadata.go @@ -2060,8 +2060,6 @@ LIMIT ? return err } else if err = tx.Exec("CREATE INDEX src_id ON src (id)").Error; err != nil { return err - } else if err = tx.Exec("CREATE INDEX src_health ON src (health)").Error; err != nil { - return err } var res *gorm.DB