Skip to content

Commit

Permalink
Merge pull request #16929 from jntullo/db_maintenance
Browse files Browse the repository at this point in the history
Add reindex to job scheduler
(cherry picked from commit da512b2)

https://bugzilla.redhat.com/show_bug.cgi?id=1553795
  • Loading branch information
carbonin authored and simaishi committed Mar 9, 2018
1 parent 920e709 commit f1c1203
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 2 deletions.
4 changes: 4 additions & 0 deletions app/models/metric.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 6 additions & 0 deletions app/models/miq_schedule_worker/jobs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,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"
Expand Down
7 changes: 7 additions & 0 deletions app/models/miq_schedule_worker/runner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,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

Expand Down
6 changes: 6 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,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 * * *"
Expand Down
10 changes: 10 additions & 0 deletions lib/extensions/ar_base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 17 additions & 2 deletions spec/models/miq_schedule_worker/runner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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
Expand Down

0 comments on commit f1c1203

Please sign in to comment.