* @ingroup Database
*/
abstract class LBFactory implements DestructibleService {
-
/** @var ChronologyProtector */
protected $chronProt;
-
/** @var TransactionProfiler */
protected $trxProfiler;
-
/** @var LoggerInterface */
- protected $logger;
+ protected $trxLogger;
+ /** @var BagOStuff */
+ protected $srvCache;
+ /** @var WANObjectCache */
+ protected $wanCache;
/** @var string|bool Reason all LBs are read-only or false if not */
protected $readOnlyReason = false;
/**
* Construct a factory based on a configuration array (typically from $wgLBFactoryConf)
* @param array $conf
+ * @TODO: inject objects via dependency framework
*/
public function __construct( array $conf ) {
if ( isset( $conf['readOnlyReason'] ) && is_string( $conf['readOnlyReason'] ) ) {
$this->readOnlyReason = $conf['readOnlyReason'];
}
-
$this->chronProt = $this->newChronologyProtector();
$this->trxProfiler = Profiler::instance()->getTransactionProfiler();
- $this->logger = LoggerFactory::getInstance( 'DBTransaction' );
+ // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
+ $cache = ObjectCache::getLocalServerInstance();
+ if ( $cache->getQoS( $cache::ATTR_EMULATION ) > $cache::QOS_EMULATION_SQL ) {
+ $this->srvCache = $cache;
+ } else {
+ $this->srvCache = new EmptyBagOStuff();
+ }
+ $wCache = ObjectCache::getMainWANInstance();
+ if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
+ $this->wanCache = $wCache;
+ } else {
+ $this->wanCache = WANObjectCache::newEmpty();
+ }
+ $this->trxLogger = LoggerFactory::getInstance( 'DBTransaction' );
}
/**
foreach ( $callersByDB as $db => $callers ) {
$msg .= "$db: " . implode( '; ', $callers ) . "\n";
}
- $this->logger->info( $msg );
+ $this->trxLogger->info( $msg );
}
}
'servers' => $this->makeServerArray( $template, $loads, $groupLoads ),
'loadMonitor' => $this->loadMonitorClass,
'readOnlyReason' => $readOnlyReason,
- 'trxProfiler' => $this->trxProfiler
+ 'trxProfiler' => $this->trxProfiler,
+ 'srvCache' => $this->srvCache,
+ 'wanCache' => $this->wanCache
] );
}
] ];
}
- return new LoadBalancer( [
- 'servers' => $servers,
- 'loadMonitor' => $this->loadMonitorClass,
- 'readOnlyReason' => $this->readOnlyReason,
- 'trxProfiler' => $this->trxProfiler
- ] );
+ return $this->newLoadBalancer( $servers );
}
/**
throw new MWException( __METHOD__ . ": Unknown cluster \"$cluster\"" );
}
- return new LoadBalancer( [
- 'servers' => $wgExternalServers[$cluster],
- 'loadMonitor' => $this->loadMonitorClass,
- 'readOnlyReason' => $this->readOnlyReason,
- 'trxProfiler' => $this->trxProfiler
- ] );
+ return $this->newLoadBalancer( $wgExternalServers[$cluster] );
}
/**
return $this->extLBs[$cluster];
}
+ private function newLoadBalancer( array $servers ) {
+ return new LoadBalancer( [
+ 'servers' => $servers,
+ 'loadMonitor' => $this->loadMonitorClass,
+ 'readOnlyReason' => $this->readOnlyReason,
+ 'trxProfiler' => $this->trxProfiler,
+ 'srvCache' => $this->srvCache,
+ 'wanCache' => $this->wanCache
+ ] );
+ }
+
/**
* Execute a function for each tracked load balancer
* The callback is called with the load balancer as the first parameter,
$this->lb = new LoadBalancerSingle( [
'readOnlyReason' => $this->readOnlyReason,
- 'trxProfiler' => $this->trxProfiler
+ 'trxProfiler' => $this->trxProfiler,
+ 'srvCache' => $this->srvCache,
+ 'wanCache' => $this->wanCache
] + $conf );
}
'load' => 1,
]
],
- 'trxProfiler' => $this->trxProfiler
+ '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'] ) ) {
* - servers : Required. Array of server info structures.
* - loadMonitor : Name of a class used to fetch server lag and load.
* - readOnlyReason : Reason the master DB is read-only if so [optional]
+ * - srvCache : BagOStuff object [optional]
+ * - wanCache : WANObjectCache object [optional]
* @throws MWException
*/
public function __construct( array $params ) {
}
}
- // Use APC/memcached style caching, but avoids loops with CACHE_DB (T141804)
- // @TODO: inject these in via LBFactory at some point
- $cache = ObjectCache::getLocalServerInstance();
- if ( $cache->getQoS( $cache::ATTR_EMULATION ) > $cache::QOS_EMULATION_SQL ) {
- $this->srvCache = $cache;
+ if ( isset( $params['srvCache'] ) ) {
+ $this->srvCache = $params['srvCache'];
} else {
$this->srvCache = new EmptyBagOStuff();
}
- $wCache = ObjectCache::getMainWANInstance();
- if ( $wCache->getQoS( $wCache::ATTR_EMULATION ) > $wCache::QOS_EMULATION_SQL ) {
- $this->wanCache = $wCache;
+ if ( isset( $params['wanCache'] ) ) {
+ $this->wanCache = $params['wanCache'];
} else {
$this->wanCache = WANObjectCache::newEmpty();
}
-
if ( isset( $params['trxProfiler'] ) ) {
$this->trxProfiler = $params['trxProfiler'];
} else {