Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (10.7 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
  ) where
79

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

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

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

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

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

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

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

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

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

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

    
113
-- | The Group allocation policy type.
114
--
115
-- Note that the order of constructors is important as the automatic
116
-- Ord instance will order them in the order they are defined, so when
117
-- changing this data type be careful about the interaction with the
118
-- desired sorting order.
119
$(THH.declareSADT "AllocPolicy"
120
       [ ("AllocPreferred",   'C.allocPolicyPreferred)
121
       , ("AllocLastResort",  'C.allocPolicyLastResort)
122
       , ("AllocUnallocable", 'C.allocPolicyUnallocable)
123
       ])
124
$(THH.makeJSONInstance ''AllocPolicy)
125

    
126
-- | The Instance real state type.
127
$(THH.declareSADT "InstanceStatus"
128
       [ ("AdminDown", 'C.inststAdmindown)
129
       , ("AdminOffline", 'C.inststAdminoffline)
130
       , ("ErrorDown", 'C.inststErrordown)
131
       , ("ErrorUp", 'C.inststErrorup)
132
       , ("NodeDown", 'C.inststNodedown)
133
       , ("NodeOffline", 'C.inststNodeoffline)
134
       , ("Running", 'C.inststRunning)
135
       , ("WrongNode", 'C.inststWrongnode)
136
       ])
137
$(THH.makeJSONInstance ''InstanceStatus)
138

    
139
-- | The resource spec type.
140
data RSpec = RSpec
141
  { rspecCpu  :: Int  -- ^ Requested VCPUs
142
  , rspecMem  :: Int  -- ^ Requested memory
143
  , rspecDsk  :: Int  -- ^ Requested disk
144
  } deriving (Show, Read, Eq)
145

    
146

    
147
-- | Instance specification type.
148
$(THH.buildObject "ISpec" "iSpec"
149
  [ THH.renameField "MemorySize" $ THH.simpleField "memory-size" [t| Int |]
150
  , THH.renameField "CpuCount"   $ THH.simpleField "cpu-count"   [t| Int |]
151
  , THH.renameField "DiskSize"   $ THH.simpleField "disk-size"   [t| Int |]
152
  , THH.renameField "DiskCount"  $ THH.simpleField "disk-count"  [t| Int |]
153
  , THH.renameField "NicCount"   $ THH.simpleField "nic-count"   [t| Int |]
154
  ])
155

    
156
-- | The default minimum ispec.
157
defMinISpec :: ISpec
158
defMinISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsMinMemorySize
159
                    , iSpecCpuCount   = C.ipolicyDefaultsMinCpuCount
160
                    , iSpecDiskSize   = C.ipolicyDefaultsMinDiskSize
161
                    , iSpecDiskCount  = C.ipolicyDefaultsMinDiskCount
162
                    , iSpecNicCount   = C.ipolicyDefaultsMinNicCount
163
                    }
164

    
165
-- | The default standard ispec.
166
defStdISpec :: ISpec
167
defStdISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsStdMemorySize
168
                    , iSpecCpuCount   = C.ipolicyDefaultsStdCpuCount
169
                    , iSpecDiskSize   = C.ipolicyDefaultsStdDiskSize
170
                    , iSpecDiskCount  = C.ipolicyDefaultsStdDiskCount
171
                    , iSpecNicCount   = C.ipolicyDefaultsStdNicCount
172
                    }
173

    
174
-- | The default max ispec.
175
defMaxISpec :: ISpec
176
defMaxISpec = ISpec { iSpecMemorySize = C.ipolicyDefaultsMaxMemorySize
177
                    , iSpecCpuCount   = C.ipolicyDefaultsMaxCpuCount
178
                    , iSpecDiskSize   = C.ipolicyDefaultsMaxDiskSize
179
                    , iSpecDiskCount  = C.ipolicyDefaultsMaxDiskCount
180
                    , iSpecNicCount   = C.ipolicyDefaultsMaxNicCount
181
                    }
182

    
183
-- | Instance policy type.
184
$(THH.buildObject "IPolicy" "iPolicy"
185
  [ THH.renameField "StdSpec" $ THH.simpleField "std" [t| ISpec |]
186
  , THH.renameField "MinSpec" $ THH.simpleField "min" [t| ISpec |]
187
  , THH.renameField "MaxSpec" $ THH.simpleField "max" [t| ISpec |]
188
  ])
189

    
190
-- | The default instance policy.
191
defIPolicy :: IPolicy
192
defIPolicy = IPolicy { iPolicyStdSpec = defStdISpec
193
                     , iPolicyMinSpec = defMinISpec
194
                     , iPolicyMaxSpec = defMaxISpec
195
                     }
196

    
197
-- | The dynamic resource specs of a machine (i.e. load or load
198
-- capacity, as opposed to size).
199
data DynUtil = DynUtil
200
  { cpuWeight :: Weight -- ^ Standardised CPU usage
201
  , memWeight :: Weight -- ^ Standardised memory load
202
  , dskWeight :: Weight -- ^ Standardised disk I\/O usage
203
  , netWeight :: Weight -- ^ Standardised network usage
204
  } deriving (Show, Read, Eq)
205

    
206
-- | Initial empty utilisation.
207
zeroUtil :: DynUtil
208
zeroUtil = DynUtil { cpuWeight = 0, memWeight = 0
209
                   , dskWeight = 0, netWeight = 0 }
210

    
211
-- | Base utilisation (used when no actual utilisation data is
212
-- supplied).
213
baseUtil :: DynUtil
214
baseUtil = DynUtil { cpuWeight = 1, memWeight = 1
215
                   , dskWeight = 1, netWeight = 1 }
216

    
217
-- | Sum two utilisation records.
218
addUtil :: DynUtil -> DynUtil -> DynUtil
219
addUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
220
  DynUtil (a1+b1) (a2+b2) (a3+b3) (a4+b4)
221

    
222
-- | Substracts one utilisation record from another.
223
subUtil :: DynUtil -> DynUtil -> DynUtil
224
subUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
225
  DynUtil (a1-b1) (a2-b2) (a3-b3) (a4-b4)
226

    
227
-- | The description of an instance placement. It contains the
228
-- instance index, the new primary and secondary node, the move being
229
-- performed and the score of the cluster after the move.
230
type Placement = (Idx, Ndx, Ndx, IMove, Score)
231

    
232
-- | An instance move definition.
233
data IMove = Failover                -- ^ Failover the instance (f)
234
           | ReplacePrimary Ndx      -- ^ Replace primary (f, r:np, f)
235
           | ReplaceSecondary Ndx    -- ^ Replace secondary (r:ns)
236
           | ReplaceAndFailover Ndx  -- ^ Replace secondary, failover (r:np, f)
237
           | FailoverAndReplace Ndx  -- ^ Failover, replace secondary (f, r:ns)
238
             deriving (Show, Read)
239

    
240
-- | Instance disk template type.
241
$(THH.declareSADT "DiskTemplate"
242
       [ ("DTDiskless",   'C.dtDiskless)
243
       , ("DTFile",       'C.dtFile)
244
       , ("DTSharedFile", 'C.dtSharedFile)
245
       , ("DTPlain",      'C.dtPlain)
246
       , ("DTBlock",      'C.dtBlock)
247
       , ("DTDrbd8",      'C.dtDrbd8)
248
       ])
249
$(THH.makeJSONInstance ''DiskTemplate)
250

    
251
-- | Formatted solution output for one move (involved nodes and
252
-- commands.
253
type MoveJob = ([Ndx], Idx, IMove, [String])
254

    
255
-- | Unknown field in table output.
256
unknownField :: String
257
unknownField = "<unknown field>"
258

    
259
-- | A list of command elements.
260
type JobSet = [MoveJob]
261

    
262
-- | Connection timeout (when using non-file methods).
263
connTimeout :: Int
264
connTimeout = 15
265

    
266
-- | The default timeout for queries (when using non-file methods).
267
queryTimeout :: Int
268
queryTimeout = 60
269

    
270
-- | Default vcpu-to-pcpu ratio (randomly chosen value).
271
defVcpuRatio :: Double
272
defVcpuRatio = 64
273

    
274
-- | Default max disk usage ratio.
275
defReservedDiskRatio :: Double
276
defReservedDiskRatio = 0
277

    
278
-- | Base memory unit.
279
unitMem :: Int
280
unitMem = 64
281

    
282
-- | Base disk unit.
283
unitDsk :: Int
284
unitDsk = 256
285

    
286
-- | Base vcpus unit.
287
unitCpu :: Int
288
unitCpu = 1
289

    
290
-- | Reason for an operation's falure.
291
data FailMode = FailMem  -- ^ Failed due to not enough RAM
292
              | FailDisk -- ^ Failed due to not enough disk
293
              | FailCPU  -- ^ Failed due to not enough CPU capacity
294
              | FailN1   -- ^ Failed due to not passing N1 checks
295
              | FailTags -- ^ Failed due to tag exclusion
296
                deriving (Eq, Enum, Bounded, Show, Read)
297

    
298
-- | List with failure statistics.
299
type FailStats = [(FailMode, Int)]
300

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

    
312
instance Monad OpResult where
313
  (OpGood x) >>= fn = fn x
314
  (OpFail y) >>= _ = OpFail y
315
  return = OpGood
316

    
317
-- | Conversion from 'OpResult' to 'Result'.
318
opToResult :: OpResult a -> Result a
319
opToResult (OpFail f) = Bad $ show f
320
opToResult (OpGood v) = Ok v
321

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

    
341
-- | The iallocator node-evacuate evac_mode type.
342
$(THH.declareSADT "EvacMode"
343
       [ ("ChangePrimary",   'C.iallocatorNevacPri)
344
       , ("ChangeSecondary", 'C.iallocatorNevacSec)
345
       , ("ChangeAll",       'C.iallocatorNevacAll)
346
       ])
347
$(THH.makeJSONInstance ''EvacMode)