Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Types.hs @ 72bb6b4e

History | View | Annotate | Download (8.9 kB)

1 e9aaa3c6 Iustin Pop
{-# LANGUAGE TemplateHaskell #-}
2 e9aaa3c6 Iustin Pop
3 e4c5beaf Iustin Pop
{-| Some common types.
4 e4c5beaf Iustin Pop
5 e4c5beaf Iustin Pop
-}
6 e4c5beaf Iustin Pop
7 e2fa2baf Iustin Pop
{-
8 e2fa2baf Iustin Pop
9 2e5eb96a Iustin Pop
Copyright (C) 2009, 2010, 2011 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 e4c5beaf Iustin Pop
module Ganeti.HTools.Types
29 19f38ee8 Iustin Pop
    ( Idx
30 19f38ee8 Iustin Pop
    , Ndx
31 0dc1bf87 Iustin Pop
    , Gdx
32 19f38ee8 Iustin Pop
    , NameAssoc
33 92e32d76 Iustin Pop
    , Score
34 2180829f Iustin Pop
    , Weight
35 c4d98278 Iustin Pop
    , GroupID
36 0dc1bf87 Iustin Pop
    , AllocPolicy(..)
37 2c9336a4 Iustin Pop
    , allocPolicyFromString
38 2c9336a4 Iustin Pop
    , allocPolicyToString
39 1f9066c0 Iustin Pop
    , RSpec(..)
40 2180829f Iustin Pop
    , DynUtil(..)
41 2180829f Iustin Pop
    , zeroUtil
42 ee9724b9 Iustin Pop
    , baseUtil
43 2180829f Iustin Pop
    , addUtil
44 2180829f Iustin Pop
    , subUtil
45 f4c0b8c5 Iustin Pop
    , defVcpuRatio
46 f4c0b8c5 Iustin Pop
    , defReservedDiskRatio
47 1e3dccc8 Iustin Pop
    , unitMem
48 1e3dccc8 Iustin Pop
    , unitCpu
49 1e3dccc8 Iustin Pop
    , unitDsk
50 82ea2874 Iustin Pop
    , unknownField
51 92e32d76 Iustin Pop
    , Placement
52 92e32d76 Iustin Pop
    , IMove(..)
53 cc25e437 Iustin Pop
    , DiskTemplate(..)
54 2c9336a4 Iustin Pop
    , diskTemplateToString
55 2c9336a4 Iustin Pop
    , diskTemplateFromString
56 0e8ae201 Iustin Pop
    , MoveJob
57 0e8ae201 Iustin Pop
    , JobSet
58 19f38ee8 Iustin Pop
    , Result(..)
59 06fb841e Iustin Pop
    , isOk
60 06fb841e Iustin Pop
    , isBad
61 a30b473c Iustin Pop
    , eitherToResult
62 19f38ee8 Iustin Pop
    , Element(..)
63 f2280553 Iustin Pop
    , FailMode(..)
64 478df686 Iustin Pop
    , FailStats
65 f2280553 Iustin Pop
    , OpResult(..)
66 a30b473c Iustin Pop
    , opToResult
67 135a6c6a Iustin Pop
    , connTimeout
68 135a6c6a Iustin Pop
    , queryTimeout
69 1fe412bb Iustin Pop
    , EvacMode(..)
70 19f38ee8 Iustin Pop
    ) where
71 e4c5beaf Iustin Pop
72 1c7c4578 Iustin Pop
import Control.Monad
73 2d0ca2c5 Iustin Pop
import qualified Data.Map as M
74 b2ba4669 Iustin Pop
import qualified Text.JSON as JSON
75 2d0ca2c5 Iustin Pop
76 2e5eb96a Iustin Pop
import qualified Ganeti.Constants as C
77 e9aaa3c6 Iustin Pop
import qualified Ganeti.THH as THH
78 2e5eb96a Iustin Pop
79 9188aeef Iustin Pop
-- | The instance index type.
80 608efcce Iustin Pop
type Idx = Int
81 608efcce Iustin Pop
82 9188aeef Iustin Pop
-- | The node index type.
83 608efcce Iustin Pop
type Ndx = Int
84 608efcce Iustin Pop
85 0dc1bf87 Iustin Pop
-- | The group index type.
86 0dc1bf87 Iustin Pop
type Gdx = Int
87 0dc1bf87 Iustin Pop
88 9188aeef Iustin Pop
-- | The type used to hold name-to-idx mappings.
89 2d0ca2c5 Iustin Pop
type NameAssoc = M.Map String Int
90 e4c5beaf Iustin Pop
91 92e32d76 Iustin Pop
-- | A separate name for the cluster score type.
92 92e32d76 Iustin Pop
type Score = Double
93 92e32d76 Iustin Pop
94 2180829f Iustin Pop
-- | A separate name for a weight metric.
95 2180829f Iustin Pop
type Weight = Double
96 2180829f Iustin Pop
97 0dc1bf87 Iustin Pop
-- | The Group UUID type.
98 c4d98278 Iustin Pop
type GroupID = String
99 c4d98278 Iustin Pop
100 0dc1bf87 Iustin Pop
-- | The Group allocation policy type.
101 73206d0a Iustin Pop
--
102 73206d0a Iustin Pop
-- Note that the order of constructors is important as the automatic
103 73206d0a Iustin Pop
-- Ord instance will order them in the order they are defined, so when
104 73206d0a Iustin Pop
-- changing this data type be careful about the interaction with the
105 73206d0a Iustin Pop
-- desired sorting order.
106 e9aaa3c6 Iustin Pop
$(THH.declareSADT "AllocPolicy"
107 e9aaa3c6 Iustin Pop
         [ ("AllocPreferred",   'C.allocPolicyPreferred)
108 e9aaa3c6 Iustin Pop
         , ("AllocLastResort",  'C.allocPolicyLastResort)
109 e9aaa3c6 Iustin Pop
         , ("AllocUnallocable", 'C.allocPolicyUnallocable)
110 e9aaa3c6 Iustin Pop
         ])
111 e9aaa3c6 Iustin Pop
$(THH.makeJSONInstance ''AllocPolicy)
112 b2ba4669 Iustin Pop
113 1f9066c0 Iustin Pop
-- | The resource spec type.
114 1f9066c0 Iustin Pop
data RSpec = RSpec
115 1f9066c0 Iustin Pop
    { rspecCpu  :: Int  -- ^ Requested VCPUs
116 1f9066c0 Iustin Pop
    , rspecMem  :: Int  -- ^ Requested memory
117 1f9066c0 Iustin Pop
    , rspecDsk  :: Int  -- ^ Requested disk
118 6bc39970 Iustin Pop
    } deriving (Show, Read, Eq)
119 1f9066c0 Iustin Pop
120 2180829f Iustin Pop
-- | The dynamic resource specs of a machine (i.e. load or load
121 2180829f Iustin Pop
-- capacity, as opposed to size).
122 2180829f Iustin Pop
data DynUtil = DynUtil
123 2180829f Iustin Pop
    { cpuWeight :: Weight -- ^ Standardised CPU usage
124 2180829f Iustin Pop
    , memWeight :: Weight -- ^ Standardised memory load
125 c4ef235b Iustin Pop
    , dskWeight :: Weight -- ^ Standardised disk I\/O usage
126 2180829f Iustin Pop
    , netWeight :: Weight -- ^ Standardised network usage
127 6bc39970 Iustin Pop
    } deriving (Show, Read, Eq)
128 2180829f Iustin Pop
129 525bfb36 Iustin Pop
-- | Initial empty utilisation.
130 2180829f Iustin Pop
zeroUtil :: DynUtil
131 2180829f Iustin Pop
zeroUtil = DynUtil { cpuWeight = 0, memWeight = 0
132 2180829f Iustin Pop
                   , dskWeight = 0, netWeight = 0 }
133 2180829f Iustin Pop
134 525bfb36 Iustin Pop
-- | Base utilisation (used when no actual utilisation data is
135 525bfb36 Iustin Pop
-- supplied).
136 ee9724b9 Iustin Pop
baseUtil :: DynUtil
137 ee9724b9 Iustin Pop
baseUtil = DynUtil { cpuWeight = 1, memWeight = 1
138 ee9724b9 Iustin Pop
                   , dskWeight = 1, netWeight = 1 }
139 ee9724b9 Iustin Pop
140 525bfb36 Iustin Pop
-- | Sum two utilisation records.
141 2180829f Iustin Pop
addUtil :: DynUtil -> DynUtil -> DynUtil
142 2180829f Iustin Pop
addUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
143 2180829f Iustin Pop
    DynUtil (a1+b1) (a2+b2) (a3+b3) (a4+b4)
144 2180829f Iustin Pop
145 525bfb36 Iustin Pop
-- | Substracts one utilisation record from another.
146 2180829f Iustin Pop
subUtil :: DynUtil -> DynUtil -> DynUtil
147 2180829f Iustin Pop
subUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
148 2180829f Iustin Pop
    DynUtil (a1-b1) (a2-b2) (a3-b3) (a4-b4)
149 2180829f Iustin Pop
150 66dac8e0 Iustin Pop
-- | The description of an instance placement. It contains the
151 66dac8e0 Iustin Pop
-- instance index, the new primary and secondary node, the move being
152 66dac8e0 Iustin Pop
-- performed and the score of the cluster after the move.
153 66dac8e0 Iustin Pop
type Placement = (Idx, Ndx, Ndx, IMove, Score)
154 92e32d76 Iustin Pop
155 525bfb36 Iustin Pop
-- | An instance move definition.
156 92e32d76 Iustin Pop
data IMove = Failover                -- ^ Failover the instance (f)
157 92e32d76 Iustin Pop
           | ReplacePrimary Ndx      -- ^ Replace primary (f, r:np, f)
158 92e32d76 Iustin Pop
           | ReplaceSecondary Ndx    -- ^ Replace secondary (r:ns)
159 92e32d76 Iustin Pop
           | ReplaceAndFailover Ndx  -- ^ Replace secondary, failover (r:np, f)
160 92e32d76 Iustin Pop
           | FailoverAndReplace Ndx  -- ^ Failover, replace secondary (f, r:ns)
161 6bc39970 Iustin Pop
             deriving (Show, Read)
162 92e32d76 Iustin Pop
163 179c0828 Iustin Pop
-- | Instance disk template type.
164 e9aaa3c6 Iustin Pop
$(THH.declareSADT "DiskTemplate"
165 e9aaa3c6 Iustin Pop
     [ ("DTDiskless",   'C.dtDiskless)
166 e9aaa3c6 Iustin Pop
     , ("DTFile",       'C.dtFile)
167 e9aaa3c6 Iustin Pop
     , ("DTSharedFile", 'C.dtSharedFile)
168 e9aaa3c6 Iustin Pop
     , ("DTPlain",      'C.dtPlain)
169 e9aaa3c6 Iustin Pop
     , ("DTBlock",      'C.dtBlock)
170 e9aaa3c6 Iustin Pop
     , ("DTDrbd8",      'C.dtDrbd8)
171 e9aaa3c6 Iustin Pop
     ])
172 e9aaa3c6 Iustin Pop
$(THH.makeJSONInstance ''DiskTemplate)
173 cc25e437 Iustin Pop
174 0e8ae201 Iustin Pop
-- | Formatted solution output for one move (involved nodes and
175 525bfb36 Iustin Pop
-- commands.
176 924f9c16 Iustin Pop
type MoveJob = ([Ndx], Idx, IMove, [String])
177 0e8ae201 Iustin Pop
178 525bfb36 Iustin Pop
-- | Unknown field in table output.
179 82ea2874 Iustin Pop
unknownField :: String
180 82ea2874 Iustin Pop
unknownField = "<unknown field>"
181 82ea2874 Iustin Pop
182 525bfb36 Iustin Pop
-- | A list of command elements.
183 0e8ae201 Iustin Pop
type JobSet = [MoveJob]
184 0e8ae201 Iustin Pop
185 135a6c6a Iustin Pop
-- | Connection timeout (when using non-file methods).
186 135a6c6a Iustin Pop
connTimeout :: Int
187 135a6c6a Iustin Pop
connTimeout = 15
188 135a6c6a Iustin Pop
189 135a6c6a Iustin Pop
-- | The default timeout for queries (when using non-file methods).
190 135a6c6a Iustin Pop
queryTimeout :: Int
191 135a6c6a Iustin Pop
queryTimeout = 60
192 135a6c6a Iustin Pop
193 f4c0b8c5 Iustin Pop
-- | Default vcpu-to-pcpu ratio (randomly chosen value).
194 f4c0b8c5 Iustin Pop
defVcpuRatio :: Double
195 f4c0b8c5 Iustin Pop
defVcpuRatio = 64
196 f4c0b8c5 Iustin Pop
197 f4c0b8c5 Iustin Pop
-- | Default max disk usage ratio.
198 f4c0b8c5 Iustin Pop
defReservedDiskRatio :: Double
199 f4c0b8c5 Iustin Pop
defReservedDiskRatio = 0
200 f4c0b8c5 Iustin Pop
201 1e3dccc8 Iustin Pop
-- | Base memory unit.
202 1e3dccc8 Iustin Pop
unitMem :: Int
203 1e3dccc8 Iustin Pop
unitMem = 64
204 1e3dccc8 Iustin Pop
205 1e3dccc8 Iustin Pop
-- | Base disk unit.
206 1e3dccc8 Iustin Pop
unitDsk :: Int
207 1e3dccc8 Iustin Pop
unitDsk = 256
208 1e3dccc8 Iustin Pop
209 1e3dccc8 Iustin Pop
-- | Base vcpus unit.
210 1e3dccc8 Iustin Pop
unitCpu :: Int
211 1e3dccc8 Iustin Pop
unitCpu = 1
212 1e3dccc8 Iustin Pop
213 a30b473c Iustin Pop
-- | This is similar to the JSON library Result type - /very/ similar,
214 a30b473c Iustin Pop
-- but we want to use it in multiple places, so we abstract it into a
215 a30b473c Iustin Pop
-- mini-library here.
216 a30b473c Iustin Pop
--
217 a30b473c Iustin Pop
-- The failure value for this monad is simply a string.
218 e4c5beaf Iustin Pop
data Result a
219 e4c5beaf Iustin Pop
    = Bad String
220 e4c5beaf Iustin Pop
    | Ok a
221 1cb92fac Iustin Pop
    deriving (Show, Read, Eq)
222 e4c5beaf Iustin Pop
223 e4c5beaf Iustin Pop
instance Monad Result where
224 e4c5beaf Iustin Pop
    (>>=) (Bad x) _ = Bad x
225 e4c5beaf Iustin Pop
    (>>=) (Ok x) fn = fn x
226 e4c5beaf Iustin Pop
    return = Ok
227 e4c5beaf Iustin Pop
    fail = Bad
228 497e30a1 Iustin Pop
229 1c7c4578 Iustin Pop
instance MonadPlus Result where
230 1c7c4578 Iustin Pop
    mzero = Bad "zero Result when used as MonadPlus"
231 1c7c4578 Iustin Pop
    -- for mplus, when we 'add' two Bad values, we concatenate their
232 1c7c4578 Iustin Pop
    -- error descriptions
233 1c7c4578 Iustin Pop
    (Bad x) `mplus` (Bad y) = Bad (x ++ "; " ++ y)
234 1c7c4578 Iustin Pop
    (Bad _) `mplus` x = x
235 1c7c4578 Iustin Pop
    x@(Ok _) `mplus` _ = x
236 1c7c4578 Iustin Pop
237 525bfb36 Iustin Pop
-- | Simple checker for whether a 'Result' is OK.
238 06fb841e Iustin Pop
isOk :: Result a -> Bool
239 06fb841e Iustin Pop
isOk (Ok _) = True
240 06fb841e Iustin Pop
isOk _ = False
241 06fb841e Iustin Pop
242 525bfb36 Iustin Pop
-- | Simple checker for whether a 'Result' is a failure.
243 06fb841e Iustin Pop
isBad :: Result a  -> Bool
244 06fb841e Iustin Pop
isBad = not . isOk
245 06fb841e Iustin Pop
246 179c0828 Iustin Pop
-- | Converter from Either String to 'Result'.
247 a30b473c Iustin Pop
eitherToResult :: Either String a -> Result a
248 a30b473c Iustin Pop
eitherToResult (Left s) = Bad s
249 a30b473c Iustin Pop
eitherToResult (Right v) = Ok v
250 a30b473c Iustin Pop
251 525bfb36 Iustin Pop
-- | Reason for an operation's falure.
252 f2280553 Iustin Pop
data FailMode = FailMem  -- ^ Failed due to not enough RAM
253 f2280553 Iustin Pop
              | FailDisk -- ^ Failed due to not enough disk
254 f2280553 Iustin Pop
              | FailCPU  -- ^ Failed due to not enough CPU capacity
255 f2280553 Iustin Pop
              | FailN1   -- ^ Failed due to not passing N1 checks
256 5f0b9579 Iustin Pop
              | FailTags -- ^ Failed due to tag exclusion
257 6bc39970 Iustin Pop
                deriving (Eq, Enum, Bounded, Show, Read)
258 f2280553 Iustin Pop
259 525bfb36 Iustin Pop
-- | List with failure statistics.
260 478df686 Iustin Pop
type FailStats = [(FailMode, Int)]
261 478df686 Iustin Pop
262 525bfb36 Iustin Pop
-- | Either-like data-type customized for our failure modes.
263 a30b473c Iustin Pop
--
264 a30b473c Iustin Pop
-- The failure values for this monad track the specific allocation
265 a30b473c Iustin Pop
-- failures, so this is not a general error-monad (compare with the
266 a30b473c Iustin Pop
-- 'Result' data type). One downside is that this type cannot encode a
267 a30b473c Iustin Pop
-- generic failure mode, hence 'fail' for this monad is not defined
268 a30b473c Iustin Pop
-- and will cause an exception.
269 f2280553 Iustin Pop
data OpResult a = OpFail FailMode -- ^ Failed operation
270 f2280553 Iustin Pop
                | OpGood a        -- ^ Success operation
271 6bc39970 Iustin Pop
                  deriving (Show, Read)
272 f2280553 Iustin Pop
273 f2280553 Iustin Pop
instance Monad OpResult where
274 f2280553 Iustin Pop
    (OpGood x) >>= fn = fn x
275 f2280553 Iustin Pop
    (OpFail y) >>= _ = OpFail y
276 f2280553 Iustin Pop
    return = OpGood
277 f2280553 Iustin Pop
278 a30b473c Iustin Pop
-- | Conversion from 'OpResult' to 'Result'.
279 a30b473c Iustin Pop
opToResult :: OpResult a -> Result a
280 a30b473c Iustin Pop
opToResult (OpFail f) = Bad $ show f
281 a30b473c Iustin Pop
opToResult (OpGood v) = Ok v
282 a30b473c Iustin Pop
283 9188aeef Iustin Pop
-- | A generic class for items that have updateable names and indices.
284 497e30a1 Iustin Pop
class Element a where
285 9188aeef Iustin Pop
    -- | Returns the name of the element
286 262a08a2 Iustin Pop
    nameOf  :: a -> String
287 c854092b Iustin Pop
    -- | Returns all the known names of the element
288 c854092b Iustin Pop
    allNames :: a -> [String]
289 9188aeef Iustin Pop
    -- | Returns the index of the element
290 262a08a2 Iustin Pop
    idxOf   :: a -> Int
291 3e4480e0 Iustin Pop
    -- | Updates the alias of the element
292 3e4480e0 Iustin Pop
    setAlias :: a -> String -> a
293 3e4480e0 Iustin Pop
    -- | Compute the alias by stripping a given suffix (domain) from
294 525bfb36 Iustin Pop
    -- the name
295 3e4480e0 Iustin Pop
    computeAlias :: String -> a -> a
296 3e4480e0 Iustin Pop
    computeAlias dom e = setAlias e alias
297 3e4480e0 Iustin Pop
        where alias = take (length name - length dom) name
298 3e4480e0 Iustin Pop
              name = nameOf e
299 9188aeef Iustin Pop
    -- | Updates the index of the element
300 497e30a1 Iustin Pop
    setIdx  :: a -> Int -> a
301 1fe412bb Iustin Pop
302 1fe412bb Iustin Pop
-- | The iallocator node-evacuate evac_mode type.
303 e9aaa3c6 Iustin Pop
$(THH.declareSADT "EvacMode"
304 e9aaa3c6 Iustin Pop
     [ ("ChangePrimary",   'C.iallocatorNevacPri)
305 e9aaa3c6 Iustin Pop
     , ("ChangeSecondary", 'C.iallocatorNevacSec)
306 e9aaa3c6 Iustin Pop
     , ("ChangeAll",       'C.iallocatorNevacAll)
307 e9aaa3c6 Iustin Pop
     ])
308 e9aaa3c6 Iustin Pop
$(THH.makeJSONInstance ''EvacMode)