Skip to content

Commit

Permalink
(PDB-5672) Analyze reports every hour
Browse files Browse the repository at this point in the history
postgres (up to at least 16) never analyzes partitioned table parents
Nearly fixed in 14, but removed before release:
  https://www.postgresql.org/about/news/postgresql-14-beta-1-released-2213/
  https://www.postgresql.org/about/news/postgresql-14-rc-1-released-2309/

Assumes the analysis runs quickly enough to avoid needing any enforced
serialization, and to avoid (with the current single threaded
executor) ever delaying gc enough to matter.
  • Loading branch information
rbrw committed Sep 24, 2024
1 parent d9c2f5c commit 2311b05
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/puppetlabs/puppetdb/cli/services.clj
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
[metrics.timers :refer [time! timer]]
[metrics.reporters.jmx :as jmx-reporter]
[murphy :refer [try! with-final]]
[next.jdbc :as nxt]
[puppetlabs.i18n.core :refer [trs tru]]
[puppetlabs.kitchensink.core :as kitchensink]
[puppetlabs.puppetdb.cli.tk-util :refer [run-tk-cli-cmd]]
Expand Down Expand Up @@ -939,6 +940,18 @@
(catch InterruptedException _
(log/info (trs "Garbage collector interrupted")))))))

(defn analyze-partitioned-tables [db shutdown-for-ex]
(with-nonfatal-exceptions-suppressed
(with-monitored-execution shutdown-for-ex
(try!
(jdbc/with-transacted-connection db
;; REVIEW: timeout here?
(nxt/execute! (jdbc/connection) ["analyze reports"]))
(catch InterruptedException _
(log/info (trs "reports analysis interrupted")))
(catch Exception ex
(log/error ex))))))

(defn start-garbage-collection
"Starts garbage collection of the databases represented in db-configs"
[{:keys [clean-lock] :as _context}
Expand All @@ -952,7 +965,18 @@
(schedule-with-fixed-delay sched #(invoke-periodic-gc db cfg request
shutdown-for-ex
clean-lock lock-status)
(to-millis interval) (to-millis interval)))))))
(to-millis interval) (to-millis interval))))
;; pg (up to at least 16) never analyzes partitioned table parents
;; Nearly fixed in 14, but removed before release:
;; https://www.postgresql.org/about/news/postgresql-14-beta-1-released-2213/
;; https://www.postgresql.org/about/news/postgresql-14-rc-1-released-2309/
;;
;; Assumes the analysis runs quickly enough to avoid needing any
;; enforced serialization, and to avoid (with the current single
;; threaded executor) ever delaying gc enough to matter.
(let [hourly (* 60 60 1000)
analyze #(analyze-partitioned-tables db shutdown-for-ex)]
(schedule-with-fixed-delay sched analyze 0 hourly)))))


(defn database-lock-status []
Expand Down
21 changes: 21 additions & 0 deletions test/puppetlabs/puppetdb/cli/services_test.clj
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
(ns puppetlabs.puppetdb.cli.services-test
(:require [clojure.set :refer [subset?]]
[next.jdbc.plan :refer [select-one!]]
[puppetlabs.http.client.sync :as pl-http]
[puppetlabs.puppetdb.cli.util :refer [err-exit-status]]
[puppetlabs.puppetdb.command.constants :as cmd-consts]
Expand Down Expand Up @@ -620,3 +621,23 @@
:db-lock-status db-lock-status})
(is (= 1 (count (jdbc/query ["SELECT * FROM reports_latest"]))))
(is (empty? (jdbc/query ["SELECT * FROM reports_historical"])))))))))

(deftest reports-analysis
;; For now, just test for the initial invocation
(let [start (now)
orig-analyze svcs/analyze-partitioned-tables
finished (promise)
analyze (fn [& args]
(let [result (apply orig-analyze args)]
(deliver finished true)
result))]
(with-redefs [svcs/analyze-partitioned-tables analyze]
(svc-utils/with-puppetdb-instance
(is (deref finished default-timeout-ms nil))
(jdbc/with-db-transaction []
(let [r (->> [(str "select last_analyze, last_autoanalyze"
" from pg_stat_user_tables where relname = 'reports'")]
(select-one! (jdbc/connection) [:last_analyze :last_autoanalyze]))]
(is (= nil (:last_autoanalyze r)))
(is (time/after? (-> r :last_analyze .getTime time/from-long)
start))))))))

0 comments on commit 2311b05

Please sign in to comment.