## root / Ganeti / HTools / Cluster.hs @ 78694255

History | View | Annotate | Download (29.7 kB)

1 |
{-| Implementation of cluster-wide logic. |
---|---|

2 | |

3 |
This module holds all pure cluster-logic; I\/O related functionality |

4 |
goes into the "Main" module for the individual binaries. |

5 | |

6 |
-} |

7 | |

8 |
{- |

9 | |

10 |
Copyright (C) 2009 Google Inc. |

11 | |

12 |
This program is free software; you can redistribute it and/or modify |

13 |
it under the terms of the GNU General Public License as published by |

14 |
the Free Software Foundation; either version 2 of the License, or |

15 |
(at your option) any later version. |

16 | |

17 |
This program is distributed in the hope that it will be useful, but |

18 |
WITHOUT ANY WARRANTY; without even the implied warranty of |

19 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |

20 |
General Public License for more details. |

21 | |

22 |
You should have received a copy of the GNU General Public License |

23 |
along with this program; if not, write to the Free Software |

24 |
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |

25 |
02110-1301, USA. |

26 | |

27 |
-} |

28 | |

29 |
module Ganeti.HTools.Cluster |

30 |
( |

31 |
-- * Types |

32 |
Placement |

33 |
, AllocSolution |

34 |
, Solution(..) |

35 |
, Table(..) |

36 |
, Removal |

37 |
, Score |

38 |
, IMove(..) |

39 |
-- * Generic functions |

40 |
, totalResources |

41 |
-- * First phase functions |

42 |
, computeBadItems |

43 |
-- * Second phase functions |

44 |
, computeSolution |

45 |
, applySolution |

46 |
, printSolution |

47 |
, printSolutionLine |

48 |
, formatCmds |

49 |
, printNodes |

50 |
-- * Balacing functions |

51 |
, applyMove |

52 |
, checkMove |

53 |
, compCV |

54 |
, printStats |

55 |
-- * IAllocator functions |

56 |
, allocateOnSingle |

57 |
, allocateOnPair |

58 |
, tryAlloc |

59 |
, tryReloc |

60 |
) where |

61 | |

62 |
import Data.List |

63 |
import Data.Maybe (isNothing, fromJust) |

64 |
import Text.Printf (printf) |

65 |
import Data.Function |

66 |
import Control.Monad |

67 | |

68 |
import qualified Ganeti.HTools.Container as Container |

69 |
import qualified Ganeti.HTools.Instance as Instance |

70 |
import qualified Ganeti.HTools.Node as Node |

71 |
import Ganeti.HTools.Types |

72 |
import Ganeti.HTools.Utils |

73 | |

74 |
-- * Types |

75 | |

76 |
-- | A separate name for the cluster score type. |

77 |
type Score = Double |

78 | |

79 |
-- | The description of an instance placement. |

80 |
type Placement = (Idx, Ndx, Ndx, Score) |

81 | |

82 |
-- | Allocation/relocation solution. |

83 |
type AllocSolution = [(Maybe Node.List, Instance.Instance, [Node.Node])] |

84 | |

85 |
-- | A cluster solution described as the solution delta and the list |

86 |
-- of placements. |

87 |
data Solution = Solution Int [Placement] |

88 |
deriving (Eq, Ord, Show) |

89 | |

90 |
-- | A removal set. |

91 |
data Removal = Removal Node.List [Instance.Instance] |

92 | |

93 |
-- | An instance move definition |

94 |
data IMove = Failover -- ^ Failover the instance (f) |

95 |
| ReplacePrimary Ndx -- ^ Replace primary (f, r:np, f) |

96 |
| ReplaceSecondary Ndx -- ^ Replace secondary (r:ns) |

97 |
| ReplaceAndFailover Ndx -- ^ Replace secondary, failover (r:np, f) |

98 |
| FailoverAndReplace Ndx -- ^ Failover, replace secondary (f, r:ns) |

99 |
deriving (Show) |

100 | |

101 |
-- | The complete state for the balancing solution |

102 |
data Table = Table Node.List Instance.List Score [Placement] |

103 |
deriving (Show) |

104 | |

105 |
-- * Utility functions |

106 | |

107 |
-- | Returns the delta of a solution or -1 for Nothing. |

108 |
solutionDelta :: Maybe Solution -> Int |

109 |
solutionDelta sol = case sol of |

110 |
Just (Solution d _) -> d |

111 |
_ -> -1 |

112 | |

113 |
-- | Cap the removal list if needed. |

114 |
capRemovals :: [a] -> Int -> [a] |

115 |
capRemovals removals max_removals = |

116 |
if max_removals > 0 then |

117 |
take max_removals removals |

118 |
else |

119 |
removals |

120 | |

121 |
-- | Check if the given node list fails the N+1 check. |

122 |
verifyN1Check :: [Node.Node] -> Bool |

123 |
verifyN1Check nl = any Node.failN1 nl |

