root / Ganeti / HTools / Types.hs @ 06fb841e
History | View | Annotate | Download (6.2 kB)
1 |
{-| Some common types. |
---|---|
2 |
|
3 |
-} |
4 |
|
5 |
{- |
6 |
|
7 |
Copyright (C) 2009, 2010 Google Inc. |
8 |
|
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. |
13 |
|
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. |
18 |
|
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 |
22 |
02110-1301, USA. |
23 |
|
24 |
-} |
25 |
|
26 |
module Ganeti.HTools.Types |
27 |
( Idx |
28 |
, Ndx |
29 |
, NameAssoc |
30 |
, Score |
31 |
, Weight |
32 |
, GroupID |
33 |
, RSpec(..) |
34 |
, DynUtil(..) |
35 |
, zeroUtil |
36 |
, baseUtil |
37 |
, addUtil |
38 |
, subUtil |
39 |
, defVcpuRatio |
40 |
, defReservedDiskRatio |
41 |
, unitMem |
42 |
, unitCpu |
43 |
, unitDsk |
44 |
, unknownField |
45 |
, Placement |
46 |
, IMove(..) |
47 |
, MoveJob |
48 |
, JobSet |
49 |
, Result(..) |
50 |
, isOk |
51 |
, isBad |
52 |
, Element(..) |
53 |
, FailMode(..) |
54 |
, FailStats |
55 |
, OpResult(..) |
56 |
, connTimeout |
57 |
, queryTimeout |
58 |
) where |
59 |
|
60 |
import qualified Data.Map as M |
61 |
|
62 |
-- | The instance index type. |
63 |
type Idx = Int |
64 |
|
65 |
-- | The node index type. |
66 |
type Ndx = Int |
67 |
|
68 |
-- | The type used to hold name-to-idx mappings. |
69 |
type NameAssoc = M.Map String Int |
70 |
|
71 |
-- | A separate name for the cluster score type. |
72 |
type Score = Double |
73 |
|
74 |
-- | A separate name for a weight metric. |
75 |
type Weight = Double |
76 |
|
77 |
-- | The Group UUID type |
78 |
type GroupID = String |
79 |
|
80 |
-- | The resource spec type. |
81 |
data RSpec = RSpec |
82 |
{ rspecCpu :: Int -- ^ Requested VCPUs |
83 |
, rspecMem :: Int -- ^ Requested memory |
84 |
, rspecDsk :: Int -- ^ Requested disk |
85 |
} deriving (Show, Eq) |
86 |
|
87 |
-- | The dynamic resource specs of a machine (i.e. load or load |
88 |
-- capacity, as opposed to size). |
89 |
data DynUtil = DynUtil |
90 |
{ cpuWeight :: Weight -- ^ Standardised CPU usage |
91 |
, memWeight :: Weight -- ^ Standardised memory load |
92 |
, dskWeight :: Weight -- ^ Standardised disk I\/O usage |
93 |
, netWeight :: Weight -- ^ Standardised network usage |
94 |
} deriving (Show, Eq) |
95 |
|
96 |
-- | Initial empty utilisation |
97 |
zeroUtil :: DynUtil |
98 |
zeroUtil = DynUtil { cpuWeight = 0, memWeight = 0 |
99 |
, dskWeight = 0, netWeight = 0 } |
100 |
|
101 |
baseUtil :: DynUtil |
102 |
baseUtil = DynUtil { cpuWeight = 1, memWeight = 1 |
103 |
, dskWeight = 1, netWeight = 1 } |
104 |
|
105 |
addUtil :: DynUtil -> DynUtil -> DynUtil |
106 |
addUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) = |
107 |
DynUtil (a1+b1) (a2+b2) (a3+b3) (a4+b4) |
108 |
|
109 |
subUtil :: DynUtil -> DynUtil -> DynUtil |
110 |
subUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) = |
111 |
DynUtil (a1-b1) (a2-b2) (a3-b3) (a4-b4) |
112 |
|
113 |
-- | The description of an instance placement. It contains the |
114 |
-- instance index, the new primary and secondary node, the move being |
115 |
-- performed and the score of the cluster after the move. |
116 |
type Placement = (Idx, Ndx, Ndx, IMove, Score) |
117 |
|
118 |
-- | An instance move definition |
119 |
data IMove = Failover -- ^ Failover the instance (f) |
120 |
| ReplacePrimary Ndx -- ^ Replace primary (f, r:np, f) |
121 |
| ReplaceSecondary Ndx -- ^ Replace secondary (r:ns) |
122 |
| ReplaceAndFailover Ndx -- ^ Replace secondary, failover (r:np, f) |
123 |
| FailoverAndReplace Ndx -- ^ Failover, replace secondary (f, r:ns) |
124 |
deriving (Show) |
125 |
|
126 |
-- | Formatted solution output for one move (involved nodes and |
127 |
-- commands |
128 |
type MoveJob = ([Ndx], Idx, IMove, [String]) |
129 |
|
130 |
-- | Unknown field in table output |
131 |
unknownField :: String |
132 |
unknownField = "<unknown field>" |
133 |
|
134 |
-- | A list of command elements |
135 |
type JobSet = [MoveJob] |
136 |
|
137 |
-- | Connection timeout (when using non-file methods). |
138 |
connTimeout :: Int |
139 |
connTimeout = 15 |
140 |
|
141 |
-- | The default timeout for queries (when using non-file methods). |
142 |
queryTimeout :: Int |
143 |
queryTimeout = 60 |
144 |
|
145 |
-- | Default vcpu-to-pcpu ratio (randomly chosen value). |
146 |
defVcpuRatio :: Double |
147 |
defVcpuRatio = 64 |
148 |
|
149 |
-- | Default max disk usage ratio. |
150 |
defReservedDiskRatio :: Double |
151 |
defReservedDiskRatio = 0 |
152 |
|
153 |
-- | Base memory unit. |
154 |
unitMem :: Int |
155 |
unitMem = 64 |
156 |
|
157 |
-- | Base disk unit. |
158 |
unitDsk :: Int |
159 |
unitDsk = 256 |
160 |
|
161 |
-- | Base vcpus unit. |
162 |
unitCpu :: Int |
163 |
unitCpu = 1 |
164 |
|
165 |
{-| |
166 |
|
167 |
This is similar to the JSON library Result type - *very* similar, but |
168 |
we want to use it in multiple places, so we abstract it into a |
169 |
mini-library here |
170 |
|
171 |
-} |
172 |
data Result a |
173 |
= Bad String |
174 |
| Ok a |
175 |
deriving (Show) |
176 |
|
177 |
instance Monad Result where |
178 |
(>>=) (Bad x) _ = Bad x |
179 |
(>>=) (Ok x) fn = fn x |
180 |
return = Ok |
181 |
fail = Bad |
182 |
|
183 |
-- | Simple checker for whether Result is OK |
184 |
isOk :: Result a -> Bool |
185 |
isOk (Ok _) = True |
186 |
isOk _ = False |
187 |
|
188 |
-- | Simple checker for whether Result is a failure |
189 |
isBad :: Result a -> Bool |
190 |
isBad = not . isOk |
191 |
|
192 |
-- | Reason for an operation's falure |
193 |
data FailMode = FailMem -- ^ Failed due to not enough RAM |
194 |
| FailDisk -- ^ Failed due to not enough disk |
195 |
| FailCPU -- ^ Failed due to not enough CPU capacity |
196 |
| FailN1 -- ^ Failed due to not passing N1 checks |
197 |
| FailTags -- ^ Failed due to tag exclusion |
198 |
deriving (Eq, Enum, Bounded, Show) |
199 |
|
200 |
-- | List with failure statistics |
201 |
type FailStats = [(FailMode, Int)] |
202 |
|
203 |
-- | Either-like data-type customized for our failure modes |
204 |
data OpResult a = OpFail FailMode -- ^ Failed operation |
205 |
| OpGood a -- ^ Success operation |
206 |
deriving (Show) |
207 |
|
208 |
instance Monad OpResult where |
209 |
(OpGood x) >>= fn = fn x |
210 |
(OpFail y) >>= _ = OpFail y |
211 |
return = OpGood |
212 |
|
213 |
-- | A generic class for items that have updateable names and indices. |
214 |
class Element a where |
215 |
-- | Returns the name of the element |
216 |
nameOf :: a -> String |
217 |
-- | Returns all the known names of the element |
218 |
allNames :: a -> [String] |
219 |
-- | Returns the index of the element |
220 |
idxOf :: a -> Int |
221 |
-- | Updates the alias of the element |
222 |
setAlias :: a -> String -> a |
223 |
-- | Compute the alias by stripping a given suffix (domain) from |
224 |
-- | the name |
225 |
computeAlias :: String -> a -> a |
226 |
computeAlias dom e = setAlias e alias |
227 |
where alias = take (length name - length dom) name |
228 |
name = nameOf e |
229 |
-- | Updates the index of the element |
230 |
setIdx :: a -> Int -> a |