Statistics
| Branch: | Tag: | Revision:

root / hail.hs @ 23f9ab76

History | View | Annotate | Download (3.3 kB)

1 585d4420 Iustin Pop
{-| Solver for N+1 cluster errors
2 585d4420 Iustin Pop
3 585d4420 Iustin Pop
-}
4 585d4420 Iustin Pop
5 e2fa2baf Iustin Pop
{-
6 e2fa2baf Iustin Pop
7 e2fa2baf Iustin Pop
Copyright (C) 2009 Google Inc.
8 e2fa2baf Iustin Pop
9 e2fa2baf Iustin Pop
This program is free software; you can redistribute it and/or modify
10 e2fa2baf Iustin Pop
it under the terms of the GNU General Public License as published by
11 e2fa2baf Iustin Pop
the Free Software Foundation; either version 2 of the License, or
12 e2fa2baf Iustin Pop
(at your option) any later version.
13 e2fa2baf Iustin Pop
14 e2fa2baf Iustin Pop
This program is distributed in the hope that it will be useful, but
15 e2fa2baf Iustin Pop
WITHOUT ANY WARRANTY; without even the implied warranty of
16 e2fa2baf Iustin Pop
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 e2fa2baf Iustin Pop
General Public License for more details.
18 e2fa2baf Iustin Pop
19 e2fa2baf Iustin Pop
You should have received a copy of the GNU General Public License
20 e2fa2baf Iustin Pop
along with this program; if not, write to the Free Software
21 e2fa2baf Iustin Pop
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 e2fa2baf Iustin Pop
02110-1301, USA.
23 e2fa2baf Iustin Pop
24 e2fa2baf Iustin Pop
-}
25 e2fa2baf Iustin Pop
26 585d4420 Iustin Pop
module Main (main) where
27 585d4420 Iustin Pop
28 585d4420 Iustin Pop
import Data.List
29 585d4420 Iustin Pop
import Data.Function
30 f3d53161 Iustin Pop
import Data.Maybe (isJust, fromJust)
31 585d4420 Iustin Pop
import Monad
32 585d4420 Iustin Pop
import System
33 585d4420 Iustin Pop
import System.IO
34 585d4420 Iustin Pop
import qualified System
35 585d4420 Iustin Pop
36 585d4420 Iustin Pop
import Text.Printf (printf)
37 585d4420 Iustin Pop
38 585d4420 Iustin Pop
import qualified Ganeti.HTools.Cluster as Cluster
39 585d4420 Iustin Pop
import qualified Ganeti.HTools.Node as Node
40 0427285d Iustin Pop
41 0427285d Iustin Pop
import Ganeti.HTools.CLI
42 585d4420 Iustin Pop
import Ganeti.HTools.IAlloc
43 e4c5beaf Iustin Pop
import Ganeti.HTools.Types
44 19f38ee8 Iustin Pop
import Ganeti.HTools.Loader (RqType(..), Request(..))
45 585d4420 Iustin Pop
46 585d4420 Iustin Pop
-- | Options list and functions
47 0427285d Iustin Pop
options :: [OptType]
48 f3d53161 Iustin Pop
options = [oPrintNodes, oShowVer, oShowHelp]
49 f826c5e0 Iustin Pop
50 478df686 Iustin Pop
processResults :: (Monad m) => Cluster.AllocSolution -> m (String, [Node.Node])
51 fbb95f28 Iustin Pop
processResults (fstats, successes, sols) =
52 478df686 Iustin Pop
    case sols of
53 23f9ab76 Iustin Pop
      [] -> fail "No valid allocation solutions"
54 23f9ab76 Iustin Pop
      (best, (_, _, w)):[] ->
55 478df686 Iustin Pop
          let tfails = length fstats
56 478df686 Iustin Pop
              info = printf "successes %d, failures %d,\
57 478df686 Iustin Pop
                            \ best score: %.8f for node(s) %s"
58 fbb95f28 Iustin Pop
                            successes tfails
59 478df686 Iustin Pop
                            best (intercalate "/" . map Node.name $ w)::String
60 478df686 Iustin Pop
          in return (info, w)
61 23f9ab76 Iustin Pop
      _ -> fail "Internal error: multiple allocation solutions"
62 585d4420 Iustin Pop
63 78694255 Iustin Pop
-- | Process a request and return new node lists
64 f2280553 Iustin Pop
processRequest :: Request
65 478df686 Iustin Pop
               -> Result Cluster.AllocSolution
66 78694255 Iustin Pop
processRequest request =
67 669ea132 Iustin Pop
  let Request rqtype nl il _ _ = request
68 78694255 Iustin Pop
  in case rqtype of
69 78694255 Iustin Pop
       Allocate xi reqn -> Cluster.tryAlloc nl il xi reqn
70 78694255 Iustin Pop
       Relocate idx reqn exnodes -> Cluster.tryReloc nl il idx reqn exnodes
71 78694255 Iustin Pop
72 585d4420 Iustin Pop
-- | Main function.
73 585d4420 Iustin Pop
main :: IO ()
74 585d4420 Iustin Pop
main = do
75 585d4420 Iustin Pop
  cmd_args <- System.getArgs
76 f3d53161 Iustin Pop
  (opts, args) <- parseOpts cmd_args "hail" options
77 585d4420 Iustin Pop
78 585d4420 Iustin Pop
  when (null args) $ do
79 585d4420 Iustin Pop
         hPutStrLn stderr "Error: this program needs an input file."
80 585d4420 Iustin Pop
         exitWith $ ExitFailure 1
81 585d4420 Iustin Pop
82 585d4420 Iustin Pop
  let input_file = head args
83 f3d53161 Iustin Pop
      shownodes = optShowNodes opts
84 585d4420 Iustin Pop
  input_data <- readFile input_file
85 585d4420 Iustin Pop
86 585d4420 Iustin Pop
  request <- case (parseData input_data) of
87 585d4420 Iustin Pop
               Bad err -> do
88 2795466b Iustin Pop
                 hPutStrLn stderr $ "Error: " ++ err
89 585d4420 Iustin Pop
                 exitWith $ ExitFailure 1
90 585d4420 Iustin Pop
               Ok rq -> return rq
91 585d4420 Iustin Pop
92 f3d53161 Iustin Pop
  let Request _ nl _ _ csf = request
93 f3d53161 Iustin Pop
94 f3d53161 Iustin Pop
  when (isJust shownodes) $ do
95 f3d53161 Iustin Pop
         hPutStrLn stderr "Initial cluster status:"
96 f3d53161 Iustin Pop
         hPutStrLn stderr $ Cluster.printNodes nl (fromJust shownodes)
97 f3d53161 Iustin Pop
98 f3d53161 Iustin Pop
  let sols = processRequest request >>= processResults
99 478df686 Iustin Pop
  let (ok, info, rn) =
100 478df686 Iustin Pop
          case sols of
101 fbb95f28 Iustin Pop
            Ok (ginfo, sn) -> (True, "Request successful: " ++ ginfo,
102 fbb95f28 Iustin Pop
                                   map ((++ csf) . Node.name) sn)
103 478df686 Iustin Pop
            Bad s -> (False, "Request failed: " ++ s, [])
104 ed41c179 Iustin Pop
      resp = formatResponse ok info rn
105 ed41c179 Iustin Pop
  putStrLn resp