Statistics
| Branch: | Tag: | Revision:

root / Ganeti / HTools / Loader.hs @ e2fa2baf

History | View | Annotate | Download (6.7 kB)

1 040afc35 Iustin Pop
{-| Loading data from external sources
2 040afc35 Iustin Pop
3 040afc35 Iustin Pop
This module holds the common code for loading the cluster state from external sources.
4 040afc35 Iustin Pop
5 040afc35 Iustin Pop
-}
6 040afc35 Iustin Pop
7 e2fa2baf Iustin Pop
{-
8 e2fa2baf Iustin Pop
9 e2fa2baf Iustin Pop
Copyright (C) 2009 Google Inc.
10 e2fa2baf Iustin Pop
11 e2fa2baf Iustin Pop
This program is free software; you can redistribute it and/or modify
12 e2fa2baf Iustin Pop
it under the terms of the GNU General Public License as published by
13 e2fa2baf Iustin Pop
the Free Software Foundation; either version 2 of the License, or
14 e2fa2baf Iustin Pop
(at your option) any later version.
15 e2fa2baf Iustin Pop
16 e2fa2baf Iustin Pop
This program is distributed in the hope that it will be useful, but
17 e2fa2baf Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
18 e2fa2baf Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 e2fa2baf Iustin Pop
General Public License for more details.
20 e2fa2baf Iustin Pop
21 e2fa2baf Iustin Pop
You should have received a copy of the GNU General Public License
22 e2fa2baf Iustin Pop
along with this program; if not, write to the Free Software
23 e2fa2baf Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 e2fa2baf Iustin Pop
02110-1301, USA.
25 e2fa2baf Iustin Pop
26 e2fa2baf Iustin Pop
-}
27 e2fa2baf Iustin Pop
28 040afc35 Iustin Pop
module Ganeti.HTools.Loader
29 446d8827 Iustin Pop
    ( mergeData
30 446d8827 Iustin Pop
    , checkData
31 446d8827 Iustin Pop
    , assignIndices
32 446d8827 Iustin Pop
    , lookupNode
33 5a1edeb6 Iustin Pop
    , lookupInstance
34 ed41c179 Iustin Pop
    , stripSuffix
35 19f38ee8 Iustin Pop
    , RqType(..)
36 19f38ee8 Iustin Pop
    , Request(..)
37 446d8827 Iustin Pop
    ) where
38 040afc35 Iustin Pop
39 e4c5beaf Iustin Pop
import Data.List
40 446d8827 Iustin Pop
import Data.Maybe (fromJust)
41 446d8827 Iustin Pop
import Text.Printf (printf)
42 e4c5beaf Iustin Pop
43 e4c5beaf Iustin Pop
import qualified Ganeti.HTools.Container as Container
44 e4c5beaf Iustin Pop
import qualified Ganeti.HTools.Instance as Instance
45 e4c5beaf Iustin Pop
import qualified Ganeti.HTools.Node as Node
46 e4c5beaf Iustin Pop
47 e4c5beaf Iustin Pop
import Ganeti.HTools.Types
48 e4c5beaf Iustin Pop
49 19f38ee8 Iustin Pop
-- * Types
50 19f38ee8 Iustin Pop
51 19f38ee8 Iustin Pop
{-| The request type.
52 19f38ee8 Iustin Pop
53 19f38ee8 Iustin Pop
This type denotes what request we got from Ganeti and also holds
54 19f38ee8 Iustin Pop
request-specific fields.
55 19f38ee8 Iustin Pop
56 19f38ee8 Iustin Pop
-}
57 19f38ee8 Iustin Pop
data RqType
58 19f38ee8 Iustin Pop
    = Allocate Instance.Instance Int -- ^ A new instance allocation
59 19f38ee8 Iustin Pop
    | Relocate Idx Int [Ndx]         -- ^ Move an instance to a new
60 19f38ee8 Iustin Pop
                                     -- secondary node
61 19f38ee8 Iustin Pop
    deriving (Show)
62 19f38ee8 Iustin Pop
63 19f38ee8 Iustin Pop
-- | A complete request, as received from Ganeti.
64 19f38ee8 Iustin Pop
data Request = Request RqType Node.List Instance.List String
65 19f38ee8 Iustin Pop
    deriving (Show)
66 19f38ee8 Iustin Pop
67 19f38ee8 Iustin Pop
-- * Functions
68 19f38ee8 Iustin Pop
69 9188aeef Iustin Pop
-- | Lookups a node into an assoc list.
70 608efcce Iustin Pop
lookupNode :: (Monad m) => [(String, Ndx)] -> String -> String -> m Ndx
71 040afc35 Iustin Pop
lookupNode ktn inst node =
72 040afc35 Iustin Pop
    case lookup node ktn of
73 040afc35 Iustin Pop
      Nothing -> fail $ "Unknown node '" ++ node ++ "' for instance " ++ inst
74 040afc35 Iustin Pop
      Just idx -> return idx
75 040afc35 Iustin Pop
76 9188aeef Iustin Pop
-- | Lookups an instance into an assoc list.
77 5a1edeb6 Iustin Pop
lookupInstance :: (Monad m) => [(String, Idx)] -> String -> m Idx
78 5a1edeb6 Iustin Pop
lookupInstance kti inst =
79 5a1edeb6 Iustin Pop
    case lookup inst kti of
80 5a1edeb6 Iustin Pop
      Nothing -> fail $ "Unknown instance '" ++ inst ++ "'"
81 5a1edeb6 Iustin Pop
      Just idx -> return idx
82 5a1edeb6 Iustin Pop
83 9188aeef Iustin Pop
-- | Given a list of elements (and their names), assign indices to them.
84 497e30a1 Iustin Pop
assignIndices :: (Element a) =>
85 497e30a1 Iustin Pop
                 [(String, a)]
86 040afc35 Iustin Pop
              -> (NameAssoc, [(Int, a)])
87 497e30a1 Iustin Pop
assignIndices =
88 497e30a1 Iustin Pop
    unzip . map (\ (idx, (k, v)) -> ((k, idx), (idx, setIdx v idx)))
89 040afc35 Iustin Pop
          . zip [0..]
90 e4c5beaf Iustin Pop
91 9188aeef Iustin Pop
-- | For each instance, add its index to its primary and secondary nodes.
92 608efcce Iustin Pop
fixNodes :: [(Ndx, Node.Node)]
93 608efcce Iustin Pop
         -> [(Idx, Instance.Instance)]
94 608efcce Iustin Pop
         -> [(Ndx, Node.Node)]
95 e4c5beaf Iustin Pop
fixNodes nl il =
96 e4c5beaf Iustin Pop
    foldl' (\accu (idx, inst) ->
97 e4c5beaf Iustin Pop
                let
98 e4c5beaf Iustin Pop
                    assocEqual = (\ (i, _) (j, _) -> i == j)
99 e4c5beaf Iustin Pop
                    pdx = Instance.pnode inst
100 e4c5beaf Iustin Pop
                    sdx = Instance.snode inst
101 e4c5beaf Iustin Pop
                    pold = fromJust $ lookup pdx accu
102 e4c5beaf Iustin Pop
                    pnew = Node.setPri pold idx
103 e4c5beaf Iustin Pop
                    ac1 = deleteBy assocEqual (pdx, pold) accu
104 e4c5beaf Iustin Pop
                    ac2 = (pdx, pnew):ac1
105 e4c5beaf Iustin Pop
                in
106 e4c5beaf Iustin Pop
                  if sdx /= Node.noSecondary then
107 e4c5beaf Iustin Pop
                      let
108 e4c5beaf Iustin Pop
                          sold = fromJust $ lookup sdx accu
109 e4c5beaf Iustin Pop
                          snew = Node.setSec sold idx
110 e4c5beaf Iustin Pop
                          ac3 = deleteBy assocEqual (sdx, sold) ac2
111 e4c5beaf Iustin Pop
                          ac4 = (sdx, snew):ac3
112 e4c5beaf Iustin Pop
                      in ac4
113 e4c5beaf Iustin Pop
                  else
114 e4c5beaf Iustin Pop
                      ac2
115 e4c5beaf Iustin Pop
           ) nl il
116 e4c5beaf Iustin Pop
117 f9fc7a63 Iustin Pop
-- | Compute the longest common suffix of a list of strings that
118 9188aeef Iustin Pop
-- | starts with a dot.
119 8472a321 Iustin Pop
longestDomain :: [String] -> String
120 e4c5beaf Iustin Pop
longestDomain [] = ""
121 8472a321 Iustin Pop
longestDomain (x:xs) =
122 8472a321 Iustin Pop
      foldr (\ suffix accu -> if all (isSuffixOf suffix) xs
123 e4c5beaf Iustin Pop
                              then suffix
124 e4c5beaf Iustin Pop
                              else accu)
125 e4c5beaf Iustin Pop
      "" $ filter (isPrefixOf ".") (tails x)
126 e4c5beaf Iustin Pop
127 9188aeef Iustin Pop
-- | Remove tail suffix from a string.
128 26b5d395 Iustin Pop
stripSuffix :: Int -> String -> String
129 26b5d395 Iustin Pop
stripSuffix sflen name = take ((length name) - sflen) name
130 e4c5beaf Iustin Pop
131 9188aeef Iustin Pop
-- | Initializer function that loads the data from a node and instance
132 9188aeef Iustin Pop
-- list and massages it into the correct format.
133 e3a684c5 Iustin Pop
mergeData :: (Node.AssocList,
134 e3a684c5 Iustin Pop
              Instance.AssocList) -- ^ Data from either Text.loadData
135 e3a684c5 Iustin Pop
                                  -- or Rapi.loadData
136 262a08a2 Iustin Pop
          -> Result (Node.List, Instance.List, String)
137 e3a684c5 Iustin Pop
mergeData (nl, il) = do
138 e4c5beaf Iustin Pop
  let
139 e4c5beaf Iustin Pop
      nl2 = fixNodes nl il
140 e4c5beaf Iustin Pop
      il3 = Container.fromAssocList il
141 e4c5beaf Iustin Pop
      nl3 = Container.fromAssocList
142 9cf4267a Iustin Pop
            (map (\ (k, v) -> (k, Node.buildPeers v il3)) nl2)
143 8472a321 Iustin Pop
      node_names = map Node.name $ Container.elems nl3
144 8472a321 Iustin Pop
      inst_names = map Instance.name $ Container.elems il3
145 8472a321 Iustin Pop
      common_suffix = longestDomain (node_names ++ inst_names)
146 26b5d395 Iustin Pop
      csl = length common_suffix
147 262a08a2 Iustin Pop
      snl = Container.map (\n -> setName n (stripSuffix csl $ nameOf n)) nl3
148 262a08a2 Iustin Pop
      sil = Container.map (\i -> setName i (stripSuffix csl $ nameOf i)) il3
149 8472a321 Iustin Pop
  return (snl, sil, common_suffix)
150 446d8827 Iustin Pop
151 9188aeef Iustin Pop
-- | Checks the cluster data for consistency.
152 262a08a2 Iustin Pop
checkData :: Node.List -> Instance.List
153 262a08a2 Iustin Pop
          -> ([String], Node.List)
154 dbd6700b Iustin Pop
checkData nl il =
155 446d8827 Iustin Pop
    Container.mapAccum
156 446d8827 Iustin Pop
        (\ msgs node ->
157 dbd6700b Iustin Pop
             let nname = Node.name node
158 446d8827 Iustin Pop
                 nilst = map (flip Container.find $ il) (Node.plist node)
159 446d8827 Iustin Pop
                 dilst = filter (not . Instance.running) nilst
160 446d8827 Iustin Pop
                 adj_mem = sum . map Instance.mem $ dilst
161 446d8827 Iustin Pop
                 delta_mem = (truncate $ Node.t_mem node)
162 446d8827 Iustin Pop
                             - (Node.n_mem node)
163 446d8827 Iustin Pop
                             - (Node.f_mem node)
164 446d8827 Iustin Pop
                             - (nodeImem node il)
165 446d8827 Iustin Pop
                             + adj_mem
166 446d8827 Iustin Pop
                 delta_dsk = (truncate $ Node.t_dsk node)
167 446d8827 Iustin Pop
                             - (Node.f_dsk node)
168 446d8827 Iustin Pop
                             - (nodeIdsk node il)
169 446d8827 Iustin Pop
                 newn = Node.setFmem (Node.setXmem node delta_mem)
170 446d8827 Iustin Pop
                        (Node.f_mem node - adj_mem)
171 446d8827 Iustin Pop
                 umsg1 = if delta_mem > 512 || delta_dsk > 1024
172 446d8827 Iustin Pop
                         then [printf "node %s is missing %d MB ram \
173 446d8827 Iustin Pop
                                     \and %d GB disk"
174 446d8827 Iustin Pop
                                     nname delta_mem (delta_dsk `div` 1024)]
175 446d8827 Iustin Pop
                         else []
176 446d8827 Iustin Pop
             in (msgs ++ umsg1, newn)
177 446d8827 Iustin Pop
        ) [] nl
178 446d8827 Iustin Pop
179 446d8827 Iustin Pop
-- | Compute the amount of memory used by primary instances on a node.
180 262a08a2 Iustin Pop
nodeImem :: Node.Node -> Instance.List -> Int
181 446d8827 Iustin Pop
nodeImem node il =
182 446d8827 Iustin Pop
    let rfind = flip Container.find $ il
183 446d8827 Iustin Pop
    in sum . map Instance.mem .
184 446d8827 Iustin Pop
       map rfind $ Node.plist node
185 446d8827 Iustin Pop
186 446d8827 Iustin Pop
-- | Compute the amount of disk used by instances on a node (either primary
187 446d8827 Iustin Pop
-- or secondary).
188 262a08a2 Iustin Pop
nodeIdsk :: Node.Node -> Instance.List -> Int
189 446d8827 Iustin Pop
nodeIdsk node il =
190 446d8827 Iustin Pop
    let rfind = flip Container.find $ il
191 446d8827 Iustin Pop
    in sum . map Instance.dsk .
192 446d8827 Iustin Pop
       map rfind $ (Node.plist node) ++ (Node.slist node)