-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Have Postgres GC lock so that it only runs on a single node at a time
This should reduce Postgres datastore CPU pressure
- Loading branch information
1 parent
4d0a80d
commit 49d31c6
Showing
9 changed files
with
183 additions
and
43 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,54 @@ | ||
package common | ||
|
||
import ( | ||
"time" | ||
|
||
"cirello.io/pglock" | ||
"github.com/jackc/pgx/v5" | ||
"github.com/jackc/pgx/v5/pgxpool" | ||
"github.com/jackc/pgx/v5/stdlib" | ||
) | ||
|
||
const ( | ||
locksTableName = "locks" | ||
heartbeatFrequency = 2 * time.Second | ||
) | ||
|
||
// RunWithLocksClient runs the provided function with a pglock.Client. Should only be used | ||
// for migrations. | ||
func RunWithLocksClient(conn *pgx.Conn, runner func(client *pglock.Client) error) error { | ||
db := stdlib.OpenDB(*conn.Config()) | ||
defer db.Close() | ||
|
||
client, err := pglock.UnsafeNew(db, pglock.WithCustomTable(locksTableName)) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := runner(client); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// RunWithLocksClientOverPool runs the provided function with a pglock.Client. | ||
func RunWithLocksClientOverPool(pool *pgxpool.Pool, timeout time.Duration, runner func(client *pglock.Client) error) error { | ||
db := stdlib.OpenDBFromPool(pool) | ||
defer db.Close() | ||
|
||
client, err := pglock.UnsafeNew(db, | ||
pglock.WithCustomTable(locksTableName), | ||
pglock.WithLeaseDuration(timeout), | ||
pglock.WithHeartbeatFrequency(heartbeatFrequency), | ||
) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if err := runner(client); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
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
22 changes: 22 additions & 0 deletions
22
internal/datastore/postgres/migrations/zz_migration.0022_add_gc_lock_table.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,22 @@ | ||
package migrations | ||
|
||
import ( | ||
"context" | ||
|
||
"cirello.io/pglock" | ||
"github.com/jackc/pgx/v5" | ||
|
||
"github.com/authzed/spicedb/internal/datastore/postgres/common" | ||
) | ||
|
||
func init() { | ||
if err := DatabaseMigrations.Register("add-gc-lock-table", "add-expiration-support", | ||
func(ctx context.Context, conn *pgx.Conn) error { | ||
return common.RunWithLocksClient(conn, func(client *pglock.Client) error { | ||
return client.TryCreateTable() | ||
}) | ||
}, | ||
noTxMigration); err != nil { | ||
panic("failed to register migration: " + err.Error()) | ||
} | ||
} |
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