Skip to content
Snippets Groups Projects
Commit 5b798c39 authored by ale's avatar ale
Browse files

Update r2db with the most recent code

The configuration has been refactored to be a JSON file. This is
the codebase that has been running in production for the last 3
years, from ai3/docker/noblogs.
parent 6db8ed54
No related branches found
No related tags found
No related merge requests found
<?php <?php
function noblogs_split_db($file) { function noblogs_load_backends($hashptr) {
$db_url = trim(file_get_contents($file));
return parse_url($db_url);
}
function noblogs_load_backends($db_config_file, $hashptr) {
global $wpdb; global $wpdb;
global $noblogs_config;
$backend_map = array(); $backend_map = array();
foreach ($noblogs_config['db_config']['backends'] as $backend_name => $backend) {
$fp = @fopen($db_config_file, "r"); $b = array(
if (!$fp) { "host" => $backend['host'] . ":" . $backend['port'],
die("Database backends not configured!"); "user" => $backend['user'],
} "password" => $backend['password'],
while (($line = fgets($fp, 1024)) !== false) { "name" => $backend['name'],
$wline = rtrim($line); "dataset" => $backend_name,
if ($wline == "" || $wline[0] == '#') { "read" => 1,
continue; "write" => 1,
} "timeout" => 10
$line_parts = explode(" ", $wline); );
$server_id = $line_parts[0]; $wpdb->add_database($b);
$dataset = "backend_" . $server_id; $hashptr->addTarget($backend_name);
$backend_url = $line_parts[2]; $backend_map[$backend_name] = $b;
$backend_url_data = parse_url($backend_url);
$backend = array(
"host" => $backend_url_data["host"] . ":" . $backend_url_data["port"],
"user" => $backend_url_data["user"],
"password" => $backend_url_data["pass"],
"name" => substr($backend_url_data["path"], 1),
"dataset" => $dataset,
"read" => 1, "write" => 1, "timeout" => 10
);
$wpdb->add_database($backend);
$hashptr->addTarget($dataset);
$backend_map[$dataset] = $backend;
} }
fclose($fp);
return $backend_map; return $backend_map;
} }
function noblogs_load_global_dataset($master_file, $ip_file) { function noblogs_load_global_dataset() {
global $wpdb; global $wpdb;
$mdata = noblogs_split_db($master_file); global $noblogs_config;
$ldata = trim(file_get_contents($ip_file)); $master = $noblogs_config['db_config']['master'];
$globaldb = array( if ($noblogs_config['db_config']['is_master']) {
"host" => $mdata["host"] . ":" . $mdata["port"], $wpdb->add_database(array(
"user" => $mdata["user"], "host" => "127.0.0.1:" . $master['port'],
"password" => $mdata["pass"], "user" => $master['user'],
"name" => substr($mdata["path"], 1), "password" => $master['password'],
"dataset" => "global", "name" => $master['name'],
"read" => 1, "write" => 1, "timeout" => 2 "dataset" => "global",
); "read" => 1,
if ($mdata['host'] == $ldata) { "write" => 1,
$wpdb->add_database($globaldb); "timeout" => 2
} else { ));
$globaldb['read'] = 0; } else {
$wpdb->add_database($globaldb); $wpdb->add_database(array(
$globaldb['host'] = $ldata . ":" . $mdata["port"]; "host" => $master['host'] . ":" . $master['port'],
$globaldb['read'] = 1; "user" => $master['user'],
$globaldb['write'] = 0; "password" => $master['password'],
$wpdb->add_database($globaldb); "name" => $master['name'],
} "dataset" => "global",
"read" => 0,
"write" => 1,
"timeout" => 2
));
$wpdb->add_database(array(
"host" => "127.0.0.1:" . $master['port'],
"user" => $master['user'],
"password" => $master['password'],
"name" => $master['name'],
"dataset" => "global",
"read" => 1,
"write" => 0,
"timeout" => 2
));
}
} }
...@@ -8,11 +8,11 @@ $wpdb->max_connections = 30; ...@@ -8,11 +8,11 @@ $wpdb->max_connections = 30;
connection errors for the wrong implementation */ connection errors for the wrong implementation */
$wpdb->check_tcp_responsiveness = false; $wpdb->check_tcp_responsiveness = false;
include_once('../r2db/db-hash.php'); include_once('r2db/db-hash.php');
include_once('../r2db/db-backends.php'); include_once('r2db/db-backends.php');
// Set up global dataset with master databases // Set up global dataset with master databases
noblogs_load_global_dataset(NOBLOGS_MASTER_CONFIG, NOBLOGS_HOST_FILE); noblogs_load_global_dataset();
// Add all the sharded blog databases. // Add all the sharded blog databases.
$wpdb_reverse_backend_map = noblogs_load_backends(NOBLOGS_BACKEND_CONFIG, $wpdb->hash_map); $wpdb_reverse_backend_map = noblogs_load_backends($wpdb->hash_map);
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment