Skip to content

Commit

Permalink
qnd: avoid caching full sphinx DSNs in the api cache, cache the kConf…
Browse files Browse the repository at this point in the history
… key instead (sphinx1,2,3..). This is in order to enable the removal of a sphinx server from grid, without having it queried by the cache

git-svn-id: svn+ssh://kelev.kaltura.com/usr/local/kalsource/backend/server/trunk/core@100352 6b8eccd3-e8c5-4e7d-8186-e12b5326b719
  • Loading branch information
erank committed Jun 23, 2013
1 parent 17cb0a8 commit 24c6425
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 19 deletions.
17 changes: 14 additions & 3 deletions alpha/apps/kaltura/lib/cache/kApiCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class kApiCache extends kApiCacheBase
const SUFFIX_RULES = '.rules';
const SUFFIX_LOG = '.log';

const CACHE_VERSION = '3';
const CACHE_VERSION = '4';

// cache modes
const CACHE_MODE_ANONYMOUS = 1; // anonymous caching should be performed - the cached response will not be associated with any conditions
Expand Down Expand Up @@ -374,11 +374,22 @@ protected static function getMaxInvalidationTime($invalidationKeys)

protected function validateSqlQueries($sqlConditions)
{
foreach ($sqlConditions as $dsn => $queries)
if (!$sqlConditions)
return true;

$dataSources = kConf::get('datasources', 'db', null);
if (!$dataSources)
return false;

foreach ($sqlConditions as $configKey => $queries)
{
if (!isset($dataSources[$configKey]['connection']['dsn']))
return false;

try
{
$pdo = new PDO($dsn, null, null, array(PDO::ATTR_TIMEOUT => 1));
$pdo = new PDO(
$dataSources[$configKey]['connection']['dsn'], null, null, array(PDO::ATTR_TIMEOUT => 1));
}
catch(PDOException $e)
{
Expand Down
20 changes: 10 additions & 10 deletions alpha/apps/kaltura/lib/cache/kApiCacheBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class kApiCacheBase

// conditional cache constants
const MAX_INVALIDATION_KEYS = 250;
const MAX_SQL_CONDITION_DSNS = 1; // max number of connections required to verify the SQL conditions
const MAX_SQL_CONDITION_QUERIES = 4; // max number of queries per dsn required to verify the SQL conditions
const MAX_SQL_CONDITION_CONNS = 1; // max number of connections required to verify the SQL conditions
const MAX_SQL_CONDITION_QUERIES = 4; // max number of queries per connection required to verify the SQL conditions

// cache instances
protected $_instanceId = 0;
Expand Down Expand Up @@ -156,31 +156,31 @@ public static function addInvalidationKeys($invalidationKeys, $invalidationTime)
}
}

public static function addSqlQueryCondition($dsn, $sql, $fetchStyle, $columnIndex, $filter, $result)
public static function addSqlQueryCondition($configKey, $sql, $fetchStyle, $columnIndex, $filter, $result)
{
foreach (self::$_activeInstances as $curInstance)
{
if ($curInstance->_cacheStatus == self::CACHE_STATUS_ANONYMOUS_ONLY)
continue;

if (!isset($curInstance->_sqlConditions[$dsn]))
if (!isset($curInstance->_sqlConditions[$configKey]))
{
if (count($curInstance->_sqlConditions) >= self::MAX_SQL_CONDITION_DSNS)
if (count($curInstance->_sqlConditions) >= self::MAX_SQL_CONDITION_CONNS)
{
$curInstance->disableConditionalCacheInternal();
continue;
}

$curInstance->_sqlConditions[$dsn] = array();
$curInstance->_sqlConditions[$configKey] = array();
}

if (count($curInstance->_sqlConditions[$dsn]) >= self::MAX_SQL_CONDITION_QUERIES)
if (count($curInstance->_sqlConditions[$configKey]) >= self::MAX_SQL_CONDITION_QUERIES)
{
$curInstance->disableConditionalCacheInternal();
continue;
}

$curInstance->_sqlConditions[$dsn][] = array(
$curInstance->_sqlConditions[$configKey][] = array(
'sql' => $sql,
'fetchStyle' => $fetchStyle,
'columnIndex' => $columnIndex,
Expand All @@ -191,12 +191,12 @@ public static function addSqlQueryCondition($dsn, $sql, $fetchStyle, $columnInde

protected static function addSqlQueryConditions($sqlConditions)
{
foreach ($sqlConditions as $dsn => $queries)
foreach ($sqlConditions as $configKey => $queries)
{
foreach ($queries as $query)
{
self::addSqlQueryCondition(
$dsn,
$configKey,
$query['sql'],
$query['fetchStyle'],
$query['columnIndex'],
Expand Down
2 changes: 1 addition & 1 deletion alpha/apps/kaltura/lib/db/DbManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ private static function getSphinxConnectionInternal($key, $connectTimeout)
throw new Exception("DB Config [$key] not found");

$dataSource = self::$config['datasources'][$key]['connection']['dsn'];
self::$sphinxConnection = new KalturaPDO($dataSource, null, null, array(PDO::ATTR_TIMEOUT => $connectTimeout, KalturaPDO::KALTURA_ATTR_NAME => $key));
self::$sphinxConnection = new KalturaPDO($dataSource, null, null, array(PDO::ATTR_TIMEOUT => $connectTimeout, KalturaPDO::KALTURA_ATTR_NAME => $key), $key);
self::$sphinxConnection->setCommentsEnabled(false);

return self::$sphinxConnection;
Expand Down
10 changes: 5 additions & 5 deletions alpha/apps/kaltura/lib/db/KalturaPDO.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ class KalturaPDO extends PropelPDO
const KALTURA_ATTR_NO_TRANSACTION = 'noTransaction';

protected static $comment = null;
protected $dsn = null;
protected $kalturaOptions = array();
protected $connectionName = null;
protected $hostName = null;
protected $enableComments = true;
protected $configKey = null;

/* (non-PHPdoc)
* @see PDO::__construct()
*/
public function __construct($dsn, $username = null, $password = null, $driver_options = array())
public function __construct($dsn, $username = null, $password = null, $driver_options = array(), $config_key = null)
{
if(isset($driver_options[KalturaPDO::KALTURA_ATTR_NAME]))
{
Expand All @@ -45,8 +45,8 @@ public function __construct($dsn, $username = null, $password = null, $driver_op
break;
}
}
$this->dsn = $dsn;

$this->configKey = $config_key;
$connStart = microtime(true);

parent::__construct($dsn, $username, $password, $driver_options);
Expand Down Expand Up @@ -148,7 +148,7 @@ public function queryAndFetchAll($sql, $fetchStyle, $columnIndex = 0, $filter =

$filteredResult = kApiCache::filterQueryResult($result, $filter);

kApiCache::addSqlQueryCondition($this->dsn, $sql, $fetchStyle, $columnIndex, $filter, $filteredResult);
kApiCache::addSqlQueryCondition($this->configKey, $sql, $fetchStyle, $columnIndex, $filter, $filteredResult);

return $filteredResult;
}
Expand Down

0 comments on commit 24c6425

Please sign in to comment.