124 | |

125 |
-- | Verifies the N+1 status and return the affected nodes. |

126 |
verifyN1 :: [Node.Node] -> [Node.Node] |

127 |
verifyN1 nl = filter Node.failN1 nl |

128 | |

129 |
{-| Computes the pair of bad nodes and instances. |

130 | |

131 |
The bad node list is computed via a simple 'verifyN1' check, and the |

132 |
bad instance list is the list of primary and secondary instances of |

133 |
those nodes. |

134 | |

135 |
-} |

136 |
computeBadItems :: Node.List -> Instance.List -> |

137 |
([Node.Node], [Instance.Instance]) |

138 |
computeBadItems nl il = |

139 |
let bad_nodes = verifyN1 $ getOnline nl |

140 |
bad_instances = map (\idx -> Container.find idx il) $ |

141 |
sort $ nub $ concat $ |

142 |
map (\ n -> (Node.slist n) ++ (Node.plist n)) bad_nodes |

143 |
in |

144 |
(bad_nodes, bad_instances) |

145 | |

146 |
-- | Compute the total free disk and memory in the cluster. |

147 |
totalResources :: Node.List -> (Int, Int) |

148 |
totalResources nl = |

149 |
foldl' |

150 |
(\ (mem, dsk) node -> (mem + (Node.f_mem node), |

151 |
dsk + (Node.f_dsk node))) |

152 |
(0, 0) (Container.elems nl) |

153 | |

154 |
-- | Compute the mem and disk covariance. |

155 |
compDetailedCV :: Node.List -> (Double, Double, Double, Double, Double, Double) |

156 |
compDetailedCV nl = |

157 |
let |

158 |
all_nodes = Container.elems nl |

159 |
(offline, nodes) = partition Node.offline all_nodes |

160 |
mem_l = map Node.p_mem nodes |

161 |
dsk_l = map Node.p_dsk nodes |

162 |
mem_cv = varianceCoeff mem_l |

163 |
dsk_cv = varianceCoeff dsk_l |

164 |
n1_l = length $ filter Node.failN1 nodes |

165 |
n1_score = ((fromIntegral n1_l) / |

166 |
(fromIntegral $ length nodes))::Double |

167 |
res_l = map Node.p_rem nodes |

168 |
res_cv = varianceCoeff res_l |

169 |
offline_inst = sum . map (\n -> (length . Node.plist $ n) + |

170 |
(length . Node.slist $ n)) $ offline |

171 |
online_inst = sum . map (\n -> (length . Node.plist $ n) + |

172 |
(length . Node.slist $ n)) $ nodes |

173 |
off_score = ((fromIntegral offline_inst) / |

174 |
(fromIntegral $ online_inst + offline_inst))::Double |

175 |
cpu_l = map Node.p_cpu nodes |

176 |
cpu_cv = varianceCoeff cpu_l |

177 |
in (mem_cv, dsk_cv, n1_score, res_cv, off_score, cpu_cv) |

178 | |

179 |
-- | Compute the /total/ variance. |

180 |
compCV :: Node.List -> Double |

181 |
compCV nl = |

182 |
let (mem_cv, dsk_cv, n1_score, res_cv, off_score, cpu_cv) = |

183 |
compDetailedCV nl |

184 |
in mem_cv + dsk_cv + n1_score + res_cv + off_score + cpu_cv |

185 | |

186 |
-- | Compute online nodes from a Node.List |

187 |
getOnline :: Node.List -> [Node.Node] |

188 |
getOnline = filter (not . Node.offline) . Container.elems |

189 | |

190 |
-- * hn1 functions |

191 | |

192 |
-- | Add an instance and return the new node and instance maps. |

193 |
addInstance :: Node.List -> Instance.Instance -> |

194 |
Node.Node -> Node.Node -> Maybe Node.List |

195 |
addInstance nl idata pri sec = |

196 |
let pdx = Node.idx pri |

197 |
sdx = Node.idx sec |

198 |
in do |

199 |
pnode <- Node.addPri pri idata |

200 |
snode <- Node.addSec sec idata pdx |

201 |
new_nl <- return $ Container.addTwo sdx snode |

202 |
pdx pnode nl |

203 |
return new_nl |

204 | |

205 |
-- | Remove an instance and return the new node and instance maps. |

206 |
removeInstance :: Node.List -> Instance.Instance -> Node.List |

207 |
removeInstance nl idata = |

208 |
let pnode = Instance.pnode idata |

209 |
snode = Instance.snode idata |

210 |
pn = Container.find pnode nl |

211 |
sn = Container.find snode nl |

212 |
new_nl = Container.addTwo |

213 |
pnode (Node.removePri pn idata) |

214 |
snode (Node.removeSec sn idata) nl in |

215 |
new_nl |

216 | |

217 |
-- | Remove an instance and return the new node map. |

218 |
removeInstances :: Node.List -> [Instance.Instance] -> Node.List |

