diff --git a/internal/sql/migrations.go b/internal/sql/migrations.go index 7c591c19a..bf4853e39 100644 --- a/internal/sql/migrations.go +++ b/internal/sql/migrations.go @@ -230,6 +230,12 @@ var ( return performMigration(ctx, tx, migrationsFs, dbIdentifier, "00019_settings", log) }, }, + { + ID: "00020_idx_db_directory", + Migrate: func(tx Tx) error { + return performMigration(ctx, tx, migrationsFs, dbIdentifier, "00020_idx_db_directory", log) + }, + }, } } MetricsMigrations = func(ctx context.Context, migrationsFs embed.FS, log *zap.SugaredLogger) []Migration { diff --git a/internal/worker/accounts.go b/internal/worker/accounts.go index 1022075f1..f73c9f529 100644 --- a/internal/worker/accounts.go +++ b/internal/worker/accounts.go @@ -336,7 +336,7 @@ func (a *AccountMgr) refillAccounts() { defer cancel() // refill - err := a.refillAccount(rCtx, c, cs.BlockHeight, a.revisionSubmissionBuffer) + refilled, err := a.refillAccount(rCtx, c, cs.BlockHeight, a.revisionSubmissionBuffer) // determine whether to log something shouldLog := true @@ -351,7 +351,7 @@ func (a *AccountMgr) refillAccounts() { if err != nil && shouldLog { a.logger.Error("failed to refill account for host", zap.Stringer("hostKey", contract.HostKey), zap.Error(err)) - } else { + } else if refilled { a.logger.Infow("successfully refilled account for host", zap.Stringer("hostKey", contract.HostKey), zap.Error(err)) } }(c) @@ -359,7 +359,7 @@ func (a *AccountMgr) refillAccounts() { } } -func (a *AccountMgr) refillAccount(ctx context.Context, contract api.ContractMetadata, bh, revisionSubmissionBuffer uint64) error { +func (a *AccountMgr) refillAccount(ctx context.Context, contract api.ContractMetadata, bh, revisionSubmissionBuffer uint64) (bool, error) { // fetch the account account := a.Account(contract.HostKey) @@ -367,7 +367,7 @@ func (a *AccountMgr) refillAccount(ctx context.Context, contract api.ContractMet // trying to refill the account would result in the host not returning the // revision and returning an obfuscated error if (bh + revisionSubmissionBuffer) > contract.WindowStart { - return fmt.Errorf("contract %v is too close to the proof window to be revised", contract.ID) + return false, fmt.Errorf("contract %v is too close to the proof window to be revised", contract.ID) } // check if a host is potentially cheating before refilling. @@ -382,7 +382,7 @@ func (a *AccountMgr) refillAccount(ctx context.Context, contract api.ContractMet "drift", account.Drift.String(), ) _ = a.alerts.RegisterAlert(a.shutdownCtx, alert) - return fmt.Errorf("not refilling account since host is potentially cheating: %w", errMaxDriftExceeded) + return false, fmt.Errorf("not refilling account since host is potentially cheating: %w", errMaxDriftExceeded) } else { _ = a.alerts.DismissAlerts(a.shutdownCtx, alerts.IDForAccount(alertAccountRefillID, account.ID)) } @@ -392,7 +392,7 @@ func (a *AccountMgr) refillAccount(ctx context.Context, contract api.ContractMet // sync the account err := a.syncer.SyncAccount(ctx, contract.ID, contract.HostKey, contract.SiamuxAddr) if err != nil { - return fmt.Errorf("failed to sync account's balance: %w", err) + return false, fmt.Errorf("failed to sync account's balance: %w", err) } // refetch the account after syncing @@ -401,15 +401,15 @@ func (a *AccountMgr) refillAccount(ctx context.Context, contract api.ContractMet // check if refill is needed if account.Balance.Cmp(minBalance) >= 0 { - return nil + return false, nil } // fund the account err := a.funder.FundAccount(ctx, contract.ID, contract.HostKey, maxBalance) if err != nil { - return fmt.Errorf("failed to fund account: %w", err) + return false, fmt.Errorf("failed to fund account: %w", err) } - return nil + return true, nil } // WithSync syncs an accounts balance with the bus. To do so, the account is diff --git a/stores/sql/mysql/migrations/main/migration_00020_idx_db_directory.sql b/stores/sql/mysql/migrations/main/migration_00020_idx_db_directory.sql new file mode 100644 index 000000000..8b1378917 --- /dev/null +++ b/stores/sql/mysql/migrations/main/migration_00020_idx_db_directory.sql @@ -0,0 +1 @@ + diff --git a/stores/sql/sqlite/migrations/main/migration_00020_idx_db_directory.sql b/stores/sql/sqlite/migrations/main/migration_00020_idx_db_directory.sql new file mode 100644 index 000000000..5757fd280 --- /dev/null +++ b/stores/sql/sqlite/migrations/main/migration_00020_idx_db_directory.sql @@ -0,0 +1 @@ +CREATE INDEX IF NOT EXISTS `idx_objects_db_directory_id` ON `objects`(`db_directory_id`); diff --git a/stores/sql/sqlite/migrations/main/schema.sql b/stores/sql/sqlite/migrations/main/schema.sql index 6d8d0ee6c..af4c59654 100644 --- a/stores/sql/sqlite/migrations/main/schema.sql +++ b/stores/sql/sqlite/migrations/main/schema.sql @@ -58,6 +58,7 @@ CREATE INDEX `idx_objects_object_id` ON `objects`(`object_id`); CREATE INDEX `idx_objects_size` ON `objects`(`size`); CREATE UNIQUE INDEX `idx_object_bucket` ON `objects`(`db_bucket_id`,`object_id`); CREATE INDEX `idx_objects_created_at` ON `objects`(`created_at`); +CREATE INDEX `idx_objects_db_directory_id` ON `objects`(`db_directory_id`); -- dbMultipartUpload CREATE TABLE `multipart_uploads` (`id` integer PRIMARY KEY AUTOINCREMENT,`created_at` datetime,`key` blob,`upload_id` text NOT NULL,`object_id` text NOT NULL,`db_bucket_id` integer NOT NULL,`mime_type` text,CONSTRAINT `fk_multipart_uploads_db_bucket` FOREIGN KEY (`db_bucket_id`) REFERENCES `buckets`(`id`) ON DELETE CASCADE);