From ad53e7b878159eab1648962dc9cfd71474e321b3 Mon Sep 17 00:00:00 2001 From: epriestley Date: Sat, 24 Oct 2015 05:58:44 -0700 Subject: [PATCH] Record how long storage patches took to apply Summary: It's hard for us to predict how long patches and migrations will take in the general case since it varies a lot from install to install, but we can give installs some kind of rough heads up about longer patches. I'm planning to just put a sort of hint for things in the changelog, something like this: {F905579} To make this easier, start storing how long stuff took. I'll write a little script to dump this into a table for the changelog. Test Plan: Ran `bin/storage status`: {F905580} Reviewers: chad Reviewed By: chad Differential Revision: https://secure.phabricator.com/D14320 --- .../autopatches/20151023.patchduration.sql | 2 + .../PhabricatorStorageManagementAPI.php | 40 +++++++++++++++++-- ...ricatorStorageManagementStatusWorkflow.php | 11 +++++ ...icatorStorageManagementUpgradeWorkflow.php | 6 ++- .../schema/PhabricatorStorageSchemaSpec.php | 1 + 5 files changed, 55 insertions(+), 5 deletions(-) create mode 100644 resources/sql/autopatches/20151023.patchduration.sql diff --git a/resources/sql/autopatches/20151023.patchduration.sql b/resources/sql/autopatches/20151023.patchduration.sql new file mode 100644 index 0000000000..3e0c363931 --- /dev/null +++ b/resources/sql/autopatches/20151023.patchduration.sql @@ -0,0 +1,2 @@ +ALTER TABLE {$NAMESPACE}_meta_data.patch_status + ADD duration BIGINT UNSIGNED; diff --git a/src/infrastructure/storage/management/PhabricatorStorageManagementAPI.php b/src/infrastructure/storage/management/PhabricatorStorageManagementAPI.php index 0d8fc79bb0..76e8e0a057 100644 --- a/src/infrastructure/storage/management/PhabricatorStorageManagementAPI.php +++ b/src/infrastructure/storage/management/PhabricatorStorageManagementAPI.php @@ -17,6 +17,8 @@ final class PhabricatorStorageManagementAPI extends Phobject { const COLLATE_SORT = 'COLLATE_SORT'; const COLLATE_FULLTEXT = 'COLLATE_FULLTEXT'; + const TABLE_STATUS = 'patch_status'; + public function setDisableUTF8MB4($disable_utf8_mb4) { $this->disableUTF8MB4 = $disable_utf8_mb4; return $this; @@ -118,13 +120,26 @@ final class PhabricatorStorageManagementAPI extends Phobject { try { $applied = queryfx_all( $this->getConn('meta_data'), - 'SELECT patch FROM patch_status'); + 'SELECT patch FROM %T', + self::TABLE_STATUS); return ipull($applied, 'patch'); } catch (AphrontQueryException $ex) { return null; } } + public function getPatchDurations() { + try { + $rows = queryfx_all( + $this->getConn('meta_data'), + 'SELECT patch, duration FROM %T WHERE duration IS NOT NULL', + self::TABLE_STATUS); + return ipull($rows, 'duration', 'patch'); + } catch (AphrontQueryException $ex) { + return array(); + } + } + public function createDatabase($fragment) { $info = $this->getCharsetInfo(); @@ -168,13 +183,30 @@ final class PhabricatorStorageManagementAPI extends Phobject { return $legacy; } - public function markPatchApplied($patch) { + public function markPatchApplied($patch, $duration = null) { + $conn = $this->getConn('meta_data'); + queryfx( - $this->getConn('meta_data'), + $conn, 'INSERT INTO %T (patch, applied) VALUES (%s, %d)', - 'patch_status', + self::TABLE_STATUS, $patch, time()); + + // We didn't add this column for a long time, so it may not exist yet. + if ($duration !== null) { + try { + queryfx( + $conn, + 'UPDATE %T SET duration = %d WHERE patch = %s', + self::TABLE_STATUS, + (int)floor($duration * 1000000), + $patch); + } catch (AphrontQueryException $ex) { + // Just ignore this, as it almost certainly indicates that we just + // don't have the column yet. + } + } } public function applyPatch(PhabricatorStoragePatch $patch) { diff --git a/src/infrastructure/storage/management/workflow/PhabricatorStorageManagementStatusWorkflow.php b/src/infrastructure/storage/management/workflow/PhabricatorStorageManagementStatusWorkflow.php index ebff080bd4..774ae8445c 100644 --- a/src/infrastructure/storage/management/workflow/PhabricatorStorageManagementStatusWorkflow.php +++ b/src/infrastructure/storage/management/workflow/PhabricatorStorageManagementStatusWorkflow.php @@ -29,15 +29,26 @@ final class PhabricatorStorageManagementStatusWorkflow ->setShowHeader(false) ->addColumn('id', array('title' => pht('ID'))) ->addColumn('status', array('title' => pht('Status'))) + ->addColumn('duration', array('title' => pht('Duration'))) ->addColumn('type', array('title' => pht('Type'))) ->addColumn('name', array('title' => pht('Name'))); + $durations = $api->getPatchDurations(); + foreach ($patches as $patch) { + $duration = idx($durations, $patch->getFullKey()); + if ($duration === null) { + $duration = '-'; + } else { + $duration = pht('%s us', new PhutilNumber($duration)); + } + $table->addRow(array( 'id' => $patch->getFullKey(), 'status' => in_array($patch->getFullKey(), $applied) ? pht('Applied') : pht('Not Applied'), + 'duration' => $duration, 'type' => $patch->getType(), 'name' => $patch->getName(), )); diff --git a/src/infrastructure/storage/management/workflow/PhabricatorStorageManagementUpgradeWorkflow.php b/src/infrastructure/storage/management/workflow/PhabricatorStorageManagementUpgradeWorkflow.php index 55cf3b223e..d5a4f41cad 100644 --- a/src/infrastructure/storage/management/workflow/PhabricatorStorageManagementUpgradeWorkflow.php +++ b/src/infrastructure/storage/management/workflow/PhabricatorStorageManagementUpgradeWorkflow.php @@ -187,9 +187,13 @@ final class PhabricatorStorageManagementUpgradeWorkflow echo pht("DRYRUN: Would apply patch '%s'.", $key)."\n"; } else { echo pht("Applying patch '%s'...", $key)."\n"; + + $t_begin = microtime(true); $api->applyPatch($patch); + $t_end = microtime(true); + if (!$skip_mark) { - $api->markPatchApplied($key); + $api->markPatchApplied($key, ($t_end - $t_begin)); } } diff --git a/src/infrastructure/storage/schema/PhabricatorStorageSchemaSpec.php b/src/infrastructure/storage/schema/PhabricatorStorageSchemaSpec.php index 886df14e2c..df48ce3812 100644 --- a/src/infrastructure/storage/schema/PhabricatorStorageSchemaSpec.php +++ b/src/infrastructure/storage/schema/PhabricatorStorageSchemaSpec.php @@ -10,6 +10,7 @@ final class PhabricatorStorageSchemaSpec array( 'patch' => 'text128', 'applied' => 'uint32', + 'duration' => 'uint64?', ), array( 'PRIMARY' => array(