root / htools / hbal.hs @ e7f7c003
History | View | Annotate | Download (15.3 kB)
1 |
{-| Cluster rebalancer |
---|---|
2 |
|
3 |
-} |
4 |
|
5 |
{- |
6 |
|
7 |
Copyright (C) 2009, 2010, 2011 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 Main (main) where |
27 |
|
28 |
import Control.Concurrent (threadDelay) |
29 |
import Control.Exception (bracket) |
30 |
import Data.List |
31 |
import Data.Maybe (isJust, isNothing, fromJust) |
32 |
import Data.IORef |
33 |
import Monad |
34 |
import System (exitWith, ExitCode(..)) |
35 |
import System.IO |
36 |
import System.Posix.Process |
37 |
import System.Posix.Signals |
38 |
import qualified System |
39 |
|
40 |
import Text.Printf (printf, hPrintf) |
41 |
|
42 |
import qualified Ganeti.HTools.Container as Container |
43 |
import qualified Ganeti.HTools.Cluster as Cluster |
44 |
import qualified Ganeti.HTools.Group as Group |
45 |
import qualified Ganeti.HTools.Node as Node |
46 |
import qualified Ganeti.HTools.Instance as Instance |
47 |
|
48 |
import Ganeti.HTools.CLI |
49 |
import Ganeti.HTools.ExtLoader |
50 |
import Ganeti.HTools.Utils |
51 |
import Ganeti.HTools.Types |
52 |
import Ganeti.HTools.Loader (ClusterData(..)) |
53 |
|
54 |
import qualified Ganeti.Luxi as L |
55 |
import Ganeti.Jobs |
56 |
|
57 |
-- | Options list and functions |
58 |
options :: [OptType] |
59 |
options = |
60 |
[ oPrintNodes |
61 |
, oPrintInsts |
62 |
, oPrintCommands |
63 |
, oOneline |
64 |
, oDataFile |
65 |
, oEvacMode |
66 |
, oRapiMaster |
67 |
, oLuxiSocket |
68 |
, oExecJobs |
69 |
, oGroup |
70 |
, oMaxSolLength |
71 |
, oVerbose |
72 |
, oQuiet |
73 |
, oOfflineNode |
74 |
, oMinScore |
75 |
, oMaxCpu |
76 |
, oMinDisk |
77 |
, oMinGain |
78 |
, oMinGainLim |
79 |
, oDiskMoves |
80 |
, oInstMoves |
81 |
, oDynuFile |
82 |
, oExTags |
83 |
, oExInst |
84 |
, oSaveCluster |
85 |
, oShowVer |
86 |
, oShowHelp |
87 |
] |
88 |
|
89 |
{- | Start computing the solution at the given depth and recurse until |
90 |
we find a valid solution or we exceed the maximum depth. |
91 |
|
92 |
-} |
93 |
iterateDepth :: Cluster.Table -- ^ The starting table |
94 |
-> Int -- ^ Remaining length |
95 |
-> Bool -- ^ Allow disk moves |
96 |
-> Bool -- ^ Allow instance moves |
97 |
-> Int -- ^ Max node name len |
98 |
-> Int -- ^ Max instance name len |
99 |
-> [MoveJob] -- ^ Current command list |
100 |
-> Bool -- ^ Whether to be silent |
101 |
-> Score -- ^ Score at which to stop |
102 |
-> Score -- ^ Min gain limit |
103 |
-> Score -- ^ Min score gain |
104 |
-> Bool -- ^ Enable evacuation mode |
105 |
-> IO (Cluster.Table, [MoveJob]) -- ^ The resulting table |
106 |
-- and commands |
107 |
iterateDepth ini_tbl max_rounds disk_moves inst_moves nmlen imlen |
108 |
cmd_strs oneline min_score mg_limit min_gain evac_mode = |
109 |
let Cluster.Table ini_nl ini_il _ _ = ini_tbl |
110 |
allowed_next = Cluster.doNextBalance ini_tbl max_rounds min_score |
111 |
m_fin_tbl = if allowed_next |
112 |
then Cluster.tryBalance ini_tbl disk_moves evac_mode |
113 |
mg_limit min_gain |
114 |
else Nothing |
115 |
in |
116 |
case m_fin_tbl of |
117 |
Just fin_tbl -> |
118 |
do |
119 |
let |
120 |
(Cluster.Table _ _ _ fin_plc) = fin_tbl |
121 |
fin_plc_len = length fin_plc |
122 |
cur_plc@(idx, _, _, move, _) = head fin_plc |
123 |
(sol_line, cmds) = Cluster.printSolutionLine ini_nl ini_il |
124 |
nmlen imlen cur_plc fin_plc_len |
125 |
afn = Cluster.involvedNodes ini_il cur_plc |
126 |
upd_cmd_strs = (afn, idx, move, cmds):cmd_strs |
127 |
unless oneline $ do |
128 |
putStrLn sol_line |
129 |
hFlush stdout |
130 |
iterateDepth fin_tbl max_rounds disk_moves inst_moves |
131 |
nmlen imlen upd_cmd_strs oneline min_score |
132 |
mg_limit min_gain evac_mode |
133 |
Nothing -> return (ini_tbl, cmd_strs) |
134 |
|
135 |
-- | Formats the solution for the oneline display |
136 |
formatOneline :: Double -> Int -> Double -> String |
137 |
formatOneline ini_cv plc_len fin_cv = |
138 |
printf "%.8f %d %.8f %8.3f" ini_cv plc_len fin_cv |
139 |
(if fin_cv == 0 then 1 else ini_cv / fin_cv) |
140 |
|
141 |
-- | Polls a set of jobs at a fixed interval until all are finished |
142 |
-- one way or another |
143 |
waitForJobs :: L.Client -> [String] -> IO (Result [JobStatus]) |
144 |
waitForJobs client jids = do |
145 |
sts <- L.queryJobsStatus client jids |
146 |
case sts of |
147 |
Bad x -> return $ Bad x |
148 |
Ok s -> if any (<= JOB_STATUS_RUNNING) s |
149 |
then do |
150 |
-- TODO: replace hardcoded value with a better thing |
151 |
threadDelay (1000000 * 15) |
152 |
waitForJobs client jids |
153 |
else return $ Ok s |
154 |
|
155 |
-- | Check that a set of job statuses is all success |
156 |
checkJobsStatus :: [JobStatus] -> Bool |
157 |
checkJobsStatus = all (== JOB_STATUS_SUCCESS) |
158 |
|
159 |
-- | Wrapper over execJobSet checking for early termination |
160 |
execWrapper :: String -> Node.List |
161 |
-> Instance.List -> IORef Int -> [JobSet] -> IO Bool |
162 |
execWrapper _ _ _ _ [] = return True |
163 |
execWrapper master nl il cref alljss = do |
164 |
cancel <- readIORef cref |
165 |
(if cancel > 0 |
166 |
then do |
167 |
hPrintf stderr "Exiting early due to user request, %d\ |
168 |
\ jobset(s) remaining." (length alljss)::IO () |
169 |
return False |
170 |
else execJobSet master nl il cref alljss) |
171 |
|
172 |
-- | Execute an entire jobset |
173 |
execJobSet :: String -> Node.List |
174 |
-> Instance.List -> IORef Int -> [JobSet] -> IO Bool |
175 |
execJobSet _ _ _ _ [] = return True |
176 |
execJobSet master nl il cref (js:jss) = do |
177 |
-- map from jobset (htools list of positions) to [[opcodes]] |
178 |
let jobs = map (\(_, idx, move, _) -> |
179 |
Cluster.iMoveToJob nl il idx move) js |
180 |
let descr = map (\(_, idx, _, _) -> Container.nameOf il idx) js |
181 |
putStrLn $ "Executing jobset for instances " ++ commaJoin descr |
182 |
jrs <- bracket (L.getClient master) L.closeClient |
183 |
(\client -> do |
184 |
jids <- L.submitManyJobs client jobs |
185 |
case jids of |
186 |
Bad x -> return $ Bad x |
187 |
Ok x -> do |
188 |
putStrLn $ "Got job IDs " ++ commaJoin x |
189 |
waitForJobs client x |
190 |
) |
191 |
(case jrs of |
192 |
Bad x -> do |
193 |
hPutStrLn stderr $ "Cannot compute job status, aborting: " ++ show x |
194 |
return False |
195 |
Ok x -> if checkJobsStatus x |
196 |
then execWrapper master nl il cref jss |
197 |
else do |
198 |
hPutStrLn stderr $ "Not all jobs completed successfully: " ++ |
199 |
show x |
200 |
hPutStrLn stderr "Aborting." |
201 |
return False) |
202 |
|
203 |
-- | Signal handler for graceful termination |
204 |
hangleSigInt :: IORef Int -> IO () |
205 |
hangleSigInt cref = do |
206 |
writeIORef cref 1 |
207 |
putStrLn ("Cancel request registered, will exit at" ++ |
208 |
" the end of the current job set...") |
209 |
|
210 |
-- | Signal handler for immediate termination |
211 |
hangleSigTerm :: IORef Int -> IO () |
212 |
hangleSigTerm cref = do |
213 |
-- update the cref to 2, just for consistency |
214 |
writeIORef cref 2 |
215 |
putStrLn "Double cancel request, exiting now..." |
216 |
exitImmediately $ ExitFailure 2 |
217 |
|
218 |
runJobSet :: String -> Node.List -> Instance.List -> [JobSet] -> IO Bool |
219 |
runJobSet master fin_nl il cmd_jobs = do |
220 |
cref <- newIORef 0 |
221 |
mapM_ (\(hnd, sig) -> installHandler sig (Catch (hnd cref)) Nothing) |
222 |
[(hangleSigTerm, softwareTermination), (hangleSigInt, keyboardSignal)] |
223 |
execWrapper master fin_nl il cref cmd_jobs |
224 |
|
225 |
-- | Main function. |
226 |
main :: IO () |
227 |
main = do |
228 |
cmd_args <- System.getArgs |
229 |
(opts, args) <- parseOpts cmd_args "hbal" options |
230 |
|
231 |
unless (null args) $ do |
232 |
hPutStrLn stderr "Error: this program doesn't take any arguments." |
233 |
exitWith $ ExitFailure 1 |
234 |
|
235 |
let oneline = optOneline opts |
236 |
verbose = optVerbose opts |
237 |
shownodes = optShowNodes opts |
238 |
showinsts = optShowInsts opts |
239 |
|
240 |
ini_cdata@(ClusterData gl fixed_nl ilf ctags) <- loadExternalData opts |
241 |
|
242 |
let offline_names = optOffline opts |
243 |
all_nodes = Container.elems fixed_nl |
244 |
all_names = concatMap allNames all_nodes |
245 |
offline_wrong = filter (`notElem` all_names) offline_names |
246 |
offline_indices = map Node.idx $ |
247 |
filter (\n -> |
248 |
Node.name n `elem` offline_names || |
249 |
Node.alias n `elem` offline_names) |
250 |
all_nodes |
251 |
m_cpu = optMcpu opts |
252 |
m_dsk = optMdsk opts |
253 |
csf = commonSuffix fixed_nl ilf |
254 |
|
255 |
when (length offline_wrong > 0) $ do |
256 |
hPrintf stderr "Wrong node name(s) set as offline: %s\n" |
257 |
(commaJoin offline_wrong) :: IO () |
258 |
exitWith $ ExitFailure 1 |
259 |
|
260 |
let nm = Container.map (\n -> if Node.idx n `elem` offline_indices |
261 |
then Node.setOffline n True |
262 |
else n) fixed_nl |
263 |
nlf = Container.map (flip Node.setMdsk m_dsk . flip Node.setMcpu m_cpu) |
264 |
nm |
265 |
|
266 |
when (not oneline && verbose > 1) $ |
267 |
putStrLn $ "Loaded cluster tags: " ++ intercalate "," ctags |
268 |
|
269 |
when (Container.size ilf == 0) $ do |
270 |
(if oneline then putStrLn $ formatOneline 0 0 0 |
271 |
else printf "Cluster is empty, exiting.\n") |
272 |
exitWith ExitSuccess |
273 |
|
274 |
let split_insts = Cluster.findSplitInstances nlf ilf |
275 |
when (not . null $ split_insts) $ do |
276 |
hPutStrLn stderr "Found instances belonging to multiple node groups:" |
277 |
mapM_ (\i -> hPutStrLn stderr $ " " ++ Instance.name i) split_insts |
278 |
hPutStrLn stderr "Aborting." |
279 |
exitWith $ ExitFailure 1 |
280 |
|
281 |
let ngroups = Cluster.splitCluster nlf ilf |
282 |
when (length ngroups > 1 && isNothing (optGroup opts)) $ do |
283 |
hPutStrLn stderr "Found multiple node groups:" |
284 |
mapM_ (hPutStrLn stderr . (" " ++) . Group.name . |
285 |
flip Container.find gl . fst) ngroups |
286 |
hPutStrLn stderr "Aborting." |
287 |
exitWith $ ExitFailure 1 |
288 |
|
289 |
maybeSaveData (optSaveCluster opts) "original" "before balancing" ini_cdata |
290 |
|
291 |
unless oneline $ printf "Loaded %d nodes, %d instances\n" |
292 |
(Container.size nlf) |
293 |
(Container.size ilf) |
294 |
|
295 |
(gname, (nl, il)) <- case optGroup opts of |
296 |
Nothing -> do |
297 |
let (gidx, cdata) = head ngroups |
298 |
grp = Container.find gidx gl |
299 |
return (Group.name grp, cdata) |
300 |
Just g -> case Container.findByName gl g of |
301 |
Nothing -> do |
302 |
hPutStrLn stderr $ "Node group " ++ g ++ |
303 |
" not found. Node group list is:" |
304 |
mapM_ (hPutStrLn stderr . (" " ++) . Group.name ) (Container.elems gl) |
305 |
hPutStrLn stderr "Aborting." |
306 |
exitWith $ ExitFailure 1 |
307 |
Just grp -> |
308 |
case lookup (Group.idx grp) ngroups of |
309 |
Nothing -> do |
310 |
-- TODO: while this is unlikely to happen, log here the |
311 |
-- actual group data to help debugging |
312 |
hPutStrLn stderr "Internal failure, missing group idx" |
313 |
exitWith $ ExitFailure 1 |
314 |
Just cdata -> return (Group.name grp, cdata) |
315 |
|
316 |
unless oneline $ printf "Group size %d nodes, %d instances\n" |
317 |
(Container.size nl) |
318 |
(Container.size il) |
319 |
|
320 |
putStrLn $ "Selected node group: " ++ gname |
321 |
|
322 |
when (length csf > 0 && not oneline && verbose > 1) $ |
323 |
printf "Note: Stripping common suffix of '%s' from names\n" csf |
324 |
|
325 |
let (bad_nodes, bad_instances) = Cluster.computeBadItems nl il |
326 |
unless (oneline || verbose == 0) $ printf |
327 |
"Initial check done: %d bad nodes, %d bad instances.\n" |
328 |
(length bad_nodes) (length bad_instances) |
329 |
|
330 |
when (length bad_nodes > 0) $ |
331 |
putStrLn "Cluster is not N+1 happy, continuing but no guarantee \ |
332 |
\that the cluster will end N+1 happy." |
333 |
|
334 |
maybePrintInsts showinsts "Initial" (Cluster.printInsts nl il) |
335 |
|
336 |
maybePrintNodes shownodes "Initial cluster" (Cluster.printNodes nl) |
337 |
|
338 |
let ini_cv = Cluster.compCV nl |
339 |
ini_tbl = Cluster.Table nl il ini_cv [] |
340 |
min_cv = optMinScore opts |
341 |
|
342 |
when (ini_cv < min_cv) $ do |
343 |
(if oneline then |
344 |
putStrLn $ formatOneline ini_cv 0 ini_cv |
345 |
else printf "Cluster is already well balanced (initial score %.6g,\n\ |
346 |
\minimum score %.6g).\nNothing to do, exiting\n" |
347 |
ini_cv min_cv) |
348 |
exitWith ExitSuccess |
349 |
|
350 |
unless oneline (if verbose > 2 then |
351 |
printf "Initial coefficients: overall %.8f, %s\n" |
352 |
ini_cv (Cluster.printStats nl) |
353 |
else |
354 |
printf "Initial score: %.8f\n" ini_cv) |
355 |
|
356 |
unless oneline $ putStrLn "Trying to minimize the CV..." |
357 |
let imlen = maximum . map (length . Instance.alias) $ Container.elems il |
358 |
nmlen = maximum . map (length . Node.alias) $ Container.elems nl |
359 |
|
360 |
(fin_tbl, cmd_strs) <- iterateDepth ini_tbl (optMaxLength opts) |
361 |
(optDiskMoves opts) |
362 |
(optInstMoves opts) |
363 |
nmlen imlen [] oneline min_cv |
364 |
(optMinGainLim opts) (optMinGain opts) |
365 |
(optEvacMode opts) |
366 |
let (Cluster.Table fin_nl fin_il fin_cv fin_plc) = fin_tbl |
367 |
ord_plc = reverse fin_plc |
368 |
sol_msg = if null fin_plc |
369 |
then printf "No solution found\n" |
370 |
else if verbose > 2 |
371 |
then printf "Final coefficients: overall %.8f, %s\n" |
372 |
fin_cv (Cluster.printStats fin_nl) |
373 |
else printf "Cluster score improved from %.8f to %.8f\n" |
374 |
ini_cv fin_cv |
375 |
::String |
376 |
|
377 |
unless oneline $ putStr sol_msg |
378 |
|
379 |
unless (oneline || verbose == 0) $ |
380 |
printf "Solution length=%d\n" (length ord_plc) |
381 |
|
382 |
let cmd_jobs = Cluster.splitJobs cmd_strs |
383 |
cmd_data = Cluster.formatCmds cmd_jobs |
384 |
|
385 |
when (isJust $ optShowCmds opts) $ |
386 |
do |
387 |
let out_path = fromJust $ optShowCmds opts |
388 |
putStrLn "" |
389 |
(if out_path == "-" then |
390 |
printf "Commands to run to reach the above solution:\n%s" |
391 |
(unlines . map (" " ++) . |
392 |
filter (/= " check") . |
393 |
lines $ cmd_data) |
394 |
else do |
395 |
writeFile out_path (shTemplate ++ cmd_data) |
396 |
printf "The commands have been written to file '%s'\n" out_path) |
397 |
|
398 |
maybeSaveData (optSaveCluster opts) "balanced" "after balancing" |
399 |
(ClusterData gl fin_nl fin_il ctags) |
400 |
|
401 |
maybePrintInsts showinsts "Final" (Cluster.printInsts fin_nl fin_il) |
402 |
|
403 |
maybePrintNodes shownodes "Final cluster" (Cluster.printNodes fin_nl) |
404 |
|
405 |
when (verbose > 3) $ do |
406 |
let ini_cs = Cluster.totalResources nl |
407 |
fin_cs = Cluster.totalResources fin_nl |
408 |
printf "Original: mem=%d disk=%d\n" |
409 |
(Cluster.csFmem ini_cs) (Cluster.csFdsk ini_cs) :: IO () |
410 |
printf "Final: mem=%d disk=%d\n" |
411 |
(Cluster.csFmem fin_cs) (Cluster.csFdsk fin_cs) |
412 |
when oneline $ |
413 |
putStrLn $ formatOneline ini_cv (length ord_plc) fin_cv |
414 |
|
415 |
eval <- |
416 |
if optExecJobs opts && not (null ord_plc) |
417 |
then (case optLuxi opts of |
418 |
Nothing -> do |
419 |
hPutStrLn stderr "Execution of commands possible only on LUXI" |
420 |
return False |
421 |
Just master -> runJobSet master fin_nl il cmd_jobs) |
422 |
else return True |
423 |
unless eval (exitWith (ExitFailure 1)) |