## root / htools / Ganeti / HTools / Types.hs @ 4cd79ca8

History | View | Annotate | Download (13.2 kB)

1 |
{-# LANGUAGE TemplateHaskell #-} |
---|---|

2 | |

3 |
{-| Some common types. |

4 | |

5 |
-} |

6 | |

7 |
{- |

8 | |

9 |
Copyright (C) 2009, 2010, 2011, 2012 Google Inc. |

10 | |

11 |
This program is free software; you can redistribute it and/or modify |

12 |
it under the terms of the GNU General Public License as published by |

13 |
the Free Software Foundation; either version 2 of the License, or |

14 |
(at your option) any later version. |

15 | |

16 |
This program is distributed in the hope that it will be useful, but |

17 |
WITHOUT ANY WARRANTY; without even the implied warranty of |

18 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |

19 |
General Public License for more details. |

20 | |

21 |
You should have received a copy of the GNU General Public License |

22 |
along with this program; if not, write to the Free Software |

23 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |

24 |
02110-1301, USA. |

25 | |

26 |
-} |

27 | |

28 |
module Ganeti.HTools.Types |

29 |
( Idx |

30 |
, Ndx |

31 |
, Gdx |

32 |
, NameAssoc |

33 |
, Score |

34 |
, Weight |

35 |
, GroupID |

36 |
, defaultGroupID |

37 |
, AllocPolicy(..) |

38 |
, allocPolicyFromRaw |

39 |
, allocPolicyToRaw |

40 |
, InstanceStatus(..) |

41 |
, instanceStatusFromRaw |

42 |
, instanceStatusToRaw |

43 |
, RSpec(..) |

44 |
, AllocInfo(..) |

45 |
, AllocStats |

46 |
, DynUtil(..) |

47 |
, zeroUtil |

48 |
, baseUtil |

49 |
, addUtil |

50 |
, subUtil |

51 |
, defReservedDiskRatio |

52 |
, unitMem |

53 |
, unitCpu |

54 |
, unitDsk |

55 |
, unknownField |

56 |
, Placement |

57 |
, IMove(..) |

58 |
, DiskTemplate(..) |

59 |
, diskTemplateToRaw |

60 |
, diskTemplateFromRaw |

61 |
, MirrorType(..) |

62 |
, templateMirrorType |

63 |
, MoveJob |

64 |
, JobSet |

65 |
, Result(..) |

66 |
, isOk |

67 |
, isBad |

68 |
, eitherToResult |

69 |
, annotateResult |

70 |
, Element(..) |

71 |
, FailMode(..) |

72 |
, FailStats |

73 |
, OpResult(..) |

74 |
, opToResult |

75 |
, EvacMode(..) |

76 |
, ISpec(..) |

77 |
, IPolicy(..) |

78 |
, defIPolicy |

79 |
, rspecFromISpec |

80 |
) where |

81 | |

82 |
import qualified Data.Map as M |

83 |
import Text.JSON (makeObj, readJSON, showJSON) |

84 | |

85 |
import qualified Ganeti.Constants as C |

86 |
import qualified Ganeti.THH as THH |

87 |
import Ganeti.BasicTypes |

88 |
import Ganeti.JSON |

89 | |

90 |
-- | The instance index type. |

91 |
type Idx = Int |

92 | |

93 |
-- | The node index type. |

94 |
type Ndx = Int |

95 | |

96 |
-- | The group index type. |

97 |
type Gdx = Int |

98 | |

99 |
-- | The type used to hold name-to-idx mappings. |

100 |
type NameAssoc = M.Map String Int |

101 | |

102 |
-- | A separate name for the cluster score type. |

103 |
type Score = Double |

104 | |

105 |
-- | A separate name for a weight metric. |

106 |
type Weight = Double |

107 | |

108 |
-- | The Group UUID type. |

109 |
type GroupID = String |

110 | |

111 |
-- | Default group UUID (just a string, not a real UUID). |

112 |
defaultGroupID :: GroupID |

113 |
defaultGroupID = "00000000-0000-0000-0000-000000000000" |

114 | |

115 |
-- | Instance disk template type. |

116 |
$(THH.declareSADT "DiskTemplate" |

117 |
[ ("DTDiskless", 'C.dtDiskless) |

118 |
, ("DTFile", 'C.dtFile) |

119 |
, ("DTSharedFile", 'C.dtSharedFile) |

120 |
, ("DTPlain", 'C.dtPlain) |

121 |
, ("DTBlock", 'C.dtBlock) |

122 |
, ("DTDrbd8", 'C.dtDrbd8) |

123 |
, ("DTRbd", 'C.dtRbd) |

124 |
]) |

125 |
$(THH.makeJSONInstance ''DiskTemplate) |

126 | |

127 |
-- | Mirroring type. |

128 |
data MirrorType = MirrorNone -- ^ No mirroring/movability |

129 |
| MirrorInternal -- ^ DRBD-type mirroring |

130 |
| MirrorExternal -- ^ Shared-storage type mirroring |

131 |
deriving (Eq, Show, Read) |

132 | |

133 |
-- | Correspondence between disk template and mirror type. |

134 |
templateMirrorType :: DiskTemplate -> MirrorType |

135 |
templateMirrorType DTDiskless = MirrorExternal |

136 |
templateMirrorType DTFile = MirrorNone |

137 |
templateMirrorType DTSharedFile = MirrorExternal |

138 |
templateMirrorType DTPlain = MirrorNone |

139 |
templateMirrorType DTBlock = MirrorExternal |

140 |
templateMirrorType DTDrbd8 = MirrorInternal |

141 |
templateMirrorType DTRbd = MirrorExternal |

142 | |

143 |
-- | The Group allocation policy type. |

144 |
-- |

145 |
-- Note that the order of constructors is important as the automatic |

146 |
-- Ord instance will order them in the order they are defined, so when |

147 |
-- changing this data type be careful about the interaction with the |

148 |
-- desired sorting order. |

149 |
$(THH.declareSADT "AllocPolicy" |

150 |
[ ("AllocPreferred", 'C.allocPolicyPreferred) |

151 |
, ("AllocLastResort", 'C.allocPolicyLastResort) |

152 |
, ("AllocUnallocable", 'C.allocPolicyUnallocable) |

153 |
]) |

154 |
$(THH.makeJSONInstance ''AllocPolicy) |

155 | |

156 |
-- | The Instance real state type. |

157 |
$(THH.declareSADT "InstanceStatus" |

158 |
[ ("AdminDown", 'C.inststAdmindown) |

159 |
, ("AdminOffline", 'C.inststAdminoffline) |

160 |
, ("ErrorDown", 'C.inststErrordown) |

161 |
, ("ErrorUp", 'C.inststErrorup) |

162 |
, ("NodeDown", 'C.inststNodedown) |

163 |
, ("NodeOffline", 'C.inststNodeoffline) |

164 |
, ("Running", 'C.inststRunning) |

165 |
, ("WrongNode", 'C.inststWrongnode) |

166 |
]) |

167 |
$(THH.makeJSONInstance ''InstanceStatus) |

168 | |

169 |
-- | The resource spec type. |

170 |
data RSpec = RSpec |

171 |
{ rspecCpu :: Int -- ^ Requested VCPUs |

172 |
, rspecMem :: Int -- ^ Requested memory |

173 |
, rspecDsk :: Int -- ^ Requested disk |

174 |
} deriving (Show, Read, Eq) |

175 | |

176 |
-- | Allocation stats type. This is used instead of 'RSpec' (which was |

177 |
-- used at first), because we need to track more stats. The actual |

178 |
-- data can refer either to allocated, or available, etc. values |

179 |
-- depending on the context. See also |

180 |
-- 'Cluster.computeAllocationDelta'. |

181 |
data AllocInfo = AllocInfo |

182 |
{ allocInfoVCpus :: Int -- ^ VCPUs |

183 |
, allocInfoNCpus :: Double -- ^ Normalised CPUs |

184 |
, allocInfoMem :: Int -- ^ Memory |

185 |
, allocInfoDisk :: Int -- ^ Disk |

186 |
} deriving (Show, Read, Eq) |

187 | |

188 |
-- | Currently used, possibly to allocate, unallocable. |

189 |
type AllocStats = (AllocInfo, AllocInfo, AllocInfo) |

190 | |

191 |
-- | Instance specification type. |

192 |
$(THH.buildObject "ISpec" "iSpec" |

193 |
[ THH.renameField "MemorySize" $ THH.simpleField C.ispecMemSize [t| Int |] |

194 |
, THH.renameField "CpuCount" $ THH.simpleField C.ispecCpuCount [t| Int |] |

195 |
, THH.renameField "DiskSize" $ THH.simpleField C.ispecDiskSize [t| Int |] |

196 |
, THH.renameField "DiskCount" $ THH.simpleField C.ispecDiskCount [t| Int |] |

197 |
, THH.renameField "NicCount" $ THH.simpleField C.ispecNicCount [t| Int |] |

198 |
, THH.renameField "SpindleUse" $ THH.simpleField C.ispecSpindleUse [t| Int |] |

199 |
]) |

200 | |

201 |
-- | The default minimum ispec. |

202 |
defMinISpec :: ISpec |

203 |
defMinISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsMinMemorySize |

204 |
, iSpecCpuCount = C.ipolicyDefaultsMinCpuCount |

205 |
, iSpecDiskSize = C.ipolicyDefaultsMinDiskSize |

206 |
, iSpecDiskCount = C.ipolicyDefaultsMinDiskCount |

207 |
, iSpecNicCount = C.ipolicyDefaultsMinNicCount |

208 |
, iSpecSpindleUse = C.ipolicyDefaultsMinSpindleUse |

209 |
} |

210 | |

211 |
-- | The default standard ispec. |

212 |
defStdISpec :: ISpec |

213 |
defStdISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsStdMemorySize |

214 |
, iSpecCpuCount = C.ipolicyDefaultsStdCpuCount |

215 |
, iSpecDiskSize = C.ipolicyDefaultsStdDiskSize |

216 |
, iSpecDiskCount = C.ipolicyDefaultsStdDiskCount |

217 |
, iSpecNicCount = C.ipolicyDefaultsStdNicCount |

218 |
, iSpecSpindleUse = C.ipolicyDefaultsStdSpindleUse |

219 |
} |

220 | |

221 |
-- | The default max ispec. |

222 |
defMaxISpec :: ISpec |

223 |
defMaxISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsMaxMemorySize |

224 |
, iSpecCpuCount = C.ipolicyDefaultsMaxCpuCount |

225 |
, iSpecDiskSize = C.ipolicyDefaultsMaxDiskSize |

226 |
, iSpecDiskCount = C.ipolicyDefaultsMaxDiskCount |

227 |
, iSpecNicCount = C.ipolicyDefaultsMaxNicCount |

228 |
, iSpecSpindleUse = C.ipolicyDefaultsMaxSpindleUse |

229 |
} |

230 | |

231 |
-- | Instance policy type. |

232 |
$(THH.buildObject "IPolicy" "iPolicy" |

233 |
[ THH.renameField "StdSpec" $ THH.simpleField C.ispecsStd [t| ISpec |] |

234 |
, THH.renameField "MinSpec" $ THH.simpleField C.ispecsMin [t| ISpec |] |

235 |
, THH.renameField "MaxSpec" $ THH.simpleField C.ispecsMax [t| ISpec |] |

236 |
, THH.renameField "DiskTemplates" $ |

237 |
THH.simpleField C.ipolicyDts [t| [DiskTemplate] |] |

238 |
, THH.renameField "VcpuRatio" $ |

239 |
THH.simpleField C.ipolicyVcpuRatio [t| Double |] |

240 |
, THH.renameField "SpindleRatio" $ |

241 |
THH.simpleField C.ipolicySpindleRatio [t| Double |] |

242 |
]) |

243 | |

244 |
-- | Converts an ISpec type to a RSpec one. |

245 |
rspecFromISpec :: ISpec -> RSpec |

246 |
rspecFromISpec ispec = RSpec { rspecCpu = iSpecCpuCount ispec |

247 |
, rspecMem = iSpecMemorySize ispec |

248 |
, rspecDsk = iSpecDiskSize ispec |

249 |
} |

250 | |

251 |
-- | The default instance policy. |

252 |
defIPolicy :: IPolicy |

253 |
defIPolicy = IPolicy { iPolicyStdSpec = defStdISpec |

254 |
, iPolicyMinSpec = defMinISpec |

255 |
, iPolicyMaxSpec = defMaxISpec |

256 |
-- hardcoding here since Constants.hs exports the |

257 |
-- string values, not the actual type; and in |

258 |
-- htools, we are mostly looking at DRBD |

259 |
, iPolicyDiskTemplates = [minBound..maxBound] |

260 |
, iPolicyVcpuRatio = C.ipolicyDefaultsVcpuRatio |

261 |
, iPolicySpindleRatio = C.ipolicyDefaultsSpindleRatio |

262 |
} |

263 | |

264 |
-- | The dynamic resource specs of a machine (i.e. load or load |

265 |
-- capacity, as opposed to size). |

266 |
data DynUtil = DynUtil |

267 |
{ cpuWeight :: Weight -- ^ Standardised CPU usage |

268 |
, memWeight :: Weight -- ^ Standardised memory load |

269 |
, dskWeight :: Weight -- ^ Standardised disk I\/O usage |

270 |
, netWeight :: Weight -- ^ Standardised network usage |

271 |
} deriving (Show, Read, Eq) |

272 | |

273 |
-- | Initial empty utilisation. |

274 |
zeroUtil :: DynUtil |

275 |
zeroUtil = DynUtil { cpuWeight = 0, memWeight = 0 |

276 |
, dskWeight = 0, netWeight = 0 } |

277 | |

278 |
-- | Base utilisation (used when no actual utilisation data is |

279 |
-- supplied). |

280 |
baseUtil :: DynUtil |

281 |
baseUtil = DynUtil { cpuWeight = 1, memWeight = 1 |

282 |
, dskWeight = 1, netWeight = 1 } |

283 | |

284 |
-- | Sum two utilisation records. |

285 |
addUtil :: DynUtil -> DynUtil -> DynUtil |

286 |
addUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) = |

287 |
DynUtil (a1+b1) (a2+b2) (a3+b3) (a4+b4) |

288 | |

289 |
-- | Substracts one utilisation record from another. |

290 |
subUtil :: DynUtil -> DynUtil -> DynUtil |

291 |
subUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) = |

292 |
DynUtil (a1-b1) (a2-b2) (a3-b3) (a4-b4) |

293 | |

294 |
-- | The description of an instance placement. It contains the |

295 |
-- instance index, the new primary and secondary node, the move being |

296 |
-- performed and the score of the cluster after the move. |

297 |
type Placement = (Idx, Ndx, Ndx, IMove, Score) |

298 | |

299 |
-- | An instance move definition. |

300 |
data IMove = Failover -- ^ Failover the instance (f) |

301 |
| FailoverToAny Ndx -- ^ Failover to a random node |

302 |
-- (fa:np), for shared storage |

303 |
| ReplacePrimary Ndx -- ^ Replace primary (f, r:np, f) |

304 |
| ReplaceSecondary Ndx -- ^ Replace secondary (r:ns) |

305 |
| ReplaceAndFailover Ndx -- ^ Replace secondary, failover (r:np, f) |

306 |
| FailoverAndReplace Ndx -- ^ Failover, replace secondary (f, r:ns) |

307 |
deriving (Show, Read) |

308 | |

309 |
-- | Formatted solution output for one move (involved nodes and |

310 |
-- commands. |

311 |
type MoveJob = ([Ndx], Idx, IMove, [String]) |

312 | |

313 |
-- | Unknown field in table output. |

314 |
unknownField :: String |

315 |
unknownField = "<unknown field>" |

316 | |

317 |
-- | A list of command elements. |

318 |
type JobSet = [MoveJob] |

319 | |

320 |
-- | Default max disk usage ratio. |

321 |
defReservedDiskRatio :: Double |

322 |
defReservedDiskRatio = 0 |

323 | |

324 |
-- | Base memory unit. |

325 |
unitMem :: Int |

326 |
unitMem = 64 |

327 | |

328 |
-- | Base disk unit. |

329 |
unitDsk :: Int |

330 |
unitDsk = 256 |

331 | |

332 |
-- | Base vcpus unit. |

333 |
unitCpu :: Int |

334 |
unitCpu = 1 |

335 | |

336 |
-- | Reason for an operation's falure. |

337 |
data FailMode = FailMem -- ^ Failed due to not enough RAM |

338 |
| FailDisk -- ^ Failed due to not enough disk |

339 |
| FailCPU -- ^ Failed due to not enough CPU capacity |

340 |
| FailN1 -- ^ Failed due to not passing N1 checks |

341 |
| FailTags -- ^ Failed due to tag exclusion |

342 |
deriving (Eq, Enum, Bounded, Show, Read) |

343 | |

344 |
-- | List with failure statistics. |

345 |
type FailStats = [(FailMode, Int)] |

346 | |

347 |
-- | Either-like data-type customized for our failure modes. |

348 |
-- |

349 |
-- The failure values for this monad track the specific allocation |

350 |
-- failures, so this is not a general error-monad (compare with the |

351 |
-- 'Result' data type). One downside is that this type cannot encode a |

352 |
-- generic failure mode, hence 'fail' for this monad is not defined |

353 |
-- and will cause an exception. |

354 |
data OpResult a = OpFail FailMode -- ^ Failed operation |

355 |
| OpGood a -- ^ Success operation |

356 |
deriving (Show, Read) |

357 | |

358 |
instance Monad OpResult where |

359 |
(OpGood x) >>= fn = fn x |

360 |
(OpFail y) >>= _ = OpFail y |

361 |
return = OpGood |

362 | |

363 |
-- | Conversion from 'OpResult' to 'Result'. |

364 |
opToResult :: OpResult a -> Result a |

365 |
opToResult (OpFail f) = Bad $ show f |

366 |
opToResult (OpGood v) = Ok v |

367 | |

368 |
-- | A generic class for items that have updateable names and indices. |

369 |
class Element a where |

370 |
-- | Returns the name of the element |

371 |
nameOf :: a -> String |

372 |
-- | Returns all the known names of the element |

373 |
allNames :: a -> [String] |

374 |
-- | Returns the index of the element |

375 |
idxOf :: a -> Int |

376 |
-- | Updates the alias of the element |

377 |
setAlias :: a -> String -> a |

378 |
-- | Compute the alias by stripping a given suffix (domain) from |

379 |
-- the name |

380 |
computeAlias :: String -> a -> a |

381 |
computeAlias dom e = setAlias e alias |

382 |
where alias = take (length name - length dom) name |

383 |
name = nameOf e |

384 |
-- | Updates the index of the element |

385 |
setIdx :: a -> Int -> a |

386 | |

387 |
-- | The iallocator node-evacuate evac_mode type. |

388 |
$(THH.declareSADT "EvacMode" |

389 |
[ ("ChangePrimary", 'C.iallocatorNevacPri) |

390 |
, ("ChangeSecondary", 'C.iallocatorNevacSec) |

391 |
, ("ChangeAll", 'C.iallocatorNevacAll) |

392 |
]) |

393 |
$(THH.makeJSONInstance ''EvacMode) |