From 08d88e0119dc45b22985eea7e7a5a9172b2b47ba Mon Sep 17 00:00:00 2001 From: "Ruben S. Montero" Date: Sat, 12 Nov 2016 21:18:25 +0100 Subject: [PATCH] F #4901: Updates MEM & CPU when RESERVED_ attributes are updated. Adds TOTAL_CPU and TOTAL_MEM shares to store raw monitoring cpu/mem metrics. Adds migrator and increases local DB version number. --- include/HostShare.h | 17 ++++- include/Nebula.h | 2 +- install.sh | 6 +- src/cli/one_helper/onehost_helper.rb | 19 +++-- src/host/Host.cc | 2 + src/host/HostShare.cc | 24 ++++++ src/onedb/local/4.90.0_to_5.2.0.rb | 105 +++++++++++++++++++++++++++ src/onedb/local/db_schema.rb | 48 ++++++++++++ 8 files changed, 211 insertions(+), 12 deletions(-) create mode 100644 src/onedb/local/4.90.0_to_5.2.0.rb create mode 100644 src/onedb/local/db_schema.rb diff --git a/include/HostShare.h b/include/HostShare.h index eb324e4248d..0f2402f62aa 100644 --- a/include/HostShare.h +++ b/include/HostShare.h @@ -318,6 +318,9 @@ class HostShare : public ObjectXML */ void reset_capacity() { + total_cpu = 0; + total_mem = 0; + max_cpu = 0; max_mem = 0; @@ -342,6 +345,13 @@ class HostShare : public ObjectXML */ void set_capacity(Host *host, const string& crcpu, const string& crmem); + /** + * Update the capacity attributes when the RESERVED_CPU and RESERVED_MEM + * are updated. + * @param host for this share + */ + void update_capacity(Host *host); + /** * Return the number of running VMs in this host */ @@ -356,9 +366,12 @@ class HostShare : public ObjectXML long long mem_usage; /**< Memory allocated to VMs (in KB) */ long long cpu_usage; /**< CPU allocated to VMs (in percentage) */ + long long total_mem; /**< Total memory capacity (in KB) */ + long long total_cpu; /**< Total cpu capacity (in percentage) */ + long long max_disk; /**< Total disk capacity (in MB) */ - long long max_mem; /**< Total memory capacity (in KB) */ - long long max_cpu; /**< Total cpu capacity (in percentage) */ + long long max_mem; /**< Total memory capacity (in KB) +/- reserved */ + long long max_cpu; /**< Total cpu capacity (in percentage) +/- reserved*/ long long free_disk; /**< Free disk from the IM monitor */ long long free_mem; /**< Free memory from the IM monitor */ diff --git a/include/Nebula.h b/include/Nebula.h index 46dcb05db55..d3a9a91ba33 100644 --- a/include/Nebula.h +++ b/include/Nebula.h @@ -353,7 +353,7 @@ class Nebula */ static string local_db_version() { - return "4.90.0"; + return "5.2.0"; } /** diff --git a/install.sh b/install.sh index 0c7725e186a..559eb8a72ba 100755 --- a/install.sh +++ b/install.sh @@ -1155,13 +1155,15 @@ ONEDB_SHARED_MIGRATOR_FILES="src/onedb/shared/2.0_to_2.9.80.rb \ src/onedb/shared/4.11.80_to_4.90.0.rb \ src/onedb/shared/4.90.0_to_5.2.0.rb" -ONEDB_LOCAL_MIGRATOR_FILES="src/onedb/local/4.5.80_to_4.7.80.rb \ +ONEDB_LOCAL_MIGRATOR_FILES="src/onedb/local/db_schema.rb \ + src/onedb/local/4.5.80_to_4.7.80.rb \ src/onedb/local/4.7.80_to_4.9.80.rb \ src/onedb/local/4.9.80_to_4.10.3.rb \ src/onedb/local/4.10.3_to_4.11.80.rb \ src/onedb/local/4.11.80_to_4.13.80.rb \ src/onedb/local/4.13.80_to_4.13.85.rb \ - src/onedb/local/4.13.85_to_4.90.0.rb" + src/onedb/local/4.13.85_to_4.90.0.rb \ + src/onedb/local/4.90.0_to_5.2.0.rb" ONEDB_PATCH_FILES="src/onedb/patches/4.14_monitoring.rb \ src/onedb/patches/history_times.rb" diff --git a/src/cli/one_helper/onehost_helper.rb b/src/cli/one_helper/onehost_helper.rb index c3bb32e455f..714b45255a2 100644 --- a/src/cli/one_helper/onehost_helper.rb +++ b/src/cli/one_helper/onehost_helper.rb @@ -382,14 +382,19 @@ def format_resource(host, options = {}) puts CLIHelper.print_header(str_h1 % "HOST SHARES", false) - - puts str % ["TOTAL MEM", OpenNebulaHelper.unit_to_str(host['HOST_SHARE/MAX_MEM'].to_i, {})] - puts str % ["USED MEM (REAL)", OpenNebulaHelper.unit_to_str(host['HOST_SHARE/USED_MEM'].to_i, {})] - puts str % ["USED MEM (ALLOCATED)", OpenNebulaHelper.unit_to_str(host['HOST_SHARE/MEM_USAGE'].to_i, {})] - puts str % ["TOTAL CPU", host['HOST_SHARE/MAX_CPU']] - puts str % ["USED CPU (REAL)", host['HOST_SHARE/USED_CPU']] - puts str % ["USED CPU (ALLOCATED)", host['HOST_SHARE/CPU_USAGE']] puts str % ["RUNNING VMS", host['HOST_SHARE/RUNNING_VMS']] + + CLIHelper.print_header(str_h1 % "MEMORY", false) + puts str % [" TOTAL", OpenNebulaHelper.unit_to_str(host['HOST_SHARE/TOTAL_MEM'].to_i, {})] + puts str % [" TOTAL +/- RESERVED", OpenNebulaHelper.unit_to_str(host['HOST_SHARE/MAX_MEM'].to_i, {})] + puts str % [" USED (REAL)", OpenNebulaHelper.unit_to_str(host['HOST_SHARE/USED_MEM'].to_i, {})] + puts str % [" USED (ALLOCATED)", OpenNebulaHelper.unit_to_str(host['HOST_SHARE/MEM_USAGE'].to_i, {})] + + CLIHelper.print_header(str_h1 % "CPU", false) + puts str % [" TOTAL", host['HOST_SHARE/TOTAL_CPU']] + puts str % [" TOTAL +/- RESERVED", host['HOST_SHARE/MAX_CPU']] + puts str % [" USED (REAL)", host['HOST_SHARE/USED_CPU']] + puts str % [" USED (ALLOCATED)", host['HOST_SHARE/CPU_USAGE']] puts datastores = host.to_hash['HOST']['HOST_SHARE']['DATASTORES']['DS'] diff --git a/src/host/Host.cc b/src/host/Host.cc index 967a6a37c0e..62e2bfa1edf 100644 --- a/src/host/Host.cc +++ b/src/host/Host.cc @@ -740,5 +740,7 @@ int Host::post_update_template(string& error) replace_template_attribute("IM_MAD", im_mad_name); replace_template_attribute("VM_MAD", vmm_mad_name); + host_share.update_capacity(this); + return 0; }; diff --git a/src/host/HostShare.cc b/src/host/HostShare.cc index a4574874310..781c4eeef03 100644 --- a/src/host/HostShare.cc +++ b/src/host/HostShare.cc @@ -376,6 +376,7 @@ ostream& operator<<(ostream& os, const HostSharePCI& pci) return os; } + /* ************************************************************************ */ /* HostShare :: Constructor/Destructor */ /* ************************************************************************ */ @@ -385,6 +386,8 @@ HostShare::HostShare(long long _max_disk,long long _max_mem,long long _max_cpu): disk_usage(0), mem_usage(0), cpu_usage(0), + total_mem(_max_mem), + total_cpu(_max_cpu), max_disk(_max_disk), max_mem(_max_mem), max_cpu(_max_cpu), @@ -417,6 +420,8 @@ string& HostShare::to_xml(string& xml) const << "" << disk_usage << "" << "" << mem_usage << "" << "" << cpu_usage << "" + << "" << total_mem << "" + << "" << total_cpu << "" << "" << max_disk << "" << "" << max_mem << "" << "" << max_cpu << "" @@ -451,6 +456,9 @@ int HostShare::from_xml_node(const xmlNodePtr node) rc += xpath(mem_usage, "/HOST_SHARE/MEM_USAGE", -1); rc += xpath(cpu_usage, "/HOST_SHARE/CPU_USAGE", -1); + rc += xpath(total_mem , "/HOST_SHARE/TOTAL_MEM", -1); + rc += xpath(total_cpu, "/HOST_SHARE/TOTAL_CPU", -1); + rc += xpath(max_disk, "/HOST_SHARE/MAX_DISK", -1); rc += xpath(max_mem , "/HOST_SHARE/MAX_MEM", -1); rc += xpath(max_cpu , "/HOST_SHARE/MAX_CPU", -1); @@ -593,9 +601,11 @@ void HostShare::set_capacity(Host *host, const string& cluster_rcpu, } host->erase_template_attribute("TOTALCPU", val); + total_cpu = val; set_reserved_metric(max_cpu, val, host_rcpu); host->erase_template_attribute("TOTALMEMORY", val); + total_mem = val; set_reserved_metric(max_mem, val, host_rmem); host->erase_template_attribute("DS_LOCATION_TOTAL_MB", val); @@ -620,6 +630,20 @@ void HostShare::set_capacity(Host *host, const string& cluster_rcpu, used_disk = val; } +/* -------------------------------------------------------------------------- */ + +void HostShare::update_capacity(Host *host) +{ + string host_rcpu; + string host_rmem; + + host->get_reserved_capacity(host_rcpu, host_rmem); + + set_reserved_metric(max_cpu, total_cpu, host_rcpu); + + set_reserved_metric(max_mem, total_mem, host_rmem); +} + /* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */ diff --git a/src/onedb/local/4.90.0_to_5.2.0.rb b/src/onedb/local/4.90.0_to_5.2.0.rb new file mode 100644 index 00000000000..0404eee8dea --- /dev/null +++ b/src/onedb/local/4.90.0_to_5.2.0.rb @@ -0,0 +1,105 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2016, OpenNebula Project, OpenNebula Systems # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + + +require 'set' +require 'base64' +require 'zlib' +require 'pathname' + +require 'opennebula' + +$: << File.dirname(__FILE__) +require 'db_schema' + +include OpenNebula + +module Migrator + def db_version + "5.2.0" + end + + def one_version + "OpenNebula 5.2.0" + end + + def up + init_log_time() + + feature_4901() + + log_time() + + return true + end + + private + + def xpath(doc, sxpath) + element = doc.root.at_xpath(sxpath) + if !element.nil? + element.text + else + "" + end + end + + ############################################################################ + # Feature 4921. Adds TOTAL_CPU and TOTAL_MEM to HOST/HOST_SHARE to compute + # MAX_CPU and MAX_MEM when RESERVED_CPU/MEM is updated + ############################################################################ + def feature_4901 + @db.run "ALTER TABLE host_pool RENAME TO old_host_pool;" + @db.run host_pool_schema() + + @db.transaction do + @db.fetch("SELECT * FROM old_host_pool") do |row| + doc = Nokogiri::XML(row[:body], nil, NOKOGIRI_ENCODING) { |c| + c.default_xml.noblanks + } + + rcpu = xpath(doc, "TEMPLATE/RESERVED_CPU").to_i + rmem = xpath(doc, "TEMPLATE/RESERVED_MEM").to_i + + total_cpu = xpath(doc, "HOST_SHARE/MAX_CPU").to_i + rcpu + total_mem = xpath(doc, "HOST_SHARE/MAX_MEM").to_i + rmem + + total_cpu_e = doc.create_element "TOTAL_CPU", total_cpu + total_mem_e = doc.create_element "TOTAL_MEM", total_mem + + host_share = doc.root.at_xpath("HOST_SHARE") + host_share.add_child(total_cpu_e) + host_share.add_child(total_mem_e) + + @db[:host_pool].insert( + :oid => row[:oid], + :name => row[:name], + :body => doc.root.to_s, + :state => row[:state], + :last_mon_time => row[:last_mon_time], + :uid => row[:uid], + :gid => row[:gid], + :owner_u => row[:owner_u], + :group_u => row[:group_u], + :other_u => row[:other_u], + :cid => row[:cid]) + end + end + + @db.run "DROP TABLE old_host_pool;" + end + +end diff --git a/src/onedb/local/db_schema.rb b/src/onedb/local/db_schema.rb new file mode 100644 index 00000000000..e07ab62c27e --- /dev/null +++ b/src/onedb/local/db_schema.rb @@ -0,0 +1,48 @@ +# -------------------------------------------------------------------------- # +# Copyright 2002-2016, OpenNebula Project, OpenNebula Systems # +# # +# Licensed under the Apache License, Version 2.0 (the "License"); you may # +# not use this file except in compliance with the License. You may obtain # +# a copy of the License at # +# # +# http://www.apache.org/licenses/LICENSE-2.0 # +# # +# Unless required by applicable law or agreed to in writing, software # +# distributed under the License is distributed on an "AS IS" BASIS, # +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # +# See the License for the specific language governing permissions and # +# limitations under the License. # +#--------------------------------------------------------------------------- # + +require 'set' +require 'base64' +require 'zlib' +require 'pathname' + +require 'opennebula' + +include OpenNebula + +module Migrator + ############################################################################## + # DB schema for OpenNebula tables, each function may return the schema for + # each opennebula version + ############################################################################## + def host_pool_schema + case db_version() + when "4.5.80" + when "4.7.80" + when "4.9.80" + when "4.10.3" + when "4.11.80" + when "4.13.80" + when "4.13.85" + when "4.90.0" + when "5.2.0" + 'CREATE TABLE host_pool (oid INTEGER PRIMARY KEY, name VARCHAR(128), '\ + 'body MEDIUMTEXT, state INTEGER, last_mon_time INTEGER, uid INTEGER, '\ + 'gid INTEGER, owner_u INTEGER, group_u INTEGER, other_u INTEGER, '\ + 'cid INTEGER);' + end + end +end