Skip to content

Commit

Permalink
Merge pull request #95 from talis/TENT-1359_ReadPreferenceForSecondar…
Browse files Browse the repository at this point in the history
…yImprovements

Composites should listen to read preference.
  • Loading branch information
malcyL committed Sep 10, 2015
2 parents ecbcd99 + 8f16912 commit db2ecad
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 18 deletions.
12 changes: 9 additions & 3 deletions src/mongo/Driver.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,10 @@ public function __construct($podName, $storeName, $opts=array())

// is a custom stat tracker passed in?
if ($opts['stat']!=null) $this->stat = $opts['stat'];

// Set the read preference if passed in
if ($opts['readPreference']) $this->readPreference = $opts['readPreference'];

}

/**
Expand Down Expand Up @@ -546,7 +550,8 @@ public function getTripodViews()
$this->storeName,
$this->collection,
$this->defaultContext,
$this->stat
$this->stat,
$this->readPreference
);
}
return $this->tripod_views;
Expand All @@ -563,7 +568,8 @@ public function getTripodTables()
$this->storeName,
$this->collection,
$this->defaultContext,
$this->stat
$this->stat,
$this->readPreference
);
}
return $this->tripod_tables;
Expand All @@ -576,7 +582,7 @@ public function getSearchIndexer()
{
if ($this->search_indexer==null)
{
$this->search_indexer = new \Tripod\Mongo\Composites\SearchIndexer($this);
$this->search_indexer = new \Tripod\Mongo\Composites\SearchIndexer($this, $this->readPreference);
}
return $this->search_indexer;
}
Expand Down
4 changes: 3 additions & 1 deletion src/mongo/delegates/SearchDocuments.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,17 @@ class SearchDocuments extends DriverBase
* @param \MongoCollection $collection
* @param string $defaultContext
* @param \Tripod\ITripodStat|null $stat
* @param string $readPreference
*/
public function __construct($storeName, \MongoCollection $collection, $defaultContext, $stat=null)
public function __construct($storeName, \MongoCollection $collection, $defaultContext, $stat=null , $readPreference=\MongoClient::RP_PRIMARY)
{
$this->labeller = new Labeller();
$this->storeName = $storeName;
$this->collection = $collection;
$this->podName = $collection->getName();
$this->defaultContext = $defaultContext;
$this->stat = $stat;
$this->readPreference = $readPreference;
}

/**
Expand Down
5 changes: 3 additions & 2 deletions src/mongo/delegates/SearchIndexer.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,10 @@ class SearchIndexer extends CompositeBase

/**
* @param \Tripod\Mongo\Driver $tripod
* @param string $readPreference
* @throws \Tripod\Exceptions\SearchException
*/
public function __construct(\Tripod\Mongo\Driver $tripod)
public function __construct(\Tripod\Mongo\Driver $tripod, $readPreference=\MongoClient::RP_PRIMARY)
{
$this->tripod = $tripod;
$this->storeName = $tripod->getStoreName();
Expand All @@ -46,7 +47,7 @@ public function __construct(\Tripod\Mongo\Driver $tripod)
} else {
throw new \Tripod\Exceptions\SearchException("Did not recognise Search Provider, or could not find class: $provider");
}
$this->readPreference = \MongoClient::RP_PRIMARY; // todo: figure out where this should go.
$this->readPreference = $readPreference;
}

