Changeset View
Changeset View
Standalone View
Standalone View
src/applications/drydock/worker/DrydockLeaseUpdateWorker.php
Show First 20 Lines • Show All 300 Lines • ▼ Show 20 Lines | private function executeAllocator(DrydockLease $lease) { | ||||
$resources = $this->rankResources($resources, $lease); | $resources = $this->rankResources($resources, $lease); | ||||
$exceptions = array(); | $exceptions = array(); | ||||
$yields = array(); | $yields = array(); | ||||
$allocated = false; | $allocated = false; | ||||
foreach ($resources as $resource) { | foreach ($resources as $resource) { | ||||
try { | try { | ||||
$resource = $this->newResourceForAcquisition($resource, $lease); | |||||
$this->acquireLease($resource, $lease); | $this->acquireLease($resource, $lease); | ||||
$allocated = true; | $allocated = true; | ||||
break; | break; | ||||
} catch (DrydockResourceLockException $ex) { | } catch (DrydockResourceLockException $ex) { | ||||
// We need to lock the resource to actually acquire it. If we aren't | // We need to lock the resource to actually acquire it. If we aren't | ||||
// able to acquire the lock quickly enough, we can yield and try again | // able to acquire the lock quickly enough, we can yield and try again | ||||
// later. | // later. | ||||
$yields[] = $ex; | $yields[] = $ex; | ||||
} catch (DrydockAcquiredBrokenResourceException $ex) { | } catch (DrydockAcquiredBrokenResourceException $ex) { | ||||
// If a resource was reclaimed or destroyed by the time we actually | // If a resource was reclaimed or destroyed by the time we actually | ||||
// got around to acquiring it, we just got unlucky. We can yield and | // got around to acquiring it, we just got unlucky. We can yield and | ||||
// try again later. | // try again later. | ||||
$yields[] = $ex; | $yields[] = $ex; | ||||
} catch (PhabricatorWorkerYieldException $ex) { | |||||
// We can be told to yield, particularly by the supplemental allocator | |||||
// trying to give us a supplemental resource. | |||||
$yields[] = $ex; | |||||
} catch (Exception $ex) { | } catch (Exception $ex) { | ||||
$exceptions[] = $ex; | $exceptions[] = $ex; | ||||
} | } | ||||
} | } | ||||
if (!$allocated) { | if (!$allocated) { | ||||
if ($yields) { | if ($yields) { | ||||
throw new PhabricatorWorkerYieldException(15); | throw new PhabricatorWorkerYieldException(15); | ||||
▲ Show 20 Lines • Show All 456 Lines • ▼ Show 20 Lines | if ($lease_phid !== $resource_phid) { | ||||
'Blueprint "%s" (of type "%s") is not properly implemented: it '. | 'Blueprint "%s" (of type "%s") is not properly implemented: it '. | ||||
'returned from "%s" with a lease acquired on the wrong resource.', | 'returned from "%s" with a lease acquired on the wrong resource.', | ||||
$blueprint->getBlueprintName(), | $blueprint->getBlueprintName(), | ||||
$blueprint->getClassName(), | $blueprint->getClassName(), | ||||
'acquireLease()')); | 'acquireLease()')); | ||||
} | } | ||||
} | } | ||||
private function newResourceForAcquisition( | |||||
DrydockResource $resource, | |||||
DrydockLease $lease) { | |||||
// If the resource has no leases against it, never build a new one. This is | |||||
// likely already a new resource that just activated. | |||||
$viewer = $this->getViewer(); | |||||
$statuses = array( | |||||
DrydockLeaseStatus::STATUS_PENDING, | |||||
DrydockLeaseStatus::STATUS_ACQUIRED, | |||||
DrydockLeaseStatus::STATUS_ACTIVE, | |||||
); | |||||
$leases = id(new DrydockLeaseQuery()) | |||||
->setViewer($viewer) | |||||
->withResourcePHIDs(array($resource->getPHID())) | |||||
->withStatuses($statuses) | |||||
->setLimit(1) | |||||
->execute(); | |||||
if (!$leases) { | |||||
return $resource; | |||||
} | |||||
// If we're about to get a lease on a resource, check if the blueprint | |||||
// wants to allocate a supplemental resource. If it does, try to perform a | |||||
// new allocation instead. | |||||
$blueprint = $resource->getBlueprint(); | |||||
if (!$blueprint->shouldAllocateSupplementalResource($resource, $lease)) { | |||||
return $resource; | |||||
} | |||||
// If the blueprint is already overallocated, we can't allocate a new | |||||
// resource. Just return the existing resource. | |||||
$remaining = $this->removeOverallocatedBlueprints( | |||||
array($blueprint), | |||||
$lease); | |||||
if (!$remaining) { | |||||
return $resource; | |||||
} | |||||
// Try to build a new resource. | |||||
try { | |||||
$new_resource = $this->allocateResource($blueprint, $lease); | |||||
} catch (Exception $ex) { | |||||
$blueprint->logEvent( | |||||
DrydockResourceAllocationFailureLogType::LOGCONST, | |||||
array( | |||||
'class' => get_class($ex), | |||||
'message' => $ex->getMessage(), | |||||
)); | |||||
return $resource; | |||||
} | |||||
// If we can't actually acquire the new resource yet, just yield. | |||||
// (We could try to move forward with the original resource instead.) | |||||
$acquirable = $this->removeUnacquirableResources( | |||||
array($new_resource), | |||||
$lease); | |||||
if (!$acquirable) { | |||||
throw new PhabricatorWorkerYieldException(15); | |||||
} | |||||
return $new_resource; | |||||
} | |||||
/* -( Activating Leases )-------------------------------------------------- */ | /* -( Activating Leases )-------------------------------------------------- */ | ||||
/** | /** | ||||
* @task activate | * @task activate | ||||
*/ | */ | ||||
private function activateLease(DrydockLease $lease) { | private function activateLease(DrydockLease $lease) { | ||||
▲ Show 20 Lines • Show All 153 Lines • Show Last 20 Lines |