@recaptime-dev's working patches + fork for Phorge, a community fork of Phabricator. (Upstream dev and stable branches are at upstream/main and upstream/stable respectively.) hq.recaptime.dev/wiki/Phorge
phorge phabricator
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Use the same logic in "bin/drydock lease" and LeaseUpdateWorker to identify candidate blueprints

Summary:
Ref T13676. Currently, "bin/drydock lease" just creates a lease that permits any blueprint.

To prepare for "use specific blueprint X", unify the logic between this workflow and LeaseUpdateWorker so we select only blueprints which at least have coarse compatibility (e.g., if we're leasing a host, only select enabled blueprints of classes that can allocate hosts).

Test Plan: Used `bin/drydock lease` to try to lease a nonsense type, got sensible error. Leased a host.

Subscribers: yelirekim, PHID-OPKG-gm6ozazyms6q6i22gyam

Maniphest Tasks: T13676

Differential Revision: https://secure.phabricator.com/D21801

+80 -56
+41
src/applications/drydock/blueprint/DrydockBlueprintImplementation.php
··· 329 329 ->execute(); 330 330 } 331 331 332 + 333 + /** 334 + * Get all the @{class:DrydockBlueprintImplementation}s which can possibly 335 + * build a resource to satisfy a lease. 336 + * 337 + * This method returns blueprints which might, at some time, be able to 338 + * build a resource which can satisfy the lease. They may not be able to 339 + * build that resource right now. 340 + * 341 + * @param DrydockLease Requested lease. 342 + * @return list<DrydockBlueprintImplementation> List of qualifying blueprint 343 + * implementations. 344 + */ 345 + public static function getAllForAllocatingLease( 346 + DrydockLease $lease) { 347 + 348 + $impls = self::getAllBlueprintImplementations(); 349 + 350 + $keep = array(); 351 + foreach ($impls as $key => $impl) { 352 + // Don't use disabled blueprint types. 353 + if (!$impl->isEnabled()) { 354 + continue; 355 + } 356 + 357 + // Don't use blueprint types which can't allocate the correct kind of 358 + // resource. 359 + if ($impl->getType() != $lease->getResourceType()) { 360 + continue; 361 + } 362 + 363 + if (!$impl->canAnyBlueprintEverAllocateResourceForLease($lease)) { 364 + continue; 365 + } 366 + 367 + $keep[$key] = $impl; 368 + } 369 + 370 + return $keep; 371 + } 372 + 332 373 public static function getNamedImplementation($class) { 333 374 return idx(self::getAllBlueprintImplementations(), $class); 334 375 }
+38 -13
src/applications/drydock/management/DrydockManagementLeaseWorkflow.php
··· 79 79 $attributes = array(); 80 80 } 81 81 82 + $blueprint_phids = null; 83 + 82 84 $leases = array(); 83 85 for ($idx = 0; $idx < $count; $idx++) { 84 86 $lease = id(new DrydockLease()) ··· 91 93 $lease->setAttributes($attributes); 92 94 } 93 95 94 - // TODO: This is not hugely scalable, although this is a debugging 95 - // workflow so maybe it's fine. Do we even need `bin/drydock lease` in 96 - // the long run? 97 - $all_blueprints = id(new DrydockBlueprintQuery()) 98 - ->setViewer($viewer) 99 - ->execute(); 100 - $allowed_phids = mpull($all_blueprints, 'getPHID'); 101 - if (!$allowed_phids) { 102 - throw new Exception( 103 - pht( 104 - 'No blueprints exist which can plausibly allocate resources to '. 105 - 'satisfy the requested lease.')); 96 + if ($blueprint_phids === null) { 97 + $blueprint_phids = $this->newAllowedBlueprintPHIDs($lease); 106 98 } 107 - $lease->setAllowedBlueprintPHIDs($allowed_phids); 99 + 100 + $lease->setAllowedBlueprintPHIDs($blueprint_phids); 108 101 109 102 if ($until) { 110 103 $lease->setUntil($until); ··· 258 251 sleep(1); 259 252 } 260 253 } 254 + } 255 + 256 + private function newAllowedBlueprintPHIDs(DrydockLease $lease) { 257 + $viewer = $this->getViewer(); 258 + 259 + $impls = DrydockBlueprintImplementation::getAllForAllocatingLease($lease); 260 + 261 + if (!$impls) { 262 + throw new PhutilArgumentUsageException( 263 + pht( 264 + 'No known blueprint class can ever allocate the specified '. 265 + 'lease. Check that the resource type is spelled correctly.')); 266 + } 267 + 268 + $classes = array_keys($impls); 269 + 270 + $blueprints = id(new DrydockBlueprintQuery()) 271 + ->setViewer($viewer) 272 + ->withBlueprintClasses($classes) 273 + ->withDisabled(false) 274 + ->execute(); 275 + 276 + if (!$blueprints) { 277 + throw new PhutilArgumentUsageException( 278 + pht( 279 + 'No enabled blueprints exist with a blueprint class that can '. 280 + 'plausibly allocate resources to satisfy the requested lease.')); 281 + } 282 + 283 + $phids = mpull($blueprints, 'getPHID'); 284 + 285 + return $phids; 261 286 } 262 287 263 288 }
+1 -43
src/applications/drydock/worker/DrydockLeaseUpdateWorker.php
··· 344 344 345 345 346 346 /** 347 - * Get all the @{class:DrydockBlueprintImplementation}s which can possibly 348 - * build a resource to satisfy a lease. 349 - * 350 - * This method returns blueprints which might, at some time, be able to 351 - * build a resource which can satisfy the lease. They may not be able to 352 - * build that resource right now. 353 - * 354 - * @param DrydockLease Requested lease. 355 - * @return list<DrydockBlueprintImplementation> List of qualifying blueprint 356 - * implementations. 357 - * @task allocator 358 - */ 359 - private function loadBlueprintImplementationsForAllocatingLease( 360 - DrydockLease $lease) { 361 - 362 - $impls = DrydockBlueprintImplementation::getAllBlueprintImplementations(); 363 - 364 - $keep = array(); 365 - foreach ($impls as $key => $impl) { 366 - // Don't use disabled blueprint types. 367 - if (!$impl->isEnabled()) { 368 - continue; 369 - } 370 - 371 - // Don't use blueprint types which can't allocate the correct kind of 372 - // resource. 373 - if ($impl->getType() != $lease->getResourceType()) { 374 - continue; 375 - } 376 - 377 - if (!$impl->canAnyBlueprintEverAllocateResourceForLease($lease)) { 378 - continue; 379 - } 380 - 381 - $keep[$key] = $impl; 382 - } 383 - 384 - return $keep; 385 - } 386 - 387 - 388 - /** 389 347 * Get all the concrete @{class:DrydockBlueprint}s which can possibly 390 348 * build a resource to satisfy a lease. 391 349 * ··· 397 355 DrydockLease $lease) { 398 356 $viewer = $this->getViewer(); 399 357 400 - $impls = $this->loadBlueprintImplementationsForAllocatingLease($lease); 358 + $impls = DrydockBlueprintImplementation::getAllForAllocatingLease($lease); 401 359 if (!$impls) { 402 360 return array(); 403 361 }