Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (12.1 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
  , MoveJob
62
  , JobSet
63
  , Result(..)
64
  , isOk
65
  , isBad
66
  , eitherToResult
67
  , annotateResult
68
  , Element(..)
69
  , FailMode(..)
70
  , FailStats
71
  , OpResult(..)
72
  , opToResult
73
  , connTimeout
74
  , queryTimeout
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.HTools.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
-- | The Group allocation policy type.
128
--
129
-- Note that the order of constructors is important as the automatic
130
-- Ord instance will order them in the order they are defined, so when
131
-- changing this data type be careful about the interaction with the
132
-- desired sorting order.
133
$(THH.declareSADT "AllocPolicy"
134
       [ ("AllocPreferred",   'C.allocPolicyPreferred)
135
       , ("AllocLastResort",  'C.allocPolicyLastResort)
136
       , ("AllocUnallocable", 'C.allocPolicyUnallocable)
137
       ])
138
$(THH.makeJSONInstance ''AllocPolicy)
139

    
140
-- | The Instance real state type.
141
$(THH.declareSADT "InstanceStatus"
142
       [ ("AdminDown", 'C.inststAdmindown)
143
       , ("AdminOffline", 'C.inststAdminoffline)
144
       , ("ErrorDown", 'C.inststErrordown)
145
       , ("ErrorUp", 'C.inststErrorup)
146
       , ("NodeDown", 'C.inststNodedown)
147
       , ("NodeOffline", 'C.inststNodeoffline)
148
       , ("Running", 'C.inststRunning)
149
       , ("WrongNode", 'C.inststWrongnode)
150
       ])
151
$(THH.makeJSONInstance ''InstanceStatus)
152

    
153
-- | The resource spec type.
154
data RSpec = RSpec
155
  { rspecCpu  :: Int  -- ^ Requested VCPUs
156
  , rspecMem  :: Int  -- ^ Requested memory
157
  , rspecDsk  :: Int  -- ^ Requested disk
158
  } deriving (Show, Read, Eq)
159

    
160
-- | Allocation stats type. This is used instead of 'RSpec' (which was
161
-- used at first), because we need to track more stats. The actual
162
-- data can refer either to allocated, or available, etc. values
163
-- depending on the context. See also
164
-- 'Cluster.computeAllocationDelta'.
165
data AllocInfo = AllocInfo
166
  { allocInfoVCpus :: Int    -- ^ VCPUs
167
  , allocInfoNCpus :: Double -- ^ Normalised CPUs
168
  , allocInfoMem   :: Int    -- ^ Memory
169
  , allocInfoDisk  :: Int    -- ^ Disk
170
  } deriving (Show, Read, Eq)
171

    
172
-- | Currently used, possibly to allocate, unallocable.
173
type AllocStats = (AllocInfo, AllocInfo, AllocInfo)
174

    
175
-- | Instance specification type.
176
$(THH.buildObject "ISpec" "iSpec"
177
  [ THH.renameField "MemorySize" $ THH.simpleField C.ispecMemSize   [t| Int |]
178
  , THH.renameField "CpuCount"   $ THH.simpleField C.ispecCpuCount  [t| Int |]
179
  , THH.renameField "DiskSize"   $ THH.simpleField C.ispecDiskSize  [t| Int |]
180
  , THH.renameField "DiskCount"  $ THH.simpleField C.ispecDiskCount [t| Int |]
181
  , THH.renameField "NicCount"   $ THH.simpleField C.ispecNicCount  [t| Int |]
182
  ])
183

    
184
-- | The default minimum ispec.
185
defMinISpec :: ISpec
186
defMinISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsMinMemorySize
187
                    , iSpecCpuCount   = C.ipolicyDefaultsMinCpuCount
188
                    , iSpecDiskSize   = C.ipolicyDefaultsMinDiskSize
189
                    , iSpecDiskCount  = C.ipolicyDefaultsMinDiskCount
190
                    , iSpecNicCount   = C.ipolicyDefaultsMinNicCount
191
                    }
192

    
193
-- | The default standard ispec.
194
defStdISpec :: ISpec
195
defStdISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsStdMemorySize
196
                    , iSpecCpuCount   = C.ipolicyDefaultsStdCpuCount
197
                    , iSpecDiskSize   = C.ipolicyDefaultsStdDiskSize
198
                    , iSpecDiskCount  = C.ipolicyDefaultsStdDiskCount
199
                    , iSpecNicCount   = C.ipolicyDefaultsStdNicCount
200
                    }
201

    
202
-- | The default max ispec.
203
defMaxISpec :: ISpec
204
defMaxISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsMaxMemorySize
205
                    , iSpecCpuCount   = C.ipolicyDefaultsMaxCpuCount
206
                    , iSpecDiskSize   = C.ipolicyDefaultsMaxDiskSize
207
                    , iSpecDiskCount  = C.ipolicyDefaultsMaxDiskCount
208
                    , iSpecNicCount   = C.ipolicyDefaultsMaxNicCount
209
                    }
210

    
211
-- | Instance policy type.
212
$(THH.buildObject "IPolicy" "iPolicy"
213
  [ THH.renameField "StdSpec" $ THH.simpleField C.ispecsStd [t| ISpec |]
214
  , THH.renameField "MinSpec" $ THH.simpleField C.ispecsMin [t| ISpec |]
215
  , THH.renameField "MaxSpec" $ THH.simpleField C.ispecsMax [t| ISpec |]
216
  , THH.renameField "DiskTemplates" $
217
      THH.simpleField C.ipolicyDts [t| [DiskTemplate] |]
218
  , THH.renameField "VcpuRatio" $
219
      THH.simpleField C.ipolicyVcpuRatio [t| Double |]
220
  ])
221

    
222
-- | Converts an ISpec type to a RSpec one.
223
rspecFromISpec :: ISpec -> RSpec
224
rspecFromISpec ispec = RSpec { rspecCpu = iSpecCpuCount ispec
225
                             , rspecMem = iSpecMemorySize ispec
226
                             , rspecDsk = iSpecDiskSize ispec
227
                             }
228

    
229
-- | The default instance policy.
230
defIPolicy :: IPolicy
231
defIPolicy = IPolicy { iPolicyStdSpec = defStdISpec
232
                     , iPolicyMinSpec = defMinISpec
233
                     , iPolicyMaxSpec = defMaxISpec
234
                     -- hardcoding here since Constants.hs exports the
235
                     -- string values, not the actual type; and in
236
                     -- htools, we are mostly looking at DRBD
237
                     , iPolicyDiskTemplates = [DTDrbd8, DTPlain]
238
                     , iPolicyVcpuRatio = C.ipolicyDefaultsVcpuRatio
239
                     }
240

    
241
-- | The dynamic resource specs of a machine (i.e. load or load
242
-- capacity, as opposed to size).
243
data DynUtil = DynUtil
244
  { cpuWeight :: Weight -- ^ Standardised CPU usage
245
  , memWeight :: Weight -- ^ Standardised memory load
246
  , dskWeight :: Weight -- ^ Standardised disk I\/O usage
247
  , netWeight :: Weight -- ^ Standardised network usage
248
  } deriving (Show, Read, Eq)
249

    
250
-- | Initial empty utilisation.
251
zeroUtil :: DynUtil
252
zeroUtil = DynUtil { cpuWeight = 0, memWeight = 0
253
                   , dskWeight = 0, netWeight = 0 }
254

    
255
-- | Base utilisation (used when no actual utilisation data is
256
-- supplied).
257
baseUtil :: DynUtil
258
baseUtil = DynUtil { cpuWeight = 1, memWeight = 1
259
                   , dskWeight = 1, netWeight = 1 }
260

    
261
-- | Sum two utilisation records.
262
addUtil :: DynUtil -> DynUtil -> DynUtil
263
addUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
264
  DynUtil (a1+b1) (a2+b2) (a3+b3) (a4+b4)
265

    
266
-- | Substracts one utilisation record from another.
267
subUtil :: DynUtil -> DynUtil -> DynUtil
268
subUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
269
  DynUtil (a1-b1) (a2-b2) (a3-b3) (a4-b4)
270

    
271
-- | The description of an instance placement. It contains the
272
-- instance index, the new primary and secondary node, the move being
273
-- performed and the score of the cluster after the move.
274
type Placement = (Idx, Ndx, Ndx, IMove, Score)
275

    
276
-- | An instance move definition.
277
data IMove = Failover                -- ^ Failover the instance (f)
278
           | ReplacePrimary Ndx      -- ^ Replace primary (f, r:np, f)
279
           | ReplaceSecondary Ndx    -- ^ Replace secondary (r:ns)
280
           | ReplaceAndFailover Ndx  -- ^ Replace secondary, failover (r:np, f)
281
           | FailoverAndReplace Ndx  -- ^ Failover, replace secondary (f, r:ns)
282
             deriving (Show, Read)
283

    
284
-- | Formatted solution output for one move (involved nodes and
285
-- commands.
286
type MoveJob = ([Ndx], Idx, IMove, [String])
287

    
288
-- | Unknown field in table output.
289
unknownField :: String
290
unknownField = "<unknown field>"
291

    
292
-- | A list of command elements.
293
type JobSet = [MoveJob]
294

    
295
-- | Connection timeout (when using non-file methods).
296
connTimeout :: Int
297
connTimeout = 15
298

    
299
-- | The default timeout for queries (when using non-file methods).
300
queryTimeout :: Int
301
queryTimeout = 60
302

    
303
-- | Default max disk usage ratio.
304
defReservedDiskRatio :: Double
305
defReservedDiskRatio = 0
306

    
307
-- | Base memory unit.
308
unitMem :: Int
309
unitMem = 64
310

    
311
-- | Base disk unit.
312
unitDsk :: Int
313
unitDsk = 256
314

    
315
-- | Base vcpus unit.
316
unitCpu :: Int
317
unitCpu = 1
318

    
319
-- | Reason for an operation's falure.
320
data FailMode = FailMem  -- ^ Failed due to not enough RAM
321
              | FailDisk -- ^ Failed due to not enough disk
322
              | FailCPU  -- ^ Failed due to not enough CPU capacity
323
              | FailN1   -- ^ Failed due to not passing N1 checks
324
              | FailTags -- ^ Failed due to tag exclusion
325
                deriving (Eq, Enum, Bounded, Show, Read)
326

    
327
-- | List with failure statistics.
328
type FailStats = [(FailMode, Int)]
329

    
330
-- | Either-like data-type customized for our failure modes.
331
--
332
-- The failure values for this monad track the specific allocation
333
-- failures, so this is not a general error-monad (compare with the
334
-- 'Result' data type). One downside is that this type cannot encode a
335
-- generic failure mode, hence 'fail' for this monad is not defined
336
-- and will cause an exception.
337
data OpResult a = OpFail FailMode -- ^ Failed operation
338
                | OpGood a        -- ^ Success operation
339
                  deriving (Show, Read)
340

    
341
instance Monad OpResult where
342
  (OpGood x) >>= fn = fn x
343
  (OpFail y) >>= _ = OpFail y
344
  return = OpGood
345

    
346
-- | Conversion from 'OpResult' to 'Result'.
347
opToResult :: OpResult a -> Result a
348
opToResult (OpFail f) = Bad $ show f
349
opToResult (OpGood v) = Ok v
350

    
351
-- | A generic class for items that have updateable names and indices.
352
class Element a where
353
  -- | Returns the name of the element
354
  nameOf  :: a -> String
355
  -- | Returns all the known names of the element
356
  allNames :: a -> [String]
357
  -- | Returns the index of the element
358
  idxOf   :: a -> Int
359
  -- | Updates the alias of the element
360
  setAlias :: a -> String -> a
361
  -- | Compute the alias by stripping a given suffix (domain) from
362
  -- the name
363
  computeAlias :: String -> a -> a
364
  computeAlias dom e = setAlias e alias
365
    where alias = take (length name - length dom) name
366
          name = nameOf e
367
  -- | Updates the index of the element
368
  setIdx  :: a -> Int -> a
369

    
370
-- | The iallocator node-evacuate evac_mode type.
371
$(THH.declareSADT "EvacMode"
372
       [ ("ChangePrimary",   'C.iallocatorNevacPri)
373
       , ("ChangeSecondary", 'C.iallocatorNevacSec)
374
       , ("ChangeAll",       'C.iallocatorNevacAll)
375
       ])
376
$(THH.makeJSONInstance ''EvacMode)