Skip to content

Commit

Permalink
Add index for db_directory_id column in SQLite (#1511)
Browse files Browse the repository at this point in the history
Noticed that deleting objects takes forever on Arequipa. Turns out we
were missing an index.
Also snuck in a little change here to prevent account refill spam for
accounts that don't need to be refreshed.

Before adding missing index

```
sqlite> DELETE
        FROM directories
        WHERE directories.id != 1
        AND NOT EXISTS (SELECT 1 FROM objects WHERE objects.db_directory_id = directories.id)
        AND NOT EXISTS (SELECT 1 FROM (SELECT 1 FROM directories AS d WHERE d.db_parent_id = directories.id)i);
Run Time: real 61.209 user 35.705172 sys 25.502178
```

After adding missing index

```
sqlite> DELETE                   
        FROM directories
        WHERE directories.id != 1                                                               
        AND NOT EXISTS (SELECT 1 FROM objects WHERE objects.db_directory_id = directories.id)
        AND NOT EXISTS (SELECT 1 FROM (SELECT 1 FROM directories AS d WHERE d.db_parent_id = directories.id)i);
Run Time: real 0.020 user 0.018806 sys 0.001690
```
  • Loading branch information
ChrisSchinnerl committed Sep 12, 2024
2 parents fc5efe5 + 9247301 commit d9c500b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
6 changes: 6 additions & 0 deletions internal/sql/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
18 changes: 9 additions & 9 deletions internal/worker/accounts.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -351,23 +351,23 @@ 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)
}
}
}

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)

// check if the contract is too close to the proof window to be revised,
// 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.
Expand All @@ -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))
}
Expand All @@ -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
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
CREATE INDEX IF NOT EXISTS `idx_objects_db_directory_id` ON `objects`(`db_directory_id`);
1 change: 1 addition & 0 deletions stores/sql/sqlite/migrations/main/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d9c500b

Please sign in to comment.