219 |
removeInstances = foldl' removeInstance |

220 | |

221 | |

222 |
{-| Compute a new version of a cluster given a solution. |

223 | |

224 |
This is not used for computing the solutions, but for applying a |

225 |
(known-good) solution to the original cluster for final display. |

226 | |

227 |
It first removes the relocated instances after which it places them on |

228 |
their new nodes. |

229 | |

230 |
-} |

231 |
applySolution :: Node.List -> Instance.List -> [Placement] -> Node.List |

232 |
applySolution nl il sol = |

233 |
let odxes = map (\ (a, b, c, _) -> (Container.find a il, |

234 |
Node.idx (Container.find b nl), |

235 |
Node.idx (Container.find c nl)) |

236 |
) sol |

237 |
idxes = (\ (x, _, _) -> x) (unzip3 odxes) |

238 |
nc = removeInstances nl idxes |

239 |
in |

240 |
foldl' (\ nz (a, b, c) -> |

241 |
let new_p = Container.find b nz |

242 |
new_s = Container.find c nz in |

243 |
fromJust (addInstance nz a new_p new_s) |

244 |
) nc odxes |

245 | |

246 | |

247 |
-- ** First phase functions |

248 | |

249 |
{-| Given a list 1,2,3..n build a list of pairs [(1, [2..n]), (2, |

250 |
[3..n]), ...] |

251 | |

252 |
-} |

253 |
genParts :: [a] -> Int -> [(a, [a])] |

254 |
genParts l count = |

255 |
case l of |

256 |
[] -> [] |

257 |
x:xs -> |

258 |
if length l < count then |

259 |
[] |

260 |
else |

261 |
(x, xs) : (genParts xs count) |

262 | |

263 |
-- | Generates combinations of count items from the names list. |

264 |
genNames :: Int -> [b] -> [[b]] |

265 |
genNames count1 names1 = |

266 |
let aux_fn count names current = |

267 |
case count of |

268 |
0 -> [current] |

269 |
_ -> |

270 |
concatMap |

271 |
(\ (x, xs) -> aux_fn (count - 1) xs (x:current)) |

272 |
(genParts names count) |

273 |
in |

274 |
aux_fn count1 names1 [] |

275 | |

276 |
{-| Checks if removal of instances results in N+1 pass. |

277 | |

278 |
Note: the check removal cannot optimize by scanning only the affected |

279 |
nodes, since the cluster is known to be not healthy; only the check |

280 |
placement can make this shortcut. |

281 | |

282 |
-} |

283 |
checkRemoval :: Node.List -> [Instance.Instance] -> Maybe Removal |

284 |
checkRemoval nl victims = |

285 |
let nx = removeInstances nl victims |

286 |
failN1 = verifyN1Check (Container.elems nx) |

287 |
in |

288 |
if failN1 then |

289 |
Nothing |

290 |
else |

291 |
Just $ Removal nx victims |

292 | |

293 | |

294 |
-- | Computes the removals list for a given depth. |

295 |
computeRemovals :: Node.List |

296 |
-> [Instance.Instance] |

297 |
-> Int |

298 |
-> [Maybe Removal] |

299 |
computeRemovals nl bad_instances depth = |

300 |
map (checkRemoval nl) $ genNames depth bad_instances |

301 | |

302 |
-- ** Second phase functions |

303 | |

304 |
-- | Single-node relocation cost. |

305 |
nodeDelta :: Ndx -> Ndx -> Ndx -> Int |

306 |
nodeDelta i p s = |

307 |
if i == p || i == s then |

308 |
0 |

309 |
else |

310 |
1 |

311 | |

312 |
-- | Compute best solution. |

313 |
-- |

314 |
-- This function compares two solutions, choosing the minimum valid |

315 |
-- solution. |

316 |
compareSolutions :: Maybe Solution -> Maybe Solution -> Maybe Solution |

317 |
compareSolutions a b = case (a, b) of |

318 |
(Nothing, x) -> x |

319 |
(x, Nothing) -> x |

320 |
(x, y) -> min x y |

321 | |

322 |
-- | Check if a given delta is worse then an existing solution. |

323 |
tooHighDelta :: Maybe Solution -> Int -> Int -> Bool |

324 |
tooHighDelta sol new_delta max_delta = |

325 |
if new_delta > max_delta && max_delta >=0 then |

326 |
True |

327 |
else |

328 |
case sol of |

329 |
Nothing -> False |

330 |
Just (Solution old_delta _) -> old_delta <= new_delta |

331 | |

332 |
{-| Check if placement of instances still keeps the cluster N+1 compliant. |

333 | |

334 |
This is the workhorse of the allocation algorithm: given the |

335 |
current node and instance maps, the list of instances to be |

336 |
placed, and the current solution, this will return all possible |

337 |
solution by recursing until all target instances are placed. |

338 | |

339 |
-} |

340 |
checkPlacement :: Node.List -- ^ The current node list |

