Remove the noLimit values and always use limits
[ganeti-local] / Ganeti / HTools / Types.hs
1 {-| Some common types.
2
3 -}
4
5 {-
6
7 Copyright (C) 2009 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     , Placement
41     , IMove(..)
42     , MoveJob
43     , JobSet
44     , Result(..)
45     , Element(..)
46     , FailMode(..)
47     , FailStats
48     , OpResult(..)
49     , connTimeout
50     , queryTimeout
51     ) where
52
53 -- | The instance index type.
54 type Idx = Int
55
56 -- | The node index type.
57 type Ndx = Int
58
59 -- | The type used to hold name-to-idx mappings.
60 type NameAssoc = [(String, Int)]
61
62 -- | A separate name for the cluster score type.
63 type Score = Double
64
65 -- | A separate name for a weight metric.
66 type Weight = Double
67
68 -- | The resource spec type.
69 data RSpec = RSpec
70     { rspecCpu  :: Int  -- ^ Requested VCPUs
71     , rspecMem  :: Int  -- ^ Requested memory
72     , rspecDsk  :: Int  -- ^ Requested disk
73     } deriving (Show, Eq)
74
75 -- | The dynamic resource specs of a machine (i.e. load or load
76 -- capacity, as opposed to size).
77 data DynUtil = DynUtil
78     { cpuWeight :: Weight -- ^ Standardised CPU usage
79     , memWeight :: Weight -- ^ Standardised memory load
80     , dskWeight :: Weight -- ^ Standardised disk I\/O usage
81     , netWeight :: Weight -- ^ Standardised network usage
82     } deriving (Show)
83
84 -- | Initial empty utilisation
85 zeroUtil :: DynUtil
86 zeroUtil = DynUtil { cpuWeight = 0, memWeight = 0
87                    , dskWeight = 0, netWeight = 0 }
88
89 baseUtil :: DynUtil
90 baseUtil = DynUtil { cpuWeight = 1, memWeight = 1
91                    , dskWeight = 1, netWeight = 1 }
92
93 addUtil :: DynUtil -> DynUtil -> DynUtil
94 addUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
95     DynUtil (a1+b1) (a2+b2) (a3+b3) (a4+b4)
96
97 subUtil :: DynUtil -> DynUtil -> DynUtil
98 subUtil (DynUtil a1 a2 a3 a4) (DynUtil b1 b2 b3 b4) =
99     DynUtil (a1-b1) (a2-b2) (a3-b3) (a4-b4)
100
101 -- | The description of an instance placement. It contains the
102 -- instance index, the new primary and secondary node, the move being
103 -- performed and the score of the cluster after the move.
104 type Placement = (Idx, Ndx, Ndx, IMove, Score)
105
106 -- | An instance move definition
107 data IMove = Failover                -- ^ Failover the instance (f)
108            | ReplacePrimary Ndx      -- ^ Replace primary (f, r:np, f)
109            | ReplaceSecondary Ndx    -- ^ Replace secondary (r:ns)
110            | ReplaceAndFailover Ndx  -- ^ Replace secondary, failover (r:np, f)
111            | FailoverAndReplace Ndx  -- ^ Failover, replace secondary (f, r:ns)
112              deriving (Show)
113
114 -- | Formatted solution output for one move (involved nodes and
115 -- commands
116 type MoveJob = ([Ndx], Idx, IMove, [String])
117
118 -- | A list of command elements
119 type JobSet = [MoveJob]
120
121 -- | Connection timeout (when using non-file methods).
122 connTimeout :: Int
123 connTimeout = 15
124
125 -- | The default timeout for queries (when using non-file methods).
126 queryTimeout :: Int
127 queryTimeout = 60
128
129 -- | Default vcpu-to-pcpu ratio (randomly chosen value).
130 defVcpuRatio :: Double
131 defVcpuRatio = 64
132
133 -- | Default max disk usage ratio.
134 defReservedDiskRatio :: Double
135 defReservedDiskRatio = 0
136
137 {-|
138
139 This is similar to the JSON library Result type - *very* similar, but
140 we want to use it in multiple places, so we abstract it into a
141 mini-library here
142
143 -}
144 data Result a
145     = Bad String
146     | Ok a
147     deriving (Show)
148
149 instance Monad Result where
150     (>>=) (Bad x) _ = Bad x
151     (>>=) (Ok x) fn = fn x
152     return = Ok
153     fail = Bad
154
155 -- | Reason for an operation's falure
156 data FailMode = FailMem  -- ^ Failed due to not enough RAM
157               | FailDisk -- ^ Failed due to not enough disk
158               | FailCPU  -- ^ Failed due to not enough CPU capacity
159               | FailN1   -- ^ Failed due to not passing N1 checks
160               | FailTags -- ^ Failed due to tag exclusion
161                 deriving (Eq, Enum, Bounded, Show)
162
163 -- | List with failure statistics
164 type FailStats = [(FailMode, Int)]
165
166 -- | Either-like data-type customized for our failure modes
167 data OpResult a = OpFail FailMode -- ^ Failed operation
168                 | OpGood a        -- ^ Success operation
169
170 instance Monad OpResult where
171     (OpGood x) >>= fn = fn x
172     (OpFail y) >>= _ = OpFail y
173     return = OpGood
174
175 -- | A generic class for items that have updateable names and indices.
176 class Element a where
177     -- | Returns the name of the element
178     nameOf  :: a -> String
179     -- | Returns the index of the element
180     idxOf   :: a -> Int
181     -- | Updates the name of the element
182     setName :: a -> String -> a
183     -- | Updates the index of the element
184     setIdx  :: a -> Int -> a