diff --git a/src/onedb/fsck/history.rb b/src/onedb/fsck/history.rb index cbe0cb7069..5e69f2a93c 100644 --- a/src/onedb/fsck/history.rb +++ b/src/onedb/fsck/history.rb @@ -13,6 +13,8 @@ module OneDBFsck check_history_retime + check_history_seq + log_error('Removing possibly corrupted records from VM showback '\ "please run 'oneshowback calculate` to recalculate "\ 'the showback') unless @showback_delete.empty? @@ -144,6 +146,26 @@ module OneDBFsck end end + def check_history_seq + @history_delete = {} + + # Query to select history elements with max seq + @db.fetch('SELECT vid, MAX(seq) AS max_seq ' \ + 'FROM history ' \ + 'GROUP BY vid') do |row| + # Skip iteration if VM doesn't have last seq, it should never happen + last_seq = @vms_last_history[row[:vid]] + next if last_seq.nil? + + if row[:max_seq] != last_seq + log_error("VM #{row[:vid]} history last seq # #{last_seq} "\ + "doesn't match last seq in DB # #{row[:max_seq]}") + + @history_delete[row[:vid]] = [last_seq + 1, row[:max_seq]] + end + end + end + # Fix the broken history records def fix_history # DATA: FIX: update history records with fixed data @@ -155,6 +177,12 @@ module OneDBFsck end end + @db.transaction do + @history_delete.each do |vid, seq| + @db[:history].where(:vid => vid, :seq => seq[0]..seq[1]).delete + end + end + @db.transaction do @showback_delete.each do |vid| @db[:vm_showback].where(:vmid => vid).delete diff --git a/src/onedb/fsck/vm.rb b/src/onedb/fsck/vm.rb index d280620b6e..52f96d4cf6 100644 --- a/src/onedb/fsck/vm.rb +++ b/src/onedb/fsck/vm.rb @@ -8,6 +8,7 @@ module OneDBFsck cluster_vnc = @data_vm[:vnc] = {} @vms_ports = {} + @vms_last_history = Hash.new # DATA: Aggregate information of the RUNNING vms @db.fetch("SELECT oid,body FROM vm_pool WHERE state<>6") do |row| @@ -18,6 +19,10 @@ module OneDBFsck state = vm_doc.root.at_xpath('STATE').text.to_i lcm_state = vm_doc.root.at_xpath('LCM_STATE').text.to_i + # Get last history record + seq = vm_doc.root.at_xpath('HISTORY_RECORDS/HISTORY[last()]/SEQ').text.to_i rescue nil + @vms_last_history[row[:oid]] = seq unless seq.nil? + # DATA: VNC ports per cluster cid = vm_doc.root.at_xpath("HISTORY_RECORDS/HISTORY[last()]/CID").text.to_i rescue nil port = vm_doc.root.at_xpath('TEMPLATE/GRAPHICS[translate(TYPE,"vnc","VNC")="VNC"]/PORT').text.to_i rescue nil