Statistics
| Branch: | Tag: | Revision:

root / src / Ganeti / HTools / Loader.hs @ 4b542ebc

History | View | Annotate | Download (13 kB)

1 525bfb36 Iustin Pop
{-| Generic data loader.
2 040afc35 Iustin Pop
3 e8f89bb6 Iustin Pop
This module holds the common code for parsing the input data after it
4 e8f89bb6 Iustin Pop
has been loaded from external sources.
5 040afc35 Iustin Pop
6 040afc35 Iustin Pop
-}
7 040afc35 Iustin Pop
8 e2fa2baf Iustin Pop
{-
9 e2fa2baf Iustin Pop
10 d6eec019 Iustin Pop
Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
11 e2fa2baf Iustin Pop
12 e2fa2baf Iustin Pop
This program is free software; you can redistribute it and/or modify
13 e2fa2baf Iustin Pop
it under the terms of the GNU General Public License as published by
14 e2fa2baf Iustin Pop
the Free Software Foundation; either version 2 of the License, or
15 e2fa2baf Iustin Pop
(at your option) any later version.
16 e2fa2baf Iustin Pop
17 e2fa2baf Iustin Pop
This program is distributed in the hope that it will be useful, but
18 e2fa2baf Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
19 e2fa2baf Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20 e2fa2baf Iustin Pop
General Public License for more details.
21 e2fa2baf Iustin Pop
22 e2fa2baf Iustin Pop
You should have received a copy of the GNU General Public License
23 e2fa2baf Iustin Pop
along with this program; if not, write to the Free Software
24 e2fa2baf Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
25 e2fa2baf Iustin Pop
02110-1301, USA.
26 e2fa2baf Iustin Pop
27 e2fa2baf Iustin Pop
-}
28 e2fa2baf Iustin Pop
29 040afc35 Iustin Pop
module Ganeti.HTools.Loader
30 ebf38064 Iustin Pop
  ( mergeData
31 ebf38064 Iustin Pop
  , checkData
32 ebf38064 Iustin Pop
  , assignIndices
33 09ab9fb2 Klaus Aehlig
  , setMaster
34 ebf38064 Iustin Pop
  , lookupNode
35 ebf38064 Iustin Pop
  , lookupInstance
36 ebf38064 Iustin Pop
  , lookupGroup
37 ebf38064 Iustin Pop
  , commonSuffix
38 ebf38064 Iustin Pop
  , RqType(..)
39 ebf38064 Iustin Pop
  , Request(..)
40 ebf38064 Iustin Pop
  , ClusterData(..)
41 ebf38064 Iustin Pop
  , emptyCluster
42 ebf38064 Iustin Pop
  ) where
43 040afc35 Iustin Pop
44 55416810 Dato Simó
import Control.Monad
45 e4c5beaf Iustin Pop
import Data.List
46 2d0ca2c5 Iustin Pop
import qualified Data.Map as M
47 e79f576c Dato Simó
import Data.Maybe
48 446d8827 Iustin Pop
import Text.Printf (printf)
49 55416810 Dato Simó
import System.Time (ClockTime(..))
50 e4c5beaf Iustin Pop
51 e4c5beaf Iustin Pop
import qualified Ganeti.HTools.Container as Container
52 e4c5beaf Iustin Pop
import qualified Ganeti.HTools.Instance as Instance
53 e4c5beaf Iustin Pop
import qualified Ganeti.HTools.Node as Node
54 a679e9dc Iustin Pop
import qualified Ganeti.HTools.Group as Group
55 a7e1fd89 Iustin Pop
import qualified Ganeti.HTools.Cluster as Cluster
56 e4c5beaf Iustin Pop
57 2fc5653f Iustin Pop
import Ganeti.BasicTypes
58 55416810 Dato Simó
import qualified Ganeti.Constants as C
59 e4c5beaf Iustin Pop
import Ganeti.HTools.Types
60 26d62e4c Iustin Pop
import Ganeti.Utils
61 e4c5beaf Iustin Pop
62 f5e67f55 Iustin Pop
-- * Constants
63 f5e67f55 Iustin Pop
64 525bfb36 Iustin Pop
-- | The exclusion tag prefix.
65 f5e67f55 Iustin Pop
exTagsPrefix :: String
66 f5e67f55 Iustin Pop
exTagsPrefix = "htools:iextags:"
67 f5e67f55 Iustin Pop
68 19f38ee8 Iustin Pop
-- * Types
69 19f38ee8 Iustin Pop
70 54365762 Iustin Pop
{-| The iallocator request type.
71 19f38ee8 Iustin Pop
72 19f38ee8 Iustin Pop
This type denotes what request we got from Ganeti and also holds
73 19f38ee8 Iustin Pop
request-specific fields.
74 19f38ee8 Iustin Pop
75 19f38ee8 Iustin Pop
-}
76 19f38ee8 Iustin Pop
data RqType
77 2a9aff11 René Nussbaumer
  = Allocate Instance.Instance Int           -- ^ A new instance allocation
78 2a9aff11 René Nussbaumer
  | Relocate Idx Int [Ndx]                   -- ^ Choose a new secondary node
79 2a9aff11 René Nussbaumer
  | NodeEvacuate [Idx] EvacMode              -- ^ node-evacuate mode
80 2a9aff11 René Nussbaumer
  | ChangeGroup [Gdx] [Idx]                  -- ^ Multi-relocate mode
81 2a9aff11 René Nussbaumer
  | MultiAllocate [(Instance.Instance, Int)] -- ^ Multi-allocate mode
82 139c0683 Iustin Pop
    deriving (Show)
83 19f38ee8 Iustin Pop
84 19f38ee8 Iustin Pop
-- | A complete request, as received from Ganeti.
85 34c00528 Iustin Pop
data Request = Request RqType ClusterData
86 139c0683 Iustin Pop
               deriving (Show)
87 19f38ee8 Iustin Pop
88 7b6e99b3 Iustin Pop
-- | The cluster state.
89 7b6e99b3 Iustin Pop
data ClusterData = ClusterData
90 ebf38064 Iustin Pop
  { cdGroups    :: Group.List    -- ^ The node group list
91 ebf38064 Iustin Pop
  , cdNodes     :: Node.List     -- ^ The node list
92 ebf38064 Iustin Pop
  , cdInstances :: Instance.List -- ^ The instance list
93 ebf38064 Iustin Pop
  , cdTags      :: [String]      -- ^ The cluster tags
94 71375ef7 Iustin Pop
  , cdIPolicy   :: IPolicy       -- ^ The cluster instance policy
95 139c0683 Iustin Pop
  } deriving (Show, Eq)
96 7b6e99b3 Iustin Pop
97 7b6e99b3 Iustin Pop
-- | An empty cluster.
98 7b6e99b3 Iustin Pop
emptyCluster :: ClusterData
99 7b6e99b3 Iustin Pop
emptyCluster = ClusterData Container.empty Container.empty Container.empty []
100 71375ef7 Iustin Pop
                 defIPolicy
101 7b6e99b3 Iustin Pop
102 19f38ee8 Iustin Pop
-- * Functions
103 19f38ee8 Iustin Pop
104 9188aeef Iustin Pop
-- | Lookups a node into an assoc list.
105 6ff78049 Iustin Pop
lookupNode :: (Monad m) => NameAssoc -> String -> String -> m Ndx
106 040afc35 Iustin Pop
lookupNode ktn inst node =
107 76ae2e5b Iustin Pop
  maybe (fail $ "Unknown node '" ++ node ++ "' for instance " ++ inst) return $
108 76ae2e5b Iustin Pop
    M.lookup node ktn
109 040afc35 Iustin Pop
110 9188aeef Iustin Pop
-- | Lookups an instance into an assoc list.
111 6ff78049 Iustin Pop
lookupInstance :: (Monad m) => NameAssoc -> String -> m Idx
112 5a1edeb6 Iustin Pop
lookupInstance kti inst =
113 76ae2e5b Iustin Pop
  maybe (fail $ "Unknown instance '" ++ inst ++ "'") return $ M.lookup inst kti
114 5a1edeb6 Iustin Pop
115 f4531f51 Iustin Pop
-- | Lookups a group into an assoc list.
116 f4531f51 Iustin Pop
lookupGroup :: (Monad m) => NameAssoc -> String -> String -> m Gdx
117 f4531f51 Iustin Pop
lookupGroup ktg nname gname =
118 76ae2e5b Iustin Pop
  maybe (fail $ "Unknown group '" ++ gname ++ "' for node " ++ nname) return $
119 76ae2e5b Iustin Pop
    M.lookup gname ktg
120 f4531f51 Iustin Pop
121 9188aeef Iustin Pop
-- | Given a list of elements (and their names), assign indices to them.
122 497e30a1 Iustin Pop
assignIndices :: (Element a) =>
123 497e30a1 Iustin Pop
                 [(String, a)]
