root / htools / Ganeti / HTools / Backend / IAlloc.hs @ 29a30533
History | View | Annotate | Download (16.6 kB)
1 | 43643696 | Iustin Pop | {-| Implementation of the iallocator interface. |
---|---|---|---|
2 | 43643696 | Iustin Pop | |
3 | 43643696 | Iustin Pop | -} |
4 | 43643696 | Iustin Pop | |
5 | e2fa2baf | Iustin Pop | {- |
6 | e2fa2baf | Iustin Pop | |
7 | 8bc34c7b | Iustin Pop | Copyright (C) 2009, 2010, 2011, 2012 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 | 879d9290 | Iustin Pop | module Ganeti.HTools.Backend.IAlloc |
27 | 00dd69a2 | Iustin Pop | ( readRequest |
28 | 00dd69a2 | Iustin Pop | , runIAllocator |
29 | 00dd69a2 | Iustin Pop | , processRelocate |
30 | 786c514c | René Nussbaumer | , loadData |
31 | 00dd69a2 | Iustin Pop | ) where |
32 | 43643696 | Iustin Pop | |
33 | 43643696 | Iustin Pop | import Data.Either () |
34 | 2a9aff11 | René Nussbaumer | import Data.Maybe (fromMaybe, isJust, fromJust) |
35 | cabce2f4 | Iustin Pop | import Data.List |
36 | 43643696 | Iustin Pop | import Control.Monad |
37 | 34c5a24a | Iustin Pop | import Text.JSON (JSObject, JSValue(JSArray), |
38 | 34c5a24a | Iustin Pop | makeObj, encodeStrict, decodeStrict, fromJSObject, showJSON) |
39 | cabce2f4 | Iustin Pop | |
40 | 01e52493 | Iustin Pop | import Ganeti.BasicTypes |
41 | cabce2f4 | Iustin Pop | import qualified Ganeti.HTools.Cluster as Cluster |
42 | 262a08a2 | Iustin Pop | import qualified Ganeti.HTools.Container as Container |
43 | a679e9dc | Iustin Pop | import qualified Ganeti.HTools.Group as Group |
44 | 942403e6 | Iustin Pop | import qualified Ganeti.HTools.Node as Node |
45 | 942403e6 | Iustin Pop | import qualified Ganeti.HTools.Instance as Instance |
46 | df5227dc | Iustin Pop | import qualified Ganeti.Constants as C |
47 | cabce2f4 | Iustin Pop | import Ganeti.HTools.CLI |
48 | e4c5beaf | Iustin Pop | import Ganeti.HTools.Loader |
49 | e4c5beaf | Iustin Pop | import Ganeti.HTools.Types |
50 | f3baf5ef | Iustin Pop | import Ganeti.JSON |
51 | 707cd3d7 | Helga Velroyen | import Ganeti.Utils |
52 | 43643696 | Iustin Pop | |
53 | 3603605a | Iustin Pop | {-# ANN module "HLint: ignore Eta reduce" #-} |
54 | 3603605a | Iustin Pop | |
55 | 7c14b50a | Iustin Pop | -- | Type alias for the result of an IAllocator call. |
56 | f9283686 | Iustin Pop | type IAllocResult = (String, JSValue, Node.List, Instance.List) |
57 | 7c14b50a | Iustin Pop | |
58 | 9188aeef | Iustin Pop | -- | Parse the basic specifications of an instance. |
59 | 9188aeef | Iustin Pop | -- |
60 | 9188aeef | Iustin Pop | -- Instances in the cluster instance list and the instance in an |
61 | 9188aeef | Iustin Pop | -- 'Allocate' request share some common properties, which are read by |
62 | 9188aeef | Iustin Pop | -- this function. |
63 | e4c5beaf | Iustin Pop | parseBaseInstance :: String |
64 | 28f19313 | Iustin Pop | -> JSRecord |
65 | e4c5beaf | Iustin Pop | -> Result (String, Instance.Instance) |
66 | e4c5beaf | Iustin Pop | parseBaseInstance n a = do |
67 | e8230242 | Iustin Pop | let extract x = tryFromObj ("invalid data for instance '" ++ n ++ "'") a x |
68 | e8230242 | Iustin Pop | disk <- extract "disk_space_total" |
69 | e8230242 | Iustin Pop | mem <- extract "memory" |
70 | e8230242 | Iustin Pop | vcpus <- extract "vcpus" |
71 | e8230242 | Iustin Pop | tags <- extract "tags" |
72 | 5a4a3b7f | Iustin Pop | dt <- extract "disk_template" |
73 | ec629280 | René Nussbaumer | su <- extract "spindle_use" |
74 | b003b8c0 | René Nussbaumer | return (n, Instance.create n mem disk vcpus Running tags True 0 0 dt su) |
75 | 585d4420 | Iustin Pop | |
76 | 525bfb36 | Iustin Pop | -- | Parses an instance as found in the cluster instance list. |
77 | 28f19313 | Iustin Pop | parseInstance :: NameAssoc -- ^ The node name-to-index association list |
78 | 28f19313 | Iustin Pop | -> String -- ^ The name of the instance |
79 | 28f19313 | Iustin Pop | -> JSRecord -- ^ The JSON object |
80 | e4c5beaf | Iustin Pop | -> Result (String, Instance.Instance) |
81 | e4c5beaf | Iustin Pop | parseInstance ktn n a = do |
82 | 262f3e6c | Iustin Pop | base <- parseBaseInstance n a |
83 | e8230242 | Iustin Pop | nodes <- fromObj a "nodes" |
84 | e41f4ba0 | Iustin Pop | pnode <- if null nodes |
85 | e41f4ba0 | Iustin Pop | then Bad $ "empty node list for instance " ++ n |
86 | e41f4ba0 | Iustin Pop | else readEitherString $ head nodes |
87 | 262f3e6c | Iustin Pop | pidx <- lookupNode ktn n pnode |
88 | 262f3e6c | Iustin Pop | let snodes = tail nodes |
89 | 3603605a | Iustin Pop | sidx <- if null snodes |
90 | 3603605a | Iustin Pop | then return Node.noSecondary |
91 | 3603605a | Iustin Pop | else readEitherString (head snodes) >>= lookupNode ktn n |
92 | 262f3e6c | Iustin Pop | return (n, Instance.setBoth (snd base) pidx sidx) |
93 | 585d4420 | Iustin Pop | |
94 | 9188aeef | Iustin Pop | -- | Parses a node as found in the cluster node list. |
95 | 28f19313 | Iustin Pop | parseNode :: NameAssoc -- ^ The group association |
96 | 28f19313 | Iustin Pop | -> String -- ^ The node's name |
97 | 28f19313 | Iustin Pop | -> JSRecord -- ^ The JSON object |
98 | 9188aeef | Iustin Pop | -> Result (String, Node.Node) |
99 | 10ef6b4e | Iustin Pop | parseNode ktg n a = do |
100 | 3eeea90f | Iustin Pop | let desc = "invalid data for node '" ++ n ++ "'" |
101 | 3eeea90f | Iustin Pop | extract x = tryFromObj desc a x |
102 | e8230242 | Iustin Pop | offline <- extract "offline" |
103 | e8230242 | Iustin Pop | drained <- extract "drained" |
104 | e8230242 | Iustin Pop | guuid <- extract "group" |
105 | 3eeea90f | Iustin Pop | vm_capable <- annotateResult desc $ maybeFromObj a "vm_capable" |
106 | 3eeea90f | Iustin Pop | let vm_capable' = fromMaybe True vm_capable |
107 | 10ef6b4e | Iustin Pop | gidx <- lookupGroup ktg n guuid |
108 | 3603605a | Iustin Pop | node <- if offline || drained || not vm_capable' |
109 | 8bc34c7b | Iustin Pop | then return $ Node.create n 0 0 0 0 0 0 True 0 gidx |
110 | 3603605a | Iustin Pop | else do |
111 | 3603605a | Iustin Pop | mtotal <- extract "total_memory" |
112 | 3603605a | Iustin Pop | mnode <- extract "reserved_memory" |
113 | 3603605a | Iustin Pop | mfree <- extract "free_memory" |
114 | 3603605a | Iustin Pop | dtotal <- extract "total_disk" |
115 | 3603605a | Iustin Pop | dfree <- extract "free_disk" |
116 | 3603605a | Iustin Pop | ctotal <- extract "total_cpus" |
117 | 8bc34c7b | Iustin Pop | ndparams <- extract "ndparams" >>= asJSObject |
118 | 8bc34c7b | Iustin Pop | spindles <- tryFromObj desc (fromJSObject ndparams) |
119 | 8bc34c7b | Iustin Pop | "spindle_count" |
120 | 3603605a | Iustin Pop | return $ Node.create n mtotal mnode mfree |
121 | 8bc34c7b | Iustin Pop | dtotal dfree ctotal False spindles gidx |
122 | 262f3e6c | Iustin Pop | return (n, node) |
123 | 144f190b | Iustin Pop | |
124 | a679e9dc | Iustin Pop | -- | Parses a group as found in the cluster group list. |
125 | 28f19313 | Iustin Pop | parseGroup :: String -- ^ The group UUID |
126 | 28f19313 | Iustin Pop | -> JSRecord -- ^ The JSON object |
127 | a679e9dc | Iustin Pop | -> Result (String, Group.Group) |
128 | a679e9dc | Iustin Pop | parseGroup u a = do |
129 | 1b2cb110 | Iustin Pop | let extract x = tryFromObj ("invalid data for group '" ++ u ++ "'") a x |
130 | 1b2cb110 | Iustin Pop | name <- extract "name" |
131 | 1b2cb110 | Iustin Pop | apol <- extract "alloc_policy" |
132 | 6cff91f5 | Iustin Pop | ipol <- extract "ipolicy" |
133 | 6b6e335b | Dato Simó | tags <- extract "tags" |
134 | 6b6e335b | Dato Simó | return (u, Group.create name u apol ipol tags) |
135 | a679e9dc | Iustin Pop | |
136 | 9188aeef | Iustin Pop | -- | Top-level parser. |
137 | 96a12113 | Iustin Pop | -- |
138 | 96a12113 | Iustin Pop | -- The result is a tuple of eventual warning messages and the parsed |
139 | 96a12113 | Iustin Pop | -- request; if parsing the input data fails, we'll return a 'Bad' |
140 | 96a12113 | Iustin Pop | -- value. |
141 | 96a12113 | Iustin Pop | parseData :: String -- ^ The JSON message as received from Ganeti |
142 | 96a12113 | Iustin Pop | -> Result ([String], Request) -- ^ Result tuple |
143 | e4c5beaf | Iustin Pop | parseData body = do |
144 | c96d44df | Iustin Pop | decoded <- fromJResult "Parsing input IAllocator message" (decodeStrict body) |
145 | 262f3e6c | Iustin Pop | let obj = fromJSObject decoded |
146 | e8230242 | Iustin Pop | extrObj x = tryFromObj "invalid iallocator message" obj x |
147 | e4c5beaf | Iustin Pop | -- request parser |
148 | e8230242 | Iustin Pop | request <- liftM fromJSObject (extrObj "request") |
149 | 2a9aff11 | René Nussbaumer | let extrFromReq r x = tryFromObj "invalid request dict" r x |
150 | 2a9aff11 | René Nussbaumer | let extrReq x = extrFromReq request x |
151 | a679e9dc | Iustin Pop | -- existing group parsing |
152 | e8230242 | Iustin Pop | glist <- liftM fromJSObject (extrObj "nodegroups") |
153 | a679e9dc | Iustin Pop | gobj <- mapM (\(x, y) -> asJSObject y >>= parseGroup x . fromJSObject) glist |
154 | 10ef6b4e | Iustin Pop | let (ktg, gl) = assignIndices gobj |
155 | e4c5beaf | Iustin Pop | -- existing node parsing |
156 | e8230242 | Iustin Pop | nlist <- liftM fromJSObject (extrObj "nodes") |
157 | 10ef6b4e | Iustin Pop | nobj <- mapM (\(x,y) -> |
158 | 10ef6b4e | Iustin Pop | asJSObject y >>= parseNode ktg x . fromJSObject) nlist |
159 | 497e30a1 | Iustin Pop | let (ktn, nl) = assignIndices nobj |
160 | e4c5beaf | Iustin Pop | -- existing instance parsing |
161 | e8230242 | Iustin Pop | ilist <- extrObj "instances" |
162 | e4c5beaf | Iustin Pop | let idata = fromJSObject ilist |
163 | 262f3e6c | Iustin Pop | iobj <- mapM (\(x,y) -> |
164 | 262f3e6c | Iustin Pop | asJSObject y >>= parseInstance ktn x . fromJSObject) idata |
165 | 88df1fa9 | Iustin Pop | let (kti, il) = assignIndices iobj |
166 | 669ea132 | Iustin Pop | -- cluster tags |
167 | e8230242 | Iustin Pop | ctags <- extrObj "cluster_tags" |
168 | 71375ef7 | Iustin Pop | cdata1 <- mergeData [] [] [] [] (ClusterData gl nl il ctags defIPolicy) |
169 | 96a12113 | Iustin Pop | let (msgs, fix_nl) = checkData (cdNodes cdata1) (cdInstances cdata1) |
170 | 96a12113 | Iustin Pop | cdata = cdata1 { cdNodes = fix_nl } |
171 | 88df1fa9 | Iustin Pop | map_n = cdNodes cdata |
172 | 695c1bab | Iustin Pop | map_i = cdInstances cdata |
173 | 695c1bab | Iustin Pop | map_g = cdGroups cdata |
174 | e8230242 | Iustin Pop | optype <- extrReq "type" |
175 | e4c5beaf | Iustin Pop | rqtype <- |
176 | 00dd69a2 | Iustin Pop | case () of |
177 | 00dd69a2 | Iustin Pop | _ | optype == C.iallocatorModeAlloc -> |
178 | 00dd69a2 | Iustin Pop | do |
179 | 00dd69a2 | Iustin Pop | rname <- extrReq "name" |
180 | 00dd69a2 | Iustin Pop | req_nodes <- extrReq "required_nodes" |
181 | 00dd69a2 | Iustin Pop | inew <- parseBaseInstance rname request |
182 | 00dd69a2 | Iustin Pop | let io = snd inew |
183 | 00dd69a2 | Iustin Pop | return $ Allocate io req_nodes |
184 | 00dd69a2 | Iustin Pop | | optype == C.iallocatorModeReloc -> |
185 | 00dd69a2 | Iustin Pop | do |
186 | 00dd69a2 | Iustin Pop | rname <- extrReq "name" |
187 | 00dd69a2 | Iustin Pop | ridx <- lookupInstance kti rname |
188 | 00dd69a2 | Iustin Pop | req_nodes <- extrReq "required_nodes" |
189 | 00dd69a2 | Iustin Pop | ex_nodes <- extrReq "relocate_from" |
190 | 00dd69a2 | Iustin Pop | ex_idex <- mapM (Container.findByName map_n) ex_nodes |
191 | 00dd69a2 | Iustin Pop | return $ Relocate ridx req_nodes (map Node.idx ex_idex) |
192 | 00dd69a2 | Iustin Pop | | optype == C.iallocatorModeChgGroup -> |
193 | 00dd69a2 | Iustin Pop | do |
194 | 00dd69a2 | Iustin Pop | rl_names <- extrReq "instances" |
195 | 00dd69a2 | Iustin Pop | rl_insts <- mapM (liftM Instance.idx . |
196 | 00dd69a2 | Iustin Pop | Container.findByName map_i) rl_names |
197 | 00dd69a2 | Iustin Pop | gr_uuids <- extrReq "target_groups" |
198 | 00dd69a2 | Iustin Pop | gr_idxes <- mapM (liftM Group.idx . |
199 | 00dd69a2 | Iustin Pop | Container.findByName map_g) gr_uuids |
200 | 00dd69a2 | Iustin Pop | return $ ChangeGroup rl_insts gr_idxes |
201 | 00dd69a2 | Iustin Pop | | optype == C.iallocatorModeNodeEvac -> |
202 | 00dd69a2 | Iustin Pop | do |
203 | 00dd69a2 | Iustin Pop | rl_names <- extrReq "instances" |
204 | 00dd69a2 | Iustin Pop | rl_insts <- mapM (Container.findByName map_i) rl_names |
205 | 00dd69a2 | Iustin Pop | let rl_idx = map Instance.idx rl_insts |
206 | 00dd69a2 | Iustin Pop | rl_mode <- extrReq "evac_mode" |
207 | 00dd69a2 | Iustin Pop | return $ NodeEvacuate rl_idx rl_mode |
208 | 2a9aff11 | René Nussbaumer | | optype == C.iallocatorModeMultiAlloc -> |
209 | 2a9aff11 | René Nussbaumer | do |
210 | 2a9aff11 | René Nussbaumer | arry <- extrReq "instances" :: Result [JSObject JSValue] |
211 | 2a9aff11 | René Nussbaumer | let inst_reqs = map fromJSObject arry |
212 | 2a9aff11 | René Nussbaumer | prqs <- mapM (\r -> |
213 | 2a9aff11 | René Nussbaumer | do |
214 | 2a9aff11 | René Nussbaumer | rname <- extrFromReq r "name" |
215 | 2a9aff11 | René Nussbaumer | req_nodes <- extrFromReq r "required_nodes" |
216 | 2a9aff11 | René Nussbaumer | inew <- parseBaseInstance rname r |
217 | 2a9aff11 | René Nussbaumer | let io = snd inew |
218 | 2a9aff11 | René Nussbaumer | return (io, req_nodes)) inst_reqs |
219 | 2a9aff11 | René Nussbaumer | return $ MultiAllocate prqs |
220 | 00dd69a2 | Iustin Pop | | otherwise -> fail ("Invalid request type '" ++ optype ++ "'") |
221 | 1b0a6356 | Iustin Pop | return (msgs, Request rqtype cdata) |
222 | 942403e6 | Iustin Pop | |
223 | d6cf394e | Iustin Pop | -- | Formats the result into a valid IAllocator response message. |
224 | 9188aeef | Iustin Pop | formatResponse :: Bool -- ^ Whether the request was successful |
225 | 9188aeef | Iustin Pop | -> String -- ^ Information text |
226 | d6cf394e | Iustin Pop | -> JSValue -- ^ The JSON encoded result |
227 | d6cf394e | Iustin Pop | -> String -- ^ The full JSON-formatted message |
228 | d6cf394e | Iustin Pop | formatResponse success info result = |
229 | 00dd69a2 | Iustin Pop | let e_success = ("success", showJSON success) |
230 | 00dd69a2 | Iustin Pop | e_info = ("info", showJSON info) |
231 | 00dd69a2 | Iustin Pop | e_result = ("result", result) |
232 | 00dd69a2 | Iustin Pop | in encodeStrict $ makeObj [e_success, e_info, e_result] |
233 | cabce2f4 | Iustin Pop | |
234 | 7c14b50a | Iustin Pop | -- | Flatten the log of a solution into a string. |
235 | 7c14b50a | Iustin Pop | describeSolution :: Cluster.AllocSolution -> String |
236 | 7c14b50a | Iustin Pop | describeSolution = intercalate ", " . Cluster.asLog |
237 | cabce2f4 | Iustin Pop | |
238 | 7c14b50a | Iustin Pop | -- | Convert allocation/relocation results into the result format. |
239 | f9283686 | Iustin Pop | formatAllocate :: Instance.List -> Cluster.AllocSolution -> Result IAllocResult |
240 | f9283686 | Iustin Pop | formatAllocate il as = do |
241 | 7c14b50a | Iustin Pop | let info = describeSolution as |
242 | 129734d3 | Iustin Pop | case Cluster.asSolution as of |
243 | 129734d3 | Iustin Pop | Nothing -> fail info |
244 | 129734d3 | Iustin Pop | Just (nl, inst, nodes, _) -> |
245 | 00dd69a2 | Iustin Pop | do |
246 | 00dd69a2 | Iustin Pop | let il' = Container.add (Instance.idx inst) inst il |
247 | 00dd69a2 | Iustin Pop | return (info, showJSON $ map Node.name nodes, nl, il') |
248 | cabce2f4 | Iustin Pop | |
249 | 2a9aff11 | René Nussbaumer | -- | Convert multi allocation results into the result format. |
250 | 2a9aff11 | René Nussbaumer | formatMultiAlloc :: (Node.List, Instance.List, Cluster.AllocSolutionList) |
251 | 2a9aff11 | René Nussbaumer | -> Result IAllocResult |
252 | 2a9aff11 | René Nussbaumer | formatMultiAlloc (fin_nl, fin_il, ars) = |
253 | 2a9aff11 | René Nussbaumer | let rars = reverse ars |
254 | 2a9aff11 | René Nussbaumer | (allocated, failed) = partition (isJust . Cluster.asSolution . snd) rars |
255 | 2a9aff11 | René Nussbaumer | aars = map (\(_, ar) -> |
256 | 2a9aff11 | René Nussbaumer | let (_, inst, nodes, _) = fromJust $ Cluster.asSolution ar |
257 | 2a9aff11 | René Nussbaumer | iname = Instance.name inst |
258 | 2a9aff11 | René Nussbaumer | nnames = map Node.name nodes |
259 | 2a9aff11 | René Nussbaumer | in (iname, nnames)) allocated |
260 | 2a9aff11 | René Nussbaumer | fars = map (\(inst, ar) -> |
261 | 2a9aff11 | René Nussbaumer | let iname = Instance.name inst |
262 | 2a9aff11 | René Nussbaumer | in (iname, describeSolution ar)) failed |
263 | 2a9aff11 | René Nussbaumer | info = show (length failed) ++ " instances failed to allocate and " ++ |
264 | 2a9aff11 | René Nussbaumer | show (length allocated) ++ " were allocated successfully" |
265 | 2a9aff11 | René Nussbaumer | in return (info, showJSON (aars, fars), fin_nl, fin_il) |
266 | 2a9aff11 | René Nussbaumer | |
267 | 47eed3f4 | Iustin Pop | -- | Convert a node-evacuation/change group result. |
268 | 5440c877 | Iustin Pop | formatNodeEvac :: Group.List |
269 | 5440c877 | Iustin Pop | -> Node.List |
270 | 5440c877 | Iustin Pop | -> Instance.List |
271 | 4036f63a | Iustin Pop | -> (Node.List, Instance.List, Cluster.EvacSolution) |
272 | 5440c877 | Iustin Pop | -> Result IAllocResult |
273 | f9283686 | Iustin Pop | formatNodeEvac gl nl il (fin_nl, fin_il, es) = |
274 | 00dd69a2 | Iustin Pop | let iname = Instance.name . flip Container.find il |
275 | 00dd69a2 | Iustin Pop | nname = Node.name . flip Container.find nl |
276 | 00dd69a2 | Iustin Pop | gname = Group.name . flip Container.find gl |
277 | 00dd69a2 | Iustin Pop | fes = map (\(idx, msg) -> (iname idx, msg)) $ Cluster.esFailed es |
278 | 00dd69a2 | Iustin Pop | mes = map (\(idx, gdx, ndxs) -> (iname idx, gname gdx, map nname ndxs)) |
279 | 00dd69a2 | Iustin Pop | $ Cluster.esMoved es |
280 | 00dd69a2 | Iustin Pop | failed = length fes |
281 | 00dd69a2 | Iustin Pop | moved = length mes |
282 | 00dd69a2 | Iustin Pop | info = show failed ++ " instances failed to move and " ++ show moved ++ |
283 | 00dd69a2 | Iustin Pop | " were moved successfully" |
284 | 00dd69a2 | Iustin Pop | in Ok (info, showJSON (mes, fes, Cluster.esOpCodes es), fin_nl, fin_il) |
285 | 47eed3f4 | Iustin Pop | |
286 | 88df1fa9 | Iustin Pop | -- | Runs relocate for a single instance. |
287 | 88df1fa9 | Iustin Pop | -- |
288 | 88df1fa9 | Iustin Pop | -- This is wrapper over the 'Cluster.tryNodeEvac' function that is run |
289 | 88df1fa9 | Iustin Pop | -- with a single instance (ours), and further it checks that the |
290 | 88df1fa9 | Iustin Pop | -- result it got (in the nodes field) is actually consistent, as |
291 | 88df1fa9 | Iustin Pop | -- tryNodeEvac is designed to output primarily an opcode list, not a |
292 | 88df1fa9 | Iustin Pop | -- node list. |
293 | 88df1fa9 | Iustin Pop | processRelocate :: Group.List -- ^ The group list |
294 | 88df1fa9 | Iustin Pop | -> Node.List -- ^ The node list |
295 | 88df1fa9 | Iustin Pop | -> Instance.List -- ^ The instance list |
296 | 88df1fa9 | Iustin Pop | -> Idx -- ^ The index of the instance to move |
297 | 88df1fa9 | Iustin Pop | -> Int -- ^ The number of nodes required |
298 | 88df1fa9 | Iustin Pop | -> [Ndx] -- ^ Nodes which should not be used |
299 | 88df1fa9 | Iustin Pop | -> Result (Node.List, Instance.List, [Ndx]) -- ^ Solution list |
300 | 88df1fa9 | Iustin Pop | processRelocate gl nl il idx 1 exndx = do |
301 | 88df1fa9 | Iustin Pop | let orig = Container.find idx il |
302 | 88df1fa9 | Iustin Pop | sorig = Instance.sNode orig |
303 | 3d7d3a1f | Iustin Pop | porig = Instance.pNode orig |
304 | fafd0773 | Iustin Pop | mir_type = Instance.mirrorType orig |
305 | 3d7d3a1f | Iustin Pop | (exp_node, node_type, reloc_type) <- |
306 | 3d7d3a1f | Iustin Pop | case mir_type of |
307 | 3d7d3a1f | Iustin Pop | MirrorNone -> fail "Can't relocate non-mirrored instances" |
308 | 3d7d3a1f | Iustin Pop | MirrorInternal -> return (sorig, "secondary", ChangeSecondary) |
309 | 3d7d3a1f | Iustin Pop | MirrorExternal -> return (porig, "primary", ChangePrimary) |
310 | 2cdaf225 | Iustin Pop | when (exndx /= [exp_node]) . |
311 | 88df1fa9 | Iustin Pop | -- FIXME: we can't use the excluded nodes here; the logic is |
312 | 88df1fa9 | Iustin Pop | -- already _but only partially_ implemented in tryNodeEvac... |
313 | 88df1fa9 | Iustin Pop | fail $ "Unsupported request: excluded nodes not equal to\ |
314 | 3d7d3a1f | Iustin Pop | \ instance's " ++ node_type ++ "(" ++ show exp_node |
315 | 3d7d3a1f | Iustin Pop | ++ " versus " ++ show exndx ++ ")" |
316 | 3d7d3a1f | Iustin Pop | (nl', il', esol) <- Cluster.tryNodeEvac gl nl il reloc_type [idx] |
317 | 88df1fa9 | Iustin Pop | nodes <- case lookup idx (Cluster.esFailed esol) of |
318 | 88df1fa9 | Iustin Pop | Just msg -> fail msg |
319 | 88df1fa9 | Iustin Pop | Nothing -> |
320 | 88df1fa9 | Iustin Pop | case lookup idx (map (\(a, _, b) -> (a, b)) |
321 | 88df1fa9 | Iustin Pop | (Cluster.esMoved esol)) of |
322 | 88df1fa9 | Iustin Pop | Nothing -> |
323 | 88df1fa9 | Iustin Pop | fail "Internal error: lost instance idx during move" |
324 | 88df1fa9 | Iustin Pop | Just n -> return n |
325 | 88df1fa9 | Iustin Pop | let inst = Container.find idx il' |
326 | 88df1fa9 | Iustin Pop | pnode = Instance.pNode inst |
327 | 88df1fa9 | Iustin Pop | snode = Instance.sNode inst |
328 | 3d7d3a1f | Iustin Pop | nodes' <- |
329 | 3d7d3a1f | Iustin Pop | case mir_type of |
330 | 3d7d3a1f | Iustin Pop | MirrorNone -> fail "Internal error: mirror type none after relocation?!" |
331 | 3d7d3a1f | Iustin Pop | MirrorInternal -> |
332 | 3d7d3a1f | Iustin Pop | do |
333 | 3d7d3a1f | Iustin Pop | when (snode == sorig) $ |
334 | 3d7d3a1f | Iustin Pop | fail "Internal error: instance didn't change secondary node?!" |
335 | 3d7d3a1f | Iustin Pop | when (snode == pnode) $ |
336 | 3d7d3a1f | Iustin Pop | fail "Internal error: selected primary as new secondary?!" |
337 | 3d7d3a1f | Iustin Pop | if nodes == [pnode, snode] |
338 | 88df1fa9 | Iustin Pop | then return [snode] -- only the new secondary is needed |
339 | 88df1fa9 | Iustin Pop | else fail $ "Internal error: inconsistent node list (" ++ |
340 | 88df1fa9 | Iustin Pop | show nodes ++ ") versus instance nodes (" ++ show pnode ++ |
341 | 88df1fa9 | Iustin Pop | "," ++ show snode ++ ")" |
342 | 3d7d3a1f | Iustin Pop | MirrorExternal -> |
343 | 3d7d3a1f | Iustin Pop | do |
344 | 3d7d3a1f | Iustin Pop | when (pnode == porig) $ |
345 | 3d7d3a1f | Iustin Pop | fail "Internal error: instance didn't change primary node?!" |
346 | 3d7d3a1f | Iustin Pop | if nodes == [pnode] |
347 | 3d7d3a1f | Iustin Pop | then return nodes |
348 | 3d7d3a1f | Iustin Pop | else fail $ "Internal error: inconsistent node list (" ++ |
349 | 3d7d3a1f | Iustin Pop | show nodes ++ ") versus instance node (" ++ show pnode ++ ")" |
350 | 88df1fa9 | Iustin Pop | return (nl', il', nodes') |
351 | 88df1fa9 | Iustin Pop | |
352 | 88df1fa9 | Iustin Pop | processRelocate _ _ _ _ reqn _ = |
353 | 88df1fa9 | Iustin Pop | fail $ "Exchange " ++ show reqn ++ " nodes mode is not implemented" |
354 | 88df1fa9 | Iustin Pop | |
355 | 88df1fa9 | Iustin Pop | formatRelocate :: (Node.List, Instance.List, [Ndx]) |
356 | 88df1fa9 | Iustin Pop | -> Result IAllocResult |
357 | 88df1fa9 | Iustin Pop | formatRelocate (nl, il, ndxs) = |
358 | 00dd69a2 | Iustin Pop | let nodes = map (`Container.find` nl) ndxs |
359 | 00dd69a2 | Iustin Pop | names = map Node.name nodes |
360 | 00dd69a2 | Iustin Pop | in Ok ("success", showJSON names, nl, il) |
361 | 88df1fa9 | Iustin Pop | |
362 | 179c0828 | Iustin Pop | -- | Process a request and return new node lists. |
363 | 7c14b50a | Iustin Pop | processRequest :: Request -> Result IAllocResult |
364 | cabce2f4 | Iustin Pop | processRequest request = |
365 | 71375ef7 | Iustin Pop | let Request rqtype (ClusterData gl nl il _ _) = request |
366 | cabce2f4 | Iustin Pop | in case rqtype of |
367 | 7c14b50a | Iustin Pop | Allocate xi reqn -> |
368 | 00dd69a2 | Iustin Pop | Cluster.tryMGAlloc gl nl il xi reqn >>= formatAllocate il |
369 | 88df1fa9 | Iustin Pop | Relocate idx reqn exnodes -> |
370 | 00dd69a2 | Iustin Pop | processRelocate gl nl il idx reqn exnodes >>= formatRelocate |
371 | 20b376ff | Iustin Pop | ChangeGroup gdxs idxs -> |
372 | 00dd69a2 | Iustin Pop | Cluster.tryChangeGroup gl nl il idxs gdxs >>= |
373 | 00dd69a2 | Iustin Pop | formatNodeEvac gl nl il |
374 | 47eed3f4 | Iustin Pop | NodeEvacuate xi mode -> |
375 | 00dd69a2 | Iustin Pop | Cluster.tryNodeEvac gl nl il mode xi >>= |
376 | 00dd69a2 | Iustin Pop | formatNodeEvac gl nl il |
377 | 2a9aff11 | René Nussbaumer | MultiAllocate xies -> |
378 | 2a9aff11 | René Nussbaumer | Cluster.allocList gl nl il xies [] >>= formatMultiAlloc |
379 | cabce2f4 | Iustin Pop | |
380 | 179c0828 | Iustin Pop | -- | Reads the request from the data file(s). |
381 | c3f8cb12 | René Nussbaumer | readRequest :: FilePath -> IO Request |
382 | c3f8cb12 | René Nussbaumer | readRequest fp = do |
383 | f183de56 | Iustin Pop | input_data <- case fp of |
384 | f183de56 | Iustin Pop | "-" -> getContents |
385 | f183de56 | Iustin Pop | _ -> readFile fp |
386 | c3f8cb12 | René Nussbaumer | case parseData input_data of |
387 | 707cd3d7 | Helga Velroyen | Bad err -> exitErr err |
388 | c3f8cb12 | René Nussbaumer | Ok (fix_msgs, rq) -> maybeShowWarnings fix_msgs >> return rq |
389 | 00152519 | Iustin Pop | |
390 | 00152519 | Iustin Pop | -- | Main iallocator pipeline. |
391 | f9283686 | Iustin Pop | runIAllocator :: Request -> (Maybe (Node.List, Instance.List), String) |
392 | 00152519 | Iustin Pop | runIAllocator request = |
393 | f9283686 | Iustin Pop | let (ok, info, result, cdata) = |
394 | 00dd69a2 | Iustin Pop | case processRequest request of |
395 | 00dd69a2 | Iustin Pop | Ok (msg, r, nl, il) -> (True, "Request successful: " ++ msg, r, |
396 | 00dd69a2 | Iustin Pop | Just (nl, il)) |
397 | 00dd69a2 | Iustin Pop | Bad msg -> (False, "Request failed: " ++ msg, JSArray [], Nothing) |
398 | ce6a0b53 | Iustin Pop | rstring = formatResponse ok info result |
399 | f9283686 | Iustin Pop | in (cdata, rstring) |
400 | 786c514c | René Nussbaumer | |
401 | 786c514c | René Nussbaumer | -- | Load the data from an iallocation request file |
402 | 786c514c | René Nussbaumer | loadData :: FilePath -- ^ The path to the file |
403 | 786c514c | René Nussbaumer | -> IO (Result ClusterData) |
404 | 786c514c | René Nussbaumer | loadData fp = do |
405 | 786c514c | René Nussbaumer | Request _ cdata <- readRequest fp |
406 | 786c514c | René Nussbaumer | return $ Ok cdata |