1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-22 13:30:55 +01:00

Strip some obsolete code out of Drydock

Summary:
Ref T9252. This simplifies some Drydock code.

Most of this code relates to the old notion of Drydock being able to enumerate all the tasks it needs to complete in order to acquire a lease. The code has stepped back from this, since it's unnecessary, the queue is more powerful than it used to be, and it would be a lot of work to keep track of.

The ~only thing that should ever wait for leases in modern code is `bin/drydock lease`, and it's fine for it to just sit there sleeping, so this just does that.

This reduces the granularity of logging, but I'll address that separately in future logging-focused changes.

Test Plan: Used `bin/drydock lease` to acquire a lease, saw it acquire cleanly.

Reviewers: hach-que, chad

Reviewed By: chad

Maniphest Tasks: T9252

Differential Revision: https://secure.phabricator.com/D14147
This commit is contained in:
epriestley 2015-09-23 13:21:41 -07:00
parent be9cc235b2
commit fcb6d1e2fa
6 changed files with 34 additions and 160 deletions

View file

@ -0,0 +1,2 @@
ALTER TABLE {$NAMESPACE}_drydock.drydock_lease
DROP taskID;

View file

@ -812,7 +812,6 @@ phutil_register_library_map(array(
'DrydockBlueprintListController' => 'applications/drydock/controller/DrydockBlueprintListController.php', 'DrydockBlueprintListController' => 'applications/drydock/controller/DrydockBlueprintListController.php',
'DrydockBlueprintPHIDType' => 'applications/drydock/phid/DrydockBlueprintPHIDType.php', 'DrydockBlueprintPHIDType' => 'applications/drydock/phid/DrydockBlueprintPHIDType.php',
'DrydockBlueprintQuery' => 'applications/drydock/query/DrydockBlueprintQuery.php', 'DrydockBlueprintQuery' => 'applications/drydock/query/DrydockBlueprintQuery.php',
'DrydockBlueprintScopeGuard' => 'applications/drydock/util/DrydockBlueprintScopeGuard.php',
'DrydockBlueprintSearchEngine' => 'applications/drydock/query/DrydockBlueprintSearchEngine.php', 'DrydockBlueprintSearchEngine' => 'applications/drydock/query/DrydockBlueprintSearchEngine.php',
'DrydockBlueprintTransaction' => 'applications/drydock/storage/DrydockBlueprintTransaction.php', 'DrydockBlueprintTransaction' => 'applications/drydock/storage/DrydockBlueprintTransaction.php',
'DrydockBlueprintTransactionQuery' => 'applications/drydock/query/DrydockBlueprintTransactionQuery.php', 'DrydockBlueprintTransactionQuery' => 'applications/drydock/query/DrydockBlueprintTransactionQuery.php',
@ -4538,7 +4537,6 @@ phutil_register_library_map(array(
'DrydockBlueprintListController' => 'DrydockBlueprintController', 'DrydockBlueprintListController' => 'DrydockBlueprintController',
'DrydockBlueprintPHIDType' => 'PhabricatorPHIDType', 'DrydockBlueprintPHIDType' => 'PhabricatorPHIDType',
'DrydockBlueprintQuery' => 'DrydockQuery', 'DrydockBlueprintQuery' => 'DrydockQuery',
'DrydockBlueprintScopeGuard' => 'Phobject',
'DrydockBlueprintSearchEngine' => 'PhabricatorApplicationSearchEngine', 'DrydockBlueprintSearchEngine' => 'PhabricatorApplicationSearchEngine',
'DrydockBlueprintTransaction' => 'PhabricatorApplicationTransaction', 'DrydockBlueprintTransaction' => 'PhabricatorApplicationTransaction',
'DrydockBlueprintTransactionQuery' => 'PhabricatorApplicationTransactionQuery', 'DrydockBlueprintTransactionQuery' => 'PhabricatorApplicationTransactionQuery',

View file

@ -8,9 +8,6 @@
*/ */
abstract class DrydockBlueprintImplementation extends Phobject { abstract class DrydockBlueprintImplementation extends Phobject {
private $activeResource;
private $activeLease;
abstract public function getType(); abstract public function getType();
abstract public function isEnabled(); abstract public function isEnabled();
@ -265,10 +262,7 @@ abstract class DrydockBlueprintImplementation extends Phobject {
* @task log * @task log
*/ */
protected function log($message) { protected function log($message) {
self::writeLog( self::writeLog(null, null, $message);
$this->activeResource,
$this->activeLease,
$message);
} }
@ -320,13 +314,6 @@ abstract class DrydockBlueprintImplementation extends Phobject {
// Pre-allocate the resource PHID. // Pre-allocate the resource PHID.
$resource->setPHID($resource->generatePHID()); $resource->setPHID($resource->generatePHID());
$this->activeResource = $resource;
$this->log(
pht(
"Blueprint '%s': Created New Template",
get_class($this)));
return $resource; return $resource;
} }
@ -349,24 +336,4 @@ abstract class DrydockBlueprintImplementation extends Phobject {
} }
} }
private function pushActiveScope(
DrydockResource $resource = null,
DrydockLease $lease = null) {
if (($this->activeResource !== null) ||
($this->activeLease !== null)) {
throw new Exception(pht('There is already an active resource or lease!'));
}
$this->activeResource = $resource;
$this->activeLease = $lease;
return new DrydockBlueprintScopeGuard($this);
}
public function popActiveScope() {
$this->activeResource = null;
$this->activeLease = null;
}
} }

View file

@ -9,7 +9,6 @@ final class DrydockLease extends DrydockDAO
protected $ownerPHID; protected $ownerPHID;
protected $attributes = array(); protected $attributes = array();
protected $status = DrydockLeaseStatus::STATUS_PENDING; protected $status = DrydockLeaseStatus::STATUS_PENDING;
protected $taskID;
private $resource = self::ATTACHABLE; private $resource = self::ATTACHABLE;
private $releaseOnDestruction; private $releaseOnDestruction;
@ -64,7 +63,6 @@ final class DrydockLease extends DrydockDAO
'status' => 'uint32', 'status' => 'uint32',
'until' => 'epoch?', 'until' => 'epoch?',
'resourceType' => 'text128', 'resourceType' => 'text128',
'taskID' => 'id?',
'ownerPHID' => 'phid?', 'ownerPHID' => 'phid?',
'resourceID' => 'id?', 'resourceID' => 'id?',
), ),
@ -108,20 +106,15 @@ final class DrydockLease extends DrydockDAO
return ($this->resource !== null); return ($this->resource !== null);
} }
public function loadResource() {
return id(new DrydockResource())->loadOneWhere(
'id = %d',
$this->getResourceID());
}
public function queueForActivation() { public function queueForActivation() {
if ($this->getID()) { if ($this->getID()) {
throw new Exception( throw new Exception(
pht('Only new leases may be queued for activation!')); pht('Only new leases may be queued for activation!'));
} }
$this->setStatus(DrydockLeaseStatus::STATUS_PENDING); $this
$this->save(); ->setStatus(DrydockLeaseStatus::STATUS_PENDING)
->save();
$task = PhabricatorWorker::scheduleTask( $task = PhabricatorWorker::scheduleTask(
'DrydockAllocatorWorker', 'DrydockAllocatorWorker',
@ -132,14 +125,6 @@ final class DrydockLease extends DrydockDAO
'objectPHID' => $this->getPHID(), 'objectPHID' => $this->getPHID(),
)); ));
// NOTE: Scheduling the task might execute it in-process, if we're running
// from a CLI script. Reload the lease to make sure we have the most
// up-to-date information. Normally, this has no effect.
$this->reload();
$this->setTaskID($task->getID());
$this->save();
return $this; return $this;
} }
@ -161,21 +146,18 @@ final class DrydockLease extends DrydockDAO
} }
} }
public static function waitForLeases(array $leases) { public function waitUntilActive() {
assert_instances_of($leases, __CLASS__);
$task_ids = array_filter(mpull($leases, 'getTaskID'));
PhabricatorWorker::waitForTasks($task_ids);
$unresolved = $leases;
while (true) { while (true) {
foreach ($unresolved as $key => $lease) { $lease = $this->reload();
$lease->reload(); if (!$lease) {
switch ($lease->getStatus()) { throw new Exception(pht('Failed to reload lease.'));
}
$status = $lease->getStatus();
switch ($status) {
case DrydockLeaseStatus::STATUS_ACTIVE: case DrydockLeaseStatus::STATUS_ACTIVE:
unset($unresolved[$key]); return;
break;
case DrydockLeaseStatus::STATUS_RELEASED: case DrydockLeaseStatus::STATUS_RELEASED:
throw new Exception(pht('Lease has already been released!')); throw new Exception(pht('Lease has already been released!'));
case DrydockLeaseStatus::STATUS_EXPIRED: case DrydockLeaseStatus::STATUS_EXPIRED:
@ -186,31 +168,16 @@ final class DrydockLease extends DrydockDAO
case DrydockLeaseStatus::STATUS_ACQUIRED: case DrydockLeaseStatus::STATUS_ACQUIRED:
break; break;
default: default:
throw new Exception(pht('Unknown status??')); throw new Exception(
} pht(
'Lease has unknown status "%s".',
$status));
} }
if ($unresolved) {
sleep(1); sleep(1);
} else {
break;
} }
} }
foreach ($leases as $lease) {
$lease->attachResource($lease->loadResource());
}
}
public function waitUntilActive() {
if (!$this->getID()) {
$this->queueForActivation();
}
self::waitForLeases(array($this));
return $this;
}
public function setActivateWhenAcquired($activate) { public function setActivateWhenAcquired($activate) {
$this->activateWhenAcquired = true; $this->activateWhenAcquired = true;
return $this; return $this;

View file

@ -1,15 +0,0 @@
<?php
final class DrydockBlueprintScopeGuard extends Phobject {
private $blueprint;
public function __construct(DrydockBlueprintImplementation $blueprint) {
$this->blueprint = $blueprint;
}
public function __destruct() {
$this->blueprint->popActiveScope();
}
}

View file

@ -157,51 +157,6 @@ abstract class PhabricatorWorker extends Phobject {
} }
/**
* Wait for tasks to complete.
*
* @param list<int> List of queued task IDs to wait for.
* @return void
*/
final public static function waitForTasks(array $task_ids) {
if (!$task_ids) {
return;
}
$task_table = new PhabricatorWorkerActiveTask();
$waiting = array_fuse($task_ids);
while ($waiting) {
$conn_w = $task_table->establishConnection('w');
// Check if any of the tasks we're waiting on are still queued. If they
// are not, we're done waiting.
$row = queryfx_one(
$conn_w,
'SELECT COUNT(*) N FROM %T WHERE id IN (%Ld)',
$task_table->getTableName(),
$waiting);
if (!$row['N']) {
// Nothing is queued anymore. Stop waiting.
break;
}
// We were not successful in leasing anything. Sleep for a bit and
// see if we have better luck later.
sleep(1);
}
$tasks = id(new PhabricatorWorkerArchiveTaskQuery())
->withIDs($task_ids)
->execute();
foreach ($tasks as $task) {
if ($task->getResult() != PhabricatorWorkerArchiveTask::RESULT_SUCCESS) {
throw new Exception(pht('Task %d failed!', $task->getID()));
}
}
}
public function renderForDisplay(PhabricatorUser $viewer) { public function renderForDisplay(PhabricatorUser $viewer) {
return null; return null;
} }