1 {-| Solver for N+1 cluster errors
5 module Main (main) where
12 import System.Console.GetOpt
13 import qualified System
15 import Text.Printf (printf)
17 import qualified Ganeti.HTools.Container as Container
18 import qualified Ganeti.HTools.Cluster as Cluster
19 import qualified Ganeti.HTools.Version as Version
20 import Ganeti.HTools.Rapi
21 import Ganeti.HTools.Utils
23 -- | Command line options structure.
24 data Options = Options
25 { optShowNodes :: Bool -- ^ Whether to show node status
26 , optShowCmds :: Bool -- ^ Whether to show the command list
27 , optOneline :: Bool -- ^ Switch output to a single line
28 , optNodef :: FilePath -- ^ Path to the nodes file
29 , optInstf :: FilePath -- ^ Path to the instances file
30 , optMaxLength :: Int -- ^ Stop after this many steps
31 , optMaster :: String -- ^ Collect data from RAPI
32 , optVerbose :: Int -- ^ Verbosity level
33 , optShowVer :: Bool -- ^ Just show the program version
36 -- | Default values for the command line options.
37 defaultOptions :: Options
38 defaultOptions = Options
39 { optShowNodes = False
43 , optInstf = "instances"
50 -- | Options list and functions
51 options :: [OptDescr (Options -> Options)]
53 [ Option ['p'] ["print-nodes"]
54 (NoArg (\ opts -> opts { optShowNodes = True }))
55 "print the final node list"
56 , Option ['C'] ["print-commands"]
57 (NoArg (\ opts -> opts { optShowCmds = True }))
58 "print the ganeti command list for reaching the solution"
59 , Option ['o'] ["oneline"]
60 (NoArg (\ opts -> opts { optOneline = True }))
61 "print the ganeti command list for reaching the solution"
62 , Option ['n'] ["nodes"]
63 (ReqArg (\ f opts -> opts { optNodef = f }) "FILE")
65 , Option ['i'] ["instances"]
66 (ReqArg (\ f opts -> opts { optInstf = f }) "FILE")
67 "the instance list FILE"
68 , Option ['m'] ["master"]
69 (ReqArg (\ m opts -> opts { optMaster = m }) "ADDRESS")
70 "collect data via RAPI at the given ADDRESS"
71 , Option ['l'] ["max-length"]
72 (ReqArg (\ i opts -> opts { optMaxLength = (read i)::Int }) "N")
73 "cap the solution at this many moves (useful for very unbalanced \
75 , Option ['v'] ["verbose"]
76 (NoArg (\ opts -> let nv = (optVerbose opts)
77 in opts { optVerbose = nv + 1 }))
78 "increase the verbosity level"
79 , Option ['V'] ["version"]
80 (NoArg (\ opts -> opts { optShowVer = True}))
81 "show the version of the program"
84 -- | Command line parser, using the 'options' structure.
85 parseOpts :: [String] -> IO (Options, [String])
87 case getOpt Permute options argv of
89 return (foldl (flip id) defaultOptions o, n)
91 ioError (userError (concat errs ++ usageInfo header options))
92 where header = printf "hbal %s\nUsage: hbal [OPTION...]"
95 {- | Start computing the solution at the given depth and recurse until
96 we find a valid solution or we exceed the maximum depth.
99 iterateDepth :: Cluster.Table -- ^ The starting table
100 -> Int -- ^ Remaining length
101 -> [(Int, String)] -- ^ Node idx to name list
102 -> [(Int, String)] -- ^ Inst idx to name list
103 -> Int -- ^ Max node name len
104 -> Int -- ^ Max instance name len
105 -> [[String]] -- ^ Current command list
106 -> Bool -- ^ Wheter to be silent
107 -> IO (Cluster.Table, [[String]]) -- ^ The resulting table and
109 iterateDepth ini_tbl max_rounds ktn kti nmlen imlen cmd_strs oneline =
110 let Cluster.Table ini_nl ini_il ini_cv ini_plc = ini_tbl
111 all_inst = Container.elems ini_il
112 node_idx = Container.keys ini_nl
113 fin_tbl = Cluster.checkMove node_idx ini_tbl all_inst
114 (Cluster.Table _ _ fin_cv fin_plc) = fin_tbl
115 ini_plc_len = length ini_plc
116 fin_plc_len = length fin_plc
117 allowed_next = (max_rounds < 0 || length fin_plc < max_rounds)
121 (sol_line, cmds) = Cluster.printSolutionLine ini_il ktn kti
122 nmlen imlen (head fin_plc) fin_plc_len
123 upd_cmd_strs = cmds:cmd_strs
124 unless (oneline || fin_plc_len == ini_plc_len) $ do
127 (if fin_cv < ini_cv then -- this round made success, try deeper
129 then iterateDepth fin_tbl max_rounds ktn kti
130 nmlen imlen upd_cmd_strs oneline
131 -- don't go deeper, but return the better solution
132 else return (fin_tbl, upd_cmd_strs)
134 return (ini_tbl, cmd_strs))
139 cmd_args <- System.getArgs
140 (opts, _) <- parseOpts cmd_args
142 when (optShowVer opts) $ do
143 putStr $ showVersion "hbal"
146 let oneline = optOneline opts
147 verbose = optVerbose opts
148 (node_data, inst_data) =
149 case optMaster opts of
150 "" -> (readFile $ optNodef opts,
151 readFile $ optInstf opts)
152 host -> (readData getNodes host,
153 readData getInstances host)
155 (nl, il, csf, ktn, kti) <- liftM2 Cluster.loadData node_data inst_data
157 unless oneline $ printf "Loaded %d nodes, %d instances\n"
161 when (length csf > 0 && not oneline && verbose > 0) $ do
162 printf "Note: Stripping common suffix of '%s' from names\n" csf
164 let (bad_nodes, bad_instances) = Cluster.computeBadItems nl il
165 unless (oneline || verbose == 0) $ printf
166 "Initial check done: %d bad nodes, %d bad instances.\n"
167 (length bad_nodes) (length bad_instances)
169 when (length bad_nodes > 0) $ do
170 putStrLn "Cluster is not N+1 happy, continuing but no guarantee \
171 \that the cluster will end N+1 happy."
173 when (optShowNodes opts) $
175 putStrLn "Initial cluster status:"
176 putStrLn $ Cluster.printNodes ktn nl
178 let ini_cv = Cluster.compCV nl
179 ini_tbl = Cluster.Table nl il ini_cv []
180 unless oneline (if verbose > 1 then
181 printf "Initial coefficients: overall %.8f, %s\n"
182 ini_cv (Cluster.printStats nl)
184 printf "Initial score: %.8f\n" ini_cv)
186 unless oneline $ putStrLn "Trying to minimize the CV..."
187 let mlen_fn = maximum . (map length) . snd . unzip
191 (fin_tbl, cmd_strs) <- iterateDepth ini_tbl (optMaxLength opts)
192 ktn kti nmlen imlen [] oneline
193 let (Cluster.Table fin_nl _ fin_cv fin_plc) = fin_tbl
194 ord_plc = reverse fin_plc
195 sol_msg = if null fin_plc
196 then printf "No solution found\n"
198 then printf "Final coefficients: overall %.8f, %s\n"
199 fin_cv (Cluster.printStats fin_nl)
200 else printf "Cluster score improved from %.8f to %.8f\n"
204 unless oneline $ putStr sol_msg
206 unless (oneline || verbose == 0) $
207 printf "Solution length=%d\n" (length ord_plc)
209 when (optShowCmds opts) $
212 putStrLn "Commands to run to reach the above solution:"
213 putStr . Cluster.formatCmds . reverse $ cmd_strs
214 when (optShowNodes opts) $
216 let (orig_mem, orig_disk) = Cluster.totalResources nl
217 (final_mem, final_disk) = Cluster.totalResources fin_nl
219 putStrLn "Final cluster status:"
220 putStrLn $ Cluster.printNodes ktn fin_nl
223 printf "Original: mem=%d disk=%d\n" orig_mem orig_disk
224 printf "Final: mem=%d disk=%d\n" final_mem final_disk
226 printf "%.8f %d %.8f %8.3f\n"
227 ini_cv (length ord_plc) fin_cv (ini_cv / fin_cv)