root / hbal.hs @ aa8d2e71
History | View | Annotate | Download (11.3 kB)
1 |
{-| Cluster rebalancer |
---|---|
2 |
|
3 |
-} |
4 |
|
5 |
{- |
6 |
|
7 |
Copyright (C) 2009 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.Function |
32 |
import Data.Maybe (isJust, fromJust) |
33 |
import Monad |
34 |
import System |
35 |
import System.IO |
36 |
import qualified System |
37 |
|
38 |
import Text.Printf (printf, hPrintf) |
39 |
import Text.JSON (showJSON) |
40 |
|
41 |
import qualified Ganeti.HTools.Container as Container |
42 |
import qualified Ganeti.HTools.Cluster as Cluster |
43 |
import qualified Ganeti.HTools.Node as Node |
44 |
import qualified Ganeti.HTools.Instance as Instance |
45 |
|
46 |
import Ganeti.HTools.CLI |
47 |
import Ganeti.HTools.ExtLoader |
48 |
import Ganeti.HTools.Utils |
49 |
import Ganeti.HTools.Types |
50 |
|
51 |
import qualified Ganeti.Luxi as L |
52 |
import qualified Ganeti.OpCodes as OpCodes |
53 |
import Ganeti.Jobs |
54 |
|
55 |
-- | Options list and functions |
56 |
options :: [OptType] |
57 |
options = |
58 |
[ oPrintNodes |
59 |
, oPrintInsts |
60 |
, oPrintCommands |
61 |
, oOneline |
62 |
, oNodeFile |
63 |
, oInstFile |
64 |
, oRapiMaster |
65 |
, oLuxiSocket |
66 |
, oExecJobs |
67 |
, oMaxSolLength |
68 |
, oVerbose |
69 |
, oQuiet |
70 |
, oOfflineNode |
71 |
, oMinScore |
72 |
, oMaxCpu |
73 |
, oMinDisk |
74 |
, oDiskMoves |
75 |
, oDynuFile |
76 |
, oShowVer |
77 |
, oShowHelp |
78 |
] |
79 |
|
80 |
{- | Start computing the solution at the given depth and recurse until |
81 |
we find a valid solution or we exceed the maximum depth. |
82 |
|
83 |
-} |
84 |
iterateDepth :: Cluster.Table -- ^ The starting table |
85 |
-> Int -- ^ Remaining length |
86 |
-> Bool -- ^ Allow disk moves |
87 |
-> Int -- ^ Max node name len |
88 |
-> Int -- ^ Max instance name len |
89 |
-> [MoveJob] -- ^ Current command list |
90 |
-> Bool -- ^ Whether to be silent |
91 |
-> Score -- ^ Score at which to stop |
92 |
-> IO (Cluster.Table, [MoveJob]) -- ^ The resulting table |
93 |
-- and commands |
94 |
iterateDepth ini_tbl max_rounds disk_moves nmlen imlen |
95 |
cmd_strs oneline min_score = |
96 |
let Cluster.Table ini_nl ini_il _ _ = ini_tbl |
97 |
m_fin_tbl = Cluster.tryBalance ini_tbl max_rounds disk_moves min_score |
98 |
in |
99 |
case m_fin_tbl of |
100 |
Just fin_tbl -> |
101 |
do |
102 |
let |
103 |
(Cluster.Table _ _ _ fin_plc) = fin_tbl |
104 |
fin_plc_len = length fin_plc |
105 |
cur_plc@(idx, _, _, move, _) = head fin_plc |
106 |
(sol_line, cmds) = Cluster.printSolutionLine ini_nl ini_il |
107 |
nmlen imlen cur_plc fin_plc_len |
108 |
afn = Cluster.involvedNodes ini_il cur_plc |
109 |
upd_cmd_strs = (afn, idx, move, cmds):cmd_strs |
110 |
unless oneline $ do |
111 |
putStrLn sol_line |
112 |
hFlush stdout |
113 |
iterateDepth fin_tbl max_rounds disk_moves |
114 |
nmlen imlen upd_cmd_strs oneline min_score |
115 |
Nothing -> return (ini_tbl, cmd_strs) |
116 |
|
117 |
-- | Formats the solution for the oneline display |
118 |
formatOneline :: Double -> Int -> Double -> String |
119 |
formatOneline ini_cv plc_len fin_cv = |
120 |
printf "%.8f %d %.8f %8.3f" ini_cv plc_len fin_cv |
121 |
(if fin_cv == 0 then 1 else ini_cv / fin_cv) |
122 |
|
123 |
-- | Submits a list of jobs and waits for all to finish execution |
124 |
execJobs :: L.Client -> [[OpCodes.OpCode]] -> IO (Result [String]) |
125 |
execJobs client = L.submitManyJobs client . showJSON |
126 |
|
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 |
132 |
case sts of |
133 |
Bad x -> return $ Bad x |
134 |
Ok s -> if any (<= JobRunning) s |
135 |
then do |
136 |
-- TODO: replace hardcoded value with a better thing |
137 |
threadDelay (1000000 * 15) |
138 |
waitForJobs client jids |
139 |
else return $ Ok s |
140 |
|
141 |
-- | Check that a set of job statuses is all success |
142 |
checkJobsStatus :: [JobStatus] -> Bool |
143 |
checkJobsStatus = all (== JobSuccess) |
144 |
|
145 |
-- | Execute an entire jobset |
146 |
execJobSet :: String -> String -> Node.List |
147 |
-> Instance.List -> [JobSet] -> IO () |
148 |
execJobSet _ _ _ _ [] = return () |
149 |
execJobSet master csf nl il (js:jss) = do |
150 |
-- map from jobset (htools list of positions) to [[opcodes]] |
151 |
let jobs = map (\(_, idx, move, _) -> |
152 |
Cluster.iMoveToJob csf 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 |
156 |
(\client -> do |
157 |
jids <- execJobs client jobs |
158 |
case jids of |
159 |
Bad x -> return $ Bad x |
160 |
Ok x -> do |
161 |
putStrLn $ "Got job IDs " ++ commaJoin x |
162 |
waitForJobs client x |
163 |
) |
164 |
(case jrs of |
165 |
Bad x -> do |
166 |
hPutStrLn stderr $ "Cannot compute job status, aborting: " ++ show x |
167 |
return () |
168 |
Ok x -> if checkJobsStatus x |
169 |
then execJobSet master csf nl il jss |
170 |
else do |
171 |
hPutStrLn stderr $ "Not all jobs completed successfully: " ++ |
172 |
show x |
173 |
hPutStrLn stderr "Aborting.") |
174 |
|
175 |
-- | Main function. |
176 |
main :: IO () |
177 |
main = do |
178 |
cmd_args <- System.getArgs |
179 |
(opts, args) <- parseOpts cmd_args "hbal" options |
180 |
|
181 |
unless (null args) $ do |
182 |
hPutStrLn stderr "Error: this program doesn't take any arguments." |
183 |
exitWith $ ExitFailure 1 |
184 |
|
185 |
let oneline = optOneline opts |
186 |
verbose = optVerbose opts |
187 |
|
188 |
(fixed_nl, il, csf) <- loadExternalData opts |
189 |
|
190 |
let offline_names = optOffline opts |
191 |
all_nodes = Container.elems fixed_nl |
192 |
all_names = map Node.name all_nodes |
193 |
offline_wrong = filter (flip notElem all_names) offline_names |
194 |
offline_indices = map Node.idx $ |
195 |
filter (\n -> elem (Node.name n) offline_names) |
196 |
all_nodes |
197 |
m_cpu = optMcpu opts |
198 |
m_dsk = optMdsk opts |
199 |
|
200 |
when (length offline_wrong > 0) $ do |
201 |
hPrintf stderr "Wrong node name(s) set as offline: %s\n" |
202 |
(commaJoin offline_wrong) |
203 |
exitWith $ ExitFailure 1 |
204 |
|
205 |
let nm = Container.map (\n -> if elem (Node.idx n) offline_indices |
206 |
then Node.setOffline n True |
207 |
else n) fixed_nl |
208 |
nl = Container.map (flip Node.setMdsk m_dsk . flip Node.setMcpu m_cpu) |
209 |
nm |
210 |
|
211 |
when (Container.size il == 0) $ do |
212 |
(if oneline then putStrLn $ formatOneline 0 0 0 |
213 |
else printf "Cluster is empty, exiting.\n") |
214 |
exitWith ExitSuccess |
215 |
|
216 |
unless oneline $ printf "Loaded %d nodes, %d instances\n" |
217 |
(Container.size nl) |
218 |
(Container.size il) |
219 |
|
220 |
when (length csf > 0 && not oneline && verbose > 1) $ |
221 |
printf "Note: Stripping common suffix of '%s' from names\n" csf |
222 |
|
223 |
let (bad_nodes, bad_instances) = Cluster.computeBadItems nl il |
224 |
unless (oneline || verbose == 0) $ printf |
225 |
"Initial check done: %d bad nodes, %d bad instances.\n" |
226 |
(length bad_nodes) (length bad_instances) |
227 |
|
228 |
when (length bad_nodes > 0) $ |
229 |
putStrLn "Cluster is not N+1 happy, continuing but no guarantee \ |
230 |
\that the cluster will end N+1 happy." |
231 |
|
232 |
when (optShowInsts opts) $ do |
233 |
putStrLn "" |
234 |
putStrLn "Initial instance map:" |
235 |
putStrLn $ Cluster.printInsts nl il |
236 |
|
237 |
when (optShowNodes opts) $ |
238 |
do |
239 |
putStrLn "Initial cluster status:" |
240 |
putStrLn $ Cluster.printNodes nl |
241 |
|
242 |
let ini_cv = Cluster.compCV nl |
243 |
ini_tbl = Cluster.Table nl il ini_cv [] |
244 |
min_cv = optMinScore opts |
245 |
|
246 |
when (ini_cv < min_cv) $ do |
247 |
(if oneline then |
248 |
putStrLn $ formatOneline ini_cv 0 ini_cv |
249 |
else printf "Cluster is already well balanced (initial score %.6g,\n\ |
250 |
\minimum score %.6g).\nNothing to do, exiting\n" |
251 |
ini_cv min_cv) |
252 |
exitWith ExitSuccess |
253 |
|
254 |
unless oneline (if verbose > 2 then |
255 |
printf "Initial coefficients: overall %.8f, %s\n" |
256 |
ini_cv (Cluster.printStats nl) |
257 |
else |
258 |
printf "Initial score: %.8f\n" ini_cv) |
259 |
|
260 |
unless oneline $ putStrLn "Trying to minimize the CV..." |
261 |
let imlen = Container.maxNameLen il |
262 |
nmlen = Container.maxNameLen nl |
263 |
|
264 |
(fin_tbl, cmd_strs) <- iterateDepth ini_tbl (optMaxLength opts) |
265 |
(optDiskMoves opts) |
266 |
nmlen imlen [] oneline min_cv |
267 |
let (Cluster.Table fin_nl fin_il fin_cv fin_plc) = fin_tbl |
268 |
ord_plc = reverse fin_plc |
269 |
sol_msg = if null fin_plc |
270 |
then printf "No solution found\n" |
271 |
else if verbose > 2 |
272 |
then printf "Final coefficients: overall %.8f, %s\n" |
273 |
fin_cv (Cluster.printStats fin_nl) |
274 |
else printf "Cluster score improved from %.8f to %.8f\n" |
275 |
ini_cv fin_cv |
276 |
::String |
277 |
|
278 |
unless oneline $ putStr sol_msg |
279 |
|
280 |
unless (oneline || verbose == 0) $ |
281 |
printf "Solution length=%d\n" (length ord_plc) |
282 |
|
283 |
let cmd_jobs = Cluster.splitJobs cmd_strs |
284 |
cmd_data = Cluster.formatCmds cmd_jobs |
285 |
|
286 |
when (isJust $ optShowCmds opts) $ |
287 |
do |
288 |
let out_path = fromJust $ optShowCmds opts |
289 |
putStrLn "" |
290 |
(if out_path == "-" then |
291 |
printf "Commands to run to reach the above solution:\n%s" |
292 |
(unlines . map (" " ++) . |
293 |
filter (/= " check") . |
294 |
lines $ cmd_data) |
295 |
else do |
296 |
writeFile out_path (shTemplate ++ cmd_data) |
297 |
printf "The commands have been written to file '%s'\n" out_path) |
298 |
|
299 |
when (optExecJobs opts && not (null ord_plc)) |
300 |
(case optLuxi opts of |
301 |
Nothing -> do |
302 |
hPutStrLn stderr "Execution of commands possible only on LUXI" |
303 |
exitWith $ ExitFailure 1 |
304 |
Just master -> execJobSet master csf fin_nl il cmd_jobs) |
305 |
|
306 |
when (optShowInsts opts) $ do |
307 |
putStrLn "" |
308 |
putStrLn "Final instance map:" |
309 |
putStr $ Cluster.printInsts fin_nl fin_il |
310 |
|
311 |
when (optShowNodes opts) $ |
312 |
do |
313 |
let ini_cs = Cluster.totalResources nl |
314 |
fin_cs = Cluster.totalResources fin_nl |
315 |
putStrLn "" |
316 |
putStrLn "Final cluster status:" |
317 |
putStrLn $ Cluster.printNodes fin_nl |
318 |
when (verbose > 3) $ |
319 |
do |
320 |
printf "Original: mem=%d disk=%d\n" |
321 |
(Cluster.csFmem ini_cs) (Cluster.csFdsk ini_cs) |
322 |
printf "Final: mem=%d disk=%d\n" |
323 |
(Cluster.csFmem fin_cs) (Cluster.csFdsk fin_cs) |
324 |
when oneline $ |
325 |
putStrLn $ formatOneline ini_cv (length ord_plc) fin_cv |