mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2026-01-11 20:56:29 +00:00
```
NAME:
forgejo doctor cleanup-commit-status - Cleanup extra records in commit_status table
USAGE:
forgejo doctor cleanup-commit-status
DESCRIPTION:
Forgejo suffered from a bug which caused the creation of more entries in the
"commit_status" table than necessary. This operation removes the redundant
data caused by the bug. Removing this data is almost always safe.
These reundant records can be accessed by users through the API, making it
possible, but unlikely, that removing it could have an impact to
integrating services (API: /repos/{owner}/{repo}/commits/{ref}/statuses).
It is safe to run while Forgejo is online.
On very large Forgejo instances, the performance of operation will improve
if the buffer-size option is used with large values. Approximately 130 MB of
memory is required for every 100,000 records in the buffer.
Bug reference: https://codeberg.org/forgejo/forgejo/issues/10671
OPTIONS:
--help, -h show help
--custom-path string, -C string Set custom path (defaults to '{WorkPath}/custom')
--config string, -c string Set custom config file (defaults to '{WorkPath}/custom/conf/app.ini')
--work-path string, -w string Set Forgejo's working path (defaults to the directory of the Forgejo binary)
--verbose, -V Show process details
--dry-run Report statistics from the operation but do not modify the database
--buffer-size int Record count per query while iterating records; larger values are typically faster but use more memory (default: 100000)
--delete-chunk-size int Number of records to delete per DELETE query (default: 1000)
```
The cleanup effectively performs `SELECT * FROM commit_status ORDER BY repo_id, sha, context, index, id`, and iterates through the records. Whenever `index, id` changes without the other fields changing, then it's a useless record that can be deleted. The major complication is doing that at scale without bringing the entire database table into memory, which is performed through a new iteration method `IterateByKeyset`.
Manually tested against a 455,303 record table in PostgreSQL, MySQL, and SQLite, which was reduced to 10,781 records, dropping 97.5% of the records.
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/10686
Reviewed-by: Gusted <gusted@noreply.codeberg.org>
Co-authored-by: Mathieu Fenniak <mathieu@fenniak.net>
Co-committed-by: Mathieu Fenniak <mathieu@fenniak.net>
208 lines
6.9 KiB
Go
208 lines
6.9 KiB
Go
// Copyright 2022 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package db
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"reflect"
|
|
"strings"
|
|
|
|
"forgejo.org/modules/setting"
|
|
|
|
"xorm.io/builder"
|
|
)
|
|
|
|
// Iterate iterate all the Bean object. The table being iterated must have a single-column primary key.
|
|
func Iterate[Bean any](ctx context.Context, cond builder.Cond, f func(ctx context.Context, bean *Bean) error) error {
|
|
var dummy Bean
|
|
batchSize := setting.Database.IterateBufferSize
|
|
|
|
table, err := TableInfo(&dummy)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to fetch table info for bean %v: %w", dummy, err)
|
|
}
|
|
if len(table.PrimaryKeys) != 1 {
|
|
return fmt.Errorf("iterate only supported on a table with 1 primary key field, but table %s had %d", table.Name, len(table.PrimaryKeys))
|
|
}
|
|
|
|
pkDbName := table.PrimaryKeys[0]
|
|
var pkStructFieldName string
|
|
|
|
for _, c := range table.Columns() {
|
|
if c.Name == pkDbName {
|
|
pkStructFieldName = c.FieldName
|
|
break
|
|
}
|
|
}
|
|
if pkStructFieldName == "" {
|
|
return fmt.Errorf("iterate unable to identify struct field for primary key %s", pkDbName)
|
|
}
|
|
|
|
var lastPK any
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
default:
|
|
beans := make([]*Bean, 0, batchSize)
|
|
|
|
sess := GetEngine(ctx)
|
|
sess = sess.OrderBy(pkDbName)
|
|
if cond != nil {
|
|
sess = sess.Where(cond)
|
|
}
|
|
if lastPK != nil {
|
|
sess = sess.Where(builder.Gt{pkDbName: lastPK})
|
|
}
|
|
|
|
if err := sess.Limit(batchSize).Find(&beans); err != nil {
|
|
return err
|
|
}
|
|
if len(beans) == 0 {
|
|
return nil
|
|
}
|
|
|
|
for _, bean := range beans {
|
|
if err := f(ctx, bean); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
lastBean := beans[len(beans)-1]
|
|
lastPK = extractFieldValue(lastBean, pkStructFieldName)
|
|
}
|
|
}
|
|
}
|
|
|
|
func extractFieldValue(bean any, fieldName string) any {
|
|
v := reflect.ValueOf(bean)
|
|
if v.Kind() == reflect.Ptr {
|
|
v = v.Elem()
|
|
}
|
|
field := v.FieldByName(fieldName)
|
|
return field.Interface()
|
|
}
|
|
|
|
// IterateByKeyset iterates all the records on a database (matching the provided condition) in the order of specified
|
|
// order fields, and invokes the provided handler function for each record. It is safe to UPDATE or DELETE the record in
|
|
// the handler function, as long as the order fields are not mutated on the record (which could cause records to be
|
|
// missed or iterated multiple times).
|
|
//
|
|
// Assuming order fields a, b, and c, then database queries will be performed as "SELECT * FROM table WHERE (a, b, c) >
|
|
// (last_a, last_b, last_c) ORDER BY a, b, c LIMIT buffer_size" repeatedly until the query returns no records (except
|
|
// the first query will have no WHERE clause).
|
|
//
|
|
// Critical requirements for proper usage:
|
|
//
|
|
// - the order fields encompass at least one UNIQUE or PRIMARY KEY constraint of the table to ensure that records are
|
|
// not duplicated -- for example, if the table has a unique index covering `(repo_id, index)`, then it would be safe to
|
|
// use this function as long as both fields (in either order) are provided as order fields.
|
|
//
|
|
// - none of the order fields may have NULL values in them, as the `=` and `>` comparisons being performed by the
|
|
// iterative queries will not operate on these records consistently as they do with other values.
|
|
//
|
|
// This implementation could be a much simpler streaming scan of the query results, except that doesn't permit making
|
|
// any additional database queries or data modifications in the target function -- SQLite cannot write while holding a
|
|
// read lock. Buffering pages of data in-memory avoids that issue.
|
|
//
|
|
// Performance:
|
|
//
|
|
// - High performance will result from an alignment of an index on the table with the order fields, in the same field
|
|
// order, even if additional ordering fields could be provided after the index fields. In the absence of this index
|
|
// alignment, it is reasonable to expect that every extra page of data accessed will require a query that will perform
|
|
// an index scan (if available) or sequential scan of the target table. In testing on the `commit_status` table with
|
|
// 455k records, a fully index-supported ordering allowed each query page to execute in 0.18ms, as opposed to 80ms
|
|
// per-query without matching supporting index.
|
|
//
|
|
// - In the absence of a matching index, slower per-query performance can be compensated with a larger `batchSize`
|
|
// parameter, which controls how many records to fetch at once and therefore reduces the number of queries required.
|
|
// This requires more memory. Similar `commit_status` table testing showed these stats for iteration time and memory
|
|
// usage for different buffer sizes; specifics will vary depending on the target table:
|
|
// - buffer size = 1,000,000 - iterates in 2.8 seconds, consumes 363 MB of RAM
|
|
// - buffer size = 100,000 - iterates in 3.5 seconds, consume 130 MB of RAM
|
|
// - buffer size = 10,000 - iterates in 7.1 seconds, consumes 59 MB of RAM
|
|
// - buffer size = 1,000 - iterates in 33.9 seconds, consumes 42 MB of RAM
|
|
func IterateByKeyset[Bean any](ctx context.Context, cond builder.Cond, orderFields []string, batchSize int, f func(ctx context.Context, bean *Bean) error) error {
|
|
var dummy Bean
|
|
|
|
if len(orderFields) == 0 {
|
|
return errors.New("orderFields must be provided")
|
|
}
|
|
|
|
table, err := TableInfo(&dummy)
|
|
if err != nil {
|
|
return fmt.Errorf("unable to fetch table info for bean %v: %w", dummy, err)
|
|
}
|
|
goFieldNames := make([]string, len(orderFields))
|
|
for i, f := range orderFields {
|
|
goFieldNames[i] = table.GetColumn(f).FieldName
|
|
}
|
|
sqlFieldNames := make([]string, len(orderFields))
|
|
for i, f := range orderFields {
|
|
// Support field names like "index" which need quoting in builder.Cond & OrderBy
|
|
sqlFieldNames[i] = x.Dialect().Quoter().Quote(f)
|
|
}
|
|
|
|
var lastKey []any
|
|
|
|
// For the order fields, generate clauses (a, b, c) and (?, ?, ?) which will be used in the WHERE clause when
|
|
// reading additional pages of data.
|
|
rowValue := strings.Builder{}
|
|
rowParameterValue := strings.Builder{}
|
|
rowValue.WriteString("(")
|
|
rowParameterValue.WriteString("(")
|
|
for i, f := range sqlFieldNames {
|
|
rowValue.WriteString(f)
|
|
rowParameterValue.WriteString("?")
|
|
if i != len(sqlFieldNames)-1 {
|
|
rowValue.WriteString(", ")
|
|
rowParameterValue.WriteString(", ")
|
|
}
|
|
}
|
|
rowValue.WriteString(")")
|
|
rowParameterValue.WriteString(")")
|
|
|
|
for {
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
default:
|
|
beans := make([]*Bean, 0, batchSize)
|
|
|
|
sess := GetEngine(ctx)
|
|
for _, f := range sqlFieldNames {
|
|
sess = sess.OrderBy(f)
|
|
}
|
|
if cond != nil {
|
|
sess = sess.Where(cond)
|
|
}
|
|
if lastKey != nil {
|
|
sess = sess.Where(
|
|
builder.Expr(fmt.Sprintf("%s > %s", rowValue.String(), rowParameterValue.String()), lastKey...))
|
|
}
|
|
|
|
if err := sess.Limit(batchSize).Find(&beans); err != nil {
|
|
return err
|
|
}
|
|
if len(beans) == 0 {
|
|
return nil
|
|
}
|
|
|
|
for _, bean := range beans {
|
|
if err := f(ctx, bean); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
lastBean := beans[len(beans)-1]
|
|
lastKey = make([]any, len(goFieldNames))
|
|
for i := range goFieldNames {
|
|
lastKey[i] = extractFieldValue(lastBean, goFieldNames[i])
|
|
}
|
|
}
|
|
}
|
|
}
|