From c0aaec92f1d5f67cb992dab7b74ec92e1976d04c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Mart=C3=ADn?= Date: Wed, 9 Jul 2014 11:04:59 +0200 Subject: [PATCH] Feature #2858: Remove leases fixes from fsck --- src/onedb/fsck.rb | 178 ++++------------------------------------------ 1 file changed, 12 insertions(+), 166 deletions(-) diff --git a/src/onedb/fsck.rb b/src/onedb/fsck.rb index 8944bbf159..8ad66aec0e 100644 --- a/src/onedb/fsck.rb +++ b/src/onedb/fsck.rb @@ -23,7 +23,7 @@ require 'nokogiri' module OneDBFsck VERSION = "4.6.0" - LOCAL_VERSION = "4.5.80" + LOCAL_VERSION = "4.7.80" def check_db_version() db_version = read_db_version() @@ -768,8 +768,7 @@ EOT counters[:vnet][row[:oid]] = { :type => doc.root.get_text('TYPE').to_s.to_i, - :total_leases => 0, - :leases => {} + :ar_leases => {} } end @@ -808,12 +807,20 @@ EOT log_error("VM #{row[:oid]} is using VNet #{net_id}, "<< "but it does not exist") else - counters[:vnet][net_id][:leases][e.at_xpath('IP').text] = +=begin + ar_id_e = e.at_xpath('AR_ID') + ar_id = ar_id_e.nil? ? -1 : ar_id_e.text.to_i + + if counters[:vnet][net_id][:ar_leases][ar_id].nil? + counters[:vnet][net_id][:ar_leases][ar_id] = [] + end + + counters[:vnet][net_id][:ar_leases][ar_id] << [ e.at_xpath('MAC').text, # MAC - "1", # USED vm_doc.root.at_xpath('ID').text.to_i # VID ] +=end end end end @@ -1104,167 +1111,6 @@ EOT log_time() - ######################################################################## - # VNet - # - # LEASES - ######################################################################## - - @db.run "CREATE TABLE leases_new (oid INTEGER, ip BIGINT, body MEDIUMTEXT, PRIMARY KEY(oid,ip));" - - @db.transaction do - @db[:leases].each do |row| - doc = Nokogiri::XML(row[:body]){|c| c.default_xml.noblanks} - - used = (doc.root.at_xpath('USED').text == "1") - vid = doc.root.at_xpath('VID').text.to_i - - ip_str = IPAddr.new(row[:ip], Socket::AF_INET).to_s - - vnet_structure = counters[:vnet][row[:oid]] - - if vnet_structure.nil? - log_error("Table leases contains the lease #{ip_str} "<< - "for VNet #{row[:oid]}, but it does not exit") - - next - end - - ranged = vnet_structure[:type] == 0 - - counter_mac, counter_used, counter_vid = - vnet_structure[:leases][ip_str] - - vnet_structure[:leases].delete(ip_str) - - insert = true - - if used && (vid != -1) # Lease used by a VM - if counter_mac.nil? - log_error( - "VNet #{row[:oid]} has used lease #{ip_str} "<< - "(VM #{vid}) \tbut it is free") - - if ranged - insert = false - end - - doc.root.at_xpath("USED").content = "0" - - doc.root.at_xpath("VID").content = "-1" - - row[:body] = doc.root.to_s - - elsif vid != counter_vid - log_error( - "VNet #{row[:oid]} has used lease #{ip_str} "<< - "(VM #{vid}) \tbut it used by VM #{counter_vid}") - - doc.root.at_xpath("VID").content = counter_vid.to_s - - row[:body] = doc.root.to_s - end - else # Lease is free or on hold (used=1, vid=-1) - if !counter_mac.nil? - if used - log_error( - "VNet #{row[:oid]} has lease on hold #{ip_str} "<< - "\tbut it is used by VM #{counter_vid}") - else - log_error( - "VNet #{row[:oid]} has free lease #{ip_str} "<< - "\tbut it is used by VM #{counter_vid}") - end - - doc.root.at_xpath("USED").content = "1" - - doc.root.at_xpath("VID").content = counter_vid.to_s - - row[:body] = doc.root.to_s - end - end - - if (doc.root.at_xpath('USED').text == "1") - vnet_structure[:total_leases] += 1 - end - - # commit - @db[:leases_new].insert(row) if insert - end - end - - log_time() - - # Now insert all the leases left in the hash, i.e. used by a VM in - # vm_pool, but not in the leases table. This will only happen in - # ranged networks - @db.transaction do - counters[:vnet].each do |net_id,vnet_structure| - vnet_structure[:leases].each do |ip,array| - mac,used,vid = array - - ip_i = IPAddr.new(ip, Socket::AF_INET).to_i - - # TODO: MAC_PREFIX is now hardcoded to "02:00" - body = "#{ip_i}512#{ip_i}#{used}#{vid}" - - log_error("VNet #{net_id} has free lease #{ip} \tbut it is used by VM #{vid}") - - vnet_structure[:total_leases] += 1 - - @db[:leases_new].insert( - :oid => net_id, - :ip => ip_i, - :body => body) - end - end - end - - - # Rename table - @db.run("DROP TABLE leases") - @db.run("ALTER TABLE leases_new RENAME TO leases") - - log_time() - - ######################################################################## - # VNet - # - # VNET/TOTAL_LEASES - ######################################################################## - - # Create a new empty table where we will store the new calculated values - @db.run "CREATE TABLE network_pool_new (oid INTEGER PRIMARY KEY, name VARCHAR(128), body MEDIUMTEXT, uid INTEGER, gid INTEGER, owner_u INTEGER, group_u INTEGER, other_u INTEGER, cid INTEGER, UNIQUE(name,uid));" - - @db.transaction do - @db[:network_pool].each do |row| - doc = Document.new(row[:body]) - - oid = row[:oid] - - total_leases = counters[:vnet][oid][:total_leases] - - # rewrite running_vms - doc.root.each_element("TOTAL_LEASES") {|e| - if e.text != total_leases.to_s - log_error("VNet #{oid} TOTAL_LEASES has #{e.text} \tis\t#{total_leases}") - e.text = total_leases - end - } - - row[:body] = doc.to_s - - # commit - @db[:network_pool_new].insert(row) - end - end - - # Rename table - @db.run("DROP TABLE network_pool") - @db.run("ALTER TABLE network_pool_new RENAME TO network_pool") - - log_time() - ######################################################################## # Users #