示例#1
0
/**
 * Returns a random dbid from the specified dbtype pool, or pools
 *
 * @param mixed $db_type could either be on specific type as in most use cases, or multiple parameters each denoting a type
 * @return int
 * @author mcslee
 */
function get_rand_db($db_type)
{
    if (func_num_args() > 1) {
        $args = func_get_args();
        $sizes = array();
        $total = 0;
        global $DBTYPE_CONFIG;
        foreach ($args as $index => $db_pool_type) {
            if (!isset($DBTYPE_CONFIG[$db_pool_type])) {
                debug_rlog('FBID: invalid dbtype to get_rand_db: ' . $db_pool_type);
                return null;
            }
            $total += $DBTYPE_CONFIG[$db_pool_type]['count'];
            $sizes[$db_pool_type] = $total;
        }
        $index = mt_rand(1, $total);
        foreach ($sizes as $pool => $size) {
            if ($index <= $size) {
                $db_type = $pool;
                break;
            }
        }
    }
    if (!isset($DBTYPE_CONFIG[$db_type])) {
        debug_rlog('FBID: invalid dbtype to get_rand_db: ' . $db_type);
        return null;
    }
    $config = $DBTYPE_CONFIG[$db_type];
    $min = $config['min_dbid'];
    $max = $config['max_dbid'];
    for ($i = 0; $i < 10; ++$i) {
        $dbid = mt_rand($min, $max);
        if (!check_down($dbid)) {
            break;
            // exit the loop if this connection is good
        }
    }
    // On QA DB, only use 1 out of 100 object DBs
    if (is_qa_server()) {
        $dbid = 100 * (int) ($dbid / 100);
    }
    return $dbid;
}
示例#2
0
<?php

debug_rlog(1 + 2, 0);
debug_rlog(1 + 2, 1);