Differential D21808 Diff 51982 src/applications/drydock/blueprint/DrydockBlueprintImplementation.php
Changeset View
Changeset View
Standalone View
Standalone View
src/applications/drydock/blueprint/DrydockBlueprintImplementation.php
Show First 20 Lines • Show All 500 Lines • ▼ Show 20 Lines | /* -( Logging )------------------------------------------------------------ */ | ||||
* Apply standard limits on resource allocation rate. | * Apply standard limits on resource allocation rate. | ||||
* | * | ||||
* @param DrydockBlueprint The blueprint requesting an allocation. | * @param DrydockBlueprint The blueprint requesting an allocation. | ||||
* @return bool True if further allocations should be limited. | * @return bool True if further allocations should be limited. | ||||
*/ | */ | ||||
protected function shouldLimitAllocatingPoolSize( | protected function shouldLimitAllocatingPoolSize( | ||||
DrydockBlueprint $blueprint) { | DrydockBlueprint $blueprint) { | ||||
// TODO: If this mechanism sticks around, these values should be | |||||
// configurable by the blueprint implementation. | |||||
// Limit on total number of active resources. | // Limit on total number of active resources. | ||||
$total_limit = $this->getConcurrentResourceLimit($blueprint); | $total_limit = $this->getConcurrentResourceLimit($blueprint); | ||||
if ($total_limit === null) { | |||||
// Always allow at least this many allocations to be in flight at once. | return false; | ||||
$min_allowed = 1; | } | ||||
// Allow this fraction of allocating resources as a fraction of active | |||||
// resources. | |||||
$growth_factor = 0.25; | |||||
$resource = new DrydockResource(); | $resource = new DrydockResource(); | ||||
$conn_r = $resource->establishConnection('r'); | $conn = $resource->establishConnection('r'); | ||||
$counts = queryfx_all( | $counts = queryfx_all( | ||||
$conn_r, | $conn, | ||||
'SELECT status, COUNT(*) N FROM %T | 'SELECT status, COUNT(*) N FROM %R | ||||
WHERE blueprintPHID = %s AND status != %s | WHERE blueprintPHID = %s AND status != %s | ||||
GROUP BY status', | GROUP BY status', | ||||
$resource->getTableName(), | $resource, | ||||
$blueprint->getPHID(), | $blueprint->getPHID(), | ||||
DrydockResourceStatus::STATUS_DESTROYED); | DrydockResourceStatus::STATUS_DESTROYED); | ||||
$counts = ipull($counts, 'N', 'status'); | $counts = ipull($counts, 'N', 'status'); | ||||
$n_alloc = idx($counts, DrydockResourceStatus::STATUS_PENDING, 0); | $n_alloc = idx($counts, DrydockResourceStatus::STATUS_PENDING, 0); | ||||
$n_active = idx($counts, DrydockResourceStatus::STATUS_ACTIVE, 0); | $n_active = idx($counts, DrydockResourceStatus::STATUS_ACTIVE, 0); | ||||
$n_broken = idx($counts, DrydockResourceStatus::STATUS_BROKEN, 0); | $n_broken = idx($counts, DrydockResourceStatus::STATUS_BROKEN, 0); | ||||
$n_released = idx($counts, DrydockResourceStatus::STATUS_RELEASED, 0); | $n_released = idx($counts, DrydockResourceStatus::STATUS_RELEASED, 0); | ||||
// If we're at the limit on total active resources, limit additional | // If we're at the limit on total active resources, limit additional | ||||
// allocations. | // allocations. | ||||
if ($total_limit !== null) { | |||||
$n_total = ($n_alloc + $n_active + $n_broken + $n_released); | $n_total = ($n_alloc + $n_active + $n_broken + $n_released); | ||||
if ($n_total >= $total_limit) { | if ($n_total >= $total_limit) { | ||||
return true; | return true; | ||||
} | } | ||||
} | |||||
// If the number of in-flight allocations is fewer than the minimum number | |||||
// of allowed allocations, don't impose a limit. | |||||
if ($n_alloc < $min_allowed) { | |||||
return false; | return false; | ||||
} | } | ||||
$allowed_alloc = (int)ceil($n_active * $growth_factor); | |||||
// If the number of in-flight allocation is fewer than the number of | |||||
// allowed allocations according to the pool growth factor, don't impose | |||||
// a limit. | |||||
if ($n_alloc < $allowed_alloc) { | |||||
return false; | |||||
} | |||||
return true; | |||||
} | |||||
} | } |