Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / Instance.hs @ 434c15d5

History | View | Annotate | Download (5.4 kB)

1
{-| Module describing an instance.
2

    
3
The instance data type holds very few fields, the algorithm
4
intelligence is in the "Node" and "Cluster" modules.
5

    
6
-}
7

    
8
{-
9

    
10
Copyright (C) 2009 Google Inc.
11

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

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

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

    
27
-}
28

    
29
module Ganeti.HTools.Instance
30
    ( Instance(..)
31
    , AssocList
32
    , List
33
    , create
34
    , setIdx
35
    , setName
36
    , setPri
37
    , setSec
38
    , setBoth
39
    , specOf
40
    , shrinkByType
41
    ) where
42

    
43
import qualified Ganeti.HTools.Types as T
44
import qualified Ganeti.HTools.Container as Container
45

    
46
-- * Type declarations
47

    
48
-- | The instance type
49
data Instance = Instance { name :: String    -- ^ The instance name
50
                         , mem :: Int        -- ^ Memory of the instance
51
                         , dsk :: Int        -- ^ Disk size of instance
52
                         , vcpus :: Int      -- ^ Number of VCPUs
53
                         , running :: Bool   -- ^ Is the instance running?
54
                         , runSt :: String   -- ^ Original (text) run status
55
                         , pNode :: T.Ndx    -- ^ Original primary node
56
                         , sNode :: T.Ndx    -- ^ Original secondary node
57
                         , idx :: T.Idx      -- ^ Internal index
58
                         , util :: T.DynUtil -- ^ Dynamic resource usage
59
                         , tags :: [String]  -- ^ List of instance tags
60
                         } deriving (Show)
61

    
62
instance T.Element Instance where
63
    nameOf  = name
64
    idxOf   = idx
65
    setName = setName
66
    setIdx  = setIdx
67

    
68
-- | Base memory unit.
69
unitMem :: Int
70
unitMem = 64
71
-- | Base disk unit.
72
unitDsk :: Int
73
unitDsk = 256
74
-- | Base vcpus unit.
75
unitCpu :: Int
76
unitCpu = 1
77

    
78
-- | A simple name for the int, instance association list.
79
type AssocList = [(T.Idx, Instance)]
80

    
81
-- | A simple name for an instance map.
82
type List = Container.Container Instance
83

    
84
-- * Initialization
85

    
86
-- | Create an instance.
87
--
88
-- Some parameters are not initialized by function, and must be set
89
-- later (via 'setIdx' for example).
90
create :: String -> Int -> Int -> Int -> String
91
       -> [String] -> T.Ndx -> T.Ndx -> Instance
92
create name_init mem_init dsk_init vcpus_init run_init tags_init pn sn =
93
    Instance { name = name_init
94
             , mem = mem_init
95
             , dsk = dsk_init
96
             , vcpus = vcpus_init
97
             , running = run_init == "running" || run_init == "ERROR_up"
98
             , runSt = run_init
99
             , pNode = pn
100
             , sNode = sn
101
             , idx = -1
102
             , util = T.baseUtil
103
             , tags = tags_init
104
             }
105

    
106
-- | Changes the index.
107
--
108
-- This is used only during the building of the data structures.
109
setIdx :: Instance -- ^ The original instance
110
       -> T.Idx    -- ^ New index
111
       -> Instance -- ^ The modified instance
112
setIdx t i = t { idx = i }
113

    
114
-- | Changes the name.
115
--
116
-- This is used only during the building of the data structures.
117
setName :: Instance -- ^ The original instance
118
        -> String   -- ^ New name
119
        -> Instance -- ^ The modified instance
120
setName t s = t { name = s }
121

    
122
-- * Update functions
123

    
124
-- | Changes the primary node of the instance.
125
setPri :: Instance  -- ^ the original instance
126
        -> T.Ndx    -- ^ the new primary node
127
        -> Instance -- ^ the modified instance
128
setPri t p = t { pNode = p }
129

    
130
-- | Changes the secondary node of the instance.
131
setSec :: Instance  -- ^ the original instance
132
        -> T.Ndx    -- ^ the new secondary node
133
        -> Instance -- ^ the modified instance
134
setSec t s = t { sNode = s }
135

    
136
-- | Changes both nodes of the instance.
137
setBoth :: Instance  -- ^ the original instance
138
         -> T.Ndx    -- ^ new primary node index
139
         -> T.Ndx    -- ^ new secondary node index
140
         -> Instance -- ^ the modified instance
141
setBoth t p s = t { pNode = p, sNode = s }
142

    
143
-- | Try to shrink the instance based on the reason why we can't
144
-- allocate it.
145
shrinkByType :: Instance -> T.FailMode -> T.Result Instance
146
shrinkByType inst T.FailMem = let v = mem inst - unitMem
147
                              in if v < unitMem
148
                                 then T.Bad "out of memory"
149
                                 else T.Ok inst { mem = v }
150
shrinkByType inst T.FailDisk = let v = dsk inst - unitDsk
151
                               in if v < unitDsk
152
                                  then T.Bad "out of disk"
153
                                  else T.Ok inst { dsk = v }
154
shrinkByType inst T.FailCPU = let v = vcpus inst - unitCpu
155
                              in if v < unitCpu
156
                                 then T.Bad "out of vcpus"
157
                                 else T.Ok inst { vcpus = v }
158
shrinkByType _ f = T.Bad $ "Unhandled failure mode " ++ show f
159

    
160
-- | Return the spec of an instance.
161
specOf :: Instance -> T.RSpec
162
specOf Instance { mem = m, dsk = d, vcpus = c } =
163
    T.RSpec { T.rspecCpu = c, T.rspecMem = m, T.rspecDsk = d }