diff --git a/src/applications/config/check/PhabricatorMySQLSetupCheck.php b/src/applications/config/check/PhabricatorMySQLSetupCheck.php index 1990e7f09f..580ef9db67 100644 --- a/src/applications/config/check/PhabricatorMySQLSetupCheck.php +++ b/src/applications/config/check/PhabricatorMySQLSetupCheck.php @@ -1,377 +1,402 @@ establishConnection('w'); - - try { - $value = queryfx_one($conn_raw, 'SELECT @@%Q', $key); - $value = $value['@@'.$key]; - } catch (AphrontQueryException $ex) { - $value = null; + protected function executeChecks() { + $refs = PhabricatorDatabaseRef::getActiveDatabaseRefs(); + foreach ($refs as $ref) { + $this->executeRefChecks($ref); } - - return $value; } - protected function executeChecks() { - // TODO: These checks should be executed against every reachable replica? - // See T10759. - if (PhabricatorEnv::isReadOnly()) { - return; - } + private function executeRefChecks(PhabricatorDatabaseRef $ref) { + $max_allowed_packet = $ref->loadRawMySQLConfigValue('max_allowed_packet'); - $max_allowed_packet = self::loadRawConfigValue('max_allowed_packet'); + $host_name = $ref->getRefKey(); // This primarily supports setting the filesize limit for MySQL to 8MB, // which may produce a >16MB packet after escaping. $recommended_minimum = (32 * 1024 * 1024); if ($max_allowed_packet < $recommended_minimum) { $message = pht( - "MySQL is configured with a small '%s' (%d), ". - "which may cause some large writes to fail.", + 'On host "%s", MySQL is configured with a small "%s" (%d), which '. + 'may cause some large writes to fail. The recommended minimum value '. + 'for this setting is "%d".', + $host_name, 'max_allowed_packet', - $max_allowed_packet); + $max_allowed_packet, + $recommended_minimum); $this->newIssue('mysql.max_allowed_packet') ->setName(pht('Small MySQL "%s"', 'max_allowed_packet')) ->setMessage($message) + ->setDatabaseRef($ref) ->addMySQLConfig('max_allowed_packet'); } - $modes = self::loadRawConfigValue('sql_mode'); + $modes = $ref->loadRawMySQLConfigValue('sql_mode'); $modes = explode(',', $modes); if (!in_array('STRICT_ALL_TABLES', $modes)) { $summary = pht( - 'MySQL is not in strict mode, but using strict mode is strongly '. - 'encouraged.'); + 'MySQL is not in strict mode (on host "%s"), but using strict mode '. + 'is strongly encouraged.', + $host_name); $message = pht( - "On your MySQL instance, the global %s is not set to %s. ". + "On database host \"%s\", the global %s is not set to %s. ". "It is strongly encouraged that you enable this mode when running ". "Phabricator.\n\n". "By default MySQL will silently ignore some types of errors, which ". "can cause data loss and raise security concerns. Enabling strict ". "mode makes MySQL raise an explicit error instead, and prevents this ". "entire class of problems from doing any damage.\n\n". "You can find more information about this mode (and how to configure ". "it) in the MySQL manual. Usually, it is sufficient to add this to ". "your %s file (in the %s section) and then restart %s:\n\n". "%s\n". "(Note that if you run other applications against the same database, ". "they may not work in strict mode. Be careful about enabling it in ". "these cases.)", + $host_name, phutil_tag('tt', array(), 'sql_mode'), phutil_tag('tt', array(), 'STRICT_ALL_TABLES'), phutil_tag('tt', array(), 'my.cnf'), phutil_tag('tt', array(), '[mysqld]'), phutil_tag('tt', array(), 'mysqld'), phutil_tag('pre', array(), 'sql_mode=STRICT_ALL_TABLES')); $this->newIssue('mysql.mode') ->setName(pht('MySQL %s Mode Not Set', 'STRICT_ALL_TABLES')) ->setSummary($summary) ->setMessage($message) + ->setDatabaseRef($ref) ->addMySQLConfig('sql_mode'); } + if (in_array('ONLY_FULL_GROUP_BY', $modes)) { $summary = pht( - 'MySQL is in ONLY_FULL_GROUP_BY mode, but using this mode is strongly '. - 'discouraged.'); + 'MySQL is in ONLY_FULL_GROUP_BY mode (on host "%s"), but using this '. + 'mode is strongly discouraged.', + $host_name); $message = pht( - "On your MySQL instance, the global %s is set to %s. ". + "On database host \"%s\", the global %s is set to %s. ". "It is strongly encouraged that you disable this mode when running ". "Phabricator.\n\n". "With %s enabled, MySQL rejects queries for which the select list ". "or (as of MySQL 5.0.23) %s list refer to nonaggregated columns ". "that are not named in the %s clause. More importantly, Phabricator ". "does not work properly with this mode enabled.\n\n". "You can find more information about this mode (and how to configure ". "it) in the MySQL manual. Usually, it is sufficient to change the %s ". "in your %s file (in the %s section) and then restart %s:\n\n". "%s\n". "(Note that if you run other applications against the same database, ". "they may not work with %s. Be careful about enabling ". "it in these cases and consider migrating Phabricator to a different ". "database.)", + $host_name, phutil_tag('tt', array(), 'sql_mode'), phutil_tag('tt', array(), 'ONLY_FULL_GROUP_BY'), phutil_tag('tt', array(), 'ONLY_FULL_GROUP_BY'), phutil_tag('tt', array(), 'HAVING'), phutil_tag('tt', array(), 'GROUP BY'), phutil_tag('tt', array(), 'sql_mode'), phutil_tag('tt', array(), 'my.cnf'), phutil_tag('tt', array(), '[mysqld]'), phutil_tag('tt', array(), 'mysqld'), phutil_tag('pre', array(), 'sql_mode=STRICT_ALL_TABLES'), phutil_tag('tt', array(), 'ONLY_FULL_GROUP_BY')); $this->newIssue('mysql.mode') ->setName(pht('MySQL %s Mode Set', 'ONLY_FULL_GROUP_BY')) ->setSummary($summary) ->setMessage($message) + ->setDatabaseRef($ref) ->addMySQLConfig('sql_mode'); } - $stopword_file = self::loadRawConfigValue('ft_stopword_file'); - + $stopword_file = $ref->loadRawMySQLConfigValue('ft_stopword_file'); if ($this->shouldUseMySQLSearchEngine()) { if ($stopword_file === null) { $summary = pht( - 'Your version of MySQL does not support configuration of a '. - 'stopword file. You will not be able to find search results for '. - 'common words.'); + 'Your version of MySQL (on database host "%s") does not support '. + 'configuration of a stopword file. You will not be able to find '. + 'search results for common words.', + $host_name); $message = pht( - "Your MySQL instance does not support the %s option. You will not ". + "Database host \"%s\" does not support the %s option. You will not ". "be able to find search results for common words. You can gain ". "access to this option by upgrading MySQL to a more recent ". "version.\n\n". "You can ignore this warning if you plan to configure ElasticSearch ". "later, or aren't concerned about searching for common words.", + $host_name, phutil_tag('tt', array(), 'ft_stopword_file')); $this->newIssue('mysql.ft_stopword_file') ->setName(pht('MySQL %s Not Supported', 'ft_stopword_file')) ->setSummary($summary) ->setMessage($message) + ->setDatabaseRef($ref) ->addMySQLConfig('ft_stopword_file'); } else if ($stopword_file == '(built-in)') { $root = dirname(phutil_get_library_root('phabricator')); $stopword_path = $root.'/resources/sql/stopwords.txt'; $stopword_path = Filesystem::resolvePath($stopword_path); $namespace = PhabricatorEnv::getEnvConfig('storage.default-namespace'); $summary = pht( - 'MySQL is using a default stopword file, which will prevent '. - 'searching for many common words.'); + 'MySQL (on host "%s") is using a default stopword file, which '. + 'will prevent searching for many common words.', + $host_name); $message = pht( - "Your MySQL instance is using the builtin stopword file for ". + "Database host \"%s\" is using the builtin stopword file for ". "building search indexes. This can make Phabricator's search ". "feature less useful.\n\n". "Stopwords are common words which are not indexed and thus can not ". "be searched for. The default stopword file has about 500 words, ". "including various words which you are likely to wish to search ". "for, such as 'various', 'likely', 'wish', and 'zero'.\n\n". "To make search more useful, you can use an alternate stopword ". "file with fewer words. Alternatively, if you aren't concerned ". "about searching for common words, you can ignore this warning. ". "If you later plan to configure ElasticSearch, you can also ignore ". "this warning: this stopword file only affects MySQL fulltext ". "indexes.\n\n". "To choose a different stopword file, add this to your %s file ". "(in the %s section) and then restart %s:\n\n". "%s\n". "(You can also use a different file if you prefer. The file ". "suggested above has about 50 of the most common English words.)\n\n". "Finally, run this command to rebuild indexes using the new ". "rules:\n\n". "%s", + $host_name, phutil_tag('tt', array(), 'my.cnf'), phutil_tag('tt', array(), '[mysqld]'), phutil_tag('tt', array(), 'mysqld'), phutil_tag('pre', array(), 'ft_stopword_file='.$stopword_path), phutil_tag( 'pre', array(), "mysql> REPAIR TABLE {$namespace}_search.search_documentfield;")); $this->newIssue('mysql.ft_stopword_file') ->setName(pht('MySQL is Using Default Stopword File')) ->setSummary($summary) ->setMessage($message) + ->setDatabaseRef($ref) ->addMySQLConfig('ft_stopword_file'); } } - $min_len = self::loadRawConfigValue('ft_min_word_len'); + $min_len = $ref->loadRawMySQLConfigValue('ft_min_word_len'); if ($min_len >= 4) { if ($this->shouldUseMySQLSearchEngine()) { $namespace = PhabricatorEnv::getEnvConfig('storage.default-namespace'); $summary = pht( - 'MySQL is configured to only index words with at least %d '. - 'characters.', + 'MySQL is configured (on host "%s") to only index words with at '. + 'least %d characters.', + $host_name, $min_len); $message = pht( - "Your MySQL instance is configured to use the default minimum word ". + "Database host \"%s\" is configured to use the default minimum word ". "length when building search indexes, which is 4. This means words ". "which are only 3 characters long will not be indexed and can not ". "be searched for.\n\n". "For example, you will not be able to find search results for words ". "like 'SMS', 'web', or 'DOS'.\n\n". "You can change this setting to 3 to allow these words to be ". "indexed. Alternatively, you can ignore this warning if you are ". "not concerned about searching for 3-letter words. If you later ". "plan to configure ElasticSearch, you can also ignore this warning: ". "only MySQL fulltext search is affected.\n\n". "To reduce the minimum word length to 3, add this to your %s file ". "(in the %s section) and then restart %s:\n\n". "%s\n". "Finally, run this command to rebuild indexes using the new ". "rules:\n\n". "%s", + $host_name, phutil_tag('tt', array(), 'my.cnf'), phutil_tag('tt', array(), '[mysqld]'), phutil_tag('tt', array(), 'mysqld'), phutil_tag('pre', array(), 'ft_min_word_len=3'), phutil_tag( 'pre', array(), "mysql> REPAIR TABLE {$namespace}_search.search_documentfield;")); $this->newIssue('mysql.ft_min_word_len') ->setName(pht('MySQL is Using Default Minimum Word Length')) ->setSummary($summary) ->setMessage($message) + ->setDatabaseRef($ref) ->addMySQLConfig('ft_min_word_len'); } } - $bool_syntax = self::loadRawConfigValue('ft_boolean_syntax'); + $bool_syntax = $ref->loadRawMySQLConfigValue('ft_boolean_syntax'); if ($bool_syntax != ' |-><()~*:""&^') { if ($this->shouldUseMySQLSearchEngine()) { $summary = pht( - 'MySQL is configured to search on fulltext indexes using "OR" by '. - 'default. Using "AND" is usually the desired behaviour.'); + 'MySQL (on host "%s") is configured to search on fulltext indexes '. + 'using "OR" by default. Using "AND" is usually the desired '. + 'behaviour.', + $host_name); $message = pht( - "Your MySQL instance is configured to use the default Boolean ". + "Database host \"%s\" is configured to use the default Boolean ". "search syntax when using fulltext indexes. This means searching ". "for 'search words' will yield the query 'search OR words' ". "instead of the desired 'search AND words'.\n\n". "This might produce unexpected search results. \n\n". "You can change this setting to a more sensible default. ". "Alternatively, you can ignore this warning if ". "using 'OR' is the desired behaviour. If you later plan ". "to configure ElasticSearch, you can also ignore this warning: ". "only MySQL fulltext search is affected.\n\n". "To change this setting, add this to your %s file ". "(in the %s section) and then restart %s:\n\n". "%s\n", + $host_name, phutil_tag('tt', array(), 'my.cnf'), phutil_tag('tt', array(), '[mysqld]'), phutil_tag('tt', array(), 'mysqld'), phutil_tag('pre', array(), 'ft_boolean_syntax=\' |-><()~*:""&^\'')); $this->newIssue('mysql.ft_boolean_syntax') ->setName(pht('MySQL is Using the Default Boolean Syntax')) ->setSummary($summary) ->setMessage($message) + ->setDatabaseRef($ref) ->addMySQLConfig('ft_boolean_syntax'); } } - $innodb_pool = self::loadRawConfigValue('innodb_buffer_pool_size'); + $innodb_pool = $ref->loadRawMySQLConfigValue('innodb_buffer_pool_size'); $innodb_bytes = phutil_parse_bytes($innodb_pool); $innodb_readable = phutil_format_bytes($innodb_bytes); // This is arbitrary and just trying to detect values that the user // probably didn't set themselves. The Mac OS X default is 128MB and // 40% of an AWS EC2 Micro instance is 245MB, so keeping it somewhere // between those two values seems like a reasonable approximation. $minimum_readable = '225MB'; $minimum_bytes = phutil_parse_bytes($minimum_readable); if ($innodb_bytes < $minimum_bytes) { $summary = pht( - 'MySQL is configured with a very small innodb_buffer_pool_size, '. - 'which may impact performance.'); + 'MySQL (on host "%s") is configured with a very small '. + 'innodb_buffer_pool_size, which may impact performance.', + $host_name); $message = pht( - "Your MySQL instance is configured with a very small %s (%s). ". + "Database host \"%s\" is configured with a very small %s (%s). ". "This may cause poor database performance and lock exhaustion.\n\n". "There are no hard-and-fast rules to setting an appropriate value, ". "but a reasonable starting point for a standard install is something ". "like 40%% of the total memory on the machine. For example, if you ". "have 4GB of RAM on the machine you have installed Phabricator on, ". "you might set this value to %s.\n\n". "You can read more about this option in the MySQL documentation to ". "help you make a decision about how to configure it for your use ". "case. There are no concerns specific to Phabricator which make it ". "different from normal workloads with respect to this setting.\n\n". "To adjust the setting, add something like this to your %s file (in ". "the %s section), replacing %s with an appropriate value for your ". "host and use case. Then restart %s:\n\n". "%s\n". "If you're satisfied with the current setting, you can safely ". "ignore this setup warning.", + $host_name, phutil_tag('tt', array(), 'innodb_buffer_pool_size'), phutil_tag('tt', array(), $innodb_readable), phutil_tag('tt', array(), '1600M'), phutil_tag('tt', array(), 'my.cnf'), phutil_tag('tt', array(), '[mysqld]'), phutil_tag('tt', array(), '1600M'), phutil_tag('tt', array(), 'mysqld'), phutil_tag('pre', array(), 'innodb_buffer_pool_size=1600M')); $this->newIssue('mysql.innodb_buffer_pool_size') ->setName(pht('MySQL May Run Slowly')) ->setSummary($summary) ->setMessage($message) + ->setDatabaseRef($ref) ->addMySQLConfig('innodb_buffer_pool_size'); } - $conn_w = id(new PhabricatorUser())->establishConnection('w'); + $conn = $ref->newManagementConnection(); $ok = PhabricatorStorageManagementAPI::isCharacterSetAvailableOnConnection( 'utf8mb4', - $conn_w); + $conn); if (!$ok) { $summary = pht( - 'You are using an old version of MySQL, and should upgrade.'); + 'You are using an old version of MySQL (on host "%s"), and should '. + 'upgrade.', + $host_name); $message = pht( - 'You are using an old version of MySQL which has poor unicode '. - 'support (it does not support the "utf8mb4" collation set). You will '. - 'encounter limitations when working with some unicode data.'. + 'You are using an old version of MySQL (on host "%s") which has poor '. + 'unicode support (it does not support the "utf8mb4" collation set). '. + 'You will encounter limitations when working with some unicode data.'. "\n\n". - 'We strongly recommend you upgrade to MySQL 5.5 or newer.'); + 'We strongly recommend you upgrade to MySQL 5.5 or newer.', + $host_name); $this->newIssue('mysql.utf8mb4') ->setName(pht('Old MySQL Version')) ->setSummary($summary) + ->setDatabaseRef($ref) ->setMessage($message); } $info = queryfx_one( - $conn_w, + $conn, 'SELECT UNIX_TIMESTAMP() epoch'); $epoch = (int)$info['epoch']; $local = PhabricatorTime::getNow(); $delta = (int)abs($local - $epoch); if ($delta > 60) { $this->newIssue('mysql.clock') ->setName(pht('Major Web/Database Clock Skew')) ->setSummary( pht( - 'This host is set to a very different time than the database.')) + 'This web host ("%s") is set to a very different time than a '. + 'database host "%s".', + php_uname('n'), + $host_name)) ->setMessage( pht( - 'The database host and this host ("%s") disagree on the current '. - 'time by more than 60 seconds (absolute skew is %s seconds). '. - 'Check that the current time is set correctly everywhere.', + 'A database host ("%s") and this web host ("%s") disagree on the '. + 'current time by more than 60 seconds (absolute skew is %s '. + 'seconds). Check that the current time is set correctly '. + 'everywhere.', + $host_name, php_uname('n'), new PhutilNumber($delta))); } } protected function shouldUseMySQLSearchEngine() { $search_engine = PhabricatorFulltextStorageEngine::loadEngine(); return ($search_engine instanceof PhabricatorMySQLFulltextStorageEngine); } } diff --git a/src/applications/config/issue/PhabricatorSetupIssue.php b/src/applications/config/issue/PhabricatorSetupIssue.php index 648d935a3c..a3d024a728 100644 --- a/src/applications/config/issue/PhabricatorSetupIssue.php +++ b/src/applications/config/issue/PhabricatorSetupIssue.php @@ -1,220 +1,230 @@ getMessage()); $issue = id(new self()) ->setIssueKey('mysql.connect') ->setName(pht('Can Not Connect to MySQL')) ->setMessage($message) ->setIsFatal($is_fatal) ->addRelatedPhabricatorConfig('mysql.host') ->addRelatedPhabricatorConfig('mysql.port') ->addRelatedPhabricatorConfig('mysql.user') ->addRelatedPhabricatorConfig('mysql.pass'); if (PhabricatorEnv::getEnvConfig('cluster.databases')) { $issue->addRelatedPhabricatorConfig('cluster.databases'); } return $issue; } public function addCommand($command) { $this->commands[] = $command; return $this; } public function getCommands() { return $this->commands; } public function setShortName($short_name) { $this->shortName = $short_name; return $this; } public function getShortName() { if ($this->shortName === null) { return $this->getName(); } return $this->shortName; } + public function setDatabaseRef(PhabricatorDatabaseRef $database_ref) { + $this->databaseRef = $database_ref; + return $this; + } + + public function getDatabaseRef() { + return $this->databaseRef; + } + public function setGroup($group) { $this->group = $group; return $this; } public function getGroup() { if ($this->group) { return $this->group; } else { return PhabricatorSetupCheck::GROUP_OTHER; } } public function setName($name) { $this->name = $name; return $this; } public function getName() { return $this->name; } public function setSummary($summary) { $this->summary = $summary; return $this; } public function getSummary() { if ($this->summary === null) { return $this->getMessage(); } return $this->summary; } public function setIssueKey($issue_key) { $this->issueKey = $issue_key; return $this; } public function getIssueKey() { return $this->issueKey; } public function setIsFatal($is_fatal) { $this->isFatal = $is_fatal; return $this; } public function getIsFatal() { return $this->isFatal; } public function addPHPConfig($php_config) { $this->phpConfig[] = $php_config; return $this; } /** * Set an explicit value to display when showing the user PHP configuration * values. * * If Phabricator has changed a value by the time a config issue is raised, * you can provide the original value here so the UI makes sense. For example, * we alter `memory_limit` during startup, so if the original value is not * provided it will look like it is always set to `-1`. * * @param string PHP configuration option to provide a value for. * @param string Explicit value to show in the UI. * @return this */ public function addPHPConfigOriginalValue($php_config, $value) { $this->originalPHPConfigValues[$php_config] = $value; return $this; } public function getPHPConfigOriginalValue($php_config, $default = null) { return idx($this->originalPHPConfigValues, $php_config, $default); } public function getPHPConfig() { return $this->phpConfig; } public function addMySQLConfig($mysql_config) { $this->mysqlConfig[] = $mysql_config; return $this; } public function getMySQLConfig() { return $this->mysqlConfig; } public function addPhabricatorConfig($phabricator_config) { $this->phabricatorConfig[] = $phabricator_config; return $this; } public function getPhabricatorConfig() { return $this->phabricatorConfig; } public function addRelatedPhabricatorConfig($phabricator_config) { $this->relatedPhabricatorConfig[] = $phabricator_config; return $this; } public function getRelatedPhabricatorConfig() { return $this->relatedPhabricatorConfig; } public function addPHPExtension($php_extension) { $this->phpExtensions[] = $php_extension; return $this; } public function getPHPExtensions() { return $this->phpExtensions; } public function setMessage($message) { $this->message = $message; return $this; } public function getMessage() { return $this->message; } public function setIsIgnored($is_ignored) { $this->isIgnored = $is_ignored; return $this; } public function getIsIgnored() { return $this->isIgnored; } public function addLink($href, $name) { $this->links[] = array( 'href' => $href, 'name' => $name, ); return $this; } public function getLinks() { return $this->links; } } diff --git a/src/applications/config/view/PhabricatorSetupIssueView.php b/src/applications/config/view/PhabricatorSetupIssueView.php index d2e5a6c281..10e802eb3f 100644 --- a/src/applications/config/view/PhabricatorSetupIssueView.php +++ b/src/applications/config/view/PhabricatorSetupIssueView.php @@ -1,601 +1,605 @@ issue = $issue; return $this; } public function getIssue() { return $this->issue; } public function renderInFlight() { $issue = $this->getIssue(); return id(new PhabricatorInFlightErrorView()) ->setMessage($issue->getName()) ->render(); } public function render() { $issue = $this->getIssue(); $description = array(); $description[] = phutil_tag( 'div', array( 'class' => 'setup-issue-instructions', ), phutil_escape_html_newlines($issue->getMessage())); $configs = $issue->getPHPConfig(); if ($configs) { $description[] = $this->renderPHPConfig($configs, $issue); } $configs = $issue->getMySQLConfig(); if ($configs) { $description[] = $this->renderMySQLConfig($configs); } $configs = $issue->getPhabricatorConfig(); if ($configs) { $description[] = $this->renderPhabricatorConfig($configs); } $related_configs = $issue->getRelatedPhabricatorConfig(); if ($related_configs) { $description[] = $this->renderPhabricatorConfig($related_configs, $related = true); } $commands = $issue->getCommands(); if ($commands) { $run_these = pht('Run these %d command(s):', count($commands)); $description[] = phutil_tag( 'div', array( 'class' => 'setup-issue-config', ), array( phutil_tag('p', array(), $run_these), phutil_tag('pre', array(), phutil_implode_html("\n", $commands)), )); } $extensions = $issue->getPHPExtensions(); if ($extensions) { $install_these = pht( 'Install these %d PHP extension(s):', count($extensions)); $install_info = pht( 'You can usually install a PHP extension using %s or %s. Common '. 'package names are %s or %s. Try commands like these:', phutil_tag('tt', array(), 'apt-get'), phutil_tag('tt', array(), 'yum'), hsprintf('php-%s', pht('extname')), hsprintf('php5-%s', pht('extname'))); // TODO: We should do a better job of detecting how to install extensions // on the current system. $install_commands = hsprintf( "\$ sudo apt-get install php5-extname ". "# Debian / Ubuntu\n". "\$ sudo yum install php-extname ". "# Red Hat / Derivatives"); $fallback_info = pht( "If those commands don't work, try Google. The process of installing ". "PHP extensions is not specific to Phabricator, and any instructions ". "you can find for installing them on your system should work. On Mac ". "OS X, you might want to try Homebrew."); $restart_info = pht( 'After installing new PHP extensions, restart Phabricator '. 'for the changes to take effect. For help with restarting '. 'Phabricator, see %s in the documentation.', $this->renderRestartLink()); $description[] = phutil_tag( 'div', array( 'class' => 'setup-issue-config', ), array( phutil_tag('p', array(), $install_these), phutil_tag('pre', array(), implode("\n", $extensions)), phutil_tag('p', array(), $install_info), phutil_tag('pre', array(), $install_commands), phutil_tag('p', array(), $fallback_info), phutil_tag('p', array(), $restart_info), )); } $related_links = $issue->getLinks(); if ($related_links) { $description[] = $this->renderRelatedLinks($related_links); } $actions = array(); if (!$issue->getIsFatal()) { if ($issue->getIsIgnored()) { $actions[] = javelin_tag( 'a', array( 'href' => '/config/unignore/'.$issue->getIssueKey().'/', 'sigil' => 'workflow', 'class' => 'button grey', ), pht('Unignore Setup Issue')); } else { $actions[] = javelin_tag( 'a', array( 'href' => '/config/ignore/'.$issue->getIssueKey().'/', 'sigil' => 'workflow', 'class' => 'button grey', ), pht('Ignore Setup Issue')); } $actions[] = javelin_tag( 'a', array( 'href' => '/config/issue/'.$issue->getIssueKey().'/', 'class' => 'button grey', 'style' => 'float: right', ), pht('Reload Page')); } if ($actions) { $actions = phutil_tag( 'div', array( 'class' => 'setup-issue-actions', ), $actions); } if ($issue->getIsIgnored()) { $status = phutil_tag( 'div', array( 'class' => 'setup-issue-status', ), pht( 'This issue is currently ignored, and does not show a global '. 'warning.')); $next = null; } else { $status = null; $next = phutil_tag( 'div', array( 'class' => 'setup-issue-next', ), pht('To continue, resolve this problem and reload the page.')); } $name = phutil_tag( 'div', array( 'class' => 'setup-issue-name', ), $issue->getName()); $head = phutil_tag( 'div', array( 'class' => 'setup-issue-head', ), array($name, $status)); $tail = phutil_tag( 'div', array( 'class' => 'setup-issue-tail', ), array($actions)); $issue = phutil_tag( 'div', array( 'class' => 'setup-issue', ), array( $head, $description, $tail, )); $debug_info = phutil_tag( 'div', array( 'class' => 'setup-issue-debug', ), pht('Host: %s', php_uname('n'))); return phutil_tag( 'div', array( 'class' => 'setup-issue-shell', ), array( $issue, $next, $debug_info, )); } private function renderPhabricatorConfig(array $configs, $related = false) { $issue = $this->getIssue(); $table_info = phutil_tag( 'p', array(), pht( 'The current Phabricator configuration has these %d value(s):', count($configs))); $options = PhabricatorApplicationConfigOptions::loadAllOptions(); $hidden = array(); foreach ($options as $key => $option) { if ($option->getHidden()) { $hidden[$key] = true; } } $table = null; $dict = array(); foreach ($configs as $key) { if (isset($hidden[$key])) { $dict[$key] = null; } else { $dict[$key] = PhabricatorEnv::getUnrepairedEnvConfig($key); } } $table = $this->renderValueTable($dict, $hidden); if ($this->getIssue()->getIsFatal()) { $update_info = phutil_tag( 'p', array(), pht( 'To update these %d value(s), run these command(s) from the command '. 'line:', count($configs))); $update = array(); foreach ($configs as $key) { $update[] = hsprintf( 'phabricator/ $ ./bin/config set %s value', $key); } $update = phutil_tag('pre', array(), phutil_implode_html("\n", $update)); } else { $update = array(); foreach ($configs as $config) { if (idx($options, $config) && $options[$config]->getLocked()) { $name = pht('View "%s"', $config); } else { $name = pht('Edit "%s"', $config); } $link = phutil_tag( 'a', array( 'href' => '/config/edit/'.$config.'/?issue='.$issue->getIssueKey(), ), $name); $update[] = phutil_tag('li', array(), $link); } if ($update) { $update = phutil_tag('ul', array(), $update); if (!$related) { $update_info = phutil_tag( 'p', array(), pht('You can update these %d value(s) here:', count($configs))); } else { $update_info = phutil_tag( 'p', array(), pht('These %d configuration value(s) are related:', count($configs))); } } else { $update = null; $update_info = null; } } return phutil_tag( 'div', array( 'class' => 'setup-issue-config', ), array( $table_info, $table, $update_info, $update, )); } private function renderPHPConfig(array $configs, $issue) { $table_info = phutil_tag( 'p', array(), pht( 'The current PHP configuration has these %d value(s):', count($configs))); $dict = array(); foreach ($configs as $key) { $dict[$key] = $issue->getPHPConfigOriginalValue( $key, ini_get($key)); } $table = $this->renderValueTable($dict); ob_start(); phpinfo(); $phpinfo = ob_get_clean(); $rex = '@Loaded Configuration File\s*(.*?)@i'; $matches = null; $ini_loc = null; if (preg_match($rex, $phpinfo, $matches)) { $ini_loc = trim($matches[1]); } $rex = '@Additional \.ini files parsed\s*(.*?)@i'; $more_loc = array(); if (preg_match($rex, $phpinfo, $matches)) { $more_loc = trim($matches[1]); if ($more_loc == '(none)') { $more_loc = array(); } else { $more_loc = preg_split('/\s*,\s*/', $more_loc); } } $info = array(); if (!$ini_loc) { $info[] = phutil_tag( 'p', array(), pht( 'To update these %d value(s), edit your PHP configuration file.', count($configs))); } else { $info[] = phutil_tag( 'p', array(), pht( 'To update these %d value(s), edit your PHP configuration file, '. 'located here:', count($configs))); $info[] = phutil_tag( 'pre', array(), $ini_loc); } if ($more_loc) { $info[] = phutil_tag( 'p', array(), pht( 'PHP also loaded these %s configuration file(s):', phutil_count($more_loc))); $info[] = phutil_tag( 'pre', array(), implode("\n", $more_loc)); } $show_standard = false; $show_opcache = false; foreach ($configs as $key) { if (preg_match('/^opcache\./', $key)) { $show_opcache = true; } else { $show_standard = true; } } if ($show_standard) { $info[] = phutil_tag( 'p', array(), pht( 'You can find more information about PHP configuration values '. 'in the %s.', phutil_tag( 'a', array( 'href' => 'http://php.net/manual/ini.list.php', 'target' => '_blank', ), pht('PHP Documentation')))); } if ($show_opcache) { $info[] = phutil_tag( 'p', array(), pht( 'You can find more information about configuring OPCache in '. 'the %s.', phutil_tag( 'a', array( 'href' => 'http://php.net/manual/opcache.configuration.php', 'target' => '_blank', ), pht('PHP OPCache Documentation')))); } $info[] = phutil_tag( 'p', array(), pht( 'After editing the PHP configuration, restart Phabricator for '. 'the changes to take effect. For help with restarting '. 'Phabricator, see %s in the documentation.', $this->renderRestartLink())); return phutil_tag( 'div', array( 'class' => 'setup-issue-config', ), array( $table_info, $table, $info, )); } private function renderMySQLConfig(array $config) { $values = array(); - foreach ($config as $key) { - $value = PhabricatorMySQLSetupCheck::loadRawConfigValue($key); - if ($value === null) { - $value = phutil_tag( - 'em', - array(), - pht('(Not Supported)')); + $issue = $this->getIssue(); + $ref = $issue->getDatabaseRef(); + if ($ref) { + foreach ($config as $key) { + $value = $ref->loadRawMySQLConfigValue($key); + if ($value === null) { + $value = phutil_tag( + 'em', + array(), + pht('(Not Supported)')); + } + $values[$key] = $value; } - $values[$key] = $value; } $table = $this->renderValueTable($values); $doc_href = PhabricatorEnv::getDoclink('User Guide: Amazon RDS'); $doc_link = phutil_tag( 'a', array( 'href' => $doc_href, 'target' => '_blank', ), pht('User Guide: Amazon RDS')); $info = array(); $info[] = phutil_tag( 'p', array(), pht( 'If you are using Amazon RDS, some of the instructions above may '. 'not apply to you. See %s for discussion of Amazon RDS.', $doc_link)); $table_info = phutil_tag( 'p', array(), pht( 'The current MySQL configuration has these %d value(s):', count($config))); return phutil_tag( 'div', array( 'class' => 'setup-issue-config', ), array( $table_info, $table, $info, )); } private function renderValueTable(array $dict, array $hidden = array()) { $rows = array(); foreach ($dict as $key => $value) { if (isset($hidden[$key])) { $value = phutil_tag('em', array(), 'hidden'); } else { $value = $this->renderValueForDisplay($value); } $cols = array( phutil_tag('th', array(), $key), phutil_tag('td', array(), $value), ); $rows[] = phutil_tag('tr', array(), $cols); } return phutil_tag('table', array(), $rows); } private function renderValueForDisplay($value) { if ($value === null) { return phutil_tag('em', array(), 'null'); } else if ($value === false) { return phutil_tag('em', array(), 'false'); } else if ($value === true) { return phutil_tag('em', array(), 'true'); } else if ($value === '') { return phutil_tag('em', array(), 'empty string'); } else if ($value instanceof PhutilSafeHTML) { return $value; } else { return PhabricatorConfigJSON::prettyPrintJSON($value); } } private function renderRelatedLinks(array $links) { $link_info = phutil_tag( 'p', array(), pht( '%d related link(s):', count($links))); $link_list = array(); foreach ($links as $link) { $link_tag = phutil_tag( 'a', array( 'target' => '_blank', 'href' => $link['href'], ), $link['name']); $link_item = phutil_tag('li', array(), $link_tag); $link_list[] = $link_item; } $link_list = phutil_tag('ul', array(), $link_list); return phutil_tag( 'div', array( 'class' => 'setup-issue-config', ), array( $link_info, $link_list, )); } private function renderRestartLink() { $doc_href = PhabricatorEnv::getDoclink('Restarting Phabricator'); return phutil_tag( 'a', array( 'href' => $doc_href, 'target' => '_blank', ), pht('Restarting Phabricator')); } } diff --git a/src/infrastructure/cluster/PhabricatorDatabaseRef.php b/src/infrastructure/cluster/PhabricatorDatabaseRef.php index 22f405ad91..0559ba0c32 100644 --- a/src/infrastructure/cluster/PhabricatorDatabaseRef.php +++ b/src/infrastructure/cluster/PhabricatorDatabaseRef.php @@ -1,660 +1,673 @@ host = $host; return $this; } public function getHost() { return $this->host; } public function setPort($port) { $this->port = $port; return $this; } public function getPort() { return $this->port; } public function setUser($user) { $this->user = $user; return $this; } public function getUser() { return $this->user; } public function setPass(PhutilOpaqueEnvelope $pass) { $this->pass = $pass; return $this; } public function getPass() { return $this->pass; } public function setIsMaster($is_master) { $this->isMaster = $is_master; return $this; } public function getIsMaster() { return $this->isMaster; } public function setDisabled($disabled) { $this->disabled = $disabled; return $this; } public function getDisabled() { return $this->disabled; } public function setConnectionLatency($connection_latency) { $this->connectionLatency = $connection_latency; return $this; } public function getConnectionLatency() { return $this->connectionLatency; } public function setConnectionStatus($connection_status) { $this->connectionStatus = $connection_status; return $this; } public function getConnectionStatus() { if ($this->connectionStatus === null) { throw new PhutilInvalidStateException('queryAll'); } return $this->connectionStatus; } public function setConnectionMessage($connection_message) { $this->connectionMessage = $connection_message; return $this; } public function getConnectionMessage() { return $this->connectionMessage; } public function setReplicaStatus($replica_status) { $this->replicaStatus = $replica_status; return $this; } public function getReplicaStatus() { return $this->replicaStatus; } public function setReplicaMessage($replica_message) { $this->replicaMessage = $replica_message; return $this; } public function getReplicaMessage() { return $this->replicaMessage; } public function setReplicaDelay($replica_delay) { $this->replicaDelay = $replica_delay; return $this; } public function getReplicaDelay() { return $this->replicaDelay; } public function setIsIndividual($is_individual) { $this->isIndividual = $is_individual; return $this; } public function getIsIndividual() { return $this->isIndividual; } public function setIsDefaultPartition($is_default_partition) { $this->isDefaultPartition = $is_default_partition; return $this; } public function getIsDefaultPartition() { return $this->isDefaultPartition; } public function setApplicationMap(array $application_map) { $this->applicationMap = $application_map; return $this; } public function getApplicationMap() { return $this->applicationMap; } public function setMasterRef(PhabricatorDatabaseRef $master_ref) { $this->masterRef = $master_ref; return $this; } public function getMasterRef() { return $this->masterRef; } public function addReplicaRef(PhabricatorDatabaseRef $replica_ref) { $this->replicaRefs[] = $replica_ref; return $this; } public function getReplicaRefs() { return $this->replicaRefs; } public function getRefKey() { $host = $this->getHost(); $port = $this->getPort(); if (strlen($port)) { return "{$host}:{$port}"; } return $host; } public static function getConnectionStatusMap() { return array( self::STATUS_OKAY => array( 'icon' => 'fa-exchange', 'color' => 'green', 'label' => pht('Okay'), ), self::STATUS_FAIL => array( 'icon' => 'fa-times', 'color' => 'red', 'label' => pht('Failed'), ), self::STATUS_AUTH => array( 'icon' => 'fa-key', 'color' => 'red', 'label' => pht('Invalid Credentials'), ), self::STATUS_REPLICATION_CLIENT => array( 'icon' => 'fa-eye-slash', 'color' => 'yellow', 'label' => pht('Missing Permission'), ), ); } public static function getReplicaStatusMap() { return array( self::REPLICATION_OKAY => array( 'icon' => 'fa-download', 'color' => 'green', 'label' => pht('Okay'), ), self::REPLICATION_MASTER_REPLICA => array( 'icon' => 'fa-database', 'color' => 'red', 'label' => pht('Replicating Master'), ), self::REPLICATION_REPLICA_NONE => array( 'icon' => 'fa-download', 'color' => 'red', 'label' => pht('Not A Replica'), ), self::REPLICATION_SLOW => array( 'icon' => 'fa-hourglass', 'color' => 'red', 'label' => pht('Slow Replication'), ), self::REPLICATION_NOT_REPLICATING => array( 'icon' => 'fa-exclamation-triangle', 'color' => 'red', 'label' => pht('Not Replicating'), ), ); } public static function getClusterRefs() { $cache = PhabricatorCaches::getRequestCache(); $refs = $cache->getKey(self::KEY_REFS); if (!$refs) { $refs = self::newRefs(); $cache->setKey(self::KEY_REFS, $refs); } return $refs; } public static function getLiveIndividualRef() { $cache = PhabricatorCaches::getRequestCache(); $ref = $cache->getKey(self::KEY_INDIVIDUAL); if (!$ref) { $ref = self::newIndividualRef(); $cache->setKey(self::KEY_INDIVIDUAL, $ref); } return $ref; } public static function newRefs() { $default_port = PhabricatorEnv::getEnvConfig('mysql.port'); $default_port = nonempty($default_port, 3306); $default_user = PhabricatorEnv::getEnvConfig('mysql.user'); $default_pass = PhabricatorEnv::getEnvConfig('mysql.pass'); $default_pass = new PhutilOpaqueEnvelope($default_pass); $config = PhabricatorEnv::getEnvConfig('cluster.databases'); return id(new PhabricatorDatabaseRefParser()) ->setDefaultPort($default_port) ->setDefaultUser($default_user) ->setDefaultPass($default_pass) ->newRefs($config); } public static function queryAll() { $refs = self::getActiveDatabaseRefs(); return self::queryRefs($refs); } private static function queryRefs(array $refs) { foreach ($refs as $ref) { $conn = $ref->newManagementConnection(); $t_start = microtime(true); $replica_status = false; try { $replica_status = queryfx_one($conn, 'SHOW SLAVE STATUS'); $ref->setConnectionStatus(self::STATUS_OKAY); } catch (AphrontAccessDeniedQueryException $ex) { $ref->setConnectionStatus(self::STATUS_REPLICATION_CLIENT); $ref->setConnectionMessage( pht( 'No permission to run "SHOW SLAVE STATUS". Grant this user '. '"REPLICATION CLIENT" permission to allow Phabricator to '. 'monitor replica health.')); } catch (AphrontInvalidCredentialsQueryException $ex) { $ref->setConnectionStatus(self::STATUS_AUTH); $ref->setConnectionMessage($ex->getMessage()); } catch (AphrontQueryException $ex) { $ref->setConnectionStatus(self::STATUS_FAIL); $class = get_class($ex); $message = $ex->getMessage(); $ref->setConnectionMessage( pht( '%s: %s', get_class($ex), $ex->getMessage())); } $t_end = microtime(true); $ref->setConnectionLatency($t_end - $t_start); if ($replica_status !== false) { $is_replica = (bool)$replica_status; if ($ref->getIsMaster() && $is_replica) { $ref->setReplicaStatus(self::REPLICATION_MASTER_REPLICA); $ref->setReplicaMessage( pht( 'This host has a "master" role, but is replicating data from '. 'another host ("%s")!', idx($replica_status, 'Master_Host'))); } else if (!$ref->getIsMaster() && !$is_replica) { $ref->setReplicaStatus(self::REPLICATION_REPLICA_NONE); $ref->setReplicaMessage( pht( 'This host has a "replica" role, but is not replicating data '. 'from a master (no output from "SHOW SLAVE STATUS").')); } else { $ref->setReplicaStatus(self::REPLICATION_OKAY); } if ($is_replica) { $latency = idx($replica_status, 'Seconds_Behind_Master'); if (!strlen($latency)) { $ref->setReplicaStatus(self::REPLICATION_NOT_REPLICATING); } else { $latency = (int)$latency; $ref->setReplicaDelay($latency); if ($latency > 30) { $ref->setReplicaStatus(self::REPLICATION_SLOW); $ref->setReplicaMessage( pht( 'This replica is lagging far behind the master. Data is at '. 'risk!')); } } } } } return $refs; } public function newManagementConnection() { return $this->newConnection( array( 'retries' => 0, 'timeout' => 2, )); } public function newApplicationConnection($database) { return $this->newConnection( array( 'database' => $database, )); } public function isSevered() { // If we only have an individual database, never sever our connection to // it, at least for now. It's possible that using the same severing rules // might eventually make sense to help alleviate load-related failures, // but we should wait for all the cluster stuff to stabilize first. if ($this->getIsIndividual()) { return false; } if ($this->didFailToConnect) { return true; } $record = $this->getHealthRecord(); $is_healthy = $record->getIsHealthy(); if (!$is_healthy) { return true; } return false; } public function isReachable(AphrontDatabaseConnection $connection) { $record = $this->getHealthRecord(); $should_check = $record->getShouldCheck(); if ($this->isSevered() && !$should_check) { return false; } try { $connection->openConnection(); $reachable = true; } catch (AphrontSchemaQueryException $ex) { // We get one of these if the database we're trying to select does not // exist. In this case, just re-throw the exception. This is expected // during first-time setup, when databases like "config" will not exist // yet. throw $ex; } catch (Exception $ex) { $reachable = false; } if ($should_check) { $record->didHealthCheck($reachable); } if (!$reachable) { $this->didFailToConnect = true; } return $reachable; } public function checkHealth() { $health = $this->getHealthRecord(); $should_check = $health->getShouldCheck(); if ($should_check) { // This does an implicit health update. $connection = $this->newManagementConnection(); $this->isReachable($connection); } return $this; } public function getHealthRecord() { if (!$this->healthRecord) { $this->healthRecord = new PhabricatorDatabaseHealthRecord($this); } return $this->healthRecord; } public static function getActiveDatabaseRefs() { $refs = array(); foreach (self::getMasterDatabaseRefs() as $ref) { $refs[] = $ref; } foreach (self::getReplicaDatabaseRefs() as $ref) { $refs[] = $ref; } return $refs; } public static function getAllMasterDatabaseRefs() { $refs = self::getClusterRefs(); if (!$refs) { return array(self::getLiveIndividualRef()); } $masters = array(); foreach ($refs as $ref) { if ($ref->getDisabled()) { continue; } if ($ref->getIsMaster()) { $masters[] = $ref; } } return $masters; } public static function getMasterDatabaseRefs() { $refs = self::getAllMasterDatabaseRefs(); return self::getEnabledRefs($refs); } public function isApplicationHost($database) { return isset($this->applicationMap[$database]); } + public function loadRawMySQLConfigValue($key) { + $conn = $this->newManagementConnection(); + + try { + $value = queryfx_one($conn, 'SELECT @@%Q', $key); + $value = $value['@@'.$key]; + } catch (AphrontQueryException $ex) { + $value = null; + } + + return $value; + } + public static function getMasterDatabaseRefForApplication($application) { $masters = self::getMasterDatabaseRefs(); $application_master = null; $default_master = null; foreach ($masters as $master) { if ($master->isApplicationHost($application)) { $application_master = $master; break; } if ($master->getIsDefaultPartition()) { $default_master = $master; } } if ($application_master) { $masters = array($application_master); } else if ($default_master) { $masters = array($default_master); } else { $masters = array(); } $masters = self::getEnabledRefs($masters); $master = head($masters); return $master; } public static function newIndividualRef() { $conf = PhabricatorEnv::newObjectFromConfig( 'mysql.configuration-provider', array(null, 'w', null)); return id(new self()) ->setHost($conf->getHost()) ->setPort($conf->getPort()) ->setUser($conf->getUser()) ->setPass($conf->getPassword()) ->setIsIndividual(true) ->setIsMaster(true); } public static function getAllReplicaDatabaseRefs() { $refs = self::getClusterRefs(); if (!$refs) { return array(); } $replicas = array(); foreach ($refs as $ref) { if ($ref->getIsMaster()) { continue; } $replicas[] = $ref; } return $replicas; } public static function getReplicaDatabaseRefs() { $refs = self::getAllReplicaDatabaseRefs(); return self::getEnabledRefs($refs); } private static function getEnabledRefs(array $refs) { foreach ($refs as $key => $ref) { if ($ref->getDisabled()) { unset($refs[$key]); } } return $refs; } public static function getReplicaDatabaseRefForApplication($application) { $replicas = self::getReplicaDatabaseRefs(); $application_replicas = array(); $default_replicas = array(); foreach ($replicas as $replica) { $master = $replica->getMaster(); if ($master->isApplicationHost($application)) { $application_replicas[] = $replica; } if ($master->getIsDefaultPartition()) { $default_replicas[] = $replica; } } if ($application_replicas) { $replicas = $application_replicas; } else { $replicas = $default_replicas; } $replicas = self::getEnabledRefs($replicas); // TODO: We may have multiple replicas to choose from, and could make // more of an effort to pick the "best" one here instead of always // picking the first one. Once we've picked one, we should try to use // the same replica for the rest of the request, though. return head($replicas); } private function newConnection(array $options) { // If we believe the database is unhealthy, don't spend as much time // trying to connect to it, since it's likely to continue to fail and // hammering it can only make the problem worse. $record = $this->getHealthRecord(); if ($record->getIsHealthy()) { $default_retries = 3; $default_timeout = 10; } else { $default_retries = 0; $default_timeout = 2; } $spec = $options + array( 'user' => $this->getUser(), 'pass' => $this->getPass(), 'host' => $this->getHost(), 'port' => $this->getPort(), 'database' => null, 'retries' => $default_retries, 'timeout' => $default_timeout, ); return PhabricatorEnv::newObjectFromConfig( 'mysql.implementation', array( $spec, )); } }