diff --git a/db-config.php b/db-config.php index 88f67a32284dea7459d4ea504cf9176efda3e442..b82eaaa8f623a769815589c4a973f8bb7a084969 100644 --- a/db-config.php +++ b/db-config.php @@ -5,19 +5,26 @@ $wpdb->persistent = true; $wpdb->max_connections = 30; define("NOBLOGS_BACKEND_CONFIG", "/etc/noblogs/backends"); +define("NOBLOGS_MASTER_CONFIG", "/etc/noblogs/master"); 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. + +// This is the read-only slave. $wpdb->add_database(array( "host" => DB_HOST, "user" => DB_USER, "password" => DB_PASSWORD, "name" => DB_NAME, "dataset" => "global", - "write" => 1, "read" => 1, "timeout" => 2 + "write" => 0, "read" => 1, "timeout" => 2 )); +// This is the write-only master. +$wpdb->add_database(noblogs_load_master(NOBLOGS_MASTER_CONFIG)); + +// Add all the sharded blog databases. $wpdb_reverse_backend_map = noblogs_load_backends(NOBLOGS_BACKEND_CONFIG, $wpdb->hash_map); diff --git a/r2db/db-backends.php b/r2db/db-backends.php index 3988f164c98d5e9c18aad30f88220344872840f3..7cd26af0f185bb4d110e1eda23cc39691f10f551 100644 --- a/r2db/db-backends.php +++ b/r2db/db-backends.php @@ -25,7 +25,7 @@ function noblogs_load_backends($db_config_file, $hashptr) { "password" => $backend_url_data["pass"], "name" => substr($backend_url_data["path"], 1), "dataset" => $dataset, - "read" => 1, "write" => 1, "timeout" => 2 + "read" => 1, "write" => 1, "timeout" => 10 ); $wpdb->add_database($backend); $hashptr->addTarget($dataset); @@ -34,3 +34,17 @@ function noblogs_load_backends($db_config_file, $hashptr) { fclose($fp); 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 + ); +} +