Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Node.hs @ 8bc34c7b

History | View | Annotate | Download (20.9 kB)

1 e4f08c46 Iustin Pop
{-| Module describing a node.
2 e4f08c46 Iustin Pop
3 e4f08c46 Iustin Pop
    All updates are functional (copy-based) and return a new node with
4 e4f08c46 Iustin Pop
    updated value.
5 e4f08c46 Iustin Pop
-}
6 e4f08c46 Iustin Pop
7 e2fa2baf Iustin Pop
{-
8 e2fa2baf Iustin Pop
9 d6eec019 Iustin Pop
Copyright (C) 2009, 2010, 2011, 2012 Google Inc.
10 e2fa2baf Iustin Pop
11 e2fa2baf Iustin Pop
This program is free software; you can redistribute it and/or modify
12 e2fa2baf Iustin Pop
it under the terms of the GNU General Public License as published by
13 e2fa2baf Iustin Pop
the Free Software Foundation; either version 2 of the License, or
14 e2fa2baf Iustin Pop
(at your option) any later version.
15 e2fa2baf Iustin Pop
16 e2fa2baf Iustin Pop
This program is distributed in the hope that it will be useful, but
17 e2fa2baf Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
18 e2fa2baf Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 e2fa2baf Iustin Pop
General Public License for more details.
20 e2fa2baf Iustin Pop
21 e2fa2baf Iustin Pop
You should have received a copy of the GNU General Public License
22 e2fa2baf Iustin Pop
along with this program; if not, write to the Free Software
23 e2fa2baf Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 e2fa2baf Iustin Pop
02110-1301, USA.
25 e2fa2baf Iustin Pop
26 e2fa2baf Iustin Pop
-}
27 e2fa2baf Iustin Pop
28 669d7e3d Iustin Pop
module Ganeti.HTools.Node
29 fd7a7c73 Iustin Pop
  ( Node(..)
30 fd7a7c73 Iustin Pop
  , List
31 fd7a7c73 Iustin Pop
  -- * Constructor
32 fd7a7c73 Iustin Pop
  , create
33 fd7a7c73 Iustin Pop
  -- ** Finalization after data loading
34 fd7a7c73 Iustin Pop
  , buildPeers
35 fd7a7c73 Iustin Pop
  , setIdx
36 fd7a7c73 Iustin Pop
  , setAlias
37 fd7a7c73 Iustin Pop
  , setOffline
38 fd7a7c73 Iustin Pop
  , setXmem
39 fd7a7c73 Iustin Pop
  , setFmem
40 fd7a7c73 Iustin Pop
  , setPri
41 fd7a7c73 Iustin Pop
  , setSec
42 fd7a7c73 Iustin Pop
  , setMdsk
43 fd7a7c73 Iustin Pop
  , setMcpu
44 d6eec019 Iustin Pop
  , setPolicy
45 fd7a7c73 Iustin Pop
  -- * Tag maps
46 fd7a7c73 Iustin Pop
  , addTags
47 fd7a7c73 Iustin Pop
  , delTags
48 fd7a7c73 Iustin Pop
  , rejectAddTags
49 fd7a7c73 Iustin Pop
  -- * Instance (re)location
50 fd7a7c73 Iustin Pop
  , removePri
51 fd7a7c73 Iustin Pop
  , removeSec
52 fd7a7c73 Iustin Pop
  , addPri
53 fd7a7c73 Iustin Pop
  , addPriEx
54 fd7a7c73 Iustin Pop
  , addSec
55 fd7a7c73 Iustin Pop
  , addSecEx
56 fd7a7c73 Iustin Pop
  -- * Stats
57 fd7a7c73 Iustin Pop
  , availDisk
58 fd7a7c73 Iustin Pop
  , availMem
59 fd7a7c73 Iustin Pop
  , availCpu
60 fd7a7c73 Iustin Pop
  , iMem
61 fd7a7c73 Iustin Pop
  , iDsk
62 fd7a7c73 Iustin Pop
  , conflictingPrimaries
63 fd7a7c73 Iustin Pop
  -- * Formatting
64 fd7a7c73 Iustin Pop
  , defaultFields
65 fd7a7c73 Iustin Pop
  , showHeader
66 fd7a7c73 Iustin Pop
  , showField
67 fd7a7c73 Iustin Pop
  , list
68 fd7a7c73 Iustin Pop
  -- * Misc stuff
69 fd7a7c73 Iustin Pop
  , AssocList
70 fd7a7c73 Iustin Pop
  , AllocElement
71 fd7a7c73 Iustin Pop
  , noSecondary
72 fd7a7c73 Iustin Pop
  , computeGroups
73 fd7a7c73 Iustin Pop
  ) where
74 e4f08c46 Iustin Pop
75 a68004b7 Iustin Pop
import Data.List hiding (group)
76 5f0b9579 Iustin Pop
import qualified Data.Map as Map
77 1e4b5230 Iustin Pop
import qualified Data.Foldable as Foldable
78 d8bcd0a8 Iustin Pop
import Data.Ord (comparing)
79 e4f08c46 Iustin Pop
import Text.Printf (printf)
80 e4f08c46 Iustin Pop
81 669d7e3d Iustin Pop
import qualified Ganeti.HTools.Container as Container
82 669d7e3d Iustin Pop
import qualified Ganeti.HTools.Instance as Instance
83 12e6776a Iustin Pop
import qualified Ganeti.HTools.PeerMap as P
84 e4f08c46 Iustin Pop
85 262a08a2 Iustin Pop
import qualified Ganeti.HTools.Types as T
86 262a08a2 Iustin Pop
87 9188aeef Iustin Pop
-- * Type declarations
88 9188aeef Iustin Pop
89 525bfb36 Iustin Pop
-- | The tag map type.
90 5f0b9579 Iustin Pop
type TagMap = Map.Map String Int
91 5f0b9579 Iustin Pop
92 9188aeef Iustin Pop
-- | The node type.
93 12e6776a Iustin Pop
data Node = Node
94 fd7a7c73 Iustin Pop
  { name     :: String    -- ^ The node name
95 fd7a7c73 Iustin Pop
  , alias    :: String    -- ^ The shortened name (for display purposes)
96 fd7a7c73 Iustin Pop
  , tMem     :: Double    -- ^ Total memory (MiB)
97 fd7a7c73 Iustin Pop
  , nMem     :: Int       -- ^ Node memory (MiB)
98 fd7a7c73 Iustin Pop
  , fMem     :: Int       -- ^ Free memory (MiB)
99 fd7a7c73 Iustin Pop
  , xMem     :: Int       -- ^ Unaccounted memory (MiB)
100 fd7a7c73 Iustin Pop
  , tDsk     :: Double    -- ^ Total disk space (MiB)
101 fd7a7c73 Iustin Pop
  , fDsk     :: Int       -- ^ Free disk space (MiB)
102 fd7a7c73 Iustin Pop
  , tCpu     :: Double    -- ^ Total CPU count
103 fd7a7c73 Iustin Pop
  , uCpu     :: Int       -- ^ Used VCPU count
104 8bc34c7b Iustin Pop
  , spindleCount :: Int   -- ^ Node spindles (spindle_count node parameter)
105 fd7a7c73 Iustin Pop
  , pList    :: [T.Idx]   -- ^ List of primary instance indices
106 fd7a7c73 Iustin Pop
  , sList    :: [T.Idx]   -- ^ List of secondary instance indices
107 fd7a7c73 Iustin Pop
  , idx      :: T.Ndx     -- ^ Internal index for book-keeping
108 fd7a7c73 Iustin Pop
  , peers    :: P.PeerMap -- ^ Pnode to instance mapping
109 fd7a7c73 Iustin Pop
  , failN1   :: Bool      -- ^ Whether the node has failed n1
110 fd7a7c73 Iustin Pop
  , rMem     :: Int       -- ^ Maximum memory needed for failover by
111 fd7a7c73 Iustin Pop
                          -- primaries of this node
112 fd7a7c73 Iustin Pop
  , pMem     :: Double    -- ^ Percent of free memory
113 fd7a7c73 Iustin Pop
  , pDsk     :: Double    -- ^ Percent of free disk
114 fd7a7c73 Iustin Pop
  , pRem     :: Double    -- ^ Percent of reserved memory
115 fd7a7c73 Iustin Pop
  , pCpu     :: Double    -- ^ Ratio of virtual to physical CPUs
116 fd7a7c73 Iustin Pop
  , mDsk     :: Double    -- ^ Minimum free disk ratio
117 fd7a7c73 Iustin Pop
  , loDsk    :: Int       -- ^ Autocomputed from mDsk low disk
118 fd7a7c73 Iustin Pop
                          -- threshold
119 fd7a7c73 Iustin Pop
  , hiCpu    :: Int       -- ^ Autocomputed from mCpu high cpu
120 fd7a7c73 Iustin Pop
                          -- threshold
121 8bc34c7b Iustin Pop
  , hiSpindles :: Double  -- ^ Auto-computed from policy spindle_ratio
122 8bc34c7b Iustin Pop
                          -- and the node spindle count
123 8bc34c7b Iustin Pop
  , instSpindles :: Double -- ^ Spindles used by instances
124 fd7a7c73 Iustin Pop
  , offline  :: Bool      -- ^ Whether the node should not be used for
125 fd7a7c73 Iustin Pop
                          -- allocations and skipped from score
126 fd7a7c73 Iustin Pop
                          -- computations
127 fd7a7c73 Iustin Pop
  , utilPool :: T.DynUtil -- ^ Total utilisation capacity
128 fd7a7c73 Iustin Pop
  , utilLoad :: T.DynUtil -- ^ Sum of instance utilisation
129 fd7a7c73 Iustin Pop
  , pTags    :: TagMap    -- ^ Map of primary instance tags and their count
130 fd7a7c73 Iustin Pop
  , group    :: T.Gdx     -- ^ The node's group (index)
131 d6eec019 Iustin Pop
  , iPolicy  :: T.IPolicy -- ^ The instance policy (of the node's group)
132 fd7a7c73 Iustin Pop
  } deriving (Show, Read, Eq)
133 e4f08c46 Iustin Pop
134 262a08a2 Iustin Pop
instance T.Element Node where
135 fd7a7c73 Iustin Pop
  nameOf = name
136 fd7a7c73 Iustin Pop
  idxOf = idx
137 fd7a7c73 Iustin Pop
  setAlias = setAlias
138 fd7a7c73 Iustin Pop
  setIdx = setIdx
139 fd7a7c73 Iustin Pop
  allNames n = [name n, alias n]
140 262a08a2 Iustin Pop
141 9188aeef Iustin Pop
-- | A simple name for the int, node association list.
142 608efcce Iustin Pop
type AssocList = [(T.Ndx, Node)]
143 040afc35 Iustin Pop
144 9188aeef Iustin Pop
-- | A simple name for a node map.
145 262a08a2 Iustin Pop
type List = Container.Container Node
146 262a08a2 Iustin Pop
147 1fe81531 Iustin Pop
-- | A simple name for an allocation element (here just for logistic
148 525bfb36 Iustin Pop
-- reasons).
149 7d3f4253 Iustin Pop
type AllocElement = (List, Instance.Instance, [Node], T.Score)
150 1fe81531 Iustin Pop
151 9188aeef Iustin Pop
-- | Constant node index for a non-moveable instance.
152 608efcce Iustin Pop
noSecondary :: T.Ndx
153 040afc35 Iustin Pop
noSecondary = -1
154 040afc35 Iustin Pop
155 5f0b9579 Iustin Pop
-- * Helper functions
156 5f0b9579 Iustin Pop
157 525bfb36 Iustin Pop
-- | Add a tag to a tagmap.
158 5f0b9579 Iustin Pop
addTag :: TagMap -> String -> TagMap
159 5f0b9579 Iustin Pop
addTag t s = Map.insertWith (+) s 1 t
160 5f0b9579 Iustin Pop
161 525bfb36 Iustin Pop
-- | Add multiple tags.
162 5f0b9579 Iustin Pop
addTags :: TagMap -> [String] -> TagMap
163 5f0b9579 Iustin Pop
addTags = foldl' addTag
164 5f0b9579 Iustin Pop
165 525bfb36 Iustin Pop
-- | Adjust or delete a tag from a tagmap.
166 5f0b9579 Iustin Pop
delTag :: TagMap -> String -> TagMap
167 5f0b9579 Iustin Pop
delTag t s = Map.update (\v -> if v > 1
168 fd7a7c73 Iustin Pop
                                 then Just (v-1)
169 fd7a7c73 Iustin Pop
                                 else Nothing)
170 5f0b9579 Iustin Pop
             s t
171 5f0b9579 Iustin Pop
172 525bfb36 Iustin Pop
-- | Remove multiple tags.
173 5f0b9579 Iustin Pop
delTags :: TagMap -> [String] -> TagMap
174 5f0b9579 Iustin Pop
delTags = foldl' delTag
175 5f0b9579 Iustin Pop
176 525bfb36 Iustin Pop
-- | Check if we can add a list of tags to a tagmap.
177 5f0b9579 Iustin Pop
rejectAddTags :: TagMap -> [String] -> Bool
178 5182e970 Iustin Pop
rejectAddTags t = any (`Map.member` t)
179 5f0b9579 Iustin Pop
180 1e4b5230 Iustin Pop
-- | Check how many primary instances have conflicting tags. The
181 1e4b5230 Iustin Pop
-- algorithm to compute this is to sum the count of all tags, then
182 1e4b5230 Iustin Pop
-- subtract the size of the tag map (since each tag has at least one,
183 1e4b5230 Iustin Pop
-- non-conflicting instance); this is equivalent to summing the
184 1e4b5230 Iustin Pop
-- values in the tag map minus one.
185 1e4b5230 Iustin Pop
conflictingPrimaries :: Node -> Int
186 1e4b5230 Iustin Pop
conflictingPrimaries (Node { pTags = t }) = Foldable.sum t - Map.size t
187 1e4b5230 Iustin Pop
188 9188aeef Iustin Pop
-- * Initialization functions
189 e4f08c46 Iustin Pop
190 9188aeef Iustin Pop
-- | Create a new node.
191 9188aeef Iustin Pop
--
192 9188aeef Iustin Pop
-- The index and the peers maps are empty, and will be need to be
193 9188aeef Iustin Pop
-- update later via the 'setIdx' and 'buildPeers' functions.
194 1a82215d Iustin Pop
create :: String -> Double -> Int -> Int -> Double
195 8bc34c7b Iustin Pop
       -> Int -> Double -> Bool -> Int -> T.Gdx -> Node
196 2727257a Iustin Pop
create name_init mem_t_init mem_n_init mem_f_init
197 8bc34c7b Iustin Pop
       dsk_t_init dsk_f_init cpu_t_init offline_init spindles_init
198 8bc34c7b Iustin Pop
       group_init =
199 fd7a7c73 Iustin Pop
  Node { name = name_init
200 fd7a7c73 Iustin Pop
       , alias = name_init
201 fd7a7c73 Iustin Pop
       , tMem = mem_t_init
202 fd7a7c73 Iustin Pop
       , nMem = mem_n_init
203 fd7a7c73 Iustin Pop
       , fMem = mem_f_init
204 fd7a7c73 Iustin Pop
       , tDsk = dsk_t_init
205 fd7a7c73 Iustin Pop
       , fDsk = dsk_f_init
206 fd7a7c73 Iustin Pop
       , tCpu = cpu_t_init
207 8bc34c7b Iustin Pop
       , spindleCount = spindles_init
208 fd7a7c73 Iustin Pop
       , uCpu = 0
209 fd7a7c73 Iustin Pop
       , pList = []
210 fd7a7c73 Iustin Pop
       , sList = []
211 fd7a7c73 Iustin Pop
       , failN1 = True
212 fd7a7c73 Iustin Pop
       , idx = -1
213 fd7a7c73 Iustin Pop
       , peers = P.empty
214 fd7a7c73 Iustin Pop
       , rMem = 0
215 fd7a7c73 Iustin Pop
       , pMem = fromIntegral mem_f_init / mem_t_init
216 fd7a7c73 Iustin Pop
       , pDsk = fromIntegral dsk_f_init / dsk_t_init
217 fd7a7c73 Iustin Pop
       , pRem = 0
218 fd7a7c73 Iustin Pop
       , pCpu = 0
219 fd7a7c73 Iustin Pop
       , offline = offline_init
220 fd7a7c73 Iustin Pop
       , xMem = 0
221 fd7a7c73 Iustin Pop
       , mDsk = T.defReservedDiskRatio
222 fd7a7c73 Iustin Pop
       , loDsk = mDskToloDsk T.defReservedDiskRatio dsk_t_init
223 487e1962 Iustin Pop
       , hiCpu = mCpuTohiCpu (T.iPolicyVcpuRatio T.defIPolicy) cpu_t_init
224 8bc34c7b Iustin Pop
       , hiSpindles = computeHiSpindles (T.iPolicySpindleRatio T.defIPolicy)
225 8bc34c7b Iustin Pop
                      spindles_init
226 8bc34c7b Iustin Pop
       , instSpindles = 0
227 fd7a7c73 Iustin Pop
       , utilPool = T.baseUtil
228 fd7a7c73 Iustin Pop
       , utilLoad = T.zeroUtil
229 fd7a7c73 Iustin Pop
       , pTags = Map.empty
230 fd7a7c73 Iustin Pop
       , group = group_init
231 d6eec019 Iustin Pop
       , iPolicy = T.defIPolicy
232 fd7a7c73 Iustin Pop
       }
233 e4f08c46 Iustin Pop
234 525bfb36 Iustin Pop
-- | Conversion formula from mDsk\/tDsk to loDsk.
235 f4c0b8c5 Iustin Pop
mDskToloDsk :: Double -> Double -> Int
236 05ff7a00 Agata Murawska
mDskToloDsk mval = floor . (mval *)
237 f4c0b8c5 Iustin Pop
238 525bfb36 Iustin Pop
-- | Conversion formula from mCpu\/tCpu to hiCpu.
239 f4c0b8c5 Iustin Pop
mCpuTohiCpu :: Double -> Double -> Int
240 05ff7a00 Agata Murawska
mCpuTohiCpu mval = floor . (mval *)
241 f4c0b8c5 Iustin Pop
242 8bc34c7b Iustin Pop
-- | Conversiojn formula from spindles and spindle ratio to hiSpindles.
243 8bc34c7b Iustin Pop
computeHiSpindles :: Double -> Int -> Double
244 8bc34c7b Iustin Pop
computeHiSpindles spindle_ratio = (spindle_ratio *) . fromIntegral
245 8bc34c7b Iustin Pop
246 e4f08c46 Iustin Pop
-- | Changes the index.
247 9188aeef Iustin Pop
--
248 e4f08c46 Iustin Pop
-- This is used only during the building of the data structures.
249 608efcce Iustin Pop
setIdx :: Node -> T.Ndx -> Node
250 e4f08c46 Iustin Pop
setIdx t i = t {idx = i}
251 e4f08c46 Iustin Pop
252 8bcdde0c Iustin Pop
-- | Changes the alias.
253 8bcdde0c Iustin Pop
--
254 8bcdde0c Iustin Pop
-- This is used only during the building of the data structures.
255 8bcdde0c Iustin Pop
setAlias :: Node -> String -> Node
256 8bcdde0c Iustin Pop
setAlias t s = t { alias = s }
257 497e30a1 Iustin Pop
258 9188aeef Iustin Pop
-- | Sets the offline attribute.
259 c2c1ef0c Iustin Pop
setOffline :: Node -> Bool -> Node
260 c2c1ef0c Iustin Pop
setOffline t val = t { offline = val }
261 c2c1ef0c Iustin Pop
262 9188aeef Iustin Pop
-- | Sets the unnaccounted memory.
263 8c5b0a0d Iustin Pop
setXmem :: Node -> Int -> Node
264 2060348b Iustin Pop
setXmem t val = t { xMem = val }
265 8c5b0a0d Iustin Pop
266 525bfb36 Iustin Pop
-- | Sets the max disk usage ratio.
267 844eff86 Iustin Pop
setMdsk :: Node -> Double -> Node
268 f4c0b8c5 Iustin Pop
setMdsk t val = t { mDsk = val, loDsk = mDskToloDsk val (tDsk t) }
269 844eff86 Iustin Pop
270 487e1962 Iustin Pop
-- | Sets the max cpu usage ratio. This will update the node's
271 487e1962 Iustin Pop
-- ipolicy, losing sharing (but it should be a seldomly done operation).
272 844eff86 Iustin Pop
setMcpu :: Node -> Double -> Node
273 487e1962 Iustin Pop
setMcpu t val =
274 487e1962 Iustin Pop
  let new_ipol = (iPolicy t) { T.iPolicyVcpuRatio = val }
275 487e1962 Iustin Pop
  in t { hiCpu = mCpuTohiCpu val (tCpu t), iPolicy = new_ipol }
276 844eff86 Iustin Pop
277 d6eec019 Iustin Pop
-- | Sets the policy.
278 d6eec019 Iustin Pop
setPolicy :: T.IPolicy -> Node -> Node
279 487e1962 Iustin Pop
setPolicy pol node =
280 487e1962 Iustin Pop
  node { iPolicy = pol
281 8bc34c7b Iustin Pop
       , hiCpu = mCpuTohiCpu (T.iPolicyVcpuRatio pol) (tCpu node)
282 8bc34c7b Iustin Pop
       , hiSpindles = computeHiSpindles (T.iPolicySpindleRatio pol)
283 8bc34c7b Iustin Pop
                      (spindleCount node)
284 8bc34c7b Iustin Pop
       }
285 d6eec019 Iustin Pop
286 e4f08c46 Iustin Pop
-- | Computes the maximum reserved memory for peers from a peer map.
287 12e6776a Iustin Pop
computeMaxRes :: P.PeerMap -> P.Elem
288 12e6776a Iustin Pop
computeMaxRes = P.maxElem
289 e4f08c46 Iustin Pop
290 e4f08c46 Iustin Pop
-- | Builds the peer map for a given node.
291 9cf4267a Iustin Pop
buildPeers :: Node -> Instance.List -> Node
292 9cf4267a Iustin Pop
buildPeers t il =
293 fd7a7c73 Iustin Pop
  let mdata = map
294 fd7a7c73 Iustin Pop
              (\i_idx -> let inst = Container.find i_idx il
295 55bd1414 Iustin Pop
                             mem = if Instance.usesSecMem inst
296 929b60d8 Iustin Pop
                                     then Instance.mem inst
297 929b60d8 Iustin Pop
                                     else 0
298 fd7a7c73 Iustin Pop
                         in (Instance.pNode inst, mem))
299 fd7a7c73 Iustin Pop
              (sList t)
300 fd7a7c73 Iustin Pop
      pmap = P.accumArray (+) mdata
301 fd7a7c73 Iustin Pop
      new_rmem = computeMaxRes pmap
302 fd7a7c73 Iustin Pop
      new_failN1 = fMem t <= new_rmem
303 fd7a7c73 Iustin Pop
      new_prem = fromIntegral new_rmem / tMem t
304 fd7a7c73 Iustin Pop
  in t {peers=pmap, failN1 = new_failN1, rMem = new_rmem, pRem = new_prem}
305 e4f08c46 Iustin Pop
306 a488a217 Iustin Pop
-- | Assigns an instance to a node as primary and update the used VCPU
307 5f0b9579 Iustin Pop
-- count, utilisation data and tags map.
308 a488a217 Iustin Pop
setPri :: Node -> Instance.Instance -> Node
309 aa8d2e71 Iustin Pop
setPri t inst = t { pList = Instance.idx inst:pList t
310 a488a217 Iustin Pop
                  , uCpu = new_count
311 aa8d2e71 Iustin Pop
                  , pCpu = fromIntegral new_count / tCpu t
312 aa8d2e71 Iustin Pop
                  , utilLoad = utilLoad t `T.addUtil` Instance.util inst
313 5f0b9579 Iustin Pop
                  , pTags = addTags (pTags t) (Instance.tags inst)
314 aa8d2e71 Iustin Pop
                  }
315 55bd1414 Iustin Pop
  where new_count = Instance.applyIfOnline inst (+ Instance.vcpus inst)
316 55bd1414 Iustin Pop
                    (uCpu t )
317 9188aeef Iustin Pop
318 9188aeef Iustin Pop
-- | Assigns an instance to a node as secondary without other updates.
319 a488a217 Iustin Pop
setSec :: Node -> Instance.Instance -> Node
320 aa8d2e71 Iustin Pop
setSec t inst = t { sList = Instance.idx inst:sList t
321 aa8d2e71 Iustin Pop
                  , utilLoad = old_load { T.dskWeight = T.dskWeight old_load +
322 aa8d2e71 Iustin Pop
                                          T.dskWeight (Instance.util inst) }
323 aa8d2e71 Iustin Pop
                  }
324 fd7a7c73 Iustin Pop
  where old_load = utilLoad t
325 1a82215d Iustin Pop
326 9188aeef Iustin Pop
-- * Update functions
327 9188aeef Iustin Pop
328 9188aeef Iustin Pop
-- | Sets the free memory.
329 9188aeef Iustin Pop
setFmem :: Node -> Int -> Node
330 9188aeef Iustin Pop
setFmem t new_mem =
331 fd7a7c73 Iustin Pop
  let new_n1 = new_mem <= rMem t
332 fd7a7c73 Iustin Pop
      new_mp = fromIntegral new_mem / tMem t
333 fd7a7c73 Iustin Pop
  in t { fMem = new_mem, failN1 = new_n1, pMem = new_mp }
334 9188aeef Iustin Pop
335 e4f08c46 Iustin Pop
-- | Removes a primary instance.
336 e4f08c46 Iustin Pop
removePri :: Node -> Instance.Instance -> Node
337 e4f08c46 Iustin Pop
removePri t inst =
338 fd7a7c73 Iustin Pop
  let iname = Instance.idx inst
339 fd7a7c73 Iustin Pop
      new_plist = delete iname (pList t)
340 fd7a7c73 Iustin Pop
      new_mem = Instance.applyIfOnline inst (+ Instance.mem inst) (fMem t)
341 fd7a7c73 Iustin Pop
      new_dsk = fDsk t + Instance.dsk inst
342 fd7a7c73 Iustin Pop
      new_mp = fromIntegral new_mem / tMem t
343 fd7a7c73 Iustin Pop
      new_dp = fromIntegral new_dsk / tDsk t
344 fd7a7c73 Iustin Pop
      new_failn1 = new_mem <= rMem t
345 fd7a7c73 Iustin Pop
      new_ucpu = Instance.applyIfOnline inst
346 fd7a7c73 Iustin Pop
                 (\x -> x - Instance.vcpus inst) (uCpu t)
347 fd7a7c73 Iustin Pop
      new_rcpu = fromIntegral new_ucpu / tCpu t
348 fd7a7c73 Iustin Pop
      new_load = utilLoad t `T.subUtil` Instance.util inst
349 fd7a7c73 Iustin Pop
  in t { pList = new_plist, fMem = new_mem, fDsk = new_dsk
350 fd7a7c73 Iustin Pop
       , failN1 = new_failn1, pMem = new_mp, pDsk = new_dp
351 fd7a7c73 Iustin Pop
       , uCpu = new_ucpu, pCpu = new_rcpu, utilLoad = new_load
352 fd7a7c73 Iustin Pop
       , pTags = delTags (pTags t) (Instance.tags inst) }
353 e4f08c46 Iustin Pop
354 e4f08c46 Iustin Pop
-- | Removes a secondary instance.
355 e4f08c46 Iustin Pop
removeSec :: Node -> Instance.Instance -> Node
356 e4f08c46 Iustin Pop
removeSec t inst =
357 fd7a7c73 Iustin Pop
  let iname = Instance.idx inst
358 fd7a7c73 Iustin Pop
      cur_dsk = fDsk t
359 fd7a7c73 Iustin Pop
      pnode = Instance.pNode inst
360 fd7a7c73 Iustin Pop
      new_slist = delete iname (sList t)
361 3603605a Iustin Pop
      new_dsk = if Instance.usesLocalStorage inst
362 10f055ac Iustin Pop
                  then cur_dsk + Instance.dsk inst
363 10f055ac Iustin Pop
                  else cur_dsk
364 fd7a7c73 Iustin Pop
      old_peers = peers t
365 fd7a7c73 Iustin Pop
      old_peem = P.find pnode old_peers
366 55bd1414 Iustin Pop
      new_peem =  if Instance.usesSecMem inst
367 929b60d8 Iustin Pop
                    then old_peem - Instance.mem inst
368 929b60d8 Iustin Pop
                    else old_peem
369 fd7a7c73 Iustin Pop
      new_peers = if new_peem > 0
370 124b7cd7 Iustin Pop
                    then P.add pnode new_peem old_peers
371 124b7cd7 Iustin Pop
                    else P.remove pnode old_peers
372 fd7a7c73 Iustin Pop
      old_rmem = rMem t
373 fd7a7c73 Iustin Pop
      new_rmem = if old_peem < old_rmem
374 bbd8efd2 Iustin Pop
                   then old_rmem
375 bbd8efd2 Iustin Pop
                   else computeMaxRes new_peers
376 fd7a7c73 Iustin Pop
      new_prem = fromIntegral new_rmem / tMem t
377 fd7a7c73 Iustin Pop
      new_failn1 = fMem t <= new_rmem
378 fd7a7c73 Iustin Pop
      new_dp = fromIntegral new_dsk / tDsk t
379 fd7a7c73 Iustin Pop
      old_load = utilLoad t
380 fd7a7c73 Iustin Pop
      new_load = old_load { T.dskWeight = T.dskWeight old_load -
381 fd7a7c73 Iustin Pop
                                          T.dskWeight (Instance.util inst) }
382 fd7a7c73 Iustin Pop
  in t { sList = new_slist, fDsk = new_dsk, peers = new_peers
383 fd7a7c73 Iustin Pop
       , failN1 = new_failn1, rMem = new_rmem, pDsk = new_dp
384 fd7a7c73 Iustin Pop
       , pRem = new_prem, utilLoad = new_load }
385 e4f08c46 Iustin Pop
386 3e3c9393 Iustin Pop
-- | Adds a primary instance (basic version).
387 f2280553 Iustin Pop
addPri :: Node -> Instance.Instance -> T.OpResult Node
388 3e3c9393 Iustin Pop
addPri = addPriEx False
389 3e3c9393 Iustin Pop
390 3e3c9393 Iustin Pop
-- | Adds a primary instance (extended version).
391 3e3c9393 Iustin Pop
addPriEx :: Bool               -- ^ Whether to override the N+1 and
392 3e3c9393 Iustin Pop
                               -- other /soft/ checks, useful if we
393 3e3c9393 Iustin Pop
                               -- come from a worse status
394 3e3c9393 Iustin Pop
                               -- (e.g. offline)
395 3e3c9393 Iustin Pop
         -> Node               -- ^ The target node
396 3e3c9393 Iustin Pop
         -> Instance.Instance  -- ^ The instance to add
397 3e3c9393 Iustin Pop
         -> T.OpResult Node    -- ^ The result of the operation,
398 3e3c9393 Iustin Pop
                               -- either the new version of the node
399 3e3c9393 Iustin Pop
                               -- or a failure mode
400 3e3c9393 Iustin Pop
addPriEx force t inst =
401 fd7a7c73 Iustin Pop
  let iname = Instance.idx inst
402 fd7a7c73 Iustin Pop
      uses_disk = Instance.usesLocalStorage inst
403 fd7a7c73 Iustin Pop
      cur_dsk = fDsk t
404 fd7a7c73 Iustin Pop
      new_mem = Instance.applyIfOnline inst
405 fd7a7c73 Iustin Pop
                (\x -> x - Instance.mem inst) (fMem t)
406 fd7a7c73 Iustin Pop
      new_dsk = if uses_disk
407 10f055ac Iustin Pop
                  then cur_dsk - Instance.dsk inst
408 10f055ac Iustin Pop
                  else cur_dsk
409 fd7a7c73 Iustin Pop
      new_failn1 = new_mem <= rMem t
410 fd7a7c73 Iustin Pop
      new_ucpu = Instance.applyIfOnline inst (+ Instance.vcpus inst) (uCpu t)
411 fd7a7c73 Iustin Pop
      new_pcpu = fromIntegral new_ucpu / tCpu t
412 fd7a7c73 Iustin Pop
      new_dp = fromIntegral new_dsk / tDsk t
413 487e1962 Iustin Pop
      l_cpu = T.iPolicyVcpuRatio $ iPolicy t
414 fd7a7c73 Iustin Pop
      new_load = utilLoad t `T.addUtil` Instance.util inst
415 fd7a7c73 Iustin Pop
      inst_tags = Instance.tags inst
416 fd7a7c73 Iustin Pop
      old_tags = pTags t
417 fd7a7c73 Iustin Pop
      strict = not force
418 fd7a7c73 Iustin Pop
  in case () of
419 fd7a7c73 Iustin Pop
       _ | new_mem <= 0 -> T.OpFail T.FailMem
420 fd7a7c73 Iustin Pop
         | uses_disk && new_dsk <= 0 -> T.OpFail T.FailDisk
421 fd7a7c73 Iustin Pop
         | uses_disk && mDsk t > new_dp && strict -> T.OpFail T.FailDisk
422 fd7a7c73 Iustin Pop
         | new_failn1 && not (failN1 t) && strict -> T.OpFail T.FailMem
423 fd7a7c73 Iustin Pop
         | l_cpu >= 0 && l_cpu < new_pcpu && strict -> T.OpFail T.FailCPU
424 fd7a7c73 Iustin Pop
         | rejectAddTags old_tags inst_tags -> T.OpFail T.FailTags
425 fd7a7c73 Iustin Pop
         | otherwise ->
426 fd7a7c73 Iustin Pop
           let new_plist = iname:pList t
427 fd7a7c73 Iustin Pop
               new_mp = fromIntegral new_mem / tMem t
428 fd7a7c73 Iustin Pop
               r = t { pList = new_plist, fMem = new_mem, fDsk = new_dsk
429 fd7a7c73 Iustin Pop
                     , failN1 = new_failn1, pMem = new_mp, pDsk = new_dp
430 fd7a7c73 Iustin Pop
                     , uCpu = new_ucpu, pCpu = new_pcpu
431 fd7a7c73 Iustin Pop
                     , utilLoad = new_load
432 fd7a7c73 Iustin Pop
                     , pTags = addTags old_tags inst_tags }
433 fd7a7c73 Iustin Pop
           in T.OpGood r
434 e4f08c46 Iustin Pop
435 3e3c9393 Iustin Pop
-- | Adds a secondary instance (basic version).
436 f2280553 Iustin Pop
addSec :: Node -> Instance.Instance -> T.Ndx -> T.OpResult Node
437 3e3c9393 Iustin Pop
addSec = addSecEx False
438 3e3c9393 Iustin Pop
439 3e3c9393 Iustin Pop
-- | Adds a secondary instance (extended version).
440 3e3c9393 Iustin Pop
addSecEx :: Bool -> Node -> Instance.Instance -> T.Ndx -> T.OpResult Node
441 3e3c9393 Iustin Pop
addSecEx force t inst pdx =
442 fd7a7c73 Iustin Pop
  let iname = Instance.idx inst
443 fd7a7c73 Iustin Pop
      old_peers = peers t
444 fd7a7c73 Iustin Pop
      old_mem = fMem t
445 fd7a7c73 Iustin Pop
      new_dsk = fDsk t - Instance.dsk inst
446 55bd1414 Iustin Pop
      secondary_needed_mem = if Instance.usesSecMem inst
447 929b60d8 Iustin Pop
                               then Instance.mem inst
448 929b60d8 Iustin Pop
                               else 0
449 fd7a7c73 Iustin Pop
      new_peem = P.find pdx old_peers + secondary_needed_mem
450 fd7a7c73 Iustin Pop
      new_peers = P.add pdx new_peem old_peers
451 fd7a7c73 Iustin Pop
      new_rmem = max (rMem t) new_peem
452 fd7a7c73 Iustin Pop
      new_prem = fromIntegral new_rmem / tMem t
453 fd7a7c73 Iustin Pop
      new_failn1 = old_mem <= new_rmem
454 fd7a7c73 Iustin Pop
      new_dp = fromIntegral new_dsk / tDsk t
455 fd7a7c73 Iustin Pop
      old_load = utilLoad t
456 fd7a7c73 Iustin Pop
      new_load = old_load { T.dskWeight = T.dskWeight old_load +
457 fd7a7c73 Iustin Pop
                                          T.dskWeight (Instance.util inst) }
458 fd7a7c73 Iustin Pop
      strict = not force
459 fd7a7c73 Iustin Pop
  in case () of
460 fd7a7c73 Iustin Pop
       _ | not (Instance.hasSecondary inst) -> T.OpFail T.FailDisk
461 fd7a7c73 Iustin Pop
         | new_dsk <= 0 -> T.OpFail T.FailDisk
462 fd7a7c73 Iustin Pop
         | mDsk t > new_dp && strict -> T.OpFail T.FailDisk
463 fd7a7c73 Iustin Pop
         | secondary_needed_mem >= old_mem && strict -> T.OpFail T.FailMem
464 fd7a7c73 Iustin Pop
         | new_failn1 && not (failN1 t) && strict -> T.OpFail T.FailMem
465 fd7a7c73 Iustin Pop
         | otherwise ->
466 fd7a7c73 Iustin Pop
           let new_slist = iname:sList t
467 fd7a7c73 Iustin Pop
               r = t { sList = new_slist, fDsk = new_dsk
468 fd7a7c73 Iustin Pop
                     , peers = new_peers, failN1 = new_failn1
469 fd7a7c73 Iustin Pop
                     , rMem = new_rmem, pDsk = new_dp
470 fd7a7c73 Iustin Pop
                     , pRem = new_prem, utilLoad = new_load }
471 fd7a7c73 Iustin Pop
           in T.OpGood r
472 e4f08c46 Iustin Pop
473 fe3d6f02 Iustin Pop
-- * Stats functions
474 fe3d6f02 Iustin Pop
475 525bfb36 Iustin Pop
-- | Computes the amount of available disk on a given node.
476 fe3d6f02 Iustin Pop
availDisk :: Node -> Int
477 fe3d6f02 Iustin Pop
availDisk t =
478 fd7a7c73 Iustin Pop
  let _f = fDsk t
479 fd7a7c73 Iustin Pop
      _l = loDsk t
480 fd7a7c73 Iustin Pop
  in if _f < _l
481 f4c0b8c5 Iustin Pop
       then 0
482 f4c0b8c5 Iustin Pop
       else _f - _l
483 fe3d6f02 Iustin Pop
484 525bfb36 Iustin Pop
-- | Computes the amount of used disk on a given node.
485 55da339e Iustin Pop
iDsk :: Node -> Int
486 55da339e Iustin Pop
iDsk t = truncate (tDsk t) - fDsk t
487 55da339e Iustin Pop
488 525bfb36 Iustin Pop
-- | Computes the amount of available memory on a given node.
489 1e3dccc8 Iustin Pop
availMem :: Node -> Int
490 1e3dccc8 Iustin Pop
availMem t =
491 fd7a7c73 Iustin Pop
  let _f = fMem t
492 fd7a7c73 Iustin Pop
      _l = rMem t
493 fd7a7c73 Iustin Pop
  in if _f < _l
494 1e3dccc8 Iustin Pop
       then 0
495 1e3dccc8 Iustin Pop
       else _f - _l
496 1e3dccc8 Iustin Pop
497 525bfb36 Iustin Pop
-- | Computes the amount of available memory on a given node.
498 1e3dccc8 Iustin Pop
availCpu :: Node -> Int
499 1e3dccc8 Iustin Pop
availCpu t =
500 fd7a7c73 Iustin Pop
  let _u = uCpu t
501 fd7a7c73 Iustin Pop
      _l = hiCpu t
502 fd7a7c73 Iustin Pop
  in if _l >= _u
503 1e3dccc8 Iustin Pop
       then _l - _u
504 1e3dccc8 Iustin Pop
       else 0
505 1e3dccc8 Iustin Pop
506 425af248 Iustin Pop
-- | The memory used by instances on a given node.
507 425af248 Iustin Pop
iMem :: Node -> Int
508 425af248 Iustin Pop
iMem t = truncate (tMem t) - nMem t - xMem t - fMem t
509 425af248 Iustin Pop
510 9188aeef Iustin Pop
-- * Display functions
511 01f6a5d2 Iustin Pop
512 525bfb36 Iustin Pop
-- | Return a field for a given node.
513 525bfb36 Iustin Pop
showField :: Node   -- ^ Node which we're querying
514 525bfb36 Iustin Pop
          -> String -- ^ Field name
515 525bfb36 Iustin Pop
          -> String -- ^ Field value as string
516 c5f7412e Iustin Pop
showField t field =
517 fd7a7c73 Iustin Pop
  case field of
518 fd7a7c73 Iustin Pop
    "idx"  -> printf "%4d" $ idx t
519 fd7a7c73 Iustin Pop
    "name" -> alias t
520 fd7a7c73 Iustin Pop
    "fqdn" -> name t
521 fd7a7c73 Iustin Pop
    "status" -> case () of
522 fd7a7c73 Iustin Pop
                  _ | offline t -> "-"
523 fd7a7c73 Iustin Pop
                    | failN1 t -> "*"
524 fd7a7c73 Iustin Pop
                    | otherwise -> " "
525 fd7a7c73 Iustin Pop
    "tmem" -> printf "%5.0f" $ tMem t
526 fd7a7c73 Iustin Pop
    "nmem" -> printf "%5d" $ nMem t
527 fd7a7c73 Iustin Pop
    "xmem" -> printf "%5d" $ xMem t
528 fd7a7c73 Iustin Pop
    "fmem" -> printf "%5d" $ fMem t
529 fd7a7c73 Iustin Pop
    "imem" -> printf "%5d" $ iMem t
530 fd7a7c73 Iustin Pop
    "rmem" -> printf "%5d" $ rMem t
531 fd7a7c73 Iustin Pop
    "amem" -> printf "%5d" $ fMem t - rMem t
532 fd7a7c73 Iustin Pop
    "tdsk" -> printf "%5.0f" $ tDsk t / 1024
533 fd7a7c73 Iustin Pop
    "fdsk" -> printf "%5d" $ fDsk t `div` 1024
534 fd7a7c73 Iustin Pop
    "tcpu" -> printf "%4.0f" $ tCpu t
535 fd7a7c73 Iustin Pop
    "ucpu" -> printf "%4d" $ uCpu t
536 fd7a7c73 Iustin Pop
    "pcnt" -> printf "%3d" $ length (pList t)
537 fd7a7c73 Iustin Pop
    "scnt" -> printf "%3d" $ length (sList t)
538 fd7a7c73 Iustin Pop
    "plist" -> show $ pList t
539 fd7a7c73 Iustin Pop
    "slist" -> show $ sList t
540 fd7a7c73 Iustin Pop
    "pfmem" -> printf "%6.4f" $ pMem t
541 fd7a7c73 Iustin Pop
    "pfdsk" -> printf "%6.4f" $ pDsk t
542 fd7a7c73 Iustin Pop
    "rcpu"  -> printf "%5.2f" $ pCpu t
543 fd7a7c73 Iustin Pop
    "cload" -> printf "%5.3f" uC
544 fd7a7c73 Iustin Pop
    "mload" -> printf "%5.3f" uM
545 fd7a7c73 Iustin Pop
    "dload" -> printf "%5.3f" uD
546 fd7a7c73 Iustin Pop
    "nload" -> printf "%5.3f" uN
547 fd7a7c73 Iustin Pop
    "ptags" -> intercalate "," . map (uncurry (printf "%s=%d")) .
548 fd7a7c73 Iustin Pop
               Map.toList $ pTags t
549 fd7a7c73 Iustin Pop
    "peermap" -> show $ peers t
550 fd7a7c73 Iustin Pop
    _ -> T.unknownField
551 fd7a7c73 Iustin Pop
  where
552 fd7a7c73 Iustin Pop
    T.DynUtil { T.cpuWeight = uC, T.memWeight = uM,
553 fd7a7c73 Iustin Pop
                T.dskWeight = uD, T.netWeight = uN } = utilLoad t
554 c5f7412e Iustin Pop
555 525bfb36 Iustin Pop
-- | Returns the header and numeric propery of a field.
556 76354e11 Iustin Pop
showHeader :: String -> (String, Bool)
557 76354e11 Iustin Pop
showHeader field =
558 fd7a7c73 Iustin Pop
  case field of
559 fd7a7c73 Iustin Pop
    "idx" -> ("Index", True)
560 fd7a7c73 Iustin Pop
    "name" -> ("Name", False)
561 fd7a7c73 Iustin Pop
    "fqdn" -> ("Name", False)
562 fd7a7c73 Iustin Pop
    "status" -> ("F", False)
563 fd7a7c73 Iustin Pop
    "tmem" -> ("t_mem", True)
564 fd7a7c73 Iustin Pop
    "nmem" -> ("n_mem", True)
565 fd7a7c73 Iustin Pop
    "xmem" -> ("x_mem", True)
566 fd7a7c73 Iustin Pop
    "fmem" -> ("f_mem", True)
567 fd7a7c73 Iustin Pop
    "imem" -> ("i_mem", True)
568 fd7a7c73 Iustin Pop
    "rmem" -> ("r_mem", True)
569 fd7a7c73 Iustin Pop
    "amem" -> ("a_mem", True)
570 fd7a7c73 Iustin Pop
    "tdsk" -> ("t_dsk", True)
571 fd7a7c73 Iustin Pop
    "fdsk" -> ("f_dsk", True)
572 fd7a7c73 Iustin Pop
    "tcpu" -> ("pcpu", True)
573 fd7a7c73 Iustin Pop
    "ucpu" -> ("vcpu", True)
574 fd7a7c73 Iustin Pop
    "pcnt" -> ("pcnt", True)
575 fd7a7c73 Iustin Pop
    "scnt" -> ("scnt", True)
576 fd7a7c73 Iustin Pop
    "plist" -> ("primaries", True)
577 fd7a7c73 Iustin Pop
    "slist" -> ("secondaries", True)
578 fd7a7c73 Iustin Pop
    "pfmem" -> ("p_fmem", True)
579 fd7a7c73 Iustin Pop
    "pfdsk" -> ("p_fdsk", True)
580 fd7a7c73 Iustin Pop
    "rcpu"  -> ("r_cpu", True)
581 fd7a7c73 Iustin Pop
    "cload" -> ("lCpu", True)
582 fd7a7c73 Iustin Pop
    "mload" -> ("lMem", True)
583 fd7a7c73 Iustin Pop
    "dload" -> ("lDsk", True)
584 fd7a7c73 Iustin Pop
    "nload" -> ("lNet", True)
585 fd7a7c73 Iustin Pop
    "ptags" -> ("PrimaryTags", False)
586 fd7a7c73 Iustin Pop
    "peermap" -> ("PeerMap", False)
587 fd7a7c73 Iustin Pop
    -- TODO: add node fields (group.uuid, group)
588 fd7a7c73 Iustin Pop
    _ -> (T.unknownField, False)
589 c5f7412e Iustin Pop
590 e4f08c46 Iustin Pop
-- | String converter for the node list functionality.
591 76354e11 Iustin Pop
list :: [String] -> Node -> [String]
592 76354e11 Iustin Pop
list fields t = map (showField t) fields
593 76354e11 Iustin Pop
594 525bfb36 Iustin Pop
-- | Constant holding the fields we're displaying by default.
595 76354e11 Iustin Pop
defaultFields :: [String]
596 76354e11 Iustin Pop
defaultFields =
597 fd7a7c73 Iustin Pop
  [ "status", "name", "tmem", "nmem", "imem", "xmem", "fmem"
598 fd7a7c73 Iustin Pop
  , "rmem", "tdsk", "fdsk", "tcpu", "ucpu", "pcnt", "scnt"
599 fd7a7c73 Iustin Pop
  , "pfmem", "pfdsk", "rcpu"
600 fd7a7c73 Iustin Pop
  , "cload", "mload", "dload", "nload" ]
601 d8bcd0a8 Iustin Pop
602 d8bcd0a8 Iustin Pop
-- | Split a list of nodes into a list of (node group UUID, list of
603 525bfb36 Iustin Pop
-- associated nodes).
604 10ef6b4e Iustin Pop
computeGroups :: [Node] -> [(T.Gdx, [Node])]
605 d8bcd0a8 Iustin Pop
computeGroups nodes =
606 d8bcd0a8 Iustin Pop
  let nodes' = sortBy (comparing group) nodes
607 d8bcd0a8 Iustin Pop
      nodes'' = groupBy (\a b -> group a == group b) nodes'
608 d8bcd0a8 Iustin Pop
  in map (\nl -> (group (head nl), nl)) nodes''