| 1 |
{-# LANGUAGE TemplateHaskell, ScopedTypeVariables, NamedFieldPuns #-} |
| 2 |
{-# OPTIONS_GHC -fno-warn-orphans #-} |
| 3 |
|
| 4 |
{-| Unittests for the job scheduler. |
| 5 |
|
| 6 |
-} |
| 7 |
|
| 8 |
{- |
| 9 |
|
| 10 |
Copyright (C) 2014 Google Inc. |
| 11 |
All rights reserved. |
| 12 |
|
| 13 |
Redistribution and use in source and binary forms, with or without |
| 14 |
modification, are permitted provided that the following conditions are |
| 15 |
met: |
| 16 |
|
| 17 |
1. Redistributions of source code must retain the above copyright notice, |
| 18 |
this list of conditions and the following disclaimer. |
| 19 |
|
| 20 |
2. Redistributions in binary form must reproduce the above copyright |
| 21 |
notice, this list of conditions and the following disclaimer in the |
| 22 |
documentation and/or other materials provided with the distribution. |
| 23 |
|
| 24 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS |
| 25 |
IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
| 26 |
TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 27 |
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR |
| 28 |
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 29 |
EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 30 |
PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 31 |
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
| 32 |
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 33 |
NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 34 |
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 35 |
|
| 36 |
-} |
| 37 |
|
| 38 |
module Test.Ganeti.JQScheduler (testJQScheduler) where |
| 39 |
|
| 40 |
import Control.Lens ((&), (.~), _2) |
| 41 |
import qualified Data.ByteString.UTF8 as UTF8 |
| 42 |
import Data.List (inits) |
| 43 |
import Data.Maybe |
| 44 |
import qualified Data.Map as Map |
| 45 |
import Data.Set (Set, difference) |
| 46 |
import qualified Data.Set as Set |
| 47 |
import Text.JSON (JSValue(..)) |
| 48 |
import Test.HUnit |
| 49 |
import Test.QuickCheck |
| 50 |
|
| 51 |
import Test.Ganeti.JQueue.Objects (genQueuedOpCode, genJobId, justNoTs) |
| 52 |
import Test.Ganeti.SlotMap (genTestKey, overfullKeys) |
| 53 |
import Test.Ganeti.TestCommon |
| 54 |
import Test.Ganeti.TestHelper |
| 55 |
import Test.Ganeti.Types () |
| 56 |
|
| 57 |
import Ganeti.JQScheduler.Filtering |
| 58 |
import Ganeti.JQScheduler.ReasonRateLimiting |
| 59 |
import Ganeti.JQScheduler.Types |
| 60 |
import Ganeti.JQueue.Lens |
| 61 |
import Ganeti.JQueue.Objects |
| 62 |
import Ganeti.Objects (FilterRule(..), FilterPredicate(..), FilterAction(..), |
| 63 |
filterRuleOrder) |
| 64 |
import Ganeti.OpCodes |
| 65 |
import Ganeti.OpCodes.Lens |
| 66 |
import Ganeti.Query.Language (Filter(..), FilterValue(..)) |
| 67 |
import Ganeti.SlotMap |
| 68 |
import Ganeti.Types |
| 69 |
import Ganeti.Utils (isSubsequenceOf, newUUID) |
| 70 |
|
| 71 |
{-# ANN module "HLint: ignore Use camelCase" #-} |
| 72 |
|
| 73 |
|
| 74 |
genRateLimitReason :: Gen String |
| 75 |
genRateLimitReason = do |
| 76 |
Slot{ slotLimit = n } <- arbitrary |
| 77 |
l <- genTestKey |
| 78 |
return $ "rate-limit:" ++ show n ++ ":" ++ l |
| 79 |
|
| 80 |
|
| 81 |
instance Arbitrary QueuedJob where |
| 82 |
arbitrary = do |
| 83 |
-- For our scheduler testing purposes here, we only care about |
| 84 |
-- opcodes, job ID and reason rate limits. |
| 85 |
jid <- genJobId |
| 86 |
|
| 87 |
ops <- resize 5 . listOf1 $ do |
| 88 |
o <- genQueuedOpCode |
| 89 |
-- Put some rate limits into the OpCode. |
| 90 |
limitString <- genRateLimitReason |
| 91 |
return $ |
| 92 |
o & qoInputL . validOpCodeL . metaParamsL . opReasonL . traverse . _2 |
| 93 |
.~ limitString |
| 94 |
|
| 95 |
return $ QueuedJob jid ops justNoTs justNoTs justNoTs Nothing Nothing |
| 96 |
|
| 97 |
|
| 98 |
instance Arbitrary JobWithStat where |
| 99 |
arbitrary = nullJobWithStat <$> arbitrary |
| 100 |
shrink job = [ job { jJob = x } | x <- shrink (jJob job) ] |
| 101 |
|
| 102 |
|
| 103 |
instance Arbitrary Queue where |
| 104 |
arbitrary = do |
| 105 |
|
| 106 |
let genJobsUniqueJIDs :: [JobWithStat] -> Gen [JobWithStat] |
| 107 |
genJobsUniqueJIDs = listOfUniqueBy arbitrary (qjId . jJob) |
| 108 |
|
| 109 |
queued <- genJobsUniqueJIDs [] |
| 110 |
running <- genJobsUniqueJIDs queued |
| 111 |
manip <- genJobsUniqueJIDs (queued ++ running) |
| 112 |
|
| 113 |
return $ Queue queued running manip |
| 114 |
shrink q = |
| 115 |
[ q { qEnqueued = x } | x <- shrink (qEnqueued q) ] ++ |
| 116 |
[ q { qRunning = x } | x <- shrink (qRunning q) ] ++ |
| 117 |
[ q { qManipulated = x } | x <- shrink (qManipulated q) ] |
| 118 |
|
| 119 |
|
| 120 |
-- * Test cases |
| 121 |
|
| 122 |
-- | Tests rate limit reason trail parsing. |
| 123 |
case_parseReasonRateLimit :: Assertion |
| 124 |
case_parseReasonRateLimit = do |
| 125 |
|
| 126 |
assertBool "default case" $ |
| 127 |
let a = parseReasonRateLimit "rate-limit:20:my label" |
| 128 |
b = parseReasonRateLimit "rate-limit:21:my label" |
| 129 |
in and |
| 130 |
[ a == Just ("20:my label", 20) |
| 131 |
, b == Just ("21:my label", 21) |
| 132 |
] |
| 133 |
|
| 134 |
assertEqual "be picky about whitespace" |
| 135 |
Nothing |
| 136 |
(parseReasonRateLimit " rate-limit:20:my label") |
| 137 |
|
| 138 |
|
| 139 |
-- | Tests that "rateLimit:n:..." and "rateLimit:m:..." become different |
| 140 |
-- rate limiting buckets. |
| 141 |
prop_slotMapFromJob_conflicting_buckets :: Property |
| 142 |
prop_slotMapFromJob_conflicting_buckets = do |
| 143 |
|
| 144 |
let sameBucketReasonStringGen :: Gen (String, String) |
| 145 |
sameBucketReasonStringGen = do |
| 146 |
Positive (n :: Int) <- arbitrary |
| 147 |
Positive (m :: Int) <- arbitrary `suchThat` (/= Positive n) |
| 148 |
l <- genPrintableAsciiString |
| 149 |
return ( "rate-limit:" ++ show n ++ ":" ++ l |
| 150 |
, "rate-limit:" ++ show m ++ ":" ++ l ) |
| 151 |
|
| 152 |
forAll sameBucketReasonStringGen $ \(s1, s2) -> do |
| 153 |
(lab1, lim1) <- parseReasonRateLimit s1 |
| 154 |
(lab2, _ ) <- parseReasonRateLimit s2 |
| 155 |
let sm = Map.fromList [(lab1, Slot 1 lim1)] |
| 156 |
cm = Map.fromList [(lab2, 1)] |
| 157 |
in return $ |
| 158 |
(sm `occupySlots` cm) ==? Map.fromList [ (lab1, Slot 1 lim1) |
| 159 |
, (lab2, Slot 1 0) |
| 160 |
] :: Gen Property |
| 161 |
|
| 162 |
|
| 163 |
-- | Tests some basic cases for reason rate limiting. |
| 164 |
case_reasonRateLimit :: Assertion |
| 165 |
case_reasonRateLimit = do |
| 166 |
|
| 167 |
let mkJobWithReason jobNum reasonTrail = do |
| 168 |
opc <- genSample genQueuedOpCode |
| 169 |
jid <- makeJobId jobNum |
| 170 |
let opc' = opc & (qoInputL . validOpCodeL . metaParamsL . opReasonL) |
| 171 |
.~ reasonTrail |
| 172 |
return . nullJobWithStat |
| 173 |
$ QueuedJob |
| 174 |
{ qjId = jid |
| 175 |
, qjOps = [opc'] |
| 176 |
, qjReceivedTimestamp = Nothing |
| 177 |
, qjStartTimestamp = Nothing |
| 178 |
, qjEndTimestamp = Nothing |
| 179 |
, qjLivelock = Nothing |
| 180 |
, qjProcessId = Nothing |
| 181 |
} |
| 182 |
|
| 183 |
-- 3 jobs, limited to 2 of them running. |
| 184 |
j1 <- mkJobWithReason 1 [("source1", "rate-limit:2:hello", 0)] |
| 185 |
j2 <- mkJobWithReason 2 [("source1", "rate-limit:2:hello", 0)] |
| 186 |
j3 <- mkJobWithReason 3 [("source1", "rate-limit:2:hello", 0)] |
| 187 |
|
| 188 |
assertEqual "[j1] should not be rate-limited" |
| 189 |
[j1] |
| 190 |
(reasonRateLimit (Queue [j1] [] []) [j1]) |
| 191 |
|
| 192 |
assertEqual "[j1, j2] should not be rate-limited" |
| 193 |
[j1, j2] |
| 194 |
(reasonRateLimit (Queue [j1, j2] [] []) [j1, j2]) |
| 195 |
|
| 196 |
assertEqual "j3 should be rate-limited 1" |
| 197 |
[j1, j2] |
| 198 |
(reasonRateLimit (Queue [j1, j2, j3] [] []) [j1, j2, j3]) |
| 199 |
|
| 200 |
assertEqual "j3 should be rate-limited 2" |
| 201 |
[j2] |
| 202 |
(reasonRateLimit (Queue [j2, j3] [j1] []) [j2, j3]) |
| 203 |
|
| 204 |
assertEqual "j3 should be rate-limited 3" |
| 205 |
[] |
| 206 |
(reasonRateLimit (Queue [j3] [j1] [j2]) [j3]) |
| 207 |
|
| 208 |
|
| 209 |
-- | Tests the specified properties of `reasonRateLimit`, as defined in |
| 210 |
-- `doc/design-optables.rst`. |
| 211 |
prop_reasonRateLimit :: Property |
| 212 |
prop_reasonRateLimit = |
| 213 |
forAllShrink arbitrary shrink $ \q -> |
| 214 |
|
| 215 |
let slotMapFromJobWithStat = slotMapFromJobs . map jJob |
| 216 |
|
| 217 |
enqueued = qEnqueued q |
| 218 |
|
| 219 |
toRun = reasonRateLimit q enqueued |
| 220 |
|
| 221 |
oldSlots = slotMapFromJobWithStat (qRunning q) |
| 222 |
newSlots = slotMapFromJobWithStat (qRunning q ++ toRun) |
| 223 |
-- What would happen without rate limiting. |
| 224 |
newSlotsNoLimits = slotMapFromJobWithStat (qRunning q ++ enqueued) |
| 225 |
|
| 226 |
in -- Ensure it's unlikely that jobs are all in different buckets. |
| 227 |
cover' |
| 228 |
50 |
| 229 |
(any ((> 1) . slotOccupied) . Map.elems $ newSlotsNoLimits) |
| 230 |
"some jobs have the same rate-limit bucket" |
| 231 |
|
| 232 |
-- Ensure it's likely that rate limiting has any effect. |
| 233 |
. cover' |
| 234 |
50 |
| 235 |
(overfullKeys newSlotsNoLimits |
| 236 |
`difference` overfullKeys oldSlots /= Set.empty) |
| 237 |
"queued jobs cannot be started because of rate limiting" |
| 238 |
|
| 239 |
$ conjoin |
| 240 |
[ counterexample "scheduled jobs must be subsequence" $ |
| 241 |
toRun `isSubsequenceOf` enqueued |
| 242 |
|
| 243 |
-- This is the key property: |
| 244 |
, counterexample "no job may exceed its bucket limits, except from\ |
| 245 |
\ jobs that were already running with exceeded\ |
| 246 |
\ limits; those must not increase" $ |
| 247 |
conjoin |
| 248 |
[ if occup <= limit |
| 249 |
-- Within limits, all fine. |
| 250 |
then passTest |
| 251 |
-- Bucket exceeds limits - it must have exceeded them |
| 252 |
-- in the initial running list already, with the same |
| 253 |
-- slot count. |
| 254 |
else Map.lookup k oldSlots ==? Just slot |
| 255 |
| (k, slot@(Slot occup limit)) <- Map.toList newSlots ] |
| 256 |
] |
| 257 |
|
| 258 |
-- | Tests that filter rule ordering is determined (solely) by priority, |
| 259 |
-- watermark and UUID, as defined in `doc/design-optables.rst`. |
| 260 |
prop_filterRuleOrder :: Property |
| 261 |
prop_filterRuleOrder = property $ do |
| 262 |
a <- arbitrary |
| 263 |
b <- arbitrary `suchThat` ((frUuid a /=) . frUuid) |
| 264 |
return $ filterRuleOrder a b ==? (frPriority a, frWatermark a, frUuid a) |
| 265 |
`compare` |
| 266 |
(frPriority b, frWatermark b, frUuid b) |
| 267 |
|
| 268 |
|
| 269 |
-- | Tests common inputs for `matchPredicate`, especially the predicates |
| 270 |
-- and fields available to them as defined in the spec. |
| 271 |
case_matchPredicate :: Assertion |
| 272 |
case_matchPredicate = do |
| 273 |
|
| 274 |
jid1 <- makeJobId 1 |
| 275 |
clusterName <- mkNonEmpty "cluster1" |
| 276 |
|
| 277 |
let job = |
| 278 |
QueuedJob |
| 279 |
{ qjId = jid1 |
| 280 |
, qjOps = |
| 281 |
[ QueuedOpCode |
| 282 |
{ qoInput = ValidOpCode MetaOpCode |
| 283 |
{ metaParams = CommonOpParams |
| 284 |
{ opDryRun = Nothing |
| 285 |
, opDebugLevel = Nothing |
| 286 |
, opPriority = OpPrioHigh |
| 287 |
, opDepends = Just [] |
| 288 |
, opComment = Nothing |
| 289 |
, opReason = [("source1", "reason1", 1234)] |
| 290 |
} |
| 291 |
, metaOpCode = OpClusterRename |
| 292 |
{ opName = clusterName |
| 293 |
} |
| 294 |
} |
| 295 |
, qoStatus = OP_STATUS_QUEUED |
| 296 |
, qoResult = JSNull |
| 297 |
, qoLog = [] |
| 298 |
, qoPriority = -1 |
| 299 |
, qoStartTimestamp = Nothing |
| 300 |
, qoExecTimestamp = Nothing |
| 301 |
, qoEndTimestamp = Nothing |
| 302 |
} |
| 303 |
] |
| 304 |
, qjReceivedTimestamp = Nothing |
| 305 |
, qjStartTimestamp = Nothing |
| 306 |
, qjEndTimestamp = Nothing |
| 307 |
, qjLivelock = Nothing |
| 308 |
, qjProcessId = Nothing |
| 309 |
} |
| 310 |
|
| 311 |
let watermark = jid1 |
| 312 |
|
| 313 |
check = matchPredicate job watermark |
| 314 |
|
| 315 |
-- jobid filters |
| 316 |
|
| 317 |
assertEqual "matching jobid filter" |
| 318 |
True |
| 319 |
. check $ FPJobId (EQFilter "id" (NumericValue 1)) |
| 320 |
|
| 321 |
assertEqual "non-matching jobid filter" |
| 322 |
False |
| 323 |
. check $ FPJobId (EQFilter "id" (NumericValue 2)) |
| 324 |
|
| 325 |
assertEqual "non-matching jobid filter (string passed)" |
| 326 |
False |
| 327 |
. check $ FPJobId (EQFilter "id" (QuotedString "1")) |
| 328 |
|
| 329 |
-- jobid filters: watermarks |
| 330 |
|
| 331 |
assertEqual "matching jobid watermark filter" |
| 332 |
True |
| 333 |
. check $ FPJobId (EQFilter "id" (QuotedString "watermark")) |
| 334 |
|
| 335 |
-- opcode filters |
| 336 |
|
| 337 |
assertEqual "matching opcode filter (type of opcode)" |
| 338 |
True |
| 339 |
. check $ FPOpCode (EQFilter "OP_ID" (QuotedString "OP_CLUSTER_RENAME")) |
| 340 |
|
| 341 |
assertEqual "non-matching opcode filter (type of opcode)" |
| 342 |
False |
| 343 |
. check $ FPOpCode (EQFilter "OP_ID" (QuotedString "OP_INSTANCE_CREATE")) |
| 344 |
|
| 345 |
assertEqual "matching opcode filter (nested access)" |
| 346 |
True |
| 347 |
. check $ FPOpCode (EQFilter "name" (QuotedString "cluster1")) |
| 348 |
|
| 349 |
assertEqual "non-matching opcode filter (nonexistent nested access)" |
| 350 |
False |
| 351 |
. check $ FPOpCode (EQFilter "something" (QuotedString "cluster1")) |
| 352 |
|
| 353 |
-- reason filters |
| 354 |
|
| 355 |
assertEqual "matching reason filter (reason field)" |
| 356 |
True |
| 357 |
. check $ FPReason (EQFilter "reason" (QuotedString "reason1")) |
| 358 |
|
| 359 |
assertEqual "non-matching reason filter (reason field)" |
| 360 |
False |
| 361 |
. check $ FPReason (EQFilter "reason" (QuotedString "reasonGarbage")) |
| 362 |
|
| 363 |
assertEqual "matching reason filter (source field)" |
| 364 |
True |
| 365 |
. check $ FPReason (EQFilter "source" (QuotedString "source1")) |
| 366 |
|
| 367 |
assertEqual "matching reason filter (timestamp field)" |
| 368 |
True |
| 369 |
. check $ FPReason (EQFilter "timestamp" (NumericValue 1234)) |
| 370 |
|
| 371 |
assertEqual "non-matching reason filter (nonexistent field)" |
| 372 |
False |
| 373 |
. check $ FPReason (EQFilter "something" (QuotedString "")) |
| 374 |
|
| 375 |
|
| 376 |
-- | Tests that jobs selected by `applyingFilter` actually match |
| 377 |
-- and have an effect (are not CONTINUE filters). |
| 378 |
prop_applyingFilter :: Property |
| 379 |
prop_applyingFilter = |
| 380 |
forAllShrink arbitrary shrink $ \job -> |
| 381 |
forAllShrink (arbitrary `suchThat` |
| 382 |
(isJust . flip applyingFilter job . Set.fromList)) shrink |
| 383 |
$ \filters -> |
| 384 |
|
| 385 |
let applying = applyingFilter (Set.fromList filters) job |
| 386 |
|
| 387 |
in case applying of |
| 388 |
Just f -> job `matches` f && frAction f /= Continue |
| 389 |
Nothing -> error "Should not happen" |
| 390 |
|
| 391 |
|
| 392 |
case_jobFiltering :: Assertion |
| 393 |
case_jobFiltering = do |
| 394 |
|
| 395 |
clusterName <- mkNonEmpty "cluster1" |
| 396 |
jid1 <- makeJobId 1 |
| 397 |
jid2 <- makeJobId 2 |
| 398 |
jid3 <- makeJobId 3 |
| 399 |
jid4 <- makeJobId 4 |
| 400 |
unsetPrio <- mkNonNegative 1234 |
| 401 |
uuid1 <- fmap UTF8.fromString newUUID |
| 402 |
|
| 403 |
let j1 = |
| 404 |
nullJobWithStat QueuedJob |
| 405 |
{ qjId = jid1 |
| 406 |
, qjOps = |
| 407 |
[ QueuedOpCode |
| 408 |
{ qoInput = ValidOpCode MetaOpCode |
| 409 |
{ metaParams = CommonOpParams |
| 410 |
{ opDryRun = Nothing |
| 411 |
, opDebugLevel = Nothing |
| 412 |
, opPriority = OpPrioHigh |
| 413 |
, opDepends = Just [] |
| 414 |
, opComment = Nothing |
| 415 |
, opReason = [("source1", "reason1", 1234)]} |
| 416 |
, metaOpCode = OpClusterRename |
| 417 |
{ opName = clusterName |
| 418 |
} |
| 419 |
} |
| 420 |
, qoStatus = OP_STATUS_QUEUED |
| 421 |
, qoResult = JSNull |
| 422 |
, qoLog = [] |
| 423 |
, qoPriority = -1 |
| 424 |
, qoStartTimestamp = Nothing |
| 425 |
, qoExecTimestamp = Nothing |
| 426 |
, qoEndTimestamp = Nothing |
| 427 |
} |
| 428 |
] |
| 429 |
, qjReceivedTimestamp = Nothing |
| 430 |
, qjStartTimestamp = Nothing |
| 431 |
, qjEndTimestamp = Nothing |
| 432 |
, qjLivelock = Nothing |
| 433 |
, qjProcessId = Nothing |
| 434 |
} |
| 435 |
|
| 436 |
j2 = j1 & jJobL . qjIdL .~ jid2 |
| 437 |
j3 = j1 & jJobL . qjIdL .~ jid3 |
| 438 |
j4 = j1 & jJobL . qjIdL .~ jid4 |
| 439 |
|
| 440 |
|
| 441 |
fr1 = |
| 442 |
FilterRule |
| 443 |
{ frWatermark = jid1 |
| 444 |
, frPriority = unsetPrio |
| 445 |
, frPredicates = [FPJobId (EQFilter "id" (NumericValue 1))] |
| 446 |
, frAction = Reject |
| 447 |
, frReasonTrail = [] |
| 448 |
, frUuid = uuid1 |
| 449 |
} |
| 450 |
|
| 451 |
-- Gives the rule a new UUID. |
| 452 |
rule fr = do |
| 453 |
uuid <- fmap UTF8.fromString newUUID |
| 454 |
return fr{ frUuid = uuid } |
| 455 |
|
| 456 |
-- Helper to create filter chains: assigns the filters in the list |
| 457 |
-- increasing priorities, so that filters listed first are processed |
| 458 |
-- first. |
| 459 |
chain :: [FilterRule] -> Set FilterRule |
| 460 |
chain frs |
| 461 |
| any ((/= unsetPrio) . frPriority) frs = |
| 462 |
error "Filter was passed to `chain` that already had a priority." |
| 463 |
| otherwise = |
| 464 |
Set.fromList |
| 465 |
[ fr{ frPriority = prio } |
| 466 |
| (fr, Just prio) <- zip frs (map mkNonNegative [1..]) ] |
| 467 |
|
| 468 |
fr2 <- rule fr1{ frAction = Accept } |
| 469 |
fr3 <- rule fr1{ frAction = Pause } |
| 470 |
|
| 471 |
fr4 <- rule fr1{ frPredicates = |
| 472 |
[FPJobId (GTFilter "id" (QuotedString "watermark"))] |
| 473 |
} |
| 474 |
|
| 475 |
fr5 <- rule fr1{ frPredicates = [] } |
| 476 |
|
| 477 |
fr6 <- rule fr5{ frAction = Continue } |
| 478 |
fr7 <- rule fr6{ frAction = RateLimit 2 } |
| 479 |
|
| 480 |
fr8 <- rule fr4{ frAction = Continue, frWatermark = jid1 } |
| 481 |
fr9 <- rule fr8{ frAction = RateLimit 2 } |
| 482 |
|
| 483 |
assertEqual "j1 should be rejected (by fr1)" |
| 484 |
[] |
| 485 |
(jobFiltering (Queue [j1] [] []) (chain [fr1]) [j1]) |
| 486 |
|
| 487 |
assertEqual "j1 should be rejected (by fr1, it has priority)" |
| 488 |
[] |
| 489 |
(jobFiltering (Queue [j1] [] []) (chain [fr1, fr2]) [j1]) |
| 490 |
|
| 491 |
assertEqual "j1 should be accepted (by fr2, it has priority)" |
| 492 |
[j1] |
| 493 |
(jobFiltering (Queue [j1] [] []) (chain [fr2, fr1]) [j1]) |
| 494 |
|
| 495 |
assertEqual "j1 should be paused (by fr3)" |
| 496 |
[] |
| 497 |
(jobFiltering (Queue [j1] [] []) (chain [fr3]) [j1]) |
| 498 |
|
| 499 |
assertEqual "j2 should be rejected (over watermark1)" |
| 500 |
[j1] |
| 501 |
(jobFiltering (Queue [j1, j2] [] []) (chain [fr4]) [j1, j2]) |
| 502 |
|
| 503 |
assertEqual "all jobs should be rejected (since no predicates)" |
| 504 |
[] |
| 505 |
(jobFiltering (Queue [j1, j2] [] []) (chain [fr5]) [j1, j2]) |
| 506 |
|
| 507 |
assertEqual "j3 should be rate-limited" |
| 508 |
[j1, j2] |
| 509 |
(jobFiltering (Queue [j1, j2, j3] [] []) (chain [fr6, fr7]) [j1, j2, j3]) |
| 510 |
|
| 511 |
assertEqual "j4 should be rate-limited" |
| 512 |
-- j1 doesn't apply to fr8/fr9 (since they match only watermark > jid1) |
| 513 |
-- so j1 gets scheduled |
| 514 |
[j1, j2, j3] |
| 515 |
(jobFiltering (Queue [j1, j2, j3, j4] [] []) (chain [fr8, fr9]) |
| 516 |
[j1, j2, j3, j4]) |
| 517 |
|
| 518 |
|
| 519 |
-- | Tests the specified properties of `jobFiltering`, as defined in |
| 520 |
-- `doc/design-optables.rst`. |
| 521 |
prop_jobFiltering :: Property |
| 522 |
prop_jobFiltering = |
| 523 |
forAllShrink (arbitrary `suchThat` (not . null . qEnqueued)) shrink $ \q -> |
| 524 |
forAllShrink (resize 4 arbitrary) shrink $ \(NonEmpty filterList) -> |
| 525 |
|
| 526 |
let running = qRunning q ++ qManipulated q |
| 527 |
enqueued = qEnqueued q |
| 528 |
|
| 529 |
filters = Set.fromList filterList |
| 530 |
|
| 531 |
toRun = jobFiltering q filters enqueued -- do the filtering |
| 532 |
|
| 533 |
-- Helpers |
| 534 |
|
| 535 |
-- Whether `fr` applies to more than `n` of the `jobs` |
| 536 |
-- (that is, more than allowed). |
| 537 |
exceeds :: Int -> FilterRule -> [JobWithStat] -> Bool |
| 538 |
exceeds n fr jobs = |
| 539 |
n < (length |
| 540 |
. filter ((frUuid fr ==) . frUuid) |
| 541 |
. mapMaybe (applyingFilter filters) |
| 542 |
$ map jJob jobs) |
| 543 |
{- TODO(#1318): restore coverage checks after a way to do it nicely |
| 544 |
has been found. |
| 545 |
|
| 546 |
-- Helpers for ensuring sensible coverage. |
| 547 |
|
| 548 |
-- Makes sure that each action appears with some probability. |
| 549 |
actionName = head . words . show |
| 550 |
allActions = map actionName [ Accept, Continue, Pause, Reject |
| 551 |
, RateLimit 0 ] |
| 552 |
applyingActions = map (actionName . frAction) |
| 553 |
. mapMaybe (applyingFilter filters) |
| 554 |
$ map jJob enqueued |
| 555 |
perc = 4 -- percent; low because it's per action |
| 556 |
actionCovers = |
| 557 |
foldr (.) id |
| 558 |
[ stableCover (a `elem` applyingActions) perc ("is " ++ a) |
| 559 |
| a <- allActions ] |
| 560 |
-} |
| 561 |
|
| 562 |
-- Note: if using `covers`, it should be before `conjoin` (see |
| 563 |
-- QuickCheck bugs 25 and 27). |
| 564 |
in conjoin |
| 565 |
[ counterexample "scheduled jobs must be subsequence" $ |
| 566 |
toRun `isSubsequenceOf` enqueued |
| 567 |
|
| 568 |
, counterexample "a reason for each job (not) being scheduled" . |
| 569 |
|
| 570 |
-- All enqueued jobs must have a reason why they were (not) |
| 571 |
-- scheduled, determined by the filter that applies. |
| 572 |
flip all enqueued $ \job -> |
| 573 |
case applyingFilter filters (jJob job) of |
| 574 |
-- If no filter matches, the job must run. |
| 575 |
Nothing -> job `elem` toRun |
| 576 |
Just fr@FilterRule{ frAction } -> case frAction of |
| 577 |
-- ACCEPT filter permit the job immediately, |
| 578 |
-- PAUSE/REJECT forbid running, CONTINUE filters cannot |
| 579 |
-- be the output of `applyingFilter`, and |
| 580 |
-- RATE_LIMIT filters have a more more complex property. |
| 581 |
Accept -> job `elem` toRun |
| 582 |
Continue -> error "must not happen" |
| 583 |
Pause -> job `notElem` toRun |
| 584 |
Reject -> job `notElem` toRun |
| 585 |
RateLimit n -> |
| 586 |
|
| 587 |
let -- Jobs in queue before our job. |
| 588 |
jobsBefore = takeWhile (/= job) enqueued |
| 589 |
|
| 590 |
in if job `elem` toRun |
| 591 |
-- If it got scheduled, the job and any job |
| 592 |
-- before it doesn't overfill the rate limit. |
| 593 |
then not . exceeds n fr $ running |
| 594 |
++ jobsBefore ++ [job] |
| 595 |
-- If didn't get scheduled, then the rate limit |
| 596 |
-- was already full before scheduling or the job |
| 597 |
-- or one of the jobs before made it full. |
| 598 |
else any (exceeds n fr . (running ++)) |
| 599 |
(inits $ jobsBefore ++ [job]) |
| 600 |
-- The `inits` bit includes the [] and [...job] |
| 601 |
-- cases. |
| 602 |
|
| 603 |
] |
| 604 |
|
| 605 |
|
| 606 |
testSuite "JQScheduler" |
| 607 |
[ 'case_parseReasonRateLimit |
| 608 |
, 'prop_slotMapFromJob_conflicting_buckets |
| 609 |
, 'case_reasonRateLimit |
| 610 |
, 'prop_reasonRateLimit |
| 611 |
, 'prop_filterRuleOrder |
| 612 |
, 'case_matchPredicate |
| 613 |
, 'prop_applyingFilter |
| 614 |
, 'case_jobFiltering |
| 615 |
-- Temporarily disabled until we fix the coverage (#1318) |
| 616 |
--, 'prop_jobFiltering |
| 617 |
] |