124 99b63608 Iustin Pop
              -> (NameAssoc, Container.Container a)
125 07ac4aaf Guido Trotter
assignIndices name_element =
126 07ac4aaf Guido Trotter
  let (name_idx, idx_element) =
127 2d0ca2c5 Iustin Pop
          unzip . map (\ (idx, (k, v)) -> ((k, idx), (idx, setIdx v idx)))
128 07ac4aaf Guido Trotter
          . zip [0..] $ name_element
129 07ac4aaf Guido Trotter
  in (M.fromList name_idx, Container.fromList idx_element)
130 78694255 Iustin Pop
131 09ab9fb2 Klaus Aehlig
-- | Given am indexed node list, and the name of the master, mark it as such. 
132 09ab9fb2 Klaus Aehlig
setMaster :: (Monad m) => NameAssoc -> Node.List -> String -> m Node.List
133 09ab9fb2 Klaus Aehlig
setMaster node_names node_idx master = do
134 09ab9fb2 Klaus Aehlig
  kmaster <- maybe (fail $ "Master node " ++ master ++ " unknown") return $
135 09ab9fb2 Klaus Aehlig
             M.lookup master node_names
136 09ab9fb2 Klaus Aehlig
  let mnode = Container.find kmaster node_idx
137 09ab9fb2 Klaus Aehlig
  return $ Container.add kmaster (Node.setMaster mnode True) node_idx
138 09ab9fb2 Klaus Aehlig
139 9188aeef Iustin Pop
-- | For each instance, add its index to its primary and secondary nodes.
140 99b63608 Iustin Pop
fixNodes :: Node.List
141 aa8d2e71 Iustin Pop
         -> Instance.Instance
142 99b63608 Iustin Pop
         -> Node.List
143 aa8d2e71 Iustin Pop
fixNodes accu inst =
144 ebf38064 Iustin Pop
  let pdx = Instance.pNode inst
145 ebf38064 Iustin Pop
      sdx = Instance.sNode inst
146 ebf38064 Iustin Pop
      pold = Container.find pdx accu
147 ebf38064 Iustin Pop
      pnew = Node.setPri pold inst
148 ebf38064 Iustin Pop
      ac2 = Container.add pdx pnew accu
149 ebf38064 Iustin Pop
  in if sdx /= Node.noSecondary
150 ebf38064 Iustin Pop
       then let sold = Container.find sdx accu
151 ebf38064 Iustin Pop
                snew = Node.setSec sold inst
152 ebf38064 Iustin Pop
            in Container.add sdx snew ac2
153 ebf38064 Iustin Pop
       else ac2
154 e4c5beaf Iustin Pop
155 d6eec019 Iustin Pop
-- | Set the node's policy to its group one. Note that this requires
156 d6eec019 Iustin Pop
-- the group to exist (should have been checked before), otherwise it
157 d6eec019 Iustin Pop
-- will abort with a runtime error.
158 d6eec019 Iustin Pop
setNodePolicy :: Group.List -> Node.Node -> Node.Node
159 d6eec019 Iustin Pop
setNodePolicy gl node =
160 d6eec019 Iustin Pop
  let grp = Container.find (Node.group node) gl
161 d6eec019 Iustin Pop
      gpol = Group.iPolicy grp
162 d6eec019 Iustin Pop
  in Node.setPolicy gpol node
163 d6eec019 Iustin Pop
164 2f907bad Dato Simó
-- | Update instance with exclusion tags list.
165 2f907bad Dato Simó
updateExclTags :: [String] -> Instance.Instance -> Instance.Instance
166 2f907bad Dato Simó
updateExclTags tl inst =
167 2f907bad Dato Simó
  let allTags = Instance.allTags inst
168 2f907bad Dato Simó
      exclTags = filter (\tag -> any (`isPrefixOf` tag) tl) allTags
169 2f907bad Dato Simó
  in inst { Instance.exclTags = exclTags }
170 0f15cc76 Iustin Pop
171 525bfb36 Iustin Pop
-- | Update the movable attribute.
172 c6ccc073 Guido Trotter
updateMovable :: [String]           -- ^ Selected instances (if not empty)
173 c6ccc073 Guido Trotter
              -> [String]           -- ^ Excluded instances
174 c6ccc073 Guido Trotter
              -> Instance.Instance  -- ^ Target Instance
175 c6ccc073 Guido Trotter
              -> Instance.Instance  -- ^ Target Instance with updated attribute
176 c6ccc073 Guido Trotter
updateMovable selinsts exinsts inst =
177 a7667ba6 Iustin Pop
  if Instance.name inst `elem` exinsts ||
178 ebf38064 Iustin Pop
     not (null selinsts || Instance.name inst `elem` selinsts)
179 39f979b8 Iustin Pop
    then Instance.setMovable inst False
180 39f979b8 Iustin Pop
    else inst
181 39f979b8 Iustin Pop
182 a7e1fd89 Iustin Pop
-- | Disables moves for instances with a split group.
183 a7e1fd89 Iustin Pop
disableSplitMoves :: Node.List -> Instance.Instance -> Instance.Instance
184 a7e1fd89 Iustin Pop
disableSplitMoves nl inst =
185 a7e1fd89 Iustin Pop
  if not . isOk . Cluster.instanceGroup nl $ inst
186 a7e1fd89 Iustin Pop
    then Instance.setMovable inst False
187 a7e1fd89 Iustin Pop
    else inst
188 a7e1fd89 Iustin Pop
189 55416810 Dato Simó
-- | Set the auto-repair policy for an instance.
190 55416810 Dato Simó
setArPolicy :: [String]       -- ^ Cluster tags
191 55416810 Dato Simó
            -> Group.List     -- ^ List of node groups
192 55416810 Dato Simó
            -> Node.List      -- ^ List of nodes
193 55416810 Dato Simó
            -> Instance.List  -- ^ List of instances
194 ef947a42 Dato Simó
            -> ClockTime      -- ^ Current timestamp, to evaluate ArSuspended
195 55416810 Dato Simó
            -> Instance.List  -- ^ Updated list of instances
196 ef947a42 Dato Simó
setArPolicy ctags gl nl il time =
197 ef947a42 Dato Simó
  let getArPolicy' = flip getArPolicy time
198 ef947a42 Dato Simó
      cpol = fromMaybe ArNotEnabled $ getArPolicy' ctags
199 ef947a42 Dato Simó
      gpols = Container.map (fromMaybe cpol . getArPolicy' . Group.allTags) gl
200 ef947a42 Dato Simó
      ipolfn = getArPolicy' . Instance.allTags
201 55416810 Dato Simó
      nlookup = flip Container.find nl . Instance.pNode
202 55416810 Dato Simó
      glookup = flip Container.find gpols . Node.group . nlookup
203 55416810 Dato Simó
      updateInstance inst = inst {
204 55416810 Dato Simó
        Instance.arPolicy = fromMaybe (glookup inst) $ ipolfn inst }
205 55416810 Dato Simó
  in
206 55416810 Dato Simó
   Container.map updateInstance il
207 55416810 Dato Simó
208 55416810 Dato Simó
-- | Get the auto-repair policy from a list of tags.
209 55416810 Dato Simó
--
210 55416810 Dato Simó
-- This examines the ganeti:watcher:autorepair and
211 55416810 Dato Simó
-- ganeti:watcher:autorepair:suspend tags to determine the policy. If none of
212 55416810 Dato Simó
-- these tags are present, Nothing (and not ArNotEnabled) is returned.
213 ef947a42 Dato Simó
getArPolicy :: [String] -> ClockTime -> Maybe AutoRepairPolicy
214 ef947a42 Dato Simó
getArPolicy tags time =
215 55416810 Dato Simó
  let enabled = mapMaybe (autoRepairTypeFromRaw <=<
216 55416810 Dato Simó
                          chompPrefix C.autoRepairTagEnabled) tags
217 55416810 Dato Simó
      suspended = mapMaybe (chompPrefix C.autoRepairTagSuspended) tags
218 ef947a42 Dato Simó
      futureTs = filter (> time) . map (flip TOD 0) $
219 ef947a42 Dato Simó
                   mapMaybe (tryRead "auto-repair suspend time") suspended
220 55416810 Dato Simó
  in
221 55416810 Dato Simó
   case () of
222 55416810 Dato Simó
     -- Note how we must return ArSuspended even if "enabled" is empty, so that
223 55416810 Dato Simó
     -- node groups or instances can suspend repairs that were enabled at an
224 55416810 Dato Simó
     -- upper scope (cluster or node group).
225 ef947a42 Dato Simó
     _ | "" `elem` suspended -> Just $ ArSuspended Forever
226 ef947a42 Dato Simó
       | not $ null futureTs -> Just . ArSuspended . Until . maximum $ futureTs
227 ef947a42 Dato Simó
       | not $ null enabled  -> Just $ ArEnabled (minimum enabled)
228 ef947a42 Dato Simó
       | otherwise           -> Nothing
229 55416810 Dato Simó
230 f9fc7a63 Iustin Pop
-- | Compute the longest common suffix of a list of strings that
231 525bfb36 Iustin Pop
-- starts with a dot.
232 8472a321 Iustin Pop
longestDomain :: [String] -> String
233 e4c5beaf Iustin Pop
longestDomain [] = ""
234 8472a321 Iustin Pop
longestDomain (x:xs) =
235 ebf38064 Iustin Pop
  foldr (\ suffix accu -> if all (isSuffixOf suffix) xs
236 ebf38064 Iustin Pop
                            then suffix
237 ebf38064 Iustin Pop
                            else accu)
238 ebf38064 Iustin Pop
          "" $ filter (isPrefixOf ".") (tails x)
239 e4c5beaf Iustin Pop
240 525bfb36 Iustin Pop
-- | Extracts the exclusion tags from the cluster configuration.
241 f5e67f55 Iustin Pop
extractExTags :: [String] -> [String]
242 e79f576c Dato Simó
extractExTags = filter (not . null) . mapMaybe (chompPrefix exTagsPrefix)
243 f5e67f55 Iustin Pop
244 525bfb36 Iustin Pop
-- | Extracts the common suffix from node\/instance names.
245 3e4480e0 Iustin Pop
commonSuffix :: Node.List -> Instance.List -> String
246 3e4480e0 Iustin Pop
commonSuffix nl il =
247 ebf38064 Iustin Pop
  let node_names = map Node.name $ Container.elems nl
248 ebf38064 Iustin Pop
      inst_names = map Instance.name $ Container.elems il
249 ebf38064 Iustin Pop
  in longestDomain (node_names ++ inst_names)
250 3e4480e0 Iustin Pop
251 9188aeef Iustin Pop
-- | Initializer function that loads the data from a node and instance
252 9188aeef Iustin Pop
-- list and massages it into the correct format.
253 aa8d2e71 Iustin Pop
mergeData :: [(String, DynUtil)]  -- ^ Instance utilisation data
254 0f15cc76 Iustin Pop
          -> [String]             -- ^ Exclusion tags
255 2d1708e0 Guido Trotter
          -> [String]             -- ^ Selected instances (if not empty)
256 2d1708e0 Guido Trotter
          -> [String]             -- ^ Excluded instances
257 ef947a42 Dato Simó
          -> ClockTime            -- ^ The current timestamp
258 f4f6eb0b Iustin Pop
          -> ClusterData          -- ^ Data from backends
259 c0e31451 Iustin Pop
          -> Result ClusterData   -- ^ Fixed cluster data
260 ef947a42 Dato Simó
mergeData um extags selinsts exinsts time cdata@(ClusterData gl nl il ctags _) =
261 ef947a42 Dato Simó
  let il2 = setArPolicy ctags gl nl il time
262 a5f8dcdc Iustin Pop
      il3 = foldl' (\im (name, n_util) ->
263 a5f8dcdc Iustin Pop
                        case Container.findByName im name of
264 a5f8dcdc Iustin Pop
                          Nothing -> im -- skipping unknown instance
265 a5f8dcdc Iustin Pop
                          Just inst ->
266 a5f8dcdc Iustin Pop
                              let new_i = inst { Instance.util = n_util }
267 a5f8dcdc Iustin Pop
                              in Container.add (Instance.idx inst) new_i im
268 a5f8dcdc Iustin Pop
                   ) il2 um
269 55416810 Dato Simó
      allextags = extags ++ extractExTags ctags
270 55416810 Dato Simó
      inst_names = map Instance.name $ Container.elems il3
271 424ec11d Guido Trotter
      selinst_lkp = map (lookupName inst_names) selinsts
272 424ec11d Guido Trotter
      exinst_lkp = map (lookupName inst_names) exinsts
273 424ec11d Guido Trotter
      lkp_unknown = filter (not . goodLookupResult) (selinst_lkp ++ exinst_lkp)
274 424ec11d Guido Trotter
      selinst_names = map lrContent selinst_lkp
275 424ec11d Guido Trotter
      exinst_names = map lrContent exinst_lkp
276 99b63608 Iustin Pop
      node_names = map Node.name (Container.elems nl)
277 8472a321 Iustin Pop
      common_suffix = longestDomain (node_names ++ inst_names)
278 cdbab531 Iustin Pop
      il4 = Container.map (computeAlias common_suffix .
279 2f907bad Dato Simó
                           updateExclTags allextags .
280 cdbab531 Iustin Pop
                           updateMovable selinst_names exinst_names) il3
281 cdbab531 Iustin Pop
      nl2 = foldl' fixNodes nl (Container.elems il4)
282 d6eec019 Iustin Pop
      nl3 = Container.map (setNodePolicy gl .
283 d6eec019 Iustin Pop
                           computeAlias common_suffix .
284 cdbab531 Iustin Pop
                           (`Node.buildPeers` il4)) nl2
285 a7e1fd89 Iustin Pop
      il5 = Container.map (disableSplitMoves nl3) il4
286 424ec11d Guido Trotter
  in if' (null lkp_unknown)
287 a7e1fd89 Iustin Pop
         (Ok cdata { cdNodes = nl3, cdInstances = il5 })
288 424ec11d Guido Trotter
         (Bad $ "Unknown instance(s): " ++ show(map lrContent lkp_unknown))
289 446d8827 Iustin Pop
290 9188aeef Iustin Pop
-- | Checks the cluster data for consistency.
291 262a08a2 Iustin Pop
checkData :: Node.List -> Instance.List
292 262a08a2 Iustin Pop
          -> ([String], Node.List)
293 dbd6700b Iustin Pop
checkData nl il =
294 446d8827 Iustin Pop
    Container.mapAccum
295 446d8827 Iustin Pop
        (\ msgs node ->
296 dbd6700b Iustin Pop
             let nname = Node.name node
297 5182e970 Iustin Pop
                 nilst = map (`Container.find` il) (Node.pList node)
298 61bbbed7 Agata Murawska
                 dilst = filter Instance.instanceDown nilst
299 446d8827 Iustin Pop
                 adj_mem = sum . map Instance.mem $ dilst
300 2060348b Iustin Pop
                 delta_mem = truncate (Node.tMem node)
301 2060348b Iustin Pop
                             - Node.nMem node
302 2060348b Iustin Pop
                             - Node.fMem node
303 9f6dcdea Iustin Pop
                             - nodeImem node il
304 446d8827 Iustin Pop
                             + adj_mem
305 2060348b Iustin Pop
                 delta_dsk = truncate (Node.tDsk node)
306 2060348b Iustin Pop
                             - Node.fDsk node
307 9f6dcdea Iustin Pop
                             - nodeIdsk node il
308 446d8827 Iustin Pop
                 newn = Node.setFmem (Node.setXmem node delta_mem)
309 2060348b Iustin Pop
                        (Node.fMem node - adj_mem)
310 bdd8c739 Iustin Pop
                 umsg1 =
311 bdd8c739 Iustin Pop
                   if delta_mem > 512 || delta_dsk > 1024
312 3603605a Iustin Pop
                      then printf "node %s is missing %d MB ram \
313 3603605a Iustin Pop
                                  \and %d GB disk"
314 3603605a Iustin Pop
                                  nname delta_mem (delta_dsk `div` 1024):msgs
315 bdd8c739 Iustin Pop
                      else msgs
316 bdd8c739 Iustin Pop
             in (umsg1, newn)
317 446d8827 Iustin Pop
        ) [] nl
318 446d8827 Iustin Pop
319 446d8827 Iustin Pop
-- | Compute the amount of memory used by primary instances on a node.
320 262a08a2 Iustin Pop
nodeImem :: Node.Node -> Instance.List -> Int
321 446d8827 Iustin Pop
nodeImem node il =
322 ebf38064 Iustin Pop
  let rfind = flip Container.find il
323 ebf38064 Iustin Pop
      il' = map rfind $ Node.pList node
324 7959cbb9 Iustin Pop
      oil' = filter Instance.notOffline il'
325 ebf38064 Iustin Pop
  in sum . map Instance.mem $ oil'
326 61bbbed7 Agata Murawska
327 446d8827 Iustin Pop
328 446d8827 Iustin Pop
-- | Compute the amount of disk used by instances on a node (either primary
329 446d8827 Iustin Pop
-- or secondary).
330 262a08a2 Iustin Pop
nodeIdsk :: Node.Node -> Instance.List -> Int
331 446d8827 Iustin Pop
nodeIdsk node il =
332 ebf38064 Iustin Pop
  let rfind = flip Container.find il
333 ebf38064 Iustin Pop
  in sum . map (Instance.dsk . rfind)
334 ebf38064 Iustin Pop
       $ Node.pList node ++ Node.sList node