Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP DB code: clean up the logic, and allow for > 1 readonly replica #5697

Merged
merged 4 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 74 additions & 96 deletions html/inc/boinc_db.inc
Original file line number Diff line number Diff line change
Expand Up @@ -27,119 +27,97 @@ incs();
class BoincDb extends DbConn {
static $instance;

// connect to the database (possibly to a read-only replica)
// NOTE: choice of replica can be made only at the page level.
// If there's a page that's guaranteed to do only reads, put
// BoincDb::get(true);
// at the top of it.
// A project can have one or more databases:
// DB 0:
// the main DB; read/write
// identified in config file by db_host, db_name, db_user, db_passwd
// db_host defaults to localhost
// DB 1:
// read-only replica; identified by
// replica_db_host/name/user/passwd (must include all)
// DB 2:
// read-only replica; identified by
// replica2_db_host/name/user/passwd (must include all)
// ... and potentially more

// connect to DB $dbnum (0, 1, ...)
// If the requested DB doesn't exist or connection fails, connect to DB 0.
// Set self::$instance; no return value
//
// Specify a $fallback_mode that is used when $readonly is true:
// 0: default, use db_user if no replica_db_user is specified,
// first try replica_db_host (if specified) then db_host
// 1: only use replica_db_user, first try replica_db_host then db_host
// 2: only use replica_db_user, only try replica_db_host
// can be set projectwide using <replica_fallback_mode>
//
static function get_aux($readonly, $fallback_mode = 0) {
$config = get_config();
$user = parse_config($config, '<db_user>');
$passwd = parse_config($config, '<db_passwd>');
$host = parse_config($config, '<db_host>');
$replica_host = parse_config($config, '<replica_db_host>');
$name = parse_config($config, '<db_name>');
$fm = parse_config($config, '<replica_fallback_mode>');
if ($fm) {
// override parameter with config.xml setting
$fallback_mode = $fm;
}
if ($host == null) {
$host = "localhost";
}
static function get_aux($dbnum) {
$instance = new DbConn();
if ($readonly) {
if (($fallback_mode > 0) && (!$replica_host)) {
error_log("BoincDb::get_aux(): <replica_db_host> required for \$fallback_mode > 0 (giving up)");
$instance = null;
self::$instance = $instance;
return $instance;
}
$u = parse_config($config, '<replica_db_user>');
$p = parse_config($config, '<replica_db_passwd>');
$n = parse_config($config, '<replica_db_name>');
if (($fallback_mode > 0) && (!$u || !$p || !$n)) {
error_log("BoincDb::get_aux(): <replica_db_*> required for \$fallback_mode > 0 (giving up)");
$instance = null;
self::$instance = $instance;
return $instance;
} else {
// use replica user if given or use normal user for $fallback_mode == 0
if ($u) $user = $u;
if ($p) $passwd = $p;
if ($n) $name = $n;
}
// skip this block if no $replica_host is specified for $fallback_mode == 0
if ($replica_host) {
$retval = $instance->init_conn(
$user, $passwd, $replica_host, $name, true
);
self::$instance = null;
$config = get_config();
if ($dbnum) {
$r = $dbnum==1?'':strval($dbnum);
$host = parse_config($config, sprintf('<replica%s_db_host>', $r));
$name = parse_config($config, sprintf('<replica%s_db_name>', $r));
$user = parse_config($config, sprintf('<replica%s_db_user>', $r));
$passwd = parse_config($config, sprintf('<replica%s_db_passwd>', $r));
if ($host && $name && $user && $passwd) {
$retval = $instance->init_conn($user, $passwd, $host, $name);
if ($retval) {
// needed for places where we do direct queries
if (!$instance->do_query("use $name")) {
error_log("BoincDb::get_aux(): Couldn't select database $name on $replica_host (giving up)");
$instance = null;
if ($instance->do_query("use $name")) {
//error_log("BoincDb::get_aux(): connected to replica DB $dbnum");
brevilo marked this conversation as resolved.
Show resolved Hide resolved
self::$instance = $instance;
return;
}
self::$instance = $instance;
return $instance;
} elseif ($fallback_mode == 2) {
// no fallback to master in this case
error_log("BoincDb::get_aux(): Couldn't connect to $user@$replica_host (giving up)");
$instance = null;
self::$instance = $instance;
return $instance;
} else {
error_log("BoincDb::get_aux(): Couldn't connect to $user@$replica_host (trying $user@$host next)");
}
}
// if can't connect to replica, fall through and try DB 0
}
$retval = $instance->init_conn($user, $passwd, $host, $name, false);
if (!$retval) {
$instance = null;
error_log("BoincDb::get_aux(): Couldn't connect to $user@$host (giving up)");
} else {
// needed for places where we do direct queries
if (!$instance->do_query("use $name")) {
error_log("BoincDb::get_aux(): Couldn't select database $name on $host (giving up)");
$instance = null;
$host = parse_config($config, '<db_host>');
$user = parse_config($config, '<db_user>');
$name = parse_config($config, '<db_name>');
$passwd = parse_config($config, '<db_passwd>');
// OK if host is null; it will use localhost
brevilo marked this conversation as resolved.
Show resolved Hide resolved
if (!$name || !$user || !$passwd) {
error_log("BoincDb::get_aux(): must specify DB name, user, passwd");
return;
}
$retval = $instance->init_conn($user, $passwd, $host, $name);
if ($retval) {
if ($instance->do_query("use $name")) {
//error_log("BoincDb::get_aux(): connected to DB $dbnum");
brevilo marked this conversation as resolved.
Show resolved Hide resolved
self::$instance = $instance;
return;
}
}
self::$instance = $instance;
return $instance;
error_log("BoincDb::get_aux(): Couldn't connect to DB $dbnum");
}

// same, but
// connect to DB $dbnum, but first:
// 1) check for a cached connection
// 2) check whether the "stop_web" trigger file is present
//
static function get($readonly = false, $fallback_mode = 0) {
// If there's a page that's guaranteed to do only reads, put
// BoincDb::get(true);
// at the top of it.
//
// Note: true == 1.
// You can also 2, 3... to select other replicas
//
static function get($dbnum = 0) {
global $generating_xml;
if (!isset(self::$instance)) {
if (web_stopped()) {
if ($generating_xml) {
xml_error(-183, "project down for maintenance");
} else {
show_project_down();
}
}
self::get_aux($readonly, $fallback_mode);
if (!self::$instance) {
if ($generating_xml) {
xml_error(-138, "Can't connect to database");
} else {
error_page("Can't connect to database");
}
if (isset(self::$instance)) {
brevilo marked this conversation as resolved.
Show resolved Hide resolved
return self::$instance;
}
if (web_stopped()) {
if ($generating_xml) {
xml_error(-183, "project down for maintenance");
} else {
show_project_down();
}
}
return self::$instance;
self::get_aux($dbnum);
if (self::$instance) {
return self::$instance;
}
if ($generating_xml) {
xml_error(-138, "Can't connect to database");
} else {
error_page("Can't connect to database");
}
}

static function escape_string($string) {
Expand Down
15 changes: 1 addition & 14 deletions html/inc/db.inc
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,7 @@ require_once("../inc/util_basic.inc");
// DEPRECATED; use boinc_db.inc instead.
// TODO: replace calls to these functions

// use mysqli if available,
// but let projects not use it if they want
// (put <no_mysqli/> in config.xml)
//
if (parse_bool(get_config(), "no_mysqli")) {
define("MYSQLI", false);
} else {
if (class_exists("mysqli")) {
define("MYSQLI", true);
$mysqli = null;
} else {
define("MYSQLI", false);
}
}
define("MYSQLI", true);

if (MYSQLI) {
function _mysql_connect($host, $user, $pass, $dbname) {
Expand Down
Loading
Loading