db-config.php 2.76 KB
Newer Older
lechuck's avatar
lechuck committed
1 2
<?php

Joe's avatar
Joe committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
function noblogs_split_db($file) {
    $db_url = trim(file_get_contents($file));
    return parse_url($db_url);
}


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
    );

	// On the master, just add it locally
    if ($mdata['host'] == $ldata) {
        $wpdb->add_database($globaldb);
    } else {
		// On the slaves, add the master as write-only, the local replicas
		// as read-only.
        $globaldb['read'] = 0;
        $wpdb->add_database($globaldb);
        $globaldb['host'] = $ldata . ":" . $mdata["port"];
        $globaldb['read'] = 1;
        $globaldb['write'] = 0;
        $wpdb->add_database($globaldb);
    }
}

function noblogs_dataset($query, $wpdb) {
	if (preg_match("/^{$wpdb->base_prefix}(\d+)_/", $wpdb->table, $matches)) {
		return "local";
	}
	return "global";
}

/**
 * AI custom: Logs all queries for debugging purposes
 */
function ai_log_db_queries($query, $time, $backtrace=null, hyperdb $obj ) {
    $fh = fopen( ABSPATH . '/profiling/noblogs_queries_'. date('Ymd') . '.log', 'a');
    if (!$fh) {
        return array($query, $time, $backtrace);
    }
    fwrite($fh, sprintf("##\n#Date: %s\n#Query time: %s\n%s\n", date('r'), $time, $query));
    fclose($fh);
    return array($query, $time, $backtrace);
}
56

Joe's avatar
Joe committed
57
/* Disable responsiveness check, is not needed in our configuration and is causing
58 59 60
 connection errors for the wrong implementation */
$wpdb->check_tcp_responsiveness = false;

61
// Set up global dataset with master databases
joe's avatar
joe committed
62
noblogs_load_global_dataset(NOBLOGS_MASTER_CONFIG, NOBLOGS_HOST_FILE);
63

Joe's avatar
Joe committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
// Add all the blog databases. Note that if the partition function sends
// traffic to the wrong backend, the backend will not be able to route db requests
// which is acceptable since we already assume this in a thousand places.
$localdb = array(
	"host" => "localhost",
	"port" => "3307", # TODO: check if this works on staging?
	"user" => NOBLOGS_LOCAL_DB_USER,
	"password" => NOBLOGS_LOCAL_DB_PASSWORD,
	"name"  => "noblogs_local",
	"dataset" => "local",
	"read" => 1,
	"write" => 1,
	"timeout" => 10
);

$wpdb->add_database($localdb);

// Now add a simple callback that will allow us to choose between the
// local and global datasets
$wpdb->add_callback('noblogs_dataset');

// Common settings
$wpdb->persistent = true;
$wpdb->max_connections = 30;

if ( AI_DB_PROFILER === true ) {
    $wpdb->save_queries = true;
    $wpdb->save_query_callback = 'ai_log_db_queries';
}