root / src / Ganeti / HTools / Program / Hroller.hs @ f2e50930
History | View | Annotate | Download (10.1 kB)
1 |
{-| Cluster rolling maintenance helper. |
---|---|
2 |
|
3 |
-} |
4 |
|
5 |
{- |
6 |
|
7 |
Copyright (C) 2012 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.Program.Hroller |
27 |
( main |
28 |
, options |
29 |
, arguments |
30 |
) where |
31 |
|
32 |
import Control.Applicative |
33 |
import Control.Monad |
34 |
import Data.Function |
35 |
import Data.List |
36 |
import Data.Ord |
37 |
|
38 |
import qualified Data.IntMap as IntMap |
39 |
|
40 |
import qualified Ganeti.HTools.Container as Container |
41 |
import qualified Ganeti.HTools.Node as Node |
42 |
import qualified Ganeti.HTools.Instance as Instance |
43 |
import qualified Ganeti.HTools.Group as Group |
44 |
|
45 |
import Ganeti.BasicTypes |
46 |
import Ganeti.Common |
47 |
import Ganeti.HTools.CLI |
48 |
import Ganeti.HTools.ExtLoader |
49 |
import Ganeti.HTools.Graph |
50 |
import Ganeti.HTools.Loader |
51 |
import Ganeti.HTools.Types |
52 |
import Ganeti.Utils |
53 |
|
54 |
-- | Options list and functions. |
55 |
options :: IO [OptType] |
56 |
options = do |
57 |
luxi <- oLuxiSocket |
58 |
return |
59 |
[ luxi |
60 |
, oRapiMaster |
61 |
, oDataFile |
62 |
, oIAllocSrc |
63 |
, oOfflineNode |
64 |
, oOfflineMaintenance |
65 |
, oVerbose |
66 |
, oQuiet |
67 |
, oNoHeaders |
68 |
, oNodeTags |
69 |
, oSaveCluster |
70 |
, oGroup |
71 |
, oSkipNonRedundant |
72 |
, oIgnoreNonRedundant |
73 |
, oForce |
74 |
, oOneStepOnly |
75 |
] |
76 |
|
77 |
-- | The list of arguments supported by the program. |
78 |
arguments :: [ArgCompletion] |
79 |
arguments = [] |
80 |
|
81 |
-- | Compute the result of moving an instance to a different node. |
82 |
move :: Idx -> Ndx -> (Node.List, Instance.List) |
83 |
-> OpResult (Node.List, Instance.List) |
84 |
move idx new_ndx (nl, il) = do |
85 |
let new_node = Container.find new_ndx nl |
86 |
inst = Container.find idx il |
87 |
old_ndx = Instance.pNode inst |
88 |
old_node = Container.find old_ndx nl |
89 |
new_node' <- Node.addPriEx True new_node inst |
90 |
let old_node' = Node.removePri old_node inst |
91 |
inst' = Instance.setPri inst new_ndx |
92 |
nl' = Container.addTwo old_ndx old_node' new_ndx new_node' nl |
93 |
il' = Container.add idx inst' il |
94 |
return (nl', il') |
95 |
|
96 |
-- | Move an instance to one of the candidate nodes mentioned. |
97 |
locateInstance :: Idx -> [Ndx] -> (Node.List, Instance.List) |
98 |
-> Result (Node.List, Instance.List) |
99 |
locateInstance idx ndxs conf = |
100 |
msum $ map (opToResult . flip (move idx) conf) ndxs |
101 |
|
102 |
-- | Move a list of instances to some of the candidate nodes mentioned. |
103 |
locateInstances :: [Idx] -> [Ndx] -> (Node.List, Instance.List) |
104 |
-> Result (Node.List, Instance.List) |
105 |
locateInstances idxs ndxs conf = |
106 |
foldM (\ cf idx -> locateInstance idx ndxs cf) conf idxs |
107 |
|
108 |
-- | Greedily move the non-redundant instances away from a list of nodes. |
109 |
-- The arguments are the list of nodes to be cleared, a list of nodes the |
110 |
-- instances can be moved to, and an initial configuration. Returned is a |
111 |
-- list of nodes that can be cleared simultaneously and the configuration |
112 |
-- after these nodes are cleared. |
113 |
clearNodes :: [Ndx] -> [Ndx] -> (Node.List, Instance.List) |
114 |
-> Result ([Ndx], (Node.List, Instance.List)) |
115 |
clearNodes [] _ conf = return ([], conf) |
116 |
clearNodes (ndx:ndxs) targets conf@(nl, _) = |
117 |
withFirst `mplus` withoutFirst where |
118 |
withFirst = do |
119 |
let othernodes = delete ndx targets |
120 |
grp = Node.group $ Container.find ndx nl |
121 |
othernodesSameGroup = |
122 |
filter ((==) grp . Node.group . flip Container.find nl) othernodes |
123 |
conf' <- locateInstances (nonRedundant conf ndx) othernodesSameGroup conf |
124 |
(ndxs', conf'') <- clearNodes ndxs othernodes conf' |
125 |
return (ndx:ndxs', conf'') |
126 |
withoutFirst = clearNodes ndxs targets conf |
127 |
|
128 |
-- | Parition a list of nodes into chunks according cluster capacity. |
129 |
partitionNonRedundant :: [Ndx] -> [Ndx] -> (Node.List, Instance.List) |
130 |
-> Result [[Ndx]] |
131 |
partitionNonRedundant [] _ _ = return [] |
132 |
partitionNonRedundant ndxs targets conf = do |
133 |
(grp, _) <- clearNodes ndxs targets conf |
134 |
guard . not . null $ grp |
135 |
let remaining = ndxs \\ grp |
136 |
part <- partitionNonRedundant remaining targets conf |
137 |
return $ grp : part |
138 |
|
139 |
-- | Gather statistics for the coloring algorithms. |
140 |
-- Returns a string with a summary on how each algorithm has performed, |
141 |
-- in order of non-decreasing effectiveness, and whether it tied or lost |
142 |
-- with the previous one. |
143 |
getStats :: [(String, ColorVertMap)] -> String |
144 |
getStats colorings = snd . foldr helper (0,"") $ algBySize colorings |
145 |
where algostat (algo, cmap) = algo ++ ": " ++ size cmap ++ grpsizes cmap |
146 |
size cmap = show (IntMap.size cmap) ++ " " |
147 |
grpsizes cmap = |
148 |
"(" ++ commaJoin (map (show.length) (IntMap.elems cmap)) ++ ")" |
149 |
algBySize = sortBy (flip (comparing (IntMap.size.snd))) |
150 |
helper :: (String, ColorVertMap) -> (Int, String) -> (Int, String) |
151 |
helper el (0, _) = ((IntMap.size.snd) el, algostat el) |
152 |
helper el (old, str) |
153 |
| old == elsize = (elsize, str ++ " TIE " ++ algostat el) |
154 |
| otherwise = (elsize, str ++ " LOOSE " ++ algostat el) |
155 |
where elsize = (IntMap.size.snd) el |
156 |
|
157 |
-- | Predicate of belonging to a given group restriction. |
158 |
hasGroup :: Maybe Group.Group -> Node.Node -> Bool |
159 |
hasGroup Nothing _ = True |
160 |
hasGroup (Just grp) node = Node.group node == Group.idx grp |
161 |
|
162 |
-- | Predicate of having at least one tag in a given set. |
163 |
hasTag :: Maybe [String] -> Node.Node -> Bool |
164 |
hasTag Nothing _ = True |
165 |
hasTag (Just tags) node = not . null $ Node.nTags node `intersect` tags |
166 |
|
167 |
-- | From a cluster configuration, get the list of non-redundant instances |
168 |
-- of a node. |
169 |
nonRedundant :: (Node.List, Instance.List) -> Ndx -> [Idx] |
170 |
nonRedundant (nl, il) ndx = |
171 |
filter (not . Instance.hasSecondary . flip Container.find il) $ |
172 |
Node.pList (Container.find ndx nl) |
173 |
|
174 |
-- | Within a cluster configuration, decide if the node hosts non-redundant |
175 |
-- Instances. |
176 |
noNonRedundant :: (Node.List, Instance.List) -> Node.Node -> Bool |
177 |
noNonRedundant conf = null . nonRedundant conf . Node.idx |
178 |
|
179 |
-- | Put the master node last. |
180 |
-- Reorder a list of lists of nodes such that the master node (if present) |
181 |
-- is the last node of the last group. |
182 |
masterLast :: [[Node.Node]] -> [[Node.Node]] |
183 |
masterLast rebootgroups = |
184 |
map (uncurry (++)) . uncurry (++) . partition (null . snd) $ |
185 |
map (partition (not . Node.isMaster)) rebootgroups |
186 |
|
187 |
-- | Main function. |
188 |
main :: Options -> [String] -> IO () |
189 |
main opts args = do |
190 |
unless (null args) $ exitErr "This program doesn't take any arguments." |
191 |
|
192 |
let verbose = optVerbose opts |
193 |
maybeExit = if optForce opts then warn else exitErr |
194 |
|
195 |
-- Load cluster data. The last two arguments, cluster tags and ipolicy, are |
196 |
-- currently not used by this tool. |
197 |
ini_cdata@(ClusterData gl fixed_nl ilf _ _) <- loadExternalData opts |
198 |
|
199 |
let master_names = map Node.name . filter Node.isMaster . IntMap.elems $ |
200 |
fixed_nl |
201 |
case master_names of |
202 |
[] -> maybeExit "No master node found (maybe not supported by backend)." |
203 |
[ _ ] -> return () |
204 |
_ -> exitErr $ "Found more than one master node: " ++ show master_names |
205 |
|
206 |
nlf <- setNodeStatus opts fixed_nl |
207 |
|
208 |
maybeSaveData (optSaveCluster opts) "original" "before hroller run" ini_cdata |
209 |
|
210 |
-- Find the wanted node group, if any. |
211 |
wantedGroup <- case optGroup opts of |
212 |
Nothing -> return Nothing |
213 |
Just name -> case Container.findByName gl name of |
214 |
Nothing -> exitErr "Cannot find target group." |
215 |
Just grp -> return (Just grp) |
216 |
|
217 |
let nodes = IntMap.filter (foldl (liftA2 (&&)) (const True) |
218 |
[ not . Node.offline |
219 |
, if optSkipNonRedundant opts |
220 |
then noNonRedundant (nlf, ilf) |
221 |
else const True |
222 |
, hasTag $ optNodeTags opts |
223 |
, hasGroup wantedGroup ]) |
224 |
nlf |
225 |
mkGraph = if optOfflineMaintenance opts |
226 |
then Node.mkNodeGraph |
227 |
else Node.mkRebootNodeGraph nlf |
228 |
|
229 |
-- TODO: fail if instances are running (with option to warn only) |
230 |
|
231 |
nodeGraph <- case mkGraph nodes ilf of |
232 |
Nothing -> exitErr "Cannot create node graph" |
233 |
Just g -> return g |
234 |
|
235 |
when (verbose > 2) . putStrLn $ "Node Graph: " ++ show nodeGraph |
236 |
|
237 |
let colorAlgorithms = [ ("LF", colorLF) |
238 |
, ("Dsatur", colorDsatur) |
239 |
, ("Dcolor", colorDcolor) |
240 |
] |
241 |
colorings = map (\(v,a) -> (v,(colorVertMap.a) nodeGraph)) colorAlgorithms |
242 |
smallestColoring = IntMap.elems $ |
243 |
(snd . minimumBy (comparing (IntMap.size . snd))) colorings |
244 |
allNdx = map Node.idx $ Container.elems nlf |
245 |
splitted = mapM (\ grp -> partitionNonRedundant grp allNdx (nlf,ilf)) |
246 |
smallestColoring |
247 |
rebootGroups <- if optIgnoreNonRedundant opts |
248 |
then return smallestColoring |
249 |
else case splitted of |
250 |
Ok splitgroups -> return $ concat splitgroups |
251 |
Bad _ -> exitErr "Not enough capacity to move\ |
252 |
\ non-redundant instances" |
253 |
let idToNode = (`Container.find` nodes) |
254 |
nodesRebootGroups = |
255 |
map (map idToNode . filter (`IntMap.member` nodes)) rebootGroups |
256 |
outputRebootGroups = masterLast . |
257 |
sortBy (flip compare `on` length) $ |
258 |
nodesRebootGroups |
259 |
outputRebootNames = map (map Node.name) outputRebootGroups |
260 |
|
261 |
when (verbose > 1) . putStrLn $ getStats colorings |
262 |
|
263 |
if optOneStepOnly opts |
264 |
then do |
265 |
unless (optNoHeaders opts) $ |
266 |
putStrLn "'First Reboot Group'" |
267 |
case outputRebootNames of |
268 |
[] -> return () |
269 |
y : _ -> mapM_ putStrLn y |
270 |
else do |
271 |
unless (optNoHeaders opts) $ |
272 |
putStrLn "'Node Reboot Groups'" |
273 |
mapM_ (putStrLn . commaJoin) outputRebootNames |