diff --git a/app/models/metric.rb b/app/models/metric.rb index dfb104b1c9b8..347e1c0009ec 100644 --- a/app/models/metric.rb +++ b/app/models/metric.rb @@ -2,4 +2,8 @@ class Metric < ApplicationRecord BASE_COLS = ["id", "timestamp", "capture_interval_name", "resource_type", "resource_id", "resource_name", "tag_names", "parent_host_id", "parent_ems_cluster_id", "parent_ems_id", "parent_storage_id"] include Metric::Common + + def self.reindex_table_name + "metrics_#{Time.now.utc.hour + 1}" + end end diff --git a/app/models/miq_schedule_worker/jobs.rb b/app/models/miq_schedule_worker/jobs.rb index 5a6feddceb64..4bebb5244106 100644 --- a/app/models/miq_schedule_worker/jobs.rb +++ b/app/models/miq_schedule_worker/jobs.rb @@ -143,6 +143,12 @@ def metric_purge_all_timer end end + def database_maintenance_reindex_timer + ::Settings.database.maintenance.reindex_tables.each do |class_name| + queue_work(:class_name => class_name, :method_name => "reindex", :role => "database_operations", :zone => nil) + end + end + def check_for_stuck_dispatch(threshold_seconds) class_n = "JobProxyDispatcher" method_n = "dispatch" diff --git a/app/models/miq_schedule_worker/runner.rb b/app/models/miq_schedule_worker/runner.rb index 2ee4fc5db6c2..af898fe6c3e6 100644 --- a/app/models/miq_schedule_worker/runner.rb +++ b/app/models/miq_schedule_worker/runner.rb @@ -280,6 +280,13 @@ def schedules_for_database_operations_role :tags => [:database_operations, :database_metrics_purge_schedule], ) { enqueue(:metric_purge_all_timer) } + sched = ::Settings.database.maintenance.reindex_schedule + _log.info("database_maintenance_reindex_schedule: #{sched}") + scheduler.schedule_cron( + sched, + :tags => %i(database_operations database_maintenance_reindex_schedule), + ) { enqueue(:database_maintenance_reindex_timer) } + @schedules[:database_operations] end diff --git a/config/settings.yml b/config/settings.yml index 58dd636c44dc..f34f14fa5fec 100644 --- a/config/settings.yml +++ b/config/settings.yml @@ -83,6 +83,12 @@ :scanning_job_timeout: 20.minutes :concurrent_per_ems: 3 :database: + :maintenance: + :reindex_schedule: "1 * * * *" + :reindex_tables: + - Metric + - MiqQueue + - MiqWorker :metrics_collection: :collection_schedule: "1 * * * *" :daily_rollup_schedule: "23 0 * * *" diff --git a/lib/extensions/ar_base.rb b/lib/extensions/ar_base.rb index 53e6b9fb47ea..f9223e03ba56 100644 --- a/lib/extensions/ar_base.rb +++ b/lib/extensions/ar_base.rb @@ -10,5 +10,15 @@ class Base def self.truncate connection.truncate(table_name, "#{name} Truncate") end + + def self.reindex + _log.info("Reindexing table #{reindex_table_name}") + connection.reindex_table(reindex_table_name) + _log.info("Reindexing table #{reindex_table_name}") + end + + def self.reindex_table_name + table_name + end end end diff --git a/spec/models/miq_schedule_worker/runner_spec.rb b/spec/models/miq_schedule_worker/runner_spec.rb index e539d7caa98b..2de53a172b4e 100644 --- a/spec/models/miq_schedule_worker/runner_spec.rb +++ b/spec/models/miq_schedule_worker/runner_spec.rb @@ -276,6 +276,7 @@ @metrics_collection = {:collection_schedule => "1 * * * *", :daily_rollup_schedule => "23 0 * * *"} @metrics_history = {:purge_schedule => "50 * * * *"} + @database_maintenance = {:reindex_schedule => "1 * * * *", :reindex_tables => %w(Metric MiqQueue MiqWorker)} database_config = {:metrics_collection => @metrics_collection, :metrics_history => @metrics_history} stub_server_configuration(:database => database_config) end @@ -287,7 +288,7 @@ it "queues the right items" do scheduled_jobs = @schedule_worker.schedules_for_database_operations_role - expect(scheduled_jobs.size).to be(3) + expect(scheduled_jobs.size).to be(4) scheduled_jobs.each do |job| expect(job).to be_a_kind_of(Rufus::Scheduler::CronJob) @@ -313,6 +314,13 @@ message = MiqQueue.where(:class_name => class_name, :method_name => "purge_all_timer").first expect(message).to have_attributes(:role => "database_operations", :zone => nil) end + when %w(database_operations database_maintenance_reindex_schedule) + expect(job.original).to eq(@database_maintenance[:reindex_schedule]) + expect(MiqQueue.count).to eq(3) + @database_maintenance[:reindex_tables].each do |class_name| + message = MiqQueue.where(:class_name => class_name, :method_name => "reindex").first + expect(message).to have_attributes(:role => "database_operations", :zone => nil) + end else raise_unexpected_job_error(job) end @@ -328,7 +336,7 @@ it "queues the right items" do scheduled_jobs = @schedule_worker.schedules_for_database_operations_role - expect(scheduled_jobs.size).to be(3) + expect(scheduled_jobs.size).to be(4) scheduled_jobs.each do |job| expect(job).to be_kind_of(Rufus::Scheduler::CronJob) @@ -355,6 +363,13 @@ message = MiqQueue.where(:class_name => class_name, :method_name => "purge_all_timer").first expect(message).to have_attributes(:role => "database_operations", :zone => nil) end + when %w(database_operations database_maintenance_reindex_schedule) + expect(job.original).to eq(@database_maintenance[:reindex_schedule]) + expect(MiqQueue.count).to eq(3) + @database_maintenance[:reindex_tables].each do |class_name| + message = MiqQueue.where(:class_name => class_name, :method_name => "reindex").first + expect(message).to have_attributes(:role => "database_operations", :zone => nil) + end else raise_unexpected_job_error(job) end