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

Recover from a race when importing external objects (like JIRA issues) for the first time

Summary:
Fixes T11604. If we send two requests to render a brand new tag at about the same time (say, 50ms apart) but JIRA takes more than 50ms to return from its API call, the two processes will race one another and try to save the same external object.

If they do, have whichever one lost the race just load the object the other one created.

Apply this to other bridges, too.

Test Plan:
  - Created a new task in JIRA.
  - Referenced it for the first time in Differential, in a comment.
  - This causes two tag renders to fire. This //might// be a bug but I spend 30 seconds on it without figuring out what was up. Regardless, we should fix the race even if the reason it's triggering so easily legitimately is a bug.
  - Before patch: big error dialog (as in T11604).
  - After patch: smooth sailing.

{F1804008}

Reviewers: chad

Reviewed By: chad

Maniphest Tasks: T11604

Differential Revision: https://secure.phabricator.com/D16514
This commit is contained in:
epriestley 2016-09-07 10:46:30 -07:00
parent e2430d0aa5
commit 138efb2b10
5 changed files with 32 additions and 16 deletions

View file

@ -48,4 +48,32 @@ abstract class DoorkeeperBridge extends Phobject {
return null;
}
final protected function saveExternalObject(
DoorkeeperObjectRef $ref,
DoorkeeperExternalObject $obj) {
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
try {
$obj->save();
} catch (AphrontDuplicateKeyQueryException $ex) {
// In various cases, we may race another process importing the same
// data. If we do, we'll collide on the object key. Load the object
// the other process created and use that.
$obj = id(new DoorkeeperExternalObjectQuery())
->setViewer($this->getViewer())
->withObjectKeys(array($ref->getObjectKey()))
->executeOne();
if (!$obj) {
throw new PhutilProxyException(
pht('Failed to load external object after collision.'),
$ex);
}
$ref->attachExternalObject($obj);
}
unset($unguarded);
}
}

View file

@ -113,10 +113,7 @@ final class DoorkeeperBridgeAsana extends DoorkeeperBridge {
}
$this->fillObjectFromData($obj, $result);
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
$obj->save();
unset($unguarded);
$this->saveExternalObject($ref, $obj);
}
}

View file

@ -78,10 +78,7 @@ final class DoorkeeperBridgeGitHubIssue
$obj = $ref->getExternalObject();
$this->fillObjectFromData($obj, $result);
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
$obj->save();
unset($unguarded);
$this->saveExternalObject($ref, $obj);
}
}

View file

@ -101,10 +101,7 @@ final class DoorkeeperBridgeGitHubUser
$obj = $ref->getExternalObject();
$this->fillObjectFromData($obj, $spec);
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
$obj->save();
unset($unguarded);
$this->saveExternalObject($ref, $obj);
}
}

View file

@ -104,10 +104,7 @@ final class DoorkeeperBridgeJIRA extends DoorkeeperBridge {
}
$this->fillObjectFromData($obj, $result);
$unguarded = AphrontWriteGuard::beginScopedUnguardedWrites();
$obj->save();
unset($unguarded);
$this->saveExternalObject($ref, $obj);
}
}