Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Loader.hs @ f3f76ccc

History | View | Annotate | Download (12.4 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 2a8e2dc9 Iustin Pop
Copyright (C) 2009, 2010, 2011 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 ebf38064 Iustin Pop
  , lookupName
34 ebf38064 Iustin Pop
  , goodLookupResult
35 ebf38064 Iustin Pop
  , lookupNode
36 ebf38064 Iustin Pop
  , lookupInstance
37 ebf38064 Iustin Pop
  , lookupGroup
38 ebf38064 Iustin Pop
  , commonSuffix
39 ebf38064 Iustin Pop
  , RqType(..)
40 ebf38064 Iustin Pop
  , Request(..)
41 ebf38064 Iustin Pop
  , ClusterData(..)
42 ebf38064 Iustin Pop
  , emptyCluster
43 ebf38064 Iustin Pop
  , compareNameComponent
44 ebf38064 Iustin Pop
  , prefixMatch
45 ebf38064 Iustin Pop
  , LookupResult(..)
46 ebf38064 Iustin Pop
  , MatchPriority(..)
47 ebf38064 Iustin Pop
  ) where
48 040afc35 Iustin Pop
49 e4c5beaf Iustin Pop
import Data.List
50 efe98965 Guido Trotter
import Data.Function
51 2d0ca2c5 Iustin Pop
import qualified Data.Map as M
52 446d8827 Iustin Pop
import Text.Printf (printf)
53 e4c5beaf Iustin Pop
54 e4c5beaf Iustin Pop
import qualified Ganeti.HTools.Container as Container
55 e4c5beaf Iustin Pop
import qualified Ganeti.HTools.Instance as Instance
56 e4c5beaf Iustin Pop
import qualified Ganeti.HTools.Node as Node
57 a679e9dc Iustin Pop
import qualified Ganeti.HTools.Group as Group
58 e4c5beaf Iustin Pop
59 e4c5beaf Iustin Pop
import Ganeti.HTools.Types
60 efe98965 Guido Trotter
import Ganeti.HTools.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 ebf38064 Iustin Pop
  = Allocate Instance.Instance Int -- ^ A new instance allocation
78 ebf38064 Iustin Pop
  | Relocate Idx Int [Ndx]         -- ^ Choose a new secondary node
79 ebf38064 Iustin Pop
  | NodeEvacuate [Idx] EvacMode    -- ^ node-evacuate mode
80 ebf38064 Iustin Pop
  | ChangeGroup [Gdx] [Idx]        -- ^ Multi-relocate mode
81 6bc39970 Iustin Pop
    deriving (Show, Read)
82 19f38ee8 Iustin Pop
83 19f38ee8 Iustin Pop
-- | A complete request, as received from Ganeti.
84 34c00528 Iustin Pop
data Request = Request RqType ClusterData
85 ebf38064 Iustin Pop
               deriving (Show, Read)
86 19f38ee8 Iustin Pop
87 7b6e99b3 Iustin Pop
-- | The cluster state.
88 7b6e99b3 Iustin Pop
data ClusterData = ClusterData
89 ebf38064 Iustin Pop
  { cdGroups    :: Group.List    -- ^ The node group list
90 ebf38064 Iustin Pop
  , cdNodes     :: Node.List     -- ^ The node list
91 ebf38064 Iustin Pop
  , cdInstances :: Instance.List -- ^ The instance list
92 ebf38064 Iustin Pop
  , cdTags      :: [String]      -- ^ The cluster tags
93 ebf38064 Iustin Pop
  } deriving (Show, Read)
94 7b6e99b3 Iustin Pop
95 efe98965 Guido Trotter
-- | The priority of a match in a lookup result.
96 efe98965 Guido Trotter
data MatchPriority = ExactMatch
97 efe98965 Guido Trotter
                   | MultipleMatch
98 efe98965 Guido Trotter
                   | PartialMatch
99 efe98965 Guido Trotter
                   | FailMatch
100 efe98965 Guido Trotter
                   deriving (Show, Read, Enum, Eq, Ord)
101 efe98965 Guido Trotter
102 efe98965 Guido Trotter
-- | The result of a name lookup in a list.
103 efe98965 Guido Trotter
data LookupResult = LookupResult
104 ebf38064 Iustin Pop
  { lrMatchPriority :: MatchPriority -- ^ The result type
105 ebf38064 Iustin Pop
  -- | Matching value (for ExactMatch, PartialMatch), Lookup string otherwise
106 ebf38064 Iustin Pop
  , lrContent :: String
107 ebf38064 Iustin Pop
  } deriving (Show, Read)
108 efe98965 Guido Trotter
109 efe98965 Guido Trotter
-- | Lookup results have an absolute preference ordering.
110 efe98965 Guido Trotter
instance Eq LookupResult where
111 efe98965 Guido Trotter
  (==) = (==) `on` lrMatchPriority
112 efe98965 Guido Trotter
113 efe98965 Guido Trotter
instance Ord LookupResult where
114 efe98965 Guido Trotter
  compare = compare `on` lrMatchPriority
115 efe98965 Guido Trotter
116 7b6e99b3 Iustin Pop
-- | An empty cluster.
117 7b6e99b3 Iustin Pop
emptyCluster :: ClusterData
118 7b6e99b3 Iustin Pop
emptyCluster = ClusterData Container.empty Container.empty Container.empty []
119 7b6e99b3 Iustin Pop
120 19f38ee8 Iustin Pop
-- * Functions
121 19f38ee8 Iustin Pop
122 9188aeef Iustin Pop
-- | Lookups a node into an assoc list.
123 6ff78049 Iustin Pop
lookupNode :: (Monad m) => NameAssoc -> String -> String -> m Ndx
124 040afc35 Iustin Pop
lookupNode ktn inst node =
125 ebf38064 Iustin Pop
  case M.lookup node ktn of
126 ebf38064 Iustin Pop
    Nothing -> fail $ "Unknown node '" ++ node ++ "' for instance " ++ inst
127 ebf38064 Iustin Pop
    Just idx -> return idx
128 040afc35 Iustin Pop
129 9188aeef Iustin Pop
-- | Lookups an instance into an assoc list.
130 6ff78049 Iustin Pop
lookupInstance :: (Monad m) => NameAssoc -> String -> m Idx
131 5a1edeb6 Iustin Pop
lookupInstance kti inst =
132 ebf38064 Iustin Pop
  case M.lookup inst kti of
133 ebf38064 Iustin Pop
    Nothing -> fail $ "Unknown instance '" ++ inst ++ "'"
134 ebf38064 Iustin Pop
    Just idx -> return idx
135 5a1edeb6 Iustin Pop
136 f4531f51 Iustin Pop
-- | Lookups a group into an assoc list.
137 f4531f51 Iustin Pop
lookupGroup :: (Monad m) => NameAssoc -> String -> String -> m Gdx
138 f4531f51 Iustin Pop
lookupGroup ktg nname gname =
139 ebf38064 Iustin Pop
  case M.lookup gname ktg of
140 ebf38064 Iustin Pop
    Nothing -> fail $ "Unknown group '" ++ gname ++ "' for node " ++ nname
141 ebf38064 Iustin Pop
    Just idx -> return idx
142 f4531f51 Iustin Pop
143 efe98965 Guido Trotter
-- | Check for prefix matches in names.
144 efe98965 Guido Trotter
-- Implemented in Ganeti core utils.text.MatchNameComponent
145 efe98965 Guido Trotter
-- as the regexp r"^%s(\..*)?$" % re.escape(key)
146 efe98965 Guido Trotter
prefixMatch :: String  -- ^ Lookup
147 efe98965 Guido Trotter
            -> String  -- ^ Full name
148 efe98965 Guido Trotter
            -> Bool    -- ^ Whether there is a prefix match