341 |
-> [Instance.Instance] -- ^ List of instances still to place |

342 |
-> [Placement] -- ^ Partial solution until now |

343 |
-> Int -- ^ The delta of the partial solution |

344 |
-> Maybe Solution -- ^ The previous solution |

345 |
-> Int -- ^ Abort if the we go above this delta |

346 |
-> Maybe Solution -- ^ The new solution |

347 |
checkPlacement nl victims current current_delta prev_sol max_delta = |

348 |
let target = head victims |

349 |
opdx = Instance.pnode target |

350 |
osdx = Instance.snode target |

351 |
vtail = tail victims |

352 |
have_tail = (length vtail) > 0 |

353 |
nodes = Container.elems nl |

354 |
iidx = Instance.idx target |

355 |
in |

356 |
foldl' |

357 |
(\ accu_p pri -> |

358 |
let |

359 |
pri_idx = Node.idx pri |

360 |
upri_delta = current_delta + nodeDelta pri_idx opdx osdx |

361 |
new_pri = Node.addPri pri target |

362 |
fail_delta1 = tooHighDelta accu_p upri_delta max_delta |

363 |
in |

364 |
if fail_delta1 || isNothing(new_pri) then accu_p |

365 |
else let pri_nl = Container.add pri_idx (fromJust new_pri) nl in |

366 |
foldl' |

367 |
(\ accu sec -> |

368 |
let |

369 |
sec_idx = Node.idx sec |

370 |
upd_delta = upri_delta + |

371 |
nodeDelta sec_idx opdx osdx |

372 |
fail_delta2 = tooHighDelta accu upd_delta max_delta |

373 |
new_sec = Node.addSec sec target pri_idx |

374 |
in |

375 |
if sec_idx == pri_idx || fail_delta2 || |

376 |
isNothing new_sec then accu |

377 |
else let |

378 |
nx = Container.add sec_idx (fromJust new_sec) pri_nl |

379 |
upd_cv = compCV nx |

380 |
plc = (iidx, pri_idx, sec_idx, upd_cv) |

381 |
c2 = plc:current |

382 |
result = |

383 |
if have_tail then |

384 |
checkPlacement nx vtail c2 upd_delta |

385 |
accu max_delta |

386 |
else |

387 |
Just (Solution upd_delta c2) |

388 |
in compareSolutions accu result |

389 |
) accu_p nodes |

390 |
) prev_sol nodes |

391 | |

392 |
{-| Auxiliary function for solution computation. |

393 | |

394 |
We write this in an explicit recursive fashion in order to control |

395 |
early-abort in case we have met the min delta. We can't use foldr |

396 |
instead of explicit recursion since we need the accumulator for the |

397 |
abort decision. |

398 | |

399 |
-} |

400 |
advanceSolution :: [Maybe Removal] -- ^ The removal to process |

401 |
-> Int -- ^ Minimum delta parameter |

402 |
-> Int -- ^ Maximum delta parameter |

403 |
-> Maybe Solution -- ^ Current best solution |

404 |
-> Maybe Solution -- ^ New best solution |

405 |
advanceSolution [] _ _ sol = sol |

406 |
advanceSolution (Nothing:xs) m n sol = advanceSolution xs m n sol |

407 |
advanceSolution ((Just (Removal nx removed)):xs) min_d max_d prev_sol = |

408 |
let new_sol = checkPlacement nx removed [] 0 prev_sol max_d |

409 |
new_delta = solutionDelta $! new_sol |

410 |
in |

411 |
if new_delta >= 0 && new_delta <= min_d then |

412 |
new_sol |

413 |
else |

414 |
advanceSolution xs min_d max_d new_sol |

415 | |

416 |
-- | Computes the placement solution. |

417 |
solutionFromRemovals :: [Maybe Removal] -- ^ The list of (possible) removals |

418 |
-> Int -- ^ Minimum delta parameter |

419 |
-> Int -- ^ Maximum delta parameter |

420 |
-> Maybe Solution -- ^ The best solution found |

421 |
solutionFromRemovals removals min_delta max_delta = |

422 |
advanceSolution removals min_delta max_delta Nothing |

423 | |

424 |
{-| Computes the solution at the given depth. |

425 | |

426 |
This is a wrapper over both computeRemovals and |

427 |
solutionFromRemovals. In case we have no solution, we return Nothing. |

428 | |

429 |
-} |

430 |
computeSolution :: Node.List -- ^ The original node data |

431 |
-> [Instance.Instance] -- ^ The list of /bad/ instances |

432 |
-> Int -- ^ The /depth/ of removals |

433 |
-> Int -- ^ Maximum number of removals to process |

434 |
-> Int -- ^ Minimum delta parameter |

435 |
-> Int -- ^ Maximum delta parameter |

436 |
-> Maybe Solution -- ^ The best solution found (or Nothing) |

437 |
computeSolution nl bad_instances depth max_removals min_delta max_delta = |

