On Wed, Feb 25, 2015 at 01:58:53PM +0100, 'Klaus Aehlig' via ganeti-devel wrote:
If we don't find an instance in the output of xentop any
more that usually means it moved away. In this case, it might
be useful to still have the old CPU load as a best guess until
a new value is available on the new location. But after this
period, we should stop reporting usage information for that
instance.

Signed-off-by: Klaus Aehlig <[email protected]>
---
src/Ganeti/DataCollectors/XenCpuLoad.hs | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/src/Ganeti/DataCollectors/XenCpuLoad.hs 
b/src/Ganeti/DataCollectors/XenCpuLoad.hs
index 897512d..2a66513 100644
--- a/src/Ganeti/DataCollectors/XenCpuLoad.hs
+++ b/src/Ganeti/DataCollectors/XenCpuLoad.hs
@@ -42,7 +42,7 @@ module Ganeti.DataCollectors.XenCpuLoad
  , dcUpdate
  ) where

-import Control.Applicative ((<$>))
+import Control.Applicative ((<$>), liftA2)
import Control.Arrow ((***))
import Control.Monad (liftM, when)
import Control.Monad.IO.Class (liftIO)
@@ -141,7 +141,13 @@ dcUpdate maybeCollector = do
                         . (clockTimeToUSec now -)
                         . clockTimeToUSec . fst))
                      combinedValues
-  return $ InstanceCpuLoad withinRange
+      withoutOld = Map.filter
+                     (liftA2 (&&) (not . Seq.null)
+                      $ (>) (fromIntegral $ C.xentopAverageThreshold * 1000000)
+                        . (clockTimeToUSec now -) . clockTimeToUSec
+                        . fst . flip Seq.index 0)
+                     withinRange
+  return $ InstanceCpuLoad withoutOld

While correct, this part somewhat worries me from the reability point of view, as this relies on `False && undefined == False`. I was surprised to see that there is no such thing as `headMaybe :: Seq a -> Maybe a` (or its generalization to Foldable), nor a safe version of `index` in the standard libraries. Also there is no folding function on `ViewL` which would allow to write point-free expressions targetting the head of a sequence. That looks like an omission in the standard libraries.

I'd suggest to write our own version of headMaybe, either just locally here in a `let` block for Seq, or somewhere globally generalized to Foldable and then just use standard `maybe` here.


-- | From a list of timestamps and cumulative CPU data, compute the
-- average CPU activity in vCPUs.
--
2.2.0.rc0.207.ga3a616c

Reply via email to