From 4a60357831d2ef6686f8e0bfebde130f4251c52a Mon Sep 17 00:00:00 2001
From: Martin Sivak <msivak@redhat.com>
Date: Wed, 13 Nov 2013 14:10:31 +0100
Subject: [PATCH 08/11] Allow setting just a single value in MoM policy

This patch prevents a policy coruption that could have happened
when the 01-parameters policy was updated with just a single
value. If there were more values defined, the variables were lost
and the policy ended up broken.

This patch defines the variables in a file that is not touched
by the setMOMPolicyParameters and uses (set name value) syntax
in the generated policy part.

It also remembers the old setting and puts all the values to
the policy part when it is autogenerated.

Bug-Url: https://bugzilla.redhat.com/show_bug.cgi?id=1025780

Change-Id: I7bb1001a05e670e393b2cc0ab4b271b3b333d2e8
Signed-off-by: Martin Sivak <msivak@redhat.com>
Reviewed-on: http://gerrit.ovirt.org/21225
Reviewed-by: Amador Pahim <apahim@redhat.com>
Reviewed-by: Doron Fediuck <dfediuck@redhat.com>
Reviewed-by: Dan Kenigsberg <danken@redhat.com>
Reviewed-on: http://gerrit.ovirt.org/21839
Reviewed-by: Yaniv Bronhaim <ybronhei@redhat.com>
---
 debian/vdsm.install                                | 2 +-
 vdsm/Makefile.am                                   | 2 +-
 vdsm/mom.d/{00-python.policy => 00-defines.policy} | 5 +++++
 vdsm/mom.d/01-parameters.policy                    | 2 --
 vdsm/momIF.py                                      | 9 ++++++---
 5 files changed, 13 insertions(+), 7 deletions(-)
 rename vdsm/mom.d/{00-python.policy => 00-defines.policy} (58%)

diff --git a/debian/vdsm.install b/debian/vdsm.install
index 6de089e..89b6e6c 100644
--- a/debian/vdsm.install
+++ b/debian/vdsm.install
@@ -14,7 +14,7 @@
 ./etc/sysctl.d/vdsm.conf
 ./etc/vdsm/logger.conf
 ./etc/vdsm/mom.conf
-./etc/vdsm/mom.d/00-python.policy
+./etc/vdsm/mom.d/00-defines.policy
 ./etc/vdsm/mom.d/01-parameters.policy
 ./etc/vdsm/mom.d/02-balloon.policy
 ./etc/vdsm/mom.d/03-ksm.policy
diff --git a/vdsm/Makefile.am b/vdsm/Makefile.am
index 26be4dc..5ab291e 100644
--- a/vdsm/Makefile.am
+++ b/vdsm/Makefile.am
@@ -110,7 +110,7 @@ EXTRA_DIST = \
 	logger.conf.in \
 	mk_sysprep_floppy.in \
 	mom.conf.in \
-	mom.d/00-python.policy \
+	mom.d/00-defines.policy \
 	mom.d/01-parameters.policy \
 	mom.d/02-balloon.policy \
 	mom.d/03-ksm.policy \
diff --git a/vdsm/mom.d/00-python.policy b/vdsm/mom.d/00-defines.policy
similarity index 58%
rename from vdsm/mom.d/00-python.policy
rename to vdsm/mom.d/00-defines.policy
index face6fa..93b70ee 100644
--- a/vdsm/mom.d/00-python.policy
+++ b/vdsm/mom.d/00-defines.policy
@@ -2,3 +2,8 @@
 # received by setMOMPolicyParameters
 (defvar False 0)
 (defvar True 1)
+
+# Define variables for configurable options here
+(defvar ksmEnabled 1)
+(defvar balloonEnabled 0)
+
diff --git a/vdsm/mom.d/01-parameters.policy b/vdsm/mom.d/01-parameters.policy
index 97803d5..e69de29 100644
--- a/vdsm/mom.d/01-parameters.policy
+++ b/vdsm/mom.d/01-parameters.policy
@@ -1,2 +0,0 @@
-(defvar balloonEnabled 0)
-(defvar ksmEnabled 1)
diff --git a/vdsm/momIF.py b/vdsm/momIF.py
index 8b62657..c1e00ef 100644
--- a/vdsm/momIF.py
+++ b/vdsm/momIF.py
@@ -46,6 +46,7 @@ class MomThread(threading.Thread):
         self.log = logging.getLogger("MOM")
         self.log.info("Starting up MOM")
         self._mom = mom.MOM(momconf)
+        self._policy = {}
         threading.Thread.__init__(self, target=self._mom.run, name="MOM")
         self.start()
 
@@ -64,14 +65,16 @@ class MomThread(threading.Thread):
 
     def setPolicyParameters(self, key_value_store):
         # mom.setNamedPolicy will raise an exception on failure.
+
         # Prepare in-memory policy file with tuning variables
         # this might need to convert certain python types to proper MoM
         # policy language
+        self._policy.update(key_value_store)
 
-        # Python bool values are defined in 00-python.policy so need no
+        # Python bool values are defined in 00-defines.policy so need no
         # conversion here
-        policy_string = "\n".join(["(defvar %s %r)" % (k, v)
-                                   for k, v in key_value_store.iteritems()])
+        policy_string = "\n".join(["(set %s %r)" % (k, v)
+                                   for k, v in self._policy.iteritems()])
 
         self._mom.setNamedPolicy(config.get("mom", "tuning_policy"),
                                  policy_string)
-- 
1.8.4.2