438 |
let |

439 |
removals = computeRemovals nl bad_instances depth |

440 |
removals' = capRemovals removals max_removals |

441 |
in |

442 |
solutionFromRemovals removals' min_delta max_delta |

443 | |

444 |
-- * hbal functions |

445 | |

446 |
-- | Compute best table. Note that the ordering of the arguments is important. |

447 |
compareTables :: Table -> Table -> Table |

448 |
compareTables a@(Table _ _ a_cv _) b@(Table _ _ b_cv _ ) = |

449 |
if a_cv > b_cv then b else a |

450 | |

451 |
-- | Applies an instance move to a given node list and instance. |

452 |
applyMove :: Node.List -> Instance.Instance |

453 |
-> IMove -> (Maybe Node.List, Instance.Instance, Ndx, Ndx) |

454 |
-- Failover (f) |

455 |
applyMove nl inst Failover = |

456 |
let old_pdx = Instance.pnode inst |

457 |
old_sdx = Instance.snode inst |

458 |
old_p = Container.find old_pdx nl |

459 |
old_s = Container.find old_sdx nl |

460 |
int_p = Node.removePri old_p inst |

461 |
int_s = Node.removeSec old_s inst |

462 |
new_nl = do -- Maybe monad |

463 |
new_p <- Node.addPri int_s inst |

464 |
new_s <- Node.addSec int_p inst old_sdx |

465 |
return $ Container.addTwo old_pdx new_s old_sdx new_p nl |

466 |
in (new_nl, Instance.setBoth inst old_sdx old_pdx, old_sdx, old_pdx) |

467 | |

468 |
-- Replace the primary (f:, r:np, f) |

469 |
applyMove nl inst (ReplacePrimary new_pdx) = |

470 |
let old_pdx = Instance.pnode inst |

471 |
old_sdx = Instance.snode inst |

472 |
old_p = Container.find old_pdx nl |

473 |
old_s = Container.find old_sdx nl |

474 |
tgt_n = Container.find new_pdx nl |

475 |
int_p = Node.removePri old_p inst |

476 |
int_s = Node.removeSec old_s inst |

477 |
new_nl = do -- Maybe monad |

478 |
-- check that the current secondary can host the instance |

479 |
-- during the migration |

480 |
tmp_s <- Node.addPri int_s inst |

481 |
let tmp_s' = Node.removePri tmp_s inst |

482 |
new_p <- Node.addPri tgt_n inst |

483 |
new_s <- Node.addSec tmp_s' inst new_pdx |

484 |
return $ Container.add new_pdx new_p $ |

485 |
Container.addTwo old_pdx int_p old_sdx new_s nl |

486 |
in (new_nl, Instance.setPri inst new_pdx, new_pdx, old_sdx) |

487 | |

488 |
-- Replace the secondary (r:ns) |

489 |
applyMove nl inst (ReplaceSecondary new_sdx) = |

490 |
let old_pdx = Instance.pnode inst |

491 |
old_sdx = Instance.snode inst |

492 |
old_s = Container.find old_sdx nl |

493 |
tgt_n = Container.find new_sdx nl |

494 |
int_s = Node.removeSec old_s inst |

495 |
new_nl = Node.addSec tgt_n inst old_pdx >>= |

496 |
\new_s -> return $ Container.addTwo new_sdx |

497 |
new_s old_sdx int_s nl |

498 |
in (new_nl, Instance.setSec inst new_sdx, old_pdx, new_sdx) |

499 | |

500 |
-- Replace the secondary and failover (r:np, f) |

501 |
applyMove nl inst (ReplaceAndFailover new_pdx) = |

502 |
let old_pdx = Instance.pnode inst |

503 |
old_sdx = Instance.snode inst |

504 |
old_p = Container.find old_pdx nl |

505 |
old_s = Container.find old_sdx nl |

506 |
tgt_n = Container.find new_pdx nl |

507 |
int_p = Node.removePri old_p inst |

508 |
int_s = Node.removeSec old_s inst |

509 |
new_nl = do -- Maybe monad |

510 |
new_p <- Node.addPri tgt_n inst |

511 |
new_s <- Node.addSec int_p inst new_pdx |

512 |
return $ Container.add new_pdx new_p $ |

513 |
Container.addTwo old_pdx new_s old_sdx int_s nl |

514 |
in (new_nl, Instance.setBoth inst new_pdx old_pdx, new_pdx, old_pdx) |

515 | |

516 |
-- Failver and replace the secondary (f, r:ns) |

517 |
applyMove nl inst (FailoverAndReplace new_sdx) = |

518 |
let old_pdx = Instance.pnode inst |

519 |
old_sdx = Instance.snode inst |

520 |
old_p = Container.find old_pdx nl |

521 |
old_s = Container.find old_sdx nl |

522 |
tgt_n = Container.find new_sdx nl |

523 |
int_p = Node.removePri old_p inst |

