From 11d94cc3d2129ba15c5b1b94393a7bde623da4e2 Mon Sep 17 00:00:00 2001 From: Samuel Beaulieu Date: Fri, 12 Apr 2019 14:34:09 -0500 Subject: [PATCH] (QENG-7201) Vmpooler Status API QueueProcessor Optimization Before this change we used the API /status endpoint to get specific information on pools such as the number of ready VMs and the max. This commit creates two new endpoints to get to that information much quicker 1) poolstat?pool= takes a comma separated list of pools to return, and will provide the max, ready and alias values. 2) /totalrunning will calculate the total number of running VMs across all pools --- lib/vmpooler/api/v1.rb | 65 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/lib/vmpooler/api/v1.rb b/lib/vmpooler/api/v1.rb index b10cc7b..bfd30da 100644 --- a/lib/vmpooler/api/v1.rb +++ b/lib/vmpooler/api/v1.rb @@ -341,6 +341,71 @@ module Vmpooler JSON.pretty_generate(Hash[result.sort_by { |k, _v| k }]) end + # request statistics for specific pools by passing parameter 'pool' + # with a coma separated list of pools we want to query ?pool=ABC,DEF + # returns the ready, max numbers and the aliases (if set) + get "#{api_prefix}/poolstat/?" do + content_type :json + + result = {} + + poolscopy = [] + + if params[:pool] + subpool = params[:pool].split(",") + poolscopy = pools.select do |p| + subpool.include?(p['name']) || (p['alias'] & subpool).any? + end + end + + result[:pools] = {} + + poolscopy.each do |pool| + result[:pools][pool['name']] = {} + + max = pool['size'] + aka = pool['alias'] + + result[:pools][pool['name']][:max] = max + + if aka + result[:pools][pool['name']][:alias] = aka + end + + end + + # using pipelined is much faster than querying each of the pools + res = backend.pipelined do + poolscopy.each do |pool| + backend.scard('vmpooler__ready__' + pool['name']) + end + end + + res.each_with_index { |ready, i| result[:pools][poolscopy[i]['name']][:ready] = ready } + + JSON.pretty_generate(Hash[result.sort_by { |k, _v| k }]) + end + + # requests the total number of running VMs + get "#{api_prefix}/totalrunning/?" do + content_type :json + queue = { + running: 0, + } + + # using pipelined is much faster than querying each of the pools and adding them + # as we get the result. + res = backend.pipelined do + pools.each do |pool| + backend.scard('vmpooler__running__' + pool['name']) + end + end + + queue[:running] = res.inject(0){ |m, x| m+x } + + JSON.pretty_generate(queue) + end + get "#{api_prefix}/summary/?" do content_type :json