Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[+] make unique dbname maintenance less costly for multi-daemon setups, fixes #525 #573

Merged
merged 1 commit into from
Dec 20, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 14 additions & 2 deletions pgwatch2/pgwatch2.go
Original file line number Diff line number Diff line change
Expand Up @@ -505,9 +505,9 @@ func InitAndTestMetricStoreConnection(connStr string, failOnErr bool) error {
break
}
}
metricDb.SetMaxIdleConns(1)
metricDb.SetMaxIdleConns(2)
metricDb.SetMaxOpenConns(2)
metricDb.SetConnMaxLifetime(time.Second * time.Duration(PG_CONN_RECYCLE_SECONDS))
metricDb.SetConnMaxLifetime(time.Second * 172800) // 2d
return nil
}

Expand Down Expand Up @@ -1513,6 +1513,7 @@ func AddDBUniqueMetricToListingTable(db_unique, metric string) error {

func UniqueDbnamesListingMaintainer(daemonMode bool) {
// due to metrics deletion the listing can go out of sync (a trigger not really wanted)
sql_get_advisory_lock := `SELECT pg_try_advisory_lock(1571543679778230000) AS have_lock` // 1571543679778230000 is just a random bigint
sql_top_level_metrics := `SELECT table_name FROM admin.get_top_level_metric_tables()`
sql_distinct := `
WITH RECURSIVE t(dbname) AS (
Expand All @@ -1534,6 +1535,17 @@ func UniqueDbnamesListingMaintainer(daemonMode bool) {
time.Sleep(time.Hour * 24)
}

log.Infof("Trying to get metricsDb listing maintaner advisory lock...") // to only have one "maintainer" in case of a "push" setup, as can get costly
lock, err := DBExecRead(metricDb, METRICDB_IDENT, sql_get_advisory_lock)
if err != nil {
log.Error("Getting metricsDb listing maintaner advisory lock failed:", err)
continue
}
if !(lock[0]["have_lock"].(bool)) {
log.Info("Skipping admin.all_distinct_dbname_metrics maintenance as another instance has the advisory lock...")
continue
}

log.Infof("Refreshing admin.all_distinct_dbname_metrics listing table...")
all_distinct_metric_tables, err := DBExecRead(metricDb, METRICDB_IDENT, sql_top_level_metrics)
if err != nil {
Expand Down