7 Copyright (C) 2009 Google Inc.
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.
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.
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
26 module Main (main) where
28 import Control.Concurrent (threadDelay)
29 import Control.Exception (bracket)
31 import Data.Maybe (isJust, fromJust)
33 import System (exitWith, ExitCode(..))
35 import qualified System
37 import Text.Printf (printf, hPrintf)
39 import qualified Ganeti.HTools.Container as Container
40 import qualified Ganeti.HTools.Cluster as Cluster
41 import qualified Ganeti.HTools.Node as Node
42 import qualified Ganeti.HTools.Instance as Instance
44 import Ganeti.HTools.CLI
45 import Ganeti.HTools.ExtLoader
46 import Ganeti.HTools.Utils
47 import Ganeti.HTools.Types
49 import qualified Ganeti.Luxi as L
52 -- | Options list and functions
79 {- | Start computing the solution at the given depth and recurse until
80 we find a valid solution or we exceed the maximum depth.
83 iterateDepth :: Cluster.Table -- ^ The starting table
84 -> Int -- ^ Remaining length
85 -> Bool -- ^ Allow disk moves
86 -> Int -- ^ Max node name len
87 -> Int -- ^ Max instance name len
88 -> [MoveJob] -- ^ Current command list
89 -> Bool -- ^ Whether to be silent
90 -> Score -- ^ Score at which to stop
91 -> Bool -- ^ Enable evacuation mode
92 -> IO (Cluster.Table, [MoveJob]) -- ^ The resulting table
94 iterateDepth ini_tbl max_rounds disk_moves nmlen imlen
95 cmd_strs oneline min_score evac_mode =
96 let Cluster.Table ini_nl ini_il _ _ = ini_tbl
97 allowed_next = Cluster.doNextBalance ini_tbl max_rounds min_score
98 m_fin_tbl = if allowed_next
99 then Cluster.tryBalance ini_tbl disk_moves evac_mode
106 (Cluster.Table _ _ _ fin_plc) = fin_tbl
107 fin_plc_len = length fin_plc
108 cur_plc@(idx, _, _, move, _) = head fin_plc
109 (sol_line, cmds) = Cluster.printSolutionLine ini_nl ini_il
110 nmlen imlen cur_plc fin_plc_len
111 afn = Cluster.involvedNodes ini_il cur_plc
112 upd_cmd_strs = (afn, idx, move, cmds):cmd_strs
116 iterateDepth fin_tbl max_rounds disk_moves
117 nmlen imlen upd_cmd_strs oneline min_score
119 Nothing -> return (ini_tbl, cmd_strs)
121 -- | Formats the solution for the oneline display
122 formatOneline :: Double -> Int -> Double -> String
123 formatOneline ini_cv plc_len fin_cv =
124 printf "%.8f %d %.8f %8.3f" ini_cv plc_len fin_cv
125 (if fin_cv == 0 then 1 else ini_cv / fin_cv)
127 -- | Polls a set of jobs at a fixed interval until all are finished
128 -- one way or another
129 waitForJobs :: L.Client -> [String] -> IO (Result [JobStatus])
130 waitForJobs client jids = do
131 sts <- L.queryJobsStatus client jids
133 Bad x -> return $ Bad x
134 Ok s -> if any (<= JOB_STATUS_RUNNING) s
136 -- TODO: replace hardcoded value with a better thing
137 threadDelay (1000000 * 15)
138 waitForJobs client jids
141 -- | Check that a set of job statuses is all success
142 checkJobsStatus :: [JobStatus] -> Bool
143 checkJobsStatus = all (== JOB_STATUS_SUCCESS)
145 -- | Execute an entire jobset
146 execJobSet :: String -> Node.List
147 -> Instance.List -> [JobSet] -> IO ()
148 execJobSet _ _ _ [] = return ()
149 execJobSet master nl il (js:jss) = do
150 -- map from jobset (htools list of positions) to [[opcodes]]
151 let jobs = map (\(_, idx, move, _) ->
152 Cluster.iMoveToJob nl il idx move) js
153 let descr = map (\(_, idx, _, _) -> Container.nameOf il idx) js
154 putStrLn $ "Executing jobset for instances " ++ commaJoin descr
155 jrs <- bracket (L.getClient master) L.closeClient
157 jids <- L.submitManyJobs client jobs
159 Bad x -> return $ Bad x
161 putStrLn $ "Got job IDs " ++ commaJoin x
166 hPutStrLn stderr $ "Cannot compute job status, aborting: " ++ show x
168 Ok x -> if checkJobsStatus x
169 then execJobSet master nl il jss
171 hPutStrLn stderr $ "Not all jobs completed successfully: " ++
173 hPutStrLn stderr "Aborting.")
178 cmd_args <- System.getArgs
179 (opts, args) <- parseOpts cmd_args "hbal" options
181 unless (null args) $ do
182 hPutStrLn stderr "Error: this program doesn't take any arguments."
183 exitWith $ ExitFailure 1
185 let oneline = optOneline opts
186 verbose = optVerbose opts
187 shownodes = optShowNodes opts
189 (fixed_nl, il, ctags) <- loadExternalData opts
191 let offline_names = optOffline opts
192 all_nodes = Container.elems fixed_nl
193 all_names = concatMap allNames all_nodes
194 offline_wrong = filter (`notElem` all_names) offline_names
195 offline_indices = map Node.idx $
197 Node.name n `elem` offline_names ||
198 Node.alias n `elem` offline_names)
202 csf = commonSuffix fixed_nl il
204 when (length offline_wrong > 0) $ do
205 hPrintf stderr "Wrong node name(s) set as offline: %s\n"
206 (commaJoin offline_wrong) :: IO ()
207 exitWith $ ExitFailure 1
209 let nm = Container.map (\n -> if Node.idx n `elem` offline_indices
210 then Node.setOffline n True
212 nl = Container.map (flip Node.setMdsk m_dsk . flip Node.setMcpu m_cpu)
215 when (not oneline && verbose > 1) $
216 putStrLn $ "Loaded cluster tags: " ++ intercalate "," ctags
218 when (Container.size il == 0) $ do
219 (if oneline then putStrLn $ formatOneline 0 0 0
220 else printf "Cluster is empty, exiting.\n")
223 unless oneline $ printf "Loaded %d nodes, %d instances\n"
227 when (length csf > 0 && not oneline && verbose > 1) $
228 printf "Note: Stripping common suffix of '%s' from names\n" csf
230 let (bad_nodes, bad_instances) = Cluster.computeBadItems nl il
231 unless (oneline || verbose == 0) $ printf
232 "Initial check done: %d bad nodes, %d bad instances.\n"
233 (length bad_nodes) (length bad_instances)
235 when (length bad_nodes > 0) $
236 putStrLn "Cluster is not N+1 happy, continuing but no guarantee \
237 \that the cluster will end N+1 happy."
239 when (optShowInsts opts) $ do
241 putStrLn "Initial instance map:"
242 putStrLn $ Cluster.printInsts nl il
244 when (isJust shownodes) $
246 putStrLn "Initial cluster status:"
247 putStrLn $ Cluster.printNodes nl (fromJust shownodes)
249 let ini_cv = Cluster.compCV nl
250 ini_tbl = Cluster.Table nl il ini_cv []
251 min_cv = optMinScore opts
253 when (ini_cv < min_cv) $ do
255 putStrLn $ formatOneline ini_cv 0 ini_cv
256 else printf "Cluster is already well balanced (initial score %.6g,\n\
257 \minimum score %.6g).\nNothing to do, exiting\n"
261 unless oneline (if verbose > 2 then
262 printf "Initial coefficients: overall %.8f, %s\n"
263 ini_cv (Cluster.printStats nl)
265 printf "Initial score: %.8f\n" ini_cv)
267 unless oneline $ putStrLn "Trying to minimize the CV..."
268 let imlen = maximum . map (length . Instance.alias) $ Container.elems il
269 nmlen = maximum . map (length . Node.alias) $ Container.elems nl
271 (fin_tbl, cmd_strs) <- iterateDepth ini_tbl (optMaxLength opts)
273 nmlen imlen [] oneline min_cv (optEvacMode opts)
274 let (Cluster.Table fin_nl fin_il fin_cv fin_plc) = fin_tbl
275 ord_plc = reverse fin_plc
276 sol_msg = if null fin_plc
277 then printf "No solution found\n"
279 then printf "Final coefficients: overall %.8f, %s\n"
280 fin_cv (Cluster.printStats fin_nl)
281 else printf "Cluster score improved from %.8f to %.8f\n"
285 unless oneline $ putStr sol_msg
287 unless (oneline || verbose == 0) $
288 printf "Solution length=%d\n" (length ord_plc)
290 let cmd_jobs = Cluster.splitJobs cmd_strs
291 cmd_data = Cluster.formatCmds cmd_jobs
293 when (isJust $ optShowCmds opts) $
295 let out_path = fromJust $ optShowCmds opts
297 (if out_path == "-" then
298 printf "Commands to run to reach the above solution:\n%s"
299 (unlines . map (" " ++) .
300 filter (/= " check") .
303 writeFile out_path (shTemplate ++ cmd_data)
304 printf "The commands have been written to file '%s'\n" out_path)
306 when (optExecJobs opts && not (null ord_plc))
307 (case optLuxi opts of
309 hPutStrLn stderr "Execution of commands possible only on LUXI"
310 exitWith $ ExitFailure 1
311 Just master -> execJobSet master fin_nl il cmd_jobs)
313 when (optShowInsts opts) $ do
315 putStrLn "Final instance map:"
316 putStr $ Cluster.printInsts fin_nl fin_il
318 when (isJust shownodes) $
320 let ini_cs = Cluster.totalResources nl
321 fin_cs = Cluster.totalResources fin_nl
323 putStrLn "Final cluster status:"
324 putStrLn $ Cluster.printNodes fin_nl (fromJust shownodes)
327 printf "Original: mem=%d disk=%d\n"
328 (Cluster.csFmem ini_cs) (Cluster.csFdsk ini_cs) :: IO ()
329 printf "Final: mem=%d disk=%d\n"
330 (Cluster.csFmem fin_cs) (Cluster.csFdsk fin_cs)
332 putStrLn $ formatOneline ini_cv (length ord_plc) fin_cv