root / htools / Ganeti / HTools / Cluster.hs @ 9bb5721c
History | View | Annotate | Download (53.5 kB)
1 | e4f08c46 | Iustin Pop | {-| Implementation of cluster-wide logic. |
---|---|---|---|
2 | e4f08c46 | Iustin Pop | |
3 | e4f08c46 | Iustin Pop | This module holds all pure cluster-logic; I\/O related functionality |
4 | 525bfb36 | Iustin Pop | goes into the /Main/ module for the individual binaries. |
5 | e4f08c46 | Iustin Pop | |
6 | e4f08c46 | Iustin Pop | -} |
7 | e4f08c46 | Iustin Pop | |
8 | e2fa2baf | Iustin Pop | {- |
9 | e2fa2baf | Iustin Pop | |
10 | 4bc33d60 | Iustin Pop | Copyright (C) 2009, 2010, 2011 Google Inc. |
11 | e2fa2baf | Iustin Pop | |
12 | e2fa2baf | Iustin Pop | This program is free software; you can redistribute it and/or modify |
13 | e2fa2baf | Iustin Pop | it under the terms of the GNU General Public License as published by |
14 | e2fa2baf | Iustin Pop | the Free Software Foundation; either version 2 of the License, or |
15 | e2fa2baf | Iustin Pop | (at your option) any later version. |
16 | e2fa2baf | Iustin Pop | |
17 | e2fa2baf | Iustin Pop | This program is distributed in the hope that it will be useful, but |
18 | e2fa2baf | Iustin Pop | WITHOUT ANY WARRANTY; without even the implied warranty of |
19 | e2fa2baf | Iustin Pop | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
20 | e2fa2baf | Iustin Pop | General Public License for more details. |
21 | e2fa2baf | Iustin Pop | |
22 | e2fa2baf | Iustin Pop | You should have received a copy of the GNU General Public License |
23 | e2fa2baf | Iustin Pop | along with this program; if not, write to the Free Software |
24 | e2fa2baf | Iustin Pop | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
25 | e2fa2baf | Iustin Pop | 02110-1301, USA. |
26 | e2fa2baf | Iustin Pop | |
27 | e2fa2baf | Iustin Pop | -} |
28 | e2fa2baf | Iustin Pop | |
29 | 669d7e3d | Iustin Pop | module Ganeti.HTools.Cluster |
30 | e4f08c46 | Iustin Pop | ( |
31 | e4f08c46 | Iustin Pop | -- * Types |
32 | 85d0ddc3 | Iustin Pop | AllocSolution(..) |
33 | 47eed3f4 | Iustin Pop | , EvacSolution(..) |
34 | e4f08c46 | Iustin Pop | , Table(..) |
35 | 1a7eff0e | Iustin Pop | , CStats(..) |
36 | 9b8fac3d | Iustin Pop | , AllocStats |
37 | e4f08c46 | Iustin Pop | -- * Generic functions |
38 | e4f08c46 | Iustin Pop | , totalResources |
39 | 9b8fac3d | Iustin Pop | , computeAllocationDelta |
40 | e4f08c46 | Iustin Pop | -- * First phase functions |
41 | e4f08c46 | Iustin Pop | , computeBadItems |
42 | e4f08c46 | Iustin Pop | -- * Second phase functions |
43 | 7dfaafb1 | Iustin Pop | , printSolutionLine |
44 | 142538ff | Iustin Pop | , formatCmds |
45 | 0e8ae201 | Iustin Pop | , involvedNodes |
46 | 0e8ae201 | Iustin Pop | , splitJobs |
47 | 507fda3f | Iustin Pop | -- * Display functions |
48 | 507fda3f | Iustin Pop | , printNodes |
49 | 507fda3f | Iustin Pop | , printInsts |
50 | e4f08c46 | Iustin Pop | -- * Balacing functions |
51 | e4f08c46 | Iustin Pop | , checkMove |
52 | 5ad86777 | Iustin Pop | , doNextBalance |
53 | f25e5aac | Iustin Pop | , tryBalance |
54 | e4f08c46 | Iustin Pop | , compCV |
55 | 9bb5721c | Iustin Pop | , compCVNodes |
56 | 6bc39970 | Iustin Pop | , compDetailedCV |
57 | e4f08c46 | Iustin Pop | , printStats |
58 | 6b20875c | Iustin Pop | , iMoveToJob |
59 | 4a340313 | Iustin Pop | -- * IAllocator functions |
60 | 6cb1649f | Iustin Pop | , genAllocNodes |
61 | dbba5246 | Iustin Pop | , tryAlloc |
62 | 9b1584fc | Iustin Pop | , tryMGAlloc |
63 | dbba5246 | Iustin Pop | , tryReloc |
64 | 4bc33d60 | Iustin Pop | , tryMGReloc |
65 | 12b0511d | Iustin Pop | , tryEvac |
66 | 1bc47d38 | Iustin Pop | , tryMGEvac |
67 | 47eed3f4 | Iustin Pop | , tryNodeEvac |
68 | 478df686 | Iustin Pop | , collapseFailures |
69 | 3ce8009a | Iustin Pop | -- * Allocation functions |
70 | 3ce8009a | Iustin Pop | , iterateAlloc |
71 | 3ce8009a | Iustin Pop | , tieredAlloc |
72 | 949397c8 | Iustin Pop | , tieredSpecMap |
73 | 949397c8 | Iustin Pop | -- * Node group functions |
74 | 32b8d9c0 | Iustin Pop | , instanceGroup |
75 | 32b8d9c0 | Iustin Pop | , findSplitInstances |
76 | f4161783 | Iustin Pop | , splitCluster |
77 | e4f08c46 | Iustin Pop | ) where |
78 | e4f08c46 | Iustin Pop | |
79 | 949397c8 | Iustin Pop | import Data.Function (on) |
80 | 63a78055 | Iustin Pop | import qualified Data.IntSet as IntSet |
81 | e4f08c46 | Iustin Pop | import Data.List |
82 | 5182e970 | Iustin Pop | import Data.Ord (comparing) |
83 | e4f08c46 | Iustin Pop | import Text.Printf (printf) |
84 | 9d3fada5 | Iustin Pop | import Control.Monad |
85 | a3eee4ad | Iustin Pop | import Control.Parallel.Strategies |
86 | e4f08c46 | Iustin Pop | |
87 | 669d7e3d | Iustin Pop | import qualified Ganeti.HTools.Container as Container |
88 | 669d7e3d | Iustin Pop | import qualified Ganeti.HTools.Instance as Instance |
89 | 669d7e3d | Iustin Pop | import qualified Ganeti.HTools.Node as Node |
90 | aec636b9 | Iustin Pop | import qualified Ganeti.HTools.Group as Group |
91 | e4c5beaf | Iustin Pop | import Ganeti.HTools.Types |
92 | 669d7e3d | Iustin Pop | import Ganeti.HTools.Utils |
93 | 6b20875c | Iustin Pop | import qualified Ganeti.OpCodes as OpCodes |
94 | e4f08c46 | Iustin Pop | |
95 | 9188aeef | Iustin Pop | -- * Types |
96 | 9188aeef | Iustin Pop | |
97 | 0c936d24 | Iustin Pop | -- | Allocation\/relocation solution. |
98 | 85d0ddc3 | Iustin Pop | data AllocSolution = AllocSolution |
99 | 85d0ddc3 | Iustin Pop | { asFailures :: [FailMode] -- ^ Failure counts |
100 | 85d0ddc3 | Iustin Pop | , asAllocs :: Int -- ^ Good allocation count |
101 | 85d0ddc3 | Iustin Pop | , asSolutions :: [Node.AllocElement] -- ^ The actual result, length |
102 | 85d0ddc3 | Iustin Pop | -- of the list depends on the |
103 | 85d0ddc3 | Iustin Pop | -- allocation/relocation mode |
104 | 859fc11d | Iustin Pop | , asLog :: [String] -- ^ A list of informational messages |
105 | 85d0ddc3 | Iustin Pop | } |
106 | 85d0ddc3 | Iustin Pop | |
107 | 47eed3f4 | Iustin Pop | -- | Node evacuation/group change iallocator result type. This result |
108 | 47eed3f4 | Iustin Pop | -- type consists of actual opcodes (a restricted subset) that are |
109 | 47eed3f4 | Iustin Pop | -- transmitted back to Ganeti. |
110 | 47eed3f4 | Iustin Pop | data EvacSolution = EvacSolution |
111 | 47eed3f4 | Iustin Pop | { esMoved :: [String] -- ^ Instance moved successfully |
112 | 47eed3f4 | Iustin Pop | , esFailed :: [String] -- ^ Instance which were not |
113 | 47eed3f4 | Iustin Pop | -- relocated |
114 | 47eed3f4 | Iustin Pop | , esOpCodes :: [[[OpCodes.OpCode]]] -- ^ List of lists of jobs |
115 | 47eed3f4 | Iustin Pop | } |
116 | 47eed3f4 | Iustin Pop | |
117 | 40ee14bc | Iustin Pop | -- | Allocation results, as used in 'iterateAlloc' and 'tieredAlloc'. |
118 | 40ee14bc | Iustin Pop | type AllocResult = (FailStats, Node.List, Instance.List, |
119 | 40ee14bc | Iustin Pop | [Instance.Instance], [CStats]) |
120 | 40ee14bc | Iustin Pop | |
121 | 6cb1649f | Iustin Pop | -- | A type denoting the valid allocation mode/pairs. |
122 | 525bfb36 | Iustin Pop | -- |
123 | 6cb1649f | Iustin Pop | -- For a one-node allocation, this will be a @Left ['Node.Node']@, |
124 | 6cb1649f | Iustin Pop | -- whereas for a two-node allocation, this will be a @Right |
125 | 6cb1649f | Iustin Pop | -- [('Node.Node', 'Node.Node')]@. |
126 | 0d66ea67 | Iustin Pop | type AllocNodes = Either [Ndx] [(Ndx, Ndx)] |
127 | 6cb1649f | Iustin Pop | |
128 | 525bfb36 | Iustin Pop | -- | The empty solution we start with when computing allocations. |
129 | 97936d51 | Iustin Pop | emptyAllocSolution :: AllocSolution |
130 | 97936d51 | Iustin Pop | emptyAllocSolution = AllocSolution { asFailures = [], asAllocs = 0 |
131 | 97936d51 | Iustin Pop | , asSolutions = [], asLog = [] } |
132 | 78694255 | Iustin Pop | |
133 | 47eed3f4 | Iustin Pop | -- | The empty evac solution. |
134 | 47eed3f4 | Iustin Pop | emptyEvacSolution :: EvacSolution |
135 | 47eed3f4 | Iustin Pop | emptyEvacSolution = EvacSolution { esMoved = [] |
136 | 47eed3f4 | Iustin Pop | , esFailed = [] |
137 | 47eed3f4 | Iustin Pop | , esOpCodes = [] |
138 | 47eed3f4 | Iustin Pop | } |
139 | 47eed3f4 | Iustin Pop | |
140 | 525bfb36 | Iustin Pop | -- | The complete state for the balancing solution. |
141 | 262a08a2 | Iustin Pop | data Table = Table Node.List Instance.List Score [Placement] |
142 | 6bc39970 | Iustin Pop | deriving (Show, Read) |
143 | e4f08c46 | Iustin Pop | |
144 | 7034694d | Iustin Pop | data CStats = CStats { csFmem :: Integer -- ^ Cluster free mem |
145 | 7034694d | Iustin Pop | , csFdsk :: Integer -- ^ Cluster free disk |
146 | 7034694d | Iustin Pop | , csAmem :: Integer -- ^ Cluster allocatable mem |
147 | 7034694d | Iustin Pop | , csAdsk :: Integer -- ^ Cluster allocatable disk |
148 | 7034694d | Iustin Pop | , csAcpu :: Integer -- ^ Cluster allocatable cpus |
149 | 7034694d | Iustin Pop | , csMmem :: Integer -- ^ Max node allocatable mem |
150 | 7034694d | Iustin Pop | , csMdsk :: Integer -- ^ Max node allocatable disk |
151 | 7034694d | Iustin Pop | , csMcpu :: Integer -- ^ Max node allocatable cpu |
152 | 7034694d | Iustin Pop | , csImem :: Integer -- ^ Instance used mem |
153 | 7034694d | Iustin Pop | , csIdsk :: Integer -- ^ Instance used disk |
154 | 7034694d | Iustin Pop | , csIcpu :: Integer -- ^ Instance used cpu |
155 | 7034694d | Iustin Pop | , csTmem :: Double -- ^ Cluster total mem |
156 | 7034694d | Iustin Pop | , csTdsk :: Double -- ^ Cluster total disk |
157 | 7034694d | Iustin Pop | , csTcpu :: Double -- ^ Cluster total cpus |
158 | 7034694d | Iustin Pop | , csVcpu :: Integer -- ^ Cluster virtual cpus (if |
159 | 7034694d | Iustin Pop | -- node pCpu has been set, |
160 | 7034694d | Iustin Pop | -- otherwise -1) |
161 | 7034694d | Iustin Pop | , csXmem :: Integer -- ^ Unnacounted for mem |
162 | 7034694d | Iustin Pop | , csNmem :: Integer -- ^ Node own memory |
163 | 7034694d | Iustin Pop | , csScore :: Score -- ^ The cluster score |
164 | 7034694d | Iustin Pop | , csNinst :: Int -- ^ The total number of instances |
165 | 1a7eff0e | Iustin Pop | } |
166 | 6bc39970 | Iustin Pop | deriving (Show, Read) |
167 | 1a7eff0e | Iustin Pop | |
168 | 525bfb36 | Iustin Pop | -- | Currently used, possibly to allocate, unallocable. |
169 | 9b8fac3d | Iustin Pop | type AllocStats = (RSpec, RSpec, RSpec) |
170 | 9b8fac3d | Iustin Pop | |
171 | 9188aeef | Iustin Pop | -- * Utility functions |
172 | 9188aeef | Iustin Pop | |
173 | e4f08c46 | Iustin Pop | -- | Verifies the N+1 status and return the affected nodes. |
174 | e4f08c46 | Iustin Pop | verifyN1 :: [Node.Node] -> [Node.Node] |
175 | 9f6dcdea | Iustin Pop | verifyN1 = filter Node.failN1 |
176 | e4f08c46 | Iustin Pop | |
177 | 9188aeef | Iustin Pop | {-| Computes the pair of bad nodes and instances. |
178 | 9188aeef | Iustin Pop | |
179 | 9188aeef | Iustin Pop | The bad node list is computed via a simple 'verifyN1' check, and the |
180 | 9188aeef | Iustin Pop | bad instance list is the list of primary and secondary instances of |
181 | 9188aeef | Iustin Pop | those nodes. |
182 | 9188aeef | Iustin Pop | |
183 | 9188aeef | Iustin Pop | -} |
184 | 9188aeef | Iustin Pop | computeBadItems :: Node.List -> Instance.List -> |
185 | 9188aeef | Iustin Pop | ([Node.Node], [Instance.Instance]) |
186 | 9188aeef | Iustin Pop | computeBadItems nl il = |
187 | dbba5246 | Iustin Pop | let bad_nodes = verifyN1 $ getOnline nl |
188 | 5182e970 | Iustin Pop | bad_instances = map (`Container.find` il) . |
189 | 9f6dcdea | Iustin Pop | sort . nub $ |
190 | 2060348b | Iustin Pop | concatMap (\ n -> Node.sList n ++ Node.pList n) bad_nodes |
191 | 9188aeef | Iustin Pop | in |
192 | 9188aeef | Iustin Pop | (bad_nodes, bad_instances) |
193 | 9188aeef | Iustin Pop | |
194 | 525bfb36 | Iustin Pop | -- | Zero-initializer for the CStats type. |
195 | 1a7eff0e | Iustin Pop | emptyCStats :: CStats |
196 | 86ecce4a | Iustin Pop | emptyCStats = CStats 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |
197 | 1a7eff0e | Iustin Pop | |
198 | 525bfb36 | Iustin Pop | -- | Update stats with data from a new node. |
199 | 1a7eff0e | Iustin Pop | updateCStats :: CStats -> Node.Node -> CStats |
200 | 1a7eff0e | Iustin Pop | updateCStats cs node = |
201 | f5b553da | Iustin Pop | let CStats { csFmem = x_fmem, csFdsk = x_fdsk, |
202 | f5b553da | Iustin Pop | csAmem = x_amem, csAcpu = x_acpu, csAdsk = x_adsk, |
203 | f5b553da | Iustin Pop | csMmem = x_mmem, csMdsk = x_mdsk, csMcpu = x_mcpu, |
204 | f5b553da | Iustin Pop | csImem = x_imem, csIdsk = x_idsk, csIcpu = x_icpu, |
205 | f5b553da | Iustin Pop | csTmem = x_tmem, csTdsk = x_tdsk, csTcpu = x_tcpu, |
206 | 86ecce4a | Iustin Pop | csVcpu = x_vcpu, |
207 | f5b553da | Iustin Pop | csXmem = x_xmem, csNmem = x_nmem, csNinst = x_ninst |
208 | 8c4c6a8a | Iustin Pop | } |
209 | 1a7eff0e | Iustin Pop | = cs |
210 | 2060348b | Iustin Pop | inc_amem = Node.fMem node - Node.rMem node |
211 | 1a7eff0e | Iustin Pop | inc_amem' = if inc_amem > 0 then inc_amem else 0 |
212 | 301789f4 | Iustin Pop | inc_adsk = Node.availDisk node |
213 | 2060348b | Iustin Pop | inc_imem = truncate (Node.tMem node) - Node.nMem node |
214 | 2060348b | Iustin Pop | - Node.xMem node - Node.fMem node |
215 | 2060348b | Iustin Pop | inc_icpu = Node.uCpu node |
216 | 2060348b | Iustin Pop | inc_idsk = truncate (Node.tDsk node) - Node.fDsk node |
217 | 86ecce4a | Iustin Pop | inc_vcpu = Node.hiCpu node |
218 | f52dadb2 | Iustin Pop | inc_acpu = Node.availCpu node |
219 | 8c4c6a8a | Iustin Pop | |
220 | 7034694d | Iustin Pop | in cs { csFmem = x_fmem + fromIntegral (Node.fMem node) |
221 | 7034694d | Iustin Pop | , csFdsk = x_fdsk + fromIntegral (Node.fDsk node) |
222 | 7034694d | Iustin Pop | , csAmem = x_amem + fromIntegral inc_amem' |
223 | 7034694d | Iustin Pop | , csAdsk = x_adsk + fromIntegral inc_adsk |
224 | 7034694d | Iustin Pop | , csAcpu = x_acpu + fromIntegral inc_acpu |
225 | 7034694d | Iustin Pop | , csMmem = max x_mmem (fromIntegral inc_amem') |
226 | 7034694d | Iustin Pop | , csMdsk = max x_mdsk (fromIntegral inc_adsk) |
227 | 7034694d | Iustin Pop | , csMcpu = max x_mcpu (fromIntegral inc_acpu) |
228 | 7034694d | Iustin Pop | , csImem = x_imem + fromIntegral inc_imem |
229 | 7034694d | Iustin Pop | , csIdsk = x_idsk + fromIntegral inc_idsk |
230 | 7034694d | Iustin Pop | , csIcpu = x_icpu + fromIntegral inc_icpu |
231 | f5b553da | Iustin Pop | , csTmem = x_tmem + Node.tMem node |
232 | f5b553da | Iustin Pop | , csTdsk = x_tdsk + Node.tDsk node |
233 | f5b553da | Iustin Pop | , csTcpu = x_tcpu + Node.tCpu node |
234 | 7034694d | Iustin Pop | , csVcpu = x_vcpu + fromIntegral inc_vcpu |
235 | 7034694d | Iustin Pop | , csXmem = x_xmem + fromIntegral (Node.xMem node) |
236 | 7034694d | Iustin Pop | , csNmem = x_nmem + fromIntegral (Node.nMem node) |
237 | f5b553da | Iustin Pop | , csNinst = x_ninst + length (Node.pList node) |
238 | de4ac2c2 | Iustin Pop | } |
239 | 1a7eff0e | Iustin Pop | |
240 | 9188aeef | Iustin Pop | -- | Compute the total free disk and memory in the cluster. |
241 | 1a7eff0e | Iustin Pop | totalResources :: Node.List -> CStats |
242 | de4ac2c2 | Iustin Pop | totalResources nl = |
243 | de4ac2c2 | Iustin Pop | let cs = foldl' updateCStats emptyCStats . Container.elems $ nl |
244 | f5b553da | Iustin Pop | in cs { csScore = compCV nl } |
245 | 9188aeef | Iustin Pop | |
246 | 9b8fac3d | Iustin Pop | -- | Compute the delta between two cluster state. |
247 | 9b8fac3d | Iustin Pop | -- |
248 | 9b8fac3d | Iustin Pop | -- This is used when doing allocations, to understand better the |
249 | e2436511 | Iustin Pop | -- available cluster resources. The return value is a triple of the |
250 | e2436511 | Iustin Pop | -- current used values, the delta that was still allocated, and what |
251 | e2436511 | Iustin Pop | -- was left unallocated. |
252 | 9b8fac3d | Iustin Pop | computeAllocationDelta :: CStats -> CStats -> AllocStats |
253 | 9b8fac3d | Iustin Pop | computeAllocationDelta cini cfin = |
254 | 9b8fac3d | Iustin Pop | let CStats {csImem = i_imem, csIdsk = i_idsk, csIcpu = i_icpu} = cini |
255 | 9b8fac3d | Iustin Pop | CStats {csImem = f_imem, csIdsk = f_idsk, csIcpu = f_icpu, |
256 | 9b8fac3d | Iustin Pop | csTmem = t_mem, csTdsk = t_dsk, csVcpu = v_cpu } = cfin |
257 | 7034694d | Iustin Pop | rini = RSpec (fromIntegral i_icpu) (fromIntegral i_imem) |
258 | 7034694d | Iustin Pop | (fromIntegral i_idsk) |
259 | 7034694d | Iustin Pop | rfin = RSpec (fromIntegral (f_icpu - i_icpu)) |
260 | 7034694d | Iustin Pop | (fromIntegral (f_imem - i_imem)) |
261 | 7034694d | Iustin Pop | (fromIntegral (f_idsk - i_idsk)) |
262 | 7034694d | Iustin Pop | un_cpu = fromIntegral (v_cpu - f_icpu)::Int |
263 | 7034694d | Iustin Pop | runa = RSpec un_cpu (truncate t_mem - fromIntegral f_imem) |
264 | 7034694d | Iustin Pop | (truncate t_dsk - fromIntegral f_idsk) |
265 | 9b8fac3d | Iustin Pop | in (rini, rfin, runa) |
266 | 9b8fac3d | Iustin Pop | |
267 | 525bfb36 | Iustin Pop | -- | The names and weights of the individual elements in the CV list. |
268 | 8a3b30ca | Iustin Pop | detailedCVInfo :: [(Double, String)] |
269 | 8a3b30ca | Iustin Pop | detailedCVInfo = [ (1, "free_mem_cv") |
270 | 8a3b30ca | Iustin Pop | , (1, "free_disk_cv") |
271 | 8a3b30ca | Iustin Pop | , (1, "n1_cnt") |
272 | 8a3b30ca | Iustin Pop | , (1, "reserved_mem_cv") |
273 | 8a3b30ca | Iustin Pop | , (4, "offline_all_cnt") |
274 | 8a3b30ca | Iustin Pop | , (16, "offline_pri_cnt") |
275 | 8a3b30ca | Iustin Pop | , (1, "vcpu_ratio_cv") |
276 | 8a3b30ca | Iustin Pop | , (1, "cpu_load_cv") |
277 | 8a3b30ca | Iustin Pop | , (1, "mem_load_cv") |
278 | 8a3b30ca | Iustin Pop | , (1, "disk_load_cv") |
279 | 8a3b30ca | Iustin Pop | , (1, "net_load_cv") |
280 | 306cccd5 | Iustin Pop | , (2, "pri_tags_score") |
281 | 8a3b30ca | Iustin Pop | ] |
282 | 8a3b30ca | Iustin Pop | |
283 | 8a3b30ca | Iustin Pop | detailedCVWeights :: [Double] |
284 | 8a3b30ca | Iustin Pop | detailedCVWeights = map fst detailedCVInfo |
285 | fca250e9 | Iustin Pop | |
286 | 9188aeef | Iustin Pop | -- | Compute the mem and disk covariance. |
287 | 9bb5721c | Iustin Pop | compDetailedCV :: [Node.Node] -> [Double] |
288 | 9bb5721c | Iustin Pop | compDetailedCV all_nodes = |
289 | 9188aeef | Iustin Pop | let |
290 | 9188aeef | Iustin Pop | (offline, nodes) = partition Node.offline all_nodes |
291 | 2060348b | Iustin Pop | mem_l = map Node.pMem nodes |
292 | 2060348b | Iustin Pop | dsk_l = map Node.pDsk nodes |
293 | daee4bed | Iustin Pop | -- metric: memory covariance |
294 | 4715711d | Iustin Pop | mem_cv = stdDev mem_l |
295 | daee4bed | Iustin Pop | -- metric: disk covariance |
296 | 4715711d | Iustin Pop | dsk_cv = stdDev dsk_l |
297 | c3c7a0c1 | Iustin Pop | -- metric: count of instances living on N1 failing nodes |
298 | c3c7a0c1 | Iustin Pop | n1_score = fromIntegral . sum . map (\n -> length (Node.sList n) + |
299 | c3c7a0c1 | Iustin Pop | length (Node.pList n)) . |
300 | c3c7a0c1 | Iustin Pop | filter Node.failN1 $ nodes :: Double |
301 | 2060348b | Iustin Pop | res_l = map Node.pRem nodes |
302 | daee4bed | Iustin Pop | -- metric: reserved memory covariance |
303 | 4715711d | Iustin Pop | res_cv = stdDev res_l |
304 | e4d31268 | Iustin Pop | -- offline instances metrics |
305 | e4d31268 | Iustin Pop | offline_ipri = sum . map (length . Node.pList) $ offline |
306 | e4d31268 | Iustin Pop | offline_isec = sum . map (length . Node.sList) $ offline |
307 | e4d31268 | Iustin Pop | -- metric: count of instances on offline nodes |
308 | e4d31268 | Iustin Pop | off_score = fromIntegral (offline_ipri + offline_isec)::Double |
309 | 673f0f00 | Iustin Pop | -- metric: count of primary instances on offline nodes (this |
310 | 673f0f00 | Iustin Pop | -- helps with evacuation/failover of primary instances on |
311 | 673f0f00 | Iustin Pop | -- 2-node clusters with one node offline) |
312 | 673f0f00 | Iustin Pop | off_pri_score = fromIntegral offline_ipri::Double |
313 | 2060348b | Iustin Pop | cpu_l = map Node.pCpu nodes |
314 | daee4bed | Iustin Pop | -- metric: covariance of vcpu/pcpu ratio |
315 | 4715711d | Iustin Pop | cpu_cv = stdDev cpu_l |
316 | daee4bed | Iustin Pop | -- metrics: covariance of cpu, memory, disk and network load |
317 | ee9724b9 | Iustin Pop | (c_load, m_load, d_load, n_load) = unzip4 $ |
318 | ee9724b9 | Iustin Pop | map (\n -> |
319 | ee9724b9 | Iustin Pop | let DynUtil c1 m1 d1 n1 = Node.utilLoad n |
320 | ee9724b9 | Iustin Pop | DynUtil c2 m2 d2 n2 = Node.utilPool n |
321 | ee9724b9 | Iustin Pop | in (c1/c2, m1/m2, d1/d2, n1/n2) |
322 | ee9724b9 | Iustin Pop | ) nodes |
323 | d844fe88 | Iustin Pop | -- metric: conflicting instance count |
324 | d844fe88 | Iustin Pop | pri_tags_inst = sum $ map Node.conflictingPrimaries nodes |
325 | d844fe88 | Iustin Pop | pri_tags_score = fromIntegral pri_tags_inst::Double |
326 | 673f0f00 | Iustin Pop | in [ mem_cv, dsk_cv, n1_score, res_cv, off_score, off_pri_score, cpu_cv |
327 | 4715711d | Iustin Pop | , stdDev c_load, stdDev m_load , stdDev d_load, stdDev n_load |
328 | d844fe88 | Iustin Pop | , pri_tags_score ] |
329 | 9188aeef | Iustin Pop | |
330 | 9188aeef | Iustin Pop | -- | Compute the /total/ variance. |
331 | 9bb5721c | Iustin Pop | compCVNodes :: [Node.Node] -> Double |
332 | 9bb5721c | Iustin Pop | compCVNodes = sum . zipWith (*) detailedCVWeights . compDetailedCV |
333 | 9bb5721c | Iustin Pop | |
334 | 9bb5721c | Iustin Pop | -- | Wrapper over 'compCVNodes' for callers that have a 'Node.List'. |
335 | 9188aeef | Iustin Pop | compCV :: Node.List -> Double |
336 | 9bb5721c | Iustin Pop | compCV = compCVNodes . Container.elems |
337 | 9bb5721c | Iustin Pop | |
338 | 9188aeef | Iustin Pop | |
339 | 525bfb36 | Iustin Pop | -- | Compute online nodes from a 'Node.List'. |
340 | dbba5246 | Iustin Pop | getOnline :: Node.List -> [Node.Node] |
341 | dbba5246 | Iustin Pop | getOnline = filter (not . Node.offline) . Container.elems |
342 | dbba5246 | Iustin Pop | |
343 | 525bfb36 | Iustin Pop | -- * Balancing functions |
344 | 9188aeef | Iustin Pop | |
345 | 9188aeef | Iustin Pop | -- | Compute best table. Note that the ordering of the arguments is important. |
346 | 9188aeef | Iustin Pop | compareTables :: Table -> Table -> Table |
347 | 9188aeef | Iustin Pop | compareTables a@(Table _ _ a_cv _) b@(Table _ _ b_cv _ ) = |
348 | 9188aeef | Iustin Pop | if a_cv > b_cv then b else a |
349 | 9188aeef | Iustin Pop | |
350 | 9188aeef | Iustin Pop | -- | Applies an instance move to a given node list and instance. |
351 | 262a08a2 | Iustin Pop | applyMove :: Node.List -> Instance.Instance |
352 | 8880d889 | Iustin Pop | -> IMove -> OpResult (Node.List, Instance.Instance, Ndx, Ndx) |
353 | 00b51a14 | Iustin Pop | -- Failover (f) |
354 | e4f08c46 | Iustin Pop | applyMove nl inst Failover = |
355 | 2060348b | Iustin Pop | let old_pdx = Instance.pNode inst |
356 | 2060348b | Iustin Pop | old_sdx = Instance.sNode inst |
357 | e4f08c46 | Iustin Pop | old_p = Container.find old_pdx nl |
358 | e4f08c46 | Iustin Pop | old_s = Container.find old_sdx nl |
359 | e4f08c46 | Iustin Pop | int_p = Node.removePri old_p inst |
360 | e4f08c46 | Iustin Pop | int_s = Node.removeSec old_s inst |
361 | 2cae47e9 | Iustin Pop | force_p = Node.offline old_p |
362 | b161386d | Iustin Pop | new_nl = do -- Maybe monad |
363 | 2cae47e9 | Iustin Pop | new_p <- Node.addPriEx force_p int_s inst |
364 | b161386d | Iustin Pop | new_s <- Node.addSec int_p inst old_sdx |
365 | 8880d889 | Iustin Pop | let new_inst = Instance.setBoth inst old_sdx old_pdx |
366 | 8880d889 | Iustin Pop | return (Container.addTwo old_pdx new_s old_sdx new_p nl, |
367 | 8880d889 | Iustin Pop | new_inst, old_sdx, old_pdx) |
368 | 8880d889 | Iustin Pop | in new_nl |
369 | e4f08c46 | Iustin Pop | |
370 | 00b51a14 | Iustin Pop | -- Replace the primary (f:, r:np, f) |
371 | e4f08c46 | Iustin Pop | applyMove nl inst (ReplacePrimary new_pdx) = |
372 | 2060348b | Iustin Pop | let old_pdx = Instance.pNode inst |
373 | 2060348b | Iustin Pop | old_sdx = Instance.sNode inst |
374 | e4f08c46 | Iustin Pop | old_p = Container.find old_pdx nl |
375 | e4f08c46 | Iustin Pop | old_s = Container.find old_sdx nl |
376 | e4f08c46 | Iustin Pop | tgt_n = Container.find new_pdx nl |
377 | e4f08c46 | Iustin Pop | int_p = Node.removePri old_p inst |
378 | e4f08c46 | Iustin Pop | int_s = Node.removeSec old_s inst |
379 | 2cae47e9 | Iustin Pop | force_p = Node.offline old_p |
380 | b161386d | Iustin Pop | new_nl = do -- Maybe monad |
381 | 70db354e | Iustin Pop | -- check that the current secondary can host the instance |
382 | 70db354e | Iustin Pop | -- during the migration |
383 | 2cae47e9 | Iustin Pop | tmp_s <- Node.addPriEx force_p int_s inst |
384 | 70db354e | Iustin Pop | let tmp_s' = Node.removePri tmp_s inst |
385 | 2cae47e9 | Iustin Pop | new_p <- Node.addPriEx force_p tgt_n inst |
386 | 2cae47e9 | Iustin Pop | new_s <- Node.addSecEx force_p tmp_s' inst new_pdx |
387 | 8880d889 | Iustin Pop | let new_inst = Instance.setPri inst new_pdx |
388 | 8880d889 | Iustin Pop | return (Container.add new_pdx new_p $ |
389 | 8880d889 | Iustin Pop | Container.addTwo old_pdx int_p old_sdx new_s nl, |
390 | 8880d889 | Iustin Pop | new_inst, new_pdx, old_sdx) |
391 | 8880d889 | Iustin Pop | in new_nl |
392 | e4f08c46 | Iustin Pop | |
393 | 00b51a14 | Iustin Pop | -- Replace the secondary (r:ns) |
394 | e4f08c46 | Iustin Pop | applyMove nl inst (ReplaceSecondary new_sdx) = |
395 | 2060348b | Iustin Pop | let old_pdx = Instance.pNode inst |
396 | 2060348b | Iustin Pop | old_sdx = Instance.sNode inst |
397 | e4f08c46 | Iustin Pop | old_s = Container.find old_sdx nl |
398 | e4f08c46 | Iustin Pop | tgt_n = Container.find new_sdx nl |
399 | e4f08c46 | Iustin Pop | int_s = Node.removeSec old_s inst |
400 | 2cae47e9 | Iustin Pop | force_s = Node.offline old_s |
401 | 8880d889 | Iustin Pop | new_inst = Instance.setSec inst new_sdx |
402 | 2cae47e9 | Iustin Pop | new_nl = Node.addSecEx force_s tgt_n inst old_pdx >>= |
403 | 8880d889 | Iustin Pop | \new_s -> return (Container.addTwo new_sdx |
404 | 8880d889 | Iustin Pop | new_s old_sdx int_s nl, |
405 | 8880d889 | Iustin Pop | new_inst, old_pdx, new_sdx) |
406 | 8880d889 | Iustin Pop | in new_nl |
407 | e4f08c46 | Iustin Pop | |
408 | 00b51a14 | Iustin Pop | -- Replace the secondary and failover (r:np, f) |
409 | 79ac6b6f | Iustin Pop | applyMove nl inst (ReplaceAndFailover new_pdx) = |
410 | 2060348b | Iustin Pop | let old_pdx = Instance.pNode inst |
411 | 2060348b | Iustin Pop | old_sdx = Instance.sNode inst |
412 | 79ac6b6f | Iustin Pop | old_p = Container.find old_pdx nl |
413 | 79ac6b6f | Iustin Pop | old_s = Container.find old_sdx nl |
414 | 79ac6b6f | Iustin Pop | tgt_n = Container.find new_pdx nl |
415 | 79ac6b6f | Iustin Pop | int_p = Node.removePri old_p inst |
416 | 79ac6b6f | Iustin Pop | int_s = Node.removeSec old_s inst |
417 | 2cae47e9 | Iustin Pop | force_s = Node.offline old_s |
418 | b161386d | Iustin Pop | new_nl = do -- Maybe monad |
419 | b161386d | Iustin Pop | new_p <- Node.addPri tgt_n inst |
420 | 2cae47e9 | Iustin Pop | new_s <- Node.addSecEx force_s int_p inst new_pdx |
421 | 8880d889 | Iustin Pop | let new_inst = Instance.setBoth inst new_pdx old_pdx |
422 | 8880d889 | Iustin Pop | return (Container.add new_pdx new_p $ |
423 | 8880d889 | Iustin Pop | Container.addTwo old_pdx new_s old_sdx int_s nl, |
424 | 8880d889 | Iustin Pop | new_inst, new_pdx, old_pdx) |
425 | 8880d889 | Iustin Pop | in new_nl |
426 | 79ac6b6f | Iustin Pop | |
427 | 19493d33 | Iustin Pop | -- Failver and replace the secondary (f, r:ns) |
428 | 19493d33 | Iustin Pop | applyMove nl inst (FailoverAndReplace new_sdx) = |
429 | 2060348b | Iustin Pop | let old_pdx = Instance.pNode inst |
430 | 2060348b | Iustin Pop | old_sdx = Instance.sNode inst |
431 | 19493d33 | Iustin Pop | old_p = Container.find old_pdx nl |
432 | 19493d33 | Iustin Pop | old_s = Container.find old_sdx nl |
433 | 19493d33 | Iustin Pop | tgt_n = Container.find new_sdx nl |
434 | 19493d33 | Iustin Pop | int_p = Node.removePri old_p inst |
435 | 19493d33 | Iustin Pop | int_s = Node.removeSec old_s inst |
436 | 2cae47e9 | Iustin Pop | force_p = Node.offline old_p |
437 | b161386d | Iustin Pop | new_nl = do -- Maybe monad |
438 | 2cae47e9 | Iustin Pop | new_p <- Node.addPriEx force_p int_s inst |
439 | 2cae47e9 | Iustin Pop | new_s <- Node.addSecEx force_p tgt_n inst old_sdx |
440 | 8880d889 | Iustin Pop | let new_inst = Instance.setBoth inst old_sdx new_sdx |
441 | 8880d889 | Iustin Pop | return (Container.add new_sdx new_s $ |
442 | 8880d889 | Iustin Pop | Container.addTwo old_sdx new_p old_pdx int_p nl, |
443 | 8880d889 | Iustin Pop | new_inst, old_sdx, new_sdx) |
444 | 8880d889 | Iustin Pop | in new_nl |
445 | 19493d33 | Iustin Pop | |
446 | 9188aeef | Iustin Pop | -- | Tries to allocate an instance on one given node. |
447 | 0d66ea67 | Iustin Pop | allocateOnSingle :: Node.List -> Instance.Instance -> Ndx |
448 | 1fe81531 | Iustin Pop | -> OpResult Node.AllocElement |
449 | 0d66ea67 | Iustin Pop | allocateOnSingle nl inst new_pdx = |
450 | 0d66ea67 | Iustin Pop | let p = Container.find new_pdx nl |
451 | 8880d889 | Iustin Pop | new_inst = Instance.setBoth inst new_pdx Node.noSecondary |
452 | 7d3f4253 | Iustin Pop | in Node.addPri p inst >>= \new_p -> do |
453 | 7d3f4253 | Iustin Pop | let new_nl = Container.add new_pdx new_p nl |
454 | 7d3f4253 | Iustin Pop | new_score = compCV nl |
455 | 7d3f4253 | Iustin Pop | return (new_nl, new_inst, [new_p], new_score) |
456 | 5e15f460 | Iustin Pop | |
457 | 9188aeef | Iustin Pop | -- | Tries to allocate an instance on a given pair of nodes. |
458 | 0d66ea67 | Iustin Pop | allocateOnPair :: Node.List -> Instance.Instance -> Ndx -> Ndx |
459 | 1fe81531 | Iustin Pop | -> OpResult Node.AllocElement |
460 | 0d66ea67 | Iustin Pop | allocateOnPair nl inst new_pdx new_sdx = |
461 | 0d66ea67 | Iustin Pop | let tgt_p = Container.find new_pdx nl |
462 | 0d66ea67 | Iustin Pop | tgt_s = Container.find new_sdx nl |
463 | 7d3f4253 | Iustin Pop | in do |
464 | 7d3f4253 | Iustin Pop | new_p <- Node.addPri tgt_p inst |
465 | 7d3f4253 | Iustin Pop | new_s <- Node.addSec tgt_s inst new_pdx |
466 | 7d3f4253 | Iustin Pop | let new_inst = Instance.setBoth inst new_pdx new_sdx |
467 | 7d3f4253 | Iustin Pop | new_nl = Container.addTwo new_pdx new_p new_sdx new_s nl |
468 | 7d3f4253 | Iustin Pop | return (new_nl, new_inst, [new_p, new_s], compCV new_nl) |
469 | 4a340313 | Iustin Pop | |
470 | 9188aeef | Iustin Pop | -- | Tries to perform an instance move and returns the best table |
471 | 9188aeef | Iustin Pop | -- between the original one and the new one. |
472 | e4f08c46 | Iustin Pop | checkSingleStep :: Table -- ^ The original table |
473 | e4f08c46 | Iustin Pop | -> Instance.Instance -- ^ The instance to move |
474 | e4f08c46 | Iustin Pop | -> Table -- ^ The current best table |
475 | e4f08c46 | Iustin Pop | -> IMove -- ^ The move to apply |
476 | e4f08c46 | Iustin Pop | -> Table -- ^ The final best table |
477 | e4f08c46 | Iustin Pop | checkSingleStep ini_tbl target cur_tbl move = |
478 | e4f08c46 | Iustin Pop | let |
479 | e4f08c46 | Iustin Pop | Table ini_nl ini_il _ ini_plc = ini_tbl |
480 | 8880d889 | Iustin Pop | tmp_resu = applyMove ini_nl target move |
481 | e4f08c46 | Iustin Pop | in |
482 | 8880d889 | Iustin Pop | case tmp_resu of |
483 | f2280553 | Iustin Pop | OpFail _ -> cur_tbl |
484 | 3173c987 | Iustin Pop | OpGood (upd_nl, new_inst, pri_idx, sec_idx) -> |
485 | f2280553 | Iustin Pop | let tgt_idx = Instance.idx target |
486 | f2280553 | Iustin Pop | upd_cvar = compCV upd_nl |
487 | f2280553 | Iustin Pop | upd_il = Container.add tgt_idx new_inst ini_il |
488 | 3173c987 | Iustin Pop | upd_plc = (tgt_idx, pri_idx, sec_idx, move, upd_cvar):ini_plc |
489 | f2280553 | Iustin Pop | upd_tbl = Table upd_nl upd_il upd_cvar upd_plc |
490 | f2280553 | Iustin Pop | in |
491 | f2280553 | Iustin Pop | compareTables cur_tbl upd_tbl |
492 | e4f08c46 | Iustin Pop | |
493 | c0501c69 | Iustin Pop | -- | Given the status of the current secondary as a valid new node and |
494 | c0501c69 | Iustin Pop | -- the current candidate target node, generate the possible moves for |
495 | c0501c69 | Iustin Pop | -- a instance. |
496 | c0501c69 | Iustin Pop | possibleMoves :: Bool -- ^ Whether the secondary node is a valid new node |
497 | e08424a8 | Guido Trotter | -> Bool -- ^ Whether we can change the primary node |
498 | c0501c69 | Iustin Pop | -> Ndx -- ^ Target node candidate |
499 | c0501c69 | Iustin Pop | -> [IMove] -- ^ List of valid result moves |
500 | e08424a8 | Guido Trotter | |
501 | e08424a8 | Guido Trotter | possibleMoves _ False tdx = |
502 | e08424a8 | Guido Trotter | [ReplaceSecondary tdx] |
503 | e08424a8 | Guido Trotter | |
504 | e08424a8 | Guido Trotter | possibleMoves True True tdx = |
505 | 40d4eba0 | Iustin Pop | [ReplaceSecondary tdx, |
506 | 40d4eba0 | Iustin Pop | ReplaceAndFailover tdx, |
507 | 40d4eba0 | Iustin Pop | ReplacePrimary tdx, |
508 | 40d4eba0 | Iustin Pop | FailoverAndReplace tdx] |
509 | 40d4eba0 | Iustin Pop | |
510 | e08424a8 | Guido Trotter | possibleMoves False True tdx = |
511 | 40d4eba0 | Iustin Pop | [ReplaceSecondary tdx, |
512 | 40d4eba0 | Iustin Pop | ReplaceAndFailover tdx] |
513 | 40d4eba0 | Iustin Pop | |
514 | 40d4eba0 | Iustin Pop | -- | Compute the best move for a given instance. |
515 | c0501c69 | Iustin Pop | checkInstanceMove :: [Ndx] -- ^ Allowed target node indices |
516 | c0501c69 | Iustin Pop | -> Bool -- ^ Whether disk moves are allowed |
517 | e08424a8 | Guido Trotter | -> Bool -- ^ Whether instance moves are allowed |
518 | c0501c69 | Iustin Pop | -> Table -- ^ Original table |
519 | c0501c69 | Iustin Pop | -> Instance.Instance -- ^ Instance to move |
520 | c0501c69 | Iustin Pop | -> Table -- ^ Best new table for this instance |
521 | e08424a8 | Guido Trotter | checkInstanceMove nodes_idx disk_moves inst_moves ini_tbl target = |
522 | 4e25d1c2 | Iustin Pop | let |
523 | 2060348b | Iustin Pop | opdx = Instance.pNode target |
524 | 2060348b | Iustin Pop | osdx = Instance.sNode target |
525 | 9dc6023f | Iustin Pop | nodes = filter (\idx -> idx /= opdx && idx /= osdx) nodes_idx |
526 | e08424a8 | Guido Trotter | use_secondary = elem osdx nodes_idx && inst_moves |
527 | 40d4eba0 | Iustin Pop | aft_failover = if use_secondary -- if allowed to failover |
528 | 40d4eba0 | Iustin Pop | then checkSingleStep ini_tbl target ini_tbl Failover |
529 | 40d4eba0 | Iustin Pop | else ini_tbl |
530 | c0501c69 | Iustin Pop | all_moves = if disk_moves |
531 | e08424a8 | Guido Trotter | then concatMap |
532 | e08424a8 | Guido Trotter | (possibleMoves use_secondary inst_moves) nodes |
533 | c0501c69 | Iustin Pop | else [] |
534 | 4e25d1c2 | Iustin Pop | in |
535 | 4e25d1c2 | Iustin Pop | -- iterate over the possible nodes for this instance |
536 | 9dc6023f | Iustin Pop | foldl' (checkSingleStep ini_tbl target) aft_failover all_moves |
537 | 4e25d1c2 | Iustin Pop | |
538 | e4f08c46 | Iustin Pop | -- | Compute the best next move. |
539 | 608efcce | Iustin Pop | checkMove :: [Ndx] -- ^ Allowed target node indices |
540 | c0501c69 | Iustin Pop | -> Bool -- ^ Whether disk moves are allowed |
541 | e08424a8 | Guido Trotter | -> Bool -- ^ Whether instance moves are allowed |
542 | 256810de | Iustin Pop | -> Table -- ^ The current solution |
543 | e4f08c46 | Iustin Pop | -> [Instance.Instance] -- ^ List of instances still to move |
544 | 256810de | Iustin Pop | -> Table -- ^ The new solution |
545 | e08424a8 | Guido Trotter | checkMove nodes_idx disk_moves inst_moves ini_tbl victims = |
546 | 4e25d1c2 | Iustin Pop | let Table _ _ _ ini_plc = ini_tbl |
547 | a3eee4ad | Iustin Pop | -- we're using rwhnf from the Control.Parallel.Strategies |
548 | a3eee4ad | Iustin Pop | -- package; we don't need to use rnf as that would force too |
549 | a3eee4ad | Iustin Pop | -- much evaluation in single-threaded cases, and in |
550 | a3eee4ad | Iustin Pop | -- multi-threaded case the weak head normal form is enough to |
551 | a3eee4ad | Iustin Pop | -- spark the evaluation |
552 | e08424a8 | Guido Trotter | tables = parMap rwhnf (checkInstanceMove nodes_idx disk_moves |
553 | e08424a8 | Guido Trotter | inst_moves ini_tbl) |
554 | a3eee4ad | Iustin Pop | victims |
555 | 4e25d1c2 | Iustin Pop | -- iterate over all instances, computing the best move |
556 | 2a8e2dc9 | Iustin Pop | best_tbl = foldl' compareTables ini_tbl tables |
557 | aaaa0e43 | Iustin Pop | Table _ _ _ best_plc = best_tbl |
558 | a804261a | Iustin Pop | in if length best_plc == length ini_plc |
559 | a804261a | Iustin Pop | then ini_tbl -- no advancement |
560 | a804261a | Iustin Pop | else best_tbl |
561 | e4f08c46 | Iustin Pop | |
562 | 525bfb36 | Iustin Pop | -- | Check if we are allowed to go deeper in the balancing. |
563 | 3fea6959 | Iustin Pop | doNextBalance :: Table -- ^ The starting table |
564 | 3fea6959 | Iustin Pop | -> Int -- ^ Remaining length |
565 | 3fea6959 | Iustin Pop | -> Score -- ^ Score at which to stop |
566 | 3fea6959 | Iustin Pop | -> Bool -- ^ The resulting table and commands |
567 | 5ad86777 | Iustin Pop | doNextBalance ini_tbl max_rounds min_score = |
568 | 5ad86777 | Iustin Pop | let Table _ _ ini_cv ini_plc = ini_tbl |
569 | 5ad86777 | Iustin Pop | ini_plc_len = length ini_plc |
570 | 5ad86777 | Iustin Pop | in (max_rounds < 0 || ini_plc_len < max_rounds) && ini_cv > min_score |
571 | 5ad86777 | Iustin Pop | |
572 | 525bfb36 | Iustin Pop | -- | Run a balance move. |
573 | f25e5aac | Iustin Pop | tryBalance :: Table -- ^ The starting table |
574 | f25e5aac | Iustin Pop | -> Bool -- ^ Allow disk moves |
575 | e08424a8 | Guido Trotter | -> Bool -- ^ Allow instance moves |
576 | 2e28ac32 | Iustin Pop | -> Bool -- ^ Only evacuate moves |
577 | 848b65c9 | Iustin Pop | -> Score -- ^ Min gain threshold |
578 | 848b65c9 | Iustin Pop | -> Score -- ^ Min gain |
579 | f25e5aac | Iustin Pop | -> Maybe Table -- ^ The resulting table and commands |
580 | e08424a8 | Guido Trotter | tryBalance ini_tbl disk_moves inst_moves evac_mode mg_limit min_gain = |
581 | 5ad86777 | Iustin Pop | let Table ini_nl ini_il ini_cv _ = ini_tbl |
582 | 5ad86777 | Iustin Pop | all_inst = Container.elems ini_il |
583 | 2e28ac32 | Iustin Pop | all_inst' = if evac_mode |
584 | 2e28ac32 | Iustin Pop | then let bad_nodes = map Node.idx . filter Node.offline $ |
585 | 2e28ac32 | Iustin Pop | Container.elems ini_nl |
586 | 2e28ac32 | Iustin Pop | in filter (\e -> Instance.sNode e `elem` bad_nodes || |
587 | 2e28ac32 | Iustin Pop | Instance.pNode e `elem` bad_nodes) |
588 | 2e28ac32 | Iustin Pop | all_inst |
589 | 2e28ac32 | Iustin Pop | else all_inst |
590 | c424cdc8 | Iustin Pop | reloc_inst = filter Instance.movable all_inst' |
591 | 5ad86777 | Iustin Pop | node_idx = map Node.idx . filter (not . Node.offline) $ |
592 | 5ad86777 | Iustin Pop | Container.elems ini_nl |
593 | e08424a8 | Guido Trotter | fin_tbl = checkMove node_idx disk_moves inst_moves ini_tbl reloc_inst |
594 | 5ad86777 | Iustin Pop | (Table _ _ fin_cv _) = fin_tbl |
595 | f25e5aac | Iustin Pop | in |
596 | 848b65c9 | Iustin Pop | if fin_cv < ini_cv && (ini_cv > mg_limit || ini_cv - fin_cv >= min_gain) |
597 | 5ad86777 | Iustin Pop | then Just fin_tbl -- this round made success, return the new table |
598 | f25e5aac | Iustin Pop | else Nothing |
599 | f25e5aac | Iustin Pop | |
600 | 478df686 | Iustin Pop | -- * Allocation functions |
601 | 478df686 | Iustin Pop | |
602 | 525bfb36 | Iustin Pop | -- | Build failure stats out of a list of failures. |
603 | 478df686 | Iustin Pop | collapseFailures :: [FailMode] -> FailStats |
604 | 478df686 | Iustin Pop | collapseFailures flst = |
605 | 5182e970 | Iustin Pop | map (\k -> (k, length $ filter (k ==) flst)) [minBound..maxBound] |
606 | 478df686 | Iustin Pop | |
607 | 478df686 | Iustin Pop | -- | Update current Allocation solution and failure stats with new |
608 | 525bfb36 | Iustin Pop | -- elements. |
609 | 1fe81531 | Iustin Pop | concatAllocs :: AllocSolution -> OpResult Node.AllocElement -> AllocSolution |
610 | 85d0ddc3 | Iustin Pop | concatAllocs as (OpFail reason) = as { asFailures = reason : asFailures as } |
611 | 478df686 | Iustin Pop | |
612 | 85d0ddc3 | Iustin Pop | concatAllocs as (OpGood ns@(_, _, _, nscore)) = |
613 | 7d3f4253 | Iustin Pop | let -- Choose the old or new solution, based on the cluster score |
614 | 85d0ddc3 | Iustin Pop | cntok = asAllocs as |
615 | 85d0ddc3 | Iustin Pop | osols = asSolutions as |
616 | 478df686 | Iustin Pop | nsols = case osols of |
617 | a334d536 | Iustin Pop | [] -> [ns] |
618 | a334d536 | Iustin Pop | (_, _, _, oscore):[] -> |
619 | 478df686 | Iustin Pop | if oscore < nscore |
620 | 478df686 | Iustin Pop | then osols |
621 | a334d536 | Iustin Pop | else [ns] |
622 | 23f9ab76 | Iustin Pop | -- FIXME: here we simply concat to lists with more |
623 | 23f9ab76 | Iustin Pop | -- than one element; we should instead abort, since |
624 | 23f9ab76 | Iustin Pop | -- this is not a valid usage of this function |
625 | a334d536 | Iustin Pop | xs -> ns:xs |
626 | fbb95f28 | Iustin Pop | nsuc = cntok + 1 |
627 | 478df686 | Iustin Pop | -- Note: we force evaluation of nsols here in order to keep the |
628 | 478df686 | Iustin Pop | -- memory profile low - we know that we will need nsols for sure |
629 | 478df686 | Iustin Pop | -- in the next cycle, so we force evaluation of nsols, since the |
630 | 478df686 | Iustin Pop | -- foldl' in the caller will only evaluate the tuple, but not the |
631 | 7d11799b | Iustin Pop | -- elements of the tuple |
632 | 85d0ddc3 | Iustin Pop | in nsols `seq` nsuc `seq` as { asAllocs = nsuc, asSolutions = nsols } |
633 | dbba5246 | Iustin Pop | |
634 | 1bc47d38 | Iustin Pop | -- | Sums two allocation solutions (e.g. for two separate node groups). |
635 | 1bc47d38 | Iustin Pop | sumAllocs :: AllocSolution -> AllocSolution -> AllocSolution |
636 | 1bc47d38 | Iustin Pop | sumAllocs (AllocSolution af aa as al) (AllocSolution bf ba bs bl) = |
637 | 1bc47d38 | Iustin Pop | AllocSolution (af ++ bf) (aa + ba) (as ++ bs) (al ++ bl) |
638 | 1bc47d38 | Iustin Pop | |
639 | 525bfb36 | Iustin Pop | -- | Given a solution, generates a reasonable description for it. |
640 | 859fc11d | Iustin Pop | describeSolution :: AllocSolution -> String |
641 | 859fc11d | Iustin Pop | describeSolution as = |
642 | 859fc11d | Iustin Pop | let fcnt = asFailures as |
643 | 859fc11d | Iustin Pop | sols = asSolutions as |
644 | 859fc11d | Iustin Pop | freasons = |
645 | 859fc11d | Iustin Pop | intercalate ", " . map (\(a, b) -> printf "%s: %d" (show a) b) . |
646 | 859fc11d | Iustin Pop | filter ((> 0) . snd) . collapseFailures $ fcnt |
647 | 859fc11d | Iustin Pop | in if null sols |
648 | 859fc11d | Iustin Pop | then "No valid allocation solutions, failure reasons: " ++ |
649 | 859fc11d | Iustin Pop | (if null fcnt |
650 | 859fc11d | Iustin Pop | then "unknown reasons" |
651 | 859fc11d | Iustin Pop | else freasons) |
652 | 859fc11d | Iustin Pop | else let (_, _, nodes, cv) = head sols |
653 | 859fc11d | Iustin Pop | in printf ("score: %.8f, successes %d, failures %d (%s)" ++ |
654 | 859fc11d | Iustin Pop | " for node(s) %s") cv (asAllocs as) (length fcnt) freasons |
655 | 859fc11d | Iustin Pop | (intercalate "/" . map Node.name $ nodes) |
656 | 859fc11d | Iustin Pop | |
657 | 525bfb36 | Iustin Pop | -- | Annotates a solution with the appropriate string. |
658 | 859fc11d | Iustin Pop | annotateSolution :: AllocSolution -> AllocSolution |
659 | 859fc11d | Iustin Pop | annotateSolution as = as { asLog = describeSolution as : asLog as } |
660 | 859fc11d | Iustin Pop | |
661 | 47eed3f4 | Iustin Pop | -- | Reverses an evacuation solution. |
662 | 47eed3f4 | Iustin Pop | -- |
663 | 47eed3f4 | Iustin Pop | -- Rationale: we always concat the results to the top of the lists, so |
664 | 47eed3f4 | Iustin Pop | -- for proper jobset execution, we should reverse all lists. |
665 | 47eed3f4 | Iustin Pop | reverseEvacSolution :: EvacSolution -> EvacSolution |
666 | 47eed3f4 | Iustin Pop | reverseEvacSolution (EvacSolution f m o) = |
667 | 47eed3f4 | Iustin Pop | EvacSolution (reverse f) (reverse m) (reverse o) |
668 | 47eed3f4 | Iustin Pop | |
669 | 6cb1649f | Iustin Pop | -- | Generate the valid node allocation singles or pairs for a new instance. |
670 | 6d0bc5ca | Iustin Pop | genAllocNodes :: Group.List -- ^ Group list |
671 | 6d0bc5ca | Iustin Pop | -> Node.List -- ^ The node map |
672 | 6cb1649f | Iustin Pop | -> Int -- ^ The number of nodes required |
673 | 6d0bc5ca | Iustin Pop | -> Bool -- ^ Whether to drop or not |
674 | 6d0bc5ca | Iustin Pop | -- unallocable nodes |
675 | 6cb1649f | Iustin Pop | -> Result AllocNodes -- ^ The (monadic) result |
676 | 6d0bc5ca | Iustin Pop | genAllocNodes gl nl count drop_unalloc = |
677 | 6d0bc5ca | Iustin Pop | let filter_fn = if drop_unalloc |
678 | e4491427 | Iustin Pop | then filter (Group.isAllocable . |
679 | e4491427 | Iustin Pop | flip Container.find gl . Node.group) |
680 | 6d0bc5ca | Iustin Pop | else id |
681 | 6d0bc5ca | Iustin Pop | all_nodes = filter_fn $ getOnline nl |
682 | 6cb1649f | Iustin Pop | all_pairs = liftM2 (,) all_nodes all_nodes |
683 | 6cb1649f | Iustin Pop | ok_pairs = filter (\(x, y) -> Node.idx x /= Node.idx y && |
684 | 6cb1649f | Iustin Pop | Node.group x == Node.group y) all_pairs |
685 | 6cb1649f | Iustin Pop | in case count of |
686 | 0d66ea67 | Iustin Pop | 1 -> Ok (Left (map Node.idx all_nodes)) |
687 | 0d66ea67 | Iustin Pop | 2 -> Ok (Right (map (\(p, s) -> (Node.idx p, Node.idx s)) ok_pairs)) |
688 | 6cb1649f | Iustin Pop | _ -> Bad "Unsupported number of nodes, only one or two supported" |
689 | 6cb1649f | Iustin Pop | |
690 | dbba5246 | Iustin Pop | -- | Try to allocate an instance on the cluster. |
691 | dbba5246 | Iustin Pop | tryAlloc :: (Monad m) => |
692 | dbba5246 | Iustin Pop | Node.List -- ^ The node list |
693 | dbba5246 | Iustin Pop | -> Instance.List -- ^ The instance list |
694 | dbba5246 | Iustin Pop | -> Instance.Instance -- ^ The instance to allocate |
695 | 6cb1649f | Iustin Pop | -> AllocNodes -- ^ The allocation targets |
696 | 78694255 | Iustin Pop | -> m AllocSolution -- ^ Possible solution list |
697 | 6cb1649f | Iustin Pop | tryAlloc nl _ inst (Right ok_pairs) = |
698 | 6cb1649f | Iustin Pop | let sols = foldl' (\cstate (p, s) -> |
699 | 478df686 | Iustin Pop | concatAllocs cstate $ allocateOnPair nl inst p s |
700 | 97936d51 | Iustin Pop | ) emptyAllocSolution ok_pairs |
701 | 859fc11d | Iustin Pop | |
702 | dec88196 | Iustin Pop | in if null ok_pairs -- means we have just one node |
703 | dec88196 | Iustin Pop | then fail "Not enough online nodes" |
704 | dec88196 | Iustin Pop | else return $ annotateSolution sols |
705 | dbba5246 | Iustin Pop | |
706 | 6cb1649f | Iustin Pop | tryAlloc nl _ inst (Left all_nodes) = |
707 | 6cb1649f | Iustin Pop | let sols = foldl' (\cstate -> |
708 | 2485487d | Iustin Pop | concatAllocs cstate . allocateOnSingle nl inst |
709 | 97936d51 | Iustin Pop | ) emptyAllocSolution all_nodes |
710 | dec88196 | Iustin Pop | in if null all_nodes |
711 | dec88196 | Iustin Pop | then fail "No online nodes" |
712 | dec88196 | Iustin Pop | else return $ annotateSolution sols |
713 | dbba5246 | Iustin Pop | |
714 | 525bfb36 | Iustin Pop | -- | Given a group/result, describe it as a nice (list of) messages. |
715 | aec636b9 | Iustin Pop | solutionDescription :: Group.List -> (Gdx, Result AllocSolution) -> [String] |
716 | aec636b9 | Iustin Pop | solutionDescription gl (groupId, result) = |
717 | 9b1584fc | Iustin Pop | case result of |
718 | 73206d0a | Iustin Pop | Ok solution -> map (printf "Group %s (%s): %s" gname pol) (asLog solution) |
719 | aec636b9 | Iustin Pop | Bad message -> [printf "Group %s: error %s" gname message] |
720 | 73206d0a | Iustin Pop | where grp = Container.find groupId gl |
721 | 73206d0a | Iustin Pop | gname = Group.name grp |
722 | 73206d0a | Iustin Pop | pol = apolToString (Group.allocPolicy grp) |
723 | 9b1584fc | Iustin Pop | |
724 | 9b1584fc | Iustin Pop | -- | From a list of possibly bad and possibly empty solutions, filter |
725 | 88253d03 | Iustin Pop | -- only the groups with a valid result. Note that the result will be |
726 | 525bfb36 | Iustin Pop | -- reversed compared to the original list. |
727 | 73206d0a | Iustin Pop | filterMGResults :: Group.List |
728 | 73206d0a | Iustin Pop | -> [(Gdx, Result AllocSolution)] |
729 | 73206d0a | Iustin Pop | -> [(Gdx, AllocSolution)] |
730 | 88253d03 | Iustin Pop | filterMGResults gl = foldl' fn [] |
731 | 88253d03 | Iustin Pop | where unallocable = not . Group.isAllocable . flip Container.find gl |
732 | 88253d03 | Iustin Pop | fn accu (gdx, rasol) = |
733 | 88253d03 | Iustin Pop | case rasol of |
734 | 88253d03 | Iustin Pop | Bad _ -> accu |
735 | 88253d03 | Iustin Pop | Ok sol | null (asSolutions sol) -> accu |
736 | 88253d03 | Iustin Pop | | unallocable gdx -> accu |
737 | 88253d03 | Iustin Pop | | otherwise -> (gdx, sol):accu |
738 | 9b1584fc | Iustin Pop | |
739 | 525bfb36 | Iustin Pop | -- | Sort multigroup results based on policy and score. |
740 | 73206d0a | Iustin Pop | sortMGResults :: Group.List |
741 | 73206d0a | Iustin Pop | -> [(Gdx, AllocSolution)] |
742 | 73206d0a | Iustin Pop | -> [(Gdx, AllocSolution)] |
743 | 73206d0a | Iustin Pop | sortMGResults gl sols = |
744 | 2a8e2dc9 | Iustin Pop | let extractScore (_, _, _, x) = x |
745 | 73206d0a | Iustin Pop | solScore (gdx, sol) = (Group.allocPolicy (Container.find gdx gl), |
746 | 73206d0a | Iustin Pop | (extractScore . head . asSolutions) sol) |
747 | 73206d0a | Iustin Pop | in sortBy (comparing solScore) sols |
748 | 73206d0a | Iustin Pop | |
749 | 9b1584fc | Iustin Pop | -- | Try to allocate an instance on a multi-group cluster. |
750 | aec636b9 | Iustin Pop | tryMGAlloc :: Group.List -- ^ The group list |
751 | aec636b9 | Iustin Pop | -> Node.List -- ^ The node list |
752 | aec636b9 | Iustin Pop | -> Instance.List -- ^ The instance list |
753 | aec636b9 | Iustin Pop | -> Instance.Instance -- ^ The instance to allocate |
754 | aec636b9 | Iustin Pop | -> Int -- ^ Required number of nodes |
755 | aec636b9 | Iustin Pop | -> Result AllocSolution -- ^ Possible solution list |
756 | aec636b9 | Iustin Pop | tryMGAlloc mggl mgnl mgil inst cnt = |
757 | 9b1584fc | Iustin Pop | let groups = splitCluster mgnl mgil |
758 | 9b1584fc | Iustin Pop | sols = map (\(gid, (nl, il)) -> |
759 | 6d0bc5ca | Iustin Pop | (gid, genAllocNodes mggl nl cnt False >>= |
760 | 6d0bc5ca | Iustin Pop | tryAlloc nl il inst)) |
761 | 6cb1649f | Iustin Pop | groups::[(Gdx, Result AllocSolution)] |
762 | aec636b9 | Iustin Pop | all_msgs = concatMap (solutionDescription mggl) sols |
763 | 73206d0a | Iustin Pop | goodSols = filterMGResults mggl sols |
764 | 73206d0a | Iustin Pop | sortedSols = sortMGResults mggl goodSols |
765 | 9b1584fc | Iustin Pop | in if null sortedSols |
766 | 9b1584fc | Iustin Pop | then Bad $ intercalate ", " all_msgs |
767 | 9b1584fc | Iustin Pop | else let (final_group, final_sol) = head sortedSols |
768 | aec636b9 | Iustin Pop | final_name = Group.name $ Container.find final_group mggl |
769 | aec636b9 | Iustin Pop | selmsg = "Selected group: " ++ final_name |
770 | 9b1584fc | Iustin Pop | in Ok $ final_sol { asLog = selmsg:all_msgs } |
771 | 9b1584fc | Iustin Pop | |
772 | 9b1584fc | Iustin Pop | -- | Try to relocate an instance on the cluster. |
773 | dbba5246 | Iustin Pop | tryReloc :: (Monad m) => |
774 | 78694255 | Iustin Pop | Node.List -- ^ The node list |
775 | 78694255 | Iustin Pop | -> Instance.List -- ^ The instance list |
776 | 78694255 | Iustin Pop | -> Idx -- ^ The index of the instance to move |
777 | 478df686 | Iustin Pop | -> Int -- ^ The number of nodes required |
778 | 78694255 | Iustin Pop | -> [Ndx] -- ^ Nodes which should not be used |
779 | 78694255 | Iustin Pop | -> m AllocSolution -- ^ Solution list |
780 | dbba5246 | Iustin Pop | tryReloc nl il xid 1 ex_idx = |
781 | dbba5246 | Iustin Pop | let all_nodes = getOnline nl |
782 | dbba5246 | Iustin Pop | inst = Container.find xid il |
783 | 2060348b | Iustin Pop | ex_idx' = Instance.pNode inst:ex_idx |
784 | dbba5246 | Iustin Pop | valid_nodes = filter (not . flip elem ex_idx' . Node.idx) all_nodes |
785 | dbba5246 | Iustin Pop | valid_idxes = map Node.idx valid_nodes |
786 | 478df686 | Iustin Pop | sols1 = foldl' (\cstate x -> |
787 | fbb95f28 | Iustin Pop | let em = do |
788 | 478df686 | Iustin Pop | (mnl, i, _, _) <- |
789 | 478df686 | Iustin Pop | applyMove nl inst (ReplaceSecondary x) |
790 | 7d3f4253 | Iustin Pop | return (mnl, i, [Container.find x mnl], |
791 | 7d3f4253 | Iustin Pop | compCV mnl) |
792 | fbb95f28 | Iustin Pop | in concatAllocs cstate em |
793 | 97936d51 | Iustin Pop | ) emptyAllocSolution valid_idxes |
794 | dbba5246 | Iustin Pop | in return sols1 |
795 | dbba5246 | Iustin Pop | |
796 | dbba5246 | Iustin Pop | tryReloc _ _ _ reqn _ = fail $ "Unsupported number of relocation \ |
797 | 9f6dcdea | Iustin Pop | \destinations required (" ++ show reqn ++ |
798 | dbba5246 | Iustin Pop | "), only one supported" |
799 | e4f08c46 | Iustin Pop | |
800 | 4bc33d60 | Iustin Pop | tryMGReloc :: (Monad m) => |
801 | 4bc33d60 | Iustin Pop | Group.List -- ^ The group list |
802 | 4bc33d60 | Iustin Pop | -> Node.List -- ^ The node list |
803 | 4bc33d60 | Iustin Pop | -> Instance.List -- ^ The instance list |
804 | 4bc33d60 | Iustin Pop | -> Idx -- ^ The index of the instance to move |
805 | 4bc33d60 | Iustin Pop | -> Int -- ^ The number of nodes required |
806 | 4bc33d60 | Iustin Pop | -> [Ndx] -- ^ Nodes which should not be used |
807 | 4bc33d60 | Iustin Pop | -> m AllocSolution -- ^ Solution list |
808 | 4bc33d60 | Iustin Pop | tryMGReloc _ mgnl mgil xid ncount ex_ndx = do |
809 | 4bc33d60 | Iustin Pop | let groups = splitCluster mgnl mgil |
810 | 4bc33d60 | Iustin Pop | -- TODO: we only relocate inside the group for now |
811 | 4bc33d60 | Iustin Pop | inst = Container.find xid mgil |
812 | 4bc33d60 | Iustin Pop | (nl, il) <- case lookup (instancePriGroup mgnl inst) groups of |
813 | 4bc33d60 | Iustin Pop | Nothing -> fail $ "Cannot find group for instance " ++ |
814 | 4bc33d60 | Iustin Pop | Instance.name inst |
815 | 4bc33d60 | Iustin Pop | Just v -> return v |
816 | 4bc33d60 | Iustin Pop | tryReloc nl il xid ncount ex_ndx |
817 | 4bc33d60 | Iustin Pop | |
818 | 525bfb36 | Iustin Pop | -- | Change an instance's secondary node. |
819 | 2ca68e2b | Iustin Pop | evacInstance :: (Monad m) => |
820 | 2ca68e2b | Iustin Pop | [Ndx] -- ^ Excluded nodes |
821 | 2ca68e2b | Iustin Pop | -> Instance.List -- ^ The current instance list |
822 | 2ca68e2b | Iustin Pop | -> (Node.List, AllocSolution) -- ^ The current state |
823 | 2ca68e2b | Iustin Pop | -> Idx -- ^ The instance to evacuate |
824 | 2ca68e2b | Iustin Pop | -> m (Node.List, AllocSolution) |
825 | 2ca68e2b | Iustin Pop | evacInstance ex_ndx il (nl, old_as) idx = do |
826 | 2ca68e2b | Iustin Pop | -- FIXME: hardcoded one node here |
827 | 2ca68e2b | Iustin Pop | |
828 | 2ca68e2b | Iustin Pop | -- Longer explanation: evacuation is currently hardcoded to DRBD |
829 | 2ca68e2b | Iustin Pop | -- instances (which have one secondary); hence, even if the |
830 | 2ca68e2b | Iustin Pop | -- IAllocator protocol can request N nodes for an instance, and all |
831 | 2ca68e2b | Iustin Pop | -- the message parsing/loading pass this, this implementation only |
832 | 2ca68e2b | Iustin Pop | -- supports one; this situation needs to be revisited if we ever |
833 | 2ca68e2b | Iustin Pop | -- support more than one secondary, or if we change the storage |
834 | 2ca68e2b | Iustin Pop | -- model |
835 | 2ca68e2b | Iustin Pop | new_as <- tryReloc nl il idx 1 ex_ndx |
836 | 2ca68e2b | Iustin Pop | case asSolutions new_as of |
837 | 2ca68e2b | Iustin Pop | -- an individual relocation succeeded, we kind of compose the data |
838 | 2ca68e2b | Iustin Pop | -- from the two solutions |
839 | 2ca68e2b | Iustin Pop | csol@(nl', _, _, _):_ -> |
840 | 2ca68e2b | Iustin Pop | return (nl', new_as { asSolutions = csol:asSolutions old_as }) |
841 | 2ca68e2b | Iustin Pop | -- this relocation failed, so we fail the entire evac |
842 | 2ca68e2b | Iustin Pop | _ -> fail $ "Can't evacuate instance " ++ |
843 | 2ca68e2b | Iustin Pop | Instance.name (Container.find idx il) ++ |
844 | 2ca68e2b | Iustin Pop | ": " ++ describeSolution new_as |
845 | 2ca68e2b | Iustin Pop | |
846 | 3fea6959 | Iustin Pop | -- | Try to evacuate a list of nodes. |
847 | 12b0511d | Iustin Pop | tryEvac :: (Monad m) => |
848 | 12b0511d | Iustin Pop | Node.List -- ^ The node list |
849 | 12b0511d | Iustin Pop | -> Instance.List -- ^ The instance list |
850 | 1bc47d38 | Iustin Pop | -> [Idx] -- ^ Instances to be evacuated |
851 | 1bc47d38 | Iustin Pop | -> [Ndx] -- ^ Restricted nodes (the ones being evacuated) |
852 | 12b0511d | Iustin Pop | -> m AllocSolution -- ^ Solution list |
853 | 1bc47d38 | Iustin Pop | tryEvac nl il idxs ex_ndx = do |
854 | 97936d51 | Iustin Pop | (_, sol) <- foldM (evacInstance ex_ndx il) (nl, emptyAllocSolution) idxs |
855 | 1bc47d38 | Iustin Pop | return sol |
856 | 1bc47d38 | Iustin Pop | |
857 | 1bc47d38 | Iustin Pop | -- | Multi-group evacuation of a list of nodes. |
858 | 1bc47d38 | Iustin Pop | tryMGEvac :: (Monad m) => |
859 | 1bc47d38 | Iustin Pop | Group.List -- ^ The group list |
860 | 1bc47d38 | Iustin Pop | -> Node.List -- ^ The node list |
861 | 1bc47d38 | Iustin Pop | -> Instance.List -- ^ The instance list |
862 | 1bc47d38 | Iustin Pop | -> [Ndx] -- ^ Nodes to be evacuated |
863 | 1bc47d38 | Iustin Pop | -> m AllocSolution -- ^ Solution list |
864 | 1bc47d38 | Iustin Pop | tryMGEvac _ nl il ex_ndx = |
865 | 5182e970 | Iustin Pop | let ex_nodes = map (`Container.find` nl) ex_ndx |
866 | 5182e970 | Iustin Pop | all_insts = nub . concatMap Node.sList $ ex_nodes |
867 | 1f4ae205 | Iustin Pop | all_insts' = associateIdxs all_insts $ splitCluster nl il |
868 | 12b0511d | Iustin Pop | in do |
869 | 1f4ae205 | Iustin Pop | results <- mapM (\(_, (gnl, gil, idxs)) -> tryEvac gnl gil idxs ex_ndx) |
870 | 1f4ae205 | Iustin Pop | all_insts' |
871 | 97936d51 | Iustin Pop | let sol = foldl' sumAllocs emptyAllocSolution results |
872 | 859fc11d | Iustin Pop | return $ annotateSolution sol |
873 | 12b0511d | Iustin Pop | |
874 | 47eed3f4 | Iustin Pop | -- | Function which fails if the requested mode is change secondary. |
875 | 47eed3f4 | Iustin Pop | -- |
876 | 47eed3f4 | Iustin Pop | -- This is useful since except DRBD, no other disk template can |
877 | 47eed3f4 | Iustin Pop | -- execute change secondary; thus, we can just call this function |
878 | 47eed3f4 | Iustin Pop | -- instead of always checking for secondary mode. After the call to |
879 | 47eed3f4 | Iustin Pop | -- this function, whatever mode we have is just a primary change. |
880 | 47eed3f4 | Iustin Pop | failOnSecondaryChange :: (Monad m) => EvacMode -> DiskTemplate -> m () |
881 | 47eed3f4 | Iustin Pop | failOnSecondaryChange ChangeSecondary dt = |
882 | 47eed3f4 | Iustin Pop | fail $ "Instances with disk template '" ++ dtToString dt ++ |
883 | 47eed3f4 | Iustin Pop | "' can't execute change secondary" |
884 | 47eed3f4 | Iustin Pop | failOnSecondaryChange _ _ = return () |
885 | 47eed3f4 | Iustin Pop | |
886 | 47eed3f4 | Iustin Pop | -- | Run evacuation for a single instance. |
887 | 47eed3f4 | Iustin Pop | nodeEvacInstance :: Node.List -- ^ The node list (cluster-wide) |
888 | 47eed3f4 | Iustin Pop | -> Instance.List -- ^ Instance list (cluster-wide) |
889 | 47eed3f4 | Iustin Pop | -> EvacMode -- ^ The evacuation mode |
890 | 47eed3f4 | Iustin Pop | -> Instance.Instance -- ^ The instance to be evacuated |
891 | 47eed3f4 | Iustin Pop | -> [Ndx] -- ^ The list of available nodes |
892 | 47eed3f4 | Iustin Pop | -- for allocation |
893 | 47eed3f4 | Iustin Pop | -> Result (Node.List, Instance.List, [OpCodes.OpCode]) |
894 | 47eed3f4 | Iustin Pop | nodeEvacInstance _ _ mode (Instance.Instance |
895 | 47eed3f4 | Iustin Pop | {Instance.diskTemplate = dt@DTDiskless}) _ = |
896 | 47eed3f4 | Iustin Pop | failOnSecondaryChange mode dt >> |
897 | 47eed3f4 | Iustin Pop | fail "Diskless relocations not implemented yet" |
898 | 47eed3f4 | Iustin Pop | |
899 | 47eed3f4 | Iustin Pop | nodeEvacInstance _ _ _ (Instance.Instance |
900 | 47eed3f4 | Iustin Pop | {Instance.diskTemplate = DTPlain}) _ = |
901 | 47eed3f4 | Iustin Pop | fail "Instances of type plain cannot be relocated" |
902 | 47eed3f4 | Iustin Pop | |
903 | 47eed3f4 | Iustin Pop | nodeEvacInstance _ _ _ (Instance.Instance |
904 | 47eed3f4 | Iustin Pop | {Instance.diskTemplate = DTFile}) _ = |
905 | 47eed3f4 | Iustin Pop | fail "Instances of type file cannot be relocated" |
906 | 47eed3f4 | Iustin Pop | |
907 | 47eed3f4 | Iustin Pop | nodeEvacInstance _ _ mode (Instance.Instance |
908 | 47eed3f4 | Iustin Pop | {Instance.diskTemplate = dt@DTSharedFile}) _ = |
909 | 47eed3f4 | Iustin Pop | failOnSecondaryChange mode dt >> |
910 | 47eed3f4 | Iustin Pop | fail "Shared file relocations not implemented yet" |
911 | 47eed3f4 | Iustin Pop | |
912 | 47eed3f4 | Iustin Pop | nodeEvacInstance _ _ mode (Instance.Instance |
913 | 47eed3f4 | Iustin Pop | {Instance.diskTemplate = dt@DTBlock}) _ = |
914 | 47eed3f4 | Iustin Pop | failOnSecondaryChange mode dt >> |
915 | 47eed3f4 | Iustin Pop | fail "Block device relocations not implemented yet" |
916 | 47eed3f4 | Iustin Pop | |
917 | bef83fd1 | Iustin Pop | nodeEvacInstance nl il ChangePrimary |
918 | bef83fd1 | Iustin Pop | inst@(Instance.Instance {Instance.diskTemplate = DTDrbd8}) _ = |
919 | bef83fd1 | Iustin Pop | do |
920 | bef83fd1 | Iustin Pop | (nl', inst', _, _) <- opToResult $ applyMove nl inst Failover |
921 | bef83fd1 | Iustin Pop | let idx = Instance.idx inst |
922 | bef83fd1 | Iustin Pop | il' = Container.add idx inst' il |
923 | bef83fd1 | Iustin Pop | ops = iMoveToJob nl' il' idx Failover |
924 | bef83fd1 | Iustin Pop | return (nl', il', ops) |
925 | bef83fd1 | Iustin Pop | |
926 | 47eed3f4 | Iustin Pop | nodeEvacInstance _ _ _ (Instance.Instance |
927 | 47eed3f4 | Iustin Pop | {Instance.diskTemplate = DTDrbd8}) _ = |
928 | 47eed3f4 | Iustin Pop | fail "DRBD relocations not implemented yet" |
929 | 47eed3f4 | Iustin Pop | |
930 | 47eed3f4 | Iustin Pop | -- | Computes the local nodes of a given instance which are available |
931 | 47eed3f4 | Iustin Pop | -- for allocation. |
932 | 47eed3f4 | Iustin Pop | availableLocalNodes :: Node.List |
933 | 47eed3f4 | Iustin Pop | -> [(Gdx, [Ndx])] |
934 | 47eed3f4 | Iustin Pop | -> IntSet.IntSet |
935 | 47eed3f4 | Iustin Pop | -> Instance.Instance |
936 | 47eed3f4 | Iustin Pop | -> Result [Ndx] |
937 | 47eed3f4 | Iustin Pop | availableLocalNodes nl group_nodes excl_ndx inst = do |
938 | 47eed3f4 | Iustin Pop | let gdx = instancePriGroup nl inst |
939 | 47eed3f4 | Iustin Pop | local_nodes <- maybe (Bad $ "Can't find group with index " ++ show gdx) |
940 | 47eed3f4 | Iustin Pop | Ok (lookup gdx group_nodes) |
941 | 47eed3f4 | Iustin Pop | let avail_nodes = filter (not . flip IntSet.member excl_ndx) local_nodes |
942 | 47eed3f4 | Iustin Pop | return avail_nodes |
943 | 47eed3f4 | Iustin Pop | |
944 | 47eed3f4 | Iustin Pop | -- | Updates the evac solution with the results of an instance |
945 | 47eed3f4 | Iustin Pop | -- evacuation. |
946 | 47eed3f4 | Iustin Pop | updateEvacSolution :: (Node.List, Instance.List, EvacSolution) |
947 | 47eed3f4 | Iustin Pop | -> Instance.Instance |
948 | 47eed3f4 | Iustin Pop | -> Result (Node.List, Instance.List, [OpCodes.OpCode]) |
949 | 47eed3f4 | Iustin Pop | -> (Node.List, Instance.List, EvacSolution) |
950 | 47eed3f4 | Iustin Pop | updateEvacSolution (nl, il, es) inst (Bad msg) = |
951 | 47eed3f4 | Iustin Pop | (nl, il, es { esFailed = (Instance.name inst ++ ": " ++ msg):esFailed es}) |
952 | 47eed3f4 | Iustin Pop | updateEvacSolution (_, _, es) inst (Ok (nl, il, opcodes)) = |
953 | 47eed3f4 | Iustin Pop | (nl, il, es { esMoved = Instance.name inst:esMoved es |
954 | 47eed3f4 | Iustin Pop | , esOpCodes = [opcodes]:esOpCodes es }) |
955 | 47eed3f4 | Iustin Pop | |
956 | 47eed3f4 | Iustin Pop | -- | Node-evacuation IAllocator mode main function. |
957 | 47eed3f4 | Iustin Pop | tryNodeEvac :: Group.List -- ^ The cluster groups |
958 | 47eed3f4 | Iustin Pop | -> Node.List -- ^ The node list (cluster-wide, not per group) |
959 | 47eed3f4 | Iustin Pop | -> Instance.List -- ^ Instance list (cluster-wide) |
960 | 47eed3f4 | Iustin Pop | -> EvacMode -- ^ The evacuation mode |
961 | 47eed3f4 | Iustin Pop | -> [Idx] -- ^ List of instance (indices) to be evacuated |
962 | 47eed3f4 | Iustin Pop | -> Result EvacSolution |
963 | 47eed3f4 | Iustin Pop | tryNodeEvac _ ini_nl ini_il mode idxs = |
964 | 47eed3f4 | Iustin Pop | let evac_ndx = nodesToEvacuate ini_il mode idxs |
965 | 47eed3f4 | Iustin Pop | offline = map Node.idx . filter Node.offline $ Container.elems ini_nl |
966 | 47eed3f4 | Iustin Pop | excl_ndx = foldl' (flip IntSet.insert) evac_ndx offline |
967 | 47eed3f4 | Iustin Pop | group_ndx = map (\(gdx, (nl, _)) -> (gdx, map Node.idx |
968 | 47eed3f4 | Iustin Pop | (Container.elems nl))) $ |
969 | 47eed3f4 | Iustin Pop | splitCluster ini_nl ini_il |
970 | 47eed3f4 | Iustin Pop | (_, _, esol) = |
971 | 47eed3f4 | Iustin Pop | foldl' (\state@(nl, il, _) inst -> |
972 | 47eed3f4 | Iustin Pop | updateEvacSolution state inst $ |
973 | 47eed3f4 | Iustin Pop | availableLocalNodes nl group_ndx excl_ndx inst >>= |
974 | 47eed3f4 | Iustin Pop | nodeEvacInstance nl il mode inst |
975 | 47eed3f4 | Iustin Pop | ) |
976 | 47eed3f4 | Iustin Pop | (ini_nl, ini_il, emptyEvacSolution) |
977 | 47eed3f4 | Iustin Pop | (map (`Container.find` ini_il) idxs) |
978 | 47eed3f4 | Iustin Pop | in return $ reverseEvacSolution esol |
979 | 47eed3f4 | Iustin Pop | |
980 | 525bfb36 | Iustin Pop | -- | Recursively place instances on the cluster until we're out of space. |
981 | 3ce8009a | Iustin Pop | iterateAlloc :: Node.List |
982 | 3ce8009a | Iustin Pop | -> Instance.List |
983 | 3ce8009a | Iustin Pop | -> Instance.Instance |
984 | 41b5c85a | Iustin Pop | -> AllocNodes |
985 | 3ce8009a | Iustin Pop | -> [Instance.Instance] |
986 | d5ccec02 | Iustin Pop | -> [CStats] |
987 | 40ee14bc | Iustin Pop | -> Result AllocResult |
988 | 41b5c85a | Iustin Pop | iterateAlloc nl il newinst allocnodes ixes cstats = |
989 | 3ce8009a | Iustin Pop | let depth = length ixes |
990 | 3ce8009a | Iustin Pop | newname = printf "new-%d" depth::String |
991 | 3ce8009a | Iustin Pop | newidx = length (Container.elems il) + depth |
992 | 3ce8009a | Iustin Pop | newi2 = Instance.setIdx (Instance.setName newinst newname) newidx |
993 | 41b5c85a | Iustin Pop | in case tryAlloc nl il newi2 allocnodes of |
994 | 3ce8009a | Iustin Pop | Bad s -> Bad s |
995 | 85d0ddc3 | Iustin Pop | Ok (AllocSolution { asFailures = errs, asSolutions = sols3 }) -> |
996 | 3ce8009a | Iustin Pop | case sols3 of |
997 | d5ccec02 | Iustin Pop | [] -> Ok (collapseFailures errs, nl, il, ixes, cstats) |
998 | a334d536 | Iustin Pop | (xnl, xi, _, _):[] -> |
999 | 94d08202 | Iustin Pop | iterateAlloc xnl (Container.add newidx xi il) |
1000 | 41b5c85a | Iustin Pop | newinst allocnodes (xi:ixes) |
1001 | d5ccec02 | Iustin Pop | (totalResources xnl:cstats) |
1002 | 3ce8009a | Iustin Pop | _ -> Bad "Internal error: multiple solutions for single\ |
1003 | 3ce8009a | Iustin Pop | \ allocation" |
1004 | 3ce8009a | Iustin Pop | |
1005 | 525bfb36 | Iustin Pop | -- | The core of the tiered allocation mode. |
1006 | 3ce8009a | Iustin Pop | tieredAlloc :: Node.List |
1007 | 3ce8009a | Iustin Pop | -> Instance.List |
1008 | 3ce8009a | Iustin Pop | -> Instance.Instance |
1009 | 41b5c85a | Iustin Pop | -> AllocNodes |
1010 | 3ce8009a | Iustin Pop | -> [Instance.Instance] |
1011 | d5ccec02 | Iustin Pop | -> [CStats] |
1012 | 40ee14bc | Iustin Pop | -> Result AllocResult |
1013 | 41b5c85a | Iustin Pop | tieredAlloc nl il newinst allocnodes ixes cstats = |
1014 | 41b5c85a | Iustin Pop | case iterateAlloc nl il newinst allocnodes ixes cstats of |
1015 | 3ce8009a | Iustin Pop | Bad s -> Bad s |
1016 | d5ccec02 | Iustin Pop | Ok (errs, nl', il', ixes', cstats') -> |
1017 | 3ce8009a | Iustin Pop | case Instance.shrinkByType newinst . fst . last $ |
1018 | 3ce8009a | Iustin Pop | sortBy (comparing snd) errs of |
1019 | d5ccec02 | Iustin Pop | Bad _ -> Ok (errs, nl', il', ixes', cstats') |
1020 | 3ce8009a | Iustin Pop | Ok newinst' -> |
1021 | 41b5c85a | Iustin Pop | tieredAlloc nl' il' newinst' allocnodes ixes' cstats' |
1022 | 3ce8009a | Iustin Pop | |
1023 | 949397c8 | Iustin Pop | -- | Compute the tiered spec string description from a list of |
1024 | 949397c8 | Iustin Pop | -- allocated instances. |
1025 | 949397c8 | Iustin Pop | tieredSpecMap :: [Instance.Instance] |
1026 | 949397c8 | Iustin Pop | -> [String] |
1027 | 949397c8 | Iustin Pop | tieredSpecMap trl_ixes = |
1028 | 949397c8 | Iustin Pop | let fin_trl_ixes = reverse trl_ixes |
1029 | 949397c8 | Iustin Pop | ix_byspec = groupBy ((==) `on` Instance.specOf) fin_trl_ixes |
1030 | 949397c8 | Iustin Pop | spec_map = map (\ixs -> (Instance.specOf $ head ixs, length ixs)) |
1031 | 949397c8 | Iustin Pop | ix_byspec |
1032 | 949397c8 | Iustin Pop | in map (\(spec, cnt) -> printf "%d,%d,%d=%d" (rspecMem spec) |
1033 | 949397c8 | Iustin Pop | (rspecDsk spec) (rspecCpu spec) cnt) spec_map |
1034 | 949397c8 | Iustin Pop | |
1035 | 9188aeef | Iustin Pop | -- * Formatting functions |
1036 | e4f08c46 | Iustin Pop | |
1037 | e4f08c46 | Iustin Pop | -- | Given the original and final nodes, computes the relocation description. |
1038 | c9926b22 | Iustin Pop | computeMoves :: Instance.Instance -- ^ The instance to be moved |
1039 | c9926b22 | Iustin Pop | -> String -- ^ The instance name |
1040 | 668c03b3 | Iustin Pop | -> IMove -- ^ The move being performed |
1041 | e4f08c46 | Iustin Pop | -> String -- ^ New primary |
1042 | e4f08c46 | Iustin Pop | -> String -- ^ New secondary |
1043 | e4f08c46 | Iustin Pop | -> (String, [String]) |
1044 | e4f08c46 | Iustin Pop | -- ^ Tuple of moves and commands list; moves is containing |
1045 | e4f08c46 | Iustin Pop | -- either @/f/@ for failover or @/r:name/@ for replace |
1046 | e4f08c46 | Iustin Pop | -- secondary, while the command list holds gnt-instance |
1047 | e4f08c46 | Iustin Pop | -- commands (without that prefix), e.g \"@failover instance1@\" |
1048 | 668c03b3 | Iustin Pop | computeMoves i inam mv c d = |
1049 | 668c03b3 | Iustin Pop | case mv of |
1050 | 668c03b3 | Iustin Pop | Failover -> ("f", [mig]) |
1051 | 668c03b3 | Iustin Pop | FailoverAndReplace _ -> (printf "f r:%s" d, [mig, rep d]) |
1052 | 668c03b3 | Iustin Pop | ReplaceSecondary _ -> (printf "r:%s" d, [rep d]) |
1053 | 668c03b3 | Iustin Pop | ReplaceAndFailover _ -> (printf "r:%s f" c, [rep c, mig]) |
1054 | 668c03b3 | Iustin Pop | ReplacePrimary _ -> (printf "f r:%s f" c, [mig, rep c, mig]) |
1055 | c9926b22 | Iustin Pop | where morf = if Instance.running i then "migrate" else "failover" |
1056 | c9926b22 | Iustin Pop | mig = printf "%s -f %s" morf inam::String |
1057 | c9926b22 | Iustin Pop | rep n = printf "replace-disks -n %s %s" n inam |
1058 | e4f08c46 | Iustin Pop | |
1059 | 9188aeef | Iustin Pop | -- | Converts a placement to string format. |
1060 | 9188aeef | Iustin Pop | printSolutionLine :: Node.List -- ^ The node list |
1061 | 9188aeef | Iustin Pop | -> Instance.List -- ^ The instance list |
1062 | 9188aeef | Iustin Pop | -> Int -- ^ Maximum node name length |
1063 | 9188aeef | Iustin Pop | -> Int -- ^ Maximum instance name length |
1064 | 9188aeef | Iustin Pop | -> Placement -- ^ The current placement |
1065 | 9188aeef | Iustin Pop | -> Int -- ^ The index of the placement in |
1066 | 9188aeef | Iustin Pop | -- the solution |
1067 | db1bcfe8 | Iustin Pop | -> (String, [String]) |
1068 | db1bcfe8 | Iustin Pop | printSolutionLine nl il nmlen imlen plc pos = |
1069 | ca8258d9 | Iustin Pop | let |
1070 | ca8258d9 | Iustin Pop | pmlen = (2*nmlen + 1) |
1071 | 668c03b3 | Iustin Pop | (i, p, s, mv, c) = plc |
1072 | ca8258d9 | Iustin Pop | inst = Container.find i il |
1073 | 14c972c7 | Iustin Pop | inam = Instance.alias inst |
1074 | 14c972c7 | Iustin Pop | npri = Node.alias $ Container.find p nl |
1075 | 14c972c7 | Iustin Pop | nsec = Node.alias $ Container.find s nl |
1076 | 14c972c7 | Iustin Pop | opri = Node.alias $ Container.find (Instance.pNode inst) nl |
1077 | 14c972c7 | Iustin Pop | osec = Node.alias $ Container.find (Instance.sNode inst) nl |
1078 | 668c03b3 | Iustin Pop | (moves, cmds) = computeMoves inst inam mv npri nsec |
1079 | 9f6dcdea | Iustin Pop | ostr = printf "%s:%s" opri osec::String |
1080 | 9f6dcdea | Iustin Pop | nstr = printf "%s:%s" npri nsec::String |
1081 | ca8258d9 | Iustin Pop | in |
1082 | ab271fc1 | Iustin Pop | (printf " %3d. %-*s %-*s => %-*s %.8f a=%s" |
1083 | ab271fc1 | Iustin Pop | pos imlen inam pmlen ostr |
1084 | ca8258d9 | Iustin Pop | pmlen nstr c moves, |
1085 | ca8258d9 | Iustin Pop | cmds) |
1086 | ca8258d9 | Iustin Pop | |
1087 | 0e8ae201 | Iustin Pop | -- | Return the instance and involved nodes in an instance move. |
1088 | 0e8ae201 | Iustin Pop | involvedNodes :: Instance.List -> Placement -> [Ndx] |
1089 | 0e8ae201 | Iustin Pop | involvedNodes il plc = |
1090 | 3173c987 | Iustin Pop | let (i, np, ns, _, _) = plc |
1091 | 0e8ae201 | Iustin Pop | inst = Container.find i il |
1092 | 2060348b | Iustin Pop | op = Instance.pNode inst |
1093 | 2060348b | Iustin Pop | os = Instance.sNode inst |
1094 | 0e8ae201 | Iustin Pop | in nub [np, ns, op, os] |
1095 | 0e8ae201 | Iustin Pop | |
1096 | 0e8ae201 | Iustin Pop | -- | Inner function for splitJobs, that either appends the next job to |
1097 | 0e8ae201 | Iustin Pop | -- the current jobset, or starts a new jobset. |
1098 | 0e8ae201 | Iustin Pop | mergeJobs :: ([JobSet], [Ndx]) -> MoveJob -> ([JobSet], [Ndx]) |
1099 | 924f9c16 | Iustin Pop | mergeJobs ([], _) n@(ndx, _, _, _) = ([[n]], ndx) |
1100 | 924f9c16 | Iustin Pop | mergeJobs (cjs@(j:js), nbuf) n@(ndx, _, _, _) |
1101 | 0e8ae201 | Iustin Pop | | null (ndx `intersect` nbuf) = ((n:j):js, ndx ++ nbuf) |
1102 | 0e8ae201 | Iustin Pop | | otherwise = ([n]:cjs, ndx) |
1103 | 0e8ae201 | Iustin Pop | |
1104 | 0e8ae201 | Iustin Pop | -- | Break a list of moves into independent groups. Note that this |
1105 | 0e8ae201 | Iustin Pop | -- will reverse the order of jobs. |
1106 | 0e8ae201 | Iustin Pop | splitJobs :: [MoveJob] -> [JobSet] |
1107 | 0e8ae201 | Iustin Pop | splitJobs = fst . foldl mergeJobs ([], []) |
1108 | 0e8ae201 | Iustin Pop | |
1109 | 0e8ae201 | Iustin Pop | -- | Given a list of commands, prefix them with @gnt-instance@ and |
1110 | 0e8ae201 | Iustin Pop | -- also beautify the display a little. |
1111 | 0e8ae201 | Iustin Pop | formatJob :: Int -> Int -> (Int, MoveJob) -> [String] |
1112 | 924f9c16 | Iustin Pop | formatJob jsn jsl (sn, (_, _, _, cmds)) = |
1113 | 0e8ae201 | Iustin Pop | let out = |
1114 | 0e8ae201 | Iustin Pop | printf " echo job %d/%d" jsn sn: |
1115 | 0e8ae201 | Iustin Pop | printf " check": |
1116 | 0e8ae201 | Iustin Pop | map (" gnt-instance " ++) cmds |
1117 | 0e8ae201 | Iustin Pop | in if sn == 1 |
1118 | 0e8ae201 | Iustin Pop | then ["", printf "echo jobset %d, %d jobs" jsn jsl] ++ out |
1119 | 0e8ae201 | Iustin Pop | else out |
1120 | 0e8ae201 | Iustin Pop | |
1121 | 9188aeef | Iustin Pop | -- | Given a list of commands, prefix them with @gnt-instance@ and |
1122 | 9188aeef | Iustin Pop | -- also beautify the display a little. |
1123 | 0e8ae201 | Iustin Pop | formatCmds :: [JobSet] -> String |
1124 | 9f6dcdea | Iustin Pop | formatCmds = |
1125 | 9f6dcdea | Iustin Pop | unlines . |
1126 | 0e8ae201 | Iustin Pop | concatMap (\(jsn, js) -> concatMap (formatJob jsn (length js)) |
1127 | 0e8ae201 | Iustin Pop | (zip [1..] js)) . |
1128 | 9f6dcdea | Iustin Pop | zip [1..] |
1129 | 142538ff | Iustin Pop | |
1130 | e4f08c46 | Iustin Pop | -- | Print the node list. |
1131 | e98fb766 | Iustin Pop | printNodes :: Node.List -> [String] -> String |
1132 | e98fb766 | Iustin Pop | printNodes nl fs = |
1133 | 6dfa04fd | Iustin Pop | let fields = case fs of |
1134 | 6dfa04fd | Iustin Pop | [] -> Node.defaultFields |
1135 | 6dfa04fd | Iustin Pop | "+":rest -> Node.defaultFields ++ rest |
1136 | 6dfa04fd | Iustin Pop | _ -> fs |
1137 | 5182e970 | Iustin Pop | snl = sortBy (comparing Node.idx) (Container.elems nl) |
1138 | e98fb766 | Iustin Pop | (header, isnum) = unzip $ map Node.showHeader fields |
1139 | c5f7412e | Iustin Pop | in unlines . map ((:) ' ' . intercalate " ") $ |
1140 | e98fb766 | Iustin Pop | formatTable (header:map (Node.list fields) snl) isnum |
1141 | e4f08c46 | Iustin Pop | |
1142 | 507fda3f | Iustin Pop | -- | Print the instance list. |
1143 | 507fda3f | Iustin Pop | printInsts :: Node.List -> Instance.List -> String |
1144 | 507fda3f | Iustin Pop | printInsts nl il = |
1145 | 5182e970 | Iustin Pop | let sil = sortBy (comparing Instance.idx) (Container.elems il) |
1146 | 30ff0c73 | Iustin Pop | helper inst = [ if Instance.running inst then "R" else " " |
1147 | 30ff0c73 | Iustin Pop | , Instance.name inst |
1148 | 30ff0c73 | Iustin Pop | , Container.nameOf nl (Instance.pNode inst) |
1149 | 5182e970 | Iustin Pop | , let sdx = Instance.sNode inst |
1150 | 5182e970 | Iustin Pop | in if sdx == Node.noSecondary |
1151 | 5182e970 | Iustin Pop | then "" |
1152 | 5182e970 | Iustin Pop | else Container.nameOf nl sdx |
1153 | 0e09422b | Iustin Pop | , if Instance.autoBalance inst then "Y" else "N" |
1154 | 30ff0c73 | Iustin Pop | , printf "%3d" $ Instance.vcpus inst |
1155 | 30ff0c73 | Iustin Pop | , printf "%5d" $ Instance.mem inst |
1156 | 30ff0c73 | Iustin Pop | , printf "%5d" $ Instance.dsk inst `div` 1024 |
1157 | 30ff0c73 | Iustin Pop | , printf "%5.3f" lC |
1158 | 30ff0c73 | Iustin Pop | , printf "%5.3f" lM |
1159 | 30ff0c73 | Iustin Pop | , printf "%5.3f" lD |
1160 | 30ff0c73 | Iustin Pop | , printf "%5.3f" lN |
1161 | 30ff0c73 | Iustin Pop | ] |
1162 | 30ff0c73 | Iustin Pop | where DynUtil lC lM lD lN = Instance.util inst |
1163 | 93439b1c | Iustin Pop | header = [ "F", "Name", "Pri_node", "Sec_node", "Auto_bal" |
1164 | 93439b1c | Iustin Pop | , "vcpu", "mem" , "dsk", "lCpu", "lMem", "lDsk", "lNet" ] |
1165 | 93439b1c | Iustin Pop | isnum = False:False:False:False:False:repeat True |
1166 | c5f7412e | Iustin Pop | in unlines . map ((:) ' ' . intercalate " ") $ |
1167 | c5f7412e | Iustin Pop | formatTable (header:map helper sil) isnum |
1168 | 507fda3f | Iustin Pop | |
1169 | 9188aeef | Iustin Pop | -- | Shows statistics for a given node list. |
1170 | 262a08a2 | Iustin Pop | printStats :: Node.List -> String |
1171 | e4f08c46 | Iustin Pop | printStats nl = |
1172 | 9bb5721c | Iustin Pop | let dcvs = compDetailedCV $ Container.elems nl |
1173 | 8a3b30ca | Iustin Pop | (weights, names) = unzip detailedCVInfo |
1174 | 8a3b30ca | Iustin Pop | hd = zip3 (weights ++ repeat 1) (names ++ repeat "unknown") dcvs |
1175 | 8a3b30ca | Iustin Pop | formatted = map (\(w, header, val) -> |
1176 | 8a3b30ca | Iustin Pop | printf "%s=%.8f(x%.2f)" header val w::String) hd |
1177 | fca250e9 | Iustin Pop | in intercalate ", " formatted |
1178 | 6b20875c | Iustin Pop | |
1179 | 6b20875c | Iustin Pop | -- | Convert a placement into a list of OpCodes (basically a job). |
1180 | 3e4480e0 | Iustin Pop | iMoveToJob :: Node.List -> Instance.List |
1181 | 6b20875c | Iustin Pop | -> Idx -> IMove -> [OpCodes.OpCode] |
1182 | 3e4480e0 | Iustin Pop | iMoveToJob nl il idx move = |
1183 | dfbbd43a | Iustin Pop | let inst = Container.find idx il |
1184 | 3e4480e0 | Iustin Pop | iname = Instance.name inst |
1185 | 3e4480e0 | Iustin Pop | lookNode = Just . Container.nameOf nl |
1186 | 10028866 | René Nussbaumer | opF = OpCodes.OpInstanceMigrate iname True False True |
1187 | 10028866 | René Nussbaumer | opR n = OpCodes.OpInstanceReplaceDisks iname (lookNode n) |
1188 | 6b20875c | Iustin Pop | OpCodes.ReplaceNewSecondary [] Nothing |
1189 | 6b20875c | Iustin Pop | in case move of |
1190 | 6b20875c | Iustin Pop | Failover -> [ opF ] |
1191 | 6b20875c | Iustin Pop | ReplacePrimary np -> [ opF, opR np, opF ] |
1192 | 6b20875c | Iustin Pop | ReplaceSecondary ns -> [ opR ns ] |
1193 | 6b20875c | Iustin Pop | ReplaceAndFailover np -> [ opR np, opF ] |
1194 | 6b20875c | Iustin Pop | FailoverAndReplace ns -> [ opF, opR ns ] |
1195 | 32b8d9c0 | Iustin Pop | |
1196 | 949397c8 | Iustin Pop | -- * Node group functions |
1197 | 949397c8 | Iustin Pop | |
1198 | 525bfb36 | Iustin Pop | -- | Computes the group of an instance. |
1199 | 10ef6b4e | Iustin Pop | instanceGroup :: Node.List -> Instance.Instance -> Result Gdx |
1200 | 32b8d9c0 | Iustin Pop | instanceGroup nl i = |
1201 | 32b8d9c0 | Iustin Pop | let sidx = Instance.sNode i |
1202 | 32b8d9c0 | Iustin Pop | pnode = Container.find (Instance.pNode i) nl |
1203 | 32b8d9c0 | Iustin Pop | snode = if sidx == Node.noSecondary |
1204 | 32b8d9c0 | Iustin Pop | then pnode |
1205 | 32b8d9c0 | Iustin Pop | else Container.find sidx nl |
1206 | 10ef6b4e | Iustin Pop | pgroup = Node.group pnode |
1207 | 10ef6b4e | Iustin Pop | sgroup = Node.group snode |
1208 | 10ef6b4e | Iustin Pop | in if pgroup /= sgroup |
1209 | 10ef6b4e | Iustin Pop | then fail ("Instance placed accross two node groups, primary " ++ |
1210 | 10ef6b4e | Iustin Pop | show pgroup ++ ", secondary " ++ show sgroup) |
1211 | 10ef6b4e | Iustin Pop | else return pgroup |
1212 | 32b8d9c0 | Iustin Pop | |
1213 | 525bfb36 | Iustin Pop | -- | Computes the group of an instance per the primary node. |
1214 | 4bc33d60 | Iustin Pop | instancePriGroup :: Node.List -> Instance.Instance -> Gdx |
1215 | 4bc33d60 | Iustin Pop | instancePriGroup nl i = |
1216 | 4bc33d60 | Iustin Pop | let pnode = Container.find (Instance.pNode i) nl |
1217 | 4bc33d60 | Iustin Pop | in Node.group pnode |
1218 | 4bc33d60 | Iustin Pop | |
1219 | 32b8d9c0 | Iustin Pop | -- | Compute the list of badly allocated instances (split across node |
1220 | 525bfb36 | Iustin Pop | -- groups). |
1221 | 32b8d9c0 | Iustin Pop | findSplitInstances :: Node.List -> Instance.List -> [Instance.Instance] |
1222 | 2a8e2dc9 | Iustin Pop | findSplitInstances nl = |
1223 | 2a8e2dc9 | Iustin Pop | filter (not . isOk . instanceGroup nl) . Container.elems |
1224 | f4161783 | Iustin Pop | |
1225 | 525bfb36 | Iustin Pop | -- | Splits a cluster into the component node groups. |
1226 | f4161783 | Iustin Pop | splitCluster :: Node.List -> Instance.List -> |
1227 | 10ef6b4e | Iustin Pop | [(Gdx, (Node.List, Instance.List))] |
1228 | f4161783 | Iustin Pop | splitCluster nl il = |
1229 | f4161783 | Iustin Pop | let ngroups = Node.computeGroups (Container.elems nl) |
1230 | f4161783 | Iustin Pop | in map (\(guuid, nodes) -> |
1231 | f4161783 | Iustin Pop | let nidxs = map Node.idx nodes |
1232 | f4161783 | Iustin Pop | nodes' = zip nidxs nodes |
1233 | f4161783 | Iustin Pop | instances = Container.filter ((`elem` nidxs) . Instance.pNode) il |
1234 | cb0c77ff | Iustin Pop | in (guuid, (Container.fromList nodes', instances))) ngroups |
1235 | 1f4ae205 | Iustin Pop | |
1236 | 1f4ae205 | Iustin Pop | -- | Split a global instance index map into per-group, and associate |
1237 | 1f4ae205 | Iustin Pop | -- it with the group/node/instance lists. |
1238 | 1f4ae205 | Iustin Pop | associateIdxs :: [Idx] -- ^ Instance indices to be split/associated |
1239 | 1f4ae205 | Iustin Pop | -> [(Gdx, (Node.List, Instance.List))] -- ^ Input groups |
1240 | 1f4ae205 | Iustin Pop | -> [(Gdx, (Node.List, Instance.List, [Idx]))] -- ^ Result |
1241 | 1f4ae205 | Iustin Pop | associateIdxs idxs = |
1242 | 1f4ae205 | Iustin Pop | map (\(gdx, (nl, il)) -> |
1243 | 1f4ae205 | Iustin Pop | (gdx, (nl, il, filter (`Container.member` il) idxs))) |
1244 | 63a78055 | Iustin Pop | |
1245 | 63a78055 | Iustin Pop | -- | Compute the list of nodes that are to be evacuated, given a list |
1246 | 63a78055 | Iustin Pop | -- of instances and an evacuation mode. |
1247 | 63a78055 | Iustin Pop | nodesToEvacuate :: Instance.List -- ^ The cluster-wide instance list |
1248 | 63a78055 | Iustin Pop | -> EvacMode -- ^ The evacuation mode we're using |
1249 | 63a78055 | Iustin Pop | -> [Idx] -- ^ List of instance indices being evacuated |
1250 | 63a78055 | Iustin Pop | -> IntSet.IntSet -- ^ Set of node indices |
1251 | 63a78055 | Iustin Pop | nodesToEvacuate il mode = |
1252 | 63a78055 | Iustin Pop | IntSet.delete Node.noSecondary . |
1253 | 63a78055 | Iustin Pop | foldl' (\ns idx -> |
1254 | 63a78055 | Iustin Pop | let i = Container.find idx il |
1255 | 63a78055 | Iustin Pop | pdx = Instance.pNode i |
1256 | 63a78055 | Iustin Pop | sdx = Instance.sNode i |
1257 | 63a78055 | Iustin Pop | dt = Instance.diskTemplate i |
1258 | 63a78055 | Iustin Pop | withSecondary = case dt of |
1259 | 63a78055 | Iustin Pop | DTDrbd8 -> IntSet.insert sdx ns |
1260 | 63a78055 | Iustin Pop | _ -> ns |
1261 | 63a78055 | Iustin Pop | in case mode of |
1262 | 63a78055 | Iustin Pop | ChangePrimary -> IntSet.insert pdx ns |
1263 | 63a78055 | Iustin Pop | ChangeSecondary -> withSecondary |
1264 | 63a78055 | Iustin Pop | ChangeAll -> IntSet.insert pdx withSecondary |
1265 | 63a78055 | Iustin Pop | ) IntSet.empty |