Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

try all nodes in the connection string before sending back a failure #1

Merged
merged 1 commit into from
Apr 27, 2022
Merged
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
64 changes: 35 additions & 29 deletions Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,39 +173,45 @@ public function open()
*/
protected function populateNodes()
{
$node = reset($this->nodes);
$host = $node['http_address'];
$protocol = isset($node['protocol']) ? $node['protocol'] : $this->defaultProtocol;
if (strncmp($host, 'inet[/', 6) === 0) {
$host = substr($host, 6, -1);
}
$response = $this->httpRequest('GET', "$protocol://$host/_nodes/_all/http");
if (!empty($response['nodes'])) {
$nodes = $response['nodes'];
} else {
$nodes = [];
}
foreach ($this->nodes as $node) {
$host = $node['http_address'];
$protocol = isset($node['protocol']) ? $node['protocol'] : $this->defaultProtocol;
if (strncmp($host, 'inet[/', 6) === 0) {
$host = substr($host, 6, -1);
}

foreach ($nodes as $key => &$node) {
// Make sure that nodes have an 'http_address' property, which is not the case if you're using AWS
// Elasticsearch service (at least as of Oct., 2015). - TO BE VERIFIED
// Temporary workaround - simply ignore all invalid nodes
if (!isset($node['http']['publish_address'])) {
unset($nodes[$key]);
try {
$response = $this->httpRequest('GET', "$protocol://$host/_nodes/_all/http");
} catch (Exception $e) {
continue;
}
if (!empty($response['nodes'])) {
$nodes = $response['nodes'];
} else {
$nodes = [];
}
$node['http_address'] = $node['http']['publish_address'];

// Protocol is not a standard ES node property, so we add it manually
$node['protocol'] = $this->defaultProtocol;
}
foreach ($nodes as $key => &$node) {
// Make sure that nodes have an 'http_address' property, which is not the case if you're using AWS
// Elasticsearch service (at least as of Oct., 2015). - TO BE VERIFIED
// Temporary workaround - simply ignore all invalid nodes
if (!isset($node['http']['publish_address'])) {
unset($nodes[$key]);
}
$node['http_address'] = $node['http']['publish_address'];

if (!empty($nodes)) {
$this->nodes = array_values($nodes);
} else {
curl_close($this->_curl);
throw new Exception('Cluster autodetection did not find any active node. Make sure a GET /_nodes reguest on the hosts defined in the config returns the "http_address" field for each node.');
}
}
// Protocol is not a standard ES node property, so we add it manually
$node['protocol'] = $this->defaultProtocol;
}

if (!empty($nodes)) {
$this->nodes = array_values($nodes);
return;
}
}
curl_close($this->_curl);
throw new Exception('Cluster autodetection did not find any active node. Make sure a GET /_nodes reguest on the hosts defined in the config returns the "http_address" field for each node.');
}

/**
* select active node randomly
Expand Down