Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / Types.hs @ 306cccd5

History | View | Annotate | Download (5.9 kB)

1
{-| Some common types.
2

    
3
-}
4

    
5
{-
6

    
7
Copyright (C) 2009, 2010 Google Inc.
8

    
9
This program is free software; you can redistribute it and/or modify
10
it under the terms of the GNU General Public License as published by
11
the Free Software Foundation; either version 2 of the License, or
12
(at your option) any later version.
13

    
14
This program is distributed in the hope that it will be useful, but
15
WITHOUT ANY WARRANTY; without even the implied warranty of
16
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17
General Public License for more details.
18

    
19
You should have received a copy of the GNU General Public License
20
along with this program; if not, write to the Free Software
21
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22
02110-1301, USA.
23

    
24
-}
25

    
26
module Ganeti.HTools.Types
27
    ( Idx
28
    , Ndx
29
    , NameAssoc
30
    , Score
31
    , Weight
32
    , RSpec(..)
33
    , DynUtil(..)
34
    , zeroUtil
35
    , baseUtil
36
    , addUtil
37
    , subUtil
38
    , defVcpuRatio
39
    , defReservedDiskRatio
40
    , unitMem
41
    , unitCpu
42
    , unitDsk
43
    , unknownField
44
    , Placement
45
    , IMove(..)
46
    , MoveJob
47
    , JobSet
48
    , Result(..)
49
    , Element(..)
50
    , FailMode(..)
51
    , FailStats
52
    , OpResult(..)
53
    , connTimeout
54
    , queryTimeout
55
    ) where
56

    
57
-- | The instance index type.
58
type Idx = Int
59

    
60
-- | The node index type.
61
type Ndx = Int
62

    
63
-- | The type used to hold name-to-idx mappings.
64
type NameAssoc = [(String, Int)]
65

    
66
-- | A separate name for the cluster score type.
67
type Score = Double
68

    
69
-- | A separate name for a weight metric.
70
type Weight = Double
71

    
72
-- | The resource spec type.
73
data RSpec = RSpec
74
    { rspecCpu  :: Int  -- ^ Requested VCPUs
75
    , rspecMem  :: Int  -- ^ Requested memory
76
    , rspecDsk  :: Int  -- ^ Requested disk
77
    } deriving (Show, Eq)
78

    
79
-- | The dynamic resource specs of a machine (i.e. load or load
80
-- capacity, as opposed to size).
81
data DynUtil = DynUtil
82
    { cpuWeight :: Weight -- ^ Standardised CPU usage
83
    , memWeight :: Weight -- ^ Standardised memory load
84
    , dskWeight :: Weight -- ^ Standardised disk I\/O usage
85
    , netWeight :: Weight -- ^ Standardised network usage
86
    } deriving (Show, Eq)
87

    
88
-- | Initial empty utilisation
89
zeroUtil :: DynUtil
90
zeroUtil = DynUtil { cpuWeight = 0, memWeight = 0
91
                   , dskWeight = 0, netWeight = 0 }
92

    
93
baseUtil :: DynUtil
94
baseUtil = DynUtil { cpuWeight = 1, memWeight = 1
95
                   , dskWeight = 1, netWeight = 1 }
96

    
97
addUtil :: DynUtil -> DynUtil -> DynUtil
98
addUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
99
    DynUtil (a1+b1) (a2+b2) (a3+b3) (a4+b4)
100

    
101
subUtil :: DynUtil -> DynUtil -> DynUtil
102
subUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
103
    DynUtil (a1-b1) (a2-b2) (a3-b3) (a4-b4)
104

    
105
-- | The description of an instance placement. It contains the
106
-- instance index, the new primary and secondary node, the move being
107
-- performed and the score of the cluster after the move.
108
type Placement = (Idx, Ndx, Ndx, IMove, Score)
109

    
110
-- | An instance move definition
111
data IMove = Failover                -- ^ Failover the instance (f)
112
           | ReplacePrimary Ndx      -- ^ Replace primary (f, r:np, f)
113
           | ReplaceSecondary Ndx    -- ^ Replace secondary (r:ns)
114
           | ReplaceAndFailover Ndx  -- ^ Replace secondary, failover (r:np, f)
115
           | FailoverAndReplace Ndx  -- ^ Failover, replace secondary (f, r:ns)
116
             deriving (Show)
117

    
118
-- | Formatted solution output for one move (involved nodes and
119
-- commands
120
type MoveJob = ([Ndx], Idx, IMove, [String])
121

    
122
-- | Unknown field in table output
123
unknownField :: String
124
unknownField = "<unknown field>"
125

    
126
-- | A list of command elements
127
type JobSet = [MoveJob]
128

    
129
-- | Connection timeout (when using non-file methods).
130
connTimeout :: Int
131
connTimeout = 15
132

    
133
-- | The default timeout for queries (when using non-file methods).
134
queryTimeout :: Int
135
queryTimeout = 60
136

    
137
-- | Default vcpu-to-pcpu ratio (randomly chosen value).
138
defVcpuRatio :: Double
139
defVcpuRatio = 64
140

    
141
-- | Default max disk usage ratio.
142
defReservedDiskRatio :: Double
143
defReservedDiskRatio = 0
144

    
145
-- | Base memory unit.
146
unitMem :: Int
147
unitMem = 64
148

    
149
-- | Base disk unit.
150
unitDsk :: Int
151
unitDsk = 256
152

    
153
-- | Base vcpus unit.
154
unitCpu :: Int
155
unitCpu = 1
156

    
157
{-|
158

    
159
This is similar to the JSON library Result type - *very* similar, but
160
we want to use it in multiple places, so we abstract it into a
161
mini-library here
162

    
163
-}
164
data Result a
165
    = Bad String
166
    | Ok a
167
    deriving (Show)
168

    
169
instance Monad Result where
170
    (>>=) (Bad x) _ = Bad x
171
    (>>=) (Ok x) fn = fn x
172
    return = Ok
173
    fail = Bad
174

    
175
-- | Reason for an operation's falure
176
data FailMode = FailMem  -- ^ Failed due to not enough RAM
177
              | FailDisk -- ^ Failed due to not enough disk
178
              | FailCPU  -- ^ Failed due to not enough CPU capacity
179
              | FailN1   -- ^ Failed due to not passing N1 checks
180
              | FailTags -- ^ Failed due to tag exclusion
181
                deriving (Eq, Enum, Bounded, Show)
182

    
183
-- | List with failure statistics
184
type FailStats = [(FailMode, Int)]
185

    
186
-- | Either-like data-type customized for our failure modes
187
data OpResult a = OpFail FailMode -- ^ Failed operation
188
                | OpGood a        -- ^ Success operation
189
                  deriving (Show)
190

    
191
instance Monad OpResult where
192
    (OpGood x) >>= fn = fn x
193
    (OpFail y) >>= _ = OpFail y
194
    return = OpGood
195

    
196
-- | A generic class for items that have updateable names and indices.
197
class Element a where
198
    -- | Returns the name of the element
199
    nameOf  :: a -> String
200
    -- | Returns all the known names of the element
201
    allNames :: a -> [String]
202
    -- | Returns the index of the element
203
    idxOf   :: a -> Int
204
    -- | Updates the alias of the element
205
    setAlias :: a -> String -> a
206
    -- | Compute the alias by stripping a given suffix (domain) from
207
    -- | the name
208
    computeAlias :: String -> a -> a
209
    computeAlias dom e = setAlias e alias
210
        where alias = take (length name - length dom) name
211
              name = nameOf e
212
    -- | Updates the index of the element
213
    setIdx  :: a -> Int -> a