Skip to content

Commit b4a2312

Browse files
authored
Merge pull request #4004 from rbrw/pdb-5672-analyze-parent-partitions
(PDB-5672) Periodically analyze reports table
2 parents 8f0406e + af3348d commit b4a2312

File tree

5 files changed

+62
-5
lines changed

5 files changed

+62
-5
lines changed

ext/bin/prep-debianish-root

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ apt-get-update() {
3030
install-pg() {
3131
local need_pgdg=
3232
for pkg in "$@"; do
33-
if ! apt-cahe show "$pkg"; then
33+
if ! apt-cache show "$pkg"; then
3434
need_pgdg=1
3535
break
3636
fi
3737
done
3838
if test "$need_pgdg"; then
39+
apt install -y lsb-release gnupg2
3940
curl https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add -
4041
echo "deb http://apt.postgresql.org/pub/repos/apt $(lsb_release -sc)-pgdg main" \
4142
> /etc/apt/sources.list.d/pdb-pgdg.list

src/puppetlabs/puppetdb/cli/services.clj

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,15 @@
3838
data may linger in the database. We periodically sweep the
3939
database, compacting it and performing regular cleanup so we can
4040
maintain acceptable performance."
41-
(:require [clojure.string :as str]
41+
(:require [clojure.java.jdbc :as sql]
42+
[clojure.string :as str]
4243
[clojure.tools.logging :as log]
4344
[metrics.counters :as counters :refer [counter inc!]]
4445
[metrics.gauges :refer [gauge-fn]]
4546
[metrics.timers :refer [time! timer]]
4647
[metrics.reporters.jmx :as jmx-reporter]
4748
[murphy :refer [try! with-final]]
49+
[next.jdbc :as nxt]
4850
[puppetlabs.i18n.core :refer [trs tru]]
4951
[puppetlabs.kitchensink.core :as kitchensink]
5052
[puppetlabs.puppetdb.cli.tk-util :refer [run-tk-cli-cmd]]
@@ -939,6 +941,21 @@
939941
(catch InterruptedException _
940942
(log/info (trs "Garbage collector interrupted")))))))
941943

944+
(defn analyze-partitioned-tables [db shutdown-for-ex]
945+
(with-nonfatal-exceptions-suppressed
946+
(with-monitored-execution shutdown-for-ex
947+
(try!
948+
(jdbc/with-db-connection db
949+
(jdbc/with-db-transaction []
950+
(let [c (sql/db-connection jdbc/*db*)]
951+
(doseq [table ["reports" "resource_events"]]
952+
(try!
953+
(nxt/execute! c [(str "analyze " table)])
954+
(catch InterruptedException _
955+
(log/info (trs (str table " analysis interrupted")))))))))
956+
(catch Exception ex
957+
(log/error ex))))))
958+
942959
(defn start-garbage-collection
943960
"Starts garbage collection of the databases represented in db-configs"
944961
[{:keys [clean-lock] :as _context}
@@ -952,7 +969,18 @@
952969
(schedule-with-fixed-delay sched #(invoke-periodic-gc db cfg request
953970
shutdown-for-ex
954971
clean-lock lock-status)
955-
(to-millis interval) (to-millis interval)))))))
972+
(to-millis interval) (to-millis interval))))
973+
;; pg (up to at least 16) never analyzes partitioned table parents
974+
;; Nearly fixed in 14, but removed before release:
975+
;; https://www.postgresql.org/about/news/postgresql-14-beta-1-released-2213/
976+
;; https://www.postgresql.org/about/news/postgresql-14-rc-1-released-2309/
977+
;;
978+
;; Assumes the analysis runs quickly enough to avoid needing any
979+
;; enforced serialization, and to avoid (with the current single
980+
;; threaded executor) ever delaying gc enough to matter.
981+
(let [hourly (* 60 60 1000)
982+
analyze #(analyze-partitioned-tables db shutdown-for-ex)]
983+
(schedule-with-fixed-delay sched analyze 0 hourly)))))
956984

957985

958986
(defn database-lock-status []

src/puppetlabs/puppetdb/query/monitor.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@
355355
(reset! exit true)
356356
(.wakeup selector)
357357
(if timeout-ms
358-
(.join thread timeout-ms)
358+
(.join thread ^long timeout-ms)
359359
(.join thread))
360360
(not (.isAlive thread))))))
361361

src/puppetlabs/puppetdb/scf/storage.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@
373373
"Returns the id (primary key) from the table for the row with col = name."
374374
[table col name]
375375
(->> {:select :id :from table :where [:= col name]}
376-
hsql/format (select-one! (jdbc/connection) :id)))
376+
hsql/format (select-one! (jdbc/connection) :id)))
377377

378378
(defn environment-id [name] (named-row-id :environments :environment name))
379379
(defn certname-id [name] (named-row-id :certnames :certname name))

test/puppetlabs/puppetdb/cli/services_test.clj

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
(ns puppetlabs.puppetdb.cli.services-test
22
(:require [clojure.set :refer [subset?]]
3+
[next.jdbc.plan :refer [select-one!]]
34
[puppetlabs.http.client.sync :as pl-http]
45
[puppetlabs.puppetdb.cli.util :refer [err-exit-status]]
56
[puppetlabs.puppetdb.command.constants :as cmd-consts]
@@ -620,3 +621,30 @@
620621
:db-lock-status db-lock-status})
621622
(is (= 1 (count (jdbc/query ["SELECT * FROM reports_latest"]))))
622623
(is (empty? (jdbc/query ["SELECT * FROM reports_historical"])))))))))
624+
625+
(deftest reports-analysis
626+
;; For now, just test for the initial invocation
627+
(let [start (now)
628+
is-analyzed
629+
(fn is-analyzed
630+
([table] (is-analyzed table 0))
631+
([table i]
632+
(let [r (jdbc/with-db-transaction []
633+
(->> [(str "select last_analyze, last_autoanalyze"
634+
" from pg_stat_user_tables where relname = '" table "'")]
635+
(select-one! (jdbc/connection) [:last_analyze :last_autoanalyze])))
636+
last-ms (some-> r :last_analyze .getTime time/from-long)]
637+
(if (and last-ms (nil? (:last_autoanalyze r)))
638+
(do
639+
(is (= nil (:last_autoanalyze r)))
640+
(is (time/after? last-ms start)))
641+
(if (= i 100)
642+
(is false (str table " was eventually analyzed"))
643+
(do
644+
(Thread/sleep 100)
645+
(is-analyzed table (inc i))))))))]
646+
(svc-utils/with-puppetdb-instance
647+
(doseq [parent ["reports" "reports_historical" "reports_latest"
648+
"resource_events" "resource_events_historical" "resource_events_latest"]]
649+
(testing (str parent " analysis times")
650+
(is-analyzed parent))))))

0 commit comments

Comments
 (0)