1
0
Fork 0
mirror of https://we.phorge.it/source/phorge.git synced 2024-12-23 14:00:56 +01:00

Parameterize PhabricatorGlobalLock

Summary:
Ref T13096. Currently, we do a fair amount of clever digesting and string manipulation to build lock names which are less than 64 characters long while still being reasonably readable.

Instead, do more of this automatically. This will let lock acquisition become simpler and make it more possible to build a useful lock log.

Test Plan: Ran `bin/repository update`, saw a reasonable lock acquire and release.

Maniphest Tasks: T13096

Differential Revision: https://secure.phabricator.com/D19173
This commit is contained in:
epriestley 2018-03-05 14:07:47 -08:00
parent f31975f7a3
commit fd367adbaf
2 changed files with 32 additions and 17 deletions

View file

@ -56,19 +56,18 @@ abstract class PhabricatorRepositoryEngine extends Phobject {
$lock_key,
$lock_device_only) {
$lock_parts = array();
$lock_parts[] = $lock_key;
$lock_parts[] = $repository->getID();
$lock_parts = array(
'repositoryPHID' => $repository->getPHID(),
);
if ($lock_device_only) {
$device = AlmanacKeys::getLiveDevice();
if ($device) {
$lock_parts[] = $device->getID();
$lock_parts['devicePHID'] = $device->getPHID();
}
}
$lock_name = implode(':', $lock_parts);
return PhabricatorGlobalLock::newLock($lock_name);
return PhabricatorGlobalLock::newLock($lock_key, $lock_parts);
}

View file

@ -28,6 +28,7 @@
*/
final class PhabricatorGlobalLock extends PhutilLock {
private $parameters;
private $conn;
private $isExternalConnection = false;
@ -37,27 +38,42 @@ final class PhabricatorGlobalLock extends PhutilLock {
/* -( Constructing Locks )------------------------------------------------- */
public static function newLock($name) {
public static function newLock($name, $parameters = array()) {
$namespace = PhabricatorLiskDAO::getStorageNamespace();
$namespace = PhabricatorHash::digestToLength($namespace, 20);
$full_name = 'ph:'.$namespace.':'.$name;
$length_limit = 64;
if (strlen($full_name) > $length_limit) {
$parts = array();
ksort($parameters);
foreach ($parameters as $key => $parameter) {
if (!preg_match('/^[a-zA-Z0-9]+\z/', $key)) {
throw new Exception(
pht(
'Lock name "%s" is too long (full lock name is "%s"). The '.
'full lock name must not be longer than %s bytes.',
$name,
$full_name,
new PhutilNumber($length_limit)));
'Lock parameter key "%s" must be alphanumeric.',
$key));
}
if (!is_scalar($parameter) && !is_null($parameter)) {
throw new Exception(
pht(
'Lock parameter for key "%s" must be a scalar.',
$key));
}
$value = phutil_json_encode($parameter);
$parts[] = "{$key}={$value}";
}
$parts = implode(', ', $parts);
$local = "{$name}({$parts})";
$local = PhabricatorHash::digestToLength($local, 20);
$full_name = "ph:{$namespace}:{$local}";
$lock = self::getLock($full_name);
if (!$lock) {
$lock = new PhabricatorGlobalLock($full_name);
self::registerLock($lock);
$lock->parameters = $parameters;
}
return $lock;