/**
Expand Down
11 changes: 5 additions & 6 deletions src/mongo/delegates/Tables.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,10 @@ class Tables extends CompositeBase
* @param \MongoCollection $collection
* @param string $defaultContext
* @param \Tripod\ITripodStat|null $stat
* @param string $readPreference
* todo: MongoCollection -> podName
*/
public function __construct($storeName,\MongoCollection $collection,$defaultContext,$stat=null)
public function __construct($storeName,\MongoCollection $collection,$defaultContext,$stat=null,$readPreference=\MongoClient::RP_PRIMARY)
{
$this->labeller = new Labeller();
$this->storeName = $storeName;
Expand All @@ -82,7 +83,7 @@ public function __construct($storeName,\MongoCollection $collection,$defaultCont
$this->config = Config::getInstance();
$this->defaultContext = $this->labeller->uri_to_alias($defaultContext); // make sure default context is qnamed if applicable
$this->stat = $stat;
$this->readPreference = \MongoClient::RP_PRIMARY; // todo: figure out where this should go.
$this->readPreference = $readPreference;
}

/**
Expand Down Expand Up @@ -243,7 +244,7 @@ public function getTableRows($tableSpecId,$filter=array(),$sortBy=array(),$offse

$filter["_id." . _ID_TYPE] = $tableSpecId;

$collection = $this->config->getCollectionForTable($this->storeName, $tableSpecId);
$collection = $this->config->getCollectionForTable($this->storeName, $tableSpecId, $this->readPreference);
$results = (empty($limit)) ? $collection->find($filter) : $collection->find($filter)->skip($offset)->limit($limit);
if (isset($sortBy))
{
Expand All @@ -252,8 +253,6 @@ public function getTableRows($tableSpecId,$filter=array(),$sortBy=array(),$offse
$rows = array();
foreach ($results as $doc)
{
// $log = new \Monolog\Logger("TEST");
// $log->error("Doc",$doc);
if (array_key_exists(_IMPACT_INDEX,$doc['value'])) unset($doc['value'][_IMPACT_INDEX]); // remove impact index from client
$rows[] = $doc['value'];
}
Expand Down Expand Up @@ -286,7 +285,7 @@ public function distinct($tableSpecId, $fieldName, array $filter=array())

$filter['_id.'._ID_TYPE] = $tableSpecId;

$collection = $this->config->getCollectionForTable($this->storeName, $tableSpecId);
$collection = $this->config->getCollectionForTable($this->storeName, $tableSpecId, $this->readPreference);
$results = $collection->distinct($fieldName, $filter);

$t->stop();
Expand Down
13 changes: 7 additions & 6 deletions src/mongo/delegates/Views.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ class Views extends CompositeBase
* @param \MongoCollection $collection
* @param $defaultContext
* @param null $stat
* @param string $readPreference
*/
function __construct($storeName, \MongoCollection $collection,$defaultContext,$stat=null) // todo: $collection -> podname
function __construct($storeName, \MongoCollection $collection,$defaultContext,$stat=null,$readPreference=\MongoClient::RP_PRIMARY) // todo: $collection -> podname
{
$this->storeName = $storeName;
$this->labeller = new Labeller();
Expand All @@ -32,7 +33,7 @@ function __construct($storeName, \MongoCollection $collection,$defaultContext,$s
$this->defaultContext = $defaultContext;
$this->config = Config::getInstance();
$this->stat = $stat;
$this->readPreference = \MongoClient::RP_PRIMARY; // todo: figure out where this should go.
$this->readPreference = $readPreference;
}

/**
Expand Down Expand Up @@ -122,7 +123,7 @@ public function getSpecification($storeName, $viewSpecId)
* @param $viewType
* @return \Tripod\Mongo\MongoGraph
*/
public function getViews(Array $filter,$viewType)
public function getViews(Array $filter, $viewType)
{
$query = array("_id.type"=>$viewType);
foreach ($filter as $predicate=>$object)
Expand All @@ -141,7 +142,7 @@ public function getViews(Array $filter,$viewType)
$query['value.'._GRAPHS.'.'.$predicate] = $object;
}
}
$viewCollection = $this->getConfigInstance()->getCollectionForView($this->storeName, $viewType);
$viewCollection = $this->getConfigInstance()->getCollectionForView($this->storeName, $viewType, $this->readPreference);
return $this->fetchGraph($query,MONGO_VIEW,$viewCollection);
}

Expand All @@ -162,7 +163,7 @@ public function getViewForResource($resource,$viewType,$context=null)
$contextAlias = $this->getContextAlias($context);

$query = array( "_id" => array("r"=>$resourceAlias,"c"=>$contextAlias,"type"=>$viewType));
$viewCollection = $this->config->getCollectionForView($this->storeName, $viewType);
$viewCollection = $this->config->getCollectionForView($this->storeName, $viewType, $this->readPreference);
$graph = $this->fetchGraph($query,MONGO_VIEW,$viewCollection);
if ($graph->is_empty())
{
Expand Down Expand Up @@ -287,7 +288,7 @@ public function generateViews($resources,$context=null)
foreach (Config::getInstance()->getViewSpecifications($this->storeName) as $type=>$spec)
{
if($spec['from']==$this->podName){
$this->config->getCollectionForView($this->storeName, $type)
$this->config->getCollectionForView($this->storeName, $type, $this->readPreference)
->remove(array("_id" => array("r"=>$resourceAlias,"c"=>$contextAlias,"type"=>$type)));
}
}
Expand Down

0 comments on commit db2ecad

Please sign in to comment.