Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 75 additions & 11 deletions lib/ARP/SolrClient2/SolrCore.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ class SolrCore extends CurlBrowser
protected $core = null;
protected $url = null;
protected $version = null;
protected $mode = null;
protected $params = array();
protected $cache;
protected $cacheSize = 1048576;
protected $content = '';

protected $cloudHosts = array();
protected $availableModes = array('standalone','cloud'); //Should be final
/**
* Constructor.
* @param array $options Options.
Expand Down Expand Up @@ -52,6 +54,12 @@ public function options($options)
$this->port = isset($options['port']) ? $options['port'] : 8080;
$this->path = isset($options['path']) ? $options['path'] : 'solr';
$this->core = isset($options['core']) ? $options['core'] : '';
$this->mode = ((isset($options['mode']) && in_array($options['mode'], $this->availableModes))) ? $options['mode'] : 'standalone';
if($this->mode === "cloud"){
$this->cloudHosts = isset($options['cloudHosts']) ? $options['cloudHosts']: 'localhost:8983';
$this->port = '';
}

}

$this->version = isset($options['version']) ? (int)$options['version'] : 4;
Expand Down Expand Up @@ -82,6 +90,16 @@ public function url($url)
return $this;
}

/**
* @param $mode
* @return $this
*/
public function mode($mode)
{
$this->mode = $mode;
return $this;
}

/**
* @param $host
* @return $this
Expand Down Expand Up @@ -318,20 +336,66 @@ protected function appendToFilter($string, $cached = true)
* @param string $path
* @return string
*/
private function generateURL($path = '')
private function generateURL($path = '', $alive = 0)
{
if ($this->url !== null) {
return $this->url;
}
if($this->mode === "standalone") {
if ($this->url !== null) {
return $this->url;
}

return 'http://'
. $this->host
. ($this->port === null ?: ':' . $this->port)
. ($this->path === null ?: '/' . $this->path)
. ($this->core === null ?: '/' . $this->core)
. ($path == '' ?: '/' . $path);
}
else if($this->mode === "cloud"){
// SolrCloud docu says you could write to any node, zookeeper will do the rest. So we write for lb to a random node
// and if it not available we try to pick a other node
// I´m not sure if we need a real zookeeper client implementation here but i don think so
$rnd = rand(0, count($this->cloudHosts)-1);
$node = 'http://'
. $this->cloudHosts[$rnd]
. ($this->path === null ?: '/' . $this->path)
. ($this->core === null ?: '/' . $this->core);


if($this->isAvailable($node)){


return $node."/".$path;

return 'http://'
. $this->host
. ($this->port === null ?: ':' . $this->port)
. ($this->path === null ?: '/' . $this->path)
. ($this->core === null ?: '/' . $this->core)
. ($path == '' ?: '/' . $path);
}
else {
if($alive <10){
$alive++;
$this->generateURL($path, $alive);
}
else{

die("timed out after 10 attempts");
}
}

}
}

private function isAvailable($url){

$json = file_get_contents($url.'/admin/ping?wt=json');

$data = json_decode($json, true);
if($data['status'] === 'OK'){
return true;
}
else {
return false;
}

}


/**
* @param $content
* @param bool $checkStatus
Expand Down