524 |
int_s = Node.removeSec old_s inst |

525 |
new_nl = do -- Maybe monad |

526 |
new_p <- Node.addPri int_s inst |

527 |
new_s <- Node.addSec tgt_n inst old_sdx |

528 |
return $ Container.add new_sdx new_s $ |

529 |
Container.addTwo old_sdx new_p old_pdx int_p nl |

530 |
in (new_nl, Instance.setBoth inst old_sdx new_sdx, old_sdx, new_sdx) |

531 | |

532 |
-- | Tries to allocate an instance on one given node. |

533 |
allocateOnSingle :: Node.List -> Instance.Instance -> Node.Node |

534 |
-> (Maybe Node.List, Instance.Instance) |

535 |
allocateOnSingle nl inst p = |

536 |
let new_pdx = Node.idx p |

537 |
new_nl = Node.addPri p inst >>= \new_p -> |

538 |
return $ Container.add new_pdx new_p nl |

539 |
in (new_nl, Instance.setBoth inst new_pdx Node.noSecondary) |

540 | |

541 |
-- | Tries to allocate an instance on a given pair of nodes. |

542 |
allocateOnPair :: Node.List -> Instance.Instance -> Node.Node -> Node.Node |

543 |
-> (Maybe Node.List, Instance.Instance) |

544 |
allocateOnPair nl inst tgt_p tgt_s = |

545 |
let new_pdx = Node.idx tgt_p |

546 |
new_sdx = Node.idx tgt_s |

547 |
new_nl = do -- Maybe monad |

548 |
new_p <- Node.addPri tgt_p inst |

549 |
new_s <- Node.addSec tgt_s inst new_pdx |

550 |
return $ Container.addTwo new_pdx new_p new_sdx new_s nl |

551 |
in (new_nl, Instance.setBoth inst new_pdx new_sdx) |

552 | |

553 |
-- | Tries to perform an instance move and returns the best table |

554 |
-- between the original one and the new one. |

555 |
checkSingleStep :: Table -- ^ The original table |

556 |
-> Instance.Instance -- ^ The instance to move |

557 |
-> Table -- ^ The current best table |

558 |
-> IMove -- ^ The move to apply |

559 |
-> Table -- ^ The final best table |

560 |
checkSingleStep ini_tbl target cur_tbl move = |

561 |
let |

562 |
Table ini_nl ini_il _ ini_plc = ini_tbl |

563 |
(tmp_nl, new_inst, pri_idx, sec_idx) = applyMove ini_nl target move |

564 |
in |

565 |
if isNothing tmp_nl then cur_tbl |

566 |
else |

567 |
let tgt_idx = Instance.idx target |

568 |
upd_nl = fromJust tmp_nl |

569 |
upd_cvar = compCV upd_nl |

570 |
upd_il = Container.add tgt_idx new_inst ini_il |

571 |
upd_plc = (tgt_idx, pri_idx, sec_idx, upd_cvar):ini_plc |

572 |
upd_tbl = Table upd_nl upd_il upd_cvar upd_plc |

573 |
in |

574 |
compareTables cur_tbl upd_tbl |

575 | |

576 |
-- | Given the status of the current secondary as a valid new node |

577 |
-- and the current candidate target node, |

578 |
-- generate the possible moves for a instance. |

579 |
possibleMoves :: Bool -> Ndx -> [IMove] |

580 |
possibleMoves True tdx = |

581 |
[ReplaceSecondary tdx, |

582 |
ReplaceAndFailover tdx, |

583 |
ReplacePrimary tdx, |

584 |
FailoverAndReplace tdx] |

585 | |

586 |
possibleMoves False tdx = |

587 |
[ReplaceSecondary tdx, |

588 |
ReplaceAndFailover tdx] |

589 | |

590 |
-- | Compute the best move for a given instance. |

591 |
checkInstanceMove :: [Ndx] -- Allowed target node indices |

592 |
-> Table -- Original table |

593 |
-> Instance.Instance -- Instance to move |

594 |
-> Table -- Best new table for this instance |

595 |
checkInstanceMove nodes_idx ini_tbl target = |

596 |
let |

597 |
opdx = Instance.pnode target |

598 |
osdx = Instance.snode target |

599 |
nodes = filter (\idx -> idx /= opdx && idx /= osdx) nodes_idx |

600 |
use_secondary = elem osdx nodes_idx |

601 |
aft_failover = if use_secondary -- if allowed to failover |

602 |
then checkSingleStep ini_tbl target ini_tbl Failover |

603 |
else ini_tbl |

604 |
all_moves = concatMap (possibleMoves use_secondary) nodes |

605 |
in |

606 |
-- iterate over the possible nodes for this instance |

607 |
foldl' (checkSingleStep ini_tbl target) aft_failover all_moves |

608 | |

609 |
-- | Compute the best next move. |

610 |
checkMove :: [Ndx] -- ^ Allowed target node indices |

