-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Garbage Collection improvements in MySQL and Postgres
1) Have GC lock so that it only runs on a single node at a time 2) Add a missing index in the Postgres datastore for GC This should reduce datastore CPU pressure
- Loading branch information
1 parent
4d0a80d
commit c5d52b1
Showing
11 changed files
with
301 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package mysql | ||
|
||
import "context" | ||
|
||
type lockName string | ||
|
||
const ( | ||
// gcRunLock is the lock name for the garbage collection run. | ||
gcRunLock lockName = "gc_run" | ||
) | ||
|
||
func (mds *Datastore) tryAcquireLock(ctx context.Context, lockName lockName) (bool, error) { | ||
// Acquire the lock, with max 1s timeout. | ||
row := mds.db.QueryRowContext(ctx, ` | ||
SELECT GET_LOCK(?, 1) | ||
`, lockName) | ||
|
||
var acquired int | ||
if err := row.Scan(&acquired); err != nil { | ||
return false, err | ||
} | ||
|
||
return acquired == 1, nil | ||
} | ||
|
||
func (mds *Datastore) releaseLock(ctx context.Context, lockName lockName) error { | ||
_, err := mds.db.ExecContext(ctx, ` | ||
SELECT RELEASE_LOCK(?) | ||
`, | ||
lockName, | ||
) | ||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package postgres | ||
|
||
import "context" | ||
|
||
type lockID uint32 | ||
|
||
const ( | ||
// gcRunLock is the lock ID for the garbage collection run. | ||
gcRunLock lockID = 1 | ||
) | ||
|
||
func (pgd *pgDatastore) tryAcquireLock(ctx context.Context, lockID lockID) (bool, error) { | ||
// Acquire the lock. | ||
row := pgd.writePool.QueryRow(ctx, ` | ||
SELECT pg_try_advisory_lock($1) | ||
`, lockID) | ||
|
||
var lockAcquired bool | ||
if err := row.Scan(&lockAcquired); err != nil { | ||
return false, err | ||
} | ||
return lockAcquired, nil | ||
} | ||
|
||
func (pgd *pgDatastore) releaseLock(ctx context.Context, lockID lockID) error { | ||
_, err := pgd.writePool.Exec(ctx, ` | ||
SELECT pg_advisory_unlock($1) | ||
`, lockID) | ||
return err | ||
} |
39 changes: 39 additions & 0 deletions
39
internal/datastore/postgres/migrations/zz_migration.0022_add_missing_gc_index.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/jackc/pgx/v5" | ||
) | ||
|
||
// addGCIndexForRelationTupleTransaction adds a missing index to relation_tuple_transaction table | ||
// to support garbage collection. This is in support of the query for selecting the most recent | ||
// transaction: `SELECT xid, snapshot FROM relation_tuple_transaction WHERE timestamp < $1 ORDER BY xid DESC LIMIT 1` | ||
// | ||
// EXPLAIN before the index: | ||
// Limit (cost=0.56..1.78 rows=1 width=558) (actual time=5706.155..5706.156 rows=1 loops=1) | ||
// -> Index Scan Backward using pk_rttx on relation_tuple_transaction (cost=0.56..30428800.04 rows=25023202 width=558) (actual time=5706.154..5706.155 rows=1 loops=1) | ||
// | ||
// Filter: ("timestamp" < (now() - '04:00:00'::interval)) | ||
// Rows Removed by Filter: 6638121 | ||
// | ||
// Planning Time: 0.098 ms | ||
// Execution Time: 5706.192 ms | ||
// (6 rows) | ||
const addGCIndexForRelationTupleTransaction = `CREATE INDEX CONCURRENTLY | ||
IF NOT EXISTS ix_relation_tuple_transaction_xid_desc_timestamp | ||
ON relation_tuple_transaction (xid DESC, timestamp);` | ||
|
||
func init() { | ||
if err := DatabaseMigrations.Register("add-missing-gc-index", "add-expiration-support", | ||
func(ctx context.Context, conn *pgx.Conn) error { | ||
if _, err := conn.Exec(ctx, addGCIndexForRelationTupleTransaction); err != nil { | ||
return fmt.Errorf("failed to add missing GC index: %w", err) | ||
} | ||
return nil | ||
}, | ||
noTxMigration); err != nil { | ||
panic("failed to register migration: " + err.Error()) | ||
} | ||
} |
Oops, something went wrong.