Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Types.hs @ bebe7a73

History | View | Annotate | Download (13 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
  , connTimeout
76
  , queryTimeout
77
  , EvacMode(..)
78
  , ISpec(..)
79
  , IPolicy(..)
80
  , defIPolicy
81
  , rspecFromISpec
82
  ) where
83

    
84
import qualified Data.Map as M
85
import Text.JSON (makeObj, readJSON, showJSON)
86

    
87
import qualified Ganeti.Constants as C
88
import qualified Ganeti.THH as THH
89
import Ganeti.BasicTypes
90
import Ganeti.HTools.JSON
91

    
92
-- | The instance index type.
93
type Idx = Int
94

    
95
-- | The node index type.
96
type Ndx = Int
97

    
98
-- | The group index type.
99
type Gdx = Int
100

    
101
-- | The type used to hold name-to-idx mappings.
102
type NameAssoc = M.Map String Int
103

    
104
-- | A separate name for the cluster score type.
105
type Score = Double
106

    
107
-- | A separate name for a weight metric.
108
type Weight = Double
109

    
110
-- | The Group UUID type.
111
type GroupID = String
112

    
113
-- | Default group UUID (just a string, not a real UUID).
114
defaultGroupID :: GroupID
115
defaultGroupID = "00000000-0000-0000-0000-000000000000"
116

    
117
-- | Instance disk template type.
118
$(THH.declareSADT "DiskTemplate"
119
       [ ("DTDiskless",   'C.dtDiskless)
120
       , ("DTFile",       'C.dtFile)
121
       , ("DTSharedFile", 'C.dtSharedFile)
122
       , ("DTPlain",      'C.dtPlain)
123
       , ("DTBlock",      'C.dtBlock)
124
       , ("DTDrbd8",      'C.dtDrbd8)
125
       , ("DTRbd",        'C.dtRbd)
126
       ])
127
$(THH.makeJSONInstance ''DiskTemplate)
128

    
129
-- | Mirroring type.
130
data MirrorType = MirrorNone     -- ^ No mirroring/movability
131
                | MirrorInternal -- ^ DRBD-type mirroring
132
                | MirrorExternal -- ^ Shared-storage type mirroring
133
                  deriving (Eq, Show, Read)
134

    
135
-- | Correspondence between disk template and mirror type.
136
templateMirrorType :: DiskTemplate -> MirrorType
137
templateMirrorType DTDiskless   = MirrorExternal
138
templateMirrorType DTFile       = MirrorNone
139
templateMirrorType DTSharedFile = MirrorExternal
140
templateMirrorType DTPlain      = MirrorNone
141
templateMirrorType DTBlock      = MirrorExternal
142
templateMirrorType DTDrbd8      = MirrorInternal
143
templateMirrorType DTRbd        = MirrorExternal
144

    
145
-- | The Group allocation policy type.
146
--
147
-- Note that the order of constructors is important as the automatic
148
-- Ord instance will order them in the order they are defined, so when
149
-- changing this data type be careful about the interaction with the
150
-- desired sorting order.
151
$(THH.declareSADT "AllocPolicy"
152
       [ ("AllocPreferred",   'C.allocPolicyPreferred)
153
       , ("AllocLastResort",  'C.allocPolicyLastResort)
154
       , ("AllocUnallocable", 'C.allocPolicyUnallocable)
155
       ])
156
$(THH.makeJSONInstance ''AllocPolicy)
157

    
158
-- | The Instance real state type.
159
$(THH.declareSADT "InstanceStatus"
160
       [ ("AdminDown", 'C.inststAdmindown)
161
       , ("AdminOffline", 'C.inststAdminoffline)
162
       , ("ErrorDown", 'C.inststErrordown)
163
       , ("ErrorUp", 'C.inststErrorup)
164
       , ("NodeDown", 'C.inststNodedown)
165
       , ("NodeOffline", 'C.inststNodeoffline)
166
       , ("Running", 'C.inststRunning)
167
       , ("WrongNode", 'C.inststWrongnode)
168
       ])
169
$(THH.makeJSONInstance ''InstanceStatus)
170

    
171
-- | The resource spec type.
172
data RSpec = RSpec
173
  { rspecCpu  :: Int  -- ^ Requested VCPUs
174
  , rspecMem  :: Int  -- ^ Requested memory
175
  , rspecDsk  :: Int  -- ^ Requested disk
176
  } deriving (Show, Read, Eq)
177

    
178
-- | Allocation stats type. This is used instead of 'RSpec' (which was
179
-- used at first), because we need to track more stats. The actual
180
-- data can refer either to allocated, or available, etc. values
181
-- depending on the context. See also
182
-- 'Cluster.computeAllocationDelta'.
183
data AllocInfo = AllocInfo
184
  { allocInfoVCpus :: Int    -- ^ VCPUs
185
  , allocInfoNCpus :: Double -- ^ Normalised CPUs
186
  , allocInfoMem   :: Int    -- ^ Memory
187
  , allocInfoDisk  :: Int    -- ^ Disk
188
  } deriving (Show, Read, Eq)
189

    
190
-- | Currently used, possibly to allocate, unallocable.
191
type AllocStats = (AllocInfo, AllocInfo, AllocInfo)
192

    
193
-- | Instance specification type.
194
$(THH.buildObject "ISpec" "iSpec"
195
  [ THH.renameField "MemorySize" $ THH.simpleField C.ispecMemSize   [t| Int |]
196
  , THH.renameField "CpuCount"   $ THH.simpleField C.ispecCpuCount  [t| Int |]
197
  , THH.renameField "DiskSize"   $ THH.simpleField C.ispecDiskSize  [t| Int |]
198
  , THH.renameField "DiskCount"  $ THH.simpleField C.ispecDiskCount [t| Int |]
199
  , THH.renameField "NicCount"   $ THH.simpleField C.ispecNicCount  [t| Int |]
200
  ])
201

    
202
-- | The default minimum ispec.
203
defMinISpec :: ISpec
204
defMinISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsMinMemorySize
205
                    , iSpecCpuCount   = C.ipolicyDefaultsMinCpuCount
206
                    , iSpecDiskSize   = C.ipolicyDefaultsMinDiskSize
207
                    , iSpecDiskCount  = C.ipolicyDefaultsMinDiskCount
208
                    , iSpecNicCount   = C.ipolicyDefaultsMinNicCount
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
                    }
219

    
220
-- | The default max ispec.
221
defMaxISpec :: ISpec
222
defMaxISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsMaxMemorySize
223
                    , iSpecCpuCount   = C.ipolicyDefaultsMaxCpuCount
224
                    , iSpecDiskSize   = C.ipolicyDefaultsMaxDiskSize
225
                    , iSpecDiskCount  = C.ipolicyDefaultsMaxDiskCount
226
                    , iSpecNicCount   = C.ipolicyDefaultsMaxNicCount
227
                    }
228

    
229
-- | Instance policy type.
230
$(THH.buildObject "IPolicy" "iPolicy"
231
  [ THH.renameField "StdSpec" $ THH.simpleField C.ispecsStd [t| ISpec |]
232
  , THH.renameField "MinSpec" $ THH.simpleField C.ispecsMin [t| ISpec |]
233
  , THH.renameField "MaxSpec" $ THH.simpleField C.ispecsMax [t| ISpec |]
234
  , THH.renameField "DiskTemplates" $
235
      THH.simpleField C.ipolicyDts [t| [DiskTemplate] |]
236
  , THH.renameField "VcpuRatio" $
237
      THH.simpleField C.ipolicyVcpuRatio [t| Double |]
238
  , THH.renameField "SpindleRatio" $
239
      THH.simpleField C.ipolicySpindleRatio [t| Double |]
240
  ])
241

    
242
-- | Converts an ISpec type to a RSpec one.
243
rspecFromISpec :: ISpec -> RSpec
244
rspecFromISpec ispec = RSpec { rspecCpu = iSpecCpuCount ispec
245
                             , rspecMem = iSpecMemorySize ispec
246
                             , rspecDsk = iSpecDiskSize ispec
247
                             }
248

    
249
-- | The default instance policy.
250
defIPolicy :: IPolicy
251
defIPolicy = IPolicy { iPolicyStdSpec = defStdISpec
252
                     , iPolicyMinSpec = defMinISpec
253
                     , iPolicyMaxSpec = defMaxISpec
254
                     -- hardcoding here since Constants.hs exports the
255
                     -- string values, not the actual type; and in
256
                     -- htools, we are mostly looking at DRBD
257
                     , iPolicyDiskTemplates = [minBound..maxBound]
258
                     , iPolicyVcpuRatio = C.ipolicyDefaultsVcpuRatio
259
                     , iPolicySpindleRatio = C.ipolicyDefaultsSpindleRatio
260
                     }
261

    
262
-- | The dynamic resource specs of a machine (i.e. load or load
263
-- capacity, as opposed to size).
264
data DynUtil = DynUtil
265
  { cpuWeight :: Weight -- ^ Standardised CPU usage
266
  , memWeight :: Weight -- ^ Standardised memory load
267
  , dskWeight :: Weight -- ^ Standardised disk I\/O usage
268
  , netWeight :: Weight -- ^ Standardised network usage
269
  } deriving (Show, Read, Eq)
270

    
271
-- | Initial empty utilisation.
272
zeroUtil :: DynUtil
273
zeroUtil = DynUtil { cpuWeight = 0, memWeight = 0
274
                   , dskWeight = 0, netWeight = 0 }
275

    
276
-- | Base utilisation (used when no actual utilisation data is
277
-- supplied).
278
baseUtil :: DynUtil
279
baseUtil = DynUtil { cpuWeight = 1, memWeight = 1
280
                   , dskWeight = 1, netWeight = 1 }
281

    
282
-- | Sum two utilisation records.
283
addUtil :: DynUtil -> DynUtil -> DynUtil
284
addUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
285
  DynUtil (a1+b1) (a2+b2) (a3+b3) (a4+b4)
286

    
287
-- | Substracts one utilisation record from another.
288
subUtil :: DynUtil -> DynUtil -> DynUtil
289
subUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
290
  DynUtil (a1-b1) (a2-b2) (a3-b3) (a4-b4)
291

    
292
-- | The description of an instance placement. It contains the
293
-- instance index, the new primary and secondary node, the move being
294
-- performed and the score of the cluster after the move.
295
type Placement = (Idx, Ndx, Ndx, IMove, Score)
296

    
297
-- | An instance move definition.
298
data IMove = Failover                -- ^ Failover the instance (f)
299
           | ReplacePrimary Ndx      -- ^ Replace primary (f, r:np, f)
300
           | ReplaceSecondary Ndx    -- ^ Replace secondary (r:ns)
301
           | ReplaceAndFailover Ndx  -- ^ Replace secondary, failover (r:np, f)
302
           | FailoverAndReplace Ndx  -- ^ Failover, replace secondary (f, r:ns)
303
             deriving (Show, Read)
304

    
305
-- | Formatted solution output for one move (involved nodes and
306
-- commands.
307
type MoveJob = ([Ndx], Idx, IMove, [String])
308

    
309
-- | Unknown field in table output.
310
unknownField :: String
311
unknownField = "<unknown field>"
312

    
313
-- | A list of command elements.
314
type JobSet = [MoveJob]
315

    
316
-- | Connection timeout (when using non-file methods).
317
connTimeout :: Int
318
connTimeout = 15
319

    
320
-- | The default timeout for queries (when using non-file methods).
321
queryTimeout :: Int
322
queryTimeout = 60
323

    
324
-- | Default max disk usage ratio.
325
defReservedDiskRatio :: Double
326
defReservedDiskRatio = 0
327

    
328
-- | Base memory unit.
329
unitMem :: Int
330
unitMem = 64
331

    
332
-- | Base disk unit.
333
unitDsk :: Int
334
unitDsk = 256
335

    
336
-- | Base vcpus unit.
337
unitCpu :: Int
338
unitCpu = 1
339

    
340
-- | Reason for an operation's falure.
341
data FailMode = FailMem  -- ^ Failed due to not enough RAM
342
              | FailDisk -- ^ Failed due to not enough disk
343
              | FailCPU  -- ^ Failed due to not enough CPU capacity
344
              | FailN1   -- ^ Failed due to not passing N1 checks
345
              | FailTags -- ^ Failed due to tag exclusion
346
                deriving (Eq, Enum, Bounded, Show, Read)
347

    
348
-- | List with failure statistics.
349
type FailStats = [(FailMode, Int)]
350

    
351
-- | Either-like data-type customized for our failure modes.
352
--
353
-- The failure values for this monad track the specific allocation
354
-- failures, so this is not a general error-monad (compare with the
355
-- 'Result' data type). One downside is that this type cannot encode a
356
-- generic failure mode, hence 'fail' for this monad is not defined
357
-- and will cause an exception.
358
data OpResult a = OpFail FailMode -- ^ Failed operation
359
                | OpGood a        -- ^ Success operation
360
                  deriving (Show, Read)
361

    
362
instance Monad OpResult where
363
  (OpGood x) >>= fn = fn x
364
  (OpFail y) >>= _ = OpFail y
365
  return = OpGood
366

    
367
-- | Conversion from 'OpResult' to 'Result'.
368
opToResult :: OpResult a -> Result a
369
opToResult (OpFail f) = Bad $ show f
370
opToResult (OpGood v) = Ok v
371

    
372
-- | A generic class for items that have updateable names and indices.
373
class Element a where
374
  -- | Returns the name of the element
375
  nameOf  :: a -> String
376
  -- | Returns all the known names of the element
377
  allNames :: a -> [String]
378
  -- | Returns the index of the element
379
  idxOf   :: a -> Int
380
  -- | Updates the alias of the element
381
  setAlias :: a -> String -> a
382
  -- | Compute the alias by stripping a given suffix (domain) from
383
  -- the name
384
  computeAlias :: String -> a -> a
385
  computeAlias dom e = setAlias e alias
386
    where alias = take (length name - length dom) name
387
          name = nameOf e
388
  -- | Updates the index of the element
389
  setIdx  :: a -> Int -> a
390

    
391
-- | The iallocator node-evacuate evac_mode type.
392
$(THH.declareSADT "EvacMode"
393
       [ ("ChangePrimary",   'C.iallocatorNevacPri)
394
       , ("ChangeSecondary", 'C.iallocatorNevacSec)
395
       , ("ChangeAll",       'C.iallocatorNevacAll)
396
       ])
397
$(THH.makeJSONInstance ''EvacMode)