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
39 , defReservedDiskRatio
56 -- | The instance index type.
59 -- | The node index type.
62 -- | The type used to hold name-to-idx mappings.
63 type NameAssoc = [(String, Int)]
65 -- | A separate name for the cluster score type.
68 -- | A separate name for a weight metric.
71 -- | The resource spec type.
73 { rspecCpu :: Int -- ^ Requested VCPUs
74 , rspecMem :: Int -- ^ Requested memory
75 , rspecDsk :: Int -- ^ Requested disk
78 -- | The dynamic resource specs of a machine (i.e. load or load
79 -- capacity, as opposed to size).
80 data DynUtil = DynUtil
81 { cpuWeight :: Weight -- ^ Standardised CPU usage
82 , memWeight :: Weight -- ^ Standardised memory load
83 , dskWeight :: Weight -- ^ Standardised disk I\/O usage
84 , netWeight :: Weight -- ^ Standardised network usage
87 -- | Initial empty utilisation
89 zeroUtil = DynUtil { cpuWeight = 0, memWeight = 0
90 , dskWeight = 0, netWeight = 0 }
93 baseUtil = DynUtil { cpuWeight = 1, memWeight = 1
94 , dskWeight = 1, netWeight = 1 }
96 addUtil :: DynUtil -> DynUtil -> DynUtil
97 addUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
98 DynUtil (a1+b1) (a2+b2) (a3+b3) (a4+b4)
100 subUtil :: DynUtil -> DynUtil -> DynUtil
101 subUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
102 DynUtil (a1-b1) (a2-b2) (a3-b3) (a4-b4)
104 -- | The description of an instance placement. It contains the
105 -- instance index, the new primary and secondary node, the move being
106 -- performed and the score of the cluster after the move.
107 type Placement = (Idx, Ndx, Ndx, IMove, Score)
109 -- | An instance move definition
110 data IMove = Failover -- ^ Failover the instance (f)
111 | ReplacePrimary Ndx -- ^ Replace primary (f, r:np, f)
112 | ReplaceSecondary Ndx -- ^ Replace secondary (r:ns)
113 | ReplaceAndFailover Ndx -- ^ Replace secondary, failover (r:np, f)
114 | FailoverAndReplace Ndx -- ^ Failover, replace secondary (f, r:ns)
117 -- | Formatted solution output for one move (involved nodes and
119 type MoveJob = ([Ndx], Idx, IMove, [String])
121 -- | A list of command elements
122 type JobSet = [MoveJob]
124 -- | Connection timeout (when using non-file methods).
128 -- | The default timeout for queries (when using non-file methods).
132 -- | Default vcpu-to-pcpu ratio (randomly chosen value).
133 defVcpuRatio :: Double
136 -- | Default max disk usage ratio.
137 defReservedDiskRatio :: Double
138 defReservedDiskRatio = 0
140 -- | Base memory unit.
148 -- | Base vcpus unit.
154 This is similar to the JSON library Result type - *very* similar, but
155 we want to use it in multiple places, so we abstract it into a
164 instance Monad Result where
165 (>>=) (Bad x) _ = Bad x
166 (>>=) (Ok x) fn = fn x
170 -- | Reason for an operation's falure
171 data FailMode = FailMem -- ^ Failed due to not enough RAM
172 | FailDisk -- ^ Failed due to not enough disk
173 | FailCPU -- ^ Failed due to not enough CPU capacity
174 | FailN1 -- ^ Failed due to not passing N1 checks
175 | FailTags -- ^ Failed due to tag exclusion
176 deriving (Eq, Enum, Bounded, Show)
178 -- | List with failure statistics
179 type FailStats = [(FailMode, Int)]
181 -- | Either-like data-type customized for our failure modes
182 data OpResult a = OpFail FailMode -- ^ Failed operation
183 | OpGood a -- ^ Success operation
186 instance Monad OpResult where
187 (OpGood x) >>= fn = fn x
188 (OpFail y) >>= _ = OpFail y
191 -- | A generic class for items that have updateable names and indices.
192 class Element a where
193 -- | Returns the name of the element
194 nameOf :: a -> String
195 -- | Returns all the known names of the element
196 allNames :: a -> [String]
197 -- | Returns the index of the element
199 -- | Updates the alias of the element
200 setAlias :: a -> String -> a
201 -- | Compute the alias by stripping a given suffix (domain) from
203 computeAlias :: String -> a -> a
204 computeAlias dom e = setAlias e alias
205 where alias = take (length name - length dom) name
207 -- | Updates the index of the element
208 setIdx :: a -> Int -> a