7 Copyright (C) 2009 Google Inc.
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
26 module Ganeti.HTools.Types
51 -- | The instance index type.
54 -- | The node index type.
57 -- | The type used to hold name-to-idx mappings.
58 type NameAssoc = [(String, Int)]
60 -- | A separate name for the cluster score type.
63 -- | A separate name for a weight metric.
66 -- | The resource spec type.
68 { rspecCpu :: Int -- ^ Requested VCPUs
69 , rspecMem :: Int -- ^ Requested memory
70 , rspecDsk :: Int -- ^ Requested disk
73 -- | The dynamic resource specs of a machine (i.e. load or load
74 -- capacity, as opposed to size).
75 data DynUtil = DynUtil
76 { cpuWeight :: Weight -- ^ Standardised CPU usage
77 , memWeight :: Weight -- ^ Standardised memory load
78 , dskWeight :: Weight -- ^ Standardised disk I\/O usage
79 , netWeight :: Weight -- ^ Standardised network usage
82 -- | Initial empty utilisation
84 zeroUtil = DynUtil { cpuWeight = 0, memWeight = 0
85 , dskWeight = 0, netWeight = 0 }
88 baseUtil = DynUtil { cpuWeight = 1, memWeight = 1
89 , dskWeight = 1, netWeight = 1 }
91 addUtil :: DynUtil -> DynUtil -> DynUtil
92 addUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
93 DynUtil (a1+b1) (a2+b2) (a3+b3) (a4+b4)
95 subUtil :: DynUtil -> DynUtil -> DynUtil
96 subUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
97 DynUtil (a1-b1) (a2-b2) (a3-b3) (a4-b4)
99 -- | The description of an instance placement. It contains the
100 -- instance index, the new primary and secondary node, the move being
101 -- performed and the score of the cluster after the move.
102 type Placement = (Idx, Ndx, Ndx, IMove, Score)
104 -- | An instance move definition
105 data IMove = Failover -- ^ Failover the instance (f)
106 | ReplacePrimary Ndx -- ^ Replace primary (f, r:np, f)
107 | ReplaceSecondary Ndx -- ^ Replace secondary (r:ns)
108 | ReplaceAndFailover Ndx -- ^ Replace secondary, failover (r:np, f)
109 | FailoverAndReplace Ndx -- ^ Failover, replace secondary (f, r:ns)
112 -- | Formatted solution output for one move (involved nodes and
114 type MoveJob = ([Ndx], Idx, IMove, [String])
116 -- | A list of command elements
117 type JobSet = [MoveJob]
119 -- | Connection timeout (when using non-file methods).
123 -- | The default timeout for queries (when using non-file methods).
129 This is similar to the JSON library Result type - *very* similar, but
130 we want to use it in multiple places, so we abstract it into a
139 instance Monad Result where
140 (>>=) (Bad x) _ = Bad x
141 (>>=) (Ok x) fn = fn x
145 -- | Reason for an operation's falure
146 data FailMode = FailMem -- ^ Failed due to not enough RAM
147 | FailDisk -- ^ Failed due to not enough disk
148 | FailCPU -- ^ Failed due to not enough CPU capacity
149 | FailN1 -- ^ Failed due to not passing N1 checks
150 deriving (Eq, Enum, Bounded, Show)
152 -- | List with failure statistics
153 type FailStats = [(FailMode, Int)]
155 -- | Either-like data-type customized for our failure modes
156 data OpResult a = OpFail FailMode -- ^ Failed operation
157 | OpGood a -- ^ Success operation
159 instance Monad OpResult where
160 (OpGood x) >>= fn = fn x
161 (OpFail y) >>= _ = OpFail y
164 -- | A generic class for items that have updateable names and indices.
165 class Element a where
166 -- | Returns the name of the element
167 nameOf :: a -> String
168 -- | Returns the index of the element
170 -- | Updates the name of the element
171 setName :: a -> String -> a
172 -- | Updates the index of the element
173 setIdx :: a -> Int -> a