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