7 Copyright (C) 2009, 2010, 2011 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
44 , defReservedDiskRatio
70 import qualified Data.Map as M
71 import qualified Text.JSON as JSON
73 import qualified Ganeti.Constants as C
75 -- | The instance index type.
78 -- | The node index type.
81 -- | The group index type.
84 -- | The type used to hold name-to-idx mappings.
85 type NameAssoc = M.Map String Int
87 -- | A separate name for the cluster score type.
90 -- | A separate name for a weight metric.
93 -- | The Group UUID type.
96 -- | The Group allocation policy type.
98 -- Note that the order of constructors is important as the automatic
99 -- Ord instance will order them in the order they are defined, so when
100 -- changing this data type be careful about the interaction with the
101 -- desired sorting order.
103 = AllocPreferred -- ^ This is the normal status, the group
104 -- should be used normally during allocations
105 | AllocLastResort -- ^ This group should be used only as
106 -- last-resort, after the preferred groups
107 | AllocUnallocable -- ^ This group must not be used for new
109 deriving (Show, Read, Eq, Ord, Enum, Bounded)
111 -- | Convert a string to an alloc policy.
112 apolFromString :: (Monad m) => String -> m AllocPolicy
115 _ | s == C.allocPolicyPreferred -> return AllocPreferred
116 | s == C.allocPolicyLastResort -> return AllocLastResort
117 | s == C.allocPolicyUnallocable -> return AllocUnallocable
118 | otherwise -> fail $ "Invalid alloc policy mode: " ++ s
120 -- | Convert an alloc policy to the Ganeti string equivalent.
121 apolToString :: AllocPolicy -> String
122 apolToString AllocPreferred = C.allocPolicyPreferred
123 apolToString AllocLastResort = C.allocPolicyLastResort
124 apolToString AllocUnallocable = C.allocPolicyUnallocable
126 instance JSON.JSON AllocPolicy where
127 showJSON = JSON.showJSON . apolToString
128 readJSON s = case JSON.readJSON s of
129 JSON.Ok s' -> apolFromString s'
130 JSON.Error e -> JSON.Error $
131 "Can't parse alloc_policy: " ++ e
133 -- | The resource spec type.
135 { rspecCpu :: Int -- ^ Requested VCPUs
136 , rspecMem :: Int -- ^ Requested memory
137 , rspecDsk :: Int -- ^ Requested disk
138 } deriving (Show, Read, Eq)
140 -- | The dynamic resource specs of a machine (i.e. load or load
141 -- capacity, as opposed to size).
142 data DynUtil = DynUtil
143 { cpuWeight :: Weight -- ^ Standardised CPU usage
144 , memWeight :: Weight -- ^ Standardised memory load
145 , dskWeight :: Weight -- ^ Standardised disk I\/O usage
146 , netWeight :: Weight -- ^ Standardised network usage
147 } deriving (Show, Read, Eq)
149 -- | Initial empty utilisation.
151 zeroUtil = DynUtil { cpuWeight = 0, memWeight = 0
152 , dskWeight = 0, netWeight = 0 }
154 -- | Base utilisation (used when no actual utilisation data is
157 baseUtil = DynUtil { cpuWeight = 1, memWeight = 1
158 , dskWeight = 1, netWeight = 1 }
160 -- | Sum two utilisation records.
161 addUtil :: DynUtil -> DynUtil -> DynUtil
162 addUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
163 DynUtil (a1+b1) (a2+b2) (a3+b3) (a4+b4)
165 -- | Substracts one utilisation record from another.
166 subUtil :: DynUtil -> DynUtil -> DynUtil
167 subUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
168 DynUtil (a1-b1) (a2-b2) (a3-b3) (a4-b4)
170 -- | The description of an instance placement. It contains the
171 -- instance index, the new primary and secondary node, the move being
172 -- performed and the score of the cluster after the move.
173 type Placement = (Idx, Ndx, Ndx, IMove, Score)
175 -- | An instance move definition.
176 data IMove = Failover -- ^ Failover the instance (f)
177 | ReplacePrimary Ndx -- ^ Replace primary (f, r:np, f)
178 | ReplaceSecondary Ndx -- ^ Replace secondary (r:ns)
179 | ReplaceAndFailover Ndx -- ^ Replace secondary, failover (r:np, f)
180 | FailoverAndReplace Ndx -- ^ Failover, replace secondary (f, r:ns)
181 deriving (Show, Read)
183 -- | Instance disk template type
184 data DiskTemplate = DTDiskless
190 deriving (Show, Read, Eq, Enum, Bounded)
192 -- | Converts a DiskTemplate to String
193 dtToString :: DiskTemplate -> String
194 dtToString DTDiskless = C.dtDiskless
195 dtToString DTFile = C.dtFile
196 dtToString DTSharedFile = C.dtSharedFile
197 dtToString DTPlain = C.dtPlain
198 dtToString DTBlock = C.dtBlock
199 dtToString DTDrbd8 = C.dtDrbd8
201 -- | Converts a DiskTemplate from String
202 dtFromString :: (Monad m) => String -> m DiskTemplate
205 _ | s == C.dtDiskless -> return DTDiskless
206 | s == C.dtFile -> return DTFile
207 | s == C.dtSharedFile -> return DTSharedFile
208 | s == C.dtPlain -> return DTPlain
209 | s == C.dtBlock -> return DTBlock
210 | s == C.dtDrbd8 -> return DTDrbd8
211 | otherwise -> fail $ "Invalid disk template: " ++ s
213 instance JSON.JSON DiskTemplate where
214 showJSON = JSON.showJSON . dtToString
215 readJSON s = case JSON.readJSON s of
216 JSON.Ok s' -> dtFromString s'
217 JSON.Error e -> JSON.Error $
218 "Can't parse disk_template as string: " ++ e
220 -- | Formatted solution output for one move (involved nodes and
222 type MoveJob = ([Ndx], Idx, IMove, [String])
224 -- | Unknown field in table output.
225 unknownField :: String
226 unknownField = "<unknown field>"
228 -- | A list of command elements.
229 type JobSet = [MoveJob]
231 -- | Connection timeout (when using non-file methods).
235 -- | The default timeout for queries (when using non-file methods).
239 -- | Default vcpu-to-pcpu ratio (randomly chosen value).
240 defVcpuRatio :: Double
243 -- | Default max disk usage ratio.
244 defReservedDiskRatio :: Double
245 defReservedDiskRatio = 0
247 -- | Base memory unit.
255 -- | Base vcpus unit.
259 -- | This is similar to the JSON library Result type - /very/ similar,
260 -- but we want to use it in multiple places, so we abstract it into a
261 -- mini-library here.
263 -- The failure value for this monad is simply a string.
267 deriving (Show, Read, Eq)
269 instance Monad Result where
270 (>>=) (Bad x) _ = Bad x
271 (>>=) (Ok x) fn = fn x
275 -- | Simple checker for whether a 'Result' is OK.
276 isOk :: Result a -> Bool
280 -- | Simple checker for whether a 'Result' is a failure.
281 isBad :: Result a -> Bool
284 -- | Converter from Either String to 'Result'
285 eitherToResult :: Either String a -> Result a
286 eitherToResult (Left s) = Bad s
287 eitherToResult (Right v) = Ok v
289 -- | Reason for an operation's falure.
290 data FailMode = FailMem -- ^ Failed due to not enough RAM
291 | FailDisk -- ^ Failed due to not enough disk
292 | FailCPU -- ^ Failed due to not enough CPU capacity
293 | FailN1 -- ^ Failed due to not passing N1 checks
294 | FailTags -- ^ Failed due to tag exclusion
295 deriving (Eq, Enum, Bounded, Show, Read)
297 -- | List with failure statistics.
298 type FailStats = [(FailMode, Int)]
300 -- | Either-like data-type customized for our failure modes.
302 -- The failure values for this monad track the specific allocation
303 -- failures, so this is not a general error-monad (compare with the
304 -- 'Result' data type). One downside is that this type cannot encode a
305 -- generic failure mode, hence 'fail' for this monad is not defined
306 -- and will cause an exception.
307 data OpResult a = OpFail FailMode -- ^ Failed operation
308 | OpGood a -- ^ Success operation
309 deriving (Show, Read)
311 instance Monad OpResult where
312 (OpGood x) >>= fn = fn x
313 (OpFail y) >>= _ = OpFail y
316 -- | Conversion from 'OpResult' to 'Result'.
317 opToResult :: OpResult a -> Result a
318 opToResult (OpFail f) = Bad $ show f
319 opToResult (OpGood v) = Ok v
321 -- | A generic class for items that have updateable names and indices.
322 class Element a where
323 -- | Returns the name of the element
324 nameOf :: a -> String
325 -- | Returns all the known names of the element
326 allNames :: a -> [String]
327 -- | Returns the index of the element
329 -- | Updates the alias of the element
330 setAlias :: a -> String -> a
331 -- | Compute the alias by stripping a given suffix (domain) from
333 computeAlias :: String -> a -> a
334 computeAlias dom e = setAlias e alias
335 where alias = take (length name - length dom) name
337 -- | Updates the index of the element
338 setIdx :: a -> Int -> a
340 -- | The iallocator node-evacuate evac_mode type.
341 data EvacMode = ChangePrimary
344 deriving (Show, Read)