Statistics
| Branch: | Tag: | Revision:

root / htools / Ganeti / HTools / Types.hs @ 89c758c6

History | View | Annotate | Download (11 kB)

1
{-# LANGUAGE TemplateHaskell #-}
2

    
3
{-| Some common types.
4

    
5
-}
6

    
7
{-
8

    
9
Copyright (C) 2009, 2010, 2011 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
  , DynUtil(..)
45
  , zeroUtil
46
  , baseUtil
47
  , addUtil
48
  , subUtil
49
  , defVcpuRatio
50
  , defReservedDiskRatio
51
  , unitMem
52
  , unitCpu
53
  , unitDsk
54
  , unknownField
55
  , Placement
56
  , IMove(..)
57
  , DiskTemplate(..)
58
  , diskTemplateToRaw
59
  , diskTemplateFromRaw
60
  , MoveJob
61
  , JobSet
62
  , Result(..)
63
  , isOk
64
  , isBad
65
  , eitherToResult
66
  , annotateResult
67
  , Element(..)
68
  , FailMode(..)
69
  , FailStats
70
  , OpResult(..)
71
  , opToResult
72
  , connTimeout
73
  , queryTimeout
74
  , EvacMode(..)
75
  , ISpec(..)
76
  , IPolicy(..)
77
  , defIPolicy
78
  , rspecFromISpec
79
  ) where
80

    
81
import qualified Data.Map as M
82
import Text.JSON (makeObj, readJSON, showJSON)
83

    
84
import qualified Ganeti.Constants as C
85
import qualified Ganeti.THH as THH
86
import Ganeti.BasicTypes
87
import Ganeti.HTools.JSON
88

    
89
-- | The instance index type.
90
type Idx = Int
91

    
92
-- | The node index type.
93
type Ndx = Int
94

    
95
-- | The group index type.
96
type Gdx = Int
97

    
98
-- | The type used to hold name-to-idx mappings.
99
type NameAssoc = M.Map String Int
100

    
101
-- | A separate name for the cluster score type.
102
type Score = Double
103

    
104
-- | A separate name for a weight metric.
105
type Weight = Double
106

    
107
-- | The Group UUID type.
108
type GroupID = String
109

    
110
-- | Default group UUID (just a string, not a real UUID).
111
defaultGroupID :: GroupID
112
defaultGroupID = "00000000-0000-0000-0000-000000000000"
113

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

    
125
-- | The Group allocation policy type.
126
--
127
-- Note that the order of constructors is important as the automatic
128
-- Ord instance will order them in the order they are defined, so when
129
-- changing this data type be careful about the interaction with the
130
-- desired sorting order.
131
$(THH.declareSADT "AllocPolicy"
132
       [ ("AllocPreferred",   'C.allocPolicyPreferred)
133
       , ("AllocLastResort",  'C.allocPolicyLastResort)
134
       , ("AllocUnallocable", 'C.allocPolicyUnallocable)
135
       ])
136
$(THH.makeJSONInstance ''AllocPolicy)
137

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

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

    
158

    
159
-- | Instance specification type.
160
$(THH.buildObject "ISpec" "iSpec"
161
  [ THH.renameField "MemorySize" $ THH.simpleField "memory-size" [t| Int |]
162
  , THH.renameField "CpuCount"   $ THH.simpleField "cpu-count"   [t| Int |]
163
  , THH.renameField "DiskSize"   $ THH.simpleField "disk-size"   [t| Int |]
164
  , THH.renameField "DiskCount"  $ THH.simpleField "disk-count"  [t| Int |]
165
  , THH.renameField "NicCount"   $ THH.simpleField "nic-count"   [t| Int |]
166
  ])
167

    
168
-- | The default minimum ispec.
169
defMinISpec :: ISpec
170
defMinISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsMinMemorySize
171
                    , iSpecCpuCount   = C.ipolicyDefaultsMinCpuCount
172
                    , iSpecDiskSize   = C.ipolicyDefaultsMinDiskSize
173
                    , iSpecDiskCount  = C.ipolicyDefaultsMinDiskCount
174
                    , iSpecNicCount   = C.ipolicyDefaultsMinNicCount
175
                    }
176

    
177
-- | The default standard ispec.
178
defStdISpec :: ISpec
179
defStdISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsStdMemorySize
180
                    , iSpecCpuCount   = C.ipolicyDefaultsStdCpuCount
181
                    , iSpecDiskSize   = C.ipolicyDefaultsStdDiskSize
182
                    , iSpecDiskCount  = C.ipolicyDefaultsStdDiskCount
183
                    , iSpecNicCount   = C.ipolicyDefaultsStdNicCount
184
                    }
185

    
186
-- | The default max ispec.
187
defMaxISpec :: ISpec
188
defMaxISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsMaxMemorySize
189
                    , iSpecCpuCount   = C.ipolicyDefaultsMaxCpuCount
190
                    , iSpecDiskSize   = C.ipolicyDefaultsMaxDiskSize
191
                    , iSpecDiskCount  = C.ipolicyDefaultsMaxDiskCount
192
                    , iSpecNicCount   = C.ipolicyDefaultsMaxNicCount
193
                    }
194

    
195
-- | Instance policy type.
196
$(THH.buildObject "IPolicy" "iPolicy"
197
  [ THH.renameField "StdSpec" $ THH.simpleField "std" [t| ISpec |]
198
  , THH.renameField "MinSpec" $ THH.simpleField "min" [t| ISpec |]
199
  , THH.renameField "MaxSpec" $ THH.simpleField "max" [t| ISpec |]
200
  ])
201

    
202
-- | Converts an ISpec type to a RSpec one.
203
rspecFromISpec :: ISpec -> RSpec
204
rspecFromISpec ispec = RSpec { rspecCpu = iSpecCpuCount ispec
205
                             , rspecMem = iSpecMemorySize ispec
206
                             , rspecDsk = iSpecDiskSize ispec
207
                             }
208

    
209
-- | The default instance policy.
210
defIPolicy :: IPolicy
211
defIPolicy = IPolicy { iPolicyStdSpec = defStdISpec
212
                     , iPolicyMinSpec = defMinISpec
213
                     , iPolicyMaxSpec = defMaxISpec
214
                     }
215

    
216
-- | The dynamic resource specs of a machine (i.e. load or load
217
-- capacity, as opposed to size).
218
data DynUtil = DynUtil
219
  { cpuWeight :: Weight -- ^ Standardised CPU usage
220
  , memWeight :: Weight -- ^ Standardised memory load
221
  , dskWeight :: Weight -- ^ Standardised disk I\/O usage
222
  , netWeight :: Weight -- ^ Standardised network usage
223
  } deriving (Show, Read, Eq)
224

    
225
-- | Initial empty utilisation.
226
zeroUtil :: DynUtil
227
zeroUtil = DynUtil { cpuWeight = 0, memWeight = 0
228
                   , dskWeight = 0, netWeight = 0 }
229

    
230
-- | Base utilisation (used when no actual utilisation data is
231
-- supplied).
232
baseUtil :: DynUtil
233
baseUtil = DynUtil { cpuWeight = 1, memWeight = 1
234
                   , dskWeight = 1, netWeight = 1 }
235

    
236
-- | Sum two utilisation records.
237
addUtil :: DynUtil -> DynUtil -> DynUtil
238
addUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
239
  DynUtil (a1+b1) (a2+b2) (a3+b3) (a4+b4)
240

    
241
-- | Substracts one utilisation record from another.
242
subUtil :: DynUtil -> DynUtil -> DynUtil
243
subUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
244
  DynUtil (a1-b1) (a2-b2) (a3-b3) (a4-b4)
245

    
246
-- | The description of an instance placement. It contains the
247
-- instance index, the new primary and secondary node, the move being
248
-- performed and the score of the cluster after the move.
249
type Placement = (Idx, Ndx, Ndx, IMove, Score)
250

    
251
-- | An instance move definition.
252
data IMove = Failover                -- ^ Failover the instance (f)
253
           | ReplacePrimary Ndx      -- ^ Replace primary (f, r:np, f)
254
           | ReplaceSecondary Ndx    -- ^ Replace secondary (r:ns)
255
           | ReplaceAndFailover Ndx  -- ^ Replace secondary, failover (r:np, f)
256
           | FailoverAndReplace Ndx  -- ^ Failover, replace secondary (f, r:ns)
257
             deriving (Show, Read)
258

    
259
-- | Formatted solution output for one move (involved nodes and
260
-- commands.
261
type MoveJob = ([Ndx], Idx, IMove, [String])
262

    
263
-- | Unknown field in table output.
264
unknownField :: String
265
unknownField = "<unknown field>"
266

    
267
-- | A list of command elements.
268
type JobSet = [MoveJob]
269

    
270
-- | Connection timeout (when using non-file methods).
271
connTimeout :: Int
272
connTimeout = 15
273

    
274
-- | The default timeout for queries (when using non-file methods).
275
queryTimeout :: Int
276
queryTimeout = 60
277

    
278
-- | Default vcpu-to-pcpu ratio (randomly chosen value).
279
defVcpuRatio :: Double
280
defVcpuRatio = 64
281

    
282
-- | Default max disk usage ratio.
283
defReservedDiskRatio :: Double
284
defReservedDiskRatio = 0
285

    
286
-- | Base memory unit.
287
unitMem :: Int
288
unitMem = 64
289

    
290
-- | Base disk unit.
291
unitDsk :: Int
292
unitDsk = 256
293

    
294
-- | Base vcpus unit.
295
unitCpu :: Int
296
unitCpu = 1
297

    
298
-- | Reason for an operation's falure.
299
data FailMode = FailMem  -- ^ Failed due to not enough RAM
300
              | FailDisk -- ^ Failed due to not enough disk
301
              | FailCPU  -- ^ Failed due to not enough CPU capacity
302
              | FailN1   -- ^ Failed due to not passing N1 checks
303
              | FailTags -- ^ Failed due to tag exclusion
304
                deriving (Eq, Enum, Bounded, Show, Read)
305

    
306
-- | List with failure statistics.
307
type FailStats = [(FailMode, Int)]
308

    
309
-- | Either-like data-type customized for our failure modes.
310
--
311
-- The failure values for this monad track the specific allocation
312
-- failures, so this is not a general error-monad (compare with the
313
-- 'Result' data type). One downside is that this type cannot encode a
314
-- generic failure mode, hence 'fail' for this monad is not defined
315
-- and will cause an exception.
316
data OpResult a = OpFail FailMode -- ^ Failed operation
317
                | OpGood a        -- ^ Success operation
318
                  deriving (Show, Read)
319

    
320
instance Monad OpResult where
321
  (OpGood x) >>= fn = fn x
322
  (OpFail y) >>= _ = OpFail y
323
  return = OpGood
324

    
325
-- | Conversion from 'OpResult' to 'Result'.
326
opToResult :: OpResult a -> Result a
327
opToResult (OpFail f) = Bad $ show f
328
opToResult (OpGood v) = Ok v
329

    
330
-- | A generic class for items that have updateable names and indices.
331
class Element a where
332
  -- | Returns the name of the element
333
  nameOf  :: a -> String
334
  -- | Returns all the known names of the element
335
  allNames :: a -> [String]
336
  -- | Returns the index of the element
337
  idxOf   :: a -> Int
338
  -- | Updates the alias of the element
339
  setAlias :: a -> String -> a
340
  -- | Compute the alias by stripping a given suffix (domain) from
341
  -- the name
342
  computeAlias :: String -> a -> a
343
  computeAlias dom e = setAlias e alias
344
    where alias = take (length name - length dom) name
345
          name = nameOf e
346
  -- | Updates the index of the element
347
  setIdx  :: a -> Int -> a
348

    
349
-- | The iallocator node-evacuate evac_mode type.
350
$(THH.declareSADT "EvacMode"
351
       [ ("ChangePrimary",   'C.iallocatorNevacPri)
352
       , ("ChangeSecondary", 'C.iallocatorNevacSec)
353
       , ("ChangeAll",       'C.iallocatorNevacAll)
354
       ])
355
$(THH.makeJSONInstance ''EvacMode)