611 |
-> Table -- ^ The current solution |

612 |
-> [Instance.Instance] -- ^ List of instances still to move |

613 |
-> Table -- ^ The new solution |

614 |
checkMove nodes_idx ini_tbl victims = |

615 |
let Table _ _ _ ini_plc = ini_tbl |

616 |
-- iterate over all instances, computing the best move |

617 |
best_tbl = |

618 |
foldl' |

619 |
(\ step_tbl elem -> |

620 |
if Instance.snode elem == Node.noSecondary then step_tbl |

621 |
else compareTables step_tbl $ |

622 |
checkInstanceMove nodes_idx ini_tbl elem) |

623 |
ini_tbl victims |

624 |
Table _ _ _ best_plc = best_tbl |

625 |
in |

626 |
if length best_plc == length ini_plc then -- no advancement |

627 |
ini_tbl |

628 |
else |

629 |
best_tbl |

630 | |

631 |
-- * Alocation functions |

632 | |

633 |
-- | Try to allocate an instance on the cluster. |

634 |
tryAlloc :: (Monad m) => |

635 |
Node.List -- ^ The node list |

636 |
-> Instance.List -- ^ The instance list |

637 |
-> Instance.Instance -- ^ The instance to allocate |

638 |
-> Int -- ^ Required number of nodes |

639 |
-> m AllocSolution -- ^ Possible solution list |

640 |
tryAlloc nl _ inst 2 = |

641 |
let all_nodes = getOnline nl |

642 |
all_pairs = liftM2 (,) all_nodes all_nodes |

643 |
ok_pairs = filter (\(x, y) -> Node.idx x /= Node.idx y) all_pairs |

644 |
sols = map (\(p, s) -> let (mnl, i) = allocateOnPair nl inst p s |

645 |
in (mnl, i, [p, s])) |

646 |
ok_pairs |

647 |
in return sols |

648 | |

649 |
tryAlloc nl _ inst 1 = |

650 |
let all_nodes = getOnline nl |

651 |
sols = map (\p -> let (mnl, i) = allocateOnSingle nl inst p |

652 |
in (mnl, i, [p])) |

653 |
all_nodes |

654 |
in return sols |

655 | |

656 |
tryAlloc _ _ _ reqn = fail $ "Unsupported number of alllocation \ |

