From e560a942f1d9afc210c750bb14c7b2fcfc50ebb8 Mon Sep 17 00:00:00 2001 From: David Patzke Date: Mon, 30 Nov 2015 15:17:00 +0100 Subject: [PATCH 1/4] added super simple SolrCloud support --- lib/ARP/SolrClient2/SolrCore.php | 63 +++++++++++++++++++++++++++----- 1 file changed, 53 insertions(+), 10 deletions(-) diff --git a/lib/ARP/SolrClient2/SolrCore.php b/lib/ARP/SolrClient2/SolrCore.php index f295d3e..12363bc 100644 --- a/lib/ARP/SolrClient2/SolrCore.php +++ b/lib/ARP/SolrClient2/SolrCore.php @@ -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 = 10240; protected $content = ''; - + protected $zHosts = array(); + const $availableModes = ['standalone','cloud'] /** * Constructor. * @param array $options Options. @@ -52,6 +54,9 @@ 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'], $availableModes)) ? $options['mode'] : 'standalone'; + $this->zHosts = isset($this->mode) + } $this->version = isset($options['version']) ? (int)$options['version'] : 4; @@ -320,18 +325,56 @@ protected function appendToFilter($string, $cached = true) */ private function generateURL($path = '') { - 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 noode/shard, zookeeper will do the rest. So we write for lb to a random node/shard + // and if it not available we try to pick a other node + // I´m not sure if we need a real zookeepr client implementation here but i don think so + rnd = rand(0, count($this->$zHosts)); + shard = 'http://' + . $this->shards[rnd] + . ($this->port === null ?: ':' . $this->port) + . ($this->path === null ?: '/' . $this->path) + . ($this->core === null ?: '/' . $this->core); + + + if(isAvailable(shard)){ + + return shard.path + + } + else { + + generateURL($this->$path) + } - return 'http://' - . $this->host - . ($this->port === null ?: ':' . $this->port) - . ($this->path === null ?: '/' . $this->path) - . ($this->core === null ?: '/' . $this->core) - . ($path == '' ?: '/' . $path); + } } + public function isAvailable(url){ + + $json = file_get_contents(url.'/admin/ping?wt=json'); + $data = json_decode($json); + if(data['status'] === 'Ok'){ + return true; + } else { + return false; + } + + } + + /** * @param $content * @param bool $checkStatus From 5364761d06c9e023adb09b3a8772181218d28db8 Mon Sep 17 00:00:00 2001 From: David Patzke Date: Thu, 3 Dec 2015 16:13:29 +0100 Subject: [PATCH 2/4] Bug fixes --- lib/ARP/SolrClient2/SolrCore.php | 39 ++++++++++++++++---------------- 1 file changed, 20 insertions(+), 19 deletions(-) diff --git a/lib/ARP/SolrClient2/SolrCore.php b/lib/ARP/SolrClient2/SolrCore.php index 12363bc..e4cc199 100644 --- a/lib/ARP/SolrClient2/SolrCore.php +++ b/lib/ARP/SolrClient2/SolrCore.php @@ -21,8 +21,8 @@ class SolrCore extends CurlBrowser protected $cache; protected $cacheSize = 10240; protected $content = ''; - protected $zHosts = array(); - const $availableModes = ['standalone','cloud'] + protected $cloudHosts = array(); + protected $availableModes = array('standalone','cloud'); //Should be final /** * Constructor. * @param array $options Options. @@ -54,8 +54,8 @@ 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'], $availableModes)) ? $options['mode'] : 'standalone'; - $this->zHosts = isset($this->mode) + $this->mode = ((isset($options['mode']) && in_array($options['mode'], $this->availableModes))) ? $options['mode'] : 'standalone'; + $this->cloudHosts = isset($this->mode) ; } @@ -335,40 +335,41 @@ private function generateURL($path = '') . ($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 noode/shard, zookeeper will do the rest. So we write for lb to a random node/shard + . ($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 zookeepr client implementation here but i don think so - rnd = rand(0, count($this->$zHosts)); - shard = 'http://' - . $this->shards[rnd] + // I´m not sure if we need a real zookeeper client implementation here but i don think so + $rnd = rand(0, count($this->cloudHosts)); + $node = 'http://' + . $this->node[$rnd] . ($this->port === null ?: ':' . $this->port) . ($this->path === null ?: '/' . $this->path) . ($this->core === null ?: '/' . $this->core); - if(isAvailable(shard)){ + if(isAvailable($node)){ - return shard.path + return $node.$path; } else { - generateURL($this->$path) + $this->generateURL($this->$path); } } } - public function isAvailable(url){ + private function isAvailable($url){ - $json = file_get_contents(url.'/admin/ping?wt=json'); + $json = file_get_contents($url.'/admin/ping?wt=json'); $data = json_decode($json); - if(data['status'] === 'Ok'){ + if($data['status'] === 'Ok'){ return true; - } else { + } + else { return false; } From 0eed09af3820e612dbf28cfbbbf3a335012d887b Mon Sep 17 00:00:00 2001 From: David Patzke Date: Tue, 8 Dec 2015 10:14:22 +0100 Subject: [PATCH 3/4] Fixes: Client is now working with SolrCloud but need some improvment --- lib/ARP/SolrClient2/SolrCore.php | 34 +++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/lib/ARP/SolrClient2/SolrCore.php b/lib/ARP/SolrClient2/SolrCore.php index e4cc199..deb7907 100644 --- a/lib/ARP/SolrClient2/SolrCore.php +++ b/lib/ARP/SolrClient2/SolrCore.php @@ -55,7 +55,10 @@ public function options($options) $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'; - $this->cloudHosts = isset($this->mode) ; + if($this->mode === "cloud"){ + $this->cloudHosts = isset($options['cloudHosts']) ? $options['cloudHosts']: 'localhost:8983'; + $this->port = ''; + } } @@ -323,7 +326,7 @@ protected function appendToFilter($string, $cached = true) * @param string $path * @return string */ - private function generateURL($path = '') + private function generateURL($path = '', $alive = 0) { if($this->mode === "standalone") { if ($this->url !== null) { @@ -341,22 +344,28 @@ private function generateURL($path = '') // 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)); + $rnd = rand(0, count($this->cloudHosts)-1); $node = 'http://' - . $this->node[$rnd] - . ($this->port === null ?: ':' . $this->port) + . $this->cloudHosts[$rnd] . ($this->path === null ?: '/' . $this->path) . ($this->core === null ?: '/' . $this->core); - if(isAvailable($node)){ - - return $node.$path; + if($this->isAvailable($node)){ + + + return $node."/".$path; } else { - - $this->generateURL($this->$path); + if($alive <10){ + $alive++; + $this->generateURL($path, $alive); + } + else{ + + die("timed out after 10 attempts"); + } } } @@ -365,8 +374,9 @@ private function generateURL($path = '') private function isAvailable($url){ $json = file_get_contents($url.'/admin/ping?wt=json'); - $data = json_decode($json); - if($data['status'] === 'Ok'){ + + $data = json_decode($json, true); + if($data['status'] === 'OK'){ return true; } else { From b1399d53939c057c9bfa6e6393efc2360b9123c7 Mon Sep 17 00:00:00 2001 From: David Patzke Date: Mon, 22 Aug 2016 12:27:23 +0200 Subject: [PATCH 4/4] Add setter for mode --- lib/ARP/SolrClient2/SolrCore.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/lib/ARP/SolrClient2/SolrCore.php b/lib/ARP/SolrClient2/SolrCore.php index deb7907..55e96ee 100644 --- a/lib/ARP/SolrClient2/SolrCore.php +++ b/lib/ARP/SolrClient2/SolrCore.php @@ -90,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