mirror of
https://we.phorge.it/source/phorge.git
synced 2024-11-17 20:32:41 +01:00
Allow index extensions to skip indexing if the object has not changed
Summary: Fixes T9890. This allows IndexExtensions to emit an object version. Before we build indexes, we check if the indexed version is the same as the current version. If it is, we just don't call that extension. T9890 has a case where this is useful: a script went crazy and posted thousands of comments to a single task. Without versioning, that results in the same comments being indexed over and over again. With versioning, most of the queue could just exit without doing any work. Test Plan: - Added a `sleep(1)` to the actual indexing, used `bin/search index --background` to queue up a lot of tasks, ran them with `bin/phd debug task`, saw them complete very quickly with only one actual index operation performed. - Used `bin/search index --trace` and `bin/search index --trace --background` to observe the behavior of queries against the index version store, which looked sensible. - Made comments/transactions, saw versions update. - Used `bin/remove destroy`, verified index versions were purged. Reviewers: chad Reviewed By: chad Maniphest Tasks: T9890 Differential Revision: https://secure.phabricator.com/D14845
This commit is contained in:
parent
23c42486e4
commit
a761f73384
6 changed files with 175 additions and 3 deletions
7
resources/sql/autopatches/20151221.search.1.version.sql
Normal file
7
resources/sql/autopatches/20151221.search.1.version.sql
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
CREATE TABLE {$NAMESPACE}_search.search_indexversion (
|
||||||
|
id INT UNSIGNED NOT NULL PRIMARY KEY AUTO_INCREMENT,
|
||||||
|
objectPHID VARBINARY(64) NOT NULL,
|
||||||
|
extensionKey VARCHAR(64) NOT NULL COLLATE {$COLLATE_TEXT},
|
||||||
|
version VARCHAR(128) NOT NULL COLLATE {$COLLATE_TEXT},
|
||||||
|
UNIQUE KEY `key_object` (objectPHID, extensionKey)
|
||||||
|
) ENGINE=InnoDB, COLLATE {$COLLATE_TEXT};
|
|
@ -3042,6 +3042,8 @@ phutil_register_library_map(array(
|
||||||
'PhabricatorSearchEngineTestCase' => 'applications/search/engine/__tests__/PhabricatorSearchEngineTestCase.php',
|
'PhabricatorSearchEngineTestCase' => 'applications/search/engine/__tests__/PhabricatorSearchEngineTestCase.php',
|
||||||
'PhabricatorSearchField' => 'applications/search/field/PhabricatorSearchField.php',
|
'PhabricatorSearchField' => 'applications/search/field/PhabricatorSearchField.php',
|
||||||
'PhabricatorSearchHovercardController' => 'applications/search/controller/PhabricatorSearchHovercardController.php',
|
'PhabricatorSearchHovercardController' => 'applications/search/controller/PhabricatorSearchHovercardController.php',
|
||||||
|
'PhabricatorSearchIndexVersion' => 'applications/search/storage/PhabricatorSearchIndexVersion.php',
|
||||||
|
'PhabricatorSearchIndexVersionDestructionEngineExtension' => 'applications/search/engineextension/PhabricatorSearchIndexVersionDestructionEngineExtension.php',
|
||||||
'PhabricatorSearchManagementIndexWorkflow' => 'applications/search/management/PhabricatorSearchManagementIndexWorkflow.php',
|
'PhabricatorSearchManagementIndexWorkflow' => 'applications/search/management/PhabricatorSearchManagementIndexWorkflow.php',
|
||||||
'PhabricatorSearchManagementInitWorkflow' => 'applications/search/management/PhabricatorSearchManagementInitWorkflow.php',
|
'PhabricatorSearchManagementInitWorkflow' => 'applications/search/management/PhabricatorSearchManagementInitWorkflow.php',
|
||||||
'PhabricatorSearchManagementWorkflow' => 'applications/search/management/PhabricatorSearchManagementWorkflow.php',
|
'PhabricatorSearchManagementWorkflow' => 'applications/search/management/PhabricatorSearchManagementWorkflow.php',
|
||||||
|
@ -7407,6 +7409,8 @@ phutil_register_library_map(array(
|
||||||
'PhabricatorSearchEngineTestCase' => 'PhabricatorTestCase',
|
'PhabricatorSearchEngineTestCase' => 'PhabricatorTestCase',
|
||||||
'PhabricatorSearchField' => 'Phobject',
|
'PhabricatorSearchField' => 'Phobject',
|
||||||
'PhabricatorSearchHovercardController' => 'PhabricatorSearchBaseController',
|
'PhabricatorSearchHovercardController' => 'PhabricatorSearchBaseController',
|
||||||
|
'PhabricatorSearchIndexVersion' => 'PhabricatorSearchDAO',
|
||||||
|
'PhabricatorSearchIndexVersionDestructionEngineExtension' => 'PhabricatorDestructionEngineExtension',
|
||||||
'PhabricatorSearchManagementIndexWorkflow' => 'PhabricatorSearchManagementWorkflow',
|
'PhabricatorSearchManagementIndexWorkflow' => 'PhabricatorSearchManagementWorkflow',
|
||||||
'PhabricatorSearchManagementInitWorkflow' => 'PhabricatorSearchManagementWorkflow',
|
'PhabricatorSearchManagementInitWorkflow' => 'PhabricatorSearchManagementWorkflow',
|
||||||
'PhabricatorSearchManagementWorkflow' => 'PhabricatorManagementWorkflow',
|
'PhabricatorSearchManagementWorkflow' => 'PhabricatorManagementWorkflow',
|
||||||
|
|
|
@ -9,6 +9,23 @@ final class PhabricatorFulltextIndexEngineExtension
|
||||||
return pht('Fulltext Engine');
|
return pht('Fulltext Engine');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function getIndexVersion($object) {
|
||||||
|
$version = array();
|
||||||
|
|
||||||
|
if ($object instanceof PhabricatorApplicationTransactionInterface) {
|
||||||
|
// If this is a normal object with transactions, we only need to
|
||||||
|
// reindex it if there are new transactions (or comment edits).
|
||||||
|
$version[] = $this->getTransactionVersion($object);
|
||||||
|
$version[] = $this->getCommentVersion($object);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$version) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return implode(':', $version);
|
||||||
|
}
|
||||||
|
|
||||||
public function shouldIndexObject($object) {
|
public function shouldIndexObject($object) {
|
||||||
return ($object instanceof PhabricatorFulltextInterface);
|
return ($object instanceof PhabricatorFulltextInterface);
|
||||||
}
|
}
|
||||||
|
@ -27,4 +44,46 @@ final class PhabricatorFulltextIndexEngineExtension
|
||||||
$engine->buildFulltextIndexes();
|
$engine->buildFulltextIndexes();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function getTransactionVersion($object) {
|
||||||
|
$xaction = $object->getApplicationTransactionTemplate();
|
||||||
|
|
||||||
|
$xaction_row = queryfx_one(
|
||||||
|
$xaction->establishConnection('r'),
|
||||||
|
'SELECT id FROM %T WHERE objectPHID = %s
|
||||||
|
ORDER BY id DESC LIMIT 1',
|
||||||
|
$xaction->getTableName(),
|
||||||
|
$object->getPHID());
|
||||||
|
if (!$xaction_row) {
|
||||||
|
return 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $xaction_row['id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getCommentVersion($object) {
|
||||||
|
$xaction = $object->getApplicationTransactionTemplate();
|
||||||
|
|
||||||
|
try {
|
||||||
|
$comment = $xaction->getApplicationTransactionCommentObject();
|
||||||
|
} catch (Exception $ex) {
|
||||||
|
return 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
$comment_row = queryfx_one(
|
||||||
|
$comment->establishConnection('r'),
|
||||||
|
'SELECT c.id FROM %T x JOIN %T c
|
||||||
|
ON x.phid = c.transactionPHID
|
||||||
|
WHERE x.objectPHID = %s
|
||||||
|
ORDER BY c.id DESC LIMIT 1',
|
||||||
|
$xaction->getTableName(),
|
||||||
|
$comment->getTableName(),
|
||||||
|
$object->getPHID());
|
||||||
|
if (!$comment_row) {
|
||||||
|
return 'none';
|
||||||
|
}
|
||||||
|
|
||||||
|
return $comment_row['id'];
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,25 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
final class PhabricatorSearchIndexVersionDestructionEngineExtension
|
||||||
|
extends PhabricatorDestructionEngineExtension {
|
||||||
|
|
||||||
|
const EXTENSIONKEY = 'search.index.version';
|
||||||
|
|
||||||
|
public function getExtensionName() {
|
||||||
|
return pht('Search Index Versions');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function destroyObject(
|
||||||
|
PhabricatorDestructionEngine $engine,
|
||||||
|
$object) {
|
||||||
|
|
||||||
|
$table = new PhabricatorSearchIndexVersion();
|
||||||
|
|
||||||
|
queryfx(
|
||||||
|
$table->establishConnection('w'),
|
||||||
|
'DELETE FROM %T WHERE objectPHID = %s',
|
||||||
|
$table->getTableName(),
|
||||||
|
$object->getPHID());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -45,8 +45,8 @@ final class PhabricatorIndexEngine extends Phobject {
|
||||||
if (idx($parameters, 'force')) {
|
if (idx($parameters, 'force')) {
|
||||||
$current_versions = array();
|
$current_versions = array();
|
||||||
} else {
|
} else {
|
||||||
// TODO: Load current indexed versions.
|
$keys = array_keys($versions);
|
||||||
$current_versions = array();
|
$current_versions = $this->loadIndexVersions($keys);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($versions as $key => $version) {
|
foreach ($versions as $key => $version) {
|
||||||
|
@ -78,7 +78,7 @@ final class PhabricatorIndexEngine extends Phobject {
|
||||||
$extension->indexObject($this, $object);
|
$extension->indexObject($this, $object);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Save new index versions.
|
$this->saveIndexVersions($this->versions);
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
}
|
}
|
||||||
|
@ -96,4 +96,55 @@ final class PhabricatorIndexEngine extends Phobject {
|
||||||
return $extensions;
|
return $extensions;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private function loadIndexVersions(array $extension_keys) {
|
||||||
|
if (!$extension_keys) {
|
||||||
|
return array();
|
||||||
|
}
|
||||||
|
|
||||||
|
$object = $this->getObject();
|
||||||
|
$object_phid = $object->getPHID();
|
||||||
|
|
||||||
|
$table = new PhabricatorSearchIndexVersion();
|
||||||
|
$conn_r = $table->establishConnection('w');
|
||||||
|
|
||||||
|
$rows = queryfx_all(
|
||||||
|
$conn_r,
|
||||||
|
'SELECT * FROM %T WHERE objectPHID = %s AND extensionKey IN (%Ls)',
|
||||||
|
$table->getTableName(),
|
||||||
|
$object_phid,
|
||||||
|
$extension_keys);
|
||||||
|
|
||||||
|
return ipull($rows, 'version', 'extensionKey');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function saveIndexVersions(array $versions) {
|
||||||
|
if (!$versions) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$object = $this->getObject();
|
||||||
|
$object_phid = $object->getPHID();
|
||||||
|
|
||||||
|
$table = new PhabricatorSearchIndexVersion();
|
||||||
|
$conn_w = $table->establishConnection('w');
|
||||||
|
|
||||||
|
$sql = array();
|
||||||
|
foreach ($versions as $key => $version) {
|
||||||
|
$sql[] = qsprintf(
|
||||||
|
$conn_w,
|
||||||
|
'(%s, %s, %s)',
|
||||||
|
$object_phid,
|
||||||
|
$key,
|
||||||
|
$version);
|
||||||
|
}
|
||||||
|
|
||||||
|
queryfx(
|
||||||
|
$conn_w,
|
||||||
|
'INSERT INTO %T (objectPHID, extensionKey, version)
|
||||||
|
VALUES %Q
|
||||||
|
ON DUPLICATE KEY UPDATE version = VALUES(version)',
|
||||||
|
$table->getTableName(),
|
||||||
|
implode(', ', $sql));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,26 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
final class PhabricatorSearchIndexVersion
|
||||||
|
extends PhabricatorSearchDAO {
|
||||||
|
|
||||||
|
protected $objectPHID;
|
||||||
|
protected $extensionKey;
|
||||||
|
protected $version;
|
||||||
|
|
||||||
|
protected function getConfiguration() {
|
||||||
|
return array(
|
||||||
|
self::CONFIG_TIMESTAMPS => false,
|
||||||
|
self::CONFIG_COLUMN_SCHEMA => array(
|
||||||
|
'extensionKey' => 'text64',
|
||||||
|
'version' => 'text128',
|
||||||
|
),
|
||||||
|
self::CONFIG_KEY_SCHEMA => array(
|
||||||
|
'key_object' => array(
|
||||||
|
'columns' => array('objectPHID', 'extensionKey'),
|
||||||
|
'unique' => true,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
) + parent::getConfiguration();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in a new issue