From 2b95c7eed3ebfd1c0a03687be6328dfc639bb039 Mon Sep 17 00:00:00 2001 From: Chris Schinnerl Date: Tue, 30 Jul 2024 20:29:02 +0200 Subject: [PATCH] sql: ignore slabs with buffered slab id --- stores/metadata.go | 38 ++++++-------------------------------- stores/sql/main.go | 2 +- 2 files changed, 7 insertions(+), 33 deletions(-) diff --git a/stores/metadata.go b/stores/metadata.go index 1f3cac17a..be455383e 100644 --- a/stores/metadata.go +++ b/stores/metadata.go @@ -1068,41 +1068,15 @@ func (s *SQLStore) RefreshHealth(ctx context.Context) error { // UnhealthySlabs returns up to 'limit' slabs that do not reach full redundancy // in the given contract set. These slabs need to be migrated to good contracts // so they are restored to full health. -func (s *SQLStore) UnhealthySlabs(ctx context.Context, healthCutoff float64, set string, limit int) ([]api.UnhealthySlab, error) { +func (s *SQLStore) UnhealthySlabs(ctx context.Context, healthCutoff float64, set string, limit int) (slabs []api.UnhealthySlab, err error) { if limit <= -1 { limit = math.MaxInt } - - var rows []struct { - Key []byte - Health float64 - } - - if err := s.retryTransaction(ctx, func(tx *gorm.DB) error { - return tx.Select("slabs.key, slabs.health"). - Joins("INNER JOIN contract_sets cs ON slabs.db_contract_set_id = cs.id"). - Model(&dbSlab{}). - Where("health <= ? AND cs.name = ?", healthCutoff, set). - Order("health ASC"). - Limit(limit). - Find(&rows). - Error - }); err != nil { - return nil, err - } - - slabs := make([]api.UnhealthySlab, len(rows)) - for i, row := range rows { - var key object.EncryptionKey - if err := key.UnmarshalBinary(row.Key); err != nil { - return nil, err - } - slabs[i] = api.UnhealthySlab{ - Key: key, - Health: row.Health, - } - } - return slabs, nil + err = s.db.Transaction(ctx, func(tx sql.DatabaseTx) error { + slabs, err = tx.UnhealthySlabs(ctx, healthCutoff, set, limit) + return err + }) + return } // object retrieves an object from the store. diff --git a/stores/sql/main.go b/stores/sql/main.go index fc4321177..84936a6bd 100644 --- a/stores/sql/main.go +++ b/stores/sql/main.go @@ -2360,7 +2360,7 @@ func UnhealthySlabs(ctx context.Context, tx sql.Tx, healthCutoff float64, set st SELECT sla.key, sla.health FROM slabs sla INNER JOIN contract_sets cs ON sla.db_contract_set_id = cs.id - WHERE sla.health <= ? AND cs.name = ? AND sla.health_valid_until > ? + WHERE sla.health <= ? AND cs.name = ? AND sla.health_valid_until > ? AND sla.db_buffered_slab_id IS NULL ORDER BY sla.health ASC LIMIT ? `, healthCutoff, set, time.Now().Unix(), limit)