Commit 0b7b389d authored by root's avatar root Committed by lechuck

a few fixes to the global wpdb_hash instance (moved into $wpdb)

parent b32dd9d8
<?php <?php
// Common settings
$wpdb->save_queries = false;
$wpdb->persistent = false;
$wpdb->max_connections = 30;
define("NOBLOGS_BACKEND_CONFIG", "/etc/noblogs/backends"); define("NOBLOGS_BACKEND_CONFIG", "/etc/noblogs/backends");
require_once('r2db/db-hash.php'); include_once('r2db/db-hash.php');
require_once('r2db/db-backends.php'); include_once('r2db/db-backends.php');
// Add the global database (configured in wp-config.php), stores the global // Add the global database (configured in wp-config.php), stores the global
// blogs and users tables. // blogs and users tables.
...@@ -16,5 +21,5 @@ $wpdb->add_database(array( ...@@ -16,5 +21,5 @@ $wpdb->add_database(array(
"write" => 1, "read" => 1, "timeout" => 2 "write" => 1, "read" => 1, "timeout" => 2
)); ));
$wpdb_reverse_backend_map = noblogs_load_backends(NOBLOGS_BACKEND_CONFIG, $wpdb_hash); $wpdb_reverse_backend_map = noblogs_load_backends(NOBLOGS_BACKEND_CONFIG, $wpdb->hash_map);
<?php <?php
function noblogs_load_backends($db_config_file, &$hashptr) { function noblogs_load_backends($db_config_file, $hashptr) {
global $wpdb; global $wpdb;
$backend_map = array(); $backend_map = array();
......
<?php <?php
require_once('flexihash.php'); require_once(dirname(__FILE__) . '/flexihash.php');
$wpdb_hash = new Flexihash(); $wpdb->hash_map = new Flexihash();
/* Hashing function to map blogs to databases. /* Hashing function to map blogs to databases.
* *
* Implements a consistent hashing scheme using Flexihash. * Implements a consistent hashing scheme using Flexihash.
*/ */
function noblogs_db_callback($query, $wpdb) { function noblogs_db_callback($query, $wpdb) {
global $wpdb_hash; $wpdb_hash = $wpdb->hash_map;
if (preg_match("/^{$wpdb->base_prefix}(\d+)_/", $wpdb->table, $matches)) { if (preg_match("/^{$wpdb->base_prefix}(\d+)_/", $wpdb->table, $matches)) {
$blog_id = $matches[1]; $blog_id = $matches[1];
return $wpdb_hash->lookup($blog_id); return $wpdb_hash->lookup($blog_id);
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
define('WP_CACHE',false); define('WP_CACHE',false);
/** Setup WordPress environment */ /** Setup WordPress environment */
require_once('wp-load.php'); require_once('wp-load.php');
require_once('db-config-new.php'); require_once('db-config.php');
function old_hash($dbid) { function old_hash($dbid) {
...@@ -24,11 +24,10 @@ function old_hash($dbid) { ...@@ -24,11 +24,10 @@ function old_hash($dbid) {
} }
} }
function new_hash($dbid) { function new_hash($dbid, $reversemap) {
global $wpdb_hash; global $wpdb;
global $wpdb_reverse_backend_map; $lookup = $wpdb->hash_map->lookup($dbid);
$lookup = $wpdb_hash->lookup($dbid); $backend = $reversemap[$lookup];
$backend = $wpdb_reverse_backend_map[$lookup];
$result = array(); $result = array();
if (preg_match('/^(.*):([0-9]*)$/', $backend['host'], $matches)) { if (preg_match('/^(.*):([0-9]*)$/', $backend['host'], $matches)) {
$result['host'] = $matches[1]; $result['host'] = $matches[1];
...@@ -54,11 +53,15 @@ function get_all_blogs() ...@@ -54,11 +53,15 @@ function get_all_blogs()
{ {
global $wpdb; global $wpdb;
$sql = "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE public = 1 AND deleted = 0 AND archived = '0' ORDER BY domain, path"; // $sql = "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE public = 1 AND deleted = 0 AND archived = '0' ORDER BY domain, path";
$sql = "SELECT blog_id, domain, path FROM $wpdb->blogs WHERE public = 0 ORDER BY domain, path";
$result = $wpdb->get_results($sql); $result = $wpdb->get_results($sql);
return ($result); return ($result);
} }
$hashmap = new Flexihash();
$reverse_backend_map = noblogs_load_backends(NOBLOGS_BACKEND_CONFIG, $hashmap);
$new_counts = array(); $new_counts = array();
$moved_count = 0; $moved_count = 0;
$blogs = get_all_blogs(); $blogs = get_all_blogs();
...@@ -67,7 +70,7 @@ foreach ($blogs as $blog) { ...@@ -67,7 +70,7 @@ foreach ($blogs as $blog) {
$old_params = old_hash($blog_id); $old_params = old_hash($blog_id);
$old_dburi = mysqlurl($old_params); $old_dburi = mysqlurl($old_params);
$new_params = new_hash($blog_id); $new_params = new_hash($blog_id, $reverse_backend_map);
$new_dburi = mysqlurl($new_params); $new_dburi = mysqlurl($new_params);
if ($new_counts[$new_params['host']]) { if ($new_counts[$new_params['host']]) {
......
...@@ -40,6 +40,13 @@ define( 'HYPERDB_LAG_BEHIND', 2 ); ...@@ -40,6 +40,13 @@ define( 'HYPERDB_LAG_BEHIND', 2 );
define( 'HYPERDB_LAG_UNKNOWN', 3 ); define( 'HYPERDB_LAG_UNKNOWN', 3 );
class hyperdb extends wpdb { class hyperdb extends wpdb {
/**
* A/I patch!
* store a FlexiHash() instance here.
*/
var $hash_map;
/** /**
* The last table that was queried * The last table that was queried
* @var string * @var string
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment