1 {-| Generic data loader
3 This module holds the common code for parsing the input data after it
4 has been loaded from external sources.
10 Copyright (C) 2009 Google Inc.
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.
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.
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
29 module Ganeti.HTools.Loader
40 import Data.Function (on)
42 import Data.Maybe (fromJust)
43 import Text.Printf (printf)
45 import qualified Ganeti.HTools.Container as Container
46 import qualified Ganeti.HTools.Instance as Instance
47 import qualified Ganeti.HTools.Node as Node
49 import Ganeti.HTools.Types
55 This type denotes what request we got from Ganeti and also holds
56 request-specific fields.
60 = Allocate Instance.Instance Int -- ^ A new instance allocation
61 | Relocate Idx Int [Ndx] -- ^ Move an instance to a new
65 -- | A complete request, as received from Ganeti.
66 data Request = Request RqType Node.List Instance.List String
71 -- | Lookups a node into an assoc list.
72 lookupNode :: (Monad m) => [(String, Ndx)] -> String -> String -> m Ndx
73 lookupNode ktn inst node =
74 case lookup node ktn of
75 Nothing -> fail $ "Unknown node '" ++ node ++ "' for instance " ++ inst
76 Just idx -> return idx
78 -- | Lookups an instance into an assoc list.
79 lookupInstance :: (Monad m) => [(String, Idx)] -> String -> m Idx
80 lookupInstance kti inst =
81 case lookup inst kti of
82 Nothing -> fail $ "Unknown instance '" ++ inst ++ "'"
83 Just idx -> return idx
85 -- | Given a list of elements (and their names), assign indices to them.
86 assignIndices :: (Element a) =>
88 -> (NameAssoc, [(Int, a)])
90 unzip . map (\ (idx, (k, v)) -> ((k, idx), (idx, setIdx v idx)))
93 -- | Assoc element comparator
94 assocEqual :: (Eq a) => (a, b) -> (a, b) -> Bool
95 assocEqual = (==) `on` fst
97 -- | For each instance, add its index to its primary and secondary nodes.
98 fixNodes :: [(Ndx, Node.Node)]
99 -> (Idx, Instance.Instance)
100 -> [(Ndx, Node.Node)]
101 fixNodes accu (_, inst) =
103 pdx = Instance.pNode inst
104 sdx = Instance.sNode inst
105 pold = fromJust $ lookup pdx accu
106 pnew = Node.setPri pold inst
107 ac1 = deleteBy assocEqual (pdx, pold) accu
108 ac2 = (pdx, pnew):ac1
110 if sdx /= Node.noSecondary
111 then let sold = fromJust $ lookup sdx accu
112 snew = Node.setSec sold inst
113 ac3 = deleteBy assocEqual (sdx, sold) ac2
117 -- | Compute the longest common suffix of a list of strings that
118 -- | starts with a dot.
119 longestDomain :: [String] -> String
120 longestDomain [] = ""
121 longestDomain (x:xs) =
122 foldr (\ suffix accu -> if all (isSuffixOf suffix) xs
125 "" $ filter (isPrefixOf ".") (tails x)
127 -- | Remove tail suffix from a string.
128 stripSuffix :: Int -> String -> String
129 stripSuffix sflen name = take (length name - sflen) name
131 -- | Initializer function that loads the data from a node and instance
132 -- list and massages it into the correct format.
133 mergeData :: (Node.AssocList,
134 Instance.AssocList) -- ^ Data from either Text.loadData
136 -> Result (Node.List, Instance.List, String)
137 mergeData (nl, il) = do
139 nl2 = foldl' fixNodes nl il
140 il3 = Container.fromAssocList il
141 nl3 = Container.fromAssocList
142 (map (\ (k, v) -> (k, Node.buildPeers v il3)) nl2)
143 node_names = map Node.name $ Container.elems nl3
144 inst_names = map Instance.name $ Container.elems il3
145 common_suffix = longestDomain (node_names ++ inst_names)
146 csl = length common_suffix
147 snl = Container.map (\n -> setName n (stripSuffix csl $ nameOf n)) nl3
148 sil = Container.map (\i -> setName i (stripSuffix csl $ nameOf i)) il3
149 return (snl, sil, common_suffix)
151 -- | Checks the cluster data for consistency.
152 checkData :: Node.List -> Instance.List
153 -> ([String], Node.List)
157 let nname = Node.name node
158 nilst = map (flip Container.find il) (Node.pList node)
159 dilst = filter (not . Instance.running) nilst
160 adj_mem = sum . map Instance.mem $ dilst
161 delta_mem = truncate (Node.tMem node)
166 delta_dsk = truncate (Node.tDsk node)
169 newn = Node.setFmem (Node.setXmem node delta_mem)
170 (Node.fMem node - adj_mem)
171 umsg1 = [printf "node %s is missing %d MB ram \
173 nname delta_mem (delta_dsk `div` 1024) |
174 delta_mem > 512 || delta_dsk > 1024]::[String]
175 in (msgs ++ umsg1, newn)
178 -- | Compute the amount of memory used by primary instances on a node.
179 nodeImem :: Node.Node -> Instance.List -> Int
181 let rfind = flip Container.find il
182 in sum . map (Instance.mem . rfind)
185 -- | Compute the amount of disk used by instances on a node (either primary
187 nodeIdsk :: Node.Node -> Instance.List -> Int
189 let rfind = flip Container.find il
190 in sum . map (Instance.dsk . rfind)
191 $ Node.pList node ++ Node.sList node