diff --git a/db-config.php b/db-config.php index e6ab659a7102ebfc5f8b03926883c87f9fc5269a..f1b35d24ea713cca147fed51754b51530a702ad3 100644 --- a/db-config.php +++ b/db-config.php @@ -6,38 +6,13 @@ $wpdb->max_connections = 30; define("NOBLOGS_BACKEND_CONFIG", "/etc/noblogs/backends"); define("NOBLOGS_MASTER_CONFIG", "/etc/noblogs/master"); +define("NOBLOGS_HOST_FILE", "/etc/noblogs/ip_ring0"); include_once('r2db/db-hash.php'); include_once('r2db/db-backends.php'); -// Add the global database (configured in wp-config.php), stores the global -// blogs and users tables. -$is_master = __gf_ai_is_master(); - -$wpdb->add_database(array( - "host" => DB_HOST, - "user" => DB_USER, - "password" => DB_PASSWORD, - "name" => DB_NAME, - "dataset" => "global", - "write" => $is_master, "read" => 1, "timeout" => 2 - )); - -// This is the write-only master. -if (!$is_master) { - $wpdb->add_database(noblogs_load_master(NOBLOGS_MASTER_CONFIG)); -} - +// Set up global dataset with master databases +noblogs_load_global_dataset(NOBLOGS_MASTER_CONFIG, NOBLOGS_HOST_FILE); // Add all the sharded blog databases. $wpdb_reverse_backend_map = noblogs_load_backends(NOBLOGS_BACKEND_CONFIG, $wpdb->hash_map); - -// Ai patch: allows to understand if the current database is the master server -function __gf_ai_is_master() { - $master_dsn = trim(file_get_contents('/etc/noblogs/master')); - preg_match('/@172.16.1.(\d+):/', $master_dsn, $m); - $master_id = $m[1]; - if (!empty($_SERVER['SERVER_ADDR'])) - return (int) ('172.16.1.'.$master_id == $_SERVER['SERVER_ADDR']);//works on web - return (int) ($master_id == $_SERVER['SERVER_PUBLIC_ID']);//works on cli -} diff --git a/r2db/db-backends.php b/r2db/db-backends.php index 7cd26af0f185bb4d110e1eda23cc39691f10f551..44180a2ef4dccecf013e82f9301949b8e5d23949 100644 --- a/r2db/db-backends.php +++ b/r2db/db-backends.php @@ -1,5 +1,10 @@ <?php +function noblogs_split_db($file) { + $db_url = trim(file_get_contents($file)); + return parse_url($db_url); +} + function noblogs_load_backends($db_config_file, $hashptr) { global $wpdb; @@ -35,16 +40,26 @@ function noblogs_load_backends($db_config_file, $hashptr) { return $backend_map; } -function noblogs_load_master($master_file) { - $master_url = trim(file_get_contents($master_file)); - $mdata = parse_url($master_url); - return array( - "host" => $mdata["host"] . ":" . $mdata["port"], - "user" => $mdata["user"], - "password" => $mdata["pass"], - "name" => substr($mdata["path"], 1), - "dataset" => "global", - "read" => 0, "write" => 1, "timeout" => 10 - ); +function noblogs_load_global_dataset($master_file, $ip_file) { + global $wpdb; + $mdata = noblogs_split_db($master_file); + $ldata = trim(file_get_contents($ip_file)); + $globaldb = array( + "host" => $mdata["host"] . ":" . $mdata["port"], + "user" => $mdata["user"], + "password" => $mdata["pass"], + "name" => substr($mdata["path"], 1), + "dataset" => "global", + "read" => 1, "write" => 1, "timeout" => 2 + ); + if ($mdata['host'] == $ldata) { + $wpdb->add_database($globaldb); + } else { + $globaldb['read'] = 0; + $wpdb->add_database($globaldb); + $globaldb['host'] = $ldata; + $globaldb['read'] = 1; + $globaldb['write'] = 0; + $wpdb->add_database($globaldb); + } } -