149 05ff7a00 Agata Murawska
prefixMatch = isPrefixOf . (++ ".")
150 efe98965 Guido Trotter
151 efe98965 Guido Trotter
-- | Is the lookup priority a "good" one?
152 efe98965 Guido Trotter
goodMatchPriority :: MatchPriority -> Bool
153 efe98965 Guido Trotter
goodMatchPriority ExactMatch = True
154 efe98965 Guido Trotter
goodMatchPriority PartialMatch = True
155 efe98965 Guido Trotter
goodMatchPriority _ = False
156 efe98965 Guido Trotter
157 efe98965 Guido Trotter
-- | Is the lookup result an actual match?
158 efe98965 Guido Trotter
goodLookupResult :: LookupResult -> Bool
159 efe98965 Guido Trotter
goodLookupResult = goodMatchPriority . lrMatchPriority
160 efe98965 Guido Trotter
161 efe98965 Guido Trotter
-- | Compares a canonical name and a lookup string.
162 efe98965 Guido Trotter
compareNameComponent :: String        -- ^ Canonical (target) name
163 efe98965 Guido Trotter
                     -> String        -- ^ Partial (lookup) name
164 efe98965 Guido Trotter
                     -> LookupResult  -- ^ Result of the lookup
165 efe98965 Guido Trotter
compareNameComponent cnl lkp =
166 efe98965 Guido Trotter
  select (LookupResult FailMatch lkp)
167 efe98965 Guido Trotter
  [ (cnl == lkp          , LookupResult ExactMatch cnl)
168 efe98965 Guido Trotter
  , (prefixMatch lkp cnl , LookupResult PartialMatch cnl)
169 efe98965 Guido Trotter
  ]
170 efe98965 Guido Trotter
171 efe98965 Guido Trotter
-- | Lookup a string and choose the best result.
172 efe98965 Guido Trotter
chooseLookupResult :: String       -- ^ Lookup key
173 efe98965 Guido Trotter
                   -> String       -- ^ String to compare to the lookup key
174 efe98965 Guido Trotter
                   -> LookupResult -- ^ Previous result
175 efe98965 Guido Trotter
                   -> LookupResult -- ^ New result
176 efe98965 Guido Trotter
chooseLookupResult lkp cstr old =
177 efe98965 Guido Trotter
  -- default: use class order to pick the minimum result
178 efe98965 Guido Trotter
  select (min new old)
179 efe98965 Guido Trotter
  -- special cases:
180 efe98965 Guido Trotter
  -- short circuit if the new result is an exact match
181 05ff7a00 Agata Murawska
  [ (lrMatchPriority new == ExactMatch, new)
182 efe98965 Guido Trotter
  -- if both are partial matches generate a multiple match
183 efe98965 Guido Trotter
  , (partial2, LookupResult MultipleMatch lkp)
184 efe98965 Guido Trotter
  ] where new = compareNameComponent cstr lkp
185 efe98965 Guido Trotter
          partial2 = all ((PartialMatch==) . lrMatchPriority) [old, new]
186 efe98965 Guido Trotter
187 efe98965 Guido Trotter
-- | Find the canonical name for a lookup string in a list of names.
188 efe98965 Guido Trotter
lookupName :: [String]      -- ^ List of keys
189 efe98965 Guido Trotter
           -> String        -- ^ Lookup string
190 efe98965 Guido Trotter
           -> LookupResult  -- ^ Result of the lookup
191 efe98965 Guido Trotter
lookupName l s = foldr (chooseLookupResult s)
192 efe98965 Guido Trotter
                       (LookupResult FailMatch s) l
193 efe98965 Guido Trotter
194 9188aeef Iustin Pop
-- | Given a list of elements (and their names), assign indices to them.
195 497e30a1 Iustin Pop
assignIndices :: (Element a) =>
196 497e30a1 Iustin Pop
                 [(String, a)]
197 99b63608 Iustin Pop
              -> (NameAssoc, Container.Container a)
198 2d0ca2c5 Iustin Pop
assignIndices nodes =
199 2d0ca2c5 Iustin Pop
  let (na, idx_node) =
200 2d0ca2c5 Iustin Pop
          unzip . map (\ (idx, (k, v)) -> ((k, idx), (idx, setIdx v idx)))
201 2d0ca2c5 Iustin Pop
          . zip [0..] $ nodes
202 cb0c77ff Iustin Pop
  in (M.fromList na, Container.fromList idx_node)
203 78694255 Iustin Pop
204 9188aeef Iustin Pop
-- | For each instance, add its index to its primary and secondary nodes.
205 99b63608 Iustin Pop
fixNodes :: Node.List
206 aa8d2e71 Iustin Pop
         -> Instance.Instance
207 99b63608 Iustin Pop
         -> Node.List
208 aa8d2e71 Iustin Pop
fixNodes accu inst =
209 ebf38064 Iustin Pop
  let pdx = Instance.pNode inst
210 ebf38064 Iustin Pop
      sdx = Instance.sNode inst
211 ebf38064 Iustin Pop
      pold = Container.find pdx accu
212 ebf38064 Iustin Pop
      pnew = Node.setPri pold inst
213 ebf38064 Iustin Pop
      ac2 = Container.add pdx pnew accu
214 ebf38064 Iustin Pop
  in if sdx /= Node.noSecondary
215 ebf38064 Iustin Pop
       then let sold = Container.find sdx accu
216 ebf38064 Iustin Pop
                snew = Node.setSec sold inst
217 ebf38064 Iustin Pop
            in Container.add sdx snew ac2
218 ebf38064 Iustin Pop
       else ac2
219 e4c5beaf Iustin Pop
220 525bfb36 Iustin Pop
-- | Remove non-selected tags from the exclusion list.
221 0f15cc76 Iustin Pop
filterExTags :: [String] -> Instance.Instance -> Instance.Instance
222 0f15cc76 Iustin Pop
filterExTags tl inst =
223 ebf38064 Iustin Pop
  let old_tags = Instance.tags inst
224 ebf38064 Iustin Pop
      new_tags = filter (\tag -> any (`isPrefixOf` tag) tl) old_tags
225 ebf38064 Iustin Pop
  in inst { Instance.tags = new_tags }
226 0f15cc76 Iustin Pop
227 525bfb36 Iustin Pop
-- | Update the movable attribute.
228 c6ccc073 Guido Trotter
updateMovable :: [String]           -- ^ Selected instances (if not empty)
229 c6ccc073 Guido Trotter
              -> [String]           -- ^ Excluded instances
230 c6ccc073 Guido Trotter
              -> Instance.Instance  -- ^ Target Instance
231 c6ccc073 Guido Trotter
              -> Instance.Instance  -- ^ Target Instance with updated attribute
232 c6ccc073 Guido Trotter
updateMovable selinsts exinsts inst =
233 ebf38064 Iustin Pop
  if Instance.sNode inst == Node.noSecondary ||
234 ebf38064 Iustin Pop
     Instance.name inst `elem` exinsts ||
235 ebf38064 Iustin Pop
     not (null selinsts || Instance.name inst `elem` selinsts)
236 39f979b8 Iustin Pop
    then Instance.setMovable inst False
237 39f979b8 Iustin Pop
    else inst
238 39f979b8 Iustin Pop
239 f9fc7a63 Iustin Pop
-- | Compute the longest common suffix of a list of strings that
240 525bfb36 Iustin Pop
-- starts with a dot.
241 8472a321 Iustin Pop
longestDomain :: [String] -> String
242 e4c5beaf Iustin Pop
longestDomain [] = ""
243 8472a321 Iustin Pop
longestDomain (x:xs) =
244 ebf38064 Iustin Pop
  foldr (\ suffix accu -> if all (isSuffixOf suffix) xs
245 ebf38064 Iustin Pop
                            then suffix
246 ebf38064 Iustin Pop
                            else accu)
247 ebf38064 Iustin Pop
          "" $ filter (isPrefixOf ".") (tails x)
248 e4c5beaf Iustin Pop
249 525bfb36 Iustin Pop
-- | Extracts the exclusion tags from the cluster configuration.
250 f5e67f55 Iustin Pop
extractExTags :: [String] -> [String]
251 f5e67f55 Iustin Pop
extractExTags =
252 ebf38064 Iustin Pop
  map (drop (length exTagsPrefix)) .
253 ebf38064 Iustin Pop
  filter (isPrefixOf exTagsPrefix)
254 f5e67f55 Iustin Pop
255 525bfb36 Iustin Pop
-- | Extracts the common suffix from node\/instance names.
256 3e4480e0 Iustin Pop
commonSuffix :: Node.List -> Instance.List -> String
257 3e4480e0 Iustin Pop
commonSuffix nl il =
258 ebf38064 Iustin Pop
  let node_names = map Node.name $ Container.elems nl
259 ebf38064 Iustin Pop
      inst_names = map Instance.name $ Container.elems il
260 ebf38064 Iustin Pop
  in longestDomain (node_names ++ inst_names)
261 3e4480e0 Iustin Pop
262 9188aeef Iustin Pop
-- | Initializer function that loads the data from a node and instance
263 9188aeef Iustin Pop
-- list and massages it into the correct format.
264 aa8d2e71 Iustin Pop
mergeData :: [(String, DynUtil)]  -- ^ Instance utilisation data
265 0f15cc76 Iustin Pop
          -> [String]             -- ^ Exclusion tags
266 2d1708e0 Guido Trotter
          -> [String]             -- ^ Selected instances (if not empty)
267 2d1708e0 Guido Trotter
          -> [String]             -- ^ Excluded instances
268 f4f6eb0b Iustin Pop
          -> ClusterData          -- ^ Data from backends
269 c0e31451 Iustin Pop
          -> Result ClusterData   -- ^ Fixed cluster data
270 2d1708e0 Guido Trotter
mergeData um extags selinsts exinsts cdata@(ClusterData _ nl il2 tags) =
271 99b63608 Iustin Pop
  let il = Container.elems il2
272 a5f8dcdc Iustin Pop
      il3 = foldl' (\im (name, n_util) ->
273 a5f8dcdc Iustin Pop
                        case Container.findByName im name of
274 a5f8dcdc Iustin Pop
                          Nothing -> im -- skipping unknown instance
275 a5f8dcdc Iustin Pop
                          Just inst ->
276 a5f8dcdc Iustin Pop
                              let new_i = inst { Instance.util = n_util }
277 a5f8dcdc Iustin Pop
                              in Container.add (Instance.idx inst) new_i im
278 a5f8dcdc Iustin Pop
                   ) il2 um
279 f5e67f55 Iustin Pop
      allextags = extags ++ extractExTags tags
280 424ec11d Guido Trotter
      inst_names = map Instance.name il
281 424ec11d Guido Trotter
      selinst_lkp = map (lookupName inst_names) selinsts
282 424ec11d Guido Trotter
      exinst_lkp = map (lookupName inst_names) exinsts
283 424ec11d Guido Trotter
      lkp_unknown = filter (not . goodLookupResult) (selinst_lkp ++ exinst_lkp)
284 424ec11d Guido Trotter
      selinst_names = map lrContent selinst_lkp
285 424ec11d Guido Trotter
      exinst_names = map lrContent exinst_lkp
286 39f979b8 Iustin Pop
      il4 = Container.map (filterExTags allextags .
287 424ec11d Guido Trotter
                           updateMovable selinst_names exinst_names) il3
288 0f15cc76 Iustin Pop
      nl2 = foldl' fixNodes nl (Container.elems il4)
289 1b0a6356 Iustin Pop
      nl3 = Container.map (`Node.buildPeers` il4) nl2
290 99b63608 Iustin Pop
      node_names = map Node.name (Container.elems nl)
291 8472a321 Iustin Pop
      common_suffix = longestDomain (node_names ++ inst_names)
292 3e4480e0 Iustin Pop
      snl = Container.map (computeAlias common_suffix) nl3
293 3e4480e0 Iustin Pop
      sil = Container.map (computeAlias common_suffix) il4
294 424ec11d Guido Trotter
  in if' (null lkp_unknown)
295 424ec11d Guido Trotter
         (Ok cdata { cdNodes = snl, cdInstances = sil })
296 424ec11d Guido Trotter
         (Bad $ "Unknown instance(s): " ++ show(map lrContent lkp_unknown))
297 446d8827 Iustin Pop
298 9188aeef Iustin Pop
-- | Checks the cluster data for consistency.
299 262a08a2 Iustin Pop
checkData :: Node.List -> Instance.List
300 262a08a2 Iustin Pop
          -> ([String], Node.List)
301 dbd6700b Iustin Pop
checkData nl il =
302 446d8827 Iustin Pop
    Container.mapAccum
303 446d8827 Iustin Pop
        (\ msgs node ->
304 dbd6700b Iustin Pop
             let nname = Node.name node
305 5182e970 Iustin Pop
                 nilst = map (`Container.find` il) (Node.pList node)
306 61bbbed7 Agata Murawska
                 dilst = filter Instance.instanceDown nilst
307 446d8827 Iustin Pop
                 adj_mem = sum . map Instance.mem $ dilst
308 2060348b Iustin Pop
                 delta_mem = truncate (Node.tMem node)
309 2060348b Iustin Pop
                             - Node.nMem node
310 2060348b Iustin Pop
                             - Node.fMem node
311 9f6dcdea Iustin Pop
                             - nodeImem node il
312 446d8827 Iustin Pop
                             + adj_mem
313 2060348b Iustin Pop
                 delta_dsk = truncate (Node.tDsk node)
314 2060348b Iustin Pop
                             - Node.fDsk node
315 9f6dcdea Iustin Pop
                             - nodeIdsk node il
316 446d8827 Iustin Pop
                 newn = Node.setFmem (Node.setXmem node delta_mem)
317 2060348b Iustin Pop
                        (Node.fMem node - adj_mem)
318 bdd8c739 Iustin Pop
                 umsg1 =
319 bdd8c739 Iustin Pop
                   if delta_mem > 512 || delta_dsk > 1024
320 3603605a Iustin Pop
                      then printf "node %s is missing %d MB ram \
321 3603605a Iustin Pop
                                  \and %d GB disk"
322 3603605a Iustin Pop
                                  nname delta_mem (delta_dsk `div` 1024):msgs
323 bdd8c739 Iustin Pop
                      else msgs
324 bdd8c739 Iustin Pop
             in (umsg1, newn)
325 446d8827 Iustin Pop
        ) [] nl
326 446d8827 Iustin Pop
327 446d8827 Iustin Pop
-- | Compute the amount of memory used by primary instances on a node.
328 262a08a2 Iustin Pop
nodeImem :: Node.Node -> Instance.List -> Int
329 446d8827 Iustin Pop
nodeImem node il =
330 ebf38064 Iustin Pop
  let rfind = flip Container.find il
331 ebf38064 Iustin Pop
      il' = map rfind $ Node.pList node
332 ebf38064 Iustin Pop
      oil' = filter (not . Instance.instanceOffline) il'
333 ebf38064 Iustin Pop
  in sum . map Instance.mem $ oil'
334 61bbbed7 Agata Murawska
335 446d8827 Iustin Pop
336 446d8827 Iustin Pop
-- | Compute the amount of disk used by instances on a node (either primary
337 446d8827 Iustin Pop
-- or secondary).
338 262a08a2 Iustin Pop
nodeIdsk :: Node.Node -> Instance.List -> Int
339 446d8827 Iustin Pop
nodeIdsk node il =
340 ebf38064 Iustin Pop
  let rfind = flip Container.find il
341 ebf38064 Iustin Pop
  in sum . map (Instance.dsk . rfind)
342 ebf38064 Iustin Pop
       $ Node.pList node ++ Node.sList node