Skip to content

Commit

Permalink
B #3269: onedb purge-done eat too much RAM (#3376)
Browse files Browse the repository at this point in the history
    * Process done VMs in pages, instead of all together
    * Add new method to iterate over pages
    * Add new CLI paramter to specify pages size
  • Loading branch information
Alejandro Huertas Herrero authored and Ruben S. Montero committed May 28, 2019
1 parent 2556fb2 commit b386ae4
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 24 deletions.
36 changes: 31 additions & 5 deletions src/oca/ruby/opennebula/pool.rb
Expand Up @@ -233,10 +233,12 @@ def info_paginated(size)

# Gets a hash from a info page from pool
# size:: nil => default page size
# > 0 => page size
# > 0 => page size
# current first element of the page
# extended true to get extended information
# state state of the objects
# hash:: return page as a hash
def get_page(size, current, extended = false)
def get_page(size, current, extended = false, state = -1)
rc = nil

if PAGINATED_POOLS.include?(@pool_name)
Expand All @@ -249,19 +251,43 @@ def get_page(size, current, extended = false)
end

size = OpenNebula.pool_page_size if (!size || size == 0)
rc = @client.call(method, @user_id, current, -size, -1)
rc = @client.call(method, @user_id, current, -size, state)

initialize_xml(rc, @pool_name)
else
rc = info
end

return rc
return rc
end

# Iterates over pool pages
# size:: nil => default page size
# > 0 => page size
# state state of objects
# delete true to take always the first page
def each_page(size, state = -1, extended = false, delete = false)
current = 0
element = @pool_name.split('_')[0]
page = OpenNebula::XMLElement.new

loop do
page.initialize_xml(get_page(size, current, extended, state),
@pool_name)

break if page["//#{element}"].nil?

page.each("//#{element}") do |obj|
yield(obj)
end

current += size unless delete
end
end

# Return true if pool is paginated
def is_paginated?
PAGINATED_POOLS.include?(@pool_name)
PAGINATED_POOLS.include?(@pool_name)
end
end
end
10 changes: 9 additions & 1 deletion src/onedb/onedb
Expand Up @@ -291,6 +291,14 @@ DELETE= {
:description => "Delete all matched xpaths"
}

PAGES = {
:name => 'pages',
:short => '-p pages',
:large => '--pages pages',
:description => 'Nummber of pages to purge VMs',
:format => Integer
}

cmd=CommandParser::CmdParser.new(ARGV) do
description <<-EOT.unindent
This command enables the user to manage the OpenNebula database. It
Expand Down Expand Up @@ -500,7 +508,7 @@ cmd=CommandParser::CmdParser.new(ARGV) do
EOT

command :'purge-done', purge_done_desc,
:options => [START_TIME, END_TIME] do
:options => [START_TIME, END_TIME, PAGES] do
begin
action = OneDBLive.new
action.purge_done_vm(options)
Expand Down
30 changes: 12 additions & 18 deletions src/onedb/onedb_live.rb
Expand Up @@ -4,7 +4,8 @@

class OneDBLive

EDITOR_PATH='/bin/vi'
EDITOR_PATH = '/bin/vi'
PAGES = 0

def initialize
@client = nil
Expand Down Expand Up @@ -206,30 +207,23 @@ def purge_history(options = {})
end

def purge_done_vm(options = {})
vmpool = OpenNebula::VirtualMachinePool.new(client)
vmpool.info(OpenNebula::Pool::INFO_ALL,
-1,
-1,
OpenNebula::VirtualMachine::VM_STATE.index('DONE'))

ops = {
start_time: 0,
end_time: Time.now
}.merge(options)

ops = { :start_time => 0,
:end_time => Time.now,
:pages => PAGES }.merge(options)
vmpool = OpenNebula::VirtualMachinePool.new(client)
start_time = ops[:start_time].to_i
end_time = ops[:end_time].to_i
done = OpenNebula::VirtualMachine::VM_STATE.index('DONE')

last_id = vmpool["/VM_POOL/VM[last()]/ID"]
vmpool.each_page(ops[:pages], done, false, true) do |obj|
print "VM with ID: #{obj['ID']} purged \r"

vmpool.each do |vm|
print percentage_line(vm.id, last_id, true)
time = obj['ETIME'].to_i

time = vm["ETIME"].to_i
next unless time >= start_time && time < end_time

delete("vm_pool", "oid = #{vm.id}", false)
delete("history", "vid = #{vm.id}", false)
delete('vm_pool', "oid = #{obj['ID']}", false)
delete('history', "vid = #{obj['ID']}", false)
end
end

Expand Down

0 comments on commit b386ae4

Please sign in to comment.