657 |
\destinations required (" ++ (show reqn) ++ |

658 |
"), only two supported" |

659 | |

660 |
-- | Try to allocate an instance on the cluster. |

661 |
tryReloc :: (Monad m) => |

662 |
Node.List -- ^ The node list |

663 |
-> Instance.List -- ^ The instance list |

664 |
-> Idx -- ^ The index of the instance to move |

665 |
-> Int -- ^ The numver of nodes required |

666 |
-> [Ndx] -- ^ Nodes which should not be used |

667 |
-> m AllocSolution -- ^ Solution list |

668 |
tryReloc nl il xid 1 ex_idx = |

669 |
let all_nodes = getOnline nl |

670 |
inst = Container.find xid il |

671 |
ex_idx' = (Instance.pnode inst):ex_idx |

672 |
valid_nodes = filter (not . flip elem ex_idx' . Node.idx) all_nodes |

673 |
valid_idxes = map Node.idx valid_nodes |

674 |
sols1 = map (\x -> let (mnl, i, _, _) = |

675 |
applyMove nl inst (ReplaceSecondary x) |

676 |
in (mnl, i, [Container.find x nl]) |

677 |
) valid_idxes |

678 |
in return sols1 |

679 | |

680 |
tryReloc _ _ _ reqn _ = fail $ "Unsupported number of relocation \ |

681 |
\destinations required (" ++ (show reqn) ++ |

682 |
"), only one supported" |

683 | |

684 |
-- * Formatting functions |

685 | |

686 |
-- | Given the original and final nodes, computes the relocation description. |

687 |
computeMoves :: String -- ^ The instance name |

688 |
-> String -- ^ Original primary |

689 |
-> String -- ^ Original secondary |

690 |
-> String -- ^ New primary |

691 |
-> String -- ^ New secondary |

692 |
-> (String, [String]) |

693 |
-- ^ Tuple of moves and commands list; moves is containing |

694 |
-- either @/f/@ for failover or @/r:name/@ for replace |

695 |
-- secondary, while the command list holds gnt-instance |

696 |
-- commands (without that prefix), e.g \"@failover instance1@\" |

697 |
computeMoves i a b c d = |

698 |
if c == a then {- Same primary -} |

699 |
if d == b then {- Same sec??! -} |

700 |
("-", []) |

701 |
else {- Change of secondary -} |

702 |
(printf "r:%s" d, |

703 |
[printf "replace-disks -n %s %s" d i]) |

704 |
else |

705 |
if c == b then {- Failover and ... -} |

706 |
if d == a then {- that's all -} |

707 |
("f", [printf "migrate -f %s" i]) |

708 |
else |

709 |
(printf "f r:%s" d, |

710 |
[printf "migrate -f %s" i, |

711 |
printf "replace-disks -n %s %s" d i]) |

712 |
else |

713 |
if d == a then {- ... and keep primary as secondary -} |

714 |
(printf "r:%s f" c, |

715 |
[printf "replace-disks -n %s %s" c i, |

716 |
printf "migrate -f %s" i]) |

717 |
else |

718 |
if d == b then {- ... keep same secondary -} |

719 |
(printf "f r:%s f" c, |

720 |
[printf "migrate -f %s" i, |

721 |
printf "replace-disks -n %s %s" c i, |

722 |
printf "migrate -f %s" i]) |

723 | |

724 |
else {- Nothing in common -} |

725 |
(printf "r:%s f r:%s" c d, |

726 |
[printf "replace-disks -n %s %s" c i, |

727 |
printf "migrate -f %s" i, |

728 |
printf "replace-disks -n %s %s" d i]) |

729 | |

730 |
-- | Converts a placement to string format. |

731 |
printSolutionLine :: Node.List -- ^ The node list |

732 |
-> Instance.List -- ^ The instance list |

733 |
-> Int -- ^ Maximum node name length |

734 |
-> Int -- ^ Maximum instance name length |

735 |
-> Placement -- ^ The current placement |

736 |
-> Int -- ^ The index of the placement in |

737 |
-- the solution |

738 |
-> (String, [String]) |

739 |
printSolutionLine nl il nmlen imlen plc pos = |

740 |
let |

741 |
pmlen = (2*nmlen + 1) |

742 |
(i, p, s, c) = plc |

743 |
inst = Container.find i il |

744 |
inam = Instance.name inst |

745 |
npri = Container.nameOf nl p |

746 |
nsec = Container.nameOf nl s |

747 |
opri = Container.nameOf nl $ Instance.pnode inst |

748 |
osec = Container.nameOf nl $ Instance.snode inst |

749 |
(moves, cmds) = computeMoves inam opri osec npri nsec |

750 |
ostr = (printf "%s:%s" opri osec)::String |

751 |
nstr = (printf "%s:%s" npri nsec)::String |

752 |
in |

753 |
(printf " %3d. %-*s %-*s => %-*s %.8f a=%s" |

754 |
pos imlen inam pmlen ostr |

755 |
pmlen nstr c moves, |

756 |
cmds) |

757 | |

758 |
-- | Given a list of commands, prefix them with @gnt-instance@ and |

759 |
-- also beautify the display a little. |

760 |
formatCmds :: [[String]] -> String |

761 |
formatCmds cmd_strs = |

762 |
unlines $ |

763 |
concat $ map (\(a, b) -> |

764 |
(printf "echo step %d" (a::Int)): |

765 |
(printf "check"): |

766 |
(map ("gnt-instance " ++) b)) $ |

767 |
zip [1..] cmd_strs |

768 | |

769 |
-- | Converts a solution to string format. |

770 |
printSolution :: Node.List |

771 |
-> Instance.List |

772 |
-> [Placement] |

773 |
-> ([String], [[String]]) |

774 |
printSolution nl il sol = |

775 |
let |

776 |
nmlen = Container.maxNameLen nl |

777 |
imlen = Container.maxNameLen il |

778 |
in |

779 |
unzip $ map (uncurry $ printSolutionLine nl il nmlen imlen) $ |

780 |
zip sol [1..] |

781 | |

782 |
-- | Print the node list. |

783 |
printNodes :: Node.List -> String |

784 |
printNodes nl = |

785 |
let snl = sortBy (compare `on` Node.idx) (Container.elems nl) |

786 |
m_name = maximum . map (length . Node.name) $ snl |

787 |
helper = Node.list m_name |

788 |
header = (printf |

789 |
"%2s %-*s %5s %5s %5s %5s %5s %5s %5s %5s %4s %4s \ |

790 |
\%3s %3s %6s %6s %5s" |

791 |
" F" m_name "Name" |

792 |
"t_mem" "n_mem" "i_mem" "x_mem" "f_mem" "r_mem" |

793 |
"t_dsk" "f_dsk" "pcpu" "vcpu" |

794 |
"pri" "sec" "p_fmem" "p_fdsk" "r_cpu")::String |

795 |
in unlines $ (header:map helper snl) |

796 | |

797 |
-- | Shows statistics for a given node list. |

798 |
printStats :: Node.List -> String |

799 |
printStats nl = |

800 |
let (mem_cv, dsk_cv, n1_score, res_cv, off_score, cpu_cv) = |

801 |
compDetailedCV nl |

802 |
in printf "f_mem=%.8f, r_mem=%.8f, f_dsk=%.8f, n1=%.3f, \ |

803 |
\uf=%.3f, r_cpu=%.3f" |

804 |
mem_cv res_cv dsk_cv n1_score off_score cpu_cv |