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
57 -- | The instance index type.
60 -- | The node index type.
63 -- | The type used to hold name-to-idx mappings.
64 type NameAssoc = [(String, Int)]
66 -- | A separate name for the cluster score type.
69 -- | A separate name for a weight metric.
72 -- | The resource spec type.
74 { rspecCpu :: Int -- ^ Requested VCPUs
75 , rspecMem :: Int -- ^ Requested memory
76 , rspecDsk :: Int -- ^ Requested disk
79 -- | The dynamic resource specs of a machine (i.e. load or load
80 -- capacity, as opposed to size).
81 data DynUtil = DynUtil
82 { cpuWeight :: Weight -- ^ Standardised CPU usage
83 , memWeight :: Weight -- ^ Standardised memory load
84 , dskWeight :: Weight -- ^ Standardised disk I\/O usage
85 , netWeight :: Weight -- ^ Standardised network usage
88 -- | Initial empty utilisation
90 zeroUtil = DynUtil { cpuWeight = 0, memWeight = 0
91 , dskWeight = 0, netWeight = 0 }
94 baseUtil = DynUtil { cpuWeight = 1, memWeight = 1
95 , dskWeight = 1, netWeight = 1 }
97 addUtil :: DynUtil -> DynUtil -> DynUtil
98 addUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
99 DynUtil (a1+b1) (a2+b2) (a3+b3) (a4+b4)
101 subUtil :: DynUtil -> DynUtil -> DynUtil
102 subUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
103 DynUtil (a1-b1) (a2-b2) (a3-b3) (a4-b4)
105 -- | The description of an instance placement. It contains the
106 -- instance index, the new primary and secondary node, the move being
107 -- performed and the score of the cluster after the move.
108 type Placement = (Idx, Ndx, Ndx, IMove, Score)
110 -- | An instance move definition
111 data IMove = Failover -- ^ Failover the instance (f)
112 | ReplacePrimary Ndx -- ^ Replace primary (f, r:np, f)
113 | ReplaceSecondary Ndx -- ^ Replace secondary (r:ns)
114 | ReplaceAndFailover Ndx -- ^ Replace secondary, failover (r:np, f)
115 | FailoverAndReplace Ndx -- ^ Failover, replace secondary (f, r:ns)
118 -- | Formatted solution output for one move (involved nodes and
120 type MoveJob = ([Ndx], Idx, IMove, [String])
122 -- | Unknown field in table output
123 unknownField :: String
124 unknownField = "<unknown field>"
126 -- | A list of command elements
127 type JobSet = [MoveJob]
129 -- | Connection timeout (when using non-file methods).
133 -- | The default timeout for queries (when using non-file methods).
137 -- | Default vcpu-to-pcpu ratio (randomly chosen value).
138 defVcpuRatio :: Double
141 -- | Default max disk usage ratio.
142 defReservedDiskRatio :: Double
143 defReservedDiskRatio = 0
145 -- | Base memory unit.
153 -- | Base vcpus unit.
159 This is similar to the JSON library Result type - *very* similar, but
160 we want to use it in multiple places, so we abstract it into a
169 instance Monad Result where
170 (>>=) (Bad x) _ = Bad x
171 (>>=) (Ok x) fn = fn x
175 -- | Reason for an operation's falure
176 data FailMode = FailMem -- ^ Failed due to not enough RAM
177 | FailDisk -- ^ Failed due to not enough disk
178 | FailCPU -- ^ Failed due to not enough CPU capacity
179 | FailN1 -- ^ Failed due to not passing N1 checks
180 | FailTags -- ^ Failed due to tag exclusion
181 deriving (Eq, Enum, Bounded, Show)
183 -- | List with failure statistics
184 type FailStats = [(FailMode, Int)]
186 -- | Either-like data-type customized for our failure modes
187 data OpResult a = OpFail FailMode -- ^ Failed operation
188 | OpGood a -- ^ Success operation
191 instance Monad OpResult where
192 (OpGood x) >>= fn = fn x
193 (OpFail y) >>= _ = OpFail y
196 -- | A generic class for items that have updateable names and indices.
197 class Element a where
198 -- | Returns the name of the element
199 nameOf :: a -> String
200 -- | Returns all the known names of the element
201 allNames :: a -> [String]
202 -- | Returns the index of the element
204 -- | Updates the alias of the element
205 setAlias :: a -> String -> a
206 -- | Compute the alias by stripping a given suffix (domain) from
208 computeAlias :: String -> a -> a
209 computeAlias dom e = setAlias e alias
210 where alias = take (length name - length dom) name
212 -- | Updates the index of the element
213 setIdx :: a -> Int -> a