[Ovirt-devel] [PATCH server] Persistantly store vm / host history

Mohammed Morsi mmorsi at redhat.com
Tue Mar 10 20:14:08 UTC 2009


This patch adds a table and model classes to persistently
store the history of which vms were running on which hosts.
Changes made to dbomatic to record history on changes to
a vm's state. Fields that are recorded include vm / host ids,
time started and ended, vm state, and vnc port of the
vm when running. Additional fields will most likely
be desired, and can easily be added here or in subsequent
patches.
---
 src/app/models/host.rb                |    9 ++++++
 src/app/models/vm.rb                  |    9 ++++++
 src/app/models/vm_host_history.rb     |   22 ++++++++++++++
 src/db-omatic/db_omatic.rb            |   27 +++++++++++++++++
 src/db/migrate/036_vm_host_history.rb |   52 +++++++++++++++++++++++++++++++++
 5 files changed, 119 insertions(+), 0 deletions(-)
 create mode 100644 src/app/models/vm_host_history.rb
 create mode 100644 src/db/migrate/036_vm_host_history.rb

diff --git a/src/app/models/host.rb b/src/app/models/host.rb
index 93c474b..06d7388 100644
--- a/src/app/models/host.rb
+++ b/src/app/models/host.rb
@@ -54,6 +54,15 @@ class Host < ActiveRecord::Base
   has_many :smart_pool_tags, :as => :tagged, :dependent => :destroy
   has_many :smart_pools, :through => :smart_pool_tags
 
+  # reverse cronological collection of vm history
+  # each collection item contains vm that was running on host
+  # time started, and time ended (see VmHostHistory)
+  has_many :vm_host_histories,
+           :order => 'time_started DESC',
+           :dependent => :destroy
+
+  alias history vm_host_histories
+
   acts_as_xapian :texts => [ :hostname, :uuid, :hypervisor_type, :arch ],
                  :values => [ [ :created_at, 0, "created_at", :date ],
                               [ :updated_at, 1, "updated_at", :date ] ],
diff --git a/src/app/models/vm.rb b/src/app/models/vm.rb
index e56d6dd..c62595a 100644
--- a/src/app/models/vm.rb
+++ b/src/app/models/vm.rb
@@ -34,6 +34,15 @@ class Vm < ActiveRecord::Base
   has_many :smart_pool_tags, :as => :tagged, :dependent => :destroy
   has_many :smart_pools, :through => :smart_pool_tags
 
+  # reverse cronological collection of vm history
+  # each collection item contains host vm was running on,
+  # time started, and time ended (see VmHostHistory)
+  has_many :vm_host_histories,
+           :order => 'time_started DESC',
+           :dependent => :destroy
+
+  alias history vm_host_histories
+
   validates_presence_of :uuid, :description, :num_vcpus_allocated,
                         :boot_device, :memory_allocated_in_mb,
                         :memory_allocated, :vnic_mac_addr,
diff --git a/src/app/models/vm_host_history.rb b/src/app/models/vm_host_history.rb
new file mode 100644
index 0000000..bd61ddc
--- /dev/null
+++ b/src/app/models/vm_host_history.rb
@@ -0,0 +1,22 @@
+# Copyright (C) 2008 Red Hat, Inc.
+# Written by Mohammed Morsi<mmorsi at redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.  A copy of the GNU General Public License is
+# also available at http://www.gnu.org/copyleft/gpl.html.
+
+class VmHostHistory < ActiveRecord::Base
+   belongs_to :vm
+   belongs_to :host
+end
diff --git a/src/db-omatic/db_omatic.rb b/src/db-omatic/db_omatic.rb
index 4c8d60e..c31577c 100755
--- a/src/db-omatic/db_omatic.rb
+++ b/src/db-omatic/db_omatic.rb
@@ -141,6 +141,33 @@ class DbOmatic < Qpid::Qmf::Console
             end
         end
 
+        begin
+          # find open vm host history for this vm,
+          history = VmHostHistory.find(:first, :conditions => ["vm_id = ? AND time_ended is NULL", vm.id])
+
+          if state == Vm::STATE_RUNNING
+             if history.nil?
+               history = VmHostHistory.new
+               history.vm = vm
+               history.host = vm.host
+               history.vnc_port = vm.vnc_port
+               history.state = state
+               history.time_started = Time.now
+               history.save!
+             end
+
+          elsif state != Vm::STATE_PENDING
+             unless history.nil? # throw an exception if this fails?
+               history.time_ended = Time.now
+               history.state = state
+               history.save!
+             end
+          end
+
+        rescue Exception => e # just log any errors here
+            @logger.error "Error with VM #{domain['name']} operation: " + e
+        end
+
         @logger.info "Updating VM #{domain['name']} to state #{state}"
         vm.state = state
         vm.save
diff --git a/src/db/migrate/036_vm_host_history.rb b/src/db/migrate/036_vm_host_history.rb
new file mode 100644
index 0000000..10ba39b
--- /dev/null
+++ b/src/db/migrate/036_vm_host_history.rb
@@ -0,0 +1,52 @@
+# Copyright (C) 2008 Red Hat, Inc.
+# Written by Mohammed Morsi <mmorsi at redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA  02110-1301, USA.  A copy of the GNU General Public License is
+# also available at http://www.gnu.org/copyleft/gpl.html.
+
+# creates a vm/host history table to maintain a record
+# of which vms were running on which hosts
+class VmHostHistory < ActiveRecord::Migration
+  def self.up
+    # this table gets populated in db-omatic
+    create_table :vm_host_histories do |t|
+       # vm / host association
+       t.integer :vm_id
+       t.integer :host_id
+
+       # records operating info of vm
+       #  (most likey we will want to add a 
+       #   slew of more info here or in a future
+       #   migration)
+       t.integer :vnc_port
+       t.string  :state
+
+       # start / end timestamps
+       t.timestamp :time_started
+       t.timestamp :time_ended
+    end
+
+    execute "alter table vm_host_histories add constraint 
+             fk_vm_host_histories_vms foreign key (vm_id) references vms(id)"
+             
+    execute "alter table vm_host_histories add constraint 
+             fk_vm_host_histories_hosts foreign key (host_id) references 
+             hosts(id)"
+  end
+
+  def self.down
+    drop_table :vm_host_histories
+  end
+end
-- 
1.6.0.6




More information about the ovirt-devel mailing list