private function connectToReplSetPrimary()
{
if ($this->primaryProtocol) {
return $this->primaryProtocol;
}
// Replace the seed host list with the correct host list from the replSet itself
// TODO: Chicken/egg problem. We should really be getting this list *from* the PRIMARY.
// The MongoDB driver specification docs suggest, perhaps, doing a second query
// to make sure the PRIMARY believes it is the PRIMARY and to verify the host
// list, but I'm not sure if we should spend the IO time on that second call.
$this->hosts = $this->retrieveReplSetHosts();
foreach ($this->hosts as $host_info) {
if (isset($host_info['state']) && $host_info['state'] === static::STATE_PRIMARY) {
$this->primaryProtocol = $this->connectToHost($host_info['host'], $host_info['port']);
return $this->primaryProtocol;
}
}
throw new \MongoException("Could not determine PRIMARY member from replSet host list.");
}