Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Types.hs @ 3771d104

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