Skip to content
Snippets Groups Projects
Commit 896d2b55 authored by lechuck's avatar lechuck Committed by agata
Browse files

Moved mysql host to tcp and function refactoring

parent 1251552c
Branches
Tags
1 merge request!3Noblogs 5.4
......@@ -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
}
<?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);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment