本文整理汇总了PHP中LoadBalancer类的典型用法代码示例。如果您正苦于以下问题:PHP LoadBalancer类的具体用法?PHP LoadBalancer怎么用?PHP LoadBalancer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了LoadBalancer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getMainLB
/**
* @param bool|string $wiki
* @return LoadBalancer
*/
public function getMainLB($wiki = false)
{
if (!isset($this->mainLB)) {
$this->mainLB = $this->newMainLB($wiki);
$this->mainLB->parentInfo(array('id' => 'main'));
$this->chronProt->initLB($this->mainLB);
}
return $this->mainLB;
}
示例2:
/**
* Fetch data from given URL
* @param string $url An url
*/
function &getLoadBalancer($cluster)
{
global $wgExternalServers;
if (!array_key_exists($cluster, $this->loadBalancers)) {
$this->loadBalancers[$cluster] = LoadBalancer::newFromParams($wgExternalServers[$cluster]);
}
return $this->loadBalancers[$cluster];
}
示例3:
/** @todo Document.*/
function &getLoadBalancer($cluster)
{
global $wgExternalServers, $wgExternalLoadBalancers;
if (!array_key_exists($cluster, $wgExternalLoadBalancers)) {
$wgExternalLoadBalancers[$cluster] = LoadBalancer::newFromParams($wgExternalServers[$cluster]);
}
$wgExternalLoadBalancers[$cluster]->allowLagged(true);
return $wgExternalLoadBalancers[$cluster];
}
示例4: clear
/**
* Clears the list of sites stored in the database.
*
* @see SiteStore::clear()
*
* @return bool Success
*/
public function clear()
{
$dbw = $this->dbLoadBalancer->getConnection(DB_MASTER);
$dbw->startAtomic(__METHOD__);
$ok = $dbw->delete('sites', '*', __METHOD__);
$ok = $dbw->delete('site_identifiers', '*', __METHOD__) && $ok;
$dbw->endAtomic(__METHOD__);
$this->reset();
return $ok;
}
示例5: shutdownLB
/**
* Notify the ChronologyProtector that the LoadBalancer is about to shut
* down. Saves replication positions.
*
* @param LoadBalancer $lb
* @return void
*/
public function shutdownLB(LoadBalancer $lb)
{
if (session_id() == '' || $lb->getServerCount() <= 1) {
return;
// don't start a session; don't bother with non-replicated setups
}
$masterName = $lb->getServerName(0);
if (isset($this->shutdownPositions[$masterName])) {
return;
// already done
}
// Only save the position if writes have been done on the connection
$db = $lb->getAnyOpenConnection(0);
$info = $lb->parentInfo();
if (!$db || !$db->doneWrites()) {
wfDebug(__METHOD__ . ": LB {$info['id']}, no writes done\n");
return;
}
$pos = $db->getMasterPos();
wfDebug(__METHOD__ . ": LB {$info['id']} has master pos {$pos}\n");
$this->shutdownPositions[$masterName] = $pos;
}
示例6: getDB
/**
* @return DatabaseBase
*/
protected function getDB()
{
if (!isset($this->db)) {
# If server connection info was given, use that
if ($this->serverInfo) {
$this->lb = new LoadBalancer(array('servers' => array($this->serverInfo)));
$this->db = $this->lb->getConnection(DB_MASTER);
$this->db->clearFlag(DBO_TRX);
} else {
# We must keep a separate connection to MySQL in order to avoid deadlocks
# However, SQLite has an opposite behaviour.
# @todo Investigate behaviour for other databases
if (wfGetDB(DB_MASTER)->getType() == 'sqlite') {
$this->db = wfGetDB(DB_MASTER);
} else {
$this->lb = wfGetLBFactory()->newMainLB();
$this->db = $this->lb->getConnection(DB_MASTER);
$this->db->clearFlag(DBO_TRX);
}
}
}
return $this->db;
}
示例7: fetchFromURL
/**
* Fetch data from given URL
* @param string $url An url
*/
function fetchFromURL($url)
{
global $wgExternalServers;
#
# URLs have the form DB://cluster/id, e.g.
# DB://cluster1/3298247
#
$path = explode('/', $url);
$cluster = $path[2];
$id = $path[3];
$lb = LoadBalancer::NewFromParams($wgExternalServers[$cluster]);
$db = $lb->getConnection(DB_SLAVE);
$ret = $db->selectField('blobs', 'blob_text', array('blob_id' => $id));
return $ret;
}
示例8: getDB
/**
* @return DatabaseBase
*/
protected function getDB()
{
global $wgDebugDBTransactions;
# Don't keep timing out trying to connect for each call if the DB is down
if ($this->connFailureError && time() - $this->connFailureTime < 60) {
throw $this->connFailureError;
}
if (!isset($this->db)) {
# If server connection info was given, use that
if ($this->serverInfo) {
if ($wgDebugDBTransactions) {
wfDebug(sprintf("Using provided serverInfo for SqlBagOStuff\n"));
}
$this->lb = new LoadBalancer(array('servers' => array($this->serverInfo)));
$this->db = $this->lb->getConnection(DB_MASTER);
$this->db->clearFlag(DBO_TRX);
} else {
/*
* We must keep a separate connection to MySQL in order to avoid deadlocks
* However, SQLite has an opposite behaviour. And PostgreSQL needs to know
* if we are in transaction or no
*/
if (wfGetDB(DB_MASTER)->getType() == 'mysql') {
$this->lb = wfGetLBFactory()->newMainLB();
$this->db = $this->lb->getConnection(DB_MASTER);
$this->db->clearFlag(DBO_TRX);
// auto-commit mode
} else {
$this->db = wfGetDB(DB_MASTER);
}
}
if ($wgDebugDBTransactions) {
wfDebug(sprintf("Connection %s will be used for SqlBagOStuff\n", $this->db));
}
}
return $this->db;
}
示例9: __construct
/**
* builds a new Node object
*
* @param LoadBalancer $lb the parent LB object
* @param mixed $info either an ID or an array of values
* @returns void
*/
public function __construct(LoadBalancer $lb, $info = NULL)
{
$this->_lb = $lb;
parent::__construct($lb->Service(), $info);
}
示例10: changeLBPrefix
/**
* @param LoadBalancer $lb
* @param string $prefix
* @return void
*/
public static function changeLBPrefix($lb, $prefix)
{
$lb->forEachOpenConnection(array('CloneDatabase', 'changeDBPrefix'), array($prefix));
}
示例11: getLagTimes
public function getLagTimes($serverIndexes, $wiki)
{
if (count($serverIndexes) == 1 && reset($serverIndexes) == 0) {
// Single server only, just return zero without caching
return array(0 => 0);
}
$expiry = 5;
$requestRate = 10;
$cache = $this->cache;
$masterName = $this->parent->getServerName(0);
$memcKey = wfMemcKey('lag_times', $masterName);
$times = $cache->get($memcKey);
if (is_array($times)) {
# Randomly recache with probability rising over $expiry
$elapsed = time() - $times['timestamp'];
$chance = max(0, ($expiry - $elapsed) * $requestRate);
if (mt_rand(0, $chance) != 0) {
unset($times['timestamp']);
// hide from caller
return $times;
}
wfIncrStats('lag_cache.miss.expired');
} else {
wfIncrStats('lag_cache.miss.absent');
}
# Cache key missing or expired
if ($cache->lock($memcKey, 0, 10)) {
# Let this process alone update the cache value
$unlocker = new ScopedCallback(function () use($cache, $memcKey) {
$cache->unlock($memcKey);
});
} elseif (is_array($times)) {
# Could not acquire lock but an old cache exists, so use it
unset($times['timestamp']);
// hide from caller
return $times;
}
$times = array();
foreach ($serverIndexes as $i) {
if ($i == 0) {
# Master
$times[$i] = 0;
} elseif (false !== ($conn = $this->parent->getAnyOpenConnection($i))) {
$times[$i] = $conn->getLag();
} elseif (false !== ($conn = $this->parent->openConnection($i, $wiki))) {
$times[$i] = $conn->getLag();
// Close the connection to avoid sleeper connections piling up.
// Note that the caller will pick one of these DBs and reconnect,
// which is slightly inefficient, but this only matters for the lag
// time cache miss cache, which is far less common that cache hits.
$this->parent->closeConnection($conn);
}
}
# Add a timestamp key so we know when it was cached
$times['timestamp'] = time();
$cache->set($memcKey, $times, $expiry + 10);
unset($times['timestamp']);
// hide from caller
return $times;
}
示例12: getLagTimes
public function getLagTimes($serverIndexes, $wiki)
{
if (count($serverIndexes) == 1 && reset($serverIndexes) == 0) {
// Single server only, just return zero without caching
return array(0 => 0);
}
$section = new ProfileSection(__METHOD__);
$expiry = 5;
$requestRate = 10;
global $wgMemc;
if (empty($wgMemc)) {
$wgMemc = wfGetMainCache();
}
$masterName = $this->parent->getServerName(0);
$memcKey = wfMemcKey('lag_times', $masterName);
$times = $wgMemc->get($memcKey);
if (is_array($times)) {
# Randomly recache with probability rising over $expiry
$elapsed = time() - $times['timestamp'];
$chance = max(0, ($expiry - $elapsed) * $requestRate);
if (mt_rand(0, $chance) != 0) {
unset($times['timestamp']);
// hide from caller
return $times;
}
wfIncrStats('lag_cache_miss_expired');
} else {
wfIncrStats('lag_cache_miss_absent');
}
# Cache key missing or expired
if ($wgMemc->add("{$memcKey}:lock", 1, 10)) {
# Let this process alone update the cache value
$unlocker = new ScopedCallback(function () use($wgMemc, $memcKey) {
$wgMemc->delete($memcKey);
});
} elseif (is_array($times)) {
# Could not acquire lock but an old cache exists, so use it
unset($times['timestamp']);
// hide from caller
return $times;
}
$times = array();
foreach ($serverIndexes as $i) {
if ($i == 0) {
# Master
$times[$i] = 0;
} elseif (false !== ($conn = $this->parent->getAnyOpenConnection($i))) {
$times[$i] = $conn->getLag();
} elseif (false !== ($conn = $this->parent->openConnection($i, $wiki))) {
$times[$i] = $conn->getLag();
}
}
# Add a timestamp key so we know when it was cached
$times['timestamp'] = time();
$wgMemc->set($memcKey, $times, $expiry + 10);
unset($times['timestamp']);
// hide from caller
return $times;
}
示例13: __construct
/**
* @param array $params An associative array with one member:
* - connection: An IDatabase connection object
*/
public function __construct(array $params)
{
if (!isset($params['connection'])) {
throw new InvalidArgumentException("Missing 'connection' argument.");
}
$this->db = $params['connection'];
parent::__construct(['servers' => [['type' => $this->db->getType(), 'host' => $this->db->getServer(), 'dbname' => $this->db->getDBname(), 'load' => 1]], 'trxProfiler' => isset($params['trxProfiler']) ? $params['trxProfiler'] : null, 'srvCache' => isset($params['srvCache']) ? $params['srvCache'] : null, 'wanCache' => isset($params['wanCache']) ? $params['wanCache'] : null]);
if (isset($params['readOnlyReason'])) {
$this->db->setLBInfo('readOnlyReason', $params['readOnlyReason']);
}
}
示例14: getLagTimes
/**
* @param $serverIndexes
* @param $wiki
* @return array
*/
function getLagTimes($serverIndexes, $wiki)
{
if (count($serverIndexes) == 1 && reset($serverIndexes) == 0) {
// Single server only, just return zero without caching
return array(0 => 0);
}
wfProfileIn(__METHOD__);
$expiry = 5;
$requestRate = 10;
global $wgMemc;
if (empty($wgMemc)) {
$wgMemc = wfGetMainCache();
}
$masterName = $this->parent->getServerName(0);
$memcKey = wfMemcKey('lag_times', $masterName);
$times = $wgMemc->get($memcKey);
if ($times) {
# Randomly recache with probability rising over $expiry
$elapsed = time() - $times['timestamp'];
$chance = max(0, ($expiry - $elapsed) * $requestRate);
if (mt_rand(0, $chance) != 0) {
unset($times['timestamp']);
wfProfileOut(__METHOD__);
return $times;
}
wfIncrStats('lag_cache_miss_expired');
} else {
wfIncrStats('lag_cache_miss_absent');
}
# Cache key missing or expired
$times = array();
foreach ($serverIndexes as $i) {
if ($i == 0) {
# Master
$times[$i] = 0;
} elseif (false !== ($conn = $this->parent->getAnyOpenConnection($i))) {
$times[$i] = $conn->getLag();
} elseif (false !== ($conn = $this->parent->openConnection($i, $wiki))) {
$times[$i] = $conn->getLag();
}
}
# Add a timestamp key so we know when it was cached
$times['timestamp'] = time();
$wgMemc->set($memcKey, $times, $expiry);
# But don't give the timestamp to the caller
unset($times['timestamp']);
$lagTimes = $times;
wfProfileOut(__METHOD__);
return $lagTimes;
}
示例15: getDB
/**
* Get a connection to the specified database
*
* @param $serverIndex integer
* @return DatabaseBase
*/
protected function getDB( $serverIndex ) {
global $wgDebugDBTransactions;
if ( !isset( $this->conns[$serverIndex] ) ) {
if ( $serverIndex >= $this->numServers ) {
throw new MWException( __METHOD__ . ": Invalid server index \"$serverIndex\"" );
}
# Don't keep timing out trying to connect for each call if the DB is down
if ( isset( $this->connFailureErrors[$serverIndex] )
&& ( time() - $this->connFailureTimes[$serverIndex] ) < 60 )
{
throw $this->connFailureErrors[$serverIndex];
}
# If server connection info was given, use that
if ( $this->serverInfos ) {
if ( $wgDebugDBTransactions ) {
wfDebug( "Using provided serverInfo for SqlBagOStuff\n" );
}
$info = $this->serverInfos[$serverIndex];
$type = isset( $info['type'] ) ? $info['type'] : 'mysql';
$host = isset( $info['host'] ) ? $info['host'] : '[unknown]';
wfDebug( __CLASS__ . ": connecting to $host\n" );
$db = DatabaseBase::factory( $type, $info );
$db->clearFlag( DBO_TRX );
} else {
/*
* We must keep a separate connection to MySQL in order to avoid deadlocks
* However, SQLite has an opposite behavior. And PostgreSQL needs to know
* if we are in transaction or no
*/
if ( wfGetDB( DB_MASTER )->getType() == 'mysql' ) {
$this->lb = wfGetLBFactory()->newMainLB();
$db = $this->lb->getConnection( DB_MASTER );
$db->clearFlag( DBO_TRX ); // auto-commit mode
} else {
$db = wfGetDB( DB_MASTER );
}
}
if ( $wgDebugDBTransactions ) {
wfDebug( sprintf( "Connection %s will be used for SqlBagOStuff\n", $db ) );
}
$this->conns[$serverIndex] = $db;
}
return $this->conns[$